blob: e4838deb3a214d5d4f8baf79c877694bb5a1c3ca [file] [log] [blame]
Alexey Bataev9959db52014-05-06 10:08:46 +00001//===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===//
Alexey Bataeva769e072013-03-22 06:34:35 +00002//
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 Bataev6f6f3b42013-05-13 04:18:18 +000011/// clauses.
Alexey Bataeva769e072013-03-22 06:34:35 +000012///
13//===----------------------------------------------------------------------===//
14
Alexey Bataev9959db52014-05-06 10:08:46 +000015#include "clang/AST/ASTContext.h"
Alexey Bataev97720002014-11-11 04:05:39 +000016#include "clang/AST/ASTMutationListener.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000017#include "clang/AST/Decl.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000018#include "clang/AST/DeclCXX.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000019#include "clang/AST/DeclOpenMP.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000020#include "clang/AST/StmtCXX.h"
21#include "clang/AST/StmtOpenMP.h"
22#include "clang/AST/StmtVisitor.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000023#include "clang/Basic/OpenMPKinds.h"
24#include "clang/Lex/Preprocessor.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000025#include "clang/Sema/Initialization.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000026#include "clang/Sema/Lookup.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000027#include "clang/Sema/Scope.h"
28#include "clang/Sema/ScopeInfo.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000029#include "clang/Sema/SemaInternal.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000030using namespace clang;
31
Alexey Bataev758e55e2013-09-06 18:03:48 +000032//===----------------------------------------------------------------------===//
33// Stack of data-sharing attributes for variables
34//===----------------------------------------------------------------------===//
35
36namespace {
37/// \brief Default data sharing attributes, which can be applied to directive.
38enum DefaultDataSharingAttributes {
Alexey Bataeved09d242014-05-28 05:53:51 +000039 DSA_unspecified = 0, /// \brief Data sharing attribute not specified.
40 DSA_none = 1 << 0, /// \brief Default data sharing attribute 'none'.
41 DSA_shared = 1 << 1 /// \brief Default data sharing attribute 'shared'.
Alexey Bataev758e55e2013-09-06 18:03:48 +000042};
Alexey Bataev7ff55242014-06-19 09:13:45 +000043
Alexey Bataevf29276e2014-06-18 04:14:57 +000044template <class T> struct MatchesAny {
Alexey Bataev23b69422014-06-18 07:08:49 +000045 explicit MatchesAny(ArrayRef<T> Arr) : Arr(std::move(Arr)) {}
Alexey Bataevf29276e2014-06-18 04:14:57 +000046 bool operator()(T Kind) {
47 for (auto KindEl : Arr)
48 if (KindEl == Kind)
49 return true;
50 return false;
51 }
52
53private:
54 ArrayRef<T> Arr;
55};
Alexey Bataev23b69422014-06-18 07:08:49 +000056struct MatchesAlways {
Alexey Bataevf29276e2014-06-18 04:14:57 +000057 MatchesAlways() {}
Alexey Bataev7ff55242014-06-19 09:13:45 +000058 template <class T> bool operator()(T) { return true; }
Alexey Bataevf29276e2014-06-18 04:14:57 +000059};
60
61typedef MatchesAny<OpenMPClauseKind> MatchesAnyClause;
62typedef MatchesAny<OpenMPDirectiveKind> MatchesAnyDirective;
Alexey Bataev758e55e2013-09-06 18:03:48 +000063
64/// \brief Stack for tracking declarations used in OpenMP directives and
65/// clauses and their data-sharing attributes.
66class DSAStackTy {
67public:
68 struct DSAVarData {
69 OpenMPDirectiveKind DKind;
70 OpenMPClauseKind CKind;
71 DeclRefExpr *RefExpr;
Alexey Bataevbae9a792014-06-27 10:37:06 +000072 SourceLocation ImplicitDSALoc;
73 DSAVarData()
74 : DKind(OMPD_unknown), CKind(OMPC_unknown), RefExpr(nullptr),
75 ImplicitDSALoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000076 };
Alexey Bataeved09d242014-05-28 05:53:51 +000077
Alexey Bataev758e55e2013-09-06 18:03:48 +000078private:
79 struct DSAInfo {
80 OpenMPClauseKind Attributes;
81 DeclRefExpr *RefExpr;
82 };
83 typedef llvm::SmallDenseMap<VarDecl *, DSAInfo, 64> DeclSAMapTy;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000084 typedef llvm::SmallDenseMap<VarDecl *, DeclRefExpr *, 64> AlignedMapTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +000085
86 struct SharingMapTy {
87 DeclSAMapTy SharingMap;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000088 AlignedMapTy AlignedMap;
Alexey Bataev758e55e2013-09-06 18:03:48 +000089 DefaultDataSharingAttributes DefaultAttr;
Alexey Bataevbae9a792014-06-27 10:37:06 +000090 SourceLocation DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +000091 OpenMPDirectiveKind Directive;
92 DeclarationNameInfo DirectiveName;
93 Scope *CurScope;
Alexey Bataevbae9a792014-06-27 10:37:06 +000094 SourceLocation ConstructLoc;
Alexey Bataev9fb6e642014-07-22 06:45:04 +000095 bool OrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +000096 SourceLocation InnerTeamsRegionLoc;
Alexey Bataeved09d242014-05-28 05:53:51 +000097 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataevbae9a792014-06-27 10:37:06 +000098 Scope *CurScope, SourceLocation Loc)
Alexander Musmanf0d76e72014-05-29 14:36:25 +000099 : SharingMap(), AlignedMap(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000100 Directive(DKind), DirectiveName(std::move(Name)), CurScope(CurScope),
Alexey Bataev13314bf2014-10-09 04:18:56 +0000101 ConstructLoc(Loc), OrderedRegion(false), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000102 SharingMapTy()
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000103 : SharingMap(), AlignedMap(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000104 Directive(OMPD_unknown), DirectiveName(), CurScope(nullptr),
Alexey Bataev13314bf2014-10-09 04:18:56 +0000105 ConstructLoc(), OrderedRegion(false), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000106 };
107
108 typedef SmallVector<SharingMapTy, 64> StackTy;
109
110 /// \brief Stack of used declaration and their data-sharing attributes.
111 StackTy Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000112 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000113
114 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
115
116 DSAVarData getDSA(StackTy::reverse_iterator Iter, VarDecl *D);
Alexey Bataevec3da872014-01-31 05:15:34 +0000117
118 /// \brief Checks if the variable is a local for OpenMP region.
119 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
Alexey Bataeved09d242014-05-28 05:53:51 +0000120
Alexey Bataev758e55e2013-09-06 18:03:48 +0000121public:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000122 explicit DSAStackTy(Sema &S) : Stack(1), SemaRef(S) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000123
124 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000125 Scope *CurScope, SourceLocation Loc) {
126 Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc));
127 Stack.back().DefaultAttrLoc = Loc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000128 }
129
130 void pop() {
131 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
132 Stack.pop_back();
133 }
134
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000135 /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000136 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000137 /// for diagnostics.
138 DeclRefExpr *addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE);
139
Alexey Bataev758e55e2013-09-06 18:03:48 +0000140 /// \brief Adds explicit data sharing attribute to the specified declaration.
141 void addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A);
142
Alexey Bataev758e55e2013-09-06 18:03:48 +0000143 /// \brief Returns data sharing attributes from top of the stack for the
144 /// specified declaration.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000145 DSAVarData getTopDSA(VarDecl *D, bool FromParent);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000146 /// \brief Returns data-sharing attributes for the specified declaration.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000147 DSAVarData getImplicitDSA(VarDecl *D, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000148 /// \brief Checks if the specified variables has data-sharing attributes which
149 /// match specified \a CPred predicate in any directive which matches \a DPred
150 /// predicate.
151 template <class ClausesPredicate, class DirectivesPredicate>
152 DSAVarData hasDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000153 DirectivesPredicate DPred, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000154 /// \brief Checks if the specified variables has data-sharing attributes which
155 /// match specified \a CPred predicate in any innermost directive which
156 /// matches \a DPred predicate.
157 template <class ClausesPredicate, class DirectivesPredicate>
158 DSAVarData hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000159 DirectivesPredicate DPred,
160 bool FromParent);
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000161 /// \brief Finds a directive which matches specified \a DPred predicate.
162 template <class NamedDirectivesPredicate>
163 bool hasDirective(NamedDirectivesPredicate DPred, bool FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000164
Alexey Bataev758e55e2013-09-06 18:03:48 +0000165 /// \brief Returns currently analyzed directive.
166 OpenMPDirectiveKind getCurrentDirective() const {
167 return Stack.back().Directive;
168 }
Alexey Bataev549210e2014-06-24 04:39:47 +0000169 /// \brief Returns parent directive.
170 OpenMPDirectiveKind getParentDirective() const {
171 if (Stack.size() > 2)
172 return Stack[Stack.size() - 2].Directive;
173 return OMPD_unknown;
174 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000175
176 /// \brief Set default data sharing attribute to none.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000177 void setDefaultDSANone(SourceLocation Loc) {
178 Stack.back().DefaultAttr = DSA_none;
179 Stack.back().DefaultAttrLoc = Loc;
180 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000181 /// \brief Set default data sharing attribute to shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000182 void setDefaultDSAShared(SourceLocation Loc) {
183 Stack.back().DefaultAttr = DSA_shared;
184 Stack.back().DefaultAttrLoc = Loc;
185 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000186
187 DefaultDataSharingAttributes getDefaultDSA() const {
188 return Stack.back().DefaultAttr;
189 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000190 SourceLocation getDefaultDSALocation() const {
191 return Stack.back().DefaultAttrLoc;
192 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000193
Alexey Bataevf29276e2014-06-18 04:14:57 +0000194 /// \brief Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000195 bool isThreadPrivate(VarDecl *D) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000196 DSAVarData DVar = getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000197 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000198 }
199
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000200 /// \brief Marks current region as ordered (it has an 'ordered' clause).
201 void setOrderedRegion(bool IsOrdered = true) {
202 Stack.back().OrderedRegion = IsOrdered;
203 }
204 /// \brief Returns true, if parent region is ordered (has associated
205 /// 'ordered' clause), false - otherwise.
206 bool isParentOrderedRegion() const {
207 if (Stack.size() > 2)
208 return Stack[Stack.size() - 2].OrderedRegion;
209 return false;
210 }
211
Alexey Bataev13314bf2014-10-09 04:18:56 +0000212 /// \brief Marks current target region as one with closely nested teams
213 /// region.
214 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
215 if (Stack.size() > 2)
216 Stack[Stack.size() - 2].InnerTeamsRegionLoc = TeamsRegionLoc;
217 }
218 /// \brief Returns true, if current region has closely nested teams region.
219 bool hasInnerTeamsRegion() const {
220 return getInnerTeamsRegionLoc().isValid();
221 }
222 /// \brief Returns location of the nested teams region (if any).
223 SourceLocation getInnerTeamsRegionLoc() const {
224 if (Stack.size() > 1)
225 return Stack.back().InnerTeamsRegionLoc;
226 return SourceLocation();
227 }
228
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000229 Scope *getCurScope() const { return Stack.back().CurScope; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000230 Scope *getCurScope() { return Stack.back().CurScope; }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000231 SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000232};
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000233bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
234 return isOpenMPParallelDirective(DKind) || DKind == OMPD_task ||
Alexey Bataev13314bf2014-10-09 04:18:56 +0000235 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000236}
Alexey Bataeved09d242014-05-28 05:53:51 +0000237} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000238
239DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter,
240 VarDecl *D) {
241 DSAVarData DVar;
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000242 if (Iter == std::prev(Stack.rend())) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000243 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
244 // in a region but not in construct]
245 // File-scope or namespace-scope variables referenced in called routines
246 // in the region are shared unless they appear in a threadprivate
247 // directive.
Alexey Bataev8b9cb982014-07-24 02:33:58 +0000248 if (!D->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D))
Alexey Bataev750a58b2014-03-18 12:19:12 +0000249 DVar.CKind = OMPC_shared;
250
251 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
252 // in a region but not in construct]
253 // Variables with static storage duration that are declared in called
254 // routines in the region are shared.
255 if (D->hasGlobalStorage())
256 DVar.CKind = OMPC_shared;
257
Alexey Bataev758e55e2013-09-06 18:03:48 +0000258 return DVar;
259 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000260
Alexey Bataev758e55e2013-09-06 18:03:48 +0000261 DVar.DKind = Iter->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +0000262 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
263 // in a Construct, C/C++, predetermined, p.1]
264 // Variables with automatic storage duration that are declared in a scope
265 // inside the construct are private.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000266 if (isOpenMPLocal(D, Iter) && D->isLocalVarDecl() &&
267 (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) {
268 DVar.CKind = OMPC_private;
269 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000270 }
271
Alexey Bataev758e55e2013-09-06 18:03:48 +0000272 // Explicitly specified attributes and local variables with predetermined
273 // attributes.
274 if (Iter->SharingMap.count(D)) {
275 DVar.RefExpr = Iter->SharingMap[D].RefExpr;
276 DVar.CKind = Iter->SharingMap[D].Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000277 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000278 return DVar;
279 }
280
281 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
282 // in a Construct, C/C++, implicitly determined, p.1]
283 // In a parallel or task construct, the data-sharing attributes of these
284 // variables are determined by the default clause, if present.
285 switch (Iter->DefaultAttr) {
286 case DSA_shared:
287 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000288 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000289 return DVar;
290 case DSA_none:
291 return DVar;
292 case DSA_unspecified:
293 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
294 // in a Construct, implicitly determined, p.2]
295 // In a parallel construct, if no default clause is present, these
296 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000297 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000298 if (isOpenMPParallelDirective(DVar.DKind) ||
299 isOpenMPTeamsDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000300 DVar.CKind = OMPC_shared;
301 return DVar;
302 }
303
304 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
305 // in a Construct, implicitly determined, p.4]
306 // In a task construct, if no default clause is present, a variable that in
307 // the enclosing context is determined to be shared by all implicit tasks
308 // bound to the current team is shared.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000309 if (DVar.DKind == OMPD_task) {
310 DSAVarData DVarTemp;
Benjamin Kramer167e9992014-03-02 12:20:24 +0000311 for (StackTy::reverse_iterator I = std::next(Iter),
312 EE = std::prev(Stack.rend());
Alexey Bataev758e55e2013-09-06 18:03:48 +0000313 I != EE; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000314 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
315 // Referenced
Alexey Bataev758e55e2013-09-06 18:03:48 +0000316 // in a Construct, implicitly determined, p.6]
317 // In a task construct, if no default clause is present, a variable
318 // whose data-sharing attribute is not determined by the rules above is
319 // firstprivate.
320 DVarTemp = getDSA(I, D);
321 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000322 DVar.RefExpr = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000323 DVar.DKind = OMPD_task;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000324 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000325 return DVar;
326 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000327 if (isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000328 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000329 }
330 DVar.DKind = OMPD_task;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000331 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000332 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000333 return DVar;
334 }
335 }
336 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
337 // in a Construct, implicitly determined, p.3]
338 // For constructs other than task, if no default clause is present, these
339 // variables inherit their data-sharing attributes from the enclosing
340 // context.
Benjamin Kramer167e9992014-03-02 12:20:24 +0000341 return getDSA(std::next(Iter), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000342}
343
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000344DeclRefExpr *DSAStackTy::addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE) {
345 assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
346 auto It = Stack.back().AlignedMap.find(D);
347 if (It == Stack.back().AlignedMap.end()) {
348 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
349 Stack.back().AlignedMap[D] = NewDE;
350 return nullptr;
351 } else {
352 assert(It->second && "Unexpected nullptr expr in the aligned map");
353 return It->second;
354 }
355 return nullptr;
356}
357
Alexey Bataev758e55e2013-09-06 18:03:48 +0000358void DSAStackTy::addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A) {
359 if (A == OMPC_threadprivate) {
360 Stack[0].SharingMap[D].Attributes = A;
361 Stack[0].SharingMap[D].RefExpr = E;
362 } else {
363 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
364 Stack.back().SharingMap[D].Attributes = A;
365 Stack.back().SharingMap[D].RefExpr = E;
366 }
367}
368
Alexey Bataeved09d242014-05-28 05:53:51 +0000369bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000370 if (Stack.size() > 2) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000371 reverse_iterator I = Iter, E = std::prev(Stack.rend());
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000372 Scope *TopScope = nullptr;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000373 while (I != E && !isParallelOrTaskRegion(I->Directive)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000374 ++I;
375 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000376 if (I == E)
377 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000378 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000379 Scope *CurScope = getCurScope();
380 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000381 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000382 }
383 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000384 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000385 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000386}
387
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000388DSAStackTy::DSAVarData DSAStackTy::getTopDSA(VarDecl *D, bool FromParent) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000389 DSAVarData DVar;
390
391 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
392 // in a Construct, C/C++, predetermined, p.1]
393 // Variables appearing in threadprivate directives are threadprivate.
394 if (D->getTLSKind() != VarDecl::TLS_None) {
395 DVar.CKind = OMPC_threadprivate;
396 return DVar;
397 }
398 if (Stack[0].SharingMap.count(D)) {
399 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
400 DVar.CKind = OMPC_threadprivate;
401 return DVar;
402 }
403
404 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
405 // in a Construct, C/C++, predetermined, p.1]
406 // Variables with automatic storage duration that are declared in a scope
407 // inside the construct are private.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000408 OpenMPDirectiveKind Kind =
409 FromParent ? getParentDirective() : getCurrentDirective();
410 auto StartI = std::next(Stack.rbegin());
411 auto EndI = std::prev(Stack.rend());
412 if (FromParent && StartI != EndI) {
413 StartI = std::next(StartI);
414 }
415 if (!isParallelOrTaskRegion(Kind)) {
Alexey Bataev8b9cb982014-07-24 02:33:58 +0000416 if (isOpenMPLocal(D, StartI) &&
417 ((D->isLocalVarDecl() && (D->getStorageClass() == SC_Auto ||
418 D->getStorageClass() == SC_None)) ||
419 isa<ParmVarDecl>(D))) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000420 DVar.CKind = OMPC_private;
421 return DVar;
Alexander Musman8dba6642014-04-22 13:09:42 +0000422 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000423 }
424
425 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
426 // in a Construct, C/C++, predetermined, p.4]
Alexey Bataevf29276e2014-06-18 04:14:57 +0000427 // Static data members are shared.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000428 if (D->isStaticDataMember()) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000429 // Variables with const-qualified type having no mutable member may be
Alexey Bataevf29276e2014-06-18 04:14:57 +0000430 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000431 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
432 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000433 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
434 return DVar;
435
Alexey Bataev758e55e2013-09-06 18:03:48 +0000436 DVar.CKind = OMPC_shared;
437 return DVar;
438 }
439
440 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataev7ff55242014-06-19 09:13:45 +0000441 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
Alexey Bataev758e55e2013-09-06 18:03:48 +0000442 while (Type->isArrayType()) {
443 QualType ElemType = cast<ArrayType>(Type.getTypePtr())->getElementType();
444 Type = ElemType.getNonReferenceType().getCanonicalType();
445 }
446 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
447 // in a Construct, C/C++, predetermined, p.6]
448 // Variables with const qualified type having no mutable member are
449 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000450 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000451 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000452 if (IsConstant &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000453 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000454 // Variables with const-qualified type having no mutable member may be
455 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000456 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
457 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000458 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
459 return DVar;
460
Alexey Bataev758e55e2013-09-06 18:03:48 +0000461 DVar.CKind = OMPC_shared;
462 return DVar;
463 }
464
465 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
466 // in a Construct, C/C++, predetermined, p.7]
467 // Variables with static storage duration that are declared in a scope
468 // inside the construct are shared.
Alexey Bataevec3da872014-01-31 05:15:34 +0000469 if (D->isStaticLocal()) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000470 DVar.CKind = OMPC_shared;
471 return DVar;
472 }
473
474 // Explicitly specified attributes and local variables with predetermined
475 // attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000476 auto I = std::prev(StartI);
477 if (I->SharingMap.count(D)) {
478 DVar.RefExpr = I->SharingMap[D].RefExpr;
479 DVar.CKind = I->SharingMap[D].Attributes;
480 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000481 }
482
483 return DVar;
484}
485
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000486DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D, bool FromParent) {
487 auto StartI = Stack.rbegin();
488 auto EndI = std::prev(Stack.rend());
489 if (FromParent && StartI != EndI) {
490 StartI = std::next(StartI);
491 }
492 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000493}
494
Alexey Bataevf29276e2014-06-18 04:14:57 +0000495template <class ClausesPredicate, class DirectivesPredicate>
496DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000497 DirectivesPredicate DPred,
498 bool FromParent) {
499 auto StartI = std::next(Stack.rbegin());
500 auto EndI = std::prev(Stack.rend());
501 if (FromParent && StartI != EndI) {
502 StartI = std::next(StartI);
503 }
504 for (auto I = StartI, EE = EndI; I != EE; ++I) {
505 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000506 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000507 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000508 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000509 return DVar;
510 }
511 return DSAVarData();
512}
513
Alexey Bataevf29276e2014-06-18 04:14:57 +0000514template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000515DSAStackTy::DSAVarData
516DSAStackTy::hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
517 DirectivesPredicate DPred, bool FromParent) {
518 auto StartI = std::next(Stack.rbegin());
519 auto EndI = std::prev(Stack.rend());
520 if (FromParent && StartI != EndI) {
521 StartI = std::next(StartI);
522 }
523 for (auto I = StartI, EE = EndI; I != EE; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000524 if (!DPred(I->Directive))
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000525 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000526 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000527 if (CPred(DVar.CKind))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000528 return DVar;
529 return DSAVarData();
530 }
531 return DSAVarData();
532}
533
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000534template <class NamedDirectivesPredicate>
535bool DSAStackTy::hasDirective(NamedDirectivesPredicate DPred, bool FromParent) {
536 auto StartI = std::next(Stack.rbegin());
537 auto EndI = std::prev(Stack.rend());
538 if (FromParent && StartI != EndI) {
539 StartI = std::next(StartI);
540 }
541 for (auto I = StartI, EE = EndI; I != EE; ++I) {
542 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
543 return true;
544 }
545 return false;
546}
547
Alexey Bataev758e55e2013-09-06 18:03:48 +0000548void Sema::InitDataSharingAttributesStack() {
549 VarDataSharingAttributesStack = new DSAStackTy(*this);
550}
551
552#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
553
Alexey Bataeved09d242014-05-28 05:53:51 +0000554void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000555
556void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
557 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000558 Scope *CurScope, SourceLocation Loc) {
559 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000560 PushExpressionEvaluationContext(PotentiallyEvaluated);
561}
562
563void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000564 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
565 // A variable of class type (or array thereof) that appears in a lastprivate
566 // clause requires an accessible, unambiguous default constructor for the
567 // class type, unless the list item is also specified in a firstprivate
568 // clause.
569 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
570 for (auto C : D->clauses()) {
571 if (auto Clause = dyn_cast<OMPLastprivateClause>(C)) {
572 for (auto VarRef : Clause->varlists()) {
573 if (VarRef->isValueDependent() || VarRef->isTypeDependent())
574 continue;
575 auto VD = cast<VarDecl>(cast<DeclRefExpr>(VarRef)->getDecl());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000576 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000577 if (DVar.CKind == OMPC_lastprivate) {
578 SourceLocation ELoc = VarRef->getExprLoc();
579 auto Type = VarRef->getType();
580 if (Type->isArrayType())
581 Type = QualType(Type->getArrayElementTypeNoTypeQual(), 0);
582 CXXRecordDecl *RD =
Alexey Bataev23b69422014-06-18 07:08:49 +0000583 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
584 // FIXME This code must be replaced by actual constructing of the
585 // lastprivate variable.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000586 if (RD) {
587 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
588 PartialDiagnostic PD =
589 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
590 if (!CD ||
591 CheckConstructorAccess(
592 ELoc, CD, InitializedEntity::InitializeTemporary(Type),
593 CD->getAccess(), PD) == AR_inaccessible ||
594 CD->isDeleted()) {
595 Diag(ELoc, diag::err_omp_required_method)
596 << getOpenMPClauseName(OMPC_lastprivate) << 0;
597 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
598 VarDecl::DeclarationOnly;
599 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl
600 : diag::note_defined_here)
601 << VD;
602 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
603 continue;
604 }
605 MarkFunctionReferenced(ELoc, CD);
606 DiagnoseUseOfDecl(CD, ELoc);
607 }
608 }
609 }
610 }
611 }
612 }
613
Alexey Bataev758e55e2013-09-06 18:03:48 +0000614 DSAStack->pop();
615 DiscardCleanupsInEvaluationContext();
616 PopExpressionEvaluationContext();
617}
618
Alexey Bataeva769e072013-03-22 06:34:35 +0000619namespace {
620
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000621class VarDeclFilterCCC : public CorrectionCandidateCallback {
622private:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000623 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +0000624
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000625public:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000626 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +0000627 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000628 NamedDecl *ND = Candidate.getCorrectionDecl();
629 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
630 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000631 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
632 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +0000633 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000634 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000635 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000636};
Alexey Bataeved09d242014-05-28 05:53:51 +0000637} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000638
639ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
640 CXXScopeSpec &ScopeSpec,
641 const DeclarationNameInfo &Id) {
642 LookupResult Lookup(*this, Id, LookupOrdinaryName);
643 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
644
645 if (Lookup.isAmbiguous())
646 return ExprError();
647
648 VarDecl *VD;
649 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +0000650 if (TypoCorrection Corrected = CorrectTypo(
651 Id, LookupOrdinaryName, CurScope, nullptr,
652 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +0000653 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000654 PDiag(Lookup.empty()
655 ? diag::err_undeclared_var_use_suggest
656 : diag::err_omp_expected_var_arg_suggest)
657 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +0000658 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000659 } else {
Richard Smithf9b15102013-08-17 00:46:16 +0000660 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
661 : diag::err_omp_expected_var_arg)
662 << Id.getName();
663 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000664 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000665 } else {
666 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000667 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000668 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
669 return ExprError();
670 }
671 }
672 Lookup.suppressDiagnostics();
673
674 // OpenMP [2.9.2, Syntax, C/C++]
675 // Variables must be file-scope, namespace-scope, or static block-scope.
676 if (!VD->hasGlobalStorage()) {
677 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000678 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
679 bool IsDecl =
680 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000681 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +0000682 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
683 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000684 return ExprError();
685 }
686
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000687 VarDecl *CanonicalVD = VD->getCanonicalDecl();
688 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000689 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
690 // A threadprivate directive for file-scope variables must appear outside
691 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000692 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
693 !getCurLexicalContext()->isTranslationUnit()) {
694 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000695 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
696 bool IsDecl =
697 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
698 Diag(VD->getLocation(),
699 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
700 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000701 return ExprError();
702 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000703 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
704 // A threadprivate directive for static class member variables must appear
705 // in the class definition, in the same scope in which the member
706 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000707 if (CanonicalVD->isStaticDataMember() &&
708 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
709 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000710 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
711 bool IsDecl =
712 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
713 Diag(VD->getLocation(),
714 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
715 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000716 return ExprError();
717 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000718 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
719 // A threadprivate directive for namespace-scope variables must appear
720 // outside any definition or declaration other than the namespace
721 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000722 if (CanonicalVD->getDeclContext()->isNamespace() &&
723 (!getCurLexicalContext()->isFileContext() ||
724 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
725 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000726 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
727 bool IsDecl =
728 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
729 Diag(VD->getLocation(),
730 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
731 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000732 return ExprError();
733 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000734 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
735 // A threadprivate directive for static block-scope variables must appear
736 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000737 if (CanonicalVD->isStaticLocal() && CurScope &&
738 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000739 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000740 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
741 bool IsDecl =
742 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
743 Diag(VD->getLocation(),
744 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
745 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000746 return ExprError();
747 }
748
749 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
750 // A threadprivate directive must lexically precede all references to any
751 // of the variables in its list.
752 if (VD->isUsed()) {
753 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +0000754 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000755 return ExprError();
756 }
757
758 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataevd178ad42014-03-07 08:03:37 +0000759 ExprResult DE = BuildDeclRefExpr(VD, ExprType, VK_LValue, Id.getLoc());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000760 return DE;
761}
762
Alexey Bataeved09d242014-05-28 05:53:51 +0000763Sema::DeclGroupPtrTy
764Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
765 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000766 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000767 CurContext->addDecl(D);
768 return DeclGroupPtrTy::make(DeclGroupRef(D));
769 }
770 return DeclGroupPtrTy();
771}
772
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000773namespace {
774class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
775 Sema &SemaRef;
776
777public:
778 bool VisitDeclRefExpr(const DeclRefExpr *E) {
779 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
780 if (VD->hasLocalStorage()) {
781 SemaRef.Diag(E->getLocStart(),
782 diag::err_omp_local_var_in_threadprivate_init)
783 << E->getSourceRange();
784 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
785 << VD << VD->getSourceRange();
786 return true;
787 }
788 }
789 return false;
790 }
791 bool VisitStmt(const Stmt *S) {
792 for (auto Child : S->children()) {
793 if (Child && Visit(Child))
794 return true;
795 }
796 return false;
797 }
Alexey Bataev23b69422014-06-18 07:08:49 +0000798 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000799};
800} // namespace
801
Alexey Bataeved09d242014-05-28 05:53:51 +0000802OMPThreadPrivateDecl *
803Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000804 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +0000805 for (auto &RefExpr : VarList) {
806 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000807 VarDecl *VD = cast<VarDecl>(DE->getDecl());
808 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +0000809
810 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
811 // A threadprivate variable must not have an incomplete type.
812 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000813 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000814 continue;
815 }
816
817 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
818 // A threadprivate variable must not have a reference type.
819 if (VD->getType()->isReferenceType()) {
820 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000821 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
822 bool IsDecl =
823 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
824 Diag(VD->getLocation(),
825 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
826 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000827 continue;
828 }
829
Richard Smithfd3834f2013-04-13 02:43:54 +0000830 // Check if this is a TLS variable.
831 if (VD->getTLSKind()) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000832 Diag(ILoc, diag::err_omp_var_thread_local) << VD;
Alexey Bataeved09d242014-05-28 05:53:51 +0000833 bool IsDecl =
834 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
835 Diag(VD->getLocation(),
836 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
837 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000838 continue;
839 }
840
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000841 // Check if initial value of threadprivate variable reference variable with
842 // local storage (it is not supported by runtime).
843 if (auto Init = VD->getAnyInitializer()) {
844 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000845 if (Checker.Visit(Init))
846 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000847 }
848
Alexey Bataeved09d242014-05-28 05:53:51 +0000849 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +0000850 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +0000851 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
852 Context, SourceRange(Loc, Loc)));
853 if (auto *ML = Context.getASTMutationListener())
854 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +0000855 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000856 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000857 if (!Vars.empty()) {
858 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
859 Vars);
860 D->setAccess(AS_public);
861 }
862 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +0000863}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000864
Alexey Bataev7ff55242014-06-19 09:13:45 +0000865static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
866 const VarDecl *VD, DSAStackTy::DSAVarData DVar,
867 bool IsLoopIterVar = false) {
868 if (DVar.RefExpr) {
869 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
870 << getOpenMPClauseName(DVar.CKind);
871 return;
872 }
873 enum {
874 PDSA_StaticMemberShared,
875 PDSA_StaticLocalVarShared,
876 PDSA_LoopIterVarPrivate,
877 PDSA_LoopIterVarLinear,
878 PDSA_LoopIterVarLastprivate,
879 PDSA_ConstVarShared,
880 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000881 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000882 PDSA_LocalVarPrivate,
883 PDSA_Implicit
884 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000885 bool ReportHint = false;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000886 auto ReportLoc = VD->getLocation();
Alexey Bataev7ff55242014-06-19 09:13:45 +0000887 if (IsLoopIterVar) {
888 if (DVar.CKind == OMPC_private)
889 Reason = PDSA_LoopIterVarPrivate;
890 else if (DVar.CKind == OMPC_lastprivate)
891 Reason = PDSA_LoopIterVarLastprivate;
892 else
893 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000894 } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) {
895 Reason = PDSA_TaskVarFirstprivate;
896 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000897 } else if (VD->isStaticLocal())
898 Reason = PDSA_StaticLocalVarShared;
899 else if (VD->isStaticDataMember())
900 Reason = PDSA_StaticMemberShared;
901 else if (VD->isFileVarDecl())
902 Reason = PDSA_GlobalVarShared;
903 else if (VD->getType().isConstant(SemaRef.getASTContext()))
904 Reason = PDSA_ConstVarShared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000905 else if (VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +0000906 ReportHint = true;
907 Reason = PDSA_LocalVarPrivate;
908 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000909 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000910 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +0000911 << Reason << ReportHint
912 << getOpenMPDirectiveName(Stack->getCurrentDirective());
913 } else if (DVar.ImplicitDSALoc.isValid()) {
914 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
915 << getOpenMPClauseName(DVar.CKind);
916 }
Alexey Bataev7ff55242014-06-19 09:13:45 +0000917}
918
Alexey Bataev758e55e2013-09-06 18:03:48 +0000919namespace {
920class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
921 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000922 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000923 bool ErrorFound;
924 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000925 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000926 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +0000927
Alexey Bataev758e55e2013-09-06 18:03:48 +0000928public:
929 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000930 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000931 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +0000932 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
933 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000934
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000935 auto DVar = Stack->getTopDSA(VD, false);
936 // Check if the variable has explicit DSA set and stop analysis if it so.
937 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000938
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000939 auto ELoc = E->getExprLoc();
940 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000941 // The default(none) clause requires that each variable that is referenced
942 // in the construct, and does not have a predetermined data-sharing
943 // attribute, must have its data-sharing attribute explicitly determined
944 // by being listed in a data-sharing attribute clause.
945 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000946 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +0000947 VarsWithInheritedDSA.count(VD) == 0) {
948 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000949 return;
950 }
951
952 // OpenMP [2.9.3.6, Restrictions, p.2]
953 // A list item that appears in a reduction clause of the innermost
954 // enclosing worksharing or parallel construct may not be accessed in an
955 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000956 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000957 [](OpenMPDirectiveKind K) -> bool {
958 return isOpenMPParallelDirective(K) ||
Alexey Bataev13314bf2014-10-09 04:18:56 +0000959 isOpenMPWorksharingDirective(K) ||
960 isOpenMPTeamsDirective(K);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000961 },
962 false);
Alexey Bataevc5e02582014-06-16 07:08:35 +0000963 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
964 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000965 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
966 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +0000967 return;
968 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000969
970 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000971 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000972 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000973 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000974 }
975 }
976 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000977 for (auto *C : S->clauses()) {
978 // Skip analysis of arguments of implicitly defined firstprivate clause
979 // for task directives.
980 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
981 for (auto *CC : C->children()) {
982 if (CC)
983 Visit(CC);
984 }
985 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000986 }
987 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000988 for (auto *C : S->children()) {
989 if (C && !isa<OMPExecutableDirective>(C))
990 Visit(C);
991 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000992 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000993
994 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000995 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev4acb8592014-07-07 13:01:15 +0000996 llvm::DenseMap<VarDecl *, Expr *> &getVarsWithInheritedDSA() {
997 return VarsWithInheritedDSA;
998 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000999
Alexey Bataev7ff55242014-06-19 09:13:45 +00001000 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1001 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00001002};
Alexey Bataeved09d242014-05-28 05:53:51 +00001003} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001004
Alexey Bataevbae9a792014-06-27 10:37:06 +00001005void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001006 switch (DKind) {
1007 case OMPD_parallel: {
1008 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1009 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001010 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001011 std::make_pair(".global_tid.", KmpInt32PtrTy),
1012 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1013 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001014 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001015 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1016 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001017 break;
1018 }
1019 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001020 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001021 std::make_pair(StringRef(), QualType()) // __context with shared vars
1022 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001023 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1024 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001025 break;
1026 }
1027 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001028 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001029 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001030 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001031 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1032 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001033 break;
1034 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001035 case OMPD_for_simd: {
1036 Sema::CapturedParamNameType Params[] = {
1037 std::make_pair(StringRef(), QualType()) // __context with shared vars
1038 };
1039 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1040 Params);
1041 break;
1042 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001043 case OMPD_sections: {
1044 Sema::CapturedParamNameType Params[] = {
1045 std::make_pair(StringRef(), QualType()) // __context with shared vars
1046 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001047 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1048 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001049 break;
1050 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001051 case OMPD_section: {
1052 Sema::CapturedParamNameType Params[] = {
1053 std::make_pair(StringRef(), QualType()) // __context with shared vars
1054 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001055 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1056 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001057 break;
1058 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001059 case OMPD_single: {
1060 Sema::CapturedParamNameType Params[] = {
1061 std::make_pair(StringRef(), QualType()) // __context with shared vars
1062 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001063 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1064 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001065 break;
1066 }
Alexander Musman80c22892014-07-17 08:54:58 +00001067 case OMPD_master: {
1068 Sema::CapturedParamNameType Params[] = {
1069 std::make_pair(StringRef(), QualType()) // __context with shared vars
1070 };
1071 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1072 Params);
1073 break;
1074 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001075 case OMPD_critical: {
1076 Sema::CapturedParamNameType Params[] = {
1077 std::make_pair(StringRef(), QualType()) // __context with shared vars
1078 };
1079 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1080 Params);
1081 break;
1082 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001083 case OMPD_parallel_for: {
1084 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1085 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1086 Sema::CapturedParamNameType Params[] = {
1087 std::make_pair(".global_tid.", KmpInt32PtrTy),
1088 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1089 std::make_pair(StringRef(), QualType()) // __context with shared vars
1090 };
1091 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1092 Params);
1093 break;
1094 }
Alexander Musmane4e893b2014-09-23 09:33:00 +00001095 case OMPD_parallel_for_simd: {
1096 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1097 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1098 Sema::CapturedParamNameType Params[] = {
1099 std::make_pair(".global_tid.", KmpInt32PtrTy),
1100 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1101 std::make_pair(StringRef(), QualType()) // __context with shared vars
1102 };
1103 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1104 Params);
1105 break;
1106 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001107 case OMPD_parallel_sections: {
1108 Sema::CapturedParamNameType Params[] = {
1109 std::make_pair(StringRef(), QualType()) // __context with shared vars
1110 };
1111 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1112 Params);
1113 break;
1114 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001115 case OMPD_task: {
1116 Sema::CapturedParamNameType Params[] = {
1117 std::make_pair(StringRef(), QualType()) // __context with shared vars
1118 };
1119 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1120 Params);
1121 break;
1122 }
Alexey Bataev68446b72014-07-18 07:47:19 +00001123 case OMPD_taskyield: {
1124 Sema::CapturedParamNameType Params[] = {
1125 std::make_pair(StringRef(), QualType()) // __context with shared vars
1126 };
1127 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1128 Params);
1129 break;
1130 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001131 case OMPD_barrier: {
1132 Sema::CapturedParamNameType Params[] = {
1133 std::make_pair(StringRef(), QualType()) // __context with shared vars
1134 };
1135 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1136 Params);
1137 break;
1138 }
Alexey Bataev2df347a2014-07-18 10:17:07 +00001139 case OMPD_taskwait: {
1140 Sema::CapturedParamNameType Params[] = {
1141 std::make_pair(StringRef(), QualType()) // __context with shared vars
1142 };
1143 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1144 Params);
1145 break;
1146 }
Alexey Bataev6125da92014-07-21 11:26:11 +00001147 case OMPD_flush: {
1148 Sema::CapturedParamNameType Params[] = {
1149 std::make_pair(StringRef(), QualType()) // __context with shared vars
1150 };
1151 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1152 Params);
1153 break;
1154 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001155 case OMPD_ordered: {
1156 Sema::CapturedParamNameType Params[] = {
1157 std::make_pair(StringRef(), QualType()) // __context with shared vars
1158 };
1159 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1160 Params);
1161 break;
1162 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001163 case OMPD_atomic: {
1164 Sema::CapturedParamNameType Params[] = {
1165 std::make_pair(StringRef(), QualType()) // __context with shared vars
1166 };
1167 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1168 Params);
1169 break;
1170 }
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001171 case OMPD_target: {
1172 Sema::CapturedParamNameType Params[] = {
1173 std::make_pair(StringRef(), QualType()) // __context with shared vars
1174 };
1175 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1176 Params);
1177 break;
1178 }
Alexey Bataev13314bf2014-10-09 04:18:56 +00001179 case OMPD_teams: {
1180 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1181 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1182 Sema::CapturedParamNameType Params[] = {
1183 std::make_pair(".global_tid.", KmpInt32PtrTy),
1184 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1185 std::make_pair(StringRef(), QualType()) // __context with shared vars
1186 };
1187 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1188 Params);
1189 break;
1190 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001191 case OMPD_threadprivate:
Alexey Bataev9959db52014-05-06 10:08:46 +00001192 llvm_unreachable("OpenMP Directive is not allowed");
1193 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001194 llvm_unreachable("Unknown OpenMP directive");
1195 }
1196}
1197
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001198static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1199 OpenMPDirectiveKind CurrentRegion,
1200 const DeclarationNameInfo &CurrentName,
1201 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001202 // Allowed nesting of constructs
1203 // +------------------+-----------------+------------------------------------+
1204 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1205 // +------------------+-----------------+------------------------------------+
1206 // | parallel | parallel | * |
1207 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001208 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001209 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001210 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001211 // | parallel | simd | * |
1212 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001213 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001214 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001215 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001216 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001217 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001218 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001219 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001220 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001221 // | parallel | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001222 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001223 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001224 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001225 // | parallel | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001226 // | parallel | teams | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001227 // +------------------+-----------------+------------------------------------+
1228 // | for | parallel | * |
1229 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001230 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001231 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001232 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001233 // | for | simd | * |
1234 // | for | sections | + |
1235 // | for | section | + |
1236 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001237 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001238 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001239 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001240 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001241 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001242 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001243 // | for | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001244 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001245 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001246 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001247 // | for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001248 // | for | teams | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001249 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001250 // | master | parallel | * |
1251 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001252 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001253 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001254 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001255 // | master | simd | * |
1256 // | master | sections | + |
1257 // | master | section | + |
1258 // | master | single | + |
1259 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001260 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001261 // | master |parallel sections| * |
1262 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001263 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001264 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001265 // | master | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001266 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001267 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001268 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001269 // | master | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001270 // | master | teams | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001271 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001272 // | critical | parallel | * |
1273 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001274 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001275 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001276 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001277 // | critical | simd | * |
1278 // | critical | sections | + |
1279 // | critical | section | + |
1280 // | critical | single | + |
1281 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001282 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001283 // | critical |parallel sections| * |
1284 // | critical | task | * |
1285 // | critical | taskyield | * |
1286 // | critical | barrier | + |
1287 // | critical | taskwait | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001288 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001289 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001290 // | critical | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001291 // | critical | teams | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001292 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001293 // | simd | parallel | |
1294 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001295 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00001296 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001297 // | simd | critical | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001298 // | simd | simd | |
1299 // | simd | sections | |
1300 // | simd | section | |
1301 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001302 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001303 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001304 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001305 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00001306 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001307 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001308 // | simd | taskwait | |
Alexey Bataev6125da92014-07-21 11:26:11 +00001309 // | simd | flush | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001310 // | simd | ordered | |
Alexey Bataev0162e452014-07-22 10:10:35 +00001311 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001312 // | simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001313 // | simd | teams | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001314 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00001315 // | for simd | parallel | |
1316 // | for simd | for | |
1317 // | for simd | for simd | |
1318 // | for simd | master | |
1319 // | for simd | critical | |
1320 // | for simd | simd | |
1321 // | for simd | sections | |
1322 // | for simd | section | |
1323 // | for simd | single | |
1324 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001325 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001326 // | for simd |parallel sections| |
1327 // | for simd | task | |
1328 // | for simd | taskyield | |
1329 // | for simd | barrier | |
1330 // | for simd | taskwait | |
1331 // | for simd | flush | |
1332 // | for simd | ordered | |
1333 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001334 // | for simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001335 // | for simd | teams | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001336 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00001337 // | parallel for simd| parallel | |
1338 // | parallel for simd| for | |
1339 // | parallel for simd| for simd | |
1340 // | parallel for simd| master | |
1341 // | parallel for simd| critical | |
1342 // | parallel for simd| simd | |
1343 // | parallel for simd| sections | |
1344 // | parallel for simd| section | |
1345 // | parallel for simd| single | |
1346 // | parallel for simd| parallel for | |
1347 // | parallel for simd|parallel for simd| |
1348 // | parallel for simd|parallel sections| |
1349 // | parallel for simd| task | |
1350 // | parallel for simd| taskyield | |
1351 // | parallel for simd| barrier | |
1352 // | parallel for simd| taskwait | |
1353 // | parallel for simd| flush | |
1354 // | parallel for simd| ordered | |
1355 // | parallel for simd| atomic | |
1356 // | parallel for simd| target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001357 // | parallel for simd| teams | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001358 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001359 // | sections | parallel | * |
1360 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001361 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001362 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001363 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001364 // | sections | simd | * |
1365 // | sections | sections | + |
1366 // | sections | section | * |
1367 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001368 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001369 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001370 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001371 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001372 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001373 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001374 // | sections | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001375 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001376 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001377 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001378 // | sections | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001379 // | sections | teams | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001380 // +------------------+-----------------+------------------------------------+
1381 // | section | parallel | * |
1382 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001383 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001384 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001385 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001386 // | section | simd | * |
1387 // | section | sections | + |
1388 // | section | section | + |
1389 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001390 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001391 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001392 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001393 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001394 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001395 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001396 // | section | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001397 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001398 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001399 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001400 // | section | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001401 // | section | teams | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001402 // +------------------+-----------------+------------------------------------+
1403 // | single | parallel | * |
1404 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001405 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001406 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001407 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001408 // | single | simd | * |
1409 // | single | sections | + |
1410 // | single | section | + |
1411 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001412 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001413 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001414 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001415 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001416 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001417 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001418 // | single | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001419 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001420 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001421 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001422 // | single | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001423 // | single | teams | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001424 // +------------------+-----------------+------------------------------------+
1425 // | parallel for | parallel | * |
1426 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001427 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001428 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001429 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001430 // | parallel for | simd | * |
1431 // | parallel for | sections | + |
1432 // | parallel for | section | + |
1433 // | parallel for | single | + |
1434 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001435 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001436 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001437 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001438 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001439 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001440 // | parallel for | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001441 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001442 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001443 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001444 // | parallel for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001445 // | parallel for | teams | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001446 // +------------------+-----------------+------------------------------------+
1447 // | parallel sections| parallel | * |
1448 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001449 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001450 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001451 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001452 // | parallel sections| simd | * |
1453 // | parallel sections| sections | + |
1454 // | parallel sections| section | * |
1455 // | parallel sections| single | + |
1456 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001457 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001458 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001459 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001460 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001461 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001462 // | parallel sections| taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001463 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001464 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001465 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001466 // | parallel sections| target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001467 // | parallel sections| teams | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001468 // +------------------+-----------------+------------------------------------+
1469 // | task | parallel | * |
1470 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001471 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001472 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001473 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001474 // | task | simd | * |
1475 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001476 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001477 // | task | single | + |
1478 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001479 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001480 // | task |parallel sections| * |
1481 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001482 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001483 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001484 // | task | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001485 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001486 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001487 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001488 // | task | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001489 // | task | teams | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001490 // +------------------+-----------------+------------------------------------+
1491 // | ordered | parallel | * |
1492 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001493 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001494 // | ordered | master | * |
1495 // | ordered | critical | * |
1496 // | ordered | simd | * |
1497 // | ordered | sections | + |
1498 // | ordered | section | + |
1499 // | ordered | single | + |
1500 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001501 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001502 // | ordered |parallel sections| * |
1503 // | ordered | task | * |
1504 // | ordered | taskyield | * |
1505 // | ordered | barrier | + |
1506 // | ordered | taskwait | * |
1507 // | ordered | flush | * |
1508 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001509 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001510 // | ordered | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001511 // | ordered | teams | + |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001512 // +------------------+-----------------+------------------------------------+
1513 // | atomic | parallel | |
1514 // | atomic | for | |
1515 // | atomic | for simd | |
1516 // | atomic | master | |
1517 // | atomic | critical | |
1518 // | atomic | simd | |
1519 // | atomic | sections | |
1520 // | atomic | section | |
1521 // | atomic | single | |
1522 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001523 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001524 // | atomic |parallel sections| |
1525 // | atomic | task | |
1526 // | atomic | taskyield | |
1527 // | atomic | barrier | |
1528 // | atomic | taskwait | |
1529 // | atomic | flush | |
1530 // | atomic | ordered | |
1531 // | atomic | atomic | |
1532 // | atomic | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001533 // | atomic | teams | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001534 // +------------------+-----------------+------------------------------------+
1535 // | target | parallel | * |
1536 // | target | for | * |
1537 // | target | for simd | * |
1538 // | target | master | * |
1539 // | target | critical | * |
1540 // | target | simd | * |
1541 // | target | sections | * |
1542 // | target | section | * |
1543 // | target | single | * |
1544 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001545 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001546 // | target |parallel sections| * |
1547 // | target | task | * |
1548 // | target | taskyield | * |
1549 // | target | barrier | * |
1550 // | target | taskwait | * |
1551 // | target | flush | * |
1552 // | target | ordered | * |
1553 // | target | atomic | * |
1554 // | target | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001555 // | target | teams | * |
1556 // +------------------+-----------------+------------------------------------+
1557 // | teams | parallel | * |
1558 // | teams | for | + |
1559 // | teams | for simd | + |
1560 // | teams | master | + |
1561 // | teams | critical | + |
1562 // | teams | simd | + |
1563 // | teams | sections | + |
1564 // | teams | section | + |
1565 // | teams | single | + |
1566 // | teams | parallel for | * |
1567 // | teams |parallel for simd| * |
1568 // | teams |parallel sections| * |
1569 // | teams | task | + |
1570 // | teams | taskyield | + |
1571 // | teams | barrier | + |
1572 // | teams | taskwait | + |
1573 // | teams | flush | + |
1574 // | teams | ordered | + |
1575 // | teams | atomic | + |
1576 // | teams | target | + |
1577 // | teams | teams | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001578 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00001579 if (Stack->getCurScope()) {
1580 auto ParentRegion = Stack->getParentDirective();
1581 bool NestingProhibited = false;
1582 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001583 enum {
1584 NoRecommend,
1585 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00001586 ShouldBeInOrderedRegion,
1587 ShouldBeInTargetRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001588 } Recommend = NoRecommend;
Alexey Bataev549210e2014-06-24 04:39:47 +00001589 if (isOpenMPSimdDirective(ParentRegion)) {
1590 // OpenMP [2.16, Nesting of Regions]
1591 // OpenMP constructs may not be nested inside a simd region.
1592 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
1593 return true;
1594 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001595 if (ParentRegion == OMPD_atomic) {
1596 // OpenMP [2.16, Nesting of Regions]
1597 // OpenMP constructs may not be nested inside an atomic region.
1598 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
1599 return true;
1600 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001601 if (CurrentRegion == OMPD_section) {
1602 // OpenMP [2.7.2, sections Construct, Restrictions]
1603 // Orphaned section directives are prohibited. That is, the section
1604 // directives must appear within the sections construct and must not be
1605 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001606 if (ParentRegion != OMPD_sections &&
1607 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001608 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
1609 << (ParentRegion != OMPD_unknown)
1610 << getOpenMPDirectiveName(ParentRegion);
1611 return true;
1612 }
1613 return false;
1614 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001615 // Allow some constructs to be orphaned (they could be used in functions,
1616 // called from OpenMP regions with the required preconditions).
1617 if (ParentRegion == OMPD_unknown)
1618 return false;
Alexander Musman80c22892014-07-17 08:54:58 +00001619 if (CurrentRegion == OMPD_master) {
1620 // OpenMP [2.16, Nesting of Regions]
1621 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00001622 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00001623 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
1624 ParentRegion == OMPD_task;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001625 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
1626 // OpenMP [2.16, Nesting of Regions]
1627 // A critical region may not be nested (closely or otherwise) inside a
1628 // critical region with the same name. Note that this restriction is not
1629 // sufficient to prevent deadlock.
1630 SourceLocation PreviousCriticalLoc;
1631 bool DeadLock =
1632 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
1633 OpenMPDirectiveKind K,
1634 const DeclarationNameInfo &DNI,
1635 SourceLocation Loc)
1636 ->bool {
1637 if (K == OMPD_critical &&
1638 DNI.getName() == CurrentName.getName()) {
1639 PreviousCriticalLoc = Loc;
1640 return true;
1641 } else
1642 return false;
1643 },
1644 false /* skip top directive */);
1645 if (DeadLock) {
1646 SemaRef.Diag(StartLoc,
1647 diag::err_omp_prohibited_region_critical_same_name)
1648 << CurrentName.getName();
1649 if (PreviousCriticalLoc.isValid())
1650 SemaRef.Diag(PreviousCriticalLoc,
1651 diag::note_omp_previous_critical_region);
1652 return true;
1653 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001654 } else if (CurrentRegion == OMPD_barrier) {
1655 // OpenMP [2.16, Nesting of Regions]
1656 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00001657 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001658 NestingProhibited =
1659 isOpenMPWorksharingDirective(ParentRegion) ||
1660 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1661 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00001662 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00001663 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00001664 // OpenMP [2.16, Nesting of Regions]
1665 // A worksharing region may not be closely nested inside a worksharing,
1666 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001667 NestingProhibited =
Alexander Musmanf82886e2014-09-18 05:12:34 +00001668 isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001669 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1670 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
1671 Recommend = ShouldBeInParallelRegion;
1672 } else if (CurrentRegion == OMPD_ordered) {
1673 // OpenMP [2.16, Nesting of Regions]
1674 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00001675 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001676 // An ordered region must be closely nested inside a loop region (or
1677 // parallel loop region) with an ordered clause.
1678 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00001679 ParentRegion == OMPD_task ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001680 !Stack->isParentOrderedRegion();
1681 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00001682 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
1683 // OpenMP [2.16, Nesting of Regions]
1684 // If specified, a teams construct must be contained within a target
1685 // construct.
1686 NestingProhibited = ParentRegion != OMPD_target;
1687 Recommend = ShouldBeInTargetRegion;
1688 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
1689 }
1690 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
1691 // OpenMP [2.16, Nesting of Regions]
1692 // distribute, parallel, parallel sections, parallel workshare, and the
1693 // parallel loop and parallel loop SIMD constructs are the only OpenMP
1694 // constructs that can be closely nested in the teams region.
1695 // TODO: add distribute directive.
1696 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion);
1697 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00001698 }
1699 if (NestingProhibited) {
1700 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001701 << CloseNesting << getOpenMPDirectiveName(ParentRegion) << Recommend
1702 << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00001703 return true;
1704 }
1705 }
1706 return false;
1707}
1708
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001709StmtResult Sema::ActOnOpenMPExecutableDirective(OpenMPDirectiveKind Kind,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001710 const DeclarationNameInfo &DirName,
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001711 ArrayRef<OMPClause *> Clauses,
1712 Stmt *AStmt,
1713 SourceLocation StartLoc,
1714 SourceLocation EndLoc) {
1715 StmtResult Res = StmtError();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001716 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00001717 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001718
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001719 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev68446b72014-07-18 07:47:19 +00001720 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001721 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00001722 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00001723 if (AStmt) {
1724 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
1725
1726 // Check default data sharing attributes for referenced variables.
1727 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
1728 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
1729 if (DSAChecker.isErrorFound())
1730 return StmtError();
1731 // Generate list of implicitly defined firstprivate variables.
1732 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00001733
1734 if (!DSAChecker.getImplicitFirstprivate().empty()) {
1735 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
1736 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
1737 SourceLocation(), SourceLocation())) {
1738 ClausesWithImplicit.push_back(Implicit);
1739 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
1740 DSAChecker.getImplicitFirstprivate().size();
1741 } else
1742 ErrorFound = true;
1743 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001744 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001745
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001746 switch (Kind) {
1747 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00001748 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
1749 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001750 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001751 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001752 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
1753 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001754 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00001755 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001756 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
1757 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001758 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00001759 case OMPD_for_simd:
1760 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
1761 EndLoc, VarsWithInheritedDSA);
1762 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001763 case OMPD_sections:
1764 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
1765 EndLoc);
1766 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001767 case OMPD_section:
1768 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00001769 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001770 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
1771 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001772 case OMPD_single:
1773 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
1774 EndLoc);
1775 break;
Alexander Musman80c22892014-07-17 08:54:58 +00001776 case OMPD_master:
1777 assert(ClausesWithImplicit.empty() &&
1778 "No clauses are allowed for 'omp master' directive");
1779 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
1780 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001781 case OMPD_critical:
1782 assert(ClausesWithImplicit.empty() &&
1783 "No clauses are allowed for 'omp critical' directive");
1784 Res = ActOnOpenMPCriticalDirective(DirName, AStmt, StartLoc, EndLoc);
1785 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00001786 case OMPD_parallel_for:
1787 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
1788 EndLoc, VarsWithInheritedDSA);
1789 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00001790 case OMPD_parallel_for_simd:
1791 Res = ActOnOpenMPParallelForSimdDirective(
1792 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
1793 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001794 case OMPD_parallel_sections:
1795 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
1796 StartLoc, EndLoc);
1797 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001798 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001799 Res =
1800 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
1801 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00001802 case OMPD_taskyield:
1803 assert(ClausesWithImplicit.empty() &&
1804 "No clauses are allowed for 'omp taskyield' directive");
1805 assert(AStmt == nullptr &&
1806 "No associated statement allowed for 'omp taskyield' directive");
1807 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
1808 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001809 case OMPD_barrier:
1810 assert(ClausesWithImplicit.empty() &&
1811 "No clauses are allowed for 'omp barrier' directive");
1812 assert(AStmt == nullptr &&
1813 "No associated statement allowed for 'omp barrier' directive");
1814 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
1815 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00001816 case OMPD_taskwait:
1817 assert(ClausesWithImplicit.empty() &&
1818 "No clauses are allowed for 'omp taskwait' directive");
1819 assert(AStmt == nullptr &&
1820 "No associated statement allowed for 'omp taskwait' directive");
1821 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
1822 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00001823 case OMPD_flush:
1824 assert(AStmt == nullptr &&
1825 "No associated statement allowed for 'omp flush' directive");
1826 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
1827 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001828 case OMPD_ordered:
1829 assert(ClausesWithImplicit.empty() &&
1830 "No clauses are allowed for 'omp ordered' directive");
1831 Res = ActOnOpenMPOrderedDirective(AStmt, StartLoc, EndLoc);
1832 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00001833 case OMPD_atomic:
1834 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
1835 EndLoc);
1836 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00001837 case OMPD_teams:
1838 Res =
1839 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
1840 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001841 case OMPD_target:
1842 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
1843 EndLoc);
1844 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001845 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001846 llvm_unreachable("OpenMP Directive is not allowed");
1847 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001848 llvm_unreachable("Unknown OpenMP directive");
1849 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001850
Alexey Bataev4acb8592014-07-07 13:01:15 +00001851 for (auto P : VarsWithInheritedDSA) {
1852 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
1853 << P.first << P.second->getSourceRange();
1854 }
1855 if (!VarsWithInheritedDSA.empty())
1856 return StmtError();
1857
Alexey Bataeved09d242014-05-28 05:53:51 +00001858 if (ErrorFound)
1859 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001860 return Res;
1861}
1862
1863StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
1864 Stmt *AStmt,
1865 SourceLocation StartLoc,
1866 SourceLocation EndLoc) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001867 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1868 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
1869 // 1.2.2 OpenMP Language Terminology
1870 // Structured block - An executable statement with a single entry at the
1871 // top and a single exit at the bottom.
1872 // The point of exit cannot be a branch out of the structured block.
1873 // longjmp() and throw() must not violate the entry/exit criteria.
1874 CS->getCapturedDecl()->setNothrow();
1875
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001876 getCurFunction()->setHasBranchProtectedScope();
1877
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001878 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
1879 AStmt);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001880}
1881
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001882namespace {
1883/// \brief Helper class for checking canonical form of the OpenMP loops and
1884/// extracting iteration space of each loop in the loop nest, that will be used
1885/// for IR generation.
1886class OpenMPIterationSpaceChecker {
1887 /// \brief Reference to Sema.
1888 Sema &SemaRef;
1889 /// \brief A location for diagnostics (when there is no some better location).
1890 SourceLocation DefaultLoc;
1891 /// \brief A location for diagnostics (when increment is not compatible).
1892 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00001893 /// \brief A source location for referring to loop init later.
1894 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001895 /// \brief A source location for referring to condition later.
1896 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00001897 /// \brief A source location for referring to increment later.
1898 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001899 /// \brief Loop variable.
1900 VarDecl *Var;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001901 /// \brief Reference to loop variable.
1902 DeclRefExpr *VarRef;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001903 /// \brief Lower bound (initializer for the var).
1904 Expr *LB;
1905 /// \brief Upper bound.
1906 Expr *UB;
1907 /// \brief Loop step (increment).
1908 Expr *Step;
1909 /// \brief This flag is true when condition is one of:
1910 /// Var < UB
1911 /// Var <= UB
1912 /// UB > Var
1913 /// UB >= Var
1914 bool TestIsLessOp;
1915 /// \brief This flag is true when condition is strict ( < or > ).
1916 bool TestIsStrictOp;
1917 /// \brief This flag is true when step is subtracted on each iteration.
1918 bool SubtractStep;
1919
1920public:
1921 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
1922 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
Alexander Musmana5f070a2014-10-01 06:03:56 +00001923 InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()),
1924 IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001925 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
1926 TestIsStrictOp(false), SubtractStep(false) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001927 /// \brief Check init-expr for canonical loop form and save loop counter
1928 /// variable - #Var and its initialization value - #LB.
1929 bool CheckInit(Stmt *S);
1930 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
1931 /// for less/greater and for strict/non-strict comparison.
1932 bool CheckCond(Expr *S);
1933 /// \brief Check incr-expr for canonical loop form and return true if it
1934 /// does not conform, otherwise save loop step (#Step).
1935 bool CheckInc(Expr *S);
1936 /// \brief Return the loop counter variable.
1937 VarDecl *GetLoopVar() const { return Var; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001938 /// \brief Return the reference expression to loop counter variable.
1939 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00001940 /// \brief Source range of the loop init.
1941 SourceRange GetInitSrcRange() const { return InitSrcRange; }
1942 /// \brief Source range of the loop condition.
1943 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
1944 /// \brief Source range of the loop increment.
1945 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
1946 /// \brief True if the step should be subtracted.
1947 bool ShouldSubtractStep() const { return SubtractStep; }
1948 /// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00001949 Expr *BuildNumIterations(Scope *S, const bool LimitedType) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00001950 /// \brief Build reference expression to the counter be used for codegen.
1951 Expr *BuildCounterVar() const;
1952 /// \brief Build initization of the counter be used for codegen.
1953 Expr *BuildCounterInit() const;
1954 /// \brief Build step of the counter be used for codegen.
1955 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001956 /// \brief Return true if any expression is dependent.
1957 bool Dependent() const;
1958
1959private:
1960 /// \brief Check the right-hand side of an assignment in the increment
1961 /// expression.
1962 bool CheckIncRHS(Expr *RHS);
1963 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001964 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001965 /// \brief Helper to set upper bound.
1966 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, const SourceRange &SR,
1967 const SourceLocation &SL);
1968 /// \brief Helper to set loop increment.
1969 bool SetStep(Expr *NewStep, bool Subtract);
1970};
1971
1972bool OpenMPIterationSpaceChecker::Dependent() const {
1973 if (!Var) {
1974 assert(!LB && !UB && !Step);
1975 return false;
1976 }
1977 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
1978 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
1979}
1980
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001981bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
1982 DeclRefExpr *NewVarRefExpr,
1983 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001984 // State consistency checking to ensure correct usage.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001985 assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
1986 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001987 if (!NewVar || !NewLB)
1988 return true;
1989 Var = NewVar;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001990 VarRef = NewVarRefExpr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001991 LB = NewLB;
1992 return false;
1993}
1994
1995bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
1996 const SourceRange &SR,
1997 const SourceLocation &SL) {
1998 // State consistency checking to ensure correct usage.
1999 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
2000 !TestIsLessOp && !TestIsStrictOp);
2001 if (!NewUB)
2002 return true;
2003 UB = NewUB;
2004 TestIsLessOp = LessOp;
2005 TestIsStrictOp = StrictOp;
2006 ConditionSrcRange = SR;
2007 ConditionLoc = SL;
2008 return false;
2009}
2010
2011bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
2012 // State consistency checking to ensure correct usage.
2013 assert(Var != nullptr && LB != nullptr && Step == nullptr);
2014 if (!NewStep)
2015 return true;
2016 if (!NewStep->isValueDependent()) {
2017 // Check that the step is integer expression.
2018 SourceLocation StepLoc = NewStep->getLocStart();
2019 ExprResult Val =
2020 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
2021 if (Val.isInvalid())
2022 return true;
2023 NewStep = Val.get();
2024
2025 // OpenMP [2.6, Canonical Loop Form, Restrictions]
2026 // If test-expr is of form var relational-op b and relational-op is < or
2027 // <= then incr-expr must cause var to increase on each iteration of the
2028 // loop. If test-expr is of form var relational-op b and relational-op is
2029 // > or >= then incr-expr must cause var to decrease on each iteration of
2030 // the loop.
2031 // If test-expr is of form b relational-op var and relational-op is < or
2032 // <= then incr-expr must cause var to decrease on each iteration of the
2033 // loop. If test-expr is of form b relational-op var and relational-op is
2034 // > or >= then incr-expr must cause var to increase on each iteration of
2035 // the loop.
2036 llvm::APSInt Result;
2037 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
2038 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
2039 bool IsConstNeg =
2040 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00002041 bool IsConstPos =
2042 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002043 bool IsConstZero = IsConstant && !Result.getBoolValue();
2044 if (UB && (IsConstZero ||
2045 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00002046 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002047 SemaRef.Diag(NewStep->getExprLoc(),
2048 diag::err_omp_loop_incr_not_compatible)
2049 << Var << TestIsLessOp << NewStep->getSourceRange();
2050 SemaRef.Diag(ConditionLoc,
2051 diag::note_omp_loop_cond_requres_compatible_incr)
2052 << TestIsLessOp << ConditionSrcRange;
2053 return true;
2054 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002055 if (TestIsLessOp == Subtract) {
2056 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
2057 NewStep).get();
2058 Subtract = !Subtract;
2059 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002060 }
2061
2062 Step = NewStep;
2063 SubtractStep = Subtract;
2064 return false;
2065}
2066
2067bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S) {
2068 // Check init-expr for canonical loop form and save loop counter
2069 // variable - #Var and its initialization value - #LB.
2070 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
2071 // var = lb
2072 // integer-type var = lb
2073 // random-access-iterator-type var = lb
2074 // pointer-type var = lb
2075 //
2076 if (!S) {
2077 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
2078 return true;
2079 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002080 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002081 if (Expr *E = dyn_cast<Expr>(S))
2082 S = E->IgnoreParens();
2083 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2084 if (BO->getOpcode() == BO_Assign)
2085 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002086 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002087 BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002088 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
2089 if (DS->isSingleDecl()) {
2090 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
2091 if (Var->hasInit()) {
2092 // Accept non-canonical init form here but emit ext. warning.
2093 if (Var->getInitStyle() != VarDecl::CInit)
2094 SemaRef.Diag(S->getLocStart(),
2095 diag::ext_omp_loop_not_canonical_init)
2096 << S->getSourceRange();
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002097 return SetVarAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002098 }
2099 }
2100 }
2101 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
2102 if (CE->getOperator() == OO_Equal)
2103 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002104 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
2105 CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002106
2107 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
2108 << S->getSourceRange();
2109 return true;
2110}
2111
Alexey Bataev23b69422014-06-18 07:08:49 +00002112/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002113/// variable (which may be the loop variable) if possible.
2114static const VarDecl *GetInitVarDecl(const Expr *E) {
2115 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00002116 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002117 E = E->IgnoreParenImpCasts();
2118 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
2119 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
2120 if (Ctor->isCopyConstructor() && CE->getNumArgs() == 1 &&
2121 CE->getArg(0) != nullptr)
2122 E = CE->getArg(0)->IgnoreParenImpCasts();
2123 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
2124 if (!DRE)
2125 return nullptr;
2126 return dyn_cast<VarDecl>(DRE->getDecl());
2127}
2128
2129bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
2130 // Check test-expr for canonical form, save upper-bound UB, flags for
2131 // less/greater and for strict/non-strict comparison.
2132 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2133 // var relational-op b
2134 // b relational-op var
2135 //
2136 if (!S) {
2137 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
2138 return true;
2139 }
2140 S = S->IgnoreParenImpCasts();
2141 SourceLocation CondLoc = S->getLocStart();
2142 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2143 if (BO->isRelationalOp()) {
2144 if (GetInitVarDecl(BO->getLHS()) == Var)
2145 return SetUB(BO->getRHS(),
2146 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
2147 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2148 BO->getSourceRange(), BO->getOperatorLoc());
2149 if (GetInitVarDecl(BO->getRHS()) == Var)
2150 return SetUB(BO->getLHS(),
2151 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
2152 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2153 BO->getSourceRange(), BO->getOperatorLoc());
2154 }
2155 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2156 if (CE->getNumArgs() == 2) {
2157 auto Op = CE->getOperator();
2158 switch (Op) {
2159 case OO_Greater:
2160 case OO_GreaterEqual:
2161 case OO_Less:
2162 case OO_LessEqual:
2163 if (GetInitVarDecl(CE->getArg(0)) == Var)
2164 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
2165 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2166 CE->getOperatorLoc());
2167 if (GetInitVarDecl(CE->getArg(1)) == Var)
2168 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
2169 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2170 CE->getOperatorLoc());
2171 break;
2172 default:
2173 break;
2174 }
2175 }
2176 }
2177 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
2178 << S->getSourceRange() << Var;
2179 return true;
2180}
2181
2182bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
2183 // RHS of canonical loop form increment can be:
2184 // var + incr
2185 // incr + var
2186 // var - incr
2187 //
2188 RHS = RHS->IgnoreParenImpCasts();
2189 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
2190 if (BO->isAdditiveOp()) {
2191 bool IsAdd = BO->getOpcode() == BO_Add;
2192 if (GetInitVarDecl(BO->getLHS()) == Var)
2193 return SetStep(BO->getRHS(), !IsAdd);
2194 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
2195 return SetStep(BO->getLHS(), false);
2196 }
2197 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
2198 bool IsAdd = CE->getOperator() == OO_Plus;
2199 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
2200 if (GetInitVarDecl(CE->getArg(0)) == Var)
2201 return SetStep(CE->getArg(1), !IsAdd);
2202 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
2203 return SetStep(CE->getArg(0), false);
2204 }
2205 }
2206 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2207 << RHS->getSourceRange() << Var;
2208 return true;
2209}
2210
2211bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
2212 // Check incr-expr for canonical loop form and return true if it
2213 // does not conform.
2214 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2215 // ++var
2216 // var++
2217 // --var
2218 // var--
2219 // var += incr
2220 // var -= incr
2221 // var = var + incr
2222 // var = incr + var
2223 // var = var - incr
2224 //
2225 if (!S) {
2226 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
2227 return true;
2228 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002229 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002230 S = S->IgnoreParens();
2231 if (auto UO = dyn_cast<UnaryOperator>(S)) {
2232 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
2233 return SetStep(
2234 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
2235 (UO->isDecrementOp() ? -1 : 1)).get(),
2236 false);
2237 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
2238 switch (BO->getOpcode()) {
2239 case BO_AddAssign:
2240 case BO_SubAssign:
2241 if (GetInitVarDecl(BO->getLHS()) == Var)
2242 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
2243 break;
2244 case BO_Assign:
2245 if (GetInitVarDecl(BO->getLHS()) == Var)
2246 return CheckIncRHS(BO->getRHS());
2247 break;
2248 default:
2249 break;
2250 }
2251 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2252 switch (CE->getOperator()) {
2253 case OO_PlusPlus:
2254 case OO_MinusMinus:
2255 if (GetInitVarDecl(CE->getArg(0)) == Var)
2256 return SetStep(
2257 SemaRef.ActOnIntegerConstant(
2258 CE->getLocStart(),
2259 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
2260 false);
2261 break;
2262 case OO_PlusEqual:
2263 case OO_MinusEqual:
2264 if (GetInitVarDecl(CE->getArg(0)) == Var)
2265 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
2266 break;
2267 case OO_Equal:
2268 if (GetInitVarDecl(CE->getArg(0)) == Var)
2269 return CheckIncRHS(CE->getArg(1));
2270 break;
2271 default:
2272 break;
2273 }
2274 }
2275 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2276 << S->getSourceRange() << Var;
2277 return true;
2278}
Alexander Musmana5f070a2014-10-01 06:03:56 +00002279
2280/// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00002281Expr *
2282OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S,
2283 const bool LimitedType) const {
Alexander Musmana5f070a2014-10-01 06:03:56 +00002284 ExprResult Diff;
2285 if (Var->getType()->isIntegerType() || Var->getType()->isPointerType() ||
2286 SemaRef.getLangOpts().CPlusPlus) {
2287 // Upper - Lower
2288 Expr *Upper = TestIsLessOp ? UB : LB;
2289 Expr *Lower = TestIsLessOp ? LB : UB;
2290
2291 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
2292
2293 if (!Diff.isUsable() && Var->getType()->getAsCXXRecordDecl()) {
2294 // BuildBinOp already emitted error, this one is to point user to upper
2295 // and lower bound, and to tell what is passed to 'operator-'.
2296 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
2297 << Upper->getSourceRange() << Lower->getSourceRange();
2298 return nullptr;
2299 }
2300 }
2301
2302 if (!Diff.isUsable())
2303 return nullptr;
2304
2305 // Upper - Lower [- 1]
2306 if (TestIsStrictOp)
2307 Diff = SemaRef.BuildBinOp(
2308 S, DefaultLoc, BO_Sub, Diff.get(),
2309 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
2310 if (!Diff.isUsable())
2311 return nullptr;
2312
2313 // Upper - Lower [- 1] + Step
2314 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(),
2315 Step->IgnoreImplicit());
2316 if (!Diff.isUsable())
2317 return nullptr;
2318
2319 // Parentheses (for dumping/debugging purposes only).
2320 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
2321 if (!Diff.isUsable())
2322 return nullptr;
2323
2324 // (Upper - Lower [- 1] + Step) / Step
2325 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(),
2326 Step->IgnoreImplicit());
2327 if (!Diff.isUsable())
2328 return nullptr;
2329
Alexander Musman174b3ca2014-10-06 11:16:29 +00002330 // OpenMP runtime requires 32-bit or 64-bit loop variables.
2331 if (LimitedType) {
2332 auto &C = SemaRef.Context;
2333 QualType Type = Diff.get()->getType();
2334 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
2335 if (NewSize != C.getTypeSize(Type)) {
2336 if (NewSize < C.getTypeSize(Type)) {
2337 assert(NewSize == 64 && "incorrect loop var size");
2338 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
2339 << InitSrcRange << ConditionSrcRange;
2340 }
2341 QualType NewType = C.getIntTypeForBitwidth(
2342 NewSize, Type->hasSignedIntegerRepresentation());
2343 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
2344 Sema::AA_Converting, true);
2345 if (!Diff.isUsable())
2346 return nullptr;
2347 }
2348 }
2349
Alexander Musmana5f070a2014-10-01 06:03:56 +00002350 return Diff.get();
2351}
2352
2353/// \brief Build reference expression to the counter be used for codegen.
2354Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const {
2355 return DeclRefExpr::Create(SemaRef.Context, NestedNameSpecifierLoc(),
2356 GetIncrementSrcRange().getBegin(), Var, false,
2357 DefaultLoc, Var->getType(), VK_LValue);
2358}
2359
2360/// \brief Build initization of the counter be used for codegen.
2361Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
2362
2363/// \brief Build step of the counter be used for codegen.
2364Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
2365
2366/// \brief Iteration space of a single for loop.
2367struct LoopIterationSpace {
2368 /// \brief This expression calculates the number of iterations in the loop.
2369 /// It is always possible to calculate it before starting the loop.
2370 Expr *NumIterations;
2371 /// \brief The loop counter variable.
2372 Expr *CounterVar;
2373 /// \brief This is initializer for the initial value of #CounterVar.
2374 Expr *CounterInit;
2375 /// \brief This is step for the #CounterVar used to generate its update:
2376 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
2377 Expr *CounterStep;
2378 /// \brief Should step be subtracted?
2379 bool Subtract;
2380 /// \brief Source range of the loop init.
2381 SourceRange InitSrcRange;
2382 /// \brief Source range of the loop condition.
2383 SourceRange CondSrcRange;
2384 /// \brief Source range of the loop increment.
2385 SourceRange IncSrcRange;
2386};
2387
2388/// \brief The resulting expressions built for the OpenMP loop CodeGen for the
2389/// whole collapsed loop nest. See class OMPLoopDirective for their description.
2390struct BuiltLoopExprs {
2391 Expr *IterationVarRef;
2392 Expr *LastIteration;
2393 Expr *CalcLastIteration;
2394 Expr *PreCond;
2395 Expr *Cond;
2396 Expr *SeparatedCond;
2397 Expr *Init;
2398 Expr *Inc;
2399 SmallVector<Expr *, 4> Counters;
2400 SmallVector<Expr *, 4> Updates;
2401 SmallVector<Expr *, 4> Finals;
2402
2403 bool builtAll() {
2404 return IterationVarRef != nullptr && LastIteration != nullptr &&
2405 PreCond != nullptr && Cond != nullptr && SeparatedCond != nullptr &&
2406 Init != nullptr && Inc != nullptr;
2407 }
2408 void clear(unsigned size) {
2409 IterationVarRef = nullptr;
2410 LastIteration = nullptr;
2411 CalcLastIteration = nullptr;
2412 PreCond = nullptr;
2413 Cond = nullptr;
2414 SeparatedCond = nullptr;
2415 Init = nullptr;
2416 Inc = nullptr;
2417 Counters.resize(size);
2418 Updates.resize(size);
2419 Finals.resize(size);
2420 for (unsigned i = 0; i < size; ++i) {
2421 Counters[i] = nullptr;
2422 Updates[i] = nullptr;
2423 Finals[i] = nullptr;
2424 }
2425 }
2426};
2427
Alexey Bataev23b69422014-06-18 07:08:49 +00002428} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002429
2430/// \brief Called on a for stmt to check and extract its iteration space
2431/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00002432static bool CheckOpenMPIterationSpace(
2433 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
2434 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
2435 Expr *NestedLoopCountExpr,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002436 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
2437 LoopIterationSpace &ResultIterSpace) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002438 // OpenMP [2.6, Canonical Loop Form]
2439 // for (init-expr; test-expr; incr-expr) structured-block
2440 auto For = dyn_cast_or_null<ForStmt>(S);
2441 if (!For) {
2442 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002443 << (NestedLoopCountExpr != nullptr) << getOpenMPDirectiveName(DKind)
2444 << NestedLoopCount << (CurrentNestedLoopCount > 0)
2445 << CurrentNestedLoopCount;
2446 if (NestedLoopCount > 1)
2447 SemaRef.Diag(NestedLoopCountExpr->getExprLoc(),
2448 diag::note_omp_collapse_expr)
2449 << NestedLoopCountExpr->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002450 return true;
2451 }
2452 assert(For->getBody());
2453
2454 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
2455
2456 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002457 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002458 if (ISC.CheckInit(Init)) {
2459 return true;
2460 }
2461
2462 bool HasErrors = false;
2463
2464 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002465 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002466
2467 // OpenMP [2.6, Canonical Loop Form]
2468 // Var is one of the following:
2469 // A variable of signed or unsigned integer type.
2470 // For C++, a variable of a random access iterator type.
2471 // For C, a variable of a pointer type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002472 auto VarType = Var->getType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002473 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
2474 !VarType->isPointerType() &&
2475 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
2476 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
2477 << SemaRef.getLangOpts().CPlusPlus;
2478 HasErrors = true;
2479 }
2480
Alexey Bataev4acb8592014-07-07 13:01:15 +00002481 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
2482 // Construct
2483 // The loop iteration variable(s) in the associated for-loop(s) of a for or
2484 // parallel for construct is (are) private.
2485 // The loop iteration variable in the associated for-loop of a simd construct
2486 // with just one associated for-loop is linear with a constant-linear-step
2487 // that is the increment of the associated for-loop.
2488 // Exclude loop var from the list of variables with implicitly defined data
2489 // sharing attributes.
Benjamin Kramerad8e0792014-10-10 15:32:48 +00002490 VarsWithImplicitDSA.erase(Var);
Alexey Bataev4acb8592014-07-07 13:01:15 +00002491
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002492 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
2493 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00002494 // The loop iteration variable in the associated for-loop of a simd construct
2495 // with just one associated for-loop may be listed in a linear clause with a
2496 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002497 // The loop iteration variable(s) in the associated for-loop(s) of a for or
2498 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002499 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002500 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
2501 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
2502 // declared in the loop and it is predetermined as a private.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002503 auto PredeterminedCKind =
2504 isOpenMPSimdDirective(DKind)
2505 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
2506 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002507 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00002508 DVar.CKind != PredeterminedCKind) ||
Alexander Musmanf82886e2014-09-18 05:12:34 +00002509 (isOpenMPWorksharingDirective(DKind) && !isOpenMPSimdDirective(DKind) &&
2510 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private &&
2511 DVar.CKind != OMPC_lastprivate)) &&
Alexander Musman1bb328c2014-06-04 13:06:39 +00002512 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002513 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00002514 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
2515 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002516 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002517 HasErrors = true;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002518 } else if (LoopVarRefExpr != nullptr) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00002519 // Make the loop iteration variable private (for worksharing constructs),
2520 // linear (for simd directives with the only one associated loop) or
2521 // lastprivate (for simd directives with several collapsed loops).
Alexey Bataev9aba41c2014-11-14 04:08:45 +00002522 // FIXME: the next check and error message must be removed once the
2523 // capturing of global variables in loops is fixed.
2524 if (DVar.CKind == OMPC_unknown)
2525 DVar = DSA.hasDSA(Var, isOpenMPPrivate, MatchesAlways(),
2526 /*FromParent=*/false);
2527 if (!Var->hasLocalStorage() && DVar.CKind == OMPC_unknown) {
2528 SemaRef.Diag(Init->getLocStart(), diag::err_omp_global_loop_var_dsa)
2529 << getOpenMPClauseName(PredeterminedCKind)
2530 << getOpenMPDirectiveName(DKind);
2531 HasErrors = true;
2532 } else
2533 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002534 }
2535
Alexey Bataev7ff55242014-06-19 09:13:45 +00002536 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00002537
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002538 // Check test-expr.
2539 HasErrors |= ISC.CheckCond(For->getCond());
2540
2541 // Check incr-expr.
2542 HasErrors |= ISC.CheckInc(For->getInc());
2543
Alexander Musmana5f070a2014-10-01 06:03:56 +00002544 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002545 return HasErrors;
2546
Alexander Musmana5f070a2014-10-01 06:03:56 +00002547 // Build the loop's iteration space representation.
Alexander Musman174b3ca2014-10-06 11:16:29 +00002548 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
2549 DSA.getCurScope(), /* LimitedType */ isOpenMPWorksharingDirective(DKind));
Alexander Musmana5f070a2014-10-01 06:03:56 +00002550 ResultIterSpace.CounterVar = ISC.BuildCounterVar();
2551 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
2552 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
2553 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
2554 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
2555 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
2556 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
2557
2558 HasErrors |= (ResultIterSpace.NumIterations == nullptr ||
2559 ResultIterSpace.CounterVar == nullptr ||
2560 ResultIterSpace.CounterInit == nullptr ||
2561 ResultIterSpace.CounterStep == nullptr);
2562
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002563 return HasErrors;
2564}
2565
Alexander Musmana5f070a2014-10-01 06:03:56 +00002566/// \brief Build a variable declaration for OpenMP loop iteration variable.
2567static VarDecl *BuildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
2568 StringRef Name) {
2569 DeclContext *DC = SemaRef.CurContext;
2570 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
2571 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
2572 VarDecl *Decl =
2573 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
2574 Decl->setImplicit();
2575 return Decl;
2576}
2577
2578/// \brief Build 'VarRef = Start + Iter * Step'.
2579static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S,
2580 SourceLocation Loc, ExprResult VarRef,
2581 ExprResult Start, ExprResult Iter,
2582 ExprResult Step, bool Subtract) {
2583 // Add parentheses (for debugging purposes only).
2584 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
2585 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
2586 !Step.isUsable())
2587 return ExprError();
2588
2589 ExprResult Update = SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(),
2590 Step.get()->IgnoreImplicit());
2591 if (!Update.isUsable())
2592 return ExprError();
2593
2594 // Build 'VarRef = Start + Iter * Step'.
2595 Update = SemaRef.BuildBinOp(S, Loc, (Subtract ? BO_Sub : BO_Add),
2596 Start.get()->IgnoreImplicit(), Update.get());
2597 if (!Update.isUsable())
2598 return ExprError();
2599
2600 Update = SemaRef.PerformImplicitConversion(
2601 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
2602 if (!Update.isUsable())
2603 return ExprError();
2604
2605 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
2606 return Update;
2607}
2608
2609/// \brief Convert integer expression \a E to make it have at least \a Bits
2610/// bits.
2611static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
2612 Sema &SemaRef) {
2613 if (E == nullptr)
2614 return ExprError();
2615 auto &C = SemaRef.Context;
2616 QualType OldType = E->getType();
2617 unsigned HasBits = C.getTypeSize(OldType);
2618 if (HasBits >= Bits)
2619 return ExprResult(E);
2620 // OK to convert to signed, because new type has more bits than old.
2621 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
2622 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
2623 true);
2624}
2625
2626/// \brief Check if the given expression \a E is a constant integer that fits
2627/// into \a Bits bits.
2628static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
2629 if (E == nullptr)
2630 return false;
2631 llvm::APSInt Result;
2632 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
2633 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
2634 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002635}
2636
2637/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00002638/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
2639/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002640static unsigned
2641CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *NestedLoopCountExpr,
2642 Stmt *AStmt, Sema &SemaRef, DSAStackTy &DSA,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002643 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
2644 BuiltLoopExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002645 unsigned NestedLoopCount = 1;
2646 if (NestedLoopCountExpr) {
2647 // Found 'collapse' clause - calculate collapse number.
2648 llvm::APSInt Result;
2649 if (NestedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
2650 NestedLoopCount = Result.getLimitedValue();
2651 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002652 // This is helper routine for loop directives (e.g., 'for', 'simd',
2653 // 'for simd', etc.).
Alexander Musmana5f070a2014-10-01 06:03:56 +00002654 SmallVector<LoopIterationSpace, 4> IterSpaces;
2655 IterSpaces.resize(NestedLoopCount);
2656 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002657 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002658 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev4acb8592014-07-07 13:01:15 +00002659 NestedLoopCount, NestedLoopCountExpr,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002660 VarsWithImplicitDSA, IterSpaces[Cnt]))
Alexey Bataevabfc0692014-06-25 06:52:00 +00002661 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002662 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00002663 // OpenMP [2.8.1, simd construct, Restrictions]
2664 // All loops associated with the construct must be perfectly nested; that
2665 // is, there must be no intervening code nor any OpenMP directive between
2666 // any two loops.
2667 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002668 }
2669
Alexander Musmana5f070a2014-10-01 06:03:56 +00002670 Built.clear(/* size */ NestedLoopCount);
2671
2672 if (SemaRef.CurContext->isDependentContext())
2673 return NestedLoopCount;
2674
2675 // An example of what is generated for the following code:
2676 //
2677 // #pragma omp simd collapse(2)
2678 // for (i = 0; i < NI; ++i)
2679 // for (j = J0; j < NJ; j+=2) {
2680 // <loop body>
2681 // }
2682 //
2683 // We generate the code below.
2684 // Note: the loop body may be outlined in CodeGen.
2685 // Note: some counters may be C++ classes, operator- is used to find number of
2686 // iterations and operator+= to calculate counter value.
2687 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
2688 // or i64 is currently supported).
2689 //
2690 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
2691 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
2692 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
2693 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
2694 // // similar updates for vars in clauses (e.g. 'linear')
2695 // <loop body (using local i and j)>
2696 // }
2697 // i = NI; // assign final values of counters
2698 // j = NJ;
2699 //
2700
2701 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
2702 // the iteration counts of the collapsed for loops.
2703 auto N0 = IterSpaces[0].NumIterations;
2704 ExprResult LastIteration32 = WidenIterationCount(32 /* Bits */, N0, SemaRef);
2705 ExprResult LastIteration64 = WidenIterationCount(64 /* Bits */, N0, SemaRef);
2706
2707 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
2708 return NestedLoopCount;
2709
2710 auto &C = SemaRef.Context;
2711 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
2712
2713 Scope *CurScope = DSA.getCurScope();
2714 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
2715 auto N = IterSpaces[Cnt].NumIterations;
2716 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
2717 if (LastIteration32.isUsable())
2718 LastIteration32 = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_Mul,
2719 LastIteration32.get(), N);
2720 if (LastIteration64.isUsable())
2721 LastIteration64 = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_Mul,
2722 LastIteration64.get(), N);
2723 }
2724
2725 // Choose either the 32-bit or 64-bit version.
2726 ExprResult LastIteration = LastIteration64;
2727 if (LastIteration32.isUsable() &&
2728 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
2729 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
2730 FitsInto(
2731 32 /* Bits */,
2732 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
2733 LastIteration64.get(), SemaRef)))
2734 LastIteration = LastIteration32;
2735
2736 if (!LastIteration.isUsable())
2737 return 0;
2738
2739 // Save the number of iterations.
2740 ExprResult NumIterations = LastIteration;
2741 {
2742 LastIteration = SemaRef.BuildBinOp(
2743 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
2744 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
2745 if (!LastIteration.isUsable())
2746 return 0;
2747 }
2748
2749 // Calculate the last iteration number beforehand instead of doing this on
2750 // each iteration. Do not do this if the number of iterations may be kfold-ed.
2751 llvm::APSInt Result;
2752 bool IsConstant =
2753 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
2754 ExprResult CalcLastIteration;
2755 if (!IsConstant) {
2756 SourceLocation SaveLoc;
2757 VarDecl *SaveVar =
2758 BuildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(),
2759 ".omp.last.iteration");
2760 ExprResult SaveRef = SemaRef.BuildDeclRefExpr(
2761 SaveVar, LastIteration.get()->getType(), VK_LValue, SaveLoc);
2762 CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign,
2763 SaveRef.get(), LastIteration.get());
2764 LastIteration = SaveRef;
2765
2766 // Prepare SaveRef + 1.
2767 NumIterations = SemaRef.BuildBinOp(
2768 CurScope, SaveLoc, BO_Add, SaveRef.get(),
2769 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
2770 if (!NumIterations.isUsable())
2771 return 0;
2772 }
2773
2774 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
2775
2776 // Precondition tests if there is at least one iteration (LastIteration > 0).
2777 ExprResult PreCond = SemaRef.BuildBinOp(
2778 CurScope, InitLoc, BO_GT, LastIteration.get(),
2779 SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get());
2780
2781 // Build the iteration variable and its initialization to zero before loop.
2782 ExprResult IV;
2783 ExprResult Init;
2784 {
2785 VarDecl *IVDecl = BuildVarDecl(SemaRef, InitLoc,
2786 LastIteration.get()->getType(), ".omp.iv");
2787 IV = SemaRef.BuildDeclRefExpr(IVDecl, LastIteration.get()->getType(),
2788 VK_LValue, InitLoc);
2789 Init = SemaRef.BuildBinOp(
2790 CurScope, InitLoc, BO_Assign, IV.get(),
2791 SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get());
2792 }
2793
2794 // Loop condition (IV < NumIterations)
2795 SourceLocation CondLoc;
2796 ExprResult Cond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
2797 NumIterations.get());
2798 // Loop condition with 1 iteration separated (IV < LastIteration)
2799 ExprResult SeparatedCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT,
2800 IV.get(), LastIteration.get());
2801
2802 // Loop increment (IV = IV + 1)
2803 SourceLocation IncLoc;
2804 ExprResult Inc =
2805 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
2806 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
2807 if (!Inc.isUsable())
2808 return 0;
2809 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
2810
2811 // Build updates and final values of the loop counters.
2812 bool HasErrors = false;
2813 Built.Counters.resize(NestedLoopCount);
2814 Built.Updates.resize(NestedLoopCount);
2815 Built.Finals.resize(NestedLoopCount);
2816 {
2817 ExprResult Div;
2818 // Go from inner nested loop to outer.
2819 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
2820 LoopIterationSpace &IS = IterSpaces[Cnt];
2821 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
2822 // Build: Iter = (IV / Div) % IS.NumIters
2823 // where Div is product of previous iterations' IS.NumIters.
2824 ExprResult Iter;
2825 if (Div.isUsable()) {
2826 Iter =
2827 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
2828 } else {
2829 Iter = IV;
2830 assert((Cnt == (int)NestedLoopCount - 1) &&
2831 "unusable div expected on first iteration only");
2832 }
2833
2834 if (Cnt != 0 && Iter.isUsable())
2835 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
2836 IS.NumIterations);
2837 if (!Iter.isUsable()) {
2838 HasErrors = true;
2839 break;
2840 }
2841
2842 // Build update: IS.CounterVar = IS.Start + Iter * IS.Step
2843 ExprResult Update =
2844 BuildCounterUpdate(SemaRef, CurScope, UpdLoc, IS.CounterVar,
2845 IS.CounterInit, Iter, IS.CounterStep, IS.Subtract);
2846 if (!Update.isUsable()) {
2847 HasErrors = true;
2848 break;
2849 }
2850
2851 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
2852 ExprResult Final = BuildCounterUpdate(
2853 SemaRef, CurScope, UpdLoc, IS.CounterVar, IS.CounterInit,
2854 IS.NumIterations, IS.CounterStep, IS.Subtract);
2855 if (!Final.isUsable()) {
2856 HasErrors = true;
2857 break;
2858 }
2859
2860 // Build Div for the next iteration: Div <- Div * IS.NumIters
2861 if (Cnt != 0) {
2862 if (Div.isUnset())
2863 Div = IS.NumIterations;
2864 else
2865 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
2866 IS.NumIterations);
2867
2868 // Add parentheses (for debugging purposes only).
2869 if (Div.isUsable())
2870 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
2871 if (!Div.isUsable()) {
2872 HasErrors = true;
2873 break;
2874 }
2875 }
2876 if (!Update.isUsable() || !Final.isUsable()) {
2877 HasErrors = true;
2878 break;
2879 }
2880 // Save results
2881 Built.Counters[Cnt] = IS.CounterVar;
2882 Built.Updates[Cnt] = Update.get();
2883 Built.Finals[Cnt] = Final.get();
2884 }
2885 }
2886
2887 if (HasErrors)
2888 return 0;
2889
2890 // Save results
2891 Built.IterationVarRef = IV.get();
2892 Built.LastIteration = LastIteration.get();
2893 Built.CalcLastIteration = CalcLastIteration.get();
2894 Built.PreCond = PreCond.get();
2895 Built.Cond = Cond.get();
2896 Built.SeparatedCond = SeparatedCond.get();
2897 Built.Init = Init.get();
2898 Built.Inc = Inc.get();
2899
Alexey Bataevabfc0692014-06-25 06:52:00 +00002900 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002901}
2902
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002903static Expr *GetCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002904 auto CollapseFilter = [](const OMPClause *C) -> bool {
2905 return C->getClauseKind() == OMPC_collapse;
2906 };
2907 OMPExecutableDirective::filtered_clause_iterator<decltype(CollapseFilter)> I(
2908 Clauses, CollapseFilter);
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002909 if (I)
2910 return cast<OMPCollapseClause>(*I)->getNumForLoops();
2911 return nullptr;
2912}
2913
Alexey Bataev4acb8592014-07-07 13:01:15 +00002914StmtResult Sema::ActOnOpenMPSimdDirective(
2915 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2916 SourceLocation EndLoc,
2917 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00002918 BuiltLoopExprs B;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002919 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002920 unsigned NestedLoopCount =
2921 CheckOpenMPLoop(OMPD_simd, GetCollapseNumberExpr(Clauses), AStmt, *this,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002922 *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00002923 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002924 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002925
Alexander Musmana5f070a2014-10-01 06:03:56 +00002926 assert((CurContext->isDependentContext() || B.builtAll()) &&
2927 "omp simd loop exprs were not built");
2928
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002929 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00002930 return OMPSimdDirective::Create(
2931 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt,
2932 B.IterationVarRef, B.LastIteration, B.CalcLastIteration, B.PreCond,
2933 B.Cond, B.SeparatedCond, B.Init, B.Inc, B.Counters, B.Updates, B.Finals);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002934}
2935
Alexey Bataev4acb8592014-07-07 13:01:15 +00002936StmtResult Sema::ActOnOpenMPForDirective(
2937 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2938 SourceLocation EndLoc,
2939 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00002940 BuiltLoopExprs B;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002941 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002942 unsigned NestedLoopCount =
2943 CheckOpenMPLoop(OMPD_for, GetCollapseNumberExpr(Clauses), AStmt, *this,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002944 *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00002945 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00002946 return StmtError();
2947
Alexander Musmana5f070a2014-10-01 06:03:56 +00002948 assert((CurContext->isDependentContext() || B.builtAll()) &&
2949 "omp for loop exprs were not built");
2950
Alexey Bataevf29276e2014-06-18 04:14:57 +00002951 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00002952 return OMPForDirective::Create(
2953 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt,
2954 B.IterationVarRef, B.LastIteration, B.CalcLastIteration, B.PreCond,
2955 B.Cond, B.SeparatedCond, B.Init, B.Inc, B.Counters, B.Updates, B.Finals);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002956}
2957
Alexander Musmanf82886e2014-09-18 05:12:34 +00002958StmtResult Sema::ActOnOpenMPForSimdDirective(
2959 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2960 SourceLocation EndLoc,
2961 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00002962 BuiltLoopExprs B;
Alexander Musmanf82886e2014-09-18 05:12:34 +00002963 // In presence of clause 'collapse', it will define the nested loops number.
2964 unsigned NestedLoopCount =
2965 CheckOpenMPLoop(OMPD_for_simd, GetCollapseNumberExpr(Clauses), AStmt,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002966 *this, *DSAStack, VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00002967 if (NestedLoopCount == 0)
2968 return StmtError();
2969
2970 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00002971 return OMPForSimdDirective::Create(
2972 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt,
2973 B.IterationVarRef, B.LastIteration, B.CalcLastIteration, B.PreCond,
2974 B.Cond, B.SeparatedCond, B.Init, B.Inc, B.Counters, B.Updates, B.Finals);
Alexander Musmanf82886e2014-09-18 05:12:34 +00002975}
2976
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002977StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
2978 Stmt *AStmt,
2979 SourceLocation StartLoc,
2980 SourceLocation EndLoc) {
2981 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2982 auto BaseStmt = AStmt;
2983 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
2984 BaseStmt = CS->getCapturedStmt();
2985 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
2986 auto S = C->children();
2987 if (!S)
2988 return StmtError();
2989 // All associated statements must be '#pragma omp section' except for
2990 // the first one.
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002991 for (++S; S; ++S) {
2992 auto SectionStmt = *S;
2993 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
2994 if (SectionStmt)
2995 Diag(SectionStmt->getLocStart(),
2996 diag::err_omp_sections_substmt_not_section);
2997 return StmtError();
2998 }
2999 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003000 } else {
3001 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
3002 return StmtError();
3003 }
3004
3005 getCurFunction()->setHasBranchProtectedScope();
3006
3007 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses,
3008 AStmt);
3009}
3010
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003011StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
3012 SourceLocation StartLoc,
3013 SourceLocation EndLoc) {
3014 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3015
3016 getCurFunction()->setHasBranchProtectedScope();
3017
3018 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt);
3019}
3020
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003021StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
3022 Stmt *AStmt,
3023 SourceLocation StartLoc,
3024 SourceLocation EndLoc) {
Alexey Bataev74a05c92014-07-15 02:55:09 +00003025 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3026
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003027 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00003028
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003029 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3030}
3031
Alexander Musman80c22892014-07-17 08:54:58 +00003032StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
3033 SourceLocation StartLoc,
3034 SourceLocation EndLoc) {
3035 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3036
3037 getCurFunction()->setHasBranchProtectedScope();
3038
3039 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
3040}
3041
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003042StmtResult
3043Sema::ActOnOpenMPCriticalDirective(const DeclarationNameInfo &DirName,
3044 Stmt *AStmt, SourceLocation StartLoc,
3045 SourceLocation EndLoc) {
3046 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3047
3048 getCurFunction()->setHasBranchProtectedScope();
3049
3050 return OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
3051 AStmt);
3052}
3053
Alexey Bataev4acb8592014-07-07 13:01:15 +00003054StmtResult Sema::ActOnOpenMPParallelForDirective(
3055 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3056 SourceLocation EndLoc,
3057 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
3058 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3059 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3060 // 1.2.2 OpenMP Language Terminology
3061 // Structured block - An executable statement with a single entry at the
3062 // top and a single exit at the bottom.
3063 // The point of exit cannot be a branch out of the structured block.
3064 // longjmp() and throw() must not violate the entry/exit criteria.
3065 CS->getCapturedDecl()->setNothrow();
3066
Alexander Musmana5f070a2014-10-01 06:03:56 +00003067 BuiltLoopExprs B;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003068 // In presence of clause 'collapse', it will define the nested loops number.
3069 unsigned NestedLoopCount =
3070 CheckOpenMPLoop(OMPD_parallel_for, GetCollapseNumberExpr(Clauses), AStmt,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003071 *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003072 if (NestedLoopCount == 0)
3073 return StmtError();
3074
Alexander Musmana5f070a2014-10-01 06:03:56 +00003075 assert((CurContext->isDependentContext() || B.builtAll()) &&
3076 "omp parallel for loop exprs were not built");
3077
Alexey Bataev4acb8592014-07-07 13:01:15 +00003078 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003079 return OMPParallelForDirective::Create(
3080 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt,
3081 B.IterationVarRef, B.LastIteration, B.CalcLastIteration, B.PreCond,
3082 B.Cond, B.SeparatedCond, B.Init, B.Inc, B.Counters, B.Updates, B.Finals);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003083}
3084
Alexander Musmane4e893b2014-09-23 09:33:00 +00003085StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
3086 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3087 SourceLocation EndLoc,
3088 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
3089 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3090 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3091 // 1.2.2 OpenMP Language Terminology
3092 // Structured block - An executable statement with a single entry at the
3093 // top and a single exit at the bottom.
3094 // The point of exit cannot be a branch out of the structured block.
3095 // longjmp() and throw() must not violate the entry/exit criteria.
3096 CS->getCapturedDecl()->setNothrow();
3097
Alexander Musmana5f070a2014-10-01 06:03:56 +00003098 BuiltLoopExprs B;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003099 // In presence of clause 'collapse', it will define the nested loops number.
3100 unsigned NestedLoopCount =
3101 CheckOpenMPLoop(OMPD_parallel_for_simd, GetCollapseNumberExpr(Clauses),
Alexander Musmana5f070a2014-10-01 06:03:56 +00003102 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003103 if (NestedLoopCount == 0)
3104 return StmtError();
3105
3106 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003107 return OMPParallelForSimdDirective::Create(
3108 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt,
3109 B.IterationVarRef, B.LastIteration, B.CalcLastIteration, B.PreCond,
3110 B.Cond, B.SeparatedCond, B.Init, B.Inc, B.Counters, B.Updates, B.Finals);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003111}
3112
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003113StmtResult
3114Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
3115 Stmt *AStmt, SourceLocation StartLoc,
3116 SourceLocation EndLoc) {
3117 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3118 auto BaseStmt = AStmt;
3119 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
3120 BaseStmt = CS->getCapturedStmt();
3121 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
3122 auto S = C->children();
3123 if (!S)
3124 return StmtError();
3125 // All associated statements must be '#pragma omp section' except for
3126 // the first one.
3127 for (++S; S; ++S) {
3128 auto SectionStmt = *S;
3129 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
3130 if (SectionStmt)
3131 Diag(SectionStmt->getLocStart(),
3132 diag::err_omp_parallel_sections_substmt_not_section);
3133 return StmtError();
3134 }
3135 }
3136 } else {
3137 Diag(AStmt->getLocStart(),
3138 diag::err_omp_parallel_sections_not_compound_stmt);
3139 return StmtError();
3140 }
3141
3142 getCurFunction()->setHasBranchProtectedScope();
3143
3144 return OMPParallelSectionsDirective::Create(Context, StartLoc, EndLoc,
3145 Clauses, AStmt);
3146}
3147
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003148StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
3149 Stmt *AStmt, SourceLocation StartLoc,
3150 SourceLocation EndLoc) {
3151 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3152 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3153 // 1.2.2 OpenMP Language Terminology
3154 // Structured block - An executable statement with a single entry at the
3155 // top and a single exit at the bottom.
3156 // The point of exit cannot be a branch out of the structured block.
3157 // longjmp() and throw() must not violate the entry/exit criteria.
3158 CS->getCapturedDecl()->setNothrow();
3159
3160 getCurFunction()->setHasBranchProtectedScope();
3161
3162 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3163}
3164
Alexey Bataev68446b72014-07-18 07:47:19 +00003165StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
3166 SourceLocation EndLoc) {
3167 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
3168}
3169
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003170StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
3171 SourceLocation EndLoc) {
3172 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
3173}
3174
Alexey Bataev2df347a2014-07-18 10:17:07 +00003175StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
3176 SourceLocation EndLoc) {
3177 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
3178}
3179
Alexey Bataev6125da92014-07-21 11:26:11 +00003180StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
3181 SourceLocation StartLoc,
3182 SourceLocation EndLoc) {
3183 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
3184 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
3185}
3186
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003187StmtResult Sema::ActOnOpenMPOrderedDirective(Stmt *AStmt,
3188 SourceLocation StartLoc,
3189 SourceLocation EndLoc) {
3190 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3191
3192 getCurFunction()->setHasBranchProtectedScope();
3193
3194 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, AStmt);
3195}
3196
Alexey Bataev0162e452014-07-22 10:10:35 +00003197StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
3198 Stmt *AStmt,
3199 SourceLocation StartLoc,
3200 SourceLocation EndLoc) {
3201 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003202 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00003203 // 1.2.2 OpenMP Language Terminology
3204 // Structured block - An executable statement with a single entry at the
3205 // top and a single exit at the bottom.
3206 // The point of exit cannot be a branch out of the structured block.
3207 // longjmp() and throw() must not violate the entry/exit criteria.
3208 // TODO further analysis of associated statements and clauses.
Alexey Bataevdea47612014-07-23 07:46:59 +00003209 OpenMPClauseKind AtomicKind = OMPC_unknown;
3210 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003211 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00003212 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00003213 C->getClauseKind() == OMPC_update ||
3214 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00003215 if (AtomicKind != OMPC_unknown) {
3216 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
3217 << SourceRange(C->getLocStart(), C->getLocEnd());
3218 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
3219 << getOpenMPClauseName(AtomicKind);
3220 } else {
3221 AtomicKind = C->getClauseKind();
3222 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003223 }
3224 }
3225 }
Alexey Bataev62cec442014-11-18 10:14:22 +00003226
Alexey Bataev459dec02014-07-24 06:46:57 +00003227 auto Body = CS->getCapturedStmt();
Alexey Bataev62cec442014-11-18 10:14:22 +00003228 Expr *X = nullptr;
3229 Expr *V = nullptr;
3230 Expr *E = nullptr;
3231 // OpenMP [2.12.6, atomic Construct]
3232 // In the next expressions:
3233 // * x and v (as applicable) are both l-value expressions with scalar type.
3234 // * During the execution of an atomic region, multiple syntactic
3235 // occurrences of x must designate the same storage location.
3236 // * Neither of v and expr (as applicable) may access the storage location
3237 // designated by x.
3238 // * Neither of x and expr (as applicable) may access the storage location
3239 // designated by v.
3240 // * expr is an expression with scalar type.
3241 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
3242 // * binop, binop=, ++, and -- are not overloaded operators.
3243 // * The expression x binop expr must be numerically equivalent to x binop
3244 // (expr). This requirement is satisfied if the operators in expr have
3245 // precedence greater than binop, or by using parentheses around expr or
3246 // subexpressions of expr.
3247 // * The expression expr binop x must be numerically equivalent to (expr)
3248 // binop x. This requirement is satisfied if the operators in expr have
3249 // precedence equal to or greater than binop, or by using parentheses around
3250 // expr or subexpressions of expr.
3251 // * For forms that allow multiple occurrences of x, the number of times
3252 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00003253 if (AtomicKind == OMPC_read) {
Alexey Bataev62cec442014-11-18 10:14:22 +00003254 enum {
3255 NotAnExpression,
3256 NotAnAssignmentOp,
3257 NotAScalarType,
3258 NotAnLValue,
3259 NoError
3260 } ErrorFound = NoError;
3261 SourceLocation ErrorLoc, NoteLoc;
3262 SourceRange ErrorRange, NoteRange;
3263 // If clause is read:
3264 // v = x;
3265 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
3266 auto AtomicBinOp =
3267 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
3268 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
3269 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
3270 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
3271 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
3272 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
3273 if (!X->isLValue() || !V->isLValue()) {
3274 auto NotLValueExpr = X->isLValue() ? V : X;
3275 ErrorFound = NotAnLValue;
3276 ErrorLoc = AtomicBinOp->getExprLoc();
3277 ErrorRange = AtomicBinOp->getSourceRange();
3278 NoteLoc = NotLValueExpr->getExprLoc();
3279 NoteRange = NotLValueExpr->getSourceRange();
3280 }
3281 } else if (!X->isInstantiationDependent() ||
3282 !V->isInstantiationDependent()) {
3283 auto NotScalarExpr =
3284 (X->isInstantiationDependent() || X->getType()->isScalarType())
3285 ? V
3286 : X;
3287 ErrorFound = NotAScalarType;
3288 ErrorLoc = AtomicBinOp->getExprLoc();
3289 ErrorRange = AtomicBinOp->getSourceRange();
3290 NoteLoc = NotScalarExpr->getExprLoc();
3291 NoteRange = NotScalarExpr->getSourceRange();
3292 }
3293 } else {
3294 ErrorFound = NotAnAssignmentOp;
3295 ErrorLoc = AtomicBody->getExprLoc();
3296 ErrorRange = AtomicBody->getSourceRange();
3297 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
3298 : AtomicBody->getExprLoc();
3299 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
3300 : AtomicBody->getSourceRange();
3301 }
3302 } else {
3303 ErrorFound = NotAnExpression;
3304 NoteLoc = ErrorLoc = Body->getLocStart();
3305 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00003306 }
Alexey Bataev62cec442014-11-18 10:14:22 +00003307 if (ErrorFound != NoError) {
3308 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
3309 << ErrorRange;
3310 Diag(NoteLoc, diag::note_omp_atomic_read) << ErrorFound << NoteRange;
3311 return StmtError();
3312 } else if (CurContext->isDependentContext())
3313 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00003314 } else if (AtomicKind == OMPC_write) {
Alexey Bataev459dec02014-07-24 06:46:57 +00003315 if (!isa<Expr>(Body)) {
3316 Diag(Body->getLocStart(),
Alexey Bataevdea47612014-07-23 07:46:59 +00003317 diag::err_omp_atomic_write_not_expression_statement);
3318 return StmtError();
3319 }
Alexey Bataev67a4f222014-07-23 10:25:33 +00003320 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev459dec02014-07-24 06:46:57 +00003321 if (!isa<Expr>(Body)) {
3322 Diag(Body->getLocStart(),
Alexey Bataev67a4f222014-07-23 10:25:33 +00003323 diag::err_omp_atomic_update_not_expression_statement)
3324 << (AtomicKind == OMPC_update);
3325 return StmtError();
3326 }
Alexey Bataev459dec02014-07-24 06:46:57 +00003327 } else if (AtomicKind == OMPC_capture) {
3328 if (isa<Expr>(Body) && !isa<BinaryOperator>(Body)) {
3329 Diag(Body->getLocStart(),
3330 diag::err_omp_atomic_capture_not_expression_statement);
3331 return StmtError();
3332 } else if (!isa<Expr>(Body) && !isa<CompoundStmt>(Body)) {
3333 Diag(Body->getLocStart(),
3334 diag::err_omp_atomic_capture_not_compound_statement);
3335 return StmtError();
3336 }
Alexey Bataevdea47612014-07-23 07:46:59 +00003337 }
Alexey Bataev0162e452014-07-22 10:10:35 +00003338
3339 getCurFunction()->setHasBranchProtectedScope();
3340
Alexey Bataev62cec442014-11-18 10:14:22 +00003341 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3342 X, V, E);
Alexey Bataev0162e452014-07-22 10:10:35 +00003343}
3344
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003345StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
3346 Stmt *AStmt,
3347 SourceLocation StartLoc,
3348 SourceLocation EndLoc) {
3349 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3350
Alexey Bataev13314bf2014-10-09 04:18:56 +00003351 // OpenMP [2.16, Nesting of Regions]
3352 // If specified, a teams construct must be contained within a target
3353 // construct. That target construct must contain no statements or directives
3354 // outside of the teams construct.
3355 if (DSAStack->hasInnerTeamsRegion()) {
3356 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
3357 bool OMPTeamsFound = true;
3358 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
3359 auto I = CS->body_begin();
3360 while (I != CS->body_end()) {
3361 auto OED = dyn_cast<OMPExecutableDirective>(*I);
3362 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
3363 OMPTeamsFound = false;
3364 break;
3365 }
3366 ++I;
3367 }
3368 assert(I != CS->body_end() && "Not found statement");
3369 S = *I;
3370 }
3371 if (!OMPTeamsFound) {
3372 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
3373 Diag(DSAStack->getInnerTeamsRegionLoc(),
3374 diag::note_omp_nested_teams_construct_here);
3375 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
3376 << isa<OMPExecutableDirective>(S);
3377 return StmtError();
3378 }
3379 }
3380
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003381 getCurFunction()->setHasBranchProtectedScope();
3382
3383 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3384}
3385
Alexey Bataev13314bf2014-10-09 04:18:56 +00003386StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
3387 Stmt *AStmt, SourceLocation StartLoc,
3388 SourceLocation EndLoc) {
3389 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3390 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3391 // 1.2.2 OpenMP Language Terminology
3392 // Structured block - An executable statement with a single entry at the
3393 // top and a single exit at the bottom.
3394 // The point of exit cannot be a branch out of the structured block.
3395 // longjmp() and throw() must not violate the entry/exit criteria.
3396 CS->getCapturedDecl()->setNothrow();
3397
3398 getCurFunction()->setHasBranchProtectedScope();
3399
3400 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3401}
3402
Alexey Bataeved09d242014-05-28 05:53:51 +00003403OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003404 SourceLocation StartLoc,
3405 SourceLocation LParenLoc,
3406 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003407 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003408 switch (Kind) {
3409 case OMPC_if:
3410 Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc);
3411 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00003412 case OMPC_final:
3413 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
3414 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00003415 case OMPC_num_threads:
3416 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
3417 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00003418 case OMPC_safelen:
3419 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
3420 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00003421 case OMPC_collapse:
3422 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
3423 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003424 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003425 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003426 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003427 case OMPC_private:
3428 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00003429 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003430 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00003431 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00003432 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00003433 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003434 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003435 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003436 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00003437 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003438 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003439 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003440 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00003441 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003442 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00003443 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00003444 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00003445 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003446 case OMPC_seq_cst:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003447 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003448 llvm_unreachable("Clause is not allowed.");
3449 }
3450 return Res;
3451}
3452
Alexey Bataeved09d242014-05-28 05:53:51 +00003453OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003454 SourceLocation LParenLoc,
3455 SourceLocation EndLoc) {
3456 Expr *ValExpr = Condition;
3457 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
3458 !Condition->isInstantiationDependent() &&
3459 !Condition->containsUnexpandedParameterPack()) {
3460 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00003461 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003462 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003463 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003464
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003465 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003466 }
3467
3468 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc);
3469}
3470
Alexey Bataev3778b602014-07-17 07:32:53 +00003471OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
3472 SourceLocation StartLoc,
3473 SourceLocation LParenLoc,
3474 SourceLocation EndLoc) {
3475 Expr *ValExpr = Condition;
3476 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
3477 !Condition->isInstantiationDependent() &&
3478 !Condition->containsUnexpandedParameterPack()) {
3479 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
3480 Condition->getExprLoc(), Condition);
3481 if (Val.isInvalid())
3482 return nullptr;
3483
3484 ValExpr = Val.get();
3485 }
3486
3487 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
3488}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003489ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
3490 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00003491 if (!Op)
3492 return ExprError();
3493
3494 class IntConvertDiagnoser : public ICEConvertDiagnoser {
3495 public:
3496 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00003497 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00003498 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
3499 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003500 return S.Diag(Loc, diag::err_omp_not_integral) << T;
3501 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003502 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
3503 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003504 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
3505 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003506 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
3507 QualType T,
3508 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003509 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
3510 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003511 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
3512 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003513 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00003514 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00003515 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003516 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
3517 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003518 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
3519 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003520 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
3521 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003522 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00003523 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00003524 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003525 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
3526 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003527 llvm_unreachable("conversion functions are permitted");
3528 }
3529 } ConvertDiagnoser;
3530 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
3531}
3532
3533OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
3534 SourceLocation StartLoc,
3535 SourceLocation LParenLoc,
3536 SourceLocation EndLoc) {
3537 Expr *ValExpr = NumThreads;
3538 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
Alexey Bataev568a8332014-03-06 06:15:19 +00003539 !NumThreads->containsUnexpandedParameterPack()) {
3540 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
3541 ExprResult Val =
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003542 PerformOpenMPImplicitIntegerConversion(NumThreadsLoc, NumThreads);
Alexey Bataev568a8332014-03-06 06:15:19 +00003543 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003544 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00003545
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003546 ValExpr = Val.get();
Alexey Bataev568a8332014-03-06 06:15:19 +00003547
3548 // OpenMP [2.5, Restrictions]
3549 // The num_threads expression must evaluate to a positive integer value.
3550 llvm::APSInt Result;
Alexey Bataeved09d242014-05-28 05:53:51 +00003551 if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() &&
3552 !Result.isStrictlyPositive()) {
Alexey Bataev568a8332014-03-06 06:15:19 +00003553 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
3554 << "num_threads" << NumThreads->getSourceRange();
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003555 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00003556 }
3557 }
3558
Alexey Bataeved09d242014-05-28 05:53:51 +00003559 return new (Context)
3560 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00003561}
3562
Alexey Bataev62c87d22014-03-21 04:51:18 +00003563ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
3564 OpenMPClauseKind CKind) {
3565 if (!E)
3566 return ExprError();
3567 if (E->isValueDependent() || E->isTypeDependent() ||
3568 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00003569 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00003570 llvm::APSInt Result;
3571 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
3572 if (ICE.isInvalid())
3573 return ExprError();
3574 if (!Result.isStrictlyPositive()) {
3575 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
3576 << getOpenMPClauseName(CKind) << E->getSourceRange();
3577 return ExprError();
3578 }
Alexander Musman09184fe2014-09-30 05:29:28 +00003579 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
3580 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
3581 << E->getSourceRange();
3582 return ExprError();
3583 }
Alexey Bataev62c87d22014-03-21 04:51:18 +00003584 return ICE;
3585}
3586
3587OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
3588 SourceLocation LParenLoc,
3589 SourceLocation EndLoc) {
3590 // OpenMP [2.8.1, simd construct, Description]
3591 // The parameter of the safelen clause must be a constant
3592 // positive integer expression.
3593 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
3594 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003595 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00003596 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003597 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00003598}
3599
Alexander Musman64d33f12014-06-04 07:53:32 +00003600OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
3601 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00003602 SourceLocation LParenLoc,
3603 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00003604 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00003605 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00003606 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00003607 // The parameter of the collapse clause must be a constant
3608 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00003609 ExprResult NumForLoopsResult =
3610 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
3611 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00003612 return nullptr;
3613 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00003614 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00003615}
3616
Alexey Bataeved09d242014-05-28 05:53:51 +00003617OMPClause *Sema::ActOnOpenMPSimpleClause(
3618 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
3619 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003620 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003621 switch (Kind) {
3622 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00003623 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00003624 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
3625 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003626 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003627 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00003628 Res = ActOnOpenMPProcBindClause(
3629 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
3630 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003631 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003632 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00003633 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00003634 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00003635 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00003636 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003637 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003638 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003639 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00003640 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00003641 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00003642 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00003643 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00003644 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003645 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003646 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003647 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00003648 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003649 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003650 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003651 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00003652 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003653 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00003654 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00003655 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00003656 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003657 case OMPC_seq_cst:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003658 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003659 llvm_unreachable("Clause is not allowed.");
3660 }
3661 return Res;
3662}
3663
3664OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
3665 SourceLocation KindKwLoc,
3666 SourceLocation StartLoc,
3667 SourceLocation LParenLoc,
3668 SourceLocation EndLoc) {
3669 if (Kind == OMPC_DEFAULT_unknown) {
3670 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00003671 static_assert(OMPC_DEFAULT_unknown > 0,
3672 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00003673 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00003674 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003675 Values += "'";
3676 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
3677 Values += "'";
3678 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00003679 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003680 Values += " or ";
3681 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00003682 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003683 break;
3684 default:
3685 Values += Sep;
3686 break;
3687 }
3688 }
3689 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00003690 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003691 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003692 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00003693 switch (Kind) {
3694 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003695 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003696 break;
3697 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003698 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003699 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003700 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003701 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00003702 break;
3703 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003704 return new (Context)
3705 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003706}
3707
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003708OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
3709 SourceLocation KindKwLoc,
3710 SourceLocation StartLoc,
3711 SourceLocation LParenLoc,
3712 SourceLocation EndLoc) {
3713 if (Kind == OMPC_PROC_BIND_unknown) {
3714 std::string Values;
3715 std::string Sep(", ");
3716 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
3717 Values += "'";
3718 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
3719 Values += "'";
3720 switch (i) {
3721 case OMPC_PROC_BIND_unknown - 2:
3722 Values += " or ";
3723 break;
3724 case OMPC_PROC_BIND_unknown - 1:
3725 break;
3726 default:
3727 Values += Sep;
3728 break;
3729 }
3730 }
3731 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00003732 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003733 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003734 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003735 return new (Context)
3736 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003737}
3738
Alexey Bataev56dafe82014-06-20 07:16:17 +00003739OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
3740 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
3741 SourceLocation StartLoc, SourceLocation LParenLoc,
3742 SourceLocation ArgumentLoc, SourceLocation CommaLoc,
3743 SourceLocation EndLoc) {
3744 OMPClause *Res = nullptr;
3745 switch (Kind) {
3746 case OMPC_schedule:
3747 Res = ActOnOpenMPScheduleClause(
3748 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
3749 LParenLoc, ArgumentLoc, CommaLoc, EndLoc);
3750 break;
3751 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00003752 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003753 case OMPC_num_threads:
3754 case OMPC_safelen:
3755 case OMPC_collapse:
3756 case OMPC_default:
3757 case OMPC_proc_bind:
3758 case OMPC_private:
3759 case OMPC_firstprivate:
3760 case OMPC_lastprivate:
3761 case OMPC_shared:
3762 case OMPC_reduction:
3763 case OMPC_linear:
3764 case OMPC_aligned:
3765 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003766 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003767 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00003768 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003769 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003770 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003771 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00003772 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003773 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00003774 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00003775 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00003776 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003777 case OMPC_seq_cst:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003778 case OMPC_unknown:
3779 llvm_unreachable("Clause is not allowed.");
3780 }
3781 return Res;
3782}
3783
3784OMPClause *Sema::ActOnOpenMPScheduleClause(
3785 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
3786 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
3787 SourceLocation EndLoc) {
3788 if (Kind == OMPC_SCHEDULE_unknown) {
3789 std::string Values;
3790 std::string Sep(", ");
3791 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) {
3792 Values += "'";
3793 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i);
3794 Values += "'";
3795 switch (i) {
3796 case OMPC_SCHEDULE_unknown - 2:
3797 Values += " or ";
3798 break;
3799 case OMPC_SCHEDULE_unknown - 1:
3800 break;
3801 default:
3802 Values += Sep;
3803 break;
3804 }
3805 }
3806 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
3807 << Values << getOpenMPClauseName(OMPC_schedule);
3808 return nullptr;
3809 }
3810 Expr *ValExpr = ChunkSize;
3811 if (ChunkSize) {
3812 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
3813 !ChunkSize->isInstantiationDependent() &&
3814 !ChunkSize->containsUnexpandedParameterPack()) {
3815 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
3816 ExprResult Val =
3817 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
3818 if (Val.isInvalid())
3819 return nullptr;
3820
3821 ValExpr = Val.get();
3822
3823 // OpenMP [2.7.1, Restrictions]
3824 // chunk_size must be a loop invariant integer expression with a positive
3825 // value.
3826 llvm::APSInt Result;
3827 if (ValExpr->isIntegerConstantExpr(Result, Context) &&
3828 Result.isSigned() && !Result.isStrictlyPositive()) {
3829 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
3830 << "schedule" << ChunkSize->getSourceRange();
3831 return nullptr;
3832 }
3833 }
3834 }
3835
3836 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc,
3837 EndLoc, Kind, ValExpr);
3838}
3839
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003840OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
3841 SourceLocation StartLoc,
3842 SourceLocation EndLoc) {
3843 OMPClause *Res = nullptr;
3844 switch (Kind) {
3845 case OMPC_ordered:
3846 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
3847 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00003848 case OMPC_nowait:
3849 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
3850 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003851 case OMPC_untied:
3852 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
3853 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003854 case OMPC_mergeable:
3855 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
3856 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003857 case OMPC_read:
3858 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
3859 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00003860 case OMPC_write:
3861 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
3862 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00003863 case OMPC_update:
3864 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
3865 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00003866 case OMPC_capture:
3867 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
3868 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003869 case OMPC_seq_cst:
3870 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
3871 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003872 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00003873 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003874 case OMPC_num_threads:
3875 case OMPC_safelen:
3876 case OMPC_collapse:
3877 case OMPC_schedule:
3878 case OMPC_private:
3879 case OMPC_firstprivate:
3880 case OMPC_lastprivate:
3881 case OMPC_shared:
3882 case OMPC_reduction:
3883 case OMPC_linear:
3884 case OMPC_aligned:
3885 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003886 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003887 case OMPC_default:
3888 case OMPC_proc_bind:
3889 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00003890 case OMPC_flush:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003891 case OMPC_unknown:
3892 llvm_unreachable("Clause is not allowed.");
3893 }
3894 return Res;
3895}
3896
3897OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
3898 SourceLocation EndLoc) {
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003899 DSAStack->setOrderedRegion();
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003900 return new (Context) OMPOrderedClause(StartLoc, EndLoc);
3901}
3902
Alexey Bataev236070f2014-06-20 11:19:47 +00003903OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
3904 SourceLocation EndLoc) {
3905 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
3906}
3907
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003908OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
3909 SourceLocation EndLoc) {
3910 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
3911}
3912
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003913OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
3914 SourceLocation EndLoc) {
3915 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
3916}
3917
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003918OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
3919 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003920 return new (Context) OMPReadClause(StartLoc, EndLoc);
3921}
3922
Alexey Bataevdea47612014-07-23 07:46:59 +00003923OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
3924 SourceLocation EndLoc) {
3925 return new (Context) OMPWriteClause(StartLoc, EndLoc);
3926}
3927
Alexey Bataev67a4f222014-07-23 10:25:33 +00003928OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
3929 SourceLocation EndLoc) {
3930 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
3931}
3932
Alexey Bataev459dec02014-07-24 06:46:57 +00003933OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
3934 SourceLocation EndLoc) {
3935 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
3936}
3937
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003938OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
3939 SourceLocation EndLoc) {
3940 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
3941}
3942
Alexey Bataevc5e02582014-06-16 07:08:35 +00003943OMPClause *Sema::ActOnOpenMPVarListClause(
3944 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
3945 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
3946 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
3947 const DeclarationNameInfo &ReductionId) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003948 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003949 switch (Kind) {
3950 case OMPC_private:
3951 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
3952 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003953 case OMPC_firstprivate:
3954 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
3955 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00003956 case OMPC_lastprivate:
3957 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
3958 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00003959 case OMPC_shared:
3960 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
3961 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00003962 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00003963 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
3964 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00003965 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00003966 case OMPC_linear:
3967 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
3968 ColonLoc, EndLoc);
3969 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00003970 case OMPC_aligned:
3971 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
3972 ColonLoc, EndLoc);
3973 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003974 case OMPC_copyin:
3975 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
3976 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00003977 case OMPC_copyprivate:
3978 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
3979 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003980 case OMPC_flush:
3981 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
3982 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003983 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00003984 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00003985 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00003986 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00003987 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003988 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003989 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003990 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003991 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00003992 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003993 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003994 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003995 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003996 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00003997 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00003998 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00003999 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00004000 case OMPC_seq_cst:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004001 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004002 llvm_unreachable("Clause is not allowed.");
4003 }
4004 return Res;
4005}
4006
4007OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
4008 SourceLocation StartLoc,
4009 SourceLocation LParenLoc,
4010 SourceLocation EndLoc) {
4011 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00004012 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00004013 for (auto &RefExpr : VarList) {
4014 assert(RefExpr && "NULL expr in OpenMP private clause.");
4015 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004016 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00004017 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00004018 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004019 continue;
4020 }
4021
Alexey Bataeved09d242014-05-28 05:53:51 +00004022 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004023 // OpenMP [2.1, C/C++]
4024 // A list item is a variable name.
4025 // OpenMP [2.9.3.3, Restrictions, p.1]
4026 // A variable that is part of another variable (as an array or
4027 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00004028 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004029 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00004030 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004031 continue;
4032 }
4033 Decl *D = DE->getDecl();
4034 VarDecl *VD = cast<VarDecl>(D);
4035
4036 QualType Type = VD->getType();
4037 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
4038 // It will be analyzed later.
4039 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00004040 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004041 continue;
4042 }
4043
4044 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
4045 // A variable that appears in a private clause must not have an incomplete
4046 // type or a reference type.
4047 if (RequireCompleteType(ELoc, Type,
4048 diag::err_omp_private_incomplete_type)) {
4049 continue;
4050 }
4051 if (Type->isReferenceType()) {
4052 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00004053 << getOpenMPClauseName(OMPC_private) << Type;
4054 bool IsDecl =
4055 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4056 Diag(VD->getLocation(),
4057 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4058 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004059 continue;
4060 }
4061
4062 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
4063 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00004064 // clause requires an accessible, unambiguous default constructor for the
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004065 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00004066 while (Type->isArrayType()) {
4067 Type = cast<ArrayType>(Type.getTypePtr())->getElementType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004068 }
4069
Alexey Bataev758e55e2013-09-06 18:03:48 +00004070 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
4071 // in a Construct]
4072 // Variables with the predetermined data-sharing attributes may not be
4073 // listed in data-sharing attributes clauses, except for the cases
4074 // listed below. For these exceptions only, listing a predetermined
4075 // variable in a data-sharing attribute clause is allowed and overrides
4076 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004077 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004078 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00004079 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
4080 << getOpenMPClauseName(OMPC_private);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004081 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004082 continue;
4083 }
4084
Alexey Bataev03b340a2014-10-21 03:16:40 +00004085 // Generate helper private variable and initialize it with the default
4086 // value. The address of the original variable is replaced by the address of
4087 // the new private variable in CodeGen. This new variable is not added to
4088 // IdResolver, so the code in the OpenMP region uses original variable for
4089 // proper diagnostics.
4090 auto VDPrivate =
4091 VarDecl::Create(Context, CurContext, DE->getLocStart(),
4092 DE->getExprLoc(), VD->getIdentifier(), VD->getType(),
4093 VD->getTypeSourceInfo(), /*S*/ SC_Auto);
4094 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto*/ false);
4095 if (VDPrivate->isInvalidDecl())
4096 continue;
4097 CurContext->addDecl(VDPrivate);
4098 auto VDPrivateRefExpr = DeclRefExpr::Create(
4099 Context, /*QualifierLoc*/ NestedNameSpecifierLoc(),
4100 /*TemplateKWLoc*/ SourceLocation(), VDPrivate,
4101 /*isEnclosingLocal*/ false, /*NameLoc*/ SourceLocation(), DE->getType(),
4102 /*VK*/ VK_LValue);
4103
Alexey Bataev758e55e2013-09-06 18:03:48 +00004104 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004105 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00004106 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004107 }
4108
Alexey Bataeved09d242014-05-28 05:53:51 +00004109 if (Vars.empty())
4110 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004111
Alexey Bataev03b340a2014-10-21 03:16:40 +00004112 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
4113 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004114}
4115
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004116namespace {
4117class DiagsUninitializedSeveretyRAII {
4118private:
4119 DiagnosticsEngine &Diags;
4120 SourceLocation SavedLoc;
4121 bool IsIgnored;
4122
4123public:
4124 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
4125 bool IsIgnored)
4126 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
4127 if (!IsIgnored) {
4128 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
4129 /*Map*/ diag::Severity::Ignored, Loc);
4130 }
4131 }
4132 ~DiagsUninitializedSeveretyRAII() {
4133 if (!IsIgnored)
4134 Diags.popMappings(SavedLoc);
4135 }
4136};
4137}
4138
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004139OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
4140 SourceLocation StartLoc,
4141 SourceLocation LParenLoc,
4142 SourceLocation EndLoc) {
4143 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004144 SmallVector<Expr *, 8> PrivateCopies;
4145 SmallVector<Expr *, 8> Inits;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004146 bool IsImplicitClause =
4147 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
4148 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
4149
Alexey Bataeved09d242014-05-28 05:53:51 +00004150 for (auto &RefExpr : VarList) {
4151 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
4152 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004153 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00004154 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004155 PrivateCopies.push_back(nullptr);
4156 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004157 continue;
4158 }
4159
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004160 SourceLocation ELoc =
4161 IsImplicitClause ? ImplicitClauseLoc : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004162 // OpenMP [2.1, C/C++]
4163 // A list item is a variable name.
4164 // OpenMP [2.9.3.3, Restrictions, p.1]
4165 // A variable that is part of another variable (as an array or
4166 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00004167 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004168 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00004169 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004170 continue;
4171 }
4172 Decl *D = DE->getDecl();
4173 VarDecl *VD = cast<VarDecl>(D);
4174
4175 QualType Type = VD->getType();
4176 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
4177 // It will be analyzed later.
4178 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004179 PrivateCopies.push_back(nullptr);
4180 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004181 continue;
4182 }
4183
4184 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
4185 // A variable that appears in a private clause must not have an incomplete
4186 // type or a reference type.
4187 if (RequireCompleteType(ELoc, Type,
4188 diag::err_omp_firstprivate_incomplete_type)) {
4189 continue;
4190 }
4191 if (Type->isReferenceType()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004192 if (IsImplicitClause) {
4193 Diag(ImplicitClauseLoc,
4194 diag::err_omp_task_predetermined_firstprivate_ref_type_arg)
4195 << Type;
4196 Diag(RefExpr->getExprLoc(), diag::note_used_here);
4197 } else {
4198 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
4199 << getOpenMPClauseName(OMPC_firstprivate) << Type;
4200 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004201 bool IsDecl =
4202 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4203 Diag(VD->getLocation(),
4204 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4205 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004206 continue;
4207 }
4208
4209 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
4210 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00004211 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004212 // class type.
4213 Type = Context.getBaseElementType(Type);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004214
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004215 // If an implicit firstprivate variable found it was checked already.
4216 if (!IsImplicitClause) {
4217 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004218 Type = Type.getNonReferenceType().getCanonicalType();
4219 bool IsConstant = Type.isConstant(Context);
4220 Type = Context.getBaseElementType(Type);
4221 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
4222 // A list item that specifies a given variable may not appear in more
4223 // than one clause on the same directive, except that a variable may be
4224 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004225 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00004226 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004227 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00004228 << getOpenMPClauseName(DVar.CKind)
4229 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004230 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004231 continue;
4232 }
4233
4234 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
4235 // in a Construct]
4236 // Variables with the predetermined data-sharing attributes may not be
4237 // listed in data-sharing attributes clauses, except for the cases
4238 // listed below. For these exceptions only, listing a predetermined
4239 // variable in a data-sharing attribute clause is allowed and overrides
4240 // the variable's predetermined data-sharing attributes.
4241 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
4242 // in a Construct, C/C++, p.2]
4243 // Variables with const-qualified type having no mutable member may be
4244 // listed in a firstprivate clause, even if they are static data members.
4245 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
4246 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
4247 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00004248 << getOpenMPClauseName(DVar.CKind)
4249 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004250 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004251 continue;
4252 }
4253
Alexey Bataevf29276e2014-06-18 04:14:57 +00004254 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004255 // OpenMP [2.9.3.4, Restrictions, p.2]
4256 // A list item that is private within a parallel region must not appear
4257 // in a firstprivate clause on a worksharing construct if any of the
4258 // worksharing regions arising from the worksharing construct ever bind
4259 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00004260 if (isOpenMPWorksharingDirective(CurrDir) &&
4261 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004262 DVar = DSAStack->getImplicitDSA(VD, true);
4263 if (DVar.CKind != OMPC_shared &&
4264 (isOpenMPParallelDirective(DVar.DKind) ||
4265 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00004266 Diag(ELoc, diag::err_omp_required_access)
4267 << getOpenMPClauseName(OMPC_firstprivate)
4268 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004269 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004270 continue;
4271 }
4272 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004273 // OpenMP [2.9.3.4, Restrictions, p.3]
4274 // A list item that appears in a reduction clause of a parallel construct
4275 // must not appear in a firstprivate clause on a worksharing or task
4276 // construct if any of the worksharing or task regions arising from the
4277 // worksharing or task construct ever bind to any of the parallel regions
4278 // arising from the parallel construct.
4279 // OpenMP [2.9.3.4, Restrictions, p.4]
4280 // A list item that appears in a reduction clause in worksharing
4281 // construct must not appear in a firstprivate clause in a task construct
4282 // encountered during execution of any of the worksharing regions arising
4283 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004284 if (CurrDir == OMPD_task) {
4285 DVar =
4286 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
4287 [](OpenMPDirectiveKind K) -> bool {
4288 return isOpenMPParallelDirective(K) ||
4289 isOpenMPWorksharingDirective(K);
4290 },
4291 false);
4292 if (DVar.CKind == OMPC_reduction &&
4293 (isOpenMPParallelDirective(DVar.DKind) ||
4294 isOpenMPWorksharingDirective(DVar.DKind))) {
4295 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
4296 << getOpenMPDirectiveName(DVar.DKind);
4297 ReportOriginalDSA(*this, DSAStack, VD, DVar);
4298 continue;
4299 }
4300 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004301 }
4302
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004303 Type = Type.getUnqualifiedType();
4304 auto VDPrivate = VarDecl::Create(Context, CurContext, DE->getLocStart(),
4305 ELoc, VD->getIdentifier(), VD->getType(),
4306 VD->getTypeSourceInfo(), /*S*/ SC_Auto);
4307 // Generate helper private variable and initialize it with the value of the
4308 // original variable. The address of the original variable is replaced by
4309 // the address of the new private variable in the CodeGen. This new variable
4310 // is not added to IdResolver, so the code in the OpenMP region uses
4311 // original variable for proper diagnostics and variable capturing.
4312 Expr *VDInitRefExpr = nullptr;
4313 // For arrays generate initializer for single element and replace it by the
4314 // original array element in CodeGen.
4315 if (DE->getType()->isArrayType()) {
4316 auto VDInit = VarDecl::Create(Context, CurContext, DE->getLocStart(),
4317 ELoc, VD->getIdentifier(), Type,
4318 VD->getTypeSourceInfo(), /*S*/ SC_Auto);
4319 CurContext->addHiddenDecl(VDInit);
4320 VDInitRefExpr = DeclRefExpr::Create(
4321 Context, /*QualifierLoc*/ NestedNameSpecifierLoc(),
4322 /*TemplateKWLoc*/ SourceLocation(), VDInit,
4323 /*isEnclosingLocal*/ false, ELoc, Type,
4324 /*VK*/ VK_LValue);
4325 VDInit->setIsUsed();
4326 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
4327 InitializedEntity Entity = InitializedEntity::InitializeVariable(VDInit);
4328 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
4329
4330 InitializationSequence InitSeq(*this, Entity, Kind, Init);
4331 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
4332 if (Result.isInvalid())
4333 VDPrivate->setInvalidDecl();
4334 else
4335 VDPrivate->setInit(Result.getAs<Expr>());
4336 } else {
4337 AddInitializerToDecl(VDPrivate, DefaultLvalueConversion(DE).get(),
4338 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4339 }
4340 if (VDPrivate->isInvalidDecl()) {
4341 if (IsImplicitClause) {
4342 Diag(DE->getExprLoc(),
4343 diag::note_omp_task_predetermined_firstprivate_here);
4344 }
4345 continue;
4346 }
4347 CurContext->addDecl(VDPrivate);
4348 auto VDPrivateRefExpr = DeclRefExpr::Create(
4349 Context, /*QualifierLoc*/ NestedNameSpecifierLoc(),
4350 /*TemplateKWLoc*/ SourceLocation(), VDPrivate,
4351 /*isEnclosingLocal*/ false, DE->getLocStart(), DE->getType(),
4352 /*VK*/ VK_LValue);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004353 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
4354 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004355 PrivateCopies.push_back(VDPrivateRefExpr);
4356 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004357 }
4358
Alexey Bataeved09d242014-05-28 05:53:51 +00004359 if (Vars.empty())
4360 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004361
4362 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004363 Vars, PrivateCopies, Inits);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004364}
4365
Alexander Musman1bb328c2014-06-04 13:06:39 +00004366OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
4367 SourceLocation StartLoc,
4368 SourceLocation LParenLoc,
4369 SourceLocation EndLoc) {
4370 SmallVector<Expr *, 8> Vars;
4371 for (auto &RefExpr : VarList) {
4372 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
4373 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
4374 // It will be analyzed later.
4375 Vars.push_back(RefExpr);
4376 continue;
4377 }
4378
4379 SourceLocation ELoc = RefExpr->getExprLoc();
4380 // OpenMP [2.1, C/C++]
4381 // A list item is a variable name.
4382 // OpenMP [2.14.3.5, Restrictions, p.1]
4383 // A variable that is part of another variable (as an array or structure
4384 // element) cannot appear in a lastprivate clause.
4385 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
4386 if (!DE || !isa<VarDecl>(DE->getDecl())) {
4387 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
4388 continue;
4389 }
4390 Decl *D = DE->getDecl();
4391 VarDecl *VD = cast<VarDecl>(D);
4392
4393 QualType Type = VD->getType();
4394 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
4395 // It will be analyzed later.
4396 Vars.push_back(DE);
4397 continue;
4398 }
4399
4400 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
4401 // A variable that appears in a lastprivate clause must not have an
4402 // incomplete type or a reference type.
4403 if (RequireCompleteType(ELoc, Type,
4404 diag::err_omp_lastprivate_incomplete_type)) {
4405 continue;
4406 }
4407 if (Type->isReferenceType()) {
4408 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
4409 << getOpenMPClauseName(OMPC_lastprivate) << Type;
4410 bool IsDecl =
4411 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4412 Diag(VD->getLocation(),
4413 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4414 << VD;
4415 continue;
4416 }
4417
4418 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4419 // in a Construct]
4420 // Variables with the predetermined data-sharing attributes may not be
4421 // listed in data-sharing attributes clauses, except for the cases
4422 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004423 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00004424 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
4425 DVar.CKind != OMPC_firstprivate &&
4426 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
4427 Diag(ELoc, diag::err_omp_wrong_dsa)
4428 << getOpenMPClauseName(DVar.CKind)
4429 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004430 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00004431 continue;
4432 }
4433
Alexey Bataevf29276e2014-06-18 04:14:57 +00004434 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
4435 // OpenMP [2.14.3.5, Restrictions, p.2]
4436 // A list item that is private within a parallel region, or that appears in
4437 // the reduction clause of a parallel construct, must not appear in a
4438 // lastprivate clause on a worksharing construct if any of the corresponding
4439 // worksharing regions ever binds to any of the corresponding parallel
4440 // regions.
Alexey Bataev549210e2014-06-24 04:39:47 +00004441 if (isOpenMPWorksharingDirective(CurrDir) &&
4442 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004443 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004444 if (DVar.CKind != OMPC_shared) {
4445 Diag(ELoc, diag::err_omp_required_access)
4446 << getOpenMPClauseName(OMPC_lastprivate)
4447 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004448 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004449 continue;
4450 }
4451 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00004452 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00004453 // A variable of class type (or array thereof) that appears in a
4454 // lastprivate clause requires an accessible, unambiguous default
4455 // constructor for the class type, unless the list item is also specified
4456 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00004457 // A variable of class type (or array thereof) that appears in a
4458 // lastprivate clause requires an accessible, unambiguous copy assignment
4459 // operator for the class type.
4460 while (Type.getNonReferenceType()->isArrayType())
4461 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
4462 ->getElementType();
4463 CXXRecordDecl *RD = getLangOpts().CPlusPlus
4464 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
4465 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00004466 // FIXME This code must be replaced by actual copying and destructing of the
4467 // lastprivate variable.
Alexander Musman1bb328c2014-06-04 13:06:39 +00004468 if (RD) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00004469 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
4470 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00004471 if (MD) {
4472 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
4473 MD->isDeleted()) {
4474 Diag(ELoc, diag::err_omp_required_method)
4475 << getOpenMPClauseName(OMPC_lastprivate) << 2;
4476 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4477 VarDecl::DeclarationOnly;
4478 Diag(VD->getLocation(),
4479 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4480 << VD;
4481 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4482 continue;
4483 }
4484 MarkFunctionReferenced(ELoc, MD);
4485 DiagnoseUseOfDecl(MD, ELoc);
Alexander Musman1bb328c2014-06-04 13:06:39 +00004486 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00004487
4488 CXXDestructorDecl *DD = RD->getDestructor();
4489 if (DD) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00004490 PartialDiagnostic PD =
4491 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexander Musman1bb328c2014-06-04 13:06:39 +00004492 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
4493 DD->isDeleted()) {
4494 Diag(ELoc, diag::err_omp_required_method)
4495 << getOpenMPClauseName(OMPC_lastprivate) << 4;
4496 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4497 VarDecl::DeclarationOnly;
4498 Diag(VD->getLocation(),
4499 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4500 << VD;
4501 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4502 continue;
4503 }
4504 MarkFunctionReferenced(ELoc, DD);
4505 DiagnoseUseOfDecl(DD, ELoc);
4506 }
4507 }
4508
Alexey Bataevf29276e2014-06-18 04:14:57 +00004509 if (DVar.CKind != OMPC_firstprivate)
4510 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00004511 Vars.push_back(DE);
4512 }
4513
4514 if (Vars.empty())
4515 return nullptr;
4516
4517 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
4518 Vars);
4519}
4520
Alexey Bataev758e55e2013-09-06 18:03:48 +00004521OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
4522 SourceLocation StartLoc,
4523 SourceLocation LParenLoc,
4524 SourceLocation EndLoc) {
4525 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00004526 for (auto &RefExpr : VarList) {
4527 assert(RefExpr && "NULL expr in OpenMP shared clause.");
4528 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00004529 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00004530 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004531 continue;
4532 }
4533
Alexey Bataeved09d242014-05-28 05:53:51 +00004534 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00004535 // OpenMP [2.1, C/C++]
4536 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00004537 // OpenMP [2.14.3.2, Restrictions, p.1]
4538 // A variable that is part of another variable (as an array or structure
4539 // element) cannot appear in a shared unless it is a static data member
4540 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00004541 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004542 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00004543 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00004544 continue;
4545 }
4546 Decl *D = DE->getDecl();
4547 VarDecl *VD = cast<VarDecl>(D);
4548
4549 QualType Type = VD->getType();
4550 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
4551 // It will be analyzed later.
4552 Vars.push_back(DE);
4553 continue;
4554 }
4555
4556 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
4557 // in a Construct]
4558 // Variables with the predetermined data-sharing attributes may not be
4559 // listed in data-sharing attributes clauses, except for the cases
4560 // listed below. For these exceptions only, listing a predetermined
4561 // variable in a data-sharing attribute clause is allowed and overrides
4562 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004563 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00004564 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
4565 DVar.RefExpr) {
4566 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
4567 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004568 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004569 continue;
4570 }
4571
4572 DSAStack->addDSA(VD, DE, OMPC_shared);
4573 Vars.push_back(DE);
4574 }
4575
Alexey Bataeved09d242014-05-28 05:53:51 +00004576 if (Vars.empty())
4577 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00004578
4579 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
4580}
4581
Alexey Bataevc5e02582014-06-16 07:08:35 +00004582namespace {
4583class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
4584 DSAStackTy *Stack;
4585
4586public:
4587 bool VisitDeclRefExpr(DeclRefExpr *E) {
4588 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004589 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00004590 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
4591 return false;
4592 if (DVar.CKind != OMPC_unknown)
4593 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00004594 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004595 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004596 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00004597 return true;
4598 return false;
4599 }
4600 return false;
4601 }
4602 bool VisitStmt(Stmt *S) {
4603 for (auto Child : S->children()) {
4604 if (Child && Visit(Child))
4605 return true;
4606 }
4607 return false;
4608 }
Alexey Bataev23b69422014-06-18 07:08:49 +00004609 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00004610};
Alexey Bataev23b69422014-06-18 07:08:49 +00004611} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00004612
4613OMPClause *Sema::ActOnOpenMPReductionClause(
4614 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
4615 SourceLocation ColonLoc, SourceLocation EndLoc,
4616 CXXScopeSpec &ReductionIdScopeSpec,
4617 const DeclarationNameInfo &ReductionId) {
4618 // TODO: Allow scope specification search when 'declare reduction' is
4619 // supported.
4620 assert(ReductionIdScopeSpec.isEmpty() &&
4621 "No support for scoped reduction identifiers yet.");
4622
4623 auto DN = ReductionId.getName();
4624 auto OOK = DN.getCXXOverloadedOperator();
4625 BinaryOperatorKind BOK = BO_Comma;
4626
4627 // OpenMP [2.14.3.6, reduction clause]
4628 // C
4629 // reduction-identifier is either an identifier or one of the following
4630 // operators: +, -, *, &, |, ^, && and ||
4631 // C++
4632 // reduction-identifier is either an id-expression or one of the following
4633 // operators: +, -, *, &, |, ^, && and ||
4634 // FIXME: Only 'min' and 'max' identifiers are supported for now.
4635 switch (OOK) {
4636 case OO_Plus:
4637 case OO_Minus:
4638 BOK = BO_AddAssign;
4639 break;
4640 case OO_Star:
4641 BOK = BO_MulAssign;
4642 break;
4643 case OO_Amp:
4644 BOK = BO_AndAssign;
4645 break;
4646 case OO_Pipe:
4647 BOK = BO_OrAssign;
4648 break;
4649 case OO_Caret:
4650 BOK = BO_XorAssign;
4651 break;
4652 case OO_AmpAmp:
4653 BOK = BO_LAnd;
4654 break;
4655 case OO_PipePipe:
4656 BOK = BO_LOr;
4657 break;
4658 default:
4659 if (auto II = DN.getAsIdentifierInfo()) {
4660 if (II->isStr("max"))
4661 BOK = BO_GT;
4662 else if (II->isStr("min"))
4663 BOK = BO_LT;
4664 }
4665 break;
4666 }
4667 SourceRange ReductionIdRange;
4668 if (ReductionIdScopeSpec.isValid()) {
4669 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
4670 }
4671 ReductionIdRange.setEnd(ReductionId.getEndLoc());
4672 if (BOK == BO_Comma) {
4673 // Not allowed reduction identifier is found.
4674 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
4675 << ReductionIdRange;
4676 return nullptr;
4677 }
4678
4679 SmallVector<Expr *, 8> Vars;
4680 for (auto RefExpr : VarList) {
4681 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
4682 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
4683 // It will be analyzed later.
4684 Vars.push_back(RefExpr);
4685 continue;
4686 }
4687
4688 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
4689 RefExpr->isInstantiationDependent() ||
4690 RefExpr->containsUnexpandedParameterPack()) {
4691 // It will be analyzed later.
4692 Vars.push_back(RefExpr);
4693 continue;
4694 }
4695
4696 auto ELoc = RefExpr->getExprLoc();
4697 auto ERange = RefExpr->getSourceRange();
4698 // OpenMP [2.1, C/C++]
4699 // A list item is a variable or array section, subject to the restrictions
4700 // specified in Section 2.4 on page 42 and in each of the sections
4701 // describing clauses and directives for which a list appears.
4702 // OpenMP [2.14.3.3, Restrictions, p.1]
4703 // A variable that is part of another variable (as an array or
4704 // structure element) cannot appear in a private clause.
4705 auto DE = dyn_cast<DeclRefExpr>(RefExpr);
4706 if (!DE || !isa<VarDecl>(DE->getDecl())) {
4707 Diag(ELoc, diag::err_omp_expected_var_name) << ERange;
4708 continue;
4709 }
4710 auto D = DE->getDecl();
4711 auto VD = cast<VarDecl>(D);
4712 auto Type = VD->getType();
4713 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
4714 // A variable that appears in a private clause must not have an incomplete
4715 // type or a reference type.
4716 if (RequireCompleteType(ELoc, Type,
4717 diag::err_omp_reduction_incomplete_type))
4718 continue;
4719 // OpenMP [2.14.3.6, reduction clause, Restrictions]
4720 // Arrays may not appear in a reduction clause.
4721 if (Type.getNonReferenceType()->isArrayType()) {
4722 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
4723 bool IsDecl =
4724 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4725 Diag(VD->getLocation(),
4726 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4727 << VD;
4728 continue;
4729 }
4730 // OpenMP [2.14.3.6, reduction clause, Restrictions]
4731 // A list item that appears in a reduction clause must not be
4732 // const-qualified.
4733 if (Type.getNonReferenceType().isConstant(Context)) {
4734 Diag(ELoc, diag::err_omp_const_variable)
4735 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
4736 bool IsDecl =
4737 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4738 Diag(VD->getLocation(),
4739 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4740 << VD;
4741 continue;
4742 }
4743 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
4744 // If a list-item is a reference type then it must bind to the same object
4745 // for all threads of the team.
4746 VarDecl *VDDef = VD->getDefinition();
4747 if (Type->isReferenceType() && VDDef) {
4748 DSARefChecker Check(DSAStack);
4749 if (Check.Visit(VDDef->getInit())) {
4750 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
4751 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
4752 continue;
4753 }
4754 }
4755 // OpenMP [2.14.3.6, reduction clause, Restrictions]
4756 // The type of a list item that appears in a reduction clause must be valid
4757 // for the reduction-identifier. For a max or min reduction in C, the type
4758 // of the list item must be an allowed arithmetic data type: char, int,
4759 // float, double, or _Bool, possibly modified with long, short, signed, or
4760 // unsigned. For a max or min reduction in C++, the type of the list item
4761 // must be an allowed arithmetic data type: char, wchar_t, int, float,
4762 // double, or bool, possibly modified with long, short, signed, or unsigned.
4763 if ((BOK == BO_GT || BOK == BO_LT) &&
4764 !(Type->isScalarType() ||
4765 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
4766 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
4767 << getLangOpts().CPlusPlus;
4768 bool IsDecl =
4769 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4770 Diag(VD->getLocation(),
4771 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4772 << VD;
4773 continue;
4774 }
4775 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
4776 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
4777 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
4778 bool IsDecl =
4779 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4780 Diag(VD->getLocation(),
4781 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4782 << VD;
4783 continue;
4784 }
4785 bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
4786 getDiagnostics().setSuppressAllDiagnostics(true);
4787 ExprResult ReductionOp =
4788 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
4789 RefExpr, RefExpr);
4790 getDiagnostics().setSuppressAllDiagnostics(Suppress);
4791 if (ReductionOp.isInvalid()) {
4792 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
Alexey Bataev23b69422014-06-18 07:08:49 +00004793 << ReductionIdRange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00004794 bool IsDecl =
4795 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4796 Diag(VD->getLocation(),
4797 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4798 << VD;
4799 continue;
4800 }
4801
4802 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4803 // in a Construct]
4804 // Variables with the predetermined data-sharing attributes may not be
4805 // listed in data-sharing attributes clauses, except for the cases
4806 // listed below. For these exceptions only, listing a predetermined
4807 // variable in a data-sharing attribute clause is allowed and overrides
4808 // the variable's predetermined data-sharing attributes.
4809 // OpenMP [2.14.3.6, Restrictions, p.3]
4810 // Any number of reduction clauses can be specified on the directive,
4811 // but a list item can appear only once in the reduction clauses for that
4812 // directive.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004813 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00004814 if (DVar.CKind == OMPC_reduction) {
4815 Diag(ELoc, diag::err_omp_once_referenced)
4816 << getOpenMPClauseName(OMPC_reduction);
4817 if (DVar.RefExpr) {
4818 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
4819 }
4820 } else if (DVar.CKind != OMPC_unknown) {
4821 Diag(ELoc, diag::err_omp_wrong_dsa)
4822 << getOpenMPClauseName(DVar.CKind)
4823 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004824 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00004825 continue;
4826 }
4827
4828 // OpenMP [2.14.3.6, Restrictions, p.1]
4829 // A list item that appears in a reduction clause of a worksharing
4830 // construct must be shared in the parallel regions to which any of the
4831 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf29276e2014-06-18 04:14:57 +00004832 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataev549210e2014-06-24 04:39:47 +00004833 if (isOpenMPWorksharingDirective(CurrDir) &&
4834 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004835 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004836 if (DVar.CKind != OMPC_shared) {
4837 Diag(ELoc, diag::err_omp_required_access)
4838 << getOpenMPClauseName(OMPC_reduction)
4839 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004840 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004841 continue;
4842 }
4843 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00004844
4845 CXXRecordDecl *RD = getLangOpts().CPlusPlus
4846 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
4847 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00004848 // FIXME This code must be replaced by actual constructing/destructing of
4849 // the reduction variable.
Alexey Bataevc5e02582014-06-16 07:08:35 +00004850 if (RD) {
4851 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
4852 PartialDiagnostic PD =
4853 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataev23b69422014-06-18 07:08:49 +00004854 if (!CD ||
4855 CheckConstructorAccess(ELoc, CD,
4856 InitializedEntity::InitializeTemporary(Type),
4857 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevc5e02582014-06-16 07:08:35 +00004858 CD->isDeleted()) {
4859 Diag(ELoc, diag::err_omp_required_method)
4860 << getOpenMPClauseName(OMPC_reduction) << 0;
4861 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4862 VarDecl::DeclarationOnly;
4863 Diag(VD->getLocation(),
4864 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4865 << VD;
4866 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4867 continue;
4868 }
4869 MarkFunctionReferenced(ELoc, CD);
4870 DiagnoseUseOfDecl(CD, ELoc);
4871
4872 CXXDestructorDecl *DD = RD->getDestructor();
4873 if (DD) {
4874 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
4875 DD->isDeleted()) {
4876 Diag(ELoc, diag::err_omp_required_method)
4877 << getOpenMPClauseName(OMPC_reduction) << 4;
4878 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4879 VarDecl::DeclarationOnly;
4880 Diag(VD->getLocation(),
4881 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4882 << VD;
4883 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4884 continue;
4885 }
4886 MarkFunctionReferenced(ELoc, DD);
4887 DiagnoseUseOfDecl(DD, ELoc);
4888 }
4889 }
4890
4891 DSAStack->addDSA(VD, DE, OMPC_reduction);
4892 Vars.push_back(DE);
4893 }
4894
4895 if (Vars.empty())
4896 return nullptr;
4897
4898 return OMPReductionClause::Create(
4899 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
4900 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId);
4901}
4902
Alexander Musman8dba6642014-04-22 13:09:42 +00004903OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step,
4904 SourceLocation StartLoc,
4905 SourceLocation LParenLoc,
4906 SourceLocation ColonLoc,
4907 SourceLocation EndLoc) {
4908 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00004909 for (auto &RefExpr : VarList) {
4910 assert(RefExpr && "NULL expr in OpenMP linear clause.");
4911 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00004912 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00004913 Vars.push_back(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00004914 continue;
4915 }
4916
4917 // OpenMP [2.14.3.7, linear clause]
4918 // A list item that appears in a linear clause is subject to the private
4919 // clause semantics described in Section 2.14.3.3 on page 159 except as
4920 // noted. In addition, the value of the new list item on each iteration
4921 // of the associated loop(s) corresponds to the value of the original
4922 // list item before entering the construct plus the logical number of
4923 // the iteration times linear-step.
4924
Alexey Bataeved09d242014-05-28 05:53:51 +00004925 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00004926 // OpenMP [2.1, C/C++]
4927 // A list item is a variable name.
4928 // OpenMP [2.14.3.3, Restrictions, p.1]
4929 // A variable that is part of another variable (as an array or
4930 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00004931 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00004932 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00004933 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00004934 continue;
4935 }
4936
4937 VarDecl *VD = cast<VarDecl>(DE->getDecl());
4938
4939 // OpenMP [2.14.3.7, linear clause]
4940 // A list-item cannot appear in more than one linear clause.
4941 // A list-item that appears in a linear clause cannot appear in any
4942 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004943 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00004944 if (DVar.RefExpr) {
4945 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
4946 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004947 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00004948 continue;
4949 }
4950
4951 QualType QType = VD->getType();
4952 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
4953 // It will be analyzed later.
4954 Vars.push_back(DE);
4955 continue;
4956 }
4957
4958 // A variable must not have an incomplete type or a reference type.
4959 if (RequireCompleteType(ELoc, QType,
4960 diag::err_omp_linear_incomplete_type)) {
4961 continue;
4962 }
4963 if (QType->isReferenceType()) {
4964 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
4965 << getOpenMPClauseName(OMPC_linear) << QType;
4966 bool IsDecl =
4967 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4968 Diag(VD->getLocation(),
4969 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4970 << VD;
4971 continue;
4972 }
4973
4974 // A list item must not be const-qualified.
4975 if (QType.isConstant(Context)) {
4976 Diag(ELoc, diag::err_omp_const_variable)
4977 << getOpenMPClauseName(OMPC_linear);
4978 bool IsDecl =
4979 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4980 Diag(VD->getLocation(),
4981 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4982 << VD;
4983 continue;
4984 }
4985
4986 // A list item must be of integral or pointer type.
4987 QType = QType.getUnqualifiedType().getCanonicalType();
4988 const Type *Ty = QType.getTypePtrOrNull();
4989 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
4990 !Ty->isPointerType())) {
4991 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
4992 bool IsDecl =
4993 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4994 Diag(VD->getLocation(),
4995 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4996 << VD;
4997 continue;
4998 }
4999
5000 DSAStack->addDSA(VD, DE, OMPC_linear);
5001 Vars.push_back(DE);
5002 }
5003
5004 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005005 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00005006
5007 Expr *StepExpr = Step;
5008 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
5009 !Step->isInstantiationDependent() &&
5010 !Step->containsUnexpandedParameterPack()) {
5011 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005012 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00005013 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005014 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00005015 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00005016
5017 // Warn about zero linear step (it would be probably better specified as
5018 // making corresponding variables 'const').
5019 llvm::APSInt Result;
5020 if (StepExpr->isIntegerConstantExpr(Result, Context) &&
5021 !Result.isNegative() && !Result.isStrictlyPositive())
5022 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
5023 << (Vars.size() > 1);
5024 }
5025
5026 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc,
5027 Vars, StepExpr);
5028}
5029
Alexander Musmanf0d76e72014-05-29 14:36:25 +00005030OMPClause *Sema::ActOnOpenMPAlignedClause(
5031 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
5032 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
5033
5034 SmallVector<Expr *, 8> Vars;
5035 for (auto &RefExpr : VarList) {
5036 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
5037 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
5038 // It will be analyzed later.
5039 Vars.push_back(RefExpr);
5040 continue;
5041 }
5042
5043 SourceLocation ELoc = RefExpr->getExprLoc();
5044 // OpenMP [2.1, C/C++]
5045 // A list item is a variable name.
5046 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
5047 if (!DE || !isa<VarDecl>(DE->getDecl())) {
5048 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
5049 continue;
5050 }
5051
5052 VarDecl *VD = cast<VarDecl>(DE->getDecl());
5053
5054 // OpenMP [2.8.1, simd construct, Restrictions]
5055 // The type of list items appearing in the aligned clause must be
5056 // array, pointer, reference to array, or reference to pointer.
5057 QualType QType = DE->getType()
5058 .getNonReferenceType()
5059 .getUnqualifiedType()
5060 .getCanonicalType();
5061 const Type *Ty = QType.getTypePtrOrNull();
5062 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
5063 !Ty->isPointerType())) {
5064 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
5065 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
5066 bool IsDecl =
5067 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5068 Diag(VD->getLocation(),
5069 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5070 << VD;
5071 continue;
5072 }
5073
5074 // OpenMP [2.8.1, simd construct, Restrictions]
5075 // A list-item cannot appear in more than one aligned clause.
5076 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
5077 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
5078 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
5079 << getOpenMPClauseName(OMPC_aligned);
5080 continue;
5081 }
5082
5083 Vars.push_back(DE);
5084 }
5085
5086 // OpenMP [2.8.1, simd construct, Description]
5087 // The parameter of the aligned clause, alignment, must be a constant
5088 // positive integer expression.
5089 // If no optional parameter is specified, implementation-defined default
5090 // alignments for SIMD instructions on the target platforms are assumed.
5091 if (Alignment != nullptr) {
5092 ExprResult AlignResult =
5093 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
5094 if (AlignResult.isInvalid())
5095 return nullptr;
5096 Alignment = AlignResult.get();
5097 }
5098 if (Vars.empty())
5099 return nullptr;
5100
5101 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
5102 EndLoc, Vars, Alignment);
5103}
5104
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005105OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
5106 SourceLocation StartLoc,
5107 SourceLocation LParenLoc,
5108 SourceLocation EndLoc) {
5109 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00005110 for (auto &RefExpr : VarList) {
5111 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
5112 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005113 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00005114 Vars.push_back(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005115 continue;
5116 }
5117
Alexey Bataeved09d242014-05-28 05:53:51 +00005118 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005119 // OpenMP [2.1, C/C++]
5120 // A list item is a variable name.
5121 // OpenMP [2.14.4.1, Restrictions, p.1]
5122 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00005123 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005124 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005125 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005126 continue;
5127 }
5128
5129 Decl *D = DE->getDecl();
5130 VarDecl *VD = cast<VarDecl>(D);
5131
5132 QualType Type = VD->getType();
5133 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5134 // It will be analyzed later.
5135 Vars.push_back(DE);
5136 continue;
5137 }
5138
5139 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
5140 // A list item that appears in a copyin clause must be threadprivate.
5141 if (!DSAStack->isThreadPrivate(VD)) {
5142 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00005143 << getOpenMPClauseName(OMPC_copyin)
5144 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005145 continue;
5146 }
5147
5148 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
5149 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00005150 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005151 // operator for the class type.
5152 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005153 CXXRecordDecl *RD =
5154 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00005155 // FIXME This code must be replaced by actual assignment of the
5156 // threadprivate variable.
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005157 if (RD) {
5158 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
5159 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00005160 if (MD) {
5161 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
5162 MD->isDeleted()) {
5163 Diag(ELoc, diag::err_omp_required_method)
5164 << getOpenMPClauseName(OMPC_copyin) << 2;
5165 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
5166 VarDecl::DeclarationOnly;
5167 Diag(VD->getLocation(),
5168 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5169 << VD;
5170 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
5171 continue;
5172 }
5173 MarkFunctionReferenced(ELoc, MD);
5174 DiagnoseUseOfDecl(MD, ELoc);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005175 }
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005176 }
5177
5178 DSAStack->addDSA(VD, DE, OMPC_copyin);
5179 Vars.push_back(DE);
5180 }
5181
Alexey Bataeved09d242014-05-28 05:53:51 +00005182 if (Vars.empty())
5183 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005184
5185 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
5186}
5187
Alexey Bataevbae9a792014-06-27 10:37:06 +00005188OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
5189 SourceLocation StartLoc,
5190 SourceLocation LParenLoc,
5191 SourceLocation EndLoc) {
5192 SmallVector<Expr *, 8> Vars;
5193 for (auto &RefExpr : VarList) {
5194 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
5195 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
5196 // It will be analyzed later.
5197 Vars.push_back(RefExpr);
5198 continue;
5199 }
5200
5201 SourceLocation ELoc = RefExpr->getExprLoc();
5202 // OpenMP [2.1, C/C++]
5203 // A list item is a variable name.
5204 // OpenMP [2.14.4.1, Restrictions, p.1]
5205 // A list item that appears in a copyin clause must be threadprivate.
5206 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
5207 if (!DE || !isa<VarDecl>(DE->getDecl())) {
5208 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
5209 continue;
5210 }
5211
5212 Decl *D = DE->getDecl();
5213 VarDecl *VD = cast<VarDecl>(D);
5214
5215 QualType Type = VD->getType();
5216 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5217 // It will be analyzed later.
5218 Vars.push_back(DE);
5219 continue;
5220 }
5221
5222 // OpenMP [2.14.4.2, Restrictions, p.2]
5223 // A list item that appears in a copyprivate clause may not appear in a
5224 // private or firstprivate clause on the single construct.
5225 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005226 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00005227 if (DVar.CKind != OMPC_copyprivate && DVar.CKind != OMPC_unknown &&
5228 !(DVar.CKind == OMPC_private && !DVar.RefExpr)) {
5229 Diag(ELoc, diag::err_omp_wrong_dsa)
5230 << getOpenMPClauseName(DVar.CKind)
5231 << getOpenMPClauseName(OMPC_copyprivate);
5232 ReportOriginalDSA(*this, DSAStack, VD, DVar);
5233 continue;
5234 }
5235
5236 // OpenMP [2.11.4.2, Restrictions, p.1]
5237 // All list items that appear in a copyprivate clause must be either
5238 // threadprivate or private in the enclosing context.
5239 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005240 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00005241 if (DVar.CKind == OMPC_shared) {
5242 Diag(ELoc, diag::err_omp_required_access)
5243 << getOpenMPClauseName(OMPC_copyprivate)
5244 << "threadprivate or private in the enclosing context";
5245 ReportOriginalDSA(*this, DSAStack, VD, DVar);
5246 continue;
5247 }
5248 }
5249 }
5250
5251 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
5252 // A variable of class type (or array thereof) that appears in a
5253 // copyin clause requires an accessible, unambiguous copy assignment
5254 // operator for the class type.
5255 Type = Context.getBaseElementType(Type);
5256 CXXRecordDecl *RD =
5257 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
5258 // FIXME This code must be replaced by actual assignment of the
5259 // threadprivate variable.
5260 if (RD) {
5261 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
5262 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
5263 if (MD) {
5264 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
5265 MD->isDeleted()) {
5266 Diag(ELoc, diag::err_omp_required_method)
5267 << getOpenMPClauseName(OMPC_copyprivate) << 2;
5268 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
5269 VarDecl::DeclarationOnly;
5270 Diag(VD->getLocation(),
5271 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5272 << VD;
5273 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
5274 continue;
5275 }
5276 MarkFunctionReferenced(ELoc, MD);
5277 DiagnoseUseOfDecl(MD, ELoc);
5278 }
5279 }
5280
5281 // No need to mark vars as copyprivate, they are already threadprivate or
5282 // implicitly private.
5283 Vars.push_back(DE);
5284 }
5285
5286 if (Vars.empty())
5287 return nullptr;
5288
5289 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
5290}
5291
Alexey Bataev6125da92014-07-21 11:26:11 +00005292OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
5293 SourceLocation StartLoc,
5294 SourceLocation LParenLoc,
5295 SourceLocation EndLoc) {
5296 if (VarList.empty())
5297 return nullptr;
5298
5299 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
5300}
Alexey Bataevdea47612014-07-23 07:46:59 +00005301