blob: 5f03df4f5329bb4cf73d3e779e7d1e709accb61e [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 Bataevcaf09b02014-07-25 06:27:47 +00002522 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002523 }
2524
Alexey Bataev7ff55242014-06-19 09:13:45 +00002525 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00002526
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002527 // Check test-expr.
2528 HasErrors |= ISC.CheckCond(For->getCond());
2529
2530 // Check incr-expr.
2531 HasErrors |= ISC.CheckInc(For->getInc());
2532
Alexander Musmana5f070a2014-10-01 06:03:56 +00002533 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002534 return HasErrors;
2535
Alexander Musmana5f070a2014-10-01 06:03:56 +00002536 // Build the loop's iteration space representation.
Alexander Musman174b3ca2014-10-06 11:16:29 +00002537 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
2538 DSA.getCurScope(), /* LimitedType */ isOpenMPWorksharingDirective(DKind));
Alexander Musmana5f070a2014-10-01 06:03:56 +00002539 ResultIterSpace.CounterVar = ISC.BuildCounterVar();
2540 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
2541 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
2542 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
2543 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
2544 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
2545 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
2546
2547 HasErrors |= (ResultIterSpace.NumIterations == nullptr ||
2548 ResultIterSpace.CounterVar == nullptr ||
2549 ResultIterSpace.CounterInit == nullptr ||
2550 ResultIterSpace.CounterStep == nullptr);
2551
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002552 return HasErrors;
2553}
2554
Alexander Musmana5f070a2014-10-01 06:03:56 +00002555/// \brief Build a variable declaration for OpenMP loop iteration variable.
2556static VarDecl *BuildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
2557 StringRef Name) {
2558 DeclContext *DC = SemaRef.CurContext;
2559 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
2560 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
2561 VarDecl *Decl =
2562 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
2563 Decl->setImplicit();
2564 return Decl;
2565}
2566
2567/// \brief Build 'VarRef = Start + Iter * Step'.
2568static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S,
2569 SourceLocation Loc, ExprResult VarRef,
2570 ExprResult Start, ExprResult Iter,
2571 ExprResult Step, bool Subtract) {
2572 // Add parentheses (for debugging purposes only).
2573 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
2574 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
2575 !Step.isUsable())
2576 return ExprError();
2577
2578 ExprResult Update = SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(),
2579 Step.get()->IgnoreImplicit());
2580 if (!Update.isUsable())
2581 return ExprError();
2582
2583 // Build 'VarRef = Start + Iter * Step'.
2584 Update = SemaRef.BuildBinOp(S, Loc, (Subtract ? BO_Sub : BO_Add),
2585 Start.get()->IgnoreImplicit(), Update.get());
2586 if (!Update.isUsable())
2587 return ExprError();
2588
2589 Update = SemaRef.PerformImplicitConversion(
2590 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
2591 if (!Update.isUsable())
2592 return ExprError();
2593
2594 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
2595 return Update;
2596}
2597
2598/// \brief Convert integer expression \a E to make it have at least \a Bits
2599/// bits.
2600static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
2601 Sema &SemaRef) {
2602 if (E == nullptr)
2603 return ExprError();
2604 auto &C = SemaRef.Context;
2605 QualType OldType = E->getType();
2606 unsigned HasBits = C.getTypeSize(OldType);
2607 if (HasBits >= Bits)
2608 return ExprResult(E);
2609 // OK to convert to signed, because new type has more bits than old.
2610 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
2611 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
2612 true);
2613}
2614
2615/// \brief Check if the given expression \a E is a constant integer that fits
2616/// into \a Bits bits.
2617static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
2618 if (E == nullptr)
2619 return false;
2620 llvm::APSInt Result;
2621 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
2622 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
2623 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002624}
2625
2626/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00002627/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
2628/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002629static unsigned
2630CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *NestedLoopCountExpr,
2631 Stmt *AStmt, Sema &SemaRef, DSAStackTy &DSA,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002632 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
2633 BuiltLoopExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002634 unsigned NestedLoopCount = 1;
2635 if (NestedLoopCountExpr) {
2636 // Found 'collapse' clause - calculate collapse number.
2637 llvm::APSInt Result;
2638 if (NestedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
2639 NestedLoopCount = Result.getLimitedValue();
2640 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002641 // This is helper routine for loop directives (e.g., 'for', 'simd',
2642 // 'for simd', etc.).
Alexander Musmana5f070a2014-10-01 06:03:56 +00002643 SmallVector<LoopIterationSpace, 4> IterSpaces;
2644 IterSpaces.resize(NestedLoopCount);
2645 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002646 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002647 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev4acb8592014-07-07 13:01:15 +00002648 NestedLoopCount, NestedLoopCountExpr,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002649 VarsWithImplicitDSA, IterSpaces[Cnt]))
Alexey Bataevabfc0692014-06-25 06:52:00 +00002650 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002651 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00002652 // OpenMP [2.8.1, simd construct, Restrictions]
2653 // All loops associated with the construct must be perfectly nested; that
2654 // is, there must be no intervening code nor any OpenMP directive between
2655 // any two loops.
2656 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002657 }
2658
Alexander Musmana5f070a2014-10-01 06:03:56 +00002659 Built.clear(/* size */ NestedLoopCount);
2660
2661 if (SemaRef.CurContext->isDependentContext())
2662 return NestedLoopCount;
2663
2664 // An example of what is generated for the following code:
2665 //
2666 // #pragma omp simd collapse(2)
2667 // for (i = 0; i < NI; ++i)
2668 // for (j = J0; j < NJ; j+=2) {
2669 // <loop body>
2670 // }
2671 //
2672 // We generate the code below.
2673 // Note: the loop body may be outlined in CodeGen.
2674 // Note: some counters may be C++ classes, operator- is used to find number of
2675 // iterations and operator+= to calculate counter value.
2676 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
2677 // or i64 is currently supported).
2678 //
2679 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
2680 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
2681 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
2682 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
2683 // // similar updates for vars in clauses (e.g. 'linear')
2684 // <loop body (using local i and j)>
2685 // }
2686 // i = NI; // assign final values of counters
2687 // j = NJ;
2688 //
2689
2690 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
2691 // the iteration counts of the collapsed for loops.
2692 auto N0 = IterSpaces[0].NumIterations;
2693 ExprResult LastIteration32 = WidenIterationCount(32 /* Bits */, N0, SemaRef);
2694 ExprResult LastIteration64 = WidenIterationCount(64 /* Bits */, N0, SemaRef);
2695
2696 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
2697 return NestedLoopCount;
2698
2699 auto &C = SemaRef.Context;
2700 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
2701
2702 Scope *CurScope = DSA.getCurScope();
2703 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
2704 auto N = IterSpaces[Cnt].NumIterations;
2705 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
2706 if (LastIteration32.isUsable())
2707 LastIteration32 = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_Mul,
2708 LastIteration32.get(), N);
2709 if (LastIteration64.isUsable())
2710 LastIteration64 = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_Mul,
2711 LastIteration64.get(), N);
2712 }
2713
2714 // Choose either the 32-bit or 64-bit version.
2715 ExprResult LastIteration = LastIteration64;
2716 if (LastIteration32.isUsable() &&
2717 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
2718 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
2719 FitsInto(
2720 32 /* Bits */,
2721 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
2722 LastIteration64.get(), SemaRef)))
2723 LastIteration = LastIteration32;
2724
2725 if (!LastIteration.isUsable())
2726 return 0;
2727
2728 // Save the number of iterations.
2729 ExprResult NumIterations = LastIteration;
2730 {
2731 LastIteration = SemaRef.BuildBinOp(
2732 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
2733 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
2734 if (!LastIteration.isUsable())
2735 return 0;
2736 }
2737
2738 // Calculate the last iteration number beforehand instead of doing this on
2739 // each iteration. Do not do this if the number of iterations may be kfold-ed.
2740 llvm::APSInt Result;
2741 bool IsConstant =
2742 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
2743 ExprResult CalcLastIteration;
2744 if (!IsConstant) {
2745 SourceLocation SaveLoc;
2746 VarDecl *SaveVar =
2747 BuildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(),
2748 ".omp.last.iteration");
2749 ExprResult SaveRef = SemaRef.BuildDeclRefExpr(
2750 SaveVar, LastIteration.get()->getType(), VK_LValue, SaveLoc);
2751 CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign,
2752 SaveRef.get(), LastIteration.get());
2753 LastIteration = SaveRef;
2754
2755 // Prepare SaveRef + 1.
2756 NumIterations = SemaRef.BuildBinOp(
2757 CurScope, SaveLoc, BO_Add, SaveRef.get(),
2758 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
2759 if (!NumIterations.isUsable())
2760 return 0;
2761 }
2762
2763 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
2764
2765 // Precondition tests if there is at least one iteration (LastIteration > 0).
2766 ExprResult PreCond = SemaRef.BuildBinOp(
2767 CurScope, InitLoc, BO_GT, LastIteration.get(),
2768 SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get());
2769
2770 // Build the iteration variable and its initialization to zero before loop.
2771 ExprResult IV;
2772 ExprResult Init;
2773 {
2774 VarDecl *IVDecl = BuildVarDecl(SemaRef, InitLoc,
2775 LastIteration.get()->getType(), ".omp.iv");
2776 IV = SemaRef.BuildDeclRefExpr(IVDecl, LastIteration.get()->getType(),
2777 VK_LValue, InitLoc);
2778 Init = SemaRef.BuildBinOp(
2779 CurScope, InitLoc, BO_Assign, IV.get(),
2780 SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get());
2781 }
2782
2783 // Loop condition (IV < NumIterations)
2784 SourceLocation CondLoc;
2785 ExprResult Cond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
2786 NumIterations.get());
2787 // Loop condition with 1 iteration separated (IV < LastIteration)
2788 ExprResult SeparatedCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT,
2789 IV.get(), LastIteration.get());
2790
2791 // Loop increment (IV = IV + 1)
2792 SourceLocation IncLoc;
2793 ExprResult Inc =
2794 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
2795 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
2796 if (!Inc.isUsable())
2797 return 0;
2798 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
2799
2800 // Build updates and final values of the loop counters.
2801 bool HasErrors = false;
2802 Built.Counters.resize(NestedLoopCount);
2803 Built.Updates.resize(NestedLoopCount);
2804 Built.Finals.resize(NestedLoopCount);
2805 {
2806 ExprResult Div;
2807 // Go from inner nested loop to outer.
2808 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
2809 LoopIterationSpace &IS = IterSpaces[Cnt];
2810 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
2811 // Build: Iter = (IV / Div) % IS.NumIters
2812 // where Div is product of previous iterations' IS.NumIters.
2813 ExprResult Iter;
2814 if (Div.isUsable()) {
2815 Iter =
2816 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
2817 } else {
2818 Iter = IV;
2819 assert((Cnt == (int)NestedLoopCount - 1) &&
2820 "unusable div expected on first iteration only");
2821 }
2822
2823 if (Cnt != 0 && Iter.isUsable())
2824 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
2825 IS.NumIterations);
2826 if (!Iter.isUsable()) {
2827 HasErrors = true;
2828 break;
2829 }
2830
2831 // Build update: IS.CounterVar = IS.Start + Iter * IS.Step
2832 ExprResult Update =
2833 BuildCounterUpdate(SemaRef, CurScope, UpdLoc, IS.CounterVar,
2834 IS.CounterInit, Iter, IS.CounterStep, IS.Subtract);
2835 if (!Update.isUsable()) {
2836 HasErrors = true;
2837 break;
2838 }
2839
2840 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
2841 ExprResult Final = BuildCounterUpdate(
2842 SemaRef, CurScope, UpdLoc, IS.CounterVar, IS.CounterInit,
2843 IS.NumIterations, IS.CounterStep, IS.Subtract);
2844 if (!Final.isUsable()) {
2845 HasErrors = true;
2846 break;
2847 }
2848
2849 // Build Div for the next iteration: Div <- Div * IS.NumIters
2850 if (Cnt != 0) {
2851 if (Div.isUnset())
2852 Div = IS.NumIterations;
2853 else
2854 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
2855 IS.NumIterations);
2856
2857 // Add parentheses (for debugging purposes only).
2858 if (Div.isUsable())
2859 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
2860 if (!Div.isUsable()) {
2861 HasErrors = true;
2862 break;
2863 }
2864 }
2865 if (!Update.isUsable() || !Final.isUsable()) {
2866 HasErrors = true;
2867 break;
2868 }
2869 // Save results
2870 Built.Counters[Cnt] = IS.CounterVar;
2871 Built.Updates[Cnt] = Update.get();
2872 Built.Finals[Cnt] = Final.get();
2873 }
2874 }
2875
2876 if (HasErrors)
2877 return 0;
2878
2879 // Save results
2880 Built.IterationVarRef = IV.get();
2881 Built.LastIteration = LastIteration.get();
2882 Built.CalcLastIteration = CalcLastIteration.get();
2883 Built.PreCond = PreCond.get();
2884 Built.Cond = Cond.get();
2885 Built.SeparatedCond = SeparatedCond.get();
2886 Built.Init = Init.get();
2887 Built.Inc = Inc.get();
2888
Alexey Bataevabfc0692014-06-25 06:52:00 +00002889 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002890}
2891
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002892static Expr *GetCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002893 auto CollapseFilter = [](const OMPClause *C) -> bool {
2894 return C->getClauseKind() == OMPC_collapse;
2895 };
2896 OMPExecutableDirective::filtered_clause_iterator<decltype(CollapseFilter)> I(
2897 Clauses, CollapseFilter);
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002898 if (I)
2899 return cast<OMPCollapseClause>(*I)->getNumForLoops();
2900 return nullptr;
2901}
2902
Alexey Bataev4acb8592014-07-07 13:01:15 +00002903StmtResult Sema::ActOnOpenMPSimdDirective(
2904 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2905 SourceLocation EndLoc,
2906 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00002907 BuiltLoopExprs B;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002908 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002909 unsigned NestedLoopCount =
2910 CheckOpenMPLoop(OMPD_simd, GetCollapseNumberExpr(Clauses), AStmt, *this,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002911 *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00002912 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002913 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002914
Alexander Musmana5f070a2014-10-01 06:03:56 +00002915 assert((CurContext->isDependentContext() || B.builtAll()) &&
2916 "omp simd loop exprs were not built");
2917
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002918 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00002919 return OMPSimdDirective::Create(
2920 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt,
2921 B.IterationVarRef, B.LastIteration, B.CalcLastIteration, B.PreCond,
2922 B.Cond, B.SeparatedCond, B.Init, B.Inc, B.Counters, B.Updates, B.Finals);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002923}
2924
Alexey Bataev4acb8592014-07-07 13:01:15 +00002925StmtResult Sema::ActOnOpenMPForDirective(
2926 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2927 SourceLocation EndLoc,
2928 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00002929 BuiltLoopExprs B;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002930 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002931 unsigned NestedLoopCount =
2932 CheckOpenMPLoop(OMPD_for, GetCollapseNumberExpr(Clauses), AStmt, *this,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002933 *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00002934 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00002935 return StmtError();
2936
Alexander Musmana5f070a2014-10-01 06:03:56 +00002937 assert((CurContext->isDependentContext() || B.builtAll()) &&
2938 "omp for loop exprs were not built");
2939
Alexey Bataevf29276e2014-06-18 04:14:57 +00002940 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00002941 return OMPForDirective::Create(
2942 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt,
2943 B.IterationVarRef, B.LastIteration, B.CalcLastIteration, B.PreCond,
2944 B.Cond, B.SeparatedCond, B.Init, B.Inc, B.Counters, B.Updates, B.Finals);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002945}
2946
Alexander Musmanf82886e2014-09-18 05:12:34 +00002947StmtResult Sema::ActOnOpenMPForSimdDirective(
2948 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2949 SourceLocation EndLoc,
2950 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00002951 BuiltLoopExprs B;
Alexander Musmanf82886e2014-09-18 05:12:34 +00002952 // In presence of clause 'collapse', it will define the nested loops number.
2953 unsigned NestedLoopCount =
2954 CheckOpenMPLoop(OMPD_for_simd, GetCollapseNumberExpr(Clauses), AStmt,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002955 *this, *DSAStack, VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00002956 if (NestedLoopCount == 0)
2957 return StmtError();
2958
2959 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00002960 return OMPForSimdDirective::Create(
2961 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt,
2962 B.IterationVarRef, B.LastIteration, B.CalcLastIteration, B.PreCond,
2963 B.Cond, B.SeparatedCond, B.Init, B.Inc, B.Counters, B.Updates, B.Finals);
Alexander Musmanf82886e2014-09-18 05:12:34 +00002964}
2965
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002966StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
2967 Stmt *AStmt,
2968 SourceLocation StartLoc,
2969 SourceLocation EndLoc) {
2970 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2971 auto BaseStmt = AStmt;
2972 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
2973 BaseStmt = CS->getCapturedStmt();
2974 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
2975 auto S = C->children();
2976 if (!S)
2977 return StmtError();
2978 // All associated statements must be '#pragma omp section' except for
2979 // the first one.
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002980 for (++S; S; ++S) {
2981 auto SectionStmt = *S;
2982 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
2983 if (SectionStmt)
2984 Diag(SectionStmt->getLocStart(),
2985 diag::err_omp_sections_substmt_not_section);
2986 return StmtError();
2987 }
2988 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002989 } else {
2990 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
2991 return StmtError();
2992 }
2993
2994 getCurFunction()->setHasBranchProtectedScope();
2995
2996 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses,
2997 AStmt);
2998}
2999
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003000StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
3001 SourceLocation StartLoc,
3002 SourceLocation EndLoc) {
3003 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3004
3005 getCurFunction()->setHasBranchProtectedScope();
3006
3007 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt);
3008}
3009
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003010StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
3011 Stmt *AStmt,
3012 SourceLocation StartLoc,
3013 SourceLocation EndLoc) {
Alexey Bataev74a05c92014-07-15 02:55:09 +00003014 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3015
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003016 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00003017
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003018 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3019}
3020
Alexander Musman80c22892014-07-17 08:54:58 +00003021StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
3022 SourceLocation StartLoc,
3023 SourceLocation EndLoc) {
3024 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3025
3026 getCurFunction()->setHasBranchProtectedScope();
3027
3028 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
3029}
3030
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003031StmtResult
3032Sema::ActOnOpenMPCriticalDirective(const DeclarationNameInfo &DirName,
3033 Stmt *AStmt, SourceLocation StartLoc,
3034 SourceLocation EndLoc) {
3035 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3036
3037 getCurFunction()->setHasBranchProtectedScope();
3038
3039 return OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
3040 AStmt);
3041}
3042
Alexey Bataev4acb8592014-07-07 13:01:15 +00003043StmtResult Sema::ActOnOpenMPParallelForDirective(
3044 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3045 SourceLocation EndLoc,
3046 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
3047 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3048 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3049 // 1.2.2 OpenMP Language Terminology
3050 // Structured block - An executable statement with a single entry at the
3051 // top and a single exit at the bottom.
3052 // The point of exit cannot be a branch out of the structured block.
3053 // longjmp() and throw() must not violate the entry/exit criteria.
3054 CS->getCapturedDecl()->setNothrow();
3055
Alexander Musmana5f070a2014-10-01 06:03:56 +00003056 BuiltLoopExprs B;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003057 // In presence of clause 'collapse', it will define the nested loops number.
3058 unsigned NestedLoopCount =
3059 CheckOpenMPLoop(OMPD_parallel_for, GetCollapseNumberExpr(Clauses), AStmt,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003060 *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003061 if (NestedLoopCount == 0)
3062 return StmtError();
3063
Alexander Musmana5f070a2014-10-01 06:03:56 +00003064 assert((CurContext->isDependentContext() || B.builtAll()) &&
3065 "omp parallel for loop exprs were not built");
3066
Alexey Bataev4acb8592014-07-07 13:01:15 +00003067 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003068 return OMPParallelForDirective::Create(
3069 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt,
3070 B.IterationVarRef, B.LastIteration, B.CalcLastIteration, B.PreCond,
3071 B.Cond, B.SeparatedCond, B.Init, B.Inc, B.Counters, B.Updates, B.Finals);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003072}
3073
Alexander Musmane4e893b2014-09-23 09:33:00 +00003074StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
3075 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3076 SourceLocation EndLoc,
3077 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
3078 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3079 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3080 // 1.2.2 OpenMP Language Terminology
3081 // Structured block - An executable statement with a single entry at the
3082 // top and a single exit at the bottom.
3083 // The point of exit cannot be a branch out of the structured block.
3084 // longjmp() and throw() must not violate the entry/exit criteria.
3085 CS->getCapturedDecl()->setNothrow();
3086
Alexander Musmana5f070a2014-10-01 06:03:56 +00003087 BuiltLoopExprs B;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003088 // In presence of clause 'collapse', it will define the nested loops number.
3089 unsigned NestedLoopCount =
3090 CheckOpenMPLoop(OMPD_parallel_for_simd, GetCollapseNumberExpr(Clauses),
Alexander Musmana5f070a2014-10-01 06:03:56 +00003091 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003092 if (NestedLoopCount == 0)
3093 return StmtError();
3094
3095 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003096 return OMPParallelForSimdDirective::Create(
3097 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt,
3098 B.IterationVarRef, B.LastIteration, B.CalcLastIteration, B.PreCond,
3099 B.Cond, B.SeparatedCond, B.Init, B.Inc, B.Counters, B.Updates, B.Finals);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003100}
3101
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003102StmtResult
3103Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
3104 Stmt *AStmt, SourceLocation StartLoc,
3105 SourceLocation EndLoc) {
3106 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3107 auto BaseStmt = AStmt;
3108 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
3109 BaseStmt = CS->getCapturedStmt();
3110 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
3111 auto S = C->children();
3112 if (!S)
3113 return StmtError();
3114 // All associated statements must be '#pragma omp section' except for
3115 // the first one.
3116 for (++S; S; ++S) {
3117 auto SectionStmt = *S;
3118 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
3119 if (SectionStmt)
3120 Diag(SectionStmt->getLocStart(),
3121 diag::err_omp_parallel_sections_substmt_not_section);
3122 return StmtError();
3123 }
3124 }
3125 } else {
3126 Diag(AStmt->getLocStart(),
3127 diag::err_omp_parallel_sections_not_compound_stmt);
3128 return StmtError();
3129 }
3130
3131 getCurFunction()->setHasBranchProtectedScope();
3132
3133 return OMPParallelSectionsDirective::Create(Context, StartLoc, EndLoc,
3134 Clauses, AStmt);
3135}
3136
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003137StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
3138 Stmt *AStmt, SourceLocation StartLoc,
3139 SourceLocation EndLoc) {
3140 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3141 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3142 // 1.2.2 OpenMP Language Terminology
3143 // Structured block - An executable statement with a single entry at the
3144 // top and a single exit at the bottom.
3145 // The point of exit cannot be a branch out of the structured block.
3146 // longjmp() and throw() must not violate the entry/exit criteria.
3147 CS->getCapturedDecl()->setNothrow();
3148
3149 getCurFunction()->setHasBranchProtectedScope();
3150
3151 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3152}
3153
Alexey Bataev68446b72014-07-18 07:47:19 +00003154StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
3155 SourceLocation EndLoc) {
3156 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
3157}
3158
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003159StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
3160 SourceLocation EndLoc) {
3161 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
3162}
3163
Alexey Bataev2df347a2014-07-18 10:17:07 +00003164StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
3165 SourceLocation EndLoc) {
3166 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
3167}
3168
Alexey Bataev6125da92014-07-21 11:26:11 +00003169StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
3170 SourceLocation StartLoc,
3171 SourceLocation EndLoc) {
3172 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
3173 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
3174}
3175
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003176StmtResult Sema::ActOnOpenMPOrderedDirective(Stmt *AStmt,
3177 SourceLocation StartLoc,
3178 SourceLocation EndLoc) {
3179 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3180
3181 getCurFunction()->setHasBranchProtectedScope();
3182
3183 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, AStmt);
3184}
3185
Alexey Bataev0162e452014-07-22 10:10:35 +00003186StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
3187 Stmt *AStmt,
3188 SourceLocation StartLoc,
3189 SourceLocation EndLoc) {
3190 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003191 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00003192 // 1.2.2 OpenMP Language Terminology
3193 // Structured block - An executable statement with a single entry at the
3194 // top and a single exit at the bottom.
3195 // The point of exit cannot be a branch out of the structured block.
3196 // longjmp() and throw() must not violate the entry/exit criteria.
3197 // TODO further analysis of associated statements and clauses.
Alexey Bataevdea47612014-07-23 07:46:59 +00003198 OpenMPClauseKind AtomicKind = OMPC_unknown;
3199 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003200 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00003201 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00003202 C->getClauseKind() == OMPC_update ||
3203 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00003204 if (AtomicKind != OMPC_unknown) {
3205 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
3206 << SourceRange(C->getLocStart(), C->getLocEnd());
3207 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
3208 << getOpenMPClauseName(AtomicKind);
3209 } else {
3210 AtomicKind = C->getClauseKind();
3211 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003212 }
3213 }
3214 }
Alexey Bataev459dec02014-07-24 06:46:57 +00003215 auto Body = CS->getCapturedStmt();
Alexey Bataevdea47612014-07-23 07:46:59 +00003216 if (AtomicKind == OMPC_read) {
Alexey Bataev459dec02014-07-24 06:46:57 +00003217 if (!isa<Expr>(Body)) {
3218 Diag(Body->getLocStart(),
Alexey Bataevdea47612014-07-23 07:46:59 +00003219 diag::err_omp_atomic_read_not_expression_statement);
3220 return StmtError();
3221 }
3222 } else if (AtomicKind == OMPC_write) {
Alexey Bataev459dec02014-07-24 06:46:57 +00003223 if (!isa<Expr>(Body)) {
3224 Diag(Body->getLocStart(),
Alexey Bataevdea47612014-07-23 07:46:59 +00003225 diag::err_omp_atomic_write_not_expression_statement);
3226 return StmtError();
3227 }
Alexey Bataev67a4f222014-07-23 10:25:33 +00003228 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev459dec02014-07-24 06:46:57 +00003229 if (!isa<Expr>(Body)) {
3230 Diag(Body->getLocStart(),
Alexey Bataev67a4f222014-07-23 10:25:33 +00003231 diag::err_omp_atomic_update_not_expression_statement)
3232 << (AtomicKind == OMPC_update);
3233 return StmtError();
3234 }
Alexey Bataev459dec02014-07-24 06:46:57 +00003235 } else if (AtomicKind == OMPC_capture) {
3236 if (isa<Expr>(Body) && !isa<BinaryOperator>(Body)) {
3237 Diag(Body->getLocStart(),
3238 diag::err_omp_atomic_capture_not_expression_statement);
3239 return StmtError();
3240 } else if (!isa<Expr>(Body) && !isa<CompoundStmt>(Body)) {
3241 Diag(Body->getLocStart(),
3242 diag::err_omp_atomic_capture_not_compound_statement);
3243 return StmtError();
3244 }
Alexey Bataevdea47612014-07-23 07:46:59 +00003245 }
Alexey Bataev0162e452014-07-22 10:10:35 +00003246
3247 getCurFunction()->setHasBranchProtectedScope();
3248
3249 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3250}
3251
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003252StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
3253 Stmt *AStmt,
3254 SourceLocation StartLoc,
3255 SourceLocation EndLoc) {
3256 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3257
Alexey Bataev13314bf2014-10-09 04:18:56 +00003258 // OpenMP [2.16, Nesting of Regions]
3259 // If specified, a teams construct must be contained within a target
3260 // construct. That target construct must contain no statements or directives
3261 // outside of the teams construct.
3262 if (DSAStack->hasInnerTeamsRegion()) {
3263 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
3264 bool OMPTeamsFound = true;
3265 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
3266 auto I = CS->body_begin();
3267 while (I != CS->body_end()) {
3268 auto OED = dyn_cast<OMPExecutableDirective>(*I);
3269 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
3270 OMPTeamsFound = false;
3271 break;
3272 }
3273 ++I;
3274 }
3275 assert(I != CS->body_end() && "Not found statement");
3276 S = *I;
3277 }
3278 if (!OMPTeamsFound) {
3279 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
3280 Diag(DSAStack->getInnerTeamsRegionLoc(),
3281 diag::note_omp_nested_teams_construct_here);
3282 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
3283 << isa<OMPExecutableDirective>(S);
3284 return StmtError();
3285 }
3286 }
3287
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003288 getCurFunction()->setHasBranchProtectedScope();
3289
3290 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3291}
3292
Alexey Bataev13314bf2014-10-09 04:18:56 +00003293StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
3294 Stmt *AStmt, SourceLocation StartLoc,
3295 SourceLocation EndLoc) {
3296 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3297 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3298 // 1.2.2 OpenMP Language Terminology
3299 // Structured block - An executable statement with a single entry at the
3300 // top and a single exit at the bottom.
3301 // The point of exit cannot be a branch out of the structured block.
3302 // longjmp() and throw() must not violate the entry/exit criteria.
3303 CS->getCapturedDecl()->setNothrow();
3304
3305 getCurFunction()->setHasBranchProtectedScope();
3306
3307 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3308}
3309
Alexey Bataeved09d242014-05-28 05:53:51 +00003310OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003311 SourceLocation StartLoc,
3312 SourceLocation LParenLoc,
3313 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003314 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003315 switch (Kind) {
3316 case OMPC_if:
3317 Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc);
3318 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00003319 case OMPC_final:
3320 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
3321 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00003322 case OMPC_num_threads:
3323 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
3324 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00003325 case OMPC_safelen:
3326 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
3327 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00003328 case OMPC_collapse:
3329 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
3330 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003331 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003332 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003333 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003334 case OMPC_private:
3335 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00003336 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003337 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00003338 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00003339 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00003340 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003341 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003342 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003343 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00003344 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003345 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003346 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003347 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00003348 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003349 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00003350 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00003351 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00003352 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003353 case OMPC_seq_cst:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003354 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003355 llvm_unreachable("Clause is not allowed.");
3356 }
3357 return Res;
3358}
3359
Alexey Bataeved09d242014-05-28 05:53:51 +00003360OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003361 SourceLocation LParenLoc,
3362 SourceLocation EndLoc) {
3363 Expr *ValExpr = Condition;
3364 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
3365 !Condition->isInstantiationDependent() &&
3366 !Condition->containsUnexpandedParameterPack()) {
3367 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00003368 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003369 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003370 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003371
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003372 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003373 }
3374
3375 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc);
3376}
3377
Alexey Bataev3778b602014-07-17 07:32:53 +00003378OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
3379 SourceLocation StartLoc,
3380 SourceLocation LParenLoc,
3381 SourceLocation EndLoc) {
3382 Expr *ValExpr = Condition;
3383 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
3384 !Condition->isInstantiationDependent() &&
3385 !Condition->containsUnexpandedParameterPack()) {
3386 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
3387 Condition->getExprLoc(), Condition);
3388 if (Val.isInvalid())
3389 return nullptr;
3390
3391 ValExpr = Val.get();
3392 }
3393
3394 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
3395}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003396ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
3397 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00003398 if (!Op)
3399 return ExprError();
3400
3401 class IntConvertDiagnoser : public ICEConvertDiagnoser {
3402 public:
3403 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00003404 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00003405 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
3406 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003407 return S.Diag(Loc, diag::err_omp_not_integral) << T;
3408 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003409 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
3410 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003411 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
3412 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003413 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
3414 QualType T,
3415 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003416 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
3417 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003418 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
3419 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003420 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00003421 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00003422 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003423 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
3424 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003425 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
3426 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003427 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
3428 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003429 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00003430 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00003431 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003432 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
3433 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003434 llvm_unreachable("conversion functions are permitted");
3435 }
3436 } ConvertDiagnoser;
3437 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
3438}
3439
3440OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
3441 SourceLocation StartLoc,
3442 SourceLocation LParenLoc,
3443 SourceLocation EndLoc) {
3444 Expr *ValExpr = NumThreads;
3445 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
Alexey Bataev568a8332014-03-06 06:15:19 +00003446 !NumThreads->containsUnexpandedParameterPack()) {
3447 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
3448 ExprResult Val =
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003449 PerformOpenMPImplicitIntegerConversion(NumThreadsLoc, NumThreads);
Alexey Bataev568a8332014-03-06 06:15:19 +00003450 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003451 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00003452
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003453 ValExpr = Val.get();
Alexey Bataev568a8332014-03-06 06:15:19 +00003454
3455 // OpenMP [2.5, Restrictions]
3456 // The num_threads expression must evaluate to a positive integer value.
3457 llvm::APSInt Result;
Alexey Bataeved09d242014-05-28 05:53:51 +00003458 if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() &&
3459 !Result.isStrictlyPositive()) {
Alexey Bataev568a8332014-03-06 06:15:19 +00003460 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
3461 << "num_threads" << NumThreads->getSourceRange();
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003462 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00003463 }
3464 }
3465
Alexey Bataeved09d242014-05-28 05:53:51 +00003466 return new (Context)
3467 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00003468}
3469
Alexey Bataev62c87d22014-03-21 04:51:18 +00003470ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
3471 OpenMPClauseKind CKind) {
3472 if (!E)
3473 return ExprError();
3474 if (E->isValueDependent() || E->isTypeDependent() ||
3475 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00003476 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00003477 llvm::APSInt Result;
3478 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
3479 if (ICE.isInvalid())
3480 return ExprError();
3481 if (!Result.isStrictlyPositive()) {
3482 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
3483 << getOpenMPClauseName(CKind) << E->getSourceRange();
3484 return ExprError();
3485 }
Alexander Musman09184fe2014-09-30 05:29:28 +00003486 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
3487 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
3488 << E->getSourceRange();
3489 return ExprError();
3490 }
Alexey Bataev62c87d22014-03-21 04:51:18 +00003491 return ICE;
3492}
3493
3494OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
3495 SourceLocation LParenLoc,
3496 SourceLocation EndLoc) {
3497 // OpenMP [2.8.1, simd construct, Description]
3498 // The parameter of the safelen clause must be a constant
3499 // positive integer expression.
3500 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
3501 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003502 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00003503 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003504 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00003505}
3506
Alexander Musman64d33f12014-06-04 07:53:32 +00003507OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
3508 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00003509 SourceLocation LParenLoc,
3510 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00003511 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00003512 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00003513 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00003514 // The parameter of the collapse clause must be a constant
3515 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00003516 ExprResult NumForLoopsResult =
3517 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
3518 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00003519 return nullptr;
3520 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00003521 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00003522}
3523
Alexey Bataeved09d242014-05-28 05:53:51 +00003524OMPClause *Sema::ActOnOpenMPSimpleClause(
3525 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
3526 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003527 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003528 switch (Kind) {
3529 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00003530 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00003531 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
3532 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003533 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003534 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00003535 Res = ActOnOpenMPProcBindClause(
3536 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
3537 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003538 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003539 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00003540 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00003541 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00003542 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00003543 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003544 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003545 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003546 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00003547 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00003548 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00003549 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00003550 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00003551 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003552 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003553 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003554 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00003555 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003556 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003557 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003558 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00003559 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003560 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00003561 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00003562 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00003563 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003564 case OMPC_seq_cst:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003565 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003566 llvm_unreachable("Clause is not allowed.");
3567 }
3568 return Res;
3569}
3570
3571OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
3572 SourceLocation KindKwLoc,
3573 SourceLocation StartLoc,
3574 SourceLocation LParenLoc,
3575 SourceLocation EndLoc) {
3576 if (Kind == OMPC_DEFAULT_unknown) {
3577 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00003578 static_assert(OMPC_DEFAULT_unknown > 0,
3579 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00003580 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00003581 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003582 Values += "'";
3583 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
3584 Values += "'";
3585 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00003586 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003587 Values += " or ";
3588 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00003589 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003590 break;
3591 default:
3592 Values += Sep;
3593 break;
3594 }
3595 }
3596 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00003597 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003598 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003599 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00003600 switch (Kind) {
3601 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003602 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003603 break;
3604 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003605 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003606 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003607 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003608 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00003609 break;
3610 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003611 return new (Context)
3612 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003613}
3614
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003615OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
3616 SourceLocation KindKwLoc,
3617 SourceLocation StartLoc,
3618 SourceLocation LParenLoc,
3619 SourceLocation EndLoc) {
3620 if (Kind == OMPC_PROC_BIND_unknown) {
3621 std::string Values;
3622 std::string Sep(", ");
3623 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
3624 Values += "'";
3625 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
3626 Values += "'";
3627 switch (i) {
3628 case OMPC_PROC_BIND_unknown - 2:
3629 Values += " or ";
3630 break;
3631 case OMPC_PROC_BIND_unknown - 1:
3632 break;
3633 default:
3634 Values += Sep;
3635 break;
3636 }
3637 }
3638 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00003639 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003640 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003641 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003642 return new (Context)
3643 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003644}
3645
Alexey Bataev56dafe82014-06-20 07:16:17 +00003646OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
3647 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
3648 SourceLocation StartLoc, SourceLocation LParenLoc,
3649 SourceLocation ArgumentLoc, SourceLocation CommaLoc,
3650 SourceLocation EndLoc) {
3651 OMPClause *Res = nullptr;
3652 switch (Kind) {
3653 case OMPC_schedule:
3654 Res = ActOnOpenMPScheduleClause(
3655 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
3656 LParenLoc, ArgumentLoc, CommaLoc, EndLoc);
3657 break;
3658 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00003659 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003660 case OMPC_num_threads:
3661 case OMPC_safelen:
3662 case OMPC_collapse:
3663 case OMPC_default:
3664 case OMPC_proc_bind:
3665 case OMPC_private:
3666 case OMPC_firstprivate:
3667 case OMPC_lastprivate:
3668 case OMPC_shared:
3669 case OMPC_reduction:
3670 case OMPC_linear:
3671 case OMPC_aligned:
3672 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003673 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003674 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00003675 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003676 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003677 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003678 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00003679 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003680 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00003681 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00003682 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00003683 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003684 case OMPC_seq_cst:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003685 case OMPC_unknown:
3686 llvm_unreachable("Clause is not allowed.");
3687 }
3688 return Res;
3689}
3690
3691OMPClause *Sema::ActOnOpenMPScheduleClause(
3692 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
3693 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
3694 SourceLocation EndLoc) {
3695 if (Kind == OMPC_SCHEDULE_unknown) {
3696 std::string Values;
3697 std::string Sep(", ");
3698 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) {
3699 Values += "'";
3700 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i);
3701 Values += "'";
3702 switch (i) {
3703 case OMPC_SCHEDULE_unknown - 2:
3704 Values += " or ";
3705 break;
3706 case OMPC_SCHEDULE_unknown - 1:
3707 break;
3708 default:
3709 Values += Sep;
3710 break;
3711 }
3712 }
3713 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
3714 << Values << getOpenMPClauseName(OMPC_schedule);
3715 return nullptr;
3716 }
3717 Expr *ValExpr = ChunkSize;
3718 if (ChunkSize) {
3719 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
3720 !ChunkSize->isInstantiationDependent() &&
3721 !ChunkSize->containsUnexpandedParameterPack()) {
3722 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
3723 ExprResult Val =
3724 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
3725 if (Val.isInvalid())
3726 return nullptr;
3727
3728 ValExpr = Val.get();
3729
3730 // OpenMP [2.7.1, Restrictions]
3731 // chunk_size must be a loop invariant integer expression with a positive
3732 // value.
3733 llvm::APSInt Result;
3734 if (ValExpr->isIntegerConstantExpr(Result, Context) &&
3735 Result.isSigned() && !Result.isStrictlyPositive()) {
3736 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
3737 << "schedule" << ChunkSize->getSourceRange();
3738 return nullptr;
3739 }
3740 }
3741 }
3742
3743 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc,
3744 EndLoc, Kind, ValExpr);
3745}
3746
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003747OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
3748 SourceLocation StartLoc,
3749 SourceLocation EndLoc) {
3750 OMPClause *Res = nullptr;
3751 switch (Kind) {
3752 case OMPC_ordered:
3753 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
3754 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00003755 case OMPC_nowait:
3756 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
3757 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003758 case OMPC_untied:
3759 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
3760 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003761 case OMPC_mergeable:
3762 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
3763 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003764 case OMPC_read:
3765 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
3766 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00003767 case OMPC_write:
3768 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
3769 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00003770 case OMPC_update:
3771 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
3772 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00003773 case OMPC_capture:
3774 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
3775 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003776 case OMPC_seq_cst:
3777 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
3778 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003779 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00003780 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003781 case OMPC_num_threads:
3782 case OMPC_safelen:
3783 case OMPC_collapse:
3784 case OMPC_schedule:
3785 case OMPC_private:
3786 case OMPC_firstprivate:
3787 case OMPC_lastprivate:
3788 case OMPC_shared:
3789 case OMPC_reduction:
3790 case OMPC_linear:
3791 case OMPC_aligned:
3792 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003793 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003794 case OMPC_default:
3795 case OMPC_proc_bind:
3796 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00003797 case OMPC_flush:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003798 case OMPC_unknown:
3799 llvm_unreachable("Clause is not allowed.");
3800 }
3801 return Res;
3802}
3803
3804OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
3805 SourceLocation EndLoc) {
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003806 DSAStack->setOrderedRegion();
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003807 return new (Context) OMPOrderedClause(StartLoc, EndLoc);
3808}
3809
Alexey Bataev236070f2014-06-20 11:19:47 +00003810OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
3811 SourceLocation EndLoc) {
3812 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
3813}
3814
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003815OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
3816 SourceLocation EndLoc) {
3817 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
3818}
3819
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003820OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
3821 SourceLocation EndLoc) {
3822 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
3823}
3824
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003825OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
3826 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003827 return new (Context) OMPReadClause(StartLoc, EndLoc);
3828}
3829
Alexey Bataevdea47612014-07-23 07:46:59 +00003830OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
3831 SourceLocation EndLoc) {
3832 return new (Context) OMPWriteClause(StartLoc, EndLoc);
3833}
3834
Alexey Bataev67a4f222014-07-23 10:25:33 +00003835OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
3836 SourceLocation EndLoc) {
3837 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
3838}
3839
Alexey Bataev459dec02014-07-24 06:46:57 +00003840OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
3841 SourceLocation EndLoc) {
3842 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
3843}
3844
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003845OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
3846 SourceLocation EndLoc) {
3847 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
3848}
3849
Alexey Bataevc5e02582014-06-16 07:08:35 +00003850OMPClause *Sema::ActOnOpenMPVarListClause(
3851 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
3852 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
3853 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
3854 const DeclarationNameInfo &ReductionId) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003855 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003856 switch (Kind) {
3857 case OMPC_private:
3858 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
3859 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003860 case OMPC_firstprivate:
3861 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
3862 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00003863 case OMPC_lastprivate:
3864 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
3865 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00003866 case OMPC_shared:
3867 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
3868 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00003869 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00003870 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
3871 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00003872 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00003873 case OMPC_linear:
3874 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
3875 ColonLoc, EndLoc);
3876 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00003877 case OMPC_aligned:
3878 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
3879 ColonLoc, EndLoc);
3880 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003881 case OMPC_copyin:
3882 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
3883 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00003884 case OMPC_copyprivate:
3885 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
3886 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003887 case OMPC_flush:
3888 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
3889 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003890 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00003891 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00003892 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00003893 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00003894 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003895 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003896 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003897 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003898 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00003899 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003900 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003901 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003902 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003903 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00003904 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00003905 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00003906 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003907 case OMPC_seq_cst:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003908 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003909 llvm_unreachable("Clause is not allowed.");
3910 }
3911 return Res;
3912}
3913
3914OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
3915 SourceLocation StartLoc,
3916 SourceLocation LParenLoc,
3917 SourceLocation EndLoc) {
3918 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00003919 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00003920 for (auto &RefExpr : VarList) {
3921 assert(RefExpr && "NULL expr in OpenMP private clause.");
3922 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003923 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00003924 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00003925 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003926 continue;
3927 }
3928
Alexey Bataeved09d242014-05-28 05:53:51 +00003929 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003930 // OpenMP [2.1, C/C++]
3931 // A list item is a variable name.
3932 // OpenMP [2.9.3.3, Restrictions, p.1]
3933 // A variable that is part of another variable (as an array or
3934 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00003935 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003936 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003937 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003938 continue;
3939 }
3940 Decl *D = DE->getDecl();
3941 VarDecl *VD = cast<VarDecl>(D);
3942
3943 QualType Type = VD->getType();
3944 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3945 // It will be analyzed later.
3946 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00003947 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003948 continue;
3949 }
3950
3951 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
3952 // A variable that appears in a private clause must not have an incomplete
3953 // type or a reference type.
3954 if (RequireCompleteType(ELoc, Type,
3955 diag::err_omp_private_incomplete_type)) {
3956 continue;
3957 }
3958 if (Type->isReferenceType()) {
3959 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00003960 << getOpenMPClauseName(OMPC_private) << Type;
3961 bool IsDecl =
3962 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3963 Diag(VD->getLocation(),
3964 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3965 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003966 continue;
3967 }
3968
3969 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
3970 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00003971 // clause requires an accessible, unambiguous default constructor for the
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003972 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00003973 while (Type->isArrayType()) {
3974 Type = cast<ArrayType>(Type.getTypePtr())->getElementType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003975 }
3976
Alexey Bataev758e55e2013-09-06 18:03:48 +00003977 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
3978 // in a Construct]
3979 // Variables with the predetermined data-sharing attributes may not be
3980 // listed in data-sharing attributes clauses, except for the cases
3981 // listed below. For these exceptions only, listing a predetermined
3982 // variable in a data-sharing attribute clause is allowed and overrides
3983 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003984 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003985 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003986 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
3987 << getOpenMPClauseName(OMPC_private);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003988 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003989 continue;
3990 }
3991
Alexey Bataev03b340a2014-10-21 03:16:40 +00003992 // Generate helper private variable and initialize it with the default
3993 // value. The address of the original variable is replaced by the address of
3994 // the new private variable in CodeGen. This new variable is not added to
3995 // IdResolver, so the code in the OpenMP region uses original variable for
3996 // proper diagnostics.
3997 auto VDPrivate =
3998 VarDecl::Create(Context, CurContext, DE->getLocStart(),
3999 DE->getExprLoc(), VD->getIdentifier(), VD->getType(),
4000 VD->getTypeSourceInfo(), /*S*/ SC_Auto);
4001 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto*/ false);
4002 if (VDPrivate->isInvalidDecl())
4003 continue;
4004 CurContext->addDecl(VDPrivate);
4005 auto VDPrivateRefExpr = DeclRefExpr::Create(
4006 Context, /*QualifierLoc*/ NestedNameSpecifierLoc(),
4007 /*TemplateKWLoc*/ SourceLocation(), VDPrivate,
4008 /*isEnclosingLocal*/ false, /*NameLoc*/ SourceLocation(), DE->getType(),
4009 /*VK*/ VK_LValue);
4010
Alexey Bataev758e55e2013-09-06 18:03:48 +00004011 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004012 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00004013 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004014 }
4015
Alexey Bataeved09d242014-05-28 05:53:51 +00004016 if (Vars.empty())
4017 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004018
Alexey Bataev03b340a2014-10-21 03:16:40 +00004019 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
4020 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004021}
4022
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004023namespace {
4024class DiagsUninitializedSeveretyRAII {
4025private:
4026 DiagnosticsEngine &Diags;
4027 SourceLocation SavedLoc;
4028 bool IsIgnored;
4029
4030public:
4031 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
4032 bool IsIgnored)
4033 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
4034 if (!IsIgnored) {
4035 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
4036 /*Map*/ diag::Severity::Ignored, Loc);
4037 }
4038 }
4039 ~DiagsUninitializedSeveretyRAII() {
4040 if (!IsIgnored)
4041 Diags.popMappings(SavedLoc);
4042 }
4043};
4044}
4045
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004046OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
4047 SourceLocation StartLoc,
4048 SourceLocation LParenLoc,
4049 SourceLocation EndLoc) {
4050 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004051 SmallVector<Expr *, 8> PrivateCopies;
4052 SmallVector<Expr *, 8> Inits;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004053 bool IsImplicitClause =
4054 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
4055 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
4056
Alexey Bataeved09d242014-05-28 05:53:51 +00004057 for (auto &RefExpr : VarList) {
4058 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
4059 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004060 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00004061 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004062 PrivateCopies.push_back(nullptr);
4063 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004064 continue;
4065 }
4066
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004067 SourceLocation ELoc =
4068 IsImplicitClause ? ImplicitClauseLoc : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004069 // OpenMP [2.1, C/C++]
4070 // A list item is a variable name.
4071 // OpenMP [2.9.3.3, Restrictions, p.1]
4072 // A variable that is part of another variable (as an array or
4073 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00004074 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004075 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00004076 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004077 continue;
4078 }
4079 Decl *D = DE->getDecl();
4080 VarDecl *VD = cast<VarDecl>(D);
4081
4082 QualType Type = VD->getType();
4083 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
4084 // It will be analyzed later.
4085 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004086 PrivateCopies.push_back(nullptr);
4087 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004088 continue;
4089 }
4090
4091 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
4092 // A variable that appears in a private clause must not have an incomplete
4093 // type or a reference type.
4094 if (RequireCompleteType(ELoc, Type,
4095 diag::err_omp_firstprivate_incomplete_type)) {
4096 continue;
4097 }
4098 if (Type->isReferenceType()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004099 if (IsImplicitClause) {
4100 Diag(ImplicitClauseLoc,
4101 diag::err_omp_task_predetermined_firstprivate_ref_type_arg)
4102 << Type;
4103 Diag(RefExpr->getExprLoc(), diag::note_used_here);
4104 } else {
4105 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
4106 << getOpenMPClauseName(OMPC_firstprivate) << Type;
4107 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004108 bool IsDecl =
4109 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4110 Diag(VD->getLocation(),
4111 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4112 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004113 continue;
4114 }
4115
4116 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
4117 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00004118 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004119 // class type.
4120 Type = Context.getBaseElementType(Type);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004121
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004122 // If an implicit firstprivate variable found it was checked already.
4123 if (!IsImplicitClause) {
4124 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004125 Type = Type.getNonReferenceType().getCanonicalType();
4126 bool IsConstant = Type.isConstant(Context);
4127 Type = Context.getBaseElementType(Type);
4128 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
4129 // A list item that specifies a given variable may not appear in more
4130 // than one clause on the same directive, except that a variable may be
4131 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004132 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00004133 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004134 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00004135 << getOpenMPClauseName(DVar.CKind)
4136 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004137 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004138 continue;
4139 }
4140
4141 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
4142 // in a Construct]
4143 // Variables with the predetermined data-sharing attributes may not be
4144 // listed in data-sharing attributes clauses, except for the cases
4145 // listed below. For these exceptions only, listing a predetermined
4146 // variable in a data-sharing attribute clause is allowed and overrides
4147 // the variable's predetermined data-sharing attributes.
4148 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
4149 // in a Construct, C/C++, p.2]
4150 // Variables with const-qualified type having no mutable member may be
4151 // listed in a firstprivate clause, even if they are static data members.
4152 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
4153 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
4154 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00004155 << getOpenMPClauseName(DVar.CKind)
4156 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004157 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004158 continue;
4159 }
4160
Alexey Bataevf29276e2014-06-18 04:14:57 +00004161 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004162 // OpenMP [2.9.3.4, Restrictions, p.2]
4163 // A list item that is private within a parallel region must not appear
4164 // in a firstprivate clause on a worksharing construct if any of the
4165 // worksharing regions arising from the worksharing construct ever bind
4166 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00004167 if (isOpenMPWorksharingDirective(CurrDir) &&
4168 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004169 DVar = DSAStack->getImplicitDSA(VD, true);
4170 if (DVar.CKind != OMPC_shared &&
4171 (isOpenMPParallelDirective(DVar.DKind) ||
4172 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00004173 Diag(ELoc, diag::err_omp_required_access)
4174 << getOpenMPClauseName(OMPC_firstprivate)
4175 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004176 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004177 continue;
4178 }
4179 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004180 // OpenMP [2.9.3.4, Restrictions, p.3]
4181 // A list item that appears in a reduction clause of a parallel construct
4182 // must not appear in a firstprivate clause on a worksharing or task
4183 // construct if any of the worksharing or task regions arising from the
4184 // worksharing or task construct ever bind to any of the parallel regions
4185 // arising from the parallel construct.
4186 // OpenMP [2.9.3.4, Restrictions, p.4]
4187 // A list item that appears in a reduction clause in worksharing
4188 // construct must not appear in a firstprivate clause in a task construct
4189 // encountered during execution of any of the worksharing regions arising
4190 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004191 if (CurrDir == OMPD_task) {
4192 DVar =
4193 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
4194 [](OpenMPDirectiveKind K) -> bool {
4195 return isOpenMPParallelDirective(K) ||
4196 isOpenMPWorksharingDirective(K);
4197 },
4198 false);
4199 if (DVar.CKind == OMPC_reduction &&
4200 (isOpenMPParallelDirective(DVar.DKind) ||
4201 isOpenMPWorksharingDirective(DVar.DKind))) {
4202 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
4203 << getOpenMPDirectiveName(DVar.DKind);
4204 ReportOriginalDSA(*this, DSAStack, VD, DVar);
4205 continue;
4206 }
4207 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004208 }
4209
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004210 Type = Type.getUnqualifiedType();
4211 auto VDPrivate = VarDecl::Create(Context, CurContext, DE->getLocStart(),
4212 ELoc, VD->getIdentifier(), VD->getType(),
4213 VD->getTypeSourceInfo(), /*S*/ SC_Auto);
4214 // Generate helper private variable and initialize it with the value of the
4215 // original variable. The address of the original variable is replaced by
4216 // the address of the new private variable in the CodeGen. This new variable
4217 // is not added to IdResolver, so the code in the OpenMP region uses
4218 // original variable for proper diagnostics and variable capturing.
4219 Expr *VDInitRefExpr = nullptr;
4220 // For arrays generate initializer for single element and replace it by the
4221 // original array element in CodeGen.
4222 if (DE->getType()->isArrayType()) {
4223 auto VDInit = VarDecl::Create(Context, CurContext, DE->getLocStart(),
4224 ELoc, VD->getIdentifier(), Type,
4225 VD->getTypeSourceInfo(), /*S*/ SC_Auto);
4226 CurContext->addHiddenDecl(VDInit);
4227 VDInitRefExpr = DeclRefExpr::Create(
4228 Context, /*QualifierLoc*/ NestedNameSpecifierLoc(),
4229 /*TemplateKWLoc*/ SourceLocation(), VDInit,
4230 /*isEnclosingLocal*/ false, ELoc, Type,
4231 /*VK*/ VK_LValue);
4232 VDInit->setIsUsed();
4233 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
4234 InitializedEntity Entity = InitializedEntity::InitializeVariable(VDInit);
4235 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
4236
4237 InitializationSequence InitSeq(*this, Entity, Kind, Init);
4238 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
4239 if (Result.isInvalid())
4240 VDPrivate->setInvalidDecl();
4241 else
4242 VDPrivate->setInit(Result.getAs<Expr>());
4243 } else {
4244 AddInitializerToDecl(VDPrivate, DefaultLvalueConversion(DE).get(),
4245 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4246 }
4247 if (VDPrivate->isInvalidDecl()) {
4248 if (IsImplicitClause) {
4249 Diag(DE->getExprLoc(),
4250 diag::note_omp_task_predetermined_firstprivate_here);
4251 }
4252 continue;
4253 }
4254 CurContext->addDecl(VDPrivate);
4255 auto VDPrivateRefExpr = DeclRefExpr::Create(
4256 Context, /*QualifierLoc*/ NestedNameSpecifierLoc(),
4257 /*TemplateKWLoc*/ SourceLocation(), VDPrivate,
4258 /*isEnclosingLocal*/ false, DE->getLocStart(), DE->getType(),
4259 /*VK*/ VK_LValue);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004260 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
4261 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004262 PrivateCopies.push_back(VDPrivateRefExpr);
4263 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004264 }
4265
Alexey Bataeved09d242014-05-28 05:53:51 +00004266 if (Vars.empty())
4267 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004268
4269 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004270 Vars, PrivateCopies, Inits);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004271}
4272
Alexander Musman1bb328c2014-06-04 13:06:39 +00004273OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
4274 SourceLocation StartLoc,
4275 SourceLocation LParenLoc,
4276 SourceLocation EndLoc) {
4277 SmallVector<Expr *, 8> Vars;
4278 for (auto &RefExpr : VarList) {
4279 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
4280 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
4281 // It will be analyzed later.
4282 Vars.push_back(RefExpr);
4283 continue;
4284 }
4285
4286 SourceLocation ELoc = RefExpr->getExprLoc();
4287 // OpenMP [2.1, C/C++]
4288 // A list item is a variable name.
4289 // OpenMP [2.14.3.5, Restrictions, p.1]
4290 // A variable that is part of another variable (as an array or structure
4291 // element) cannot appear in a lastprivate clause.
4292 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
4293 if (!DE || !isa<VarDecl>(DE->getDecl())) {
4294 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
4295 continue;
4296 }
4297 Decl *D = DE->getDecl();
4298 VarDecl *VD = cast<VarDecl>(D);
4299
4300 QualType Type = VD->getType();
4301 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
4302 // It will be analyzed later.
4303 Vars.push_back(DE);
4304 continue;
4305 }
4306
4307 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
4308 // A variable that appears in a lastprivate clause must not have an
4309 // incomplete type or a reference type.
4310 if (RequireCompleteType(ELoc, Type,
4311 diag::err_omp_lastprivate_incomplete_type)) {
4312 continue;
4313 }
4314 if (Type->isReferenceType()) {
4315 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
4316 << getOpenMPClauseName(OMPC_lastprivate) << Type;
4317 bool IsDecl =
4318 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4319 Diag(VD->getLocation(),
4320 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4321 << VD;
4322 continue;
4323 }
4324
4325 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4326 // in a Construct]
4327 // Variables with the predetermined data-sharing attributes may not be
4328 // listed in data-sharing attributes clauses, except for the cases
4329 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004330 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00004331 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
4332 DVar.CKind != OMPC_firstprivate &&
4333 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
4334 Diag(ELoc, diag::err_omp_wrong_dsa)
4335 << getOpenMPClauseName(DVar.CKind)
4336 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004337 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00004338 continue;
4339 }
4340
Alexey Bataevf29276e2014-06-18 04:14:57 +00004341 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
4342 // OpenMP [2.14.3.5, Restrictions, p.2]
4343 // A list item that is private within a parallel region, or that appears in
4344 // the reduction clause of a parallel construct, must not appear in a
4345 // lastprivate clause on a worksharing construct if any of the corresponding
4346 // worksharing regions ever binds to any of the corresponding parallel
4347 // regions.
Alexey Bataev549210e2014-06-24 04:39:47 +00004348 if (isOpenMPWorksharingDirective(CurrDir) &&
4349 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004350 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004351 if (DVar.CKind != OMPC_shared) {
4352 Diag(ELoc, diag::err_omp_required_access)
4353 << getOpenMPClauseName(OMPC_lastprivate)
4354 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004355 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004356 continue;
4357 }
4358 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00004359 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00004360 // A variable of class type (or array thereof) that appears in a
4361 // lastprivate clause requires an accessible, unambiguous default
4362 // constructor for the class type, unless the list item is also specified
4363 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00004364 // A variable of class type (or array thereof) that appears in a
4365 // lastprivate clause requires an accessible, unambiguous copy assignment
4366 // operator for the class type.
4367 while (Type.getNonReferenceType()->isArrayType())
4368 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
4369 ->getElementType();
4370 CXXRecordDecl *RD = getLangOpts().CPlusPlus
4371 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
4372 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00004373 // FIXME This code must be replaced by actual copying and destructing of the
4374 // lastprivate variable.
Alexander Musman1bb328c2014-06-04 13:06:39 +00004375 if (RD) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00004376 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
4377 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00004378 if (MD) {
4379 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
4380 MD->isDeleted()) {
4381 Diag(ELoc, diag::err_omp_required_method)
4382 << getOpenMPClauseName(OMPC_lastprivate) << 2;
4383 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4384 VarDecl::DeclarationOnly;
4385 Diag(VD->getLocation(),
4386 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4387 << VD;
4388 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4389 continue;
4390 }
4391 MarkFunctionReferenced(ELoc, MD);
4392 DiagnoseUseOfDecl(MD, ELoc);
Alexander Musman1bb328c2014-06-04 13:06:39 +00004393 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00004394
4395 CXXDestructorDecl *DD = RD->getDestructor();
4396 if (DD) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00004397 PartialDiagnostic PD =
4398 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexander Musman1bb328c2014-06-04 13:06:39 +00004399 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
4400 DD->isDeleted()) {
4401 Diag(ELoc, diag::err_omp_required_method)
4402 << getOpenMPClauseName(OMPC_lastprivate) << 4;
4403 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4404 VarDecl::DeclarationOnly;
4405 Diag(VD->getLocation(),
4406 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4407 << VD;
4408 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4409 continue;
4410 }
4411 MarkFunctionReferenced(ELoc, DD);
4412 DiagnoseUseOfDecl(DD, ELoc);
4413 }
4414 }
4415
Alexey Bataevf29276e2014-06-18 04:14:57 +00004416 if (DVar.CKind != OMPC_firstprivate)
4417 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00004418 Vars.push_back(DE);
4419 }
4420
4421 if (Vars.empty())
4422 return nullptr;
4423
4424 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
4425 Vars);
4426}
4427
Alexey Bataev758e55e2013-09-06 18:03:48 +00004428OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
4429 SourceLocation StartLoc,
4430 SourceLocation LParenLoc,
4431 SourceLocation EndLoc) {
4432 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00004433 for (auto &RefExpr : VarList) {
4434 assert(RefExpr && "NULL expr in OpenMP shared clause.");
4435 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00004436 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00004437 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004438 continue;
4439 }
4440
Alexey Bataeved09d242014-05-28 05:53:51 +00004441 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00004442 // OpenMP [2.1, C/C++]
4443 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00004444 // OpenMP [2.14.3.2, Restrictions, p.1]
4445 // A variable that is part of another variable (as an array or structure
4446 // element) cannot appear in a shared unless it is a static data member
4447 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00004448 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004449 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00004450 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00004451 continue;
4452 }
4453 Decl *D = DE->getDecl();
4454 VarDecl *VD = cast<VarDecl>(D);
4455
4456 QualType Type = VD->getType();
4457 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
4458 // It will be analyzed later.
4459 Vars.push_back(DE);
4460 continue;
4461 }
4462
4463 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
4464 // in a Construct]
4465 // Variables with the predetermined data-sharing attributes may not be
4466 // listed in data-sharing attributes clauses, except for the cases
4467 // listed below. For these exceptions only, listing a predetermined
4468 // variable in a data-sharing attribute clause is allowed and overrides
4469 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004470 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00004471 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
4472 DVar.RefExpr) {
4473 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
4474 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004475 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004476 continue;
4477 }
4478
4479 DSAStack->addDSA(VD, DE, OMPC_shared);
4480 Vars.push_back(DE);
4481 }
4482
Alexey Bataeved09d242014-05-28 05:53:51 +00004483 if (Vars.empty())
4484 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00004485
4486 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
4487}
4488
Alexey Bataevc5e02582014-06-16 07:08:35 +00004489namespace {
4490class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
4491 DSAStackTy *Stack;
4492
4493public:
4494 bool VisitDeclRefExpr(DeclRefExpr *E) {
4495 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004496 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00004497 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
4498 return false;
4499 if (DVar.CKind != OMPC_unknown)
4500 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00004501 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004502 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004503 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00004504 return true;
4505 return false;
4506 }
4507 return false;
4508 }
4509 bool VisitStmt(Stmt *S) {
4510 for (auto Child : S->children()) {
4511 if (Child && Visit(Child))
4512 return true;
4513 }
4514 return false;
4515 }
Alexey Bataev23b69422014-06-18 07:08:49 +00004516 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00004517};
Alexey Bataev23b69422014-06-18 07:08:49 +00004518} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00004519
4520OMPClause *Sema::ActOnOpenMPReductionClause(
4521 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
4522 SourceLocation ColonLoc, SourceLocation EndLoc,
4523 CXXScopeSpec &ReductionIdScopeSpec,
4524 const DeclarationNameInfo &ReductionId) {
4525 // TODO: Allow scope specification search when 'declare reduction' is
4526 // supported.
4527 assert(ReductionIdScopeSpec.isEmpty() &&
4528 "No support for scoped reduction identifiers yet.");
4529
4530 auto DN = ReductionId.getName();
4531 auto OOK = DN.getCXXOverloadedOperator();
4532 BinaryOperatorKind BOK = BO_Comma;
4533
4534 // OpenMP [2.14.3.6, reduction clause]
4535 // C
4536 // reduction-identifier is either an identifier or one of the following
4537 // operators: +, -, *, &, |, ^, && and ||
4538 // C++
4539 // reduction-identifier is either an id-expression or one of the following
4540 // operators: +, -, *, &, |, ^, && and ||
4541 // FIXME: Only 'min' and 'max' identifiers are supported for now.
4542 switch (OOK) {
4543 case OO_Plus:
4544 case OO_Minus:
4545 BOK = BO_AddAssign;
4546 break;
4547 case OO_Star:
4548 BOK = BO_MulAssign;
4549 break;
4550 case OO_Amp:
4551 BOK = BO_AndAssign;
4552 break;
4553 case OO_Pipe:
4554 BOK = BO_OrAssign;
4555 break;
4556 case OO_Caret:
4557 BOK = BO_XorAssign;
4558 break;
4559 case OO_AmpAmp:
4560 BOK = BO_LAnd;
4561 break;
4562 case OO_PipePipe:
4563 BOK = BO_LOr;
4564 break;
4565 default:
4566 if (auto II = DN.getAsIdentifierInfo()) {
4567 if (II->isStr("max"))
4568 BOK = BO_GT;
4569 else if (II->isStr("min"))
4570 BOK = BO_LT;
4571 }
4572 break;
4573 }
4574 SourceRange ReductionIdRange;
4575 if (ReductionIdScopeSpec.isValid()) {
4576 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
4577 }
4578 ReductionIdRange.setEnd(ReductionId.getEndLoc());
4579 if (BOK == BO_Comma) {
4580 // Not allowed reduction identifier is found.
4581 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
4582 << ReductionIdRange;
4583 return nullptr;
4584 }
4585
4586 SmallVector<Expr *, 8> Vars;
4587 for (auto RefExpr : VarList) {
4588 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
4589 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
4590 // It will be analyzed later.
4591 Vars.push_back(RefExpr);
4592 continue;
4593 }
4594
4595 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
4596 RefExpr->isInstantiationDependent() ||
4597 RefExpr->containsUnexpandedParameterPack()) {
4598 // It will be analyzed later.
4599 Vars.push_back(RefExpr);
4600 continue;
4601 }
4602
4603 auto ELoc = RefExpr->getExprLoc();
4604 auto ERange = RefExpr->getSourceRange();
4605 // OpenMP [2.1, C/C++]
4606 // A list item is a variable or array section, subject to the restrictions
4607 // specified in Section 2.4 on page 42 and in each of the sections
4608 // describing clauses and directives for which a list appears.
4609 // OpenMP [2.14.3.3, Restrictions, p.1]
4610 // A variable that is part of another variable (as an array or
4611 // structure element) cannot appear in a private clause.
4612 auto DE = dyn_cast<DeclRefExpr>(RefExpr);
4613 if (!DE || !isa<VarDecl>(DE->getDecl())) {
4614 Diag(ELoc, diag::err_omp_expected_var_name) << ERange;
4615 continue;
4616 }
4617 auto D = DE->getDecl();
4618 auto VD = cast<VarDecl>(D);
4619 auto Type = VD->getType();
4620 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
4621 // A variable that appears in a private clause must not have an incomplete
4622 // type or a reference type.
4623 if (RequireCompleteType(ELoc, Type,
4624 diag::err_omp_reduction_incomplete_type))
4625 continue;
4626 // OpenMP [2.14.3.6, reduction clause, Restrictions]
4627 // Arrays may not appear in a reduction clause.
4628 if (Type.getNonReferenceType()->isArrayType()) {
4629 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
4630 bool IsDecl =
4631 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4632 Diag(VD->getLocation(),
4633 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4634 << VD;
4635 continue;
4636 }
4637 // OpenMP [2.14.3.6, reduction clause, Restrictions]
4638 // A list item that appears in a reduction clause must not be
4639 // const-qualified.
4640 if (Type.getNonReferenceType().isConstant(Context)) {
4641 Diag(ELoc, diag::err_omp_const_variable)
4642 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
4643 bool IsDecl =
4644 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4645 Diag(VD->getLocation(),
4646 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4647 << VD;
4648 continue;
4649 }
4650 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
4651 // If a list-item is a reference type then it must bind to the same object
4652 // for all threads of the team.
4653 VarDecl *VDDef = VD->getDefinition();
4654 if (Type->isReferenceType() && VDDef) {
4655 DSARefChecker Check(DSAStack);
4656 if (Check.Visit(VDDef->getInit())) {
4657 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
4658 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
4659 continue;
4660 }
4661 }
4662 // OpenMP [2.14.3.6, reduction clause, Restrictions]
4663 // The type of a list item that appears in a reduction clause must be valid
4664 // for the reduction-identifier. For a max or min reduction in C, the type
4665 // of the list item must be an allowed arithmetic data type: char, int,
4666 // float, double, or _Bool, possibly modified with long, short, signed, or
4667 // unsigned. For a max or min reduction in C++, the type of the list item
4668 // must be an allowed arithmetic data type: char, wchar_t, int, float,
4669 // double, or bool, possibly modified with long, short, signed, or unsigned.
4670 if ((BOK == BO_GT || BOK == BO_LT) &&
4671 !(Type->isScalarType() ||
4672 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
4673 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
4674 << getLangOpts().CPlusPlus;
4675 bool IsDecl =
4676 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4677 Diag(VD->getLocation(),
4678 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4679 << VD;
4680 continue;
4681 }
4682 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
4683 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
4684 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
4685 bool IsDecl =
4686 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4687 Diag(VD->getLocation(),
4688 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4689 << VD;
4690 continue;
4691 }
4692 bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
4693 getDiagnostics().setSuppressAllDiagnostics(true);
4694 ExprResult ReductionOp =
4695 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
4696 RefExpr, RefExpr);
4697 getDiagnostics().setSuppressAllDiagnostics(Suppress);
4698 if (ReductionOp.isInvalid()) {
4699 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
Alexey Bataev23b69422014-06-18 07:08:49 +00004700 << ReductionIdRange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00004701 bool IsDecl =
4702 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4703 Diag(VD->getLocation(),
4704 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4705 << VD;
4706 continue;
4707 }
4708
4709 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4710 // in a Construct]
4711 // Variables with the predetermined data-sharing attributes may not be
4712 // listed in data-sharing attributes clauses, except for the cases
4713 // listed below. For these exceptions only, listing a predetermined
4714 // variable in a data-sharing attribute clause is allowed and overrides
4715 // the variable's predetermined data-sharing attributes.
4716 // OpenMP [2.14.3.6, Restrictions, p.3]
4717 // Any number of reduction clauses can be specified on the directive,
4718 // but a list item can appear only once in the reduction clauses for that
4719 // directive.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004720 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00004721 if (DVar.CKind == OMPC_reduction) {
4722 Diag(ELoc, diag::err_omp_once_referenced)
4723 << getOpenMPClauseName(OMPC_reduction);
4724 if (DVar.RefExpr) {
4725 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
4726 }
4727 } else if (DVar.CKind != OMPC_unknown) {
4728 Diag(ELoc, diag::err_omp_wrong_dsa)
4729 << getOpenMPClauseName(DVar.CKind)
4730 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004731 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00004732 continue;
4733 }
4734
4735 // OpenMP [2.14.3.6, Restrictions, p.1]
4736 // A list item that appears in a reduction clause of a worksharing
4737 // construct must be shared in the parallel regions to which any of the
4738 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf29276e2014-06-18 04:14:57 +00004739 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataev549210e2014-06-24 04:39:47 +00004740 if (isOpenMPWorksharingDirective(CurrDir) &&
4741 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004742 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004743 if (DVar.CKind != OMPC_shared) {
4744 Diag(ELoc, diag::err_omp_required_access)
4745 << getOpenMPClauseName(OMPC_reduction)
4746 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004747 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004748 continue;
4749 }
4750 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00004751
4752 CXXRecordDecl *RD = getLangOpts().CPlusPlus
4753 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
4754 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00004755 // FIXME This code must be replaced by actual constructing/destructing of
4756 // the reduction variable.
Alexey Bataevc5e02582014-06-16 07:08:35 +00004757 if (RD) {
4758 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
4759 PartialDiagnostic PD =
4760 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataev23b69422014-06-18 07:08:49 +00004761 if (!CD ||
4762 CheckConstructorAccess(ELoc, CD,
4763 InitializedEntity::InitializeTemporary(Type),
4764 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevc5e02582014-06-16 07:08:35 +00004765 CD->isDeleted()) {
4766 Diag(ELoc, diag::err_omp_required_method)
4767 << getOpenMPClauseName(OMPC_reduction) << 0;
4768 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4769 VarDecl::DeclarationOnly;
4770 Diag(VD->getLocation(),
4771 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4772 << VD;
4773 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4774 continue;
4775 }
4776 MarkFunctionReferenced(ELoc, CD);
4777 DiagnoseUseOfDecl(CD, ELoc);
4778
4779 CXXDestructorDecl *DD = RD->getDestructor();
4780 if (DD) {
4781 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
4782 DD->isDeleted()) {
4783 Diag(ELoc, diag::err_omp_required_method)
4784 << getOpenMPClauseName(OMPC_reduction) << 4;
4785 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4786 VarDecl::DeclarationOnly;
4787 Diag(VD->getLocation(),
4788 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4789 << VD;
4790 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4791 continue;
4792 }
4793 MarkFunctionReferenced(ELoc, DD);
4794 DiagnoseUseOfDecl(DD, ELoc);
4795 }
4796 }
4797
4798 DSAStack->addDSA(VD, DE, OMPC_reduction);
4799 Vars.push_back(DE);
4800 }
4801
4802 if (Vars.empty())
4803 return nullptr;
4804
4805 return OMPReductionClause::Create(
4806 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
4807 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId);
4808}
4809
Alexander Musman8dba6642014-04-22 13:09:42 +00004810OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step,
4811 SourceLocation StartLoc,
4812 SourceLocation LParenLoc,
4813 SourceLocation ColonLoc,
4814 SourceLocation EndLoc) {
4815 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00004816 for (auto &RefExpr : VarList) {
4817 assert(RefExpr && "NULL expr in OpenMP linear clause.");
4818 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00004819 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00004820 Vars.push_back(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00004821 continue;
4822 }
4823
4824 // OpenMP [2.14.3.7, linear clause]
4825 // A list item that appears in a linear clause is subject to the private
4826 // clause semantics described in Section 2.14.3.3 on page 159 except as
4827 // noted. In addition, the value of the new list item on each iteration
4828 // of the associated loop(s) corresponds to the value of the original
4829 // list item before entering the construct plus the logical number of
4830 // the iteration times linear-step.
4831
Alexey Bataeved09d242014-05-28 05:53:51 +00004832 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00004833 // OpenMP [2.1, C/C++]
4834 // A list item is a variable name.
4835 // OpenMP [2.14.3.3, Restrictions, p.1]
4836 // A variable that is part of another variable (as an array or
4837 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00004838 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00004839 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00004840 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00004841 continue;
4842 }
4843
4844 VarDecl *VD = cast<VarDecl>(DE->getDecl());
4845
4846 // OpenMP [2.14.3.7, linear clause]
4847 // A list-item cannot appear in more than one linear clause.
4848 // A list-item that appears in a linear clause cannot appear in any
4849 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004850 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00004851 if (DVar.RefExpr) {
4852 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
4853 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004854 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00004855 continue;
4856 }
4857
4858 QualType QType = VD->getType();
4859 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
4860 // It will be analyzed later.
4861 Vars.push_back(DE);
4862 continue;
4863 }
4864
4865 // A variable must not have an incomplete type or a reference type.
4866 if (RequireCompleteType(ELoc, QType,
4867 diag::err_omp_linear_incomplete_type)) {
4868 continue;
4869 }
4870 if (QType->isReferenceType()) {
4871 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
4872 << getOpenMPClauseName(OMPC_linear) << QType;
4873 bool IsDecl =
4874 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4875 Diag(VD->getLocation(),
4876 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4877 << VD;
4878 continue;
4879 }
4880
4881 // A list item must not be const-qualified.
4882 if (QType.isConstant(Context)) {
4883 Diag(ELoc, diag::err_omp_const_variable)
4884 << getOpenMPClauseName(OMPC_linear);
4885 bool IsDecl =
4886 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4887 Diag(VD->getLocation(),
4888 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4889 << VD;
4890 continue;
4891 }
4892
4893 // A list item must be of integral or pointer type.
4894 QType = QType.getUnqualifiedType().getCanonicalType();
4895 const Type *Ty = QType.getTypePtrOrNull();
4896 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
4897 !Ty->isPointerType())) {
4898 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
4899 bool IsDecl =
4900 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4901 Diag(VD->getLocation(),
4902 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4903 << VD;
4904 continue;
4905 }
4906
4907 DSAStack->addDSA(VD, DE, OMPC_linear);
4908 Vars.push_back(DE);
4909 }
4910
4911 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004912 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00004913
4914 Expr *StepExpr = Step;
4915 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
4916 !Step->isInstantiationDependent() &&
4917 !Step->containsUnexpandedParameterPack()) {
4918 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004919 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00004920 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004921 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00004922 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00004923
4924 // Warn about zero linear step (it would be probably better specified as
4925 // making corresponding variables 'const').
4926 llvm::APSInt Result;
4927 if (StepExpr->isIntegerConstantExpr(Result, Context) &&
4928 !Result.isNegative() && !Result.isStrictlyPositive())
4929 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
4930 << (Vars.size() > 1);
4931 }
4932
4933 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc,
4934 Vars, StepExpr);
4935}
4936
Alexander Musmanf0d76e72014-05-29 14:36:25 +00004937OMPClause *Sema::ActOnOpenMPAlignedClause(
4938 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
4939 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
4940
4941 SmallVector<Expr *, 8> Vars;
4942 for (auto &RefExpr : VarList) {
4943 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
4944 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
4945 // It will be analyzed later.
4946 Vars.push_back(RefExpr);
4947 continue;
4948 }
4949
4950 SourceLocation ELoc = RefExpr->getExprLoc();
4951 // OpenMP [2.1, C/C++]
4952 // A list item is a variable name.
4953 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
4954 if (!DE || !isa<VarDecl>(DE->getDecl())) {
4955 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
4956 continue;
4957 }
4958
4959 VarDecl *VD = cast<VarDecl>(DE->getDecl());
4960
4961 // OpenMP [2.8.1, simd construct, Restrictions]
4962 // The type of list items appearing in the aligned clause must be
4963 // array, pointer, reference to array, or reference to pointer.
4964 QualType QType = DE->getType()
4965 .getNonReferenceType()
4966 .getUnqualifiedType()
4967 .getCanonicalType();
4968 const Type *Ty = QType.getTypePtrOrNull();
4969 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
4970 !Ty->isPointerType())) {
4971 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
4972 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
4973 bool IsDecl =
4974 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4975 Diag(VD->getLocation(),
4976 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4977 << VD;
4978 continue;
4979 }
4980
4981 // OpenMP [2.8.1, simd construct, Restrictions]
4982 // A list-item cannot appear in more than one aligned clause.
4983 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
4984 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
4985 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
4986 << getOpenMPClauseName(OMPC_aligned);
4987 continue;
4988 }
4989
4990 Vars.push_back(DE);
4991 }
4992
4993 // OpenMP [2.8.1, simd construct, Description]
4994 // The parameter of the aligned clause, alignment, must be a constant
4995 // positive integer expression.
4996 // If no optional parameter is specified, implementation-defined default
4997 // alignments for SIMD instructions on the target platforms are assumed.
4998 if (Alignment != nullptr) {
4999 ExprResult AlignResult =
5000 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
5001 if (AlignResult.isInvalid())
5002 return nullptr;
5003 Alignment = AlignResult.get();
5004 }
5005 if (Vars.empty())
5006 return nullptr;
5007
5008 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
5009 EndLoc, Vars, Alignment);
5010}
5011
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005012OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
5013 SourceLocation StartLoc,
5014 SourceLocation LParenLoc,
5015 SourceLocation EndLoc) {
5016 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00005017 for (auto &RefExpr : VarList) {
5018 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
5019 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005020 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00005021 Vars.push_back(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005022 continue;
5023 }
5024
Alexey Bataeved09d242014-05-28 05:53:51 +00005025 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005026 // OpenMP [2.1, C/C++]
5027 // A list item is a variable name.
5028 // OpenMP [2.14.4.1, Restrictions, p.1]
5029 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00005030 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005031 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005032 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005033 continue;
5034 }
5035
5036 Decl *D = DE->getDecl();
5037 VarDecl *VD = cast<VarDecl>(D);
5038
5039 QualType Type = VD->getType();
5040 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5041 // It will be analyzed later.
5042 Vars.push_back(DE);
5043 continue;
5044 }
5045
5046 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
5047 // A list item that appears in a copyin clause must be threadprivate.
5048 if (!DSAStack->isThreadPrivate(VD)) {
5049 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00005050 << getOpenMPClauseName(OMPC_copyin)
5051 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005052 continue;
5053 }
5054
5055 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
5056 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00005057 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005058 // operator for the class type.
5059 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005060 CXXRecordDecl *RD =
5061 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00005062 // FIXME This code must be replaced by actual assignment of the
5063 // threadprivate variable.
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005064 if (RD) {
5065 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
5066 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00005067 if (MD) {
5068 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
5069 MD->isDeleted()) {
5070 Diag(ELoc, diag::err_omp_required_method)
5071 << getOpenMPClauseName(OMPC_copyin) << 2;
5072 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
5073 VarDecl::DeclarationOnly;
5074 Diag(VD->getLocation(),
5075 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5076 << VD;
5077 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
5078 continue;
5079 }
5080 MarkFunctionReferenced(ELoc, MD);
5081 DiagnoseUseOfDecl(MD, ELoc);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005082 }
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005083 }
5084
5085 DSAStack->addDSA(VD, DE, OMPC_copyin);
5086 Vars.push_back(DE);
5087 }
5088
Alexey Bataeved09d242014-05-28 05:53:51 +00005089 if (Vars.empty())
5090 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005091
5092 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
5093}
5094
Alexey Bataevbae9a792014-06-27 10:37:06 +00005095OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
5096 SourceLocation StartLoc,
5097 SourceLocation LParenLoc,
5098 SourceLocation EndLoc) {
5099 SmallVector<Expr *, 8> Vars;
5100 for (auto &RefExpr : VarList) {
5101 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
5102 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
5103 // It will be analyzed later.
5104 Vars.push_back(RefExpr);
5105 continue;
5106 }
5107
5108 SourceLocation ELoc = RefExpr->getExprLoc();
5109 // OpenMP [2.1, C/C++]
5110 // A list item is a variable name.
5111 // OpenMP [2.14.4.1, Restrictions, p.1]
5112 // A list item that appears in a copyin clause must be threadprivate.
5113 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
5114 if (!DE || !isa<VarDecl>(DE->getDecl())) {
5115 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
5116 continue;
5117 }
5118
5119 Decl *D = DE->getDecl();
5120 VarDecl *VD = cast<VarDecl>(D);
5121
5122 QualType Type = VD->getType();
5123 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5124 // It will be analyzed later.
5125 Vars.push_back(DE);
5126 continue;
5127 }
5128
5129 // OpenMP [2.14.4.2, Restrictions, p.2]
5130 // A list item that appears in a copyprivate clause may not appear in a
5131 // private or firstprivate clause on the single construct.
5132 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005133 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00005134 if (DVar.CKind != OMPC_copyprivate && DVar.CKind != OMPC_unknown &&
5135 !(DVar.CKind == OMPC_private && !DVar.RefExpr)) {
5136 Diag(ELoc, diag::err_omp_wrong_dsa)
5137 << getOpenMPClauseName(DVar.CKind)
5138 << getOpenMPClauseName(OMPC_copyprivate);
5139 ReportOriginalDSA(*this, DSAStack, VD, DVar);
5140 continue;
5141 }
5142
5143 // OpenMP [2.11.4.2, Restrictions, p.1]
5144 // All list items that appear in a copyprivate clause must be either
5145 // threadprivate or private in the enclosing context.
5146 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005147 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00005148 if (DVar.CKind == OMPC_shared) {
5149 Diag(ELoc, diag::err_omp_required_access)
5150 << getOpenMPClauseName(OMPC_copyprivate)
5151 << "threadprivate or private in the enclosing context";
5152 ReportOriginalDSA(*this, DSAStack, VD, DVar);
5153 continue;
5154 }
5155 }
5156 }
5157
5158 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
5159 // A variable of class type (or array thereof) that appears in a
5160 // copyin clause requires an accessible, unambiguous copy assignment
5161 // operator for the class type.
5162 Type = Context.getBaseElementType(Type);
5163 CXXRecordDecl *RD =
5164 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
5165 // FIXME This code must be replaced by actual assignment of the
5166 // threadprivate variable.
5167 if (RD) {
5168 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
5169 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
5170 if (MD) {
5171 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
5172 MD->isDeleted()) {
5173 Diag(ELoc, diag::err_omp_required_method)
5174 << getOpenMPClauseName(OMPC_copyprivate) << 2;
5175 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
5176 VarDecl::DeclarationOnly;
5177 Diag(VD->getLocation(),
5178 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5179 << VD;
5180 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
5181 continue;
5182 }
5183 MarkFunctionReferenced(ELoc, MD);
5184 DiagnoseUseOfDecl(MD, ELoc);
5185 }
5186 }
5187
5188 // No need to mark vars as copyprivate, they are already threadprivate or
5189 // implicitly private.
5190 Vars.push_back(DE);
5191 }
5192
5193 if (Vars.empty())
5194 return nullptr;
5195
5196 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
5197}
5198
Alexey Bataev6125da92014-07-21 11:26:11 +00005199OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
5200 SourceLocation StartLoc,
5201 SourceLocation LParenLoc,
5202 SourceLocation EndLoc) {
5203 if (VarList.empty())
5204 return nullptr;
5205
5206 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
5207}
Alexey Bataevdea47612014-07-23 07:46:59 +00005208