blob: 9b66d2fd9f5f48da5325004fee240052bc7f24f3 [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;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000311 for (StackTy::reverse_iterator I = std::next(Iter), EE = Stack.rend();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000312 I != EE; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000313 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
314 // Referenced
Alexey Bataev758e55e2013-09-06 18:03:48 +0000315 // in a Construct, implicitly determined, p.6]
316 // In a task construct, if no default clause is present, a variable
317 // whose data-sharing attribute is not determined by the rules above is
318 // firstprivate.
319 DVarTemp = getDSA(I, D);
320 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000321 DVar.RefExpr = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000322 DVar.DKind = OMPD_task;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000323 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000324 return DVar;
325 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000326 if (isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000327 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000328 }
329 DVar.DKind = OMPD_task;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000330 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000331 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000332 return DVar;
333 }
334 }
335 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
336 // in a Construct, implicitly determined, p.3]
337 // For constructs other than task, if no default clause is present, these
338 // variables inherit their data-sharing attributes from the enclosing
339 // context.
Benjamin Kramer167e9992014-03-02 12:20:24 +0000340 return getDSA(std::next(Iter), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000341}
342
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000343DeclRefExpr *DSAStackTy::addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE) {
344 assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
345 auto It = Stack.back().AlignedMap.find(D);
346 if (It == Stack.back().AlignedMap.end()) {
347 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
348 Stack.back().AlignedMap[D] = NewDE;
349 return nullptr;
350 } else {
351 assert(It->second && "Unexpected nullptr expr in the aligned map");
352 return It->second;
353 }
354 return nullptr;
355}
356
Alexey Bataev758e55e2013-09-06 18:03:48 +0000357void DSAStackTy::addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A) {
358 if (A == OMPC_threadprivate) {
359 Stack[0].SharingMap[D].Attributes = A;
360 Stack[0].SharingMap[D].RefExpr = E;
361 } else {
362 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
363 Stack.back().SharingMap[D].Attributes = A;
364 Stack.back().SharingMap[D].RefExpr = E;
365 }
366}
367
Alexey Bataeved09d242014-05-28 05:53:51 +0000368bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000369 if (Stack.size() > 2) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000370 reverse_iterator I = Iter, E = std::prev(Stack.rend());
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000371 Scope *TopScope = nullptr;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000372 while (I != E && !isParallelOrTaskRegion(I->Directive)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000373 ++I;
374 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000375 if (I == E)
376 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000377 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000378 Scope *CurScope = getCurScope();
379 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000380 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000381 }
382 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000383 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000384 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000385}
386
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000387DSAStackTy::DSAVarData DSAStackTy::getTopDSA(VarDecl *D, bool FromParent) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000388 DSAVarData DVar;
389
390 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
391 // in a Construct, C/C++, predetermined, p.1]
392 // Variables appearing in threadprivate directives are threadprivate.
Alexey Bataev26a39242015-01-13 03:35:30 +0000393 if (D->getTLSKind() != VarDecl::TLS_None ||
394 D->getStorageClass() == SC_Register) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000395 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
Alexey Bataev24b04aa2015-01-16 07:11:33 +0000424 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
425 // in a Construct, C/C++, predetermined, p.4]
426 // Static data members are shared.
Alexey Bataev24b04aa2015-01-16 07:11:33 +0000427 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
428 // in a Construct, C/C++, predetermined, p.7]
429 // Variables with static storage duration that are declared in a scope
430 // inside the construct are shared.
Alexey Bataev42971a32015-01-20 07:03:46 +0000431 if (D->isStaticDataMember() || D->isStaticLocal()) {
432 DSAVarData DVarTemp =
433 hasDSA(D, isOpenMPPrivate, MatchesAlways(), FromParent);
434 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
435 return DVar;
436
Alexey Bataev24b04aa2015-01-16 07:11:33 +0000437 DVar.CKind = OMPC_shared;
438 return DVar;
439 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000440 }
441
442 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataev7ff55242014-06-19 09:13:45 +0000443 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
Alexey Bataev758e55e2013-09-06 18:03:48 +0000444 while (Type->isArrayType()) {
445 QualType ElemType = cast<ArrayType>(Type.getTypePtr())->getElementType();
446 Type = ElemType.getNonReferenceType().getCanonicalType();
447 }
448 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
449 // in a Construct, C/C++, predetermined, p.6]
450 // Variables with const qualified type having no mutable member are
451 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000452 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000453 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000454 if (IsConstant &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000455 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000456 // Variables with const-qualified type having no mutable member may be
457 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000458 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
459 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000460 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
461 return DVar;
462
Alexey Bataev758e55e2013-09-06 18:03:48 +0000463 DVar.CKind = OMPC_shared;
464 return DVar;
465 }
466
Alexey Bataev758e55e2013-09-06 18:03:48 +0000467 // Explicitly specified attributes and local variables with predetermined
468 // attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000469 auto I = std::prev(StartI);
470 if (I->SharingMap.count(D)) {
471 DVar.RefExpr = I->SharingMap[D].RefExpr;
472 DVar.CKind = I->SharingMap[D].Attributes;
473 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000474 }
475
476 return DVar;
477}
478
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000479DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D, bool FromParent) {
480 auto StartI = Stack.rbegin();
481 auto EndI = std::prev(Stack.rend());
482 if (FromParent && StartI != EndI) {
483 StartI = std::next(StartI);
484 }
485 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000486}
487
Alexey Bataevf29276e2014-06-18 04:14:57 +0000488template <class ClausesPredicate, class DirectivesPredicate>
489DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000490 DirectivesPredicate DPred,
491 bool FromParent) {
492 auto StartI = std::next(Stack.rbegin());
493 auto EndI = std::prev(Stack.rend());
494 if (FromParent && StartI != EndI) {
495 StartI = std::next(StartI);
496 }
497 for (auto I = StartI, EE = EndI; I != EE; ++I) {
498 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000499 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000500 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000501 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000502 return DVar;
503 }
504 return DSAVarData();
505}
506
Alexey Bataevf29276e2014-06-18 04:14:57 +0000507template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000508DSAStackTy::DSAVarData
509DSAStackTy::hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
510 DirectivesPredicate DPred, bool FromParent) {
511 auto StartI = std::next(Stack.rbegin());
512 auto EndI = std::prev(Stack.rend());
513 if (FromParent && StartI != EndI) {
514 StartI = std::next(StartI);
515 }
516 for (auto I = StartI, EE = EndI; I != EE; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000517 if (!DPred(I->Directive))
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000518 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000519 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000520 if (CPred(DVar.CKind))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000521 return DVar;
522 return DSAVarData();
523 }
524 return DSAVarData();
525}
526
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000527template <class NamedDirectivesPredicate>
528bool DSAStackTy::hasDirective(NamedDirectivesPredicate DPred, bool FromParent) {
529 auto StartI = std::next(Stack.rbegin());
530 auto EndI = std::prev(Stack.rend());
531 if (FromParent && StartI != EndI) {
532 StartI = std::next(StartI);
533 }
534 for (auto I = StartI, EE = EndI; I != EE; ++I) {
535 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
536 return true;
537 }
538 return false;
539}
540
Alexey Bataev758e55e2013-09-06 18:03:48 +0000541void Sema::InitDataSharingAttributesStack() {
542 VarDataSharingAttributesStack = new DSAStackTy(*this);
543}
544
545#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
546
Alexey Bataevf841bd92014-12-16 07:00:22 +0000547bool Sema::IsOpenMPCapturedVar(VarDecl *VD) {
548 assert(LangOpts.OpenMP && "OpenMP is not allowed");
549 if (DSAStack->getCurrentDirective() != OMPD_unknown) {
550 auto DVarPrivate = DSAStack->getTopDSA(VD, /*FromParent=*/false);
551 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
552 return true;
553 DVarPrivate = DSAStack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(),
554 /*FromParent=*/false);
555 return DVarPrivate.CKind != OMPC_unknown;
556 }
557 return false;
558}
559
Alexey Bataeved09d242014-05-28 05:53:51 +0000560void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000561
562void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
563 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000564 Scope *CurScope, SourceLocation Loc) {
565 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000566 PushExpressionEvaluationContext(PotentiallyEvaluated);
567}
568
569void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000570 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
571 // A variable of class type (or array thereof) that appears in a lastprivate
572 // clause requires an accessible, unambiguous default constructor for the
573 // class type, unless the list item is also specified in a firstprivate
574 // clause.
575 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
576 for (auto C : D->clauses()) {
577 if (auto Clause = dyn_cast<OMPLastprivateClause>(C)) {
578 for (auto VarRef : Clause->varlists()) {
579 if (VarRef->isValueDependent() || VarRef->isTypeDependent())
580 continue;
581 auto VD = cast<VarDecl>(cast<DeclRefExpr>(VarRef)->getDecl());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000582 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000583 if (DVar.CKind == OMPC_lastprivate) {
584 SourceLocation ELoc = VarRef->getExprLoc();
585 auto Type = VarRef->getType();
586 if (Type->isArrayType())
587 Type = QualType(Type->getArrayElementTypeNoTypeQual(), 0);
588 CXXRecordDecl *RD =
Alexey Bataev23b69422014-06-18 07:08:49 +0000589 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
590 // FIXME This code must be replaced by actual constructing of the
591 // lastprivate variable.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000592 if (RD) {
593 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
594 PartialDiagnostic PD =
595 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
596 if (!CD ||
597 CheckConstructorAccess(
598 ELoc, CD, InitializedEntity::InitializeTemporary(Type),
599 CD->getAccess(), PD) == AR_inaccessible ||
600 CD->isDeleted()) {
601 Diag(ELoc, diag::err_omp_required_method)
602 << getOpenMPClauseName(OMPC_lastprivate) << 0;
603 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
604 VarDecl::DeclarationOnly;
605 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl
606 : diag::note_defined_here)
607 << VD;
608 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
609 continue;
610 }
611 MarkFunctionReferenced(ELoc, CD);
612 DiagnoseUseOfDecl(CD, ELoc);
613 }
614 }
615 }
616 }
617 }
618 }
619
Alexey Bataev758e55e2013-09-06 18:03:48 +0000620 DSAStack->pop();
621 DiscardCleanupsInEvaluationContext();
622 PopExpressionEvaluationContext();
623}
624
Alexey Bataeva769e072013-03-22 06:34:35 +0000625namespace {
626
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000627class VarDeclFilterCCC : public CorrectionCandidateCallback {
628private:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000629 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +0000630
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000631public:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000632 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +0000633 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000634 NamedDecl *ND = Candidate.getCorrectionDecl();
635 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
636 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000637 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
638 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +0000639 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000640 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000641 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000642};
Alexey Bataeved09d242014-05-28 05:53:51 +0000643} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000644
645ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
646 CXXScopeSpec &ScopeSpec,
647 const DeclarationNameInfo &Id) {
648 LookupResult Lookup(*this, Id, LookupOrdinaryName);
649 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
650
651 if (Lookup.isAmbiguous())
652 return ExprError();
653
654 VarDecl *VD;
655 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +0000656 if (TypoCorrection Corrected = CorrectTypo(
657 Id, LookupOrdinaryName, CurScope, nullptr,
658 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +0000659 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000660 PDiag(Lookup.empty()
661 ? diag::err_undeclared_var_use_suggest
662 : diag::err_omp_expected_var_arg_suggest)
663 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +0000664 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000665 } else {
Richard Smithf9b15102013-08-17 00:46:16 +0000666 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
667 : diag::err_omp_expected_var_arg)
668 << Id.getName();
669 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000670 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000671 } else {
672 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000673 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000674 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
675 return ExprError();
676 }
677 }
678 Lookup.suppressDiagnostics();
679
680 // OpenMP [2.9.2, Syntax, C/C++]
681 // Variables must be file-scope, namespace-scope, or static block-scope.
682 if (!VD->hasGlobalStorage()) {
683 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000684 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
685 bool IsDecl =
686 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000687 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +0000688 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
689 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000690 return ExprError();
691 }
692
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000693 VarDecl *CanonicalVD = VD->getCanonicalDecl();
694 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000695 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
696 // A threadprivate directive for file-scope variables must appear outside
697 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000698 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
699 !getCurLexicalContext()->isTranslationUnit()) {
700 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000701 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
702 bool IsDecl =
703 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
704 Diag(VD->getLocation(),
705 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
706 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000707 return ExprError();
708 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000709 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
710 // A threadprivate directive for static class member variables must appear
711 // in the class definition, in the same scope in which the member
712 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000713 if (CanonicalVD->isStaticDataMember() &&
714 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
715 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000716 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
717 bool IsDecl =
718 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
719 Diag(VD->getLocation(),
720 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
721 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000722 return ExprError();
723 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000724 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
725 // A threadprivate directive for namespace-scope variables must appear
726 // outside any definition or declaration other than the namespace
727 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000728 if (CanonicalVD->getDeclContext()->isNamespace() &&
729 (!getCurLexicalContext()->isFileContext() ||
730 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
731 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000732 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
733 bool IsDecl =
734 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
735 Diag(VD->getLocation(),
736 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
737 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000738 return ExprError();
739 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000740 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
741 // A threadprivate directive for static block-scope variables must appear
742 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000743 if (CanonicalVD->isStaticLocal() && CurScope &&
744 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000745 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000746 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
747 bool IsDecl =
748 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
749 Diag(VD->getLocation(),
750 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
751 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000752 return ExprError();
753 }
754
755 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
756 // A threadprivate directive must lexically precede all references to any
757 // of the variables in its list.
758 if (VD->isUsed()) {
759 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +0000760 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000761 return ExprError();
762 }
763
764 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataevd178ad42014-03-07 08:03:37 +0000765 ExprResult DE = BuildDeclRefExpr(VD, ExprType, VK_LValue, Id.getLoc());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000766 return DE;
767}
768
Alexey Bataeved09d242014-05-28 05:53:51 +0000769Sema::DeclGroupPtrTy
770Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
771 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000772 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000773 CurContext->addDecl(D);
774 return DeclGroupPtrTy::make(DeclGroupRef(D));
775 }
776 return DeclGroupPtrTy();
777}
778
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000779namespace {
780class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
781 Sema &SemaRef;
782
783public:
784 bool VisitDeclRefExpr(const DeclRefExpr *E) {
785 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
786 if (VD->hasLocalStorage()) {
787 SemaRef.Diag(E->getLocStart(),
788 diag::err_omp_local_var_in_threadprivate_init)
789 << E->getSourceRange();
790 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
791 << VD << VD->getSourceRange();
792 return true;
793 }
794 }
795 return false;
796 }
797 bool VisitStmt(const Stmt *S) {
798 for (auto Child : S->children()) {
799 if (Child && Visit(Child))
800 return true;
801 }
802 return false;
803 }
Alexey Bataev23b69422014-06-18 07:08:49 +0000804 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000805};
806} // namespace
807
Alexey Bataeved09d242014-05-28 05:53:51 +0000808OMPThreadPrivateDecl *
809Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000810 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +0000811 for (auto &RefExpr : VarList) {
812 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000813 VarDecl *VD = cast<VarDecl>(DE->getDecl());
814 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +0000815
816 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
817 // A threadprivate variable must not have an incomplete type.
818 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000819 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000820 continue;
821 }
822
823 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
824 // A threadprivate variable must not have a reference type.
825 if (VD->getType()->isReferenceType()) {
826 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000827 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
828 bool IsDecl =
829 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
830 Diag(VD->getLocation(),
831 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
832 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000833 continue;
834 }
835
Richard Smithfd3834f2013-04-13 02:43:54 +0000836 // Check if this is a TLS variable.
Alexey Bataev26a39242015-01-13 03:35:30 +0000837 if (VD->getTLSKind() != VarDecl::TLS_None ||
838 VD->getStorageClass() == SC_Register) {
839 Diag(ILoc, diag::err_omp_var_thread_local)
840 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +0000841 bool IsDecl =
842 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
843 Diag(VD->getLocation(),
844 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
845 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000846 continue;
847 }
848
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000849 // Check if initial value of threadprivate variable reference variable with
850 // local storage (it is not supported by runtime).
851 if (auto Init = VD->getAnyInitializer()) {
852 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000853 if (Checker.Visit(Init))
854 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000855 }
856
Alexey Bataeved09d242014-05-28 05:53:51 +0000857 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +0000858 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +0000859 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
860 Context, SourceRange(Loc, Loc)));
861 if (auto *ML = Context.getASTMutationListener())
862 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +0000863 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000864 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000865 if (!Vars.empty()) {
866 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
867 Vars);
868 D->setAccess(AS_public);
869 }
870 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +0000871}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000872
Alexey Bataev7ff55242014-06-19 09:13:45 +0000873static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
874 const VarDecl *VD, DSAStackTy::DSAVarData DVar,
875 bool IsLoopIterVar = false) {
876 if (DVar.RefExpr) {
877 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
878 << getOpenMPClauseName(DVar.CKind);
879 return;
880 }
881 enum {
882 PDSA_StaticMemberShared,
883 PDSA_StaticLocalVarShared,
884 PDSA_LoopIterVarPrivate,
885 PDSA_LoopIterVarLinear,
886 PDSA_LoopIterVarLastprivate,
887 PDSA_ConstVarShared,
888 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000889 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000890 PDSA_LocalVarPrivate,
891 PDSA_Implicit
892 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000893 bool ReportHint = false;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000894 auto ReportLoc = VD->getLocation();
Alexey Bataev7ff55242014-06-19 09:13:45 +0000895 if (IsLoopIterVar) {
896 if (DVar.CKind == OMPC_private)
897 Reason = PDSA_LoopIterVarPrivate;
898 else if (DVar.CKind == OMPC_lastprivate)
899 Reason = PDSA_LoopIterVarLastprivate;
900 else
901 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000902 } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) {
903 Reason = PDSA_TaskVarFirstprivate;
904 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000905 } else if (VD->isStaticLocal())
906 Reason = PDSA_StaticLocalVarShared;
907 else if (VD->isStaticDataMember())
908 Reason = PDSA_StaticMemberShared;
909 else if (VD->isFileVarDecl())
910 Reason = PDSA_GlobalVarShared;
911 else if (VD->getType().isConstant(SemaRef.getASTContext()))
912 Reason = PDSA_ConstVarShared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000913 else if (VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +0000914 ReportHint = true;
915 Reason = PDSA_LocalVarPrivate;
916 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000917 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000918 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +0000919 << Reason << ReportHint
920 << getOpenMPDirectiveName(Stack->getCurrentDirective());
921 } else if (DVar.ImplicitDSALoc.isValid()) {
922 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
923 << getOpenMPClauseName(DVar.CKind);
924 }
Alexey Bataev7ff55242014-06-19 09:13:45 +0000925}
926
Alexey Bataev758e55e2013-09-06 18:03:48 +0000927namespace {
928class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
929 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000930 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000931 bool ErrorFound;
932 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000933 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000934 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +0000935
Alexey Bataev758e55e2013-09-06 18:03:48 +0000936public:
937 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000938 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000939 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +0000940 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
941 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000942
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000943 auto DVar = Stack->getTopDSA(VD, false);
944 // Check if the variable has explicit DSA set and stop analysis if it so.
945 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000946
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000947 auto ELoc = E->getExprLoc();
948 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000949 // The default(none) clause requires that each variable that is referenced
950 // in the construct, and does not have a predetermined data-sharing
951 // attribute, must have its data-sharing attribute explicitly determined
952 // by being listed in a data-sharing attribute clause.
953 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000954 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +0000955 VarsWithInheritedDSA.count(VD) == 0) {
956 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000957 return;
958 }
959
960 // OpenMP [2.9.3.6, Restrictions, p.2]
961 // A list item that appears in a reduction clause of the innermost
962 // enclosing worksharing or parallel construct may not be accessed in an
963 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000964 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000965 [](OpenMPDirectiveKind K) -> bool {
966 return isOpenMPParallelDirective(K) ||
Alexey Bataev13314bf2014-10-09 04:18:56 +0000967 isOpenMPWorksharingDirective(K) ||
968 isOpenMPTeamsDirective(K);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000969 },
970 false);
Alexey Bataevc5e02582014-06-16 07:08:35 +0000971 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
972 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000973 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
974 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +0000975 return;
976 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000977
978 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000979 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000980 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000981 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000982 }
983 }
984 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000985 for (auto *C : S->clauses()) {
986 // Skip analysis of arguments of implicitly defined firstprivate clause
987 // for task directives.
988 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
989 for (auto *CC : C->children()) {
990 if (CC)
991 Visit(CC);
992 }
993 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000994 }
995 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000996 for (auto *C : S->children()) {
997 if (C && !isa<OMPExecutableDirective>(C))
998 Visit(C);
999 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001000 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001001
1002 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001003 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001004 llvm::DenseMap<VarDecl *, Expr *> &getVarsWithInheritedDSA() {
1005 return VarsWithInheritedDSA;
1006 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001007
Alexey Bataev7ff55242014-06-19 09:13:45 +00001008 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1009 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00001010};
Alexey Bataeved09d242014-05-28 05:53:51 +00001011} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001012
Alexey Bataevbae9a792014-06-27 10:37:06 +00001013void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001014 switch (DKind) {
1015 case OMPD_parallel: {
1016 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1017 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001018 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001019 std::make_pair(".global_tid.", KmpInt32PtrTy),
1020 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1021 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001022 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001023 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1024 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001025 break;
1026 }
1027 case OMPD_simd: {
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
1030 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001031 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1032 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001033 break;
1034 }
1035 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001036 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001037 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001038 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001039 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1040 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001041 break;
1042 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001043 case OMPD_for_simd: {
1044 Sema::CapturedParamNameType Params[] = {
1045 std::make_pair(StringRef(), QualType()) // __context with shared vars
1046 };
1047 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1048 Params);
1049 break;
1050 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001051 case OMPD_sections: {
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 Bataevd3f8dd22014-06-25 11:44:49 +00001057 break;
1058 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001059 case OMPD_section: {
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 Bataev1e0498a2014-06-26 08:21:58 +00001065 break;
1066 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001067 case OMPD_single: {
1068 Sema::CapturedParamNameType Params[] = {
1069 std::make_pair(StringRef(), QualType()) // __context with shared vars
1070 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001071 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1072 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001073 break;
1074 }
Alexander Musman80c22892014-07-17 08:54:58 +00001075 case OMPD_master: {
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 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001083 case OMPD_critical: {
1084 Sema::CapturedParamNameType Params[] = {
1085 std::make_pair(StringRef(), QualType()) // __context with shared vars
1086 };
1087 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1088 Params);
1089 break;
1090 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001091 case OMPD_parallel_for: {
1092 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1093 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1094 Sema::CapturedParamNameType Params[] = {
1095 std::make_pair(".global_tid.", KmpInt32PtrTy),
1096 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1097 std::make_pair(StringRef(), QualType()) // __context with shared vars
1098 };
1099 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1100 Params);
1101 break;
1102 }
Alexander Musmane4e893b2014-09-23 09:33:00 +00001103 case OMPD_parallel_for_simd: {
1104 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1105 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1106 Sema::CapturedParamNameType Params[] = {
1107 std::make_pair(".global_tid.", KmpInt32PtrTy),
1108 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1109 std::make_pair(StringRef(), QualType()) // __context with shared vars
1110 };
1111 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1112 Params);
1113 break;
1114 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001115 case OMPD_parallel_sections: {
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 Bataev9c2e8ee2014-07-11 11:25:16 +00001123 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001124 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001125 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001126 std::make_pair(".global_tid.", KmpInt32Ty),
1127 std::make_pair(".part_id.", KmpInt32Ty),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001128 std::make_pair(StringRef(), QualType()) // __context with shared vars
1129 };
1130 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1131 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001132 // Mark this captured region as inlined, because we don't use outlined
1133 // function directly.
1134 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1135 AlwaysInlineAttr::CreateImplicit(
1136 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001137 break;
1138 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001139 case OMPD_ordered: {
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 Bataev0162e452014-07-22 10:10:35 +00001147 case OMPD_atomic: {
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 Bataev0bd520b2014-09-19 08:19:49 +00001155 case OMPD_target: {
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 Bataev13314bf2014-10-09 04:18:56 +00001163 case OMPD_teams: {
1164 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1165 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1166 Sema::CapturedParamNameType Params[] = {
1167 std::make_pair(".global_tid.", KmpInt32PtrTy),
1168 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1169 std::make_pair(StringRef(), QualType()) // __context with shared vars
1170 };
1171 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1172 Params);
1173 break;
1174 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001175 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001176 case OMPD_taskyield:
1177 case OMPD_barrier:
1178 case OMPD_taskwait:
1179 case OMPD_flush:
Alexey Bataev9959db52014-05-06 10:08:46 +00001180 llvm_unreachable("OpenMP Directive is not allowed");
1181 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001182 llvm_unreachable("Unknown OpenMP directive");
1183 }
1184}
1185
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001186static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1187 OpenMPDirectiveKind CurrentRegion,
1188 const DeclarationNameInfo &CurrentName,
1189 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001190 // Allowed nesting of constructs
1191 // +------------------+-----------------+------------------------------------+
1192 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1193 // +------------------+-----------------+------------------------------------+
1194 // | parallel | parallel | * |
1195 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001196 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001197 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001198 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001199 // | parallel | simd | * |
1200 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001201 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001202 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001203 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001204 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001205 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001206 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001207 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001208 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001209 // | parallel | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001210 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001211 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001212 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001213 // | parallel | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001214 // | parallel | teams | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001215 // +------------------+-----------------+------------------------------------+
1216 // | for | parallel | * |
1217 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001218 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001219 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001220 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001221 // | for | simd | * |
1222 // | for | sections | + |
1223 // | for | section | + |
1224 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001225 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001226 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001227 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001228 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001229 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001230 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001231 // | for | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001232 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001233 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001234 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001235 // | for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001236 // | for | teams | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001237 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001238 // | master | parallel | * |
1239 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001240 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001241 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001242 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001243 // | master | simd | * |
1244 // | master | sections | + |
1245 // | master | section | + |
1246 // | master | single | + |
1247 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001248 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001249 // | master |parallel sections| * |
1250 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001251 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001252 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001253 // | master | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001254 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001255 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001256 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001257 // | master | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001258 // | master | teams | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001259 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001260 // | critical | parallel | * |
1261 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001262 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001263 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001264 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001265 // | critical | simd | * |
1266 // | critical | sections | + |
1267 // | critical | section | + |
1268 // | critical | single | + |
1269 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001270 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001271 // | critical |parallel sections| * |
1272 // | critical | task | * |
1273 // | critical | taskyield | * |
1274 // | critical | barrier | + |
1275 // | critical | taskwait | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001276 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001277 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001278 // | critical | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001279 // | critical | teams | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001280 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001281 // | simd | parallel | |
1282 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001283 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00001284 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001285 // | simd | critical | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001286 // | simd | simd | |
1287 // | simd | sections | |
1288 // | simd | section | |
1289 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001290 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001291 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001292 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001293 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00001294 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001295 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001296 // | simd | taskwait | |
Alexey Bataev6125da92014-07-21 11:26:11 +00001297 // | simd | flush | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001298 // | simd | ordered | |
Alexey Bataev0162e452014-07-22 10:10:35 +00001299 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001300 // | simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001301 // | simd | teams | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001302 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00001303 // | for simd | parallel | |
1304 // | for simd | for | |
1305 // | for simd | for simd | |
1306 // | for simd | master | |
1307 // | for simd | critical | |
1308 // | for simd | simd | |
1309 // | for simd | sections | |
1310 // | for simd | section | |
1311 // | for simd | single | |
1312 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001313 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001314 // | for simd |parallel sections| |
1315 // | for simd | task | |
1316 // | for simd | taskyield | |
1317 // | for simd | barrier | |
1318 // | for simd | taskwait | |
1319 // | for simd | flush | |
1320 // | for simd | ordered | |
1321 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001322 // | for simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001323 // | for simd | teams | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001324 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00001325 // | parallel for simd| parallel | |
1326 // | parallel for simd| for | |
1327 // | parallel for simd| for simd | |
1328 // | parallel for simd| master | |
1329 // | parallel for simd| critical | |
1330 // | parallel for simd| simd | |
1331 // | parallel for simd| sections | |
1332 // | parallel for simd| section | |
1333 // | parallel for simd| single | |
1334 // | parallel for simd| parallel for | |
1335 // | parallel for simd|parallel for simd| |
1336 // | parallel for simd|parallel sections| |
1337 // | parallel for simd| task | |
1338 // | parallel for simd| taskyield | |
1339 // | parallel for simd| barrier | |
1340 // | parallel for simd| taskwait | |
1341 // | parallel for simd| flush | |
1342 // | parallel for simd| ordered | |
1343 // | parallel for simd| atomic | |
1344 // | parallel for simd| target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001345 // | parallel for simd| teams | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001346 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001347 // | sections | parallel | * |
1348 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001349 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001350 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001351 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001352 // | sections | simd | * |
1353 // | sections | sections | + |
1354 // | sections | section | * |
1355 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001356 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001357 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001358 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001359 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001360 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001361 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001362 // | sections | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001363 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001364 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001365 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001366 // | sections | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001367 // | sections | teams | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001368 // +------------------+-----------------+------------------------------------+
1369 // | section | parallel | * |
1370 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001371 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001372 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001373 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001374 // | section | simd | * |
1375 // | section | sections | + |
1376 // | section | section | + |
1377 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001378 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001379 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001380 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001381 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001382 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001383 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001384 // | section | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001385 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001386 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001387 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001388 // | section | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001389 // | section | teams | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001390 // +------------------+-----------------+------------------------------------+
1391 // | single | parallel | * |
1392 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001393 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001394 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001395 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001396 // | single | simd | * |
1397 // | single | sections | + |
1398 // | single | section | + |
1399 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001400 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001401 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001402 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001403 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001404 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001405 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001406 // | single | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001407 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001408 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001409 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001410 // | single | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001411 // | single | teams | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001412 // +------------------+-----------------+------------------------------------+
1413 // | parallel for | parallel | * |
1414 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001415 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001416 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001417 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001418 // | parallel for | simd | * |
1419 // | parallel for | sections | + |
1420 // | parallel for | section | + |
1421 // | parallel for | single | + |
1422 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001423 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001424 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001425 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001426 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001427 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001428 // | parallel for | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001429 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001430 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001431 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001432 // | parallel for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001433 // | parallel for | teams | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001434 // +------------------+-----------------+------------------------------------+
1435 // | parallel sections| parallel | * |
1436 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001437 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001438 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001439 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001440 // | parallel sections| simd | * |
1441 // | parallel sections| sections | + |
1442 // | parallel sections| section | * |
1443 // | parallel sections| single | + |
1444 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001445 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001446 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001447 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001448 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001449 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001450 // | parallel sections| taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001451 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001452 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001453 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001454 // | parallel sections| target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001455 // | parallel sections| teams | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001456 // +------------------+-----------------+------------------------------------+
1457 // | task | parallel | * |
1458 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001459 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001460 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001461 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001462 // | task | simd | * |
1463 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001464 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001465 // | task | single | + |
1466 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001467 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001468 // | task |parallel sections| * |
1469 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001470 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001471 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001472 // | task | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001473 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001474 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001475 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001476 // | task | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001477 // | task | teams | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001478 // +------------------+-----------------+------------------------------------+
1479 // | ordered | parallel | * |
1480 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001481 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001482 // | ordered | master | * |
1483 // | ordered | critical | * |
1484 // | ordered | simd | * |
1485 // | ordered | sections | + |
1486 // | ordered | section | + |
1487 // | ordered | single | + |
1488 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001489 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001490 // | ordered |parallel sections| * |
1491 // | ordered | task | * |
1492 // | ordered | taskyield | * |
1493 // | ordered | barrier | + |
1494 // | ordered | taskwait | * |
1495 // | ordered | flush | * |
1496 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001497 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001498 // | ordered | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001499 // | ordered | teams | + |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001500 // +------------------+-----------------+------------------------------------+
1501 // | atomic | parallel | |
1502 // | atomic | for | |
1503 // | atomic | for simd | |
1504 // | atomic | master | |
1505 // | atomic | critical | |
1506 // | atomic | simd | |
1507 // | atomic | sections | |
1508 // | atomic | section | |
1509 // | atomic | single | |
1510 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001511 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001512 // | atomic |parallel sections| |
1513 // | atomic | task | |
1514 // | atomic | taskyield | |
1515 // | atomic | barrier | |
1516 // | atomic | taskwait | |
1517 // | atomic | flush | |
1518 // | atomic | ordered | |
1519 // | atomic | atomic | |
1520 // | atomic | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001521 // | atomic | teams | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001522 // +------------------+-----------------+------------------------------------+
1523 // | target | parallel | * |
1524 // | target | for | * |
1525 // | target | for simd | * |
1526 // | target | master | * |
1527 // | target | critical | * |
1528 // | target | simd | * |
1529 // | target | sections | * |
1530 // | target | section | * |
1531 // | target | single | * |
1532 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001533 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001534 // | target |parallel sections| * |
1535 // | target | task | * |
1536 // | target | taskyield | * |
1537 // | target | barrier | * |
1538 // | target | taskwait | * |
1539 // | target | flush | * |
1540 // | target | ordered | * |
1541 // | target | atomic | * |
1542 // | target | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001543 // | target | teams | * |
1544 // +------------------+-----------------+------------------------------------+
1545 // | teams | parallel | * |
1546 // | teams | for | + |
1547 // | teams | for simd | + |
1548 // | teams | master | + |
1549 // | teams | critical | + |
1550 // | teams | simd | + |
1551 // | teams | sections | + |
1552 // | teams | section | + |
1553 // | teams | single | + |
1554 // | teams | parallel for | * |
1555 // | teams |parallel for simd| * |
1556 // | teams |parallel sections| * |
1557 // | teams | task | + |
1558 // | teams | taskyield | + |
1559 // | teams | barrier | + |
1560 // | teams | taskwait | + |
1561 // | teams | flush | + |
1562 // | teams | ordered | + |
1563 // | teams | atomic | + |
1564 // | teams | target | + |
1565 // | teams | teams | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001566 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00001567 if (Stack->getCurScope()) {
1568 auto ParentRegion = Stack->getParentDirective();
1569 bool NestingProhibited = false;
1570 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001571 enum {
1572 NoRecommend,
1573 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00001574 ShouldBeInOrderedRegion,
1575 ShouldBeInTargetRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001576 } Recommend = NoRecommend;
Alexey Bataev549210e2014-06-24 04:39:47 +00001577 if (isOpenMPSimdDirective(ParentRegion)) {
1578 // OpenMP [2.16, Nesting of Regions]
1579 // OpenMP constructs may not be nested inside a simd region.
1580 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
1581 return true;
1582 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001583 if (ParentRegion == OMPD_atomic) {
1584 // OpenMP [2.16, Nesting of Regions]
1585 // OpenMP constructs may not be nested inside an atomic region.
1586 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
1587 return true;
1588 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001589 if (CurrentRegion == OMPD_section) {
1590 // OpenMP [2.7.2, sections Construct, Restrictions]
1591 // Orphaned section directives are prohibited. That is, the section
1592 // directives must appear within the sections construct and must not be
1593 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001594 if (ParentRegion != OMPD_sections &&
1595 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001596 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
1597 << (ParentRegion != OMPD_unknown)
1598 << getOpenMPDirectiveName(ParentRegion);
1599 return true;
1600 }
1601 return false;
1602 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001603 // Allow some constructs to be orphaned (they could be used in functions,
1604 // called from OpenMP regions with the required preconditions).
1605 if (ParentRegion == OMPD_unknown)
1606 return false;
Alexander Musman80c22892014-07-17 08:54:58 +00001607 if (CurrentRegion == OMPD_master) {
1608 // OpenMP [2.16, Nesting of Regions]
1609 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00001610 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00001611 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
1612 ParentRegion == OMPD_task;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001613 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
1614 // OpenMP [2.16, Nesting of Regions]
1615 // A critical region may not be nested (closely or otherwise) inside a
1616 // critical region with the same name. Note that this restriction is not
1617 // sufficient to prevent deadlock.
1618 SourceLocation PreviousCriticalLoc;
1619 bool DeadLock =
1620 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
1621 OpenMPDirectiveKind K,
1622 const DeclarationNameInfo &DNI,
1623 SourceLocation Loc)
1624 ->bool {
1625 if (K == OMPD_critical &&
1626 DNI.getName() == CurrentName.getName()) {
1627 PreviousCriticalLoc = Loc;
1628 return true;
1629 } else
1630 return false;
1631 },
1632 false /* skip top directive */);
1633 if (DeadLock) {
1634 SemaRef.Diag(StartLoc,
1635 diag::err_omp_prohibited_region_critical_same_name)
1636 << CurrentName.getName();
1637 if (PreviousCriticalLoc.isValid())
1638 SemaRef.Diag(PreviousCriticalLoc,
1639 diag::note_omp_previous_critical_region);
1640 return true;
1641 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001642 } else if (CurrentRegion == OMPD_barrier) {
1643 // OpenMP [2.16, Nesting of Regions]
1644 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00001645 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001646 NestingProhibited =
1647 isOpenMPWorksharingDirective(ParentRegion) ||
1648 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1649 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00001650 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00001651 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00001652 // OpenMP [2.16, Nesting of Regions]
1653 // A worksharing region may not be closely nested inside a worksharing,
1654 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001655 NestingProhibited =
Alexander Musmanf82886e2014-09-18 05:12:34 +00001656 isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001657 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1658 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
1659 Recommend = ShouldBeInParallelRegion;
1660 } else if (CurrentRegion == OMPD_ordered) {
1661 // OpenMP [2.16, Nesting of Regions]
1662 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00001663 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001664 // An ordered region must be closely nested inside a loop region (or
1665 // parallel loop region) with an ordered clause.
1666 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00001667 ParentRegion == OMPD_task ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001668 !Stack->isParentOrderedRegion();
1669 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00001670 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
1671 // OpenMP [2.16, Nesting of Regions]
1672 // If specified, a teams construct must be contained within a target
1673 // construct.
1674 NestingProhibited = ParentRegion != OMPD_target;
1675 Recommend = ShouldBeInTargetRegion;
1676 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
1677 }
1678 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
1679 // OpenMP [2.16, Nesting of Regions]
1680 // distribute, parallel, parallel sections, parallel workshare, and the
1681 // parallel loop and parallel loop SIMD constructs are the only OpenMP
1682 // constructs that can be closely nested in the teams region.
1683 // TODO: add distribute directive.
1684 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion);
1685 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00001686 }
1687 if (NestingProhibited) {
1688 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001689 << CloseNesting << getOpenMPDirectiveName(ParentRegion) << Recommend
1690 << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00001691 return true;
1692 }
1693 }
1694 return false;
1695}
1696
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001697StmtResult Sema::ActOnOpenMPExecutableDirective(OpenMPDirectiveKind Kind,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001698 const DeclarationNameInfo &DirName,
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001699 ArrayRef<OMPClause *> Clauses,
1700 Stmt *AStmt,
1701 SourceLocation StartLoc,
1702 SourceLocation EndLoc) {
1703 StmtResult Res = StmtError();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001704 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00001705 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001706
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001707 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev68446b72014-07-18 07:47:19 +00001708 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001709 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00001710 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00001711 if (AStmt) {
1712 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
1713
1714 // Check default data sharing attributes for referenced variables.
1715 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
1716 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
1717 if (DSAChecker.isErrorFound())
1718 return StmtError();
1719 // Generate list of implicitly defined firstprivate variables.
1720 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00001721
1722 if (!DSAChecker.getImplicitFirstprivate().empty()) {
1723 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
1724 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
1725 SourceLocation(), SourceLocation())) {
1726 ClausesWithImplicit.push_back(Implicit);
1727 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
1728 DSAChecker.getImplicitFirstprivate().size();
1729 } else
1730 ErrorFound = true;
1731 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001732 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001733
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001734 switch (Kind) {
1735 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00001736 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
1737 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001738 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001739 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001740 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
1741 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001742 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00001743 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001744 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
1745 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001746 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00001747 case OMPD_for_simd:
1748 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
1749 EndLoc, VarsWithInheritedDSA);
1750 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001751 case OMPD_sections:
1752 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
1753 EndLoc);
1754 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001755 case OMPD_section:
1756 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00001757 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001758 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
1759 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001760 case OMPD_single:
1761 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
1762 EndLoc);
1763 break;
Alexander Musman80c22892014-07-17 08:54:58 +00001764 case OMPD_master:
1765 assert(ClausesWithImplicit.empty() &&
1766 "No clauses are allowed for 'omp master' directive");
1767 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
1768 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001769 case OMPD_critical:
1770 assert(ClausesWithImplicit.empty() &&
1771 "No clauses are allowed for 'omp critical' directive");
1772 Res = ActOnOpenMPCriticalDirective(DirName, AStmt, StartLoc, EndLoc);
1773 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00001774 case OMPD_parallel_for:
1775 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
1776 EndLoc, VarsWithInheritedDSA);
1777 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00001778 case OMPD_parallel_for_simd:
1779 Res = ActOnOpenMPParallelForSimdDirective(
1780 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
1781 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001782 case OMPD_parallel_sections:
1783 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
1784 StartLoc, EndLoc);
1785 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001786 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001787 Res =
1788 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
1789 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00001790 case OMPD_taskyield:
1791 assert(ClausesWithImplicit.empty() &&
1792 "No clauses are allowed for 'omp taskyield' directive");
1793 assert(AStmt == nullptr &&
1794 "No associated statement allowed for 'omp taskyield' directive");
1795 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
1796 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001797 case OMPD_barrier:
1798 assert(ClausesWithImplicit.empty() &&
1799 "No clauses are allowed for 'omp barrier' directive");
1800 assert(AStmt == nullptr &&
1801 "No associated statement allowed for 'omp barrier' directive");
1802 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
1803 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00001804 case OMPD_taskwait:
1805 assert(ClausesWithImplicit.empty() &&
1806 "No clauses are allowed for 'omp taskwait' directive");
1807 assert(AStmt == nullptr &&
1808 "No associated statement allowed for 'omp taskwait' directive");
1809 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
1810 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00001811 case OMPD_flush:
1812 assert(AStmt == nullptr &&
1813 "No associated statement allowed for 'omp flush' directive");
1814 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
1815 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001816 case OMPD_ordered:
1817 assert(ClausesWithImplicit.empty() &&
1818 "No clauses are allowed for 'omp ordered' directive");
1819 Res = ActOnOpenMPOrderedDirective(AStmt, StartLoc, EndLoc);
1820 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00001821 case OMPD_atomic:
1822 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
1823 EndLoc);
1824 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00001825 case OMPD_teams:
1826 Res =
1827 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
1828 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001829 case OMPD_target:
1830 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
1831 EndLoc);
1832 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001833 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001834 llvm_unreachable("OpenMP Directive is not allowed");
1835 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001836 llvm_unreachable("Unknown OpenMP directive");
1837 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001838
Alexey Bataev4acb8592014-07-07 13:01:15 +00001839 for (auto P : VarsWithInheritedDSA) {
1840 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
1841 << P.first << P.second->getSourceRange();
1842 }
1843 if (!VarsWithInheritedDSA.empty())
1844 return StmtError();
1845
Alexey Bataeved09d242014-05-28 05:53:51 +00001846 if (ErrorFound)
1847 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001848 return Res;
1849}
1850
1851StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
1852 Stmt *AStmt,
1853 SourceLocation StartLoc,
1854 SourceLocation EndLoc) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001855 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1856 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
1857 // 1.2.2 OpenMP Language Terminology
1858 // Structured block - An executable statement with a single entry at the
1859 // top and a single exit at the bottom.
1860 // The point of exit cannot be a branch out of the structured block.
1861 // longjmp() and throw() must not violate the entry/exit criteria.
1862 CS->getCapturedDecl()->setNothrow();
1863
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001864 getCurFunction()->setHasBranchProtectedScope();
1865
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001866 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
1867 AStmt);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001868}
1869
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001870namespace {
1871/// \brief Helper class for checking canonical form of the OpenMP loops and
1872/// extracting iteration space of each loop in the loop nest, that will be used
1873/// for IR generation.
1874class OpenMPIterationSpaceChecker {
1875 /// \brief Reference to Sema.
1876 Sema &SemaRef;
1877 /// \brief A location for diagnostics (when there is no some better location).
1878 SourceLocation DefaultLoc;
1879 /// \brief A location for diagnostics (when increment is not compatible).
1880 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00001881 /// \brief A source location for referring to loop init later.
1882 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001883 /// \brief A source location for referring to condition later.
1884 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00001885 /// \brief A source location for referring to increment later.
1886 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001887 /// \brief Loop variable.
1888 VarDecl *Var;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001889 /// \brief Reference to loop variable.
1890 DeclRefExpr *VarRef;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001891 /// \brief Lower bound (initializer for the var).
1892 Expr *LB;
1893 /// \brief Upper bound.
1894 Expr *UB;
1895 /// \brief Loop step (increment).
1896 Expr *Step;
1897 /// \brief This flag is true when condition is one of:
1898 /// Var < UB
1899 /// Var <= UB
1900 /// UB > Var
1901 /// UB >= Var
1902 bool TestIsLessOp;
1903 /// \brief This flag is true when condition is strict ( < or > ).
1904 bool TestIsStrictOp;
1905 /// \brief This flag is true when step is subtracted on each iteration.
1906 bool SubtractStep;
1907
1908public:
1909 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
1910 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
Alexander Musmana5f070a2014-10-01 06:03:56 +00001911 InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()),
1912 IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001913 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
1914 TestIsStrictOp(false), SubtractStep(false) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001915 /// \brief Check init-expr for canonical loop form and save loop counter
1916 /// variable - #Var and its initialization value - #LB.
1917 bool CheckInit(Stmt *S);
1918 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
1919 /// for less/greater and for strict/non-strict comparison.
1920 bool CheckCond(Expr *S);
1921 /// \brief Check incr-expr for canonical loop form and return true if it
1922 /// does not conform, otherwise save loop step (#Step).
1923 bool CheckInc(Expr *S);
1924 /// \brief Return the loop counter variable.
1925 VarDecl *GetLoopVar() const { return Var; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001926 /// \brief Return the reference expression to loop counter variable.
1927 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00001928 /// \brief Source range of the loop init.
1929 SourceRange GetInitSrcRange() const { return InitSrcRange; }
1930 /// \brief Source range of the loop condition.
1931 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
1932 /// \brief Source range of the loop increment.
1933 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
1934 /// \brief True if the step should be subtracted.
1935 bool ShouldSubtractStep() const { return SubtractStep; }
1936 /// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00001937 Expr *BuildNumIterations(Scope *S, const bool LimitedType) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00001938 /// \brief Build reference expression to the counter be used for codegen.
1939 Expr *BuildCounterVar() const;
1940 /// \brief Build initization of the counter be used for codegen.
1941 Expr *BuildCounterInit() const;
1942 /// \brief Build step of the counter be used for codegen.
1943 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001944 /// \brief Return true if any expression is dependent.
1945 bool Dependent() const;
1946
1947private:
1948 /// \brief Check the right-hand side of an assignment in the increment
1949 /// expression.
1950 bool CheckIncRHS(Expr *RHS);
1951 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001952 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001953 /// \brief Helper to set upper bound.
1954 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, const SourceRange &SR,
1955 const SourceLocation &SL);
1956 /// \brief Helper to set loop increment.
1957 bool SetStep(Expr *NewStep, bool Subtract);
1958};
1959
1960bool OpenMPIterationSpaceChecker::Dependent() const {
1961 if (!Var) {
1962 assert(!LB && !UB && !Step);
1963 return false;
1964 }
1965 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
1966 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
1967}
1968
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001969bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
1970 DeclRefExpr *NewVarRefExpr,
1971 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001972 // State consistency checking to ensure correct usage.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001973 assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
1974 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001975 if (!NewVar || !NewLB)
1976 return true;
1977 Var = NewVar;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001978 VarRef = NewVarRefExpr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001979 LB = NewLB;
1980 return false;
1981}
1982
1983bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
1984 const SourceRange &SR,
1985 const SourceLocation &SL) {
1986 // State consistency checking to ensure correct usage.
1987 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
1988 !TestIsLessOp && !TestIsStrictOp);
1989 if (!NewUB)
1990 return true;
1991 UB = NewUB;
1992 TestIsLessOp = LessOp;
1993 TestIsStrictOp = StrictOp;
1994 ConditionSrcRange = SR;
1995 ConditionLoc = SL;
1996 return false;
1997}
1998
1999bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
2000 // State consistency checking to ensure correct usage.
2001 assert(Var != nullptr && LB != nullptr && Step == nullptr);
2002 if (!NewStep)
2003 return true;
2004 if (!NewStep->isValueDependent()) {
2005 // Check that the step is integer expression.
2006 SourceLocation StepLoc = NewStep->getLocStart();
2007 ExprResult Val =
2008 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
2009 if (Val.isInvalid())
2010 return true;
2011 NewStep = Val.get();
2012
2013 // OpenMP [2.6, Canonical Loop Form, Restrictions]
2014 // If test-expr is of form var relational-op b and relational-op is < or
2015 // <= then incr-expr must cause var to increase on each iteration of the
2016 // loop. If test-expr is of form var relational-op b and relational-op is
2017 // > or >= then incr-expr must cause var to decrease on each iteration of
2018 // the loop.
2019 // If test-expr is of form b relational-op var and relational-op is < or
2020 // <= then incr-expr must cause var to decrease on each iteration of the
2021 // loop. If test-expr is of form b relational-op var and relational-op is
2022 // > or >= then incr-expr must cause var to increase on each iteration of
2023 // the loop.
2024 llvm::APSInt Result;
2025 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
2026 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
2027 bool IsConstNeg =
2028 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00002029 bool IsConstPos =
2030 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002031 bool IsConstZero = IsConstant && !Result.getBoolValue();
2032 if (UB && (IsConstZero ||
2033 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00002034 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002035 SemaRef.Diag(NewStep->getExprLoc(),
2036 diag::err_omp_loop_incr_not_compatible)
2037 << Var << TestIsLessOp << NewStep->getSourceRange();
2038 SemaRef.Diag(ConditionLoc,
2039 diag::note_omp_loop_cond_requres_compatible_incr)
2040 << TestIsLessOp << ConditionSrcRange;
2041 return true;
2042 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002043 if (TestIsLessOp == Subtract) {
2044 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
2045 NewStep).get();
2046 Subtract = !Subtract;
2047 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002048 }
2049
2050 Step = NewStep;
2051 SubtractStep = Subtract;
2052 return false;
2053}
2054
2055bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S) {
2056 // Check init-expr for canonical loop form and save loop counter
2057 // variable - #Var and its initialization value - #LB.
2058 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
2059 // var = lb
2060 // integer-type var = lb
2061 // random-access-iterator-type var = lb
2062 // pointer-type var = lb
2063 //
2064 if (!S) {
2065 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
2066 return true;
2067 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002068 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002069 if (Expr *E = dyn_cast<Expr>(S))
2070 S = E->IgnoreParens();
2071 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2072 if (BO->getOpcode() == BO_Assign)
2073 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002074 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002075 BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002076 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
2077 if (DS->isSingleDecl()) {
2078 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
2079 if (Var->hasInit()) {
2080 // Accept non-canonical init form here but emit ext. warning.
2081 if (Var->getInitStyle() != VarDecl::CInit)
2082 SemaRef.Diag(S->getLocStart(),
2083 diag::ext_omp_loop_not_canonical_init)
2084 << S->getSourceRange();
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002085 return SetVarAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002086 }
2087 }
2088 }
2089 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
2090 if (CE->getOperator() == OO_Equal)
2091 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002092 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
2093 CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002094
2095 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
2096 << S->getSourceRange();
2097 return true;
2098}
2099
Alexey Bataev23b69422014-06-18 07:08:49 +00002100/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002101/// variable (which may be the loop variable) if possible.
2102static const VarDecl *GetInitVarDecl(const Expr *E) {
2103 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00002104 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002105 E = E->IgnoreParenImpCasts();
2106 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
2107 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
2108 if (Ctor->isCopyConstructor() && CE->getNumArgs() == 1 &&
2109 CE->getArg(0) != nullptr)
2110 E = CE->getArg(0)->IgnoreParenImpCasts();
2111 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
2112 if (!DRE)
2113 return nullptr;
2114 return dyn_cast<VarDecl>(DRE->getDecl());
2115}
2116
2117bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
2118 // Check test-expr for canonical form, save upper-bound UB, flags for
2119 // less/greater and for strict/non-strict comparison.
2120 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2121 // var relational-op b
2122 // b relational-op var
2123 //
2124 if (!S) {
2125 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
2126 return true;
2127 }
2128 S = S->IgnoreParenImpCasts();
2129 SourceLocation CondLoc = S->getLocStart();
2130 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2131 if (BO->isRelationalOp()) {
2132 if (GetInitVarDecl(BO->getLHS()) == Var)
2133 return SetUB(BO->getRHS(),
2134 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
2135 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2136 BO->getSourceRange(), BO->getOperatorLoc());
2137 if (GetInitVarDecl(BO->getRHS()) == Var)
2138 return SetUB(BO->getLHS(),
2139 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
2140 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2141 BO->getSourceRange(), BO->getOperatorLoc());
2142 }
2143 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2144 if (CE->getNumArgs() == 2) {
2145 auto Op = CE->getOperator();
2146 switch (Op) {
2147 case OO_Greater:
2148 case OO_GreaterEqual:
2149 case OO_Less:
2150 case OO_LessEqual:
2151 if (GetInitVarDecl(CE->getArg(0)) == Var)
2152 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
2153 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2154 CE->getOperatorLoc());
2155 if (GetInitVarDecl(CE->getArg(1)) == Var)
2156 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
2157 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2158 CE->getOperatorLoc());
2159 break;
2160 default:
2161 break;
2162 }
2163 }
2164 }
2165 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
2166 << S->getSourceRange() << Var;
2167 return true;
2168}
2169
2170bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
2171 // RHS of canonical loop form increment can be:
2172 // var + incr
2173 // incr + var
2174 // var - incr
2175 //
2176 RHS = RHS->IgnoreParenImpCasts();
2177 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
2178 if (BO->isAdditiveOp()) {
2179 bool IsAdd = BO->getOpcode() == BO_Add;
2180 if (GetInitVarDecl(BO->getLHS()) == Var)
2181 return SetStep(BO->getRHS(), !IsAdd);
2182 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
2183 return SetStep(BO->getLHS(), false);
2184 }
2185 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
2186 bool IsAdd = CE->getOperator() == OO_Plus;
2187 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
2188 if (GetInitVarDecl(CE->getArg(0)) == Var)
2189 return SetStep(CE->getArg(1), !IsAdd);
2190 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
2191 return SetStep(CE->getArg(0), false);
2192 }
2193 }
2194 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2195 << RHS->getSourceRange() << Var;
2196 return true;
2197}
2198
2199bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
2200 // Check incr-expr for canonical loop form and return true if it
2201 // does not conform.
2202 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2203 // ++var
2204 // var++
2205 // --var
2206 // var--
2207 // var += incr
2208 // var -= incr
2209 // var = var + incr
2210 // var = incr + var
2211 // var = var - incr
2212 //
2213 if (!S) {
2214 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
2215 return true;
2216 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002217 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002218 S = S->IgnoreParens();
2219 if (auto UO = dyn_cast<UnaryOperator>(S)) {
2220 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
2221 return SetStep(
2222 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
2223 (UO->isDecrementOp() ? -1 : 1)).get(),
2224 false);
2225 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
2226 switch (BO->getOpcode()) {
2227 case BO_AddAssign:
2228 case BO_SubAssign:
2229 if (GetInitVarDecl(BO->getLHS()) == Var)
2230 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
2231 break;
2232 case BO_Assign:
2233 if (GetInitVarDecl(BO->getLHS()) == Var)
2234 return CheckIncRHS(BO->getRHS());
2235 break;
2236 default:
2237 break;
2238 }
2239 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2240 switch (CE->getOperator()) {
2241 case OO_PlusPlus:
2242 case OO_MinusMinus:
2243 if (GetInitVarDecl(CE->getArg(0)) == Var)
2244 return SetStep(
2245 SemaRef.ActOnIntegerConstant(
2246 CE->getLocStart(),
2247 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
2248 false);
2249 break;
2250 case OO_PlusEqual:
2251 case OO_MinusEqual:
2252 if (GetInitVarDecl(CE->getArg(0)) == Var)
2253 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
2254 break;
2255 case OO_Equal:
2256 if (GetInitVarDecl(CE->getArg(0)) == Var)
2257 return CheckIncRHS(CE->getArg(1));
2258 break;
2259 default:
2260 break;
2261 }
2262 }
2263 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2264 << S->getSourceRange() << Var;
2265 return true;
2266}
Alexander Musmana5f070a2014-10-01 06:03:56 +00002267
2268/// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00002269Expr *
2270OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S,
2271 const bool LimitedType) const {
Alexander Musmana5f070a2014-10-01 06:03:56 +00002272 ExprResult Diff;
2273 if (Var->getType()->isIntegerType() || Var->getType()->isPointerType() ||
2274 SemaRef.getLangOpts().CPlusPlus) {
2275 // Upper - Lower
2276 Expr *Upper = TestIsLessOp ? UB : LB;
2277 Expr *Lower = TestIsLessOp ? LB : UB;
2278
2279 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
2280
2281 if (!Diff.isUsable() && Var->getType()->getAsCXXRecordDecl()) {
2282 // BuildBinOp already emitted error, this one is to point user to upper
2283 // and lower bound, and to tell what is passed to 'operator-'.
2284 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
2285 << Upper->getSourceRange() << Lower->getSourceRange();
2286 return nullptr;
2287 }
2288 }
2289
2290 if (!Diff.isUsable())
2291 return nullptr;
2292
2293 // Upper - Lower [- 1]
2294 if (TestIsStrictOp)
2295 Diff = SemaRef.BuildBinOp(
2296 S, DefaultLoc, BO_Sub, Diff.get(),
2297 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
2298 if (!Diff.isUsable())
2299 return nullptr;
2300
2301 // Upper - Lower [- 1] + Step
2302 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(),
2303 Step->IgnoreImplicit());
2304 if (!Diff.isUsable())
2305 return nullptr;
2306
2307 // Parentheses (for dumping/debugging purposes only).
2308 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
2309 if (!Diff.isUsable())
2310 return nullptr;
2311
2312 // (Upper - Lower [- 1] + Step) / Step
2313 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(),
2314 Step->IgnoreImplicit());
2315 if (!Diff.isUsable())
2316 return nullptr;
2317
Alexander Musman174b3ca2014-10-06 11:16:29 +00002318 // OpenMP runtime requires 32-bit or 64-bit loop variables.
2319 if (LimitedType) {
2320 auto &C = SemaRef.Context;
2321 QualType Type = Diff.get()->getType();
2322 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
2323 if (NewSize != C.getTypeSize(Type)) {
2324 if (NewSize < C.getTypeSize(Type)) {
2325 assert(NewSize == 64 && "incorrect loop var size");
2326 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
2327 << InitSrcRange << ConditionSrcRange;
2328 }
2329 QualType NewType = C.getIntTypeForBitwidth(
2330 NewSize, Type->hasSignedIntegerRepresentation());
2331 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
2332 Sema::AA_Converting, true);
2333 if (!Diff.isUsable())
2334 return nullptr;
2335 }
2336 }
2337
Alexander Musmana5f070a2014-10-01 06:03:56 +00002338 return Diff.get();
2339}
2340
2341/// \brief Build reference expression to the counter be used for codegen.
2342Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const {
2343 return DeclRefExpr::Create(SemaRef.Context, NestedNameSpecifierLoc(),
2344 GetIncrementSrcRange().getBegin(), Var, false,
2345 DefaultLoc, Var->getType(), VK_LValue);
2346}
2347
2348/// \brief Build initization of the counter be used for codegen.
2349Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
2350
2351/// \brief Build step of the counter be used for codegen.
2352Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
2353
2354/// \brief Iteration space of a single for loop.
2355struct LoopIterationSpace {
2356 /// \brief This expression calculates the number of iterations in the loop.
2357 /// It is always possible to calculate it before starting the loop.
2358 Expr *NumIterations;
2359 /// \brief The loop counter variable.
2360 Expr *CounterVar;
2361 /// \brief This is initializer for the initial value of #CounterVar.
2362 Expr *CounterInit;
2363 /// \brief This is step for the #CounterVar used to generate its update:
2364 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
2365 Expr *CounterStep;
2366 /// \brief Should step be subtracted?
2367 bool Subtract;
2368 /// \brief Source range of the loop init.
2369 SourceRange InitSrcRange;
2370 /// \brief Source range of the loop condition.
2371 SourceRange CondSrcRange;
2372 /// \brief Source range of the loop increment.
2373 SourceRange IncSrcRange;
2374};
2375
Alexey Bataev23b69422014-06-18 07:08:49 +00002376} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002377
2378/// \brief Called on a for stmt to check and extract its iteration space
2379/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00002380static bool CheckOpenMPIterationSpace(
2381 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
2382 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
2383 Expr *NestedLoopCountExpr,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002384 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
2385 LoopIterationSpace &ResultIterSpace) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002386 // OpenMP [2.6, Canonical Loop Form]
2387 // for (init-expr; test-expr; incr-expr) structured-block
2388 auto For = dyn_cast_or_null<ForStmt>(S);
2389 if (!For) {
2390 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002391 << (NestedLoopCountExpr != nullptr) << getOpenMPDirectiveName(DKind)
2392 << NestedLoopCount << (CurrentNestedLoopCount > 0)
2393 << CurrentNestedLoopCount;
2394 if (NestedLoopCount > 1)
2395 SemaRef.Diag(NestedLoopCountExpr->getExprLoc(),
2396 diag::note_omp_collapse_expr)
2397 << NestedLoopCountExpr->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002398 return true;
2399 }
2400 assert(For->getBody());
2401
2402 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
2403
2404 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002405 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002406 if (ISC.CheckInit(Init)) {
2407 return true;
2408 }
2409
2410 bool HasErrors = false;
2411
2412 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002413 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002414
2415 // OpenMP [2.6, Canonical Loop Form]
2416 // Var is one of the following:
2417 // A variable of signed or unsigned integer type.
2418 // For C++, a variable of a random access iterator type.
2419 // For C, a variable of a pointer type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002420 auto VarType = Var->getType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002421 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
2422 !VarType->isPointerType() &&
2423 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
2424 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
2425 << SemaRef.getLangOpts().CPlusPlus;
2426 HasErrors = true;
2427 }
2428
Alexey Bataev4acb8592014-07-07 13:01:15 +00002429 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
2430 // Construct
2431 // The loop iteration variable(s) in the associated for-loop(s) of a for or
2432 // parallel for construct is (are) private.
2433 // The loop iteration variable in the associated for-loop of a simd construct
2434 // with just one associated for-loop is linear with a constant-linear-step
2435 // that is the increment of the associated for-loop.
2436 // Exclude loop var from the list of variables with implicitly defined data
2437 // sharing attributes.
Benjamin Kramerad8e0792014-10-10 15:32:48 +00002438 VarsWithImplicitDSA.erase(Var);
Alexey Bataev4acb8592014-07-07 13:01:15 +00002439
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002440 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
2441 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00002442 // The loop iteration variable in the associated for-loop of a simd construct
2443 // with just one associated for-loop may be listed in a linear clause with a
2444 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002445 // The loop iteration variable(s) in the associated for-loop(s) of a for or
2446 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002447 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002448 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
2449 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
2450 // declared in the loop and it is predetermined as a private.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002451 auto PredeterminedCKind =
2452 isOpenMPSimdDirective(DKind)
2453 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
2454 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002455 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00002456 DVar.CKind != PredeterminedCKind) ||
Alexander Musmanf82886e2014-09-18 05:12:34 +00002457 (isOpenMPWorksharingDirective(DKind) && !isOpenMPSimdDirective(DKind) &&
2458 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private &&
2459 DVar.CKind != OMPC_lastprivate)) &&
Alexander Musman1bb328c2014-06-04 13:06:39 +00002460 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002461 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00002462 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
2463 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002464 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002465 HasErrors = true;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002466 } else if (LoopVarRefExpr != nullptr) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00002467 // Make the loop iteration variable private (for worksharing constructs),
2468 // linear (for simd directives with the only one associated loop) or
2469 // lastprivate (for simd directives with several collapsed loops).
Alexey Bataev9aba41c2014-11-14 04:08:45 +00002470 // FIXME: the next check and error message must be removed once the
2471 // capturing of global variables in loops is fixed.
2472 if (DVar.CKind == OMPC_unknown)
2473 DVar = DSA.hasDSA(Var, isOpenMPPrivate, MatchesAlways(),
2474 /*FromParent=*/false);
2475 if (!Var->hasLocalStorage() && DVar.CKind == OMPC_unknown) {
2476 SemaRef.Diag(Init->getLocStart(), diag::err_omp_global_loop_var_dsa)
2477 << getOpenMPClauseName(PredeterminedCKind)
2478 << getOpenMPDirectiveName(DKind);
2479 HasErrors = true;
2480 } else
2481 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002482 }
2483
Alexey Bataev7ff55242014-06-19 09:13:45 +00002484 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00002485
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002486 // Check test-expr.
2487 HasErrors |= ISC.CheckCond(For->getCond());
2488
2489 // Check incr-expr.
2490 HasErrors |= ISC.CheckInc(For->getInc());
2491
Alexander Musmana5f070a2014-10-01 06:03:56 +00002492 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002493 return HasErrors;
2494
Alexander Musmana5f070a2014-10-01 06:03:56 +00002495 // Build the loop's iteration space representation.
Alexander Musman174b3ca2014-10-06 11:16:29 +00002496 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
2497 DSA.getCurScope(), /* LimitedType */ isOpenMPWorksharingDirective(DKind));
Alexander Musmana5f070a2014-10-01 06:03:56 +00002498 ResultIterSpace.CounterVar = ISC.BuildCounterVar();
2499 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
2500 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
2501 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
2502 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
2503 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
2504 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
2505
2506 HasErrors |= (ResultIterSpace.NumIterations == nullptr ||
2507 ResultIterSpace.CounterVar == nullptr ||
2508 ResultIterSpace.CounterInit == nullptr ||
2509 ResultIterSpace.CounterStep == nullptr);
2510
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002511 return HasErrors;
2512}
2513
Alexander Musmana5f070a2014-10-01 06:03:56 +00002514/// \brief Build a variable declaration for OpenMP loop iteration variable.
2515static VarDecl *BuildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
2516 StringRef Name) {
2517 DeclContext *DC = SemaRef.CurContext;
2518 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
2519 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
2520 VarDecl *Decl =
2521 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
2522 Decl->setImplicit();
2523 return Decl;
2524}
2525
2526/// \brief Build 'VarRef = Start + Iter * Step'.
2527static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S,
2528 SourceLocation Loc, ExprResult VarRef,
2529 ExprResult Start, ExprResult Iter,
2530 ExprResult Step, bool Subtract) {
2531 // Add parentheses (for debugging purposes only).
2532 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
2533 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
2534 !Step.isUsable())
2535 return ExprError();
2536
2537 ExprResult Update = SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(),
2538 Step.get()->IgnoreImplicit());
2539 if (!Update.isUsable())
2540 return ExprError();
2541
2542 // Build 'VarRef = Start + Iter * Step'.
2543 Update = SemaRef.BuildBinOp(S, Loc, (Subtract ? BO_Sub : BO_Add),
2544 Start.get()->IgnoreImplicit(), Update.get());
2545 if (!Update.isUsable())
2546 return ExprError();
2547
2548 Update = SemaRef.PerformImplicitConversion(
2549 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
2550 if (!Update.isUsable())
2551 return ExprError();
2552
2553 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
2554 return Update;
2555}
2556
2557/// \brief Convert integer expression \a E to make it have at least \a Bits
2558/// bits.
2559static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
2560 Sema &SemaRef) {
2561 if (E == nullptr)
2562 return ExprError();
2563 auto &C = SemaRef.Context;
2564 QualType OldType = E->getType();
2565 unsigned HasBits = C.getTypeSize(OldType);
2566 if (HasBits >= Bits)
2567 return ExprResult(E);
2568 // OK to convert to signed, because new type has more bits than old.
2569 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
2570 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
2571 true);
2572}
2573
2574/// \brief Check if the given expression \a E is a constant integer that fits
2575/// into \a Bits bits.
2576static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
2577 if (E == nullptr)
2578 return false;
2579 llvm::APSInt Result;
2580 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
2581 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
2582 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002583}
2584
2585/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00002586/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
2587/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002588static unsigned
2589CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *NestedLoopCountExpr,
2590 Stmt *AStmt, Sema &SemaRef, DSAStackTy &DSA,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002591 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00002592 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002593 unsigned NestedLoopCount = 1;
2594 if (NestedLoopCountExpr) {
2595 // Found 'collapse' clause - calculate collapse number.
2596 llvm::APSInt Result;
2597 if (NestedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
2598 NestedLoopCount = Result.getLimitedValue();
2599 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002600 // This is helper routine for loop directives (e.g., 'for', 'simd',
2601 // 'for simd', etc.).
Alexander Musmana5f070a2014-10-01 06:03:56 +00002602 SmallVector<LoopIterationSpace, 4> IterSpaces;
2603 IterSpaces.resize(NestedLoopCount);
2604 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002605 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002606 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev4acb8592014-07-07 13:01:15 +00002607 NestedLoopCount, NestedLoopCountExpr,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002608 VarsWithImplicitDSA, IterSpaces[Cnt]))
Alexey Bataevabfc0692014-06-25 06:52:00 +00002609 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002610 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00002611 // OpenMP [2.8.1, simd construct, Restrictions]
2612 // All loops associated with the construct must be perfectly nested; that
2613 // is, there must be no intervening code nor any OpenMP directive between
2614 // any two loops.
2615 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002616 }
2617
Alexander Musmana5f070a2014-10-01 06:03:56 +00002618 Built.clear(/* size */ NestedLoopCount);
2619
2620 if (SemaRef.CurContext->isDependentContext())
2621 return NestedLoopCount;
2622
2623 // An example of what is generated for the following code:
2624 //
2625 // #pragma omp simd collapse(2)
2626 // for (i = 0; i < NI; ++i)
2627 // for (j = J0; j < NJ; j+=2) {
2628 // <loop body>
2629 // }
2630 //
2631 // We generate the code below.
2632 // Note: the loop body may be outlined in CodeGen.
2633 // Note: some counters may be C++ classes, operator- is used to find number of
2634 // iterations and operator+= to calculate counter value.
2635 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
2636 // or i64 is currently supported).
2637 //
2638 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
2639 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
2640 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
2641 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
2642 // // similar updates for vars in clauses (e.g. 'linear')
2643 // <loop body (using local i and j)>
2644 // }
2645 // i = NI; // assign final values of counters
2646 // j = NJ;
2647 //
2648
2649 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
2650 // the iteration counts of the collapsed for loops.
2651 auto N0 = IterSpaces[0].NumIterations;
2652 ExprResult LastIteration32 = WidenIterationCount(32 /* Bits */, N0, SemaRef);
2653 ExprResult LastIteration64 = WidenIterationCount(64 /* Bits */, N0, SemaRef);
2654
2655 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
2656 return NestedLoopCount;
2657
2658 auto &C = SemaRef.Context;
2659 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
2660
2661 Scope *CurScope = DSA.getCurScope();
2662 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
2663 auto N = IterSpaces[Cnt].NumIterations;
2664 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
2665 if (LastIteration32.isUsable())
2666 LastIteration32 = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_Mul,
2667 LastIteration32.get(), N);
2668 if (LastIteration64.isUsable())
2669 LastIteration64 = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_Mul,
2670 LastIteration64.get(), N);
2671 }
2672
2673 // Choose either the 32-bit or 64-bit version.
2674 ExprResult LastIteration = LastIteration64;
2675 if (LastIteration32.isUsable() &&
2676 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
2677 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
2678 FitsInto(
2679 32 /* Bits */,
2680 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
2681 LastIteration64.get(), SemaRef)))
2682 LastIteration = LastIteration32;
2683
2684 if (!LastIteration.isUsable())
2685 return 0;
2686
2687 // Save the number of iterations.
2688 ExprResult NumIterations = LastIteration;
2689 {
2690 LastIteration = SemaRef.BuildBinOp(
2691 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
2692 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
2693 if (!LastIteration.isUsable())
2694 return 0;
2695 }
2696
2697 // Calculate the last iteration number beforehand instead of doing this on
2698 // each iteration. Do not do this if the number of iterations may be kfold-ed.
2699 llvm::APSInt Result;
2700 bool IsConstant =
2701 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
2702 ExprResult CalcLastIteration;
2703 if (!IsConstant) {
2704 SourceLocation SaveLoc;
2705 VarDecl *SaveVar =
2706 BuildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(),
2707 ".omp.last.iteration");
2708 ExprResult SaveRef = SemaRef.BuildDeclRefExpr(
2709 SaveVar, LastIteration.get()->getType(), VK_LValue, SaveLoc);
2710 CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign,
2711 SaveRef.get(), LastIteration.get());
2712 LastIteration = SaveRef;
2713
2714 // Prepare SaveRef + 1.
2715 NumIterations = SemaRef.BuildBinOp(
2716 CurScope, SaveLoc, BO_Add, SaveRef.get(),
2717 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
2718 if (!NumIterations.isUsable())
2719 return 0;
2720 }
2721
2722 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
2723
2724 // Precondition tests if there is at least one iteration (LastIteration > 0).
2725 ExprResult PreCond = SemaRef.BuildBinOp(
2726 CurScope, InitLoc, BO_GT, LastIteration.get(),
2727 SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get());
2728
Alexander Musmanc6388682014-12-15 07:07:06 +00002729 QualType VType = LastIteration.get()->getType();
2730 // Build variables passed into runtime, nesessary for worksharing directives.
2731 ExprResult LB, UB, IL, ST, EUB;
2732 if (isOpenMPWorksharingDirective(DKind)) {
2733 // Lower bound variable, initialized with zero.
2734 VarDecl *LBDecl = BuildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
2735 LB = SemaRef.BuildDeclRefExpr(LBDecl, VType, VK_LValue, InitLoc);
2736 SemaRef.AddInitializerToDecl(
2737 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
2738 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
2739
2740 // Upper bound variable, initialized with last iteration number.
2741 VarDecl *UBDecl = BuildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
2742 UB = SemaRef.BuildDeclRefExpr(UBDecl, VType, VK_LValue, InitLoc);
2743 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
2744 /*DirectInit*/ false,
2745 /*TypeMayContainAuto*/ false);
2746
2747 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
2748 // This will be used to implement clause 'lastprivate'.
2749 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
2750 VarDecl *ILDecl = BuildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
2751 IL = SemaRef.BuildDeclRefExpr(ILDecl, Int32Ty, VK_LValue, InitLoc);
2752 SemaRef.AddInitializerToDecl(
2753 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
2754 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
2755
2756 // Stride variable returned by runtime (we initialize it to 1 by default).
2757 VarDecl *STDecl = BuildVarDecl(SemaRef, InitLoc, VType, ".omp.stride");
2758 ST = SemaRef.BuildDeclRefExpr(STDecl, VType, VK_LValue, InitLoc);
2759 SemaRef.AddInitializerToDecl(
2760 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
2761 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
2762
2763 // Build expression: UB = min(UB, LastIteration)
2764 // It is nesessary for CodeGen of directives with static scheduling.
2765 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
2766 UB.get(), LastIteration.get());
2767 ExprResult CondOp = SemaRef.ActOnConditionalOp(
2768 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
2769 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
2770 CondOp.get());
2771 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
2772 }
2773
2774 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00002775 ExprResult IV;
2776 ExprResult Init;
2777 {
Alexander Musmanc6388682014-12-15 07:07:06 +00002778 VarDecl *IVDecl = BuildVarDecl(SemaRef, InitLoc, VType, ".omp.iv");
2779 IV = SemaRef.BuildDeclRefExpr(IVDecl, VType, VK_LValue, InitLoc);
2780 Expr *RHS = isOpenMPWorksharingDirective(DKind)
2781 ? LB.get()
2782 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
2783 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
2784 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00002785 }
2786
Alexander Musmanc6388682014-12-15 07:07:06 +00002787 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00002788 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00002789 ExprResult Cond =
2790 isOpenMPWorksharingDirective(DKind)
2791 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
2792 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
2793 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00002794 // Loop condition with 1 iteration separated (IV < LastIteration)
2795 ExprResult SeparatedCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT,
2796 IV.get(), LastIteration.get());
2797
2798 // Loop increment (IV = IV + 1)
2799 SourceLocation IncLoc;
2800 ExprResult Inc =
2801 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
2802 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
2803 if (!Inc.isUsable())
2804 return 0;
2805 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00002806 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
2807 if (!Inc.isUsable())
2808 return 0;
2809
2810 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
2811 // Used for directives with static scheduling.
2812 ExprResult NextLB, NextUB;
2813 if (isOpenMPWorksharingDirective(DKind)) {
2814 // LB + ST
2815 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
2816 if (!NextLB.isUsable())
2817 return 0;
2818 // LB = LB + ST
2819 NextLB =
2820 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
2821 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
2822 if (!NextLB.isUsable())
2823 return 0;
2824 // UB + ST
2825 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
2826 if (!NextUB.isUsable())
2827 return 0;
2828 // UB = UB + ST
2829 NextUB =
2830 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
2831 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
2832 if (!NextUB.isUsable())
2833 return 0;
2834 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002835
2836 // Build updates and final values of the loop counters.
2837 bool HasErrors = false;
2838 Built.Counters.resize(NestedLoopCount);
2839 Built.Updates.resize(NestedLoopCount);
2840 Built.Finals.resize(NestedLoopCount);
2841 {
2842 ExprResult Div;
2843 // Go from inner nested loop to outer.
2844 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
2845 LoopIterationSpace &IS = IterSpaces[Cnt];
2846 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
2847 // Build: Iter = (IV / Div) % IS.NumIters
2848 // where Div is product of previous iterations' IS.NumIters.
2849 ExprResult Iter;
2850 if (Div.isUsable()) {
2851 Iter =
2852 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
2853 } else {
2854 Iter = IV;
2855 assert((Cnt == (int)NestedLoopCount - 1) &&
2856 "unusable div expected on first iteration only");
2857 }
2858
2859 if (Cnt != 0 && Iter.isUsable())
2860 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
2861 IS.NumIterations);
2862 if (!Iter.isUsable()) {
2863 HasErrors = true;
2864 break;
2865 }
2866
2867 // Build update: IS.CounterVar = IS.Start + Iter * IS.Step
2868 ExprResult Update =
2869 BuildCounterUpdate(SemaRef, CurScope, UpdLoc, IS.CounterVar,
2870 IS.CounterInit, Iter, IS.CounterStep, IS.Subtract);
2871 if (!Update.isUsable()) {
2872 HasErrors = true;
2873 break;
2874 }
2875
2876 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
2877 ExprResult Final = BuildCounterUpdate(
2878 SemaRef, CurScope, UpdLoc, IS.CounterVar, IS.CounterInit,
2879 IS.NumIterations, IS.CounterStep, IS.Subtract);
2880 if (!Final.isUsable()) {
2881 HasErrors = true;
2882 break;
2883 }
2884
2885 // Build Div for the next iteration: Div <- Div * IS.NumIters
2886 if (Cnt != 0) {
2887 if (Div.isUnset())
2888 Div = IS.NumIterations;
2889 else
2890 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
2891 IS.NumIterations);
2892
2893 // Add parentheses (for debugging purposes only).
2894 if (Div.isUsable())
2895 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
2896 if (!Div.isUsable()) {
2897 HasErrors = true;
2898 break;
2899 }
2900 }
2901 if (!Update.isUsable() || !Final.isUsable()) {
2902 HasErrors = true;
2903 break;
2904 }
2905 // Save results
2906 Built.Counters[Cnt] = IS.CounterVar;
2907 Built.Updates[Cnt] = Update.get();
2908 Built.Finals[Cnt] = Final.get();
2909 }
2910 }
2911
2912 if (HasErrors)
2913 return 0;
2914
2915 // Save results
2916 Built.IterationVarRef = IV.get();
2917 Built.LastIteration = LastIteration.get();
2918 Built.CalcLastIteration = CalcLastIteration.get();
2919 Built.PreCond = PreCond.get();
2920 Built.Cond = Cond.get();
2921 Built.SeparatedCond = SeparatedCond.get();
2922 Built.Init = Init.get();
2923 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00002924 Built.LB = LB.get();
2925 Built.UB = UB.get();
2926 Built.IL = IL.get();
2927 Built.ST = ST.get();
2928 Built.EUB = EUB.get();
2929 Built.NLB = NextLB.get();
2930 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00002931
Alexey Bataevabfc0692014-06-25 06:52:00 +00002932 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002933}
2934
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002935static Expr *GetCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002936 auto CollapseFilter = [](const OMPClause *C) -> bool {
2937 return C->getClauseKind() == OMPC_collapse;
2938 };
2939 OMPExecutableDirective::filtered_clause_iterator<decltype(CollapseFilter)> I(
2940 Clauses, CollapseFilter);
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002941 if (I)
2942 return cast<OMPCollapseClause>(*I)->getNumForLoops();
2943 return nullptr;
2944}
2945
Alexey Bataev4acb8592014-07-07 13:01:15 +00002946StmtResult Sema::ActOnOpenMPSimdDirective(
2947 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2948 SourceLocation EndLoc,
2949 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmanc6388682014-12-15 07:07:06 +00002950 OMPLoopDirective::HelperExprs B;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002951 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002952 unsigned NestedLoopCount =
2953 CheckOpenMPLoop(OMPD_simd, GetCollapseNumberExpr(Clauses), AStmt, *this,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002954 *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00002955 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002956 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002957
Alexander Musmana5f070a2014-10-01 06:03:56 +00002958 assert((CurContext->isDependentContext() || B.builtAll()) &&
2959 "omp simd loop exprs were not built");
2960
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002961 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00002962 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
2963 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002964}
2965
Alexey Bataev4acb8592014-07-07 13:01:15 +00002966StmtResult Sema::ActOnOpenMPForDirective(
2967 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2968 SourceLocation EndLoc,
2969 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmanc6388682014-12-15 07:07:06 +00002970 OMPLoopDirective::HelperExprs B;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002971 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002972 unsigned NestedLoopCount =
2973 CheckOpenMPLoop(OMPD_for, GetCollapseNumberExpr(Clauses), AStmt, *this,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002974 *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00002975 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00002976 return StmtError();
2977
Alexander Musmana5f070a2014-10-01 06:03:56 +00002978 assert((CurContext->isDependentContext() || B.builtAll()) &&
2979 "omp for loop exprs were not built");
2980
Alexey Bataevf29276e2014-06-18 04:14:57 +00002981 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00002982 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
2983 Clauses, AStmt, B);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002984}
2985
Alexander Musmanf82886e2014-09-18 05:12:34 +00002986StmtResult Sema::ActOnOpenMPForSimdDirective(
2987 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2988 SourceLocation EndLoc,
2989 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmanc6388682014-12-15 07:07:06 +00002990 OMPLoopDirective::HelperExprs B;
Alexander Musmanf82886e2014-09-18 05:12:34 +00002991 // In presence of clause 'collapse', it will define the nested loops number.
2992 unsigned NestedLoopCount =
2993 CheckOpenMPLoop(OMPD_for_simd, GetCollapseNumberExpr(Clauses), AStmt,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002994 *this, *DSAStack, VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00002995 if (NestedLoopCount == 0)
2996 return StmtError();
2997
Alexander Musmanc6388682014-12-15 07:07:06 +00002998 assert((CurContext->isDependentContext() || B.builtAll()) &&
2999 "omp for simd loop exprs were not built");
3000
Alexander Musmanf82886e2014-09-18 05:12:34 +00003001 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003002 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
3003 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00003004}
3005
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003006StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
3007 Stmt *AStmt,
3008 SourceLocation StartLoc,
3009 SourceLocation EndLoc) {
3010 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3011 auto BaseStmt = AStmt;
3012 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
3013 BaseStmt = CS->getCapturedStmt();
3014 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
3015 auto S = C->children();
3016 if (!S)
3017 return StmtError();
3018 // All associated statements must be '#pragma omp section' except for
3019 // the first one.
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003020 for (++S; S; ++S) {
3021 auto SectionStmt = *S;
3022 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
3023 if (SectionStmt)
3024 Diag(SectionStmt->getLocStart(),
3025 diag::err_omp_sections_substmt_not_section);
3026 return StmtError();
3027 }
3028 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003029 } else {
3030 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
3031 return StmtError();
3032 }
3033
3034 getCurFunction()->setHasBranchProtectedScope();
3035
3036 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses,
3037 AStmt);
3038}
3039
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003040StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
3041 SourceLocation StartLoc,
3042 SourceLocation EndLoc) {
3043 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3044
3045 getCurFunction()->setHasBranchProtectedScope();
3046
3047 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt);
3048}
3049
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003050StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
3051 Stmt *AStmt,
3052 SourceLocation StartLoc,
3053 SourceLocation EndLoc) {
Alexey Bataev74a05c92014-07-15 02:55:09 +00003054 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3055
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003056 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00003057
Alexey Bataev3255bf32015-01-19 05:20:46 +00003058 // OpenMP [2.7.3, single Construct, Restrictions]
3059 // The copyprivate clause must not be used with the nowait clause.
3060 OMPClause *Nowait = nullptr;
3061 OMPClause *Copyprivate = nullptr;
3062 for (auto *Clause : Clauses) {
3063 if (Clause->getClauseKind() == OMPC_nowait)
3064 Nowait = Clause;
3065 else if (Clause->getClauseKind() == OMPC_copyprivate)
3066 Copyprivate = Clause;
3067 if (Copyprivate && Nowait) {
3068 Diag(Copyprivate->getLocStart(),
3069 diag::err_omp_single_copyprivate_with_nowait);
3070 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
3071 return StmtError();
3072 }
3073 }
3074
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003075 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3076}
3077
Alexander Musman80c22892014-07-17 08:54:58 +00003078StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
3079 SourceLocation StartLoc,
3080 SourceLocation EndLoc) {
3081 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3082
3083 getCurFunction()->setHasBranchProtectedScope();
3084
3085 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
3086}
3087
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003088StmtResult
3089Sema::ActOnOpenMPCriticalDirective(const DeclarationNameInfo &DirName,
3090 Stmt *AStmt, SourceLocation StartLoc,
3091 SourceLocation EndLoc) {
3092 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3093
3094 getCurFunction()->setHasBranchProtectedScope();
3095
3096 return OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
3097 AStmt);
3098}
3099
Alexey Bataev4acb8592014-07-07 13:01:15 +00003100StmtResult Sema::ActOnOpenMPParallelForDirective(
3101 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3102 SourceLocation EndLoc,
3103 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
3104 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3105 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3106 // 1.2.2 OpenMP Language Terminology
3107 // Structured block - An executable statement with a single entry at the
3108 // top and a single exit at the bottom.
3109 // The point of exit cannot be a branch out of the structured block.
3110 // longjmp() and throw() must not violate the entry/exit criteria.
3111 CS->getCapturedDecl()->setNothrow();
3112
Alexander Musmanc6388682014-12-15 07:07:06 +00003113 OMPLoopDirective::HelperExprs B;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003114 // In presence of clause 'collapse', it will define the nested loops number.
3115 unsigned NestedLoopCount =
3116 CheckOpenMPLoop(OMPD_parallel_for, GetCollapseNumberExpr(Clauses), AStmt,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003117 *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003118 if (NestedLoopCount == 0)
3119 return StmtError();
3120
Alexander Musmana5f070a2014-10-01 06:03:56 +00003121 assert((CurContext->isDependentContext() || B.builtAll()) &&
3122 "omp parallel for loop exprs were not built");
3123
Alexey Bataev4acb8592014-07-07 13:01:15 +00003124 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003125 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
3126 NestedLoopCount, Clauses, AStmt, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003127}
3128
Alexander Musmane4e893b2014-09-23 09:33:00 +00003129StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
3130 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3131 SourceLocation EndLoc,
3132 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
3133 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3134 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3135 // 1.2.2 OpenMP Language Terminology
3136 // Structured block - An executable statement with a single entry at the
3137 // top and a single exit at the bottom.
3138 // The point of exit cannot be a branch out of the structured block.
3139 // longjmp() and throw() must not violate the entry/exit criteria.
3140 CS->getCapturedDecl()->setNothrow();
3141
Alexander Musmanc6388682014-12-15 07:07:06 +00003142 OMPLoopDirective::HelperExprs B;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003143 // In presence of clause 'collapse', it will define the nested loops number.
3144 unsigned NestedLoopCount =
3145 CheckOpenMPLoop(OMPD_parallel_for_simd, GetCollapseNumberExpr(Clauses),
Alexander Musmana5f070a2014-10-01 06:03:56 +00003146 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003147 if (NestedLoopCount == 0)
3148 return StmtError();
3149
3150 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003151 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00003152 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003153}
3154
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003155StmtResult
3156Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
3157 Stmt *AStmt, SourceLocation StartLoc,
3158 SourceLocation EndLoc) {
3159 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3160 auto BaseStmt = AStmt;
3161 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
3162 BaseStmt = CS->getCapturedStmt();
3163 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
3164 auto S = C->children();
3165 if (!S)
3166 return StmtError();
3167 // All associated statements must be '#pragma omp section' except for
3168 // the first one.
3169 for (++S; S; ++S) {
3170 auto SectionStmt = *S;
3171 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
3172 if (SectionStmt)
3173 Diag(SectionStmt->getLocStart(),
3174 diag::err_omp_parallel_sections_substmt_not_section);
3175 return StmtError();
3176 }
3177 }
3178 } else {
3179 Diag(AStmt->getLocStart(),
3180 diag::err_omp_parallel_sections_not_compound_stmt);
3181 return StmtError();
3182 }
3183
3184 getCurFunction()->setHasBranchProtectedScope();
3185
3186 return OMPParallelSectionsDirective::Create(Context, StartLoc, EndLoc,
3187 Clauses, AStmt);
3188}
3189
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003190StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
3191 Stmt *AStmt, SourceLocation StartLoc,
3192 SourceLocation EndLoc) {
3193 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3194 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3195 // 1.2.2 OpenMP Language Terminology
3196 // Structured block - An executable statement with a single entry at the
3197 // top and a single exit at the bottom.
3198 // The point of exit cannot be a branch out of the structured block.
3199 // longjmp() and throw() must not violate the entry/exit criteria.
3200 CS->getCapturedDecl()->setNothrow();
3201
3202 getCurFunction()->setHasBranchProtectedScope();
3203
3204 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3205}
3206
Alexey Bataev68446b72014-07-18 07:47:19 +00003207StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
3208 SourceLocation EndLoc) {
3209 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
3210}
3211
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003212StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
3213 SourceLocation EndLoc) {
3214 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
3215}
3216
Alexey Bataev2df347a2014-07-18 10:17:07 +00003217StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
3218 SourceLocation EndLoc) {
3219 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
3220}
3221
Alexey Bataev6125da92014-07-21 11:26:11 +00003222StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
3223 SourceLocation StartLoc,
3224 SourceLocation EndLoc) {
3225 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
3226 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
3227}
3228
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003229StmtResult Sema::ActOnOpenMPOrderedDirective(Stmt *AStmt,
3230 SourceLocation StartLoc,
3231 SourceLocation EndLoc) {
3232 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3233
3234 getCurFunction()->setHasBranchProtectedScope();
3235
3236 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, AStmt);
3237}
3238
Alexey Bataev0162e452014-07-22 10:10:35 +00003239StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
3240 Stmt *AStmt,
3241 SourceLocation StartLoc,
3242 SourceLocation EndLoc) {
3243 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003244 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00003245 // 1.2.2 OpenMP Language Terminology
3246 // Structured block - An executable statement with a single entry at the
3247 // top and a single exit at the bottom.
3248 // The point of exit cannot be a branch out of the structured block.
3249 // longjmp() and throw() must not violate the entry/exit criteria.
3250 // TODO further analysis of associated statements and clauses.
Alexey Bataevdea47612014-07-23 07:46:59 +00003251 OpenMPClauseKind AtomicKind = OMPC_unknown;
3252 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003253 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00003254 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00003255 C->getClauseKind() == OMPC_update ||
3256 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00003257 if (AtomicKind != OMPC_unknown) {
3258 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
3259 << SourceRange(C->getLocStart(), C->getLocEnd());
3260 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
3261 << getOpenMPClauseName(AtomicKind);
3262 } else {
3263 AtomicKind = C->getClauseKind();
3264 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003265 }
3266 }
3267 }
Alexey Bataev62cec442014-11-18 10:14:22 +00003268
Alexey Bataev459dec02014-07-24 06:46:57 +00003269 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00003270 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
3271 Body = EWC->getSubExpr();
3272
Alexey Bataev62cec442014-11-18 10:14:22 +00003273 Expr *X = nullptr;
3274 Expr *V = nullptr;
3275 Expr *E = nullptr;
3276 // OpenMP [2.12.6, atomic Construct]
3277 // In the next expressions:
3278 // * x and v (as applicable) are both l-value expressions with scalar type.
3279 // * During the execution of an atomic region, multiple syntactic
3280 // occurrences of x must designate the same storage location.
3281 // * Neither of v and expr (as applicable) may access the storage location
3282 // designated by x.
3283 // * Neither of x and expr (as applicable) may access the storage location
3284 // designated by v.
3285 // * expr is an expression with scalar type.
3286 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
3287 // * binop, binop=, ++, and -- are not overloaded operators.
3288 // * The expression x binop expr must be numerically equivalent to x binop
3289 // (expr). This requirement is satisfied if the operators in expr have
3290 // precedence greater than binop, or by using parentheses around expr or
3291 // subexpressions of expr.
3292 // * The expression expr binop x must be numerically equivalent to (expr)
3293 // binop x. This requirement is satisfied if the operators in expr have
3294 // precedence equal to or greater than binop, or by using parentheses around
3295 // expr or subexpressions of expr.
3296 // * For forms that allow multiple occurrences of x, the number of times
3297 // that x is evaluated is unspecified.
Alexey Bataevf33eba62014-11-28 07:21:40 +00003298 enum {
3299 NotAnExpression,
3300 NotAnAssignmentOp,
3301 NotAScalarType,
3302 NotAnLValue,
3303 NoError
3304 } ErrorFound = NoError;
Alexey Bataevdea47612014-07-23 07:46:59 +00003305 if (AtomicKind == OMPC_read) {
Alexey Bataev62cec442014-11-18 10:14:22 +00003306 SourceLocation ErrorLoc, NoteLoc;
3307 SourceRange ErrorRange, NoteRange;
3308 // If clause is read:
3309 // v = x;
3310 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
3311 auto AtomicBinOp =
3312 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
3313 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
3314 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
3315 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
3316 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
3317 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
3318 if (!X->isLValue() || !V->isLValue()) {
3319 auto NotLValueExpr = X->isLValue() ? V : X;
3320 ErrorFound = NotAnLValue;
3321 ErrorLoc = AtomicBinOp->getExprLoc();
3322 ErrorRange = AtomicBinOp->getSourceRange();
3323 NoteLoc = NotLValueExpr->getExprLoc();
3324 NoteRange = NotLValueExpr->getSourceRange();
3325 }
3326 } else if (!X->isInstantiationDependent() ||
3327 !V->isInstantiationDependent()) {
3328 auto NotScalarExpr =
3329 (X->isInstantiationDependent() || X->getType()->isScalarType())
3330 ? V
3331 : X;
3332 ErrorFound = NotAScalarType;
3333 ErrorLoc = AtomicBinOp->getExprLoc();
3334 ErrorRange = AtomicBinOp->getSourceRange();
3335 NoteLoc = NotScalarExpr->getExprLoc();
3336 NoteRange = NotScalarExpr->getSourceRange();
3337 }
3338 } else {
3339 ErrorFound = NotAnAssignmentOp;
3340 ErrorLoc = AtomicBody->getExprLoc();
3341 ErrorRange = AtomicBody->getSourceRange();
3342 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
3343 : AtomicBody->getExprLoc();
3344 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
3345 : AtomicBody->getSourceRange();
3346 }
3347 } else {
3348 ErrorFound = NotAnExpression;
3349 NoteLoc = ErrorLoc = Body->getLocStart();
3350 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00003351 }
Alexey Bataev62cec442014-11-18 10:14:22 +00003352 if (ErrorFound != NoError) {
3353 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
3354 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00003355 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
3356 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00003357 return StmtError();
3358 } else if (CurContext->isDependentContext())
3359 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00003360 } else if (AtomicKind == OMPC_write) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00003361 SourceLocation ErrorLoc, NoteLoc;
3362 SourceRange ErrorRange, NoteRange;
3363 // If clause is write:
3364 // x = expr;
3365 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
3366 auto AtomicBinOp =
3367 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
3368 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00003369 X = AtomicBinOp->getLHS();
3370 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00003371 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
3372 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
3373 if (!X->isLValue()) {
3374 ErrorFound = NotAnLValue;
3375 ErrorLoc = AtomicBinOp->getExprLoc();
3376 ErrorRange = AtomicBinOp->getSourceRange();
3377 NoteLoc = X->getExprLoc();
3378 NoteRange = X->getSourceRange();
3379 }
3380 } else if (!X->isInstantiationDependent() ||
3381 !E->isInstantiationDependent()) {
3382 auto NotScalarExpr =
3383 (X->isInstantiationDependent() || X->getType()->isScalarType())
3384 ? E
3385 : X;
3386 ErrorFound = NotAScalarType;
3387 ErrorLoc = AtomicBinOp->getExprLoc();
3388 ErrorRange = AtomicBinOp->getSourceRange();
3389 NoteLoc = NotScalarExpr->getExprLoc();
3390 NoteRange = NotScalarExpr->getSourceRange();
3391 }
3392 } else {
3393 ErrorFound = NotAnAssignmentOp;
3394 ErrorLoc = AtomicBody->getExprLoc();
3395 ErrorRange = AtomicBody->getSourceRange();
3396 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
3397 : AtomicBody->getExprLoc();
3398 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
3399 : AtomicBody->getSourceRange();
3400 }
3401 } else {
3402 ErrorFound = NotAnExpression;
3403 NoteLoc = ErrorLoc = Body->getLocStart();
3404 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00003405 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00003406 if (ErrorFound != NoError) {
3407 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
3408 << ErrorRange;
3409 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
3410 << NoteRange;
3411 return StmtError();
3412 } else if (CurContext->isDependentContext())
3413 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00003414 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev459dec02014-07-24 06:46:57 +00003415 if (!isa<Expr>(Body)) {
3416 Diag(Body->getLocStart(),
Alexey Bataev67a4f222014-07-23 10:25:33 +00003417 diag::err_omp_atomic_update_not_expression_statement)
3418 << (AtomicKind == OMPC_update);
3419 return StmtError();
3420 }
Alexey Bataev459dec02014-07-24 06:46:57 +00003421 } else if (AtomicKind == OMPC_capture) {
3422 if (isa<Expr>(Body) && !isa<BinaryOperator>(Body)) {
3423 Diag(Body->getLocStart(),
3424 diag::err_omp_atomic_capture_not_expression_statement);
3425 return StmtError();
3426 } else if (!isa<Expr>(Body) && !isa<CompoundStmt>(Body)) {
3427 Diag(Body->getLocStart(),
3428 diag::err_omp_atomic_capture_not_compound_statement);
3429 return StmtError();
3430 }
Alexey Bataevdea47612014-07-23 07:46:59 +00003431 }
Alexey Bataev0162e452014-07-22 10:10:35 +00003432
3433 getCurFunction()->setHasBranchProtectedScope();
3434
Alexey Bataev62cec442014-11-18 10:14:22 +00003435 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3436 X, V, E);
Alexey Bataev0162e452014-07-22 10:10:35 +00003437}
3438
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003439StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
3440 Stmt *AStmt,
3441 SourceLocation StartLoc,
3442 SourceLocation EndLoc) {
3443 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3444
Alexey Bataev13314bf2014-10-09 04:18:56 +00003445 // OpenMP [2.16, Nesting of Regions]
3446 // If specified, a teams construct must be contained within a target
3447 // construct. That target construct must contain no statements or directives
3448 // outside of the teams construct.
3449 if (DSAStack->hasInnerTeamsRegion()) {
3450 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
3451 bool OMPTeamsFound = true;
3452 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
3453 auto I = CS->body_begin();
3454 while (I != CS->body_end()) {
3455 auto OED = dyn_cast<OMPExecutableDirective>(*I);
3456 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
3457 OMPTeamsFound = false;
3458 break;
3459 }
3460 ++I;
3461 }
3462 assert(I != CS->body_end() && "Not found statement");
3463 S = *I;
3464 }
3465 if (!OMPTeamsFound) {
3466 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
3467 Diag(DSAStack->getInnerTeamsRegionLoc(),
3468 diag::note_omp_nested_teams_construct_here);
3469 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
3470 << isa<OMPExecutableDirective>(S);
3471 return StmtError();
3472 }
3473 }
3474
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003475 getCurFunction()->setHasBranchProtectedScope();
3476
3477 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3478}
3479
Alexey Bataev13314bf2014-10-09 04:18:56 +00003480StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
3481 Stmt *AStmt, SourceLocation StartLoc,
3482 SourceLocation EndLoc) {
3483 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3484 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3485 // 1.2.2 OpenMP Language Terminology
3486 // Structured block - An executable statement with a single entry at the
3487 // top and a single exit at the bottom.
3488 // The point of exit cannot be a branch out of the structured block.
3489 // longjmp() and throw() must not violate the entry/exit criteria.
3490 CS->getCapturedDecl()->setNothrow();
3491
3492 getCurFunction()->setHasBranchProtectedScope();
3493
3494 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3495}
3496
Alexey Bataeved09d242014-05-28 05:53:51 +00003497OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003498 SourceLocation StartLoc,
3499 SourceLocation LParenLoc,
3500 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003501 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003502 switch (Kind) {
3503 case OMPC_if:
3504 Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc);
3505 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00003506 case OMPC_final:
3507 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
3508 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00003509 case OMPC_num_threads:
3510 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
3511 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00003512 case OMPC_safelen:
3513 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
3514 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00003515 case OMPC_collapse:
3516 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
3517 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003518 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003519 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003520 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003521 case OMPC_private:
3522 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00003523 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003524 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00003525 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00003526 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00003527 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003528 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003529 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003530 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00003531 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003532 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003533 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003534 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00003535 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003536 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00003537 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00003538 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00003539 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003540 case OMPC_seq_cst:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003541 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003542 llvm_unreachable("Clause is not allowed.");
3543 }
3544 return Res;
3545}
3546
Alexey Bataeved09d242014-05-28 05:53:51 +00003547OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003548 SourceLocation LParenLoc,
3549 SourceLocation EndLoc) {
3550 Expr *ValExpr = Condition;
3551 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
3552 !Condition->isInstantiationDependent() &&
3553 !Condition->containsUnexpandedParameterPack()) {
3554 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00003555 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003556 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003557 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003558
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003559 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003560 }
3561
3562 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc);
3563}
3564
Alexey Bataev3778b602014-07-17 07:32:53 +00003565OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
3566 SourceLocation StartLoc,
3567 SourceLocation LParenLoc,
3568 SourceLocation EndLoc) {
3569 Expr *ValExpr = Condition;
3570 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
3571 !Condition->isInstantiationDependent() &&
3572 !Condition->containsUnexpandedParameterPack()) {
3573 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
3574 Condition->getExprLoc(), Condition);
3575 if (Val.isInvalid())
3576 return nullptr;
3577
3578 ValExpr = Val.get();
3579 }
3580
3581 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
3582}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003583ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
3584 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00003585 if (!Op)
3586 return ExprError();
3587
3588 class IntConvertDiagnoser : public ICEConvertDiagnoser {
3589 public:
3590 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00003591 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00003592 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
3593 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003594 return S.Diag(Loc, diag::err_omp_not_integral) << T;
3595 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003596 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
3597 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003598 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
3599 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003600 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
3601 QualType T,
3602 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003603 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
3604 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003605 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
3606 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003607 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00003608 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00003609 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003610 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
3611 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003612 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
3613 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003614 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
3615 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003616 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00003617 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00003618 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003619 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
3620 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003621 llvm_unreachable("conversion functions are permitted");
3622 }
3623 } ConvertDiagnoser;
3624 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
3625}
3626
3627OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
3628 SourceLocation StartLoc,
3629 SourceLocation LParenLoc,
3630 SourceLocation EndLoc) {
3631 Expr *ValExpr = NumThreads;
3632 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
Alexey Bataev568a8332014-03-06 06:15:19 +00003633 !NumThreads->containsUnexpandedParameterPack()) {
3634 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
3635 ExprResult Val =
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003636 PerformOpenMPImplicitIntegerConversion(NumThreadsLoc, NumThreads);
Alexey Bataev568a8332014-03-06 06:15:19 +00003637 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003638 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00003639
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003640 ValExpr = Val.get();
Alexey Bataev568a8332014-03-06 06:15:19 +00003641
3642 // OpenMP [2.5, Restrictions]
3643 // The num_threads expression must evaluate to a positive integer value.
3644 llvm::APSInt Result;
Alexey Bataeved09d242014-05-28 05:53:51 +00003645 if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() &&
3646 !Result.isStrictlyPositive()) {
Alexey Bataev568a8332014-03-06 06:15:19 +00003647 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
3648 << "num_threads" << NumThreads->getSourceRange();
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003649 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00003650 }
3651 }
3652
Alexey Bataeved09d242014-05-28 05:53:51 +00003653 return new (Context)
3654 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00003655}
3656
Alexey Bataev62c87d22014-03-21 04:51:18 +00003657ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
3658 OpenMPClauseKind CKind) {
3659 if (!E)
3660 return ExprError();
3661 if (E->isValueDependent() || E->isTypeDependent() ||
3662 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00003663 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00003664 llvm::APSInt Result;
3665 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
3666 if (ICE.isInvalid())
3667 return ExprError();
3668 if (!Result.isStrictlyPositive()) {
3669 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
3670 << getOpenMPClauseName(CKind) << E->getSourceRange();
3671 return ExprError();
3672 }
Alexander Musman09184fe2014-09-30 05:29:28 +00003673 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
3674 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
3675 << E->getSourceRange();
3676 return ExprError();
3677 }
Alexey Bataev62c87d22014-03-21 04:51:18 +00003678 return ICE;
3679}
3680
3681OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
3682 SourceLocation LParenLoc,
3683 SourceLocation EndLoc) {
3684 // OpenMP [2.8.1, simd construct, Description]
3685 // The parameter of the safelen clause must be a constant
3686 // positive integer expression.
3687 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
3688 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003689 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00003690 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003691 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00003692}
3693
Alexander Musman64d33f12014-06-04 07:53:32 +00003694OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
3695 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00003696 SourceLocation LParenLoc,
3697 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00003698 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00003699 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00003700 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00003701 // The parameter of the collapse clause must be a constant
3702 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00003703 ExprResult NumForLoopsResult =
3704 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
3705 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00003706 return nullptr;
3707 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00003708 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00003709}
3710
Alexey Bataeved09d242014-05-28 05:53:51 +00003711OMPClause *Sema::ActOnOpenMPSimpleClause(
3712 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
3713 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003714 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003715 switch (Kind) {
3716 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00003717 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00003718 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
3719 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003720 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003721 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00003722 Res = ActOnOpenMPProcBindClause(
3723 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
3724 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003725 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003726 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00003727 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00003728 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00003729 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00003730 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003731 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003732 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003733 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00003734 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00003735 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00003736 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00003737 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00003738 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003739 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003740 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003741 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00003742 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003743 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003744 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003745 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00003746 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003747 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00003748 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00003749 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00003750 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003751 case OMPC_seq_cst:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003752 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003753 llvm_unreachable("Clause is not allowed.");
3754 }
3755 return Res;
3756}
3757
3758OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
3759 SourceLocation KindKwLoc,
3760 SourceLocation StartLoc,
3761 SourceLocation LParenLoc,
3762 SourceLocation EndLoc) {
3763 if (Kind == OMPC_DEFAULT_unknown) {
3764 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00003765 static_assert(OMPC_DEFAULT_unknown > 0,
3766 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00003767 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00003768 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003769 Values += "'";
3770 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
3771 Values += "'";
3772 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00003773 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003774 Values += " or ";
3775 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00003776 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003777 break;
3778 default:
3779 Values += Sep;
3780 break;
3781 }
3782 }
3783 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00003784 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003785 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003786 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00003787 switch (Kind) {
3788 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003789 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003790 break;
3791 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003792 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003793 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003794 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003795 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00003796 break;
3797 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003798 return new (Context)
3799 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003800}
3801
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003802OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
3803 SourceLocation KindKwLoc,
3804 SourceLocation StartLoc,
3805 SourceLocation LParenLoc,
3806 SourceLocation EndLoc) {
3807 if (Kind == OMPC_PROC_BIND_unknown) {
3808 std::string Values;
3809 std::string Sep(", ");
3810 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
3811 Values += "'";
3812 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
3813 Values += "'";
3814 switch (i) {
3815 case OMPC_PROC_BIND_unknown - 2:
3816 Values += " or ";
3817 break;
3818 case OMPC_PROC_BIND_unknown - 1:
3819 break;
3820 default:
3821 Values += Sep;
3822 break;
3823 }
3824 }
3825 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00003826 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003827 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003828 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003829 return new (Context)
3830 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003831}
3832
Alexey Bataev56dafe82014-06-20 07:16:17 +00003833OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
3834 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
3835 SourceLocation StartLoc, SourceLocation LParenLoc,
3836 SourceLocation ArgumentLoc, SourceLocation CommaLoc,
3837 SourceLocation EndLoc) {
3838 OMPClause *Res = nullptr;
3839 switch (Kind) {
3840 case OMPC_schedule:
3841 Res = ActOnOpenMPScheduleClause(
3842 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
3843 LParenLoc, ArgumentLoc, CommaLoc, EndLoc);
3844 break;
3845 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00003846 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003847 case OMPC_num_threads:
3848 case OMPC_safelen:
3849 case OMPC_collapse:
3850 case OMPC_default:
3851 case OMPC_proc_bind:
3852 case OMPC_private:
3853 case OMPC_firstprivate:
3854 case OMPC_lastprivate:
3855 case OMPC_shared:
3856 case OMPC_reduction:
3857 case OMPC_linear:
3858 case OMPC_aligned:
3859 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003860 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003861 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00003862 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003863 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003864 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003865 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00003866 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003867 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00003868 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00003869 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00003870 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003871 case OMPC_seq_cst:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003872 case OMPC_unknown:
3873 llvm_unreachable("Clause is not allowed.");
3874 }
3875 return Res;
3876}
3877
3878OMPClause *Sema::ActOnOpenMPScheduleClause(
3879 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
3880 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
3881 SourceLocation EndLoc) {
3882 if (Kind == OMPC_SCHEDULE_unknown) {
3883 std::string Values;
3884 std::string Sep(", ");
3885 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) {
3886 Values += "'";
3887 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i);
3888 Values += "'";
3889 switch (i) {
3890 case OMPC_SCHEDULE_unknown - 2:
3891 Values += " or ";
3892 break;
3893 case OMPC_SCHEDULE_unknown - 1:
3894 break;
3895 default:
3896 Values += Sep;
3897 break;
3898 }
3899 }
3900 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
3901 << Values << getOpenMPClauseName(OMPC_schedule);
3902 return nullptr;
3903 }
3904 Expr *ValExpr = ChunkSize;
3905 if (ChunkSize) {
3906 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
3907 !ChunkSize->isInstantiationDependent() &&
3908 !ChunkSize->containsUnexpandedParameterPack()) {
3909 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
3910 ExprResult Val =
3911 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
3912 if (Val.isInvalid())
3913 return nullptr;
3914
3915 ValExpr = Val.get();
3916
3917 // OpenMP [2.7.1, Restrictions]
3918 // chunk_size must be a loop invariant integer expression with a positive
3919 // value.
3920 llvm::APSInt Result;
3921 if (ValExpr->isIntegerConstantExpr(Result, Context) &&
3922 Result.isSigned() && !Result.isStrictlyPositive()) {
3923 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
3924 << "schedule" << ChunkSize->getSourceRange();
3925 return nullptr;
3926 }
3927 }
3928 }
3929
3930 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc,
3931 EndLoc, Kind, ValExpr);
3932}
3933
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003934OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
3935 SourceLocation StartLoc,
3936 SourceLocation EndLoc) {
3937 OMPClause *Res = nullptr;
3938 switch (Kind) {
3939 case OMPC_ordered:
3940 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
3941 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00003942 case OMPC_nowait:
3943 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
3944 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003945 case OMPC_untied:
3946 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
3947 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003948 case OMPC_mergeable:
3949 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
3950 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003951 case OMPC_read:
3952 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
3953 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00003954 case OMPC_write:
3955 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
3956 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00003957 case OMPC_update:
3958 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
3959 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00003960 case OMPC_capture:
3961 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
3962 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003963 case OMPC_seq_cst:
3964 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
3965 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003966 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00003967 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003968 case OMPC_num_threads:
3969 case OMPC_safelen:
3970 case OMPC_collapse:
3971 case OMPC_schedule:
3972 case OMPC_private:
3973 case OMPC_firstprivate:
3974 case OMPC_lastprivate:
3975 case OMPC_shared:
3976 case OMPC_reduction:
3977 case OMPC_linear:
3978 case OMPC_aligned:
3979 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003980 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003981 case OMPC_default:
3982 case OMPC_proc_bind:
3983 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00003984 case OMPC_flush:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003985 case OMPC_unknown:
3986 llvm_unreachable("Clause is not allowed.");
3987 }
3988 return Res;
3989}
3990
3991OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
3992 SourceLocation EndLoc) {
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003993 DSAStack->setOrderedRegion();
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003994 return new (Context) OMPOrderedClause(StartLoc, EndLoc);
3995}
3996
Alexey Bataev236070f2014-06-20 11:19:47 +00003997OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
3998 SourceLocation EndLoc) {
3999 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
4000}
4001
Alexey Bataev7aea99a2014-07-17 12:19:31 +00004002OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
4003 SourceLocation EndLoc) {
4004 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
4005}
4006
Alexey Bataev74ba3a52014-07-17 12:47:03 +00004007OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
4008 SourceLocation EndLoc) {
4009 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
4010}
4011
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004012OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
4013 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004014 return new (Context) OMPReadClause(StartLoc, EndLoc);
4015}
4016
Alexey Bataevdea47612014-07-23 07:46:59 +00004017OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
4018 SourceLocation EndLoc) {
4019 return new (Context) OMPWriteClause(StartLoc, EndLoc);
4020}
4021
Alexey Bataev67a4f222014-07-23 10:25:33 +00004022OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
4023 SourceLocation EndLoc) {
4024 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
4025}
4026
Alexey Bataev459dec02014-07-24 06:46:57 +00004027OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
4028 SourceLocation EndLoc) {
4029 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
4030}
4031
Alexey Bataev82bad8b2014-07-24 08:55:34 +00004032OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
4033 SourceLocation EndLoc) {
4034 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
4035}
4036
Alexey Bataevc5e02582014-06-16 07:08:35 +00004037OMPClause *Sema::ActOnOpenMPVarListClause(
4038 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
4039 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
4040 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
4041 const DeclarationNameInfo &ReductionId) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004042 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004043 switch (Kind) {
4044 case OMPC_private:
4045 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
4046 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004047 case OMPC_firstprivate:
4048 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
4049 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00004050 case OMPC_lastprivate:
4051 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
4052 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00004053 case OMPC_shared:
4054 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
4055 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00004056 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00004057 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
4058 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00004059 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00004060 case OMPC_linear:
4061 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
4062 ColonLoc, EndLoc);
4063 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00004064 case OMPC_aligned:
4065 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
4066 ColonLoc, EndLoc);
4067 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004068 case OMPC_copyin:
4069 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
4070 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00004071 case OMPC_copyprivate:
4072 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
4073 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00004074 case OMPC_flush:
4075 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
4076 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004077 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00004078 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00004079 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00004080 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00004081 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004082 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004083 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00004084 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004085 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00004086 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00004087 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00004088 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004089 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004090 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00004091 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00004092 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00004093 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00004094 case OMPC_seq_cst:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004095 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004096 llvm_unreachable("Clause is not allowed.");
4097 }
4098 return Res;
4099}
4100
4101OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
4102 SourceLocation StartLoc,
4103 SourceLocation LParenLoc,
4104 SourceLocation EndLoc) {
4105 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00004106 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00004107 for (auto &RefExpr : VarList) {
4108 assert(RefExpr && "NULL expr in OpenMP private clause.");
4109 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004110 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00004111 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00004112 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004113 continue;
4114 }
4115
Alexey Bataeved09d242014-05-28 05:53:51 +00004116 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004117 // OpenMP [2.1, C/C++]
4118 // A list item is a variable name.
4119 // OpenMP [2.9.3.3, Restrictions, p.1]
4120 // A variable that is part of another variable (as an array or
4121 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00004122 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004123 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00004124 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004125 continue;
4126 }
4127 Decl *D = DE->getDecl();
4128 VarDecl *VD = cast<VarDecl>(D);
4129
4130 QualType Type = VD->getType();
4131 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
4132 // It will be analyzed later.
4133 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00004134 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004135 continue;
4136 }
4137
4138 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
4139 // A variable that appears in a private clause must not have an incomplete
4140 // type or a reference type.
4141 if (RequireCompleteType(ELoc, Type,
4142 diag::err_omp_private_incomplete_type)) {
4143 continue;
4144 }
4145 if (Type->isReferenceType()) {
4146 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00004147 << getOpenMPClauseName(OMPC_private) << Type;
4148 bool IsDecl =
4149 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4150 Diag(VD->getLocation(),
4151 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4152 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004153 continue;
4154 }
4155
4156 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
4157 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00004158 // clause requires an accessible, unambiguous default constructor for the
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004159 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00004160 while (Type->isArrayType()) {
4161 Type = cast<ArrayType>(Type.getTypePtr())->getElementType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004162 }
4163
Alexey Bataev758e55e2013-09-06 18:03:48 +00004164 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
4165 // in a Construct]
4166 // Variables with the predetermined data-sharing attributes may not be
4167 // listed in data-sharing attributes clauses, except for the cases
4168 // listed below. For these exceptions only, listing a predetermined
4169 // variable in a data-sharing attribute clause is allowed and overrides
4170 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004171 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004172 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00004173 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
4174 << getOpenMPClauseName(OMPC_private);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004175 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004176 continue;
4177 }
4178
Alexey Bataev03b340a2014-10-21 03:16:40 +00004179 // Generate helper private variable and initialize it with the default
4180 // value. The address of the original variable is replaced by the address of
4181 // the new private variable in CodeGen. This new variable is not added to
4182 // IdResolver, so the code in the OpenMP region uses original variable for
4183 // proper diagnostics.
4184 auto VDPrivate =
4185 VarDecl::Create(Context, CurContext, DE->getLocStart(),
4186 DE->getExprLoc(), VD->getIdentifier(), VD->getType(),
4187 VD->getTypeSourceInfo(), /*S*/ SC_Auto);
4188 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto*/ false);
4189 if (VDPrivate->isInvalidDecl())
4190 continue;
4191 CurContext->addDecl(VDPrivate);
Alexey Bataev07649fb2014-12-16 08:01:48 +00004192 auto VDPrivateRefExpr =
4193 DeclRefExpr::Create(Context, /*QualifierLoc*/ NestedNameSpecifierLoc(),
4194 /*TemplateKWLoc*/ SourceLocation(), VDPrivate,
Alexey Bataev19acc3d2015-01-12 10:17:46 +00004195 /*RefersToEnclosingVariableOrCapture*/ false,
Alexey Bataev07649fb2014-12-16 08:01:48 +00004196 /*NameLoc*/ SourceLocation(), DE->getType(),
4197 /*VK*/ VK_LValue);
Alexey Bataev03b340a2014-10-21 03:16:40 +00004198
Alexey Bataev758e55e2013-09-06 18:03:48 +00004199 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004200 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00004201 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004202 }
4203
Alexey Bataeved09d242014-05-28 05:53:51 +00004204 if (Vars.empty())
4205 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004206
Alexey Bataev03b340a2014-10-21 03:16:40 +00004207 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
4208 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004209}
4210
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004211namespace {
4212class DiagsUninitializedSeveretyRAII {
4213private:
4214 DiagnosticsEngine &Diags;
4215 SourceLocation SavedLoc;
4216 bool IsIgnored;
4217
4218public:
4219 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
4220 bool IsIgnored)
4221 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
4222 if (!IsIgnored) {
4223 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
4224 /*Map*/ diag::Severity::Ignored, Loc);
4225 }
4226 }
4227 ~DiagsUninitializedSeveretyRAII() {
4228 if (!IsIgnored)
4229 Diags.popMappings(SavedLoc);
4230 }
4231};
4232}
4233
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004234OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
4235 SourceLocation StartLoc,
4236 SourceLocation LParenLoc,
4237 SourceLocation EndLoc) {
4238 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004239 SmallVector<Expr *, 8> PrivateCopies;
4240 SmallVector<Expr *, 8> Inits;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004241 bool IsImplicitClause =
4242 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
4243 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
4244
Alexey Bataeved09d242014-05-28 05:53:51 +00004245 for (auto &RefExpr : VarList) {
4246 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
4247 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004248 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00004249 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004250 PrivateCopies.push_back(nullptr);
4251 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004252 continue;
4253 }
4254
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004255 SourceLocation ELoc =
4256 IsImplicitClause ? ImplicitClauseLoc : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004257 // OpenMP [2.1, C/C++]
4258 // A list item is a variable name.
4259 // OpenMP [2.9.3.3, Restrictions, p.1]
4260 // A variable that is part of another variable (as an array or
4261 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00004262 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004263 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00004264 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004265 continue;
4266 }
4267 Decl *D = DE->getDecl();
4268 VarDecl *VD = cast<VarDecl>(D);
4269
4270 QualType Type = VD->getType();
4271 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
4272 // It will be analyzed later.
4273 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004274 PrivateCopies.push_back(nullptr);
4275 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004276 continue;
4277 }
4278
4279 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
4280 // A variable that appears in a private clause must not have an incomplete
4281 // type or a reference type.
4282 if (RequireCompleteType(ELoc, Type,
4283 diag::err_omp_firstprivate_incomplete_type)) {
4284 continue;
4285 }
4286 if (Type->isReferenceType()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004287 if (IsImplicitClause) {
4288 Diag(ImplicitClauseLoc,
4289 diag::err_omp_task_predetermined_firstprivate_ref_type_arg)
4290 << Type;
4291 Diag(RefExpr->getExprLoc(), diag::note_used_here);
4292 } else {
4293 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
4294 << getOpenMPClauseName(OMPC_firstprivate) << Type;
4295 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004296 bool IsDecl =
4297 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4298 Diag(VD->getLocation(),
4299 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4300 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004301 continue;
4302 }
4303
4304 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
4305 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00004306 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004307 // class type.
4308 Type = Context.getBaseElementType(Type);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004309
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004310 // If an implicit firstprivate variable found it was checked already.
4311 if (!IsImplicitClause) {
4312 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004313 Type = Type.getNonReferenceType().getCanonicalType();
4314 bool IsConstant = Type.isConstant(Context);
4315 Type = Context.getBaseElementType(Type);
4316 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
4317 // A list item that specifies a given variable may not appear in more
4318 // than one clause on the same directive, except that a variable may be
4319 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004320 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00004321 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004322 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00004323 << getOpenMPClauseName(DVar.CKind)
4324 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004325 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004326 continue;
4327 }
4328
4329 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
4330 // in a Construct]
4331 // Variables with the predetermined data-sharing attributes may not be
4332 // listed in data-sharing attributes clauses, except for the cases
4333 // listed below. For these exceptions only, listing a predetermined
4334 // variable in a data-sharing attribute clause is allowed and overrides
4335 // the variable's predetermined data-sharing attributes.
4336 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
4337 // in a Construct, C/C++, p.2]
4338 // Variables with const-qualified type having no mutable member may be
4339 // listed in a firstprivate clause, even if they are static data members.
4340 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
4341 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
4342 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00004343 << getOpenMPClauseName(DVar.CKind)
4344 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004345 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004346 continue;
4347 }
4348
Alexey Bataevf29276e2014-06-18 04:14:57 +00004349 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004350 // OpenMP [2.9.3.4, Restrictions, p.2]
4351 // A list item that is private within a parallel region must not appear
4352 // in a firstprivate clause on a worksharing construct if any of the
4353 // worksharing regions arising from the worksharing construct ever bind
4354 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00004355 if (isOpenMPWorksharingDirective(CurrDir) &&
4356 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004357 DVar = DSAStack->getImplicitDSA(VD, true);
4358 if (DVar.CKind != OMPC_shared &&
4359 (isOpenMPParallelDirective(DVar.DKind) ||
4360 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00004361 Diag(ELoc, diag::err_omp_required_access)
4362 << getOpenMPClauseName(OMPC_firstprivate)
4363 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004364 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004365 continue;
4366 }
4367 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004368 // OpenMP [2.9.3.4, Restrictions, p.3]
4369 // A list item that appears in a reduction clause of a parallel construct
4370 // must not appear in a firstprivate clause on a worksharing or task
4371 // construct if any of the worksharing or task regions arising from the
4372 // worksharing or task construct ever bind to any of the parallel regions
4373 // arising from the parallel construct.
4374 // OpenMP [2.9.3.4, Restrictions, p.4]
4375 // A list item that appears in a reduction clause in worksharing
4376 // construct must not appear in a firstprivate clause in a task construct
4377 // encountered during execution of any of the worksharing regions arising
4378 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004379 if (CurrDir == OMPD_task) {
4380 DVar =
4381 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
4382 [](OpenMPDirectiveKind K) -> bool {
4383 return isOpenMPParallelDirective(K) ||
4384 isOpenMPWorksharingDirective(K);
4385 },
4386 false);
4387 if (DVar.CKind == OMPC_reduction &&
4388 (isOpenMPParallelDirective(DVar.DKind) ||
4389 isOpenMPWorksharingDirective(DVar.DKind))) {
4390 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
4391 << getOpenMPDirectiveName(DVar.DKind);
4392 ReportOriginalDSA(*this, DSAStack, VD, DVar);
4393 continue;
4394 }
4395 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004396 }
4397
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004398 Type = Type.getUnqualifiedType();
4399 auto VDPrivate = VarDecl::Create(Context, CurContext, DE->getLocStart(),
4400 ELoc, VD->getIdentifier(), VD->getType(),
4401 VD->getTypeSourceInfo(), /*S*/ SC_Auto);
4402 // Generate helper private variable and initialize it with the value of the
4403 // original variable. The address of the original variable is replaced by
4404 // the address of the new private variable in the CodeGen. This new variable
4405 // is not added to IdResolver, so the code in the OpenMP region uses
4406 // original variable for proper diagnostics and variable capturing.
4407 Expr *VDInitRefExpr = nullptr;
4408 // For arrays generate initializer for single element and replace it by the
4409 // original array element in CodeGen.
4410 if (DE->getType()->isArrayType()) {
4411 auto VDInit = VarDecl::Create(Context, CurContext, DE->getLocStart(),
4412 ELoc, VD->getIdentifier(), Type,
4413 VD->getTypeSourceInfo(), /*S*/ SC_Auto);
4414 CurContext->addHiddenDecl(VDInit);
4415 VDInitRefExpr = DeclRefExpr::Create(
4416 Context, /*QualifierLoc*/ NestedNameSpecifierLoc(),
4417 /*TemplateKWLoc*/ SourceLocation(), VDInit,
Alexey Bataev19acc3d2015-01-12 10:17:46 +00004418 /*RefersToEnclosingVariableOrCapture*/ true, ELoc, Type,
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004419 /*VK*/ VK_LValue);
4420 VDInit->setIsUsed();
4421 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
4422 InitializedEntity Entity = InitializedEntity::InitializeVariable(VDInit);
4423 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
4424
4425 InitializationSequence InitSeq(*this, Entity, Kind, Init);
4426 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
4427 if (Result.isInvalid())
4428 VDPrivate->setInvalidDecl();
4429 else
4430 VDPrivate->setInit(Result.getAs<Expr>());
4431 } else {
Alexey Bataevf841bd92014-12-16 07:00:22 +00004432 AddInitializerToDecl(
Alexey Bataev19acc3d2015-01-12 10:17:46 +00004433 VDPrivate,
4434 DefaultLvalueConversion(
4435 DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
4436 SourceLocation(), DE->getDecl(),
4437 /*RefersToEnclosingVariableOrCapture=*/true,
4438 DE->getExprLoc(), DE->getType(),
4439 /*VK=*/VK_LValue)).get(),
Alexey Bataevf841bd92014-12-16 07:00:22 +00004440 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004441 }
4442 if (VDPrivate->isInvalidDecl()) {
4443 if (IsImplicitClause) {
4444 Diag(DE->getExprLoc(),
4445 diag::note_omp_task_predetermined_firstprivate_here);
4446 }
4447 continue;
4448 }
4449 CurContext->addDecl(VDPrivate);
Alexey Bataev19acc3d2015-01-12 10:17:46 +00004450 auto VDPrivateRefExpr =
4451 DeclRefExpr::Create(Context, /*QualifierLoc*/ NestedNameSpecifierLoc(),
4452 /*TemplateKWLoc*/ SourceLocation(), VDPrivate,
4453 /*RefersToEnclosingVariableOrCapture*/ false,
4454 DE->getLocStart(), DE->getType(),
4455 /*VK*/ VK_LValue);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004456 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
4457 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004458 PrivateCopies.push_back(VDPrivateRefExpr);
4459 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004460 }
4461
Alexey Bataeved09d242014-05-28 05:53:51 +00004462 if (Vars.empty())
4463 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004464
4465 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004466 Vars, PrivateCopies, Inits);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004467}
4468
Alexander Musman1bb328c2014-06-04 13:06:39 +00004469OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
4470 SourceLocation StartLoc,
4471 SourceLocation LParenLoc,
4472 SourceLocation EndLoc) {
4473 SmallVector<Expr *, 8> Vars;
4474 for (auto &RefExpr : VarList) {
4475 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
4476 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
4477 // It will be analyzed later.
4478 Vars.push_back(RefExpr);
4479 continue;
4480 }
4481
4482 SourceLocation ELoc = RefExpr->getExprLoc();
4483 // OpenMP [2.1, C/C++]
4484 // A list item is a variable name.
4485 // OpenMP [2.14.3.5, Restrictions, p.1]
4486 // A variable that is part of another variable (as an array or structure
4487 // element) cannot appear in a lastprivate clause.
4488 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
4489 if (!DE || !isa<VarDecl>(DE->getDecl())) {
4490 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
4491 continue;
4492 }
4493 Decl *D = DE->getDecl();
4494 VarDecl *VD = cast<VarDecl>(D);
4495
4496 QualType Type = VD->getType();
4497 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
4498 // It will be analyzed later.
4499 Vars.push_back(DE);
4500 continue;
4501 }
4502
4503 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
4504 // A variable that appears in a lastprivate clause must not have an
4505 // incomplete type or a reference type.
4506 if (RequireCompleteType(ELoc, Type,
4507 diag::err_omp_lastprivate_incomplete_type)) {
4508 continue;
4509 }
4510 if (Type->isReferenceType()) {
4511 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
4512 << getOpenMPClauseName(OMPC_lastprivate) << Type;
4513 bool IsDecl =
4514 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4515 Diag(VD->getLocation(),
4516 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4517 << VD;
4518 continue;
4519 }
4520
4521 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4522 // in a Construct]
4523 // Variables with the predetermined data-sharing attributes may not be
4524 // listed in data-sharing attributes clauses, except for the cases
4525 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004526 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00004527 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
4528 DVar.CKind != OMPC_firstprivate &&
4529 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
4530 Diag(ELoc, diag::err_omp_wrong_dsa)
4531 << getOpenMPClauseName(DVar.CKind)
4532 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004533 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00004534 continue;
4535 }
4536
Alexey Bataevf29276e2014-06-18 04:14:57 +00004537 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
4538 // OpenMP [2.14.3.5, Restrictions, p.2]
4539 // A list item that is private within a parallel region, or that appears in
4540 // the reduction clause of a parallel construct, must not appear in a
4541 // lastprivate clause on a worksharing construct if any of the corresponding
4542 // worksharing regions ever binds to any of the corresponding parallel
4543 // regions.
Alexey Bataev549210e2014-06-24 04:39:47 +00004544 if (isOpenMPWorksharingDirective(CurrDir) &&
4545 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004546 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004547 if (DVar.CKind != OMPC_shared) {
4548 Diag(ELoc, diag::err_omp_required_access)
4549 << getOpenMPClauseName(OMPC_lastprivate)
4550 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004551 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004552 continue;
4553 }
4554 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00004555 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00004556 // A variable of class type (or array thereof) that appears in a
4557 // lastprivate clause requires an accessible, unambiguous default
4558 // constructor for the class type, unless the list item is also specified
4559 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00004560 // A variable of class type (or array thereof) that appears in a
4561 // lastprivate clause requires an accessible, unambiguous copy assignment
4562 // operator for the class type.
4563 while (Type.getNonReferenceType()->isArrayType())
4564 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
4565 ->getElementType();
4566 CXXRecordDecl *RD = getLangOpts().CPlusPlus
4567 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
4568 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00004569 // FIXME This code must be replaced by actual copying and destructing of the
4570 // lastprivate variable.
Alexander Musman1bb328c2014-06-04 13:06:39 +00004571 if (RD) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00004572 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
4573 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00004574 if (MD) {
4575 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
4576 MD->isDeleted()) {
4577 Diag(ELoc, diag::err_omp_required_method)
4578 << getOpenMPClauseName(OMPC_lastprivate) << 2;
4579 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4580 VarDecl::DeclarationOnly;
4581 Diag(VD->getLocation(),
4582 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4583 << VD;
4584 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4585 continue;
4586 }
4587 MarkFunctionReferenced(ELoc, MD);
4588 DiagnoseUseOfDecl(MD, ELoc);
Alexander Musman1bb328c2014-06-04 13:06:39 +00004589 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00004590
4591 CXXDestructorDecl *DD = RD->getDestructor();
4592 if (DD) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00004593 PartialDiagnostic PD =
4594 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexander Musman1bb328c2014-06-04 13:06:39 +00004595 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
4596 DD->isDeleted()) {
4597 Diag(ELoc, diag::err_omp_required_method)
4598 << getOpenMPClauseName(OMPC_lastprivate) << 4;
4599 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4600 VarDecl::DeclarationOnly;
4601 Diag(VD->getLocation(),
4602 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4603 << VD;
4604 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4605 continue;
4606 }
4607 MarkFunctionReferenced(ELoc, DD);
4608 DiagnoseUseOfDecl(DD, ELoc);
4609 }
4610 }
4611
Alexey Bataevf29276e2014-06-18 04:14:57 +00004612 if (DVar.CKind != OMPC_firstprivate)
4613 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00004614 Vars.push_back(DE);
4615 }
4616
4617 if (Vars.empty())
4618 return nullptr;
4619
4620 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
4621 Vars);
4622}
4623
Alexey Bataev758e55e2013-09-06 18:03:48 +00004624OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
4625 SourceLocation StartLoc,
4626 SourceLocation LParenLoc,
4627 SourceLocation EndLoc) {
4628 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00004629 for (auto &RefExpr : VarList) {
4630 assert(RefExpr && "NULL expr in OpenMP shared clause.");
4631 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00004632 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00004633 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004634 continue;
4635 }
4636
Alexey Bataeved09d242014-05-28 05:53:51 +00004637 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00004638 // OpenMP [2.1, C/C++]
4639 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00004640 // OpenMP [2.14.3.2, Restrictions, p.1]
4641 // A variable that is part of another variable (as an array or structure
4642 // element) cannot appear in a shared unless it is a static data member
4643 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00004644 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004645 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00004646 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00004647 continue;
4648 }
4649 Decl *D = DE->getDecl();
4650 VarDecl *VD = cast<VarDecl>(D);
4651
4652 QualType Type = VD->getType();
4653 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
4654 // It will be analyzed later.
4655 Vars.push_back(DE);
4656 continue;
4657 }
4658
4659 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
4660 // in a Construct]
4661 // Variables with the predetermined data-sharing attributes may not be
4662 // listed in data-sharing attributes clauses, except for the cases
4663 // listed below. For these exceptions only, listing a predetermined
4664 // variable in a data-sharing attribute clause is allowed and overrides
4665 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004666 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00004667 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
4668 DVar.RefExpr) {
4669 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
4670 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004671 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004672 continue;
4673 }
4674
4675 DSAStack->addDSA(VD, DE, OMPC_shared);
4676 Vars.push_back(DE);
4677 }
4678
Alexey Bataeved09d242014-05-28 05:53:51 +00004679 if (Vars.empty())
4680 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00004681
4682 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
4683}
4684
Alexey Bataevc5e02582014-06-16 07:08:35 +00004685namespace {
4686class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
4687 DSAStackTy *Stack;
4688
4689public:
4690 bool VisitDeclRefExpr(DeclRefExpr *E) {
4691 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004692 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00004693 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
4694 return false;
4695 if (DVar.CKind != OMPC_unknown)
4696 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00004697 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004698 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004699 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00004700 return true;
4701 return false;
4702 }
4703 return false;
4704 }
4705 bool VisitStmt(Stmt *S) {
4706 for (auto Child : S->children()) {
4707 if (Child && Visit(Child))
4708 return true;
4709 }
4710 return false;
4711 }
Alexey Bataev23b69422014-06-18 07:08:49 +00004712 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00004713};
Alexey Bataev23b69422014-06-18 07:08:49 +00004714} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00004715
4716OMPClause *Sema::ActOnOpenMPReductionClause(
4717 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
4718 SourceLocation ColonLoc, SourceLocation EndLoc,
4719 CXXScopeSpec &ReductionIdScopeSpec,
4720 const DeclarationNameInfo &ReductionId) {
4721 // TODO: Allow scope specification search when 'declare reduction' is
4722 // supported.
4723 assert(ReductionIdScopeSpec.isEmpty() &&
4724 "No support for scoped reduction identifiers yet.");
4725
4726 auto DN = ReductionId.getName();
4727 auto OOK = DN.getCXXOverloadedOperator();
4728 BinaryOperatorKind BOK = BO_Comma;
4729
4730 // OpenMP [2.14.3.6, reduction clause]
4731 // C
4732 // reduction-identifier is either an identifier or one of the following
4733 // operators: +, -, *, &, |, ^, && and ||
4734 // C++
4735 // reduction-identifier is either an id-expression or one of the following
4736 // operators: +, -, *, &, |, ^, && and ||
4737 // FIXME: Only 'min' and 'max' identifiers are supported for now.
4738 switch (OOK) {
4739 case OO_Plus:
4740 case OO_Minus:
4741 BOK = BO_AddAssign;
4742 break;
4743 case OO_Star:
4744 BOK = BO_MulAssign;
4745 break;
4746 case OO_Amp:
4747 BOK = BO_AndAssign;
4748 break;
4749 case OO_Pipe:
4750 BOK = BO_OrAssign;
4751 break;
4752 case OO_Caret:
4753 BOK = BO_XorAssign;
4754 break;
4755 case OO_AmpAmp:
4756 BOK = BO_LAnd;
4757 break;
4758 case OO_PipePipe:
4759 BOK = BO_LOr;
4760 break;
4761 default:
4762 if (auto II = DN.getAsIdentifierInfo()) {
4763 if (II->isStr("max"))
4764 BOK = BO_GT;
4765 else if (II->isStr("min"))
4766 BOK = BO_LT;
4767 }
4768 break;
4769 }
4770 SourceRange ReductionIdRange;
4771 if (ReductionIdScopeSpec.isValid()) {
4772 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
4773 }
4774 ReductionIdRange.setEnd(ReductionId.getEndLoc());
4775 if (BOK == BO_Comma) {
4776 // Not allowed reduction identifier is found.
4777 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
4778 << ReductionIdRange;
4779 return nullptr;
4780 }
4781
4782 SmallVector<Expr *, 8> Vars;
4783 for (auto RefExpr : VarList) {
4784 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
4785 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
4786 // It will be analyzed later.
4787 Vars.push_back(RefExpr);
4788 continue;
4789 }
4790
4791 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
4792 RefExpr->isInstantiationDependent() ||
4793 RefExpr->containsUnexpandedParameterPack()) {
4794 // It will be analyzed later.
4795 Vars.push_back(RefExpr);
4796 continue;
4797 }
4798
4799 auto ELoc = RefExpr->getExprLoc();
4800 auto ERange = RefExpr->getSourceRange();
4801 // OpenMP [2.1, C/C++]
4802 // A list item is a variable or array section, subject to the restrictions
4803 // specified in Section 2.4 on page 42 and in each of the sections
4804 // describing clauses and directives for which a list appears.
4805 // OpenMP [2.14.3.3, Restrictions, p.1]
4806 // A variable that is part of another variable (as an array or
4807 // structure element) cannot appear in a private clause.
4808 auto DE = dyn_cast<DeclRefExpr>(RefExpr);
4809 if (!DE || !isa<VarDecl>(DE->getDecl())) {
4810 Diag(ELoc, diag::err_omp_expected_var_name) << ERange;
4811 continue;
4812 }
4813 auto D = DE->getDecl();
4814 auto VD = cast<VarDecl>(D);
4815 auto Type = VD->getType();
4816 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
4817 // A variable that appears in a private clause must not have an incomplete
4818 // type or a reference type.
4819 if (RequireCompleteType(ELoc, Type,
4820 diag::err_omp_reduction_incomplete_type))
4821 continue;
4822 // OpenMP [2.14.3.6, reduction clause, Restrictions]
4823 // Arrays may not appear in a reduction clause.
4824 if (Type.getNonReferenceType()->isArrayType()) {
4825 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
4826 bool IsDecl =
4827 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4828 Diag(VD->getLocation(),
4829 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4830 << VD;
4831 continue;
4832 }
4833 // OpenMP [2.14.3.6, reduction clause, Restrictions]
4834 // A list item that appears in a reduction clause must not be
4835 // const-qualified.
4836 if (Type.getNonReferenceType().isConstant(Context)) {
4837 Diag(ELoc, diag::err_omp_const_variable)
4838 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
4839 bool IsDecl =
4840 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4841 Diag(VD->getLocation(),
4842 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4843 << VD;
4844 continue;
4845 }
4846 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
4847 // If a list-item is a reference type then it must bind to the same object
4848 // for all threads of the team.
4849 VarDecl *VDDef = VD->getDefinition();
4850 if (Type->isReferenceType() && VDDef) {
4851 DSARefChecker Check(DSAStack);
4852 if (Check.Visit(VDDef->getInit())) {
4853 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
4854 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
4855 continue;
4856 }
4857 }
4858 // OpenMP [2.14.3.6, reduction clause, Restrictions]
4859 // The type of a list item that appears in a reduction clause must be valid
4860 // for the reduction-identifier. For a max or min reduction in C, the type
4861 // of the list item must be an allowed arithmetic data type: char, int,
4862 // float, double, or _Bool, possibly modified with long, short, signed, or
4863 // unsigned. For a max or min reduction in C++, the type of the list item
4864 // must be an allowed arithmetic data type: char, wchar_t, int, float,
4865 // double, or bool, possibly modified with long, short, signed, or unsigned.
4866 if ((BOK == BO_GT || BOK == BO_LT) &&
4867 !(Type->isScalarType() ||
4868 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
4869 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
4870 << getLangOpts().CPlusPlus;
4871 bool IsDecl =
4872 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4873 Diag(VD->getLocation(),
4874 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4875 << VD;
4876 continue;
4877 }
4878 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
4879 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
4880 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
4881 bool IsDecl =
4882 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4883 Diag(VD->getLocation(),
4884 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4885 << VD;
4886 continue;
4887 }
4888 bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
4889 getDiagnostics().setSuppressAllDiagnostics(true);
4890 ExprResult ReductionOp =
4891 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
4892 RefExpr, RefExpr);
4893 getDiagnostics().setSuppressAllDiagnostics(Suppress);
4894 if (ReductionOp.isInvalid()) {
4895 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
Alexey Bataev23b69422014-06-18 07:08:49 +00004896 << ReductionIdRange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00004897 bool IsDecl =
4898 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4899 Diag(VD->getLocation(),
4900 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4901 << VD;
4902 continue;
4903 }
4904
4905 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4906 // in a Construct]
4907 // Variables with the predetermined data-sharing attributes may not be
4908 // listed in data-sharing attributes clauses, except for the cases
4909 // listed below. For these exceptions only, listing a predetermined
4910 // variable in a data-sharing attribute clause is allowed and overrides
4911 // the variable's predetermined data-sharing attributes.
4912 // OpenMP [2.14.3.6, Restrictions, p.3]
4913 // Any number of reduction clauses can be specified on the directive,
4914 // but a list item can appear only once in the reduction clauses for that
4915 // directive.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004916 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00004917 if (DVar.CKind == OMPC_reduction) {
4918 Diag(ELoc, diag::err_omp_once_referenced)
4919 << getOpenMPClauseName(OMPC_reduction);
4920 if (DVar.RefExpr) {
4921 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
4922 }
4923 } else if (DVar.CKind != OMPC_unknown) {
4924 Diag(ELoc, diag::err_omp_wrong_dsa)
4925 << getOpenMPClauseName(DVar.CKind)
4926 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004927 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00004928 continue;
4929 }
4930
4931 // OpenMP [2.14.3.6, Restrictions, p.1]
4932 // A list item that appears in a reduction clause of a worksharing
4933 // construct must be shared in the parallel regions to which any of the
4934 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf29276e2014-06-18 04:14:57 +00004935 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataev549210e2014-06-24 04:39:47 +00004936 if (isOpenMPWorksharingDirective(CurrDir) &&
4937 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004938 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004939 if (DVar.CKind != OMPC_shared) {
4940 Diag(ELoc, diag::err_omp_required_access)
4941 << getOpenMPClauseName(OMPC_reduction)
4942 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004943 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004944 continue;
4945 }
4946 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00004947
4948 CXXRecordDecl *RD = getLangOpts().CPlusPlus
4949 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
4950 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00004951 // FIXME This code must be replaced by actual constructing/destructing of
4952 // the reduction variable.
Alexey Bataevc5e02582014-06-16 07:08:35 +00004953 if (RD) {
4954 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
4955 PartialDiagnostic PD =
4956 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataev23b69422014-06-18 07:08:49 +00004957 if (!CD ||
4958 CheckConstructorAccess(ELoc, CD,
4959 InitializedEntity::InitializeTemporary(Type),
4960 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevc5e02582014-06-16 07:08:35 +00004961 CD->isDeleted()) {
4962 Diag(ELoc, diag::err_omp_required_method)
4963 << getOpenMPClauseName(OMPC_reduction) << 0;
4964 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4965 VarDecl::DeclarationOnly;
4966 Diag(VD->getLocation(),
4967 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4968 << VD;
4969 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4970 continue;
4971 }
4972 MarkFunctionReferenced(ELoc, CD);
4973 DiagnoseUseOfDecl(CD, ELoc);
4974
4975 CXXDestructorDecl *DD = RD->getDestructor();
4976 if (DD) {
4977 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
4978 DD->isDeleted()) {
4979 Diag(ELoc, diag::err_omp_required_method)
4980 << getOpenMPClauseName(OMPC_reduction) << 4;
4981 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4982 VarDecl::DeclarationOnly;
4983 Diag(VD->getLocation(),
4984 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4985 << VD;
4986 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4987 continue;
4988 }
4989 MarkFunctionReferenced(ELoc, DD);
4990 DiagnoseUseOfDecl(DD, ELoc);
4991 }
4992 }
4993
4994 DSAStack->addDSA(VD, DE, OMPC_reduction);
4995 Vars.push_back(DE);
4996 }
4997
4998 if (Vars.empty())
4999 return nullptr;
5000
5001 return OMPReductionClause::Create(
5002 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
5003 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId);
5004}
5005
Alexander Musman8dba6642014-04-22 13:09:42 +00005006OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step,
5007 SourceLocation StartLoc,
5008 SourceLocation LParenLoc,
5009 SourceLocation ColonLoc,
5010 SourceLocation EndLoc) {
5011 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00005012 for (auto &RefExpr : VarList) {
5013 assert(RefExpr && "NULL expr in OpenMP linear clause.");
5014 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00005015 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00005016 Vars.push_back(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00005017 continue;
5018 }
5019
5020 // OpenMP [2.14.3.7, linear clause]
5021 // A list item that appears in a linear clause is subject to the private
5022 // clause semantics described in Section 2.14.3.3 on page 159 except as
5023 // noted. In addition, the value of the new list item on each iteration
5024 // of the associated loop(s) corresponds to the value of the original
5025 // list item before entering the construct plus the logical number of
5026 // the iteration times linear-step.
5027
Alexey Bataeved09d242014-05-28 05:53:51 +00005028 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00005029 // OpenMP [2.1, C/C++]
5030 // A list item is a variable name.
5031 // OpenMP [2.14.3.3, Restrictions, p.1]
5032 // A variable that is part of another variable (as an array or
5033 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00005034 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00005035 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005036 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00005037 continue;
5038 }
5039
5040 VarDecl *VD = cast<VarDecl>(DE->getDecl());
5041
5042 // OpenMP [2.14.3.7, linear clause]
5043 // A list-item cannot appear in more than one linear clause.
5044 // A list-item that appears in a linear clause cannot appear in any
5045 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005046 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00005047 if (DVar.RefExpr) {
5048 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
5049 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005050 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00005051 continue;
5052 }
5053
5054 QualType QType = VD->getType();
5055 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
5056 // It will be analyzed later.
5057 Vars.push_back(DE);
5058 continue;
5059 }
5060
5061 // A variable must not have an incomplete type or a reference type.
5062 if (RequireCompleteType(ELoc, QType,
5063 diag::err_omp_linear_incomplete_type)) {
5064 continue;
5065 }
5066 if (QType->isReferenceType()) {
5067 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
5068 << getOpenMPClauseName(OMPC_linear) << QType;
5069 bool IsDecl =
5070 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5071 Diag(VD->getLocation(),
5072 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5073 << VD;
5074 continue;
5075 }
5076
5077 // A list item must not be const-qualified.
5078 if (QType.isConstant(Context)) {
5079 Diag(ELoc, diag::err_omp_const_variable)
5080 << getOpenMPClauseName(OMPC_linear);
5081 bool IsDecl =
5082 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5083 Diag(VD->getLocation(),
5084 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5085 << VD;
5086 continue;
5087 }
5088
5089 // A list item must be of integral or pointer type.
5090 QType = QType.getUnqualifiedType().getCanonicalType();
5091 const Type *Ty = QType.getTypePtrOrNull();
5092 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
5093 !Ty->isPointerType())) {
5094 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
5095 bool IsDecl =
5096 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5097 Diag(VD->getLocation(),
5098 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5099 << VD;
5100 continue;
5101 }
5102
5103 DSAStack->addDSA(VD, DE, OMPC_linear);
5104 Vars.push_back(DE);
5105 }
5106
5107 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005108 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00005109
5110 Expr *StepExpr = Step;
5111 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
5112 !Step->isInstantiationDependent() &&
5113 !Step->containsUnexpandedParameterPack()) {
5114 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005115 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00005116 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005117 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00005118 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00005119
5120 // Warn about zero linear step (it would be probably better specified as
5121 // making corresponding variables 'const').
5122 llvm::APSInt Result;
5123 if (StepExpr->isIntegerConstantExpr(Result, Context) &&
5124 !Result.isNegative() && !Result.isStrictlyPositive())
5125 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
5126 << (Vars.size() > 1);
5127 }
5128
5129 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc,
5130 Vars, StepExpr);
5131}
5132
Alexander Musmanf0d76e72014-05-29 14:36:25 +00005133OMPClause *Sema::ActOnOpenMPAlignedClause(
5134 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
5135 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
5136
5137 SmallVector<Expr *, 8> Vars;
5138 for (auto &RefExpr : VarList) {
5139 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
5140 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
5141 // It will be analyzed later.
5142 Vars.push_back(RefExpr);
5143 continue;
5144 }
5145
5146 SourceLocation ELoc = RefExpr->getExprLoc();
5147 // OpenMP [2.1, C/C++]
5148 // A list item is a variable name.
5149 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
5150 if (!DE || !isa<VarDecl>(DE->getDecl())) {
5151 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
5152 continue;
5153 }
5154
5155 VarDecl *VD = cast<VarDecl>(DE->getDecl());
5156
5157 // OpenMP [2.8.1, simd construct, Restrictions]
5158 // The type of list items appearing in the aligned clause must be
5159 // array, pointer, reference to array, or reference to pointer.
5160 QualType QType = DE->getType()
5161 .getNonReferenceType()
5162 .getUnqualifiedType()
5163 .getCanonicalType();
5164 const Type *Ty = QType.getTypePtrOrNull();
5165 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
5166 !Ty->isPointerType())) {
5167 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
5168 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
5169 bool IsDecl =
5170 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5171 Diag(VD->getLocation(),
5172 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5173 << VD;
5174 continue;
5175 }
5176
5177 // OpenMP [2.8.1, simd construct, Restrictions]
5178 // A list-item cannot appear in more than one aligned clause.
5179 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
5180 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
5181 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
5182 << getOpenMPClauseName(OMPC_aligned);
5183 continue;
5184 }
5185
5186 Vars.push_back(DE);
5187 }
5188
5189 // OpenMP [2.8.1, simd construct, Description]
5190 // The parameter of the aligned clause, alignment, must be a constant
5191 // positive integer expression.
5192 // If no optional parameter is specified, implementation-defined default
5193 // alignments for SIMD instructions on the target platforms are assumed.
5194 if (Alignment != nullptr) {
5195 ExprResult AlignResult =
5196 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
5197 if (AlignResult.isInvalid())
5198 return nullptr;
5199 Alignment = AlignResult.get();
5200 }
5201 if (Vars.empty())
5202 return nullptr;
5203
5204 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
5205 EndLoc, Vars, Alignment);
5206}
5207
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005208OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
5209 SourceLocation StartLoc,
5210 SourceLocation LParenLoc,
5211 SourceLocation EndLoc) {
5212 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00005213 for (auto &RefExpr : VarList) {
5214 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
5215 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005216 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00005217 Vars.push_back(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005218 continue;
5219 }
5220
Alexey Bataeved09d242014-05-28 05:53:51 +00005221 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005222 // OpenMP [2.1, C/C++]
5223 // A list item is a variable name.
5224 // OpenMP [2.14.4.1, Restrictions, p.1]
5225 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00005226 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005227 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005228 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005229 continue;
5230 }
5231
5232 Decl *D = DE->getDecl();
5233 VarDecl *VD = cast<VarDecl>(D);
5234
5235 QualType Type = VD->getType();
5236 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5237 // It will be analyzed later.
5238 Vars.push_back(DE);
5239 continue;
5240 }
5241
5242 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
5243 // A list item that appears in a copyin clause must be threadprivate.
5244 if (!DSAStack->isThreadPrivate(VD)) {
5245 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00005246 << getOpenMPClauseName(OMPC_copyin)
5247 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005248 continue;
5249 }
5250
5251 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
5252 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00005253 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005254 // operator for the class type.
5255 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005256 CXXRecordDecl *RD =
5257 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00005258 // FIXME This code must be replaced by actual assignment of the
5259 // threadprivate variable.
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005260 if (RD) {
5261 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
5262 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00005263 if (MD) {
5264 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
5265 MD->isDeleted()) {
5266 Diag(ELoc, diag::err_omp_required_method)
5267 << getOpenMPClauseName(OMPC_copyin) << 2;
5268 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
5269 VarDecl::DeclarationOnly;
5270 Diag(VD->getLocation(),
5271 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5272 << VD;
5273 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
5274 continue;
5275 }
5276 MarkFunctionReferenced(ELoc, MD);
5277 DiagnoseUseOfDecl(MD, ELoc);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005278 }
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005279 }
5280
5281 DSAStack->addDSA(VD, DE, OMPC_copyin);
5282 Vars.push_back(DE);
5283 }
5284
Alexey Bataeved09d242014-05-28 05:53:51 +00005285 if (Vars.empty())
5286 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005287
5288 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
5289}
5290
Alexey Bataevbae9a792014-06-27 10:37:06 +00005291OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
5292 SourceLocation StartLoc,
5293 SourceLocation LParenLoc,
5294 SourceLocation EndLoc) {
5295 SmallVector<Expr *, 8> Vars;
5296 for (auto &RefExpr : VarList) {
5297 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
5298 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
5299 // It will be analyzed later.
5300 Vars.push_back(RefExpr);
5301 continue;
5302 }
5303
5304 SourceLocation ELoc = RefExpr->getExprLoc();
5305 // OpenMP [2.1, C/C++]
5306 // A list item is a variable name.
5307 // OpenMP [2.14.4.1, Restrictions, p.1]
5308 // A list item that appears in a copyin clause must be threadprivate.
5309 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
5310 if (!DE || !isa<VarDecl>(DE->getDecl())) {
5311 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
5312 continue;
5313 }
5314
5315 Decl *D = DE->getDecl();
5316 VarDecl *VD = cast<VarDecl>(D);
5317
5318 QualType Type = VD->getType();
5319 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5320 // It will be analyzed later.
5321 Vars.push_back(DE);
5322 continue;
5323 }
5324
5325 // OpenMP [2.14.4.2, Restrictions, p.2]
5326 // A list item that appears in a copyprivate clause may not appear in a
5327 // private or firstprivate clause on the single construct.
5328 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005329 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00005330 if (DVar.CKind != OMPC_copyprivate && DVar.CKind != OMPC_unknown &&
5331 !(DVar.CKind == OMPC_private && !DVar.RefExpr)) {
5332 Diag(ELoc, diag::err_omp_wrong_dsa)
5333 << getOpenMPClauseName(DVar.CKind)
5334 << getOpenMPClauseName(OMPC_copyprivate);
5335 ReportOriginalDSA(*this, DSAStack, VD, DVar);
5336 continue;
5337 }
5338
5339 // OpenMP [2.11.4.2, Restrictions, p.1]
5340 // All list items that appear in a copyprivate clause must be either
5341 // threadprivate or private in the enclosing context.
5342 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005343 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00005344 if (DVar.CKind == OMPC_shared) {
5345 Diag(ELoc, diag::err_omp_required_access)
5346 << getOpenMPClauseName(OMPC_copyprivate)
5347 << "threadprivate or private in the enclosing context";
5348 ReportOriginalDSA(*this, DSAStack, VD, DVar);
5349 continue;
5350 }
5351 }
5352 }
5353
5354 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
5355 // A variable of class type (or array thereof) that appears in a
5356 // copyin clause requires an accessible, unambiguous copy assignment
5357 // operator for the class type.
5358 Type = Context.getBaseElementType(Type);
5359 CXXRecordDecl *RD =
5360 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
5361 // FIXME This code must be replaced by actual assignment of the
5362 // threadprivate variable.
5363 if (RD) {
5364 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
5365 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
5366 if (MD) {
5367 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
5368 MD->isDeleted()) {
5369 Diag(ELoc, diag::err_omp_required_method)
5370 << getOpenMPClauseName(OMPC_copyprivate) << 2;
5371 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
5372 VarDecl::DeclarationOnly;
5373 Diag(VD->getLocation(),
5374 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5375 << VD;
5376 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
5377 continue;
5378 }
5379 MarkFunctionReferenced(ELoc, MD);
5380 DiagnoseUseOfDecl(MD, ELoc);
5381 }
5382 }
5383
5384 // No need to mark vars as copyprivate, they are already threadprivate or
5385 // implicitly private.
5386 Vars.push_back(DE);
5387 }
5388
5389 if (Vars.empty())
5390 return nullptr;
5391
5392 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
5393}
5394
Alexey Bataev6125da92014-07-21 11:26:11 +00005395OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
5396 SourceLocation StartLoc,
5397 SourceLocation LParenLoc,
5398 SourceLocation EndLoc) {
5399 if (VarList.empty())
5400 return nullptr;
5401
5402 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
5403}
Alexey Bataevdea47612014-07-23 07:46:59 +00005404