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