blob: 995054321d362410eabdbd62cf507e9eafd53843 [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 Bataev9fb6e642014-07-22 06:45:04 +00001123 case OMPD_ordered: {
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 Bataev0162e452014-07-22 10:10:35 +00001131 case OMPD_atomic: {
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 Bataev0bd520b2014-09-19 08:19:49 +00001139 case OMPD_target: {
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 Bataev13314bf2014-10-09 04:18:56 +00001147 case OMPD_teams: {
1148 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1149 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1150 Sema::CapturedParamNameType Params[] = {
1151 std::make_pair(".global_tid.", KmpInt32PtrTy),
1152 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1153 std::make_pair(StringRef(), QualType()) // __context with shared vars
1154 };
1155 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1156 Params);
1157 break;
1158 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001159 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001160 case OMPD_taskyield:
1161 case OMPD_barrier:
1162 case OMPD_taskwait:
1163 case OMPD_flush:
Alexey Bataev9959db52014-05-06 10:08:46 +00001164 llvm_unreachable("OpenMP Directive is not allowed");
1165 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001166 llvm_unreachable("Unknown OpenMP directive");
1167 }
1168}
1169
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001170static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1171 OpenMPDirectiveKind CurrentRegion,
1172 const DeclarationNameInfo &CurrentName,
1173 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001174 // Allowed nesting of constructs
1175 // +------------------+-----------------+------------------------------------+
1176 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1177 // +------------------+-----------------+------------------------------------+
1178 // | parallel | parallel | * |
1179 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001180 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001181 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001182 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001183 // | parallel | simd | * |
1184 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001185 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001186 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001187 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001188 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001189 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001190 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001191 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001192 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001193 // | parallel | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001194 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001195 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001196 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001197 // | parallel | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001198 // | parallel | teams | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001199 // +------------------+-----------------+------------------------------------+
1200 // | for | parallel | * |
1201 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001202 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001203 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001204 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001205 // | for | simd | * |
1206 // | for | sections | + |
1207 // | for | section | + |
1208 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001209 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001210 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001211 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001212 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001213 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001214 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001215 // | for | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001216 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001217 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001218 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001219 // | for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001220 // | for | teams | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001221 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001222 // | master | parallel | * |
1223 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001224 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001225 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001226 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001227 // | master | simd | * |
1228 // | master | sections | + |
1229 // | master | section | + |
1230 // | master | single | + |
1231 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001232 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001233 // | master |parallel sections| * |
1234 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001235 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001236 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001237 // | master | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001238 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001239 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001240 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001241 // | master | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001242 // | master | teams | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001243 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001244 // | critical | parallel | * |
1245 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001246 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001247 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001248 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001249 // | critical | simd | * |
1250 // | critical | sections | + |
1251 // | critical | section | + |
1252 // | critical | single | + |
1253 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001254 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001255 // | critical |parallel sections| * |
1256 // | critical | task | * |
1257 // | critical | taskyield | * |
1258 // | critical | barrier | + |
1259 // | critical | taskwait | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001260 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001261 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001262 // | critical | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001263 // | critical | teams | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001264 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001265 // | simd | parallel | |
1266 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001267 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00001268 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001269 // | simd | critical | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001270 // | simd | simd | |
1271 // | simd | sections | |
1272 // | simd | section | |
1273 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001274 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001275 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001276 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001277 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00001278 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001279 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001280 // | simd | taskwait | |
Alexey Bataev6125da92014-07-21 11:26:11 +00001281 // | simd | flush | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001282 // | simd | ordered | |
Alexey Bataev0162e452014-07-22 10:10:35 +00001283 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001284 // | simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001285 // | simd | teams | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001286 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00001287 // | for simd | parallel | |
1288 // | for simd | for | |
1289 // | for simd | for simd | |
1290 // | for simd | master | |
1291 // | for simd | critical | |
1292 // | for simd | simd | |
1293 // | for simd | sections | |
1294 // | for simd | section | |
1295 // | for simd | single | |
1296 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001297 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001298 // | for simd |parallel sections| |
1299 // | for simd | task | |
1300 // | for simd | taskyield | |
1301 // | for simd | barrier | |
1302 // | for simd | taskwait | |
1303 // | for simd | flush | |
1304 // | for simd | ordered | |
1305 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001306 // | for simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001307 // | for simd | teams | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001308 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00001309 // | parallel for simd| parallel | |
1310 // | parallel for simd| for | |
1311 // | parallel for simd| for simd | |
1312 // | parallel for simd| master | |
1313 // | parallel for simd| critical | |
1314 // | parallel for simd| simd | |
1315 // | parallel for simd| sections | |
1316 // | parallel for simd| section | |
1317 // | parallel for simd| single | |
1318 // | parallel for simd| parallel for | |
1319 // | parallel for simd|parallel for simd| |
1320 // | parallel for simd|parallel sections| |
1321 // | parallel for simd| task | |
1322 // | parallel for simd| taskyield | |
1323 // | parallel for simd| barrier | |
1324 // | parallel for simd| taskwait | |
1325 // | parallel for simd| flush | |
1326 // | parallel for simd| ordered | |
1327 // | parallel for simd| atomic | |
1328 // | parallel for simd| target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001329 // | parallel for simd| teams | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001330 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001331 // | sections | parallel | * |
1332 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001333 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001334 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001335 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001336 // | sections | simd | * |
1337 // | sections | sections | + |
1338 // | sections | section | * |
1339 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001340 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001341 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001342 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001343 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001344 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001345 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001346 // | sections | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001347 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001348 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001349 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001350 // | sections | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001351 // | sections | teams | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001352 // +------------------+-----------------+------------------------------------+
1353 // | section | parallel | * |
1354 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001355 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001356 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001357 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001358 // | section | simd | * |
1359 // | section | sections | + |
1360 // | section | section | + |
1361 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001362 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001363 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001364 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001365 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001366 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001367 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001368 // | section | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001369 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001370 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001371 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001372 // | section | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001373 // | section | teams | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001374 // +------------------+-----------------+------------------------------------+
1375 // | single | parallel | * |
1376 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001377 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001378 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001379 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001380 // | single | simd | * |
1381 // | single | sections | + |
1382 // | single | section | + |
1383 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001384 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001385 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001386 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001387 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001388 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001389 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001390 // | single | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001391 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001392 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001393 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001394 // | single | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001395 // | single | teams | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001396 // +------------------+-----------------+------------------------------------+
1397 // | parallel for | parallel | * |
1398 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001399 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001400 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001401 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001402 // | parallel for | simd | * |
1403 // | parallel for | sections | + |
1404 // | parallel for | section | + |
1405 // | parallel for | single | + |
1406 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001407 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001408 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001409 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001410 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001411 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001412 // | parallel for | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001413 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001414 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001415 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001416 // | parallel for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001417 // | parallel for | teams | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001418 // +------------------+-----------------+------------------------------------+
1419 // | parallel sections| parallel | * |
1420 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001421 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001422 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001423 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001424 // | parallel sections| simd | * |
1425 // | parallel sections| sections | + |
1426 // | parallel sections| section | * |
1427 // | parallel sections| single | + |
1428 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001429 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001430 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001431 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001432 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001433 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001434 // | parallel sections| taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001435 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001436 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001437 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001438 // | parallel sections| target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001439 // | parallel sections| teams | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001440 // +------------------+-----------------+------------------------------------+
1441 // | task | parallel | * |
1442 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001443 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001444 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001445 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001446 // | task | simd | * |
1447 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001448 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001449 // | task | single | + |
1450 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001451 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001452 // | task |parallel sections| * |
1453 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001454 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001455 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001456 // | task | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001457 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001458 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001459 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001460 // | task | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001461 // | task | teams | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001462 // +------------------+-----------------+------------------------------------+
1463 // | ordered | parallel | * |
1464 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001465 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001466 // | ordered | master | * |
1467 // | ordered | critical | * |
1468 // | ordered | simd | * |
1469 // | ordered | sections | + |
1470 // | ordered | section | + |
1471 // | ordered | single | + |
1472 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001473 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001474 // | ordered |parallel sections| * |
1475 // | ordered | task | * |
1476 // | ordered | taskyield | * |
1477 // | ordered | barrier | + |
1478 // | ordered | taskwait | * |
1479 // | ordered | flush | * |
1480 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001481 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001482 // | ordered | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001483 // | ordered | teams | + |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001484 // +------------------+-----------------+------------------------------------+
1485 // | atomic | parallel | |
1486 // | atomic | for | |
1487 // | atomic | for simd | |
1488 // | atomic | master | |
1489 // | atomic | critical | |
1490 // | atomic | simd | |
1491 // | atomic | sections | |
1492 // | atomic | section | |
1493 // | atomic | single | |
1494 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001495 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001496 // | atomic |parallel sections| |
1497 // | atomic | task | |
1498 // | atomic | taskyield | |
1499 // | atomic | barrier | |
1500 // | atomic | taskwait | |
1501 // | atomic | flush | |
1502 // | atomic | ordered | |
1503 // | atomic | atomic | |
1504 // | atomic | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001505 // | atomic | teams | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001506 // +------------------+-----------------+------------------------------------+
1507 // | target | parallel | * |
1508 // | target | for | * |
1509 // | target | for simd | * |
1510 // | target | master | * |
1511 // | target | critical | * |
1512 // | target | simd | * |
1513 // | target | sections | * |
1514 // | target | section | * |
1515 // | target | single | * |
1516 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001517 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001518 // | target |parallel sections| * |
1519 // | target | task | * |
1520 // | target | taskyield | * |
1521 // | target | barrier | * |
1522 // | target | taskwait | * |
1523 // | target | flush | * |
1524 // | target | ordered | * |
1525 // | target | atomic | * |
1526 // | target | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001527 // | target | teams | * |
1528 // +------------------+-----------------+------------------------------------+
1529 // | teams | parallel | * |
1530 // | teams | for | + |
1531 // | teams | for simd | + |
1532 // | teams | master | + |
1533 // | teams | critical | + |
1534 // | teams | simd | + |
1535 // | teams | sections | + |
1536 // | teams | section | + |
1537 // | teams | single | + |
1538 // | teams | parallel for | * |
1539 // | teams |parallel for simd| * |
1540 // | teams |parallel sections| * |
1541 // | teams | task | + |
1542 // | teams | taskyield | + |
1543 // | teams | barrier | + |
1544 // | teams | taskwait | + |
1545 // | teams | flush | + |
1546 // | teams | ordered | + |
1547 // | teams | atomic | + |
1548 // | teams | target | + |
1549 // | teams | teams | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001550 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00001551 if (Stack->getCurScope()) {
1552 auto ParentRegion = Stack->getParentDirective();
1553 bool NestingProhibited = false;
1554 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001555 enum {
1556 NoRecommend,
1557 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00001558 ShouldBeInOrderedRegion,
1559 ShouldBeInTargetRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001560 } Recommend = NoRecommend;
Alexey Bataev549210e2014-06-24 04:39:47 +00001561 if (isOpenMPSimdDirective(ParentRegion)) {
1562 // OpenMP [2.16, Nesting of Regions]
1563 // OpenMP constructs may not be nested inside a simd region.
1564 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
1565 return true;
1566 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001567 if (ParentRegion == OMPD_atomic) {
1568 // OpenMP [2.16, Nesting of Regions]
1569 // OpenMP constructs may not be nested inside an atomic region.
1570 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
1571 return true;
1572 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001573 if (CurrentRegion == OMPD_section) {
1574 // OpenMP [2.7.2, sections Construct, Restrictions]
1575 // Orphaned section directives are prohibited. That is, the section
1576 // directives must appear within the sections construct and must not be
1577 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001578 if (ParentRegion != OMPD_sections &&
1579 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001580 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
1581 << (ParentRegion != OMPD_unknown)
1582 << getOpenMPDirectiveName(ParentRegion);
1583 return true;
1584 }
1585 return false;
1586 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001587 // Allow some constructs to be orphaned (they could be used in functions,
1588 // called from OpenMP regions with the required preconditions).
1589 if (ParentRegion == OMPD_unknown)
1590 return false;
Alexander Musman80c22892014-07-17 08:54:58 +00001591 if (CurrentRegion == OMPD_master) {
1592 // OpenMP [2.16, Nesting of Regions]
1593 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00001594 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00001595 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
1596 ParentRegion == OMPD_task;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001597 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
1598 // OpenMP [2.16, Nesting of Regions]
1599 // A critical region may not be nested (closely or otherwise) inside a
1600 // critical region with the same name. Note that this restriction is not
1601 // sufficient to prevent deadlock.
1602 SourceLocation PreviousCriticalLoc;
1603 bool DeadLock =
1604 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
1605 OpenMPDirectiveKind K,
1606 const DeclarationNameInfo &DNI,
1607 SourceLocation Loc)
1608 ->bool {
1609 if (K == OMPD_critical &&
1610 DNI.getName() == CurrentName.getName()) {
1611 PreviousCriticalLoc = Loc;
1612 return true;
1613 } else
1614 return false;
1615 },
1616 false /* skip top directive */);
1617 if (DeadLock) {
1618 SemaRef.Diag(StartLoc,
1619 diag::err_omp_prohibited_region_critical_same_name)
1620 << CurrentName.getName();
1621 if (PreviousCriticalLoc.isValid())
1622 SemaRef.Diag(PreviousCriticalLoc,
1623 diag::note_omp_previous_critical_region);
1624 return true;
1625 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001626 } else if (CurrentRegion == OMPD_barrier) {
1627 // OpenMP [2.16, Nesting of Regions]
1628 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00001629 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001630 NestingProhibited =
1631 isOpenMPWorksharingDirective(ParentRegion) ||
1632 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1633 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00001634 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00001635 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00001636 // OpenMP [2.16, Nesting of Regions]
1637 // A worksharing region may not be closely nested inside a worksharing,
1638 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001639 NestingProhibited =
Alexander Musmanf82886e2014-09-18 05:12:34 +00001640 isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001641 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1642 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
1643 Recommend = ShouldBeInParallelRegion;
1644 } else if (CurrentRegion == OMPD_ordered) {
1645 // OpenMP [2.16, Nesting of Regions]
1646 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00001647 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001648 // An ordered region must be closely nested inside a loop region (or
1649 // parallel loop region) with an ordered clause.
1650 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00001651 ParentRegion == OMPD_task ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001652 !Stack->isParentOrderedRegion();
1653 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00001654 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
1655 // OpenMP [2.16, Nesting of Regions]
1656 // If specified, a teams construct must be contained within a target
1657 // construct.
1658 NestingProhibited = ParentRegion != OMPD_target;
1659 Recommend = ShouldBeInTargetRegion;
1660 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
1661 }
1662 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
1663 // OpenMP [2.16, Nesting of Regions]
1664 // distribute, parallel, parallel sections, parallel workshare, and the
1665 // parallel loop and parallel loop SIMD constructs are the only OpenMP
1666 // constructs that can be closely nested in the teams region.
1667 // TODO: add distribute directive.
1668 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion);
1669 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00001670 }
1671 if (NestingProhibited) {
1672 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001673 << CloseNesting << getOpenMPDirectiveName(ParentRegion) << Recommend
1674 << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00001675 return true;
1676 }
1677 }
1678 return false;
1679}
1680
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001681StmtResult Sema::ActOnOpenMPExecutableDirective(OpenMPDirectiveKind Kind,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001682 const DeclarationNameInfo &DirName,
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001683 ArrayRef<OMPClause *> Clauses,
1684 Stmt *AStmt,
1685 SourceLocation StartLoc,
1686 SourceLocation EndLoc) {
1687 StmtResult Res = StmtError();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001688 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00001689 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001690
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001691 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev68446b72014-07-18 07:47:19 +00001692 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001693 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00001694 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00001695 if (AStmt) {
1696 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
1697
1698 // Check default data sharing attributes for referenced variables.
1699 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
1700 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
1701 if (DSAChecker.isErrorFound())
1702 return StmtError();
1703 // Generate list of implicitly defined firstprivate variables.
1704 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00001705
1706 if (!DSAChecker.getImplicitFirstprivate().empty()) {
1707 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
1708 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
1709 SourceLocation(), SourceLocation())) {
1710 ClausesWithImplicit.push_back(Implicit);
1711 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
1712 DSAChecker.getImplicitFirstprivate().size();
1713 } else
1714 ErrorFound = true;
1715 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001716 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001717
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001718 switch (Kind) {
1719 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00001720 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
1721 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001722 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001723 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001724 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
1725 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001726 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00001727 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001728 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
1729 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001730 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00001731 case OMPD_for_simd:
1732 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
1733 EndLoc, VarsWithInheritedDSA);
1734 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001735 case OMPD_sections:
1736 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
1737 EndLoc);
1738 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001739 case OMPD_section:
1740 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00001741 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001742 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
1743 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001744 case OMPD_single:
1745 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
1746 EndLoc);
1747 break;
Alexander Musman80c22892014-07-17 08:54:58 +00001748 case OMPD_master:
1749 assert(ClausesWithImplicit.empty() &&
1750 "No clauses are allowed for 'omp master' directive");
1751 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
1752 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001753 case OMPD_critical:
1754 assert(ClausesWithImplicit.empty() &&
1755 "No clauses are allowed for 'omp critical' directive");
1756 Res = ActOnOpenMPCriticalDirective(DirName, AStmt, StartLoc, EndLoc);
1757 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00001758 case OMPD_parallel_for:
1759 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
1760 EndLoc, VarsWithInheritedDSA);
1761 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00001762 case OMPD_parallel_for_simd:
1763 Res = ActOnOpenMPParallelForSimdDirective(
1764 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
1765 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001766 case OMPD_parallel_sections:
1767 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
1768 StartLoc, EndLoc);
1769 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001770 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001771 Res =
1772 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
1773 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00001774 case OMPD_taskyield:
1775 assert(ClausesWithImplicit.empty() &&
1776 "No clauses are allowed for 'omp taskyield' directive");
1777 assert(AStmt == nullptr &&
1778 "No associated statement allowed for 'omp taskyield' directive");
1779 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
1780 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001781 case OMPD_barrier:
1782 assert(ClausesWithImplicit.empty() &&
1783 "No clauses are allowed for 'omp barrier' directive");
1784 assert(AStmt == nullptr &&
1785 "No associated statement allowed for 'omp barrier' directive");
1786 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
1787 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00001788 case OMPD_taskwait:
1789 assert(ClausesWithImplicit.empty() &&
1790 "No clauses are allowed for 'omp taskwait' directive");
1791 assert(AStmt == nullptr &&
1792 "No associated statement allowed for 'omp taskwait' directive");
1793 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
1794 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00001795 case OMPD_flush:
1796 assert(AStmt == nullptr &&
1797 "No associated statement allowed for 'omp flush' directive");
1798 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
1799 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001800 case OMPD_ordered:
1801 assert(ClausesWithImplicit.empty() &&
1802 "No clauses are allowed for 'omp ordered' directive");
1803 Res = ActOnOpenMPOrderedDirective(AStmt, StartLoc, EndLoc);
1804 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00001805 case OMPD_atomic:
1806 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
1807 EndLoc);
1808 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00001809 case OMPD_teams:
1810 Res =
1811 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
1812 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001813 case OMPD_target:
1814 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
1815 EndLoc);
1816 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001817 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001818 llvm_unreachable("OpenMP Directive is not allowed");
1819 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001820 llvm_unreachable("Unknown OpenMP directive");
1821 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001822
Alexey Bataev4acb8592014-07-07 13:01:15 +00001823 for (auto P : VarsWithInheritedDSA) {
1824 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
1825 << P.first << P.second->getSourceRange();
1826 }
1827 if (!VarsWithInheritedDSA.empty())
1828 return StmtError();
1829
Alexey Bataeved09d242014-05-28 05:53:51 +00001830 if (ErrorFound)
1831 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001832 return Res;
1833}
1834
1835StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
1836 Stmt *AStmt,
1837 SourceLocation StartLoc,
1838 SourceLocation EndLoc) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001839 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1840 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
1841 // 1.2.2 OpenMP Language Terminology
1842 // Structured block - An executable statement with a single entry at the
1843 // top and a single exit at the bottom.
1844 // The point of exit cannot be a branch out of the structured block.
1845 // longjmp() and throw() must not violate the entry/exit criteria.
1846 CS->getCapturedDecl()->setNothrow();
1847
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001848 getCurFunction()->setHasBranchProtectedScope();
1849
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001850 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
1851 AStmt);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001852}
1853
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001854namespace {
1855/// \brief Helper class for checking canonical form of the OpenMP loops and
1856/// extracting iteration space of each loop in the loop nest, that will be used
1857/// for IR generation.
1858class OpenMPIterationSpaceChecker {
1859 /// \brief Reference to Sema.
1860 Sema &SemaRef;
1861 /// \brief A location for diagnostics (when there is no some better location).
1862 SourceLocation DefaultLoc;
1863 /// \brief A location for diagnostics (when increment is not compatible).
1864 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00001865 /// \brief A source location for referring to loop init later.
1866 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001867 /// \brief A source location for referring to condition later.
1868 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00001869 /// \brief A source location for referring to increment later.
1870 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001871 /// \brief Loop variable.
1872 VarDecl *Var;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001873 /// \brief Reference to loop variable.
1874 DeclRefExpr *VarRef;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001875 /// \brief Lower bound (initializer for the var).
1876 Expr *LB;
1877 /// \brief Upper bound.
1878 Expr *UB;
1879 /// \brief Loop step (increment).
1880 Expr *Step;
1881 /// \brief This flag is true when condition is one of:
1882 /// Var < UB
1883 /// Var <= UB
1884 /// UB > Var
1885 /// UB >= Var
1886 bool TestIsLessOp;
1887 /// \brief This flag is true when condition is strict ( < or > ).
1888 bool TestIsStrictOp;
1889 /// \brief This flag is true when step is subtracted on each iteration.
1890 bool SubtractStep;
1891
1892public:
1893 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
1894 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
Alexander Musmana5f070a2014-10-01 06:03:56 +00001895 InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()),
1896 IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001897 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
1898 TestIsStrictOp(false), SubtractStep(false) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001899 /// \brief Check init-expr for canonical loop form and save loop counter
1900 /// variable - #Var and its initialization value - #LB.
1901 bool CheckInit(Stmt *S);
1902 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
1903 /// for less/greater and for strict/non-strict comparison.
1904 bool CheckCond(Expr *S);
1905 /// \brief Check incr-expr for canonical loop form and return true if it
1906 /// does not conform, otherwise save loop step (#Step).
1907 bool CheckInc(Expr *S);
1908 /// \brief Return the loop counter variable.
1909 VarDecl *GetLoopVar() const { return Var; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001910 /// \brief Return the reference expression to loop counter variable.
1911 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00001912 /// \brief Source range of the loop init.
1913 SourceRange GetInitSrcRange() const { return InitSrcRange; }
1914 /// \brief Source range of the loop condition.
1915 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
1916 /// \brief Source range of the loop increment.
1917 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
1918 /// \brief True if the step should be subtracted.
1919 bool ShouldSubtractStep() const { return SubtractStep; }
1920 /// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00001921 Expr *BuildNumIterations(Scope *S, const bool LimitedType) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00001922 /// \brief Build reference expression to the counter be used for codegen.
1923 Expr *BuildCounterVar() const;
1924 /// \brief Build initization of the counter be used for codegen.
1925 Expr *BuildCounterInit() const;
1926 /// \brief Build step of the counter be used for codegen.
1927 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001928 /// \brief Return true if any expression is dependent.
1929 bool Dependent() const;
1930
1931private:
1932 /// \brief Check the right-hand side of an assignment in the increment
1933 /// expression.
1934 bool CheckIncRHS(Expr *RHS);
1935 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001936 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001937 /// \brief Helper to set upper bound.
1938 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, const SourceRange &SR,
1939 const SourceLocation &SL);
1940 /// \brief Helper to set loop increment.
1941 bool SetStep(Expr *NewStep, bool Subtract);
1942};
1943
1944bool OpenMPIterationSpaceChecker::Dependent() const {
1945 if (!Var) {
1946 assert(!LB && !UB && !Step);
1947 return false;
1948 }
1949 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
1950 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
1951}
1952
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001953bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
1954 DeclRefExpr *NewVarRefExpr,
1955 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001956 // State consistency checking to ensure correct usage.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001957 assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
1958 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001959 if (!NewVar || !NewLB)
1960 return true;
1961 Var = NewVar;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001962 VarRef = NewVarRefExpr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001963 LB = NewLB;
1964 return false;
1965}
1966
1967bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
1968 const SourceRange &SR,
1969 const SourceLocation &SL) {
1970 // State consistency checking to ensure correct usage.
1971 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
1972 !TestIsLessOp && !TestIsStrictOp);
1973 if (!NewUB)
1974 return true;
1975 UB = NewUB;
1976 TestIsLessOp = LessOp;
1977 TestIsStrictOp = StrictOp;
1978 ConditionSrcRange = SR;
1979 ConditionLoc = SL;
1980 return false;
1981}
1982
1983bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
1984 // State consistency checking to ensure correct usage.
1985 assert(Var != nullptr && LB != nullptr && Step == nullptr);
1986 if (!NewStep)
1987 return true;
1988 if (!NewStep->isValueDependent()) {
1989 // Check that the step is integer expression.
1990 SourceLocation StepLoc = NewStep->getLocStart();
1991 ExprResult Val =
1992 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
1993 if (Val.isInvalid())
1994 return true;
1995 NewStep = Val.get();
1996
1997 // OpenMP [2.6, Canonical Loop Form, Restrictions]
1998 // If test-expr is of form var relational-op b and relational-op is < or
1999 // <= then incr-expr must cause var to increase on each iteration of the
2000 // loop. If test-expr is of form var relational-op b and relational-op is
2001 // > or >= then incr-expr must cause var to decrease on each iteration of
2002 // the loop.
2003 // If test-expr is of form b relational-op var and relational-op is < or
2004 // <= then incr-expr must cause var to decrease on each iteration of the
2005 // loop. If test-expr is of form b relational-op var and relational-op is
2006 // > or >= then incr-expr must cause var to increase on each iteration of
2007 // the loop.
2008 llvm::APSInt Result;
2009 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
2010 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
2011 bool IsConstNeg =
2012 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00002013 bool IsConstPos =
2014 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002015 bool IsConstZero = IsConstant && !Result.getBoolValue();
2016 if (UB && (IsConstZero ||
2017 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00002018 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002019 SemaRef.Diag(NewStep->getExprLoc(),
2020 diag::err_omp_loop_incr_not_compatible)
2021 << Var << TestIsLessOp << NewStep->getSourceRange();
2022 SemaRef.Diag(ConditionLoc,
2023 diag::note_omp_loop_cond_requres_compatible_incr)
2024 << TestIsLessOp << ConditionSrcRange;
2025 return true;
2026 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002027 if (TestIsLessOp == Subtract) {
2028 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
2029 NewStep).get();
2030 Subtract = !Subtract;
2031 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002032 }
2033
2034 Step = NewStep;
2035 SubtractStep = Subtract;
2036 return false;
2037}
2038
2039bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S) {
2040 // Check init-expr for canonical loop form and save loop counter
2041 // variable - #Var and its initialization value - #LB.
2042 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
2043 // var = lb
2044 // integer-type var = lb
2045 // random-access-iterator-type var = lb
2046 // pointer-type var = lb
2047 //
2048 if (!S) {
2049 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
2050 return true;
2051 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002052 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002053 if (Expr *E = dyn_cast<Expr>(S))
2054 S = E->IgnoreParens();
2055 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2056 if (BO->getOpcode() == BO_Assign)
2057 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002058 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002059 BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002060 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
2061 if (DS->isSingleDecl()) {
2062 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
2063 if (Var->hasInit()) {
2064 // Accept non-canonical init form here but emit ext. warning.
2065 if (Var->getInitStyle() != VarDecl::CInit)
2066 SemaRef.Diag(S->getLocStart(),
2067 diag::ext_omp_loop_not_canonical_init)
2068 << S->getSourceRange();
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002069 return SetVarAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002070 }
2071 }
2072 }
2073 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
2074 if (CE->getOperator() == OO_Equal)
2075 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002076 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
2077 CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002078
2079 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
2080 << S->getSourceRange();
2081 return true;
2082}
2083
Alexey Bataev23b69422014-06-18 07:08:49 +00002084/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002085/// variable (which may be the loop variable) if possible.
2086static const VarDecl *GetInitVarDecl(const Expr *E) {
2087 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00002088 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002089 E = E->IgnoreParenImpCasts();
2090 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
2091 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
2092 if (Ctor->isCopyConstructor() && CE->getNumArgs() == 1 &&
2093 CE->getArg(0) != nullptr)
2094 E = CE->getArg(0)->IgnoreParenImpCasts();
2095 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
2096 if (!DRE)
2097 return nullptr;
2098 return dyn_cast<VarDecl>(DRE->getDecl());
2099}
2100
2101bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
2102 // Check test-expr for canonical form, save upper-bound UB, flags for
2103 // less/greater and for strict/non-strict comparison.
2104 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2105 // var relational-op b
2106 // b relational-op var
2107 //
2108 if (!S) {
2109 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
2110 return true;
2111 }
2112 S = S->IgnoreParenImpCasts();
2113 SourceLocation CondLoc = S->getLocStart();
2114 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2115 if (BO->isRelationalOp()) {
2116 if (GetInitVarDecl(BO->getLHS()) == Var)
2117 return SetUB(BO->getRHS(),
2118 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
2119 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2120 BO->getSourceRange(), BO->getOperatorLoc());
2121 if (GetInitVarDecl(BO->getRHS()) == Var)
2122 return SetUB(BO->getLHS(),
2123 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
2124 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2125 BO->getSourceRange(), BO->getOperatorLoc());
2126 }
2127 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2128 if (CE->getNumArgs() == 2) {
2129 auto Op = CE->getOperator();
2130 switch (Op) {
2131 case OO_Greater:
2132 case OO_GreaterEqual:
2133 case OO_Less:
2134 case OO_LessEqual:
2135 if (GetInitVarDecl(CE->getArg(0)) == Var)
2136 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
2137 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2138 CE->getOperatorLoc());
2139 if (GetInitVarDecl(CE->getArg(1)) == Var)
2140 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
2141 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2142 CE->getOperatorLoc());
2143 break;
2144 default:
2145 break;
2146 }
2147 }
2148 }
2149 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
2150 << S->getSourceRange() << Var;
2151 return true;
2152}
2153
2154bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
2155 // RHS of canonical loop form increment can be:
2156 // var + incr
2157 // incr + var
2158 // var - incr
2159 //
2160 RHS = RHS->IgnoreParenImpCasts();
2161 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
2162 if (BO->isAdditiveOp()) {
2163 bool IsAdd = BO->getOpcode() == BO_Add;
2164 if (GetInitVarDecl(BO->getLHS()) == Var)
2165 return SetStep(BO->getRHS(), !IsAdd);
2166 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
2167 return SetStep(BO->getLHS(), false);
2168 }
2169 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
2170 bool IsAdd = CE->getOperator() == OO_Plus;
2171 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
2172 if (GetInitVarDecl(CE->getArg(0)) == Var)
2173 return SetStep(CE->getArg(1), !IsAdd);
2174 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
2175 return SetStep(CE->getArg(0), false);
2176 }
2177 }
2178 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2179 << RHS->getSourceRange() << Var;
2180 return true;
2181}
2182
2183bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
2184 // Check incr-expr for canonical loop form and return true if it
2185 // does not conform.
2186 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2187 // ++var
2188 // var++
2189 // --var
2190 // var--
2191 // var += incr
2192 // var -= incr
2193 // var = var + incr
2194 // var = incr + var
2195 // var = var - incr
2196 //
2197 if (!S) {
2198 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
2199 return true;
2200 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002201 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002202 S = S->IgnoreParens();
2203 if (auto UO = dyn_cast<UnaryOperator>(S)) {
2204 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
2205 return SetStep(
2206 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
2207 (UO->isDecrementOp() ? -1 : 1)).get(),
2208 false);
2209 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
2210 switch (BO->getOpcode()) {
2211 case BO_AddAssign:
2212 case BO_SubAssign:
2213 if (GetInitVarDecl(BO->getLHS()) == Var)
2214 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
2215 break;
2216 case BO_Assign:
2217 if (GetInitVarDecl(BO->getLHS()) == Var)
2218 return CheckIncRHS(BO->getRHS());
2219 break;
2220 default:
2221 break;
2222 }
2223 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2224 switch (CE->getOperator()) {
2225 case OO_PlusPlus:
2226 case OO_MinusMinus:
2227 if (GetInitVarDecl(CE->getArg(0)) == Var)
2228 return SetStep(
2229 SemaRef.ActOnIntegerConstant(
2230 CE->getLocStart(),
2231 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
2232 false);
2233 break;
2234 case OO_PlusEqual:
2235 case OO_MinusEqual:
2236 if (GetInitVarDecl(CE->getArg(0)) == Var)
2237 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
2238 break;
2239 case OO_Equal:
2240 if (GetInitVarDecl(CE->getArg(0)) == Var)
2241 return CheckIncRHS(CE->getArg(1));
2242 break;
2243 default:
2244 break;
2245 }
2246 }
2247 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2248 << S->getSourceRange() << Var;
2249 return true;
2250}
Alexander Musmana5f070a2014-10-01 06:03:56 +00002251
2252/// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00002253Expr *
2254OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S,
2255 const bool LimitedType) const {
Alexander Musmana5f070a2014-10-01 06:03:56 +00002256 ExprResult Diff;
2257 if (Var->getType()->isIntegerType() || Var->getType()->isPointerType() ||
2258 SemaRef.getLangOpts().CPlusPlus) {
2259 // Upper - Lower
2260 Expr *Upper = TestIsLessOp ? UB : LB;
2261 Expr *Lower = TestIsLessOp ? LB : UB;
2262
2263 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
2264
2265 if (!Diff.isUsable() && Var->getType()->getAsCXXRecordDecl()) {
2266 // BuildBinOp already emitted error, this one is to point user to upper
2267 // and lower bound, and to tell what is passed to 'operator-'.
2268 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
2269 << Upper->getSourceRange() << Lower->getSourceRange();
2270 return nullptr;
2271 }
2272 }
2273
2274 if (!Diff.isUsable())
2275 return nullptr;
2276
2277 // Upper - Lower [- 1]
2278 if (TestIsStrictOp)
2279 Diff = SemaRef.BuildBinOp(
2280 S, DefaultLoc, BO_Sub, Diff.get(),
2281 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
2282 if (!Diff.isUsable())
2283 return nullptr;
2284
2285 // Upper - Lower [- 1] + Step
2286 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(),
2287 Step->IgnoreImplicit());
2288 if (!Diff.isUsable())
2289 return nullptr;
2290
2291 // Parentheses (for dumping/debugging purposes only).
2292 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
2293 if (!Diff.isUsable())
2294 return nullptr;
2295
2296 // (Upper - Lower [- 1] + Step) / Step
2297 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(),
2298 Step->IgnoreImplicit());
2299 if (!Diff.isUsable())
2300 return nullptr;
2301
Alexander Musman174b3ca2014-10-06 11:16:29 +00002302 // OpenMP runtime requires 32-bit or 64-bit loop variables.
2303 if (LimitedType) {
2304 auto &C = SemaRef.Context;
2305 QualType Type = Diff.get()->getType();
2306 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
2307 if (NewSize != C.getTypeSize(Type)) {
2308 if (NewSize < C.getTypeSize(Type)) {
2309 assert(NewSize == 64 && "incorrect loop var size");
2310 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
2311 << InitSrcRange << ConditionSrcRange;
2312 }
2313 QualType NewType = C.getIntTypeForBitwidth(
2314 NewSize, Type->hasSignedIntegerRepresentation());
2315 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
2316 Sema::AA_Converting, true);
2317 if (!Diff.isUsable())
2318 return nullptr;
2319 }
2320 }
2321
Alexander Musmana5f070a2014-10-01 06:03:56 +00002322 return Diff.get();
2323}
2324
2325/// \brief Build reference expression to the counter be used for codegen.
2326Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const {
2327 return DeclRefExpr::Create(SemaRef.Context, NestedNameSpecifierLoc(),
2328 GetIncrementSrcRange().getBegin(), Var, false,
2329 DefaultLoc, Var->getType(), VK_LValue);
2330}
2331
2332/// \brief Build initization of the counter be used for codegen.
2333Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
2334
2335/// \brief Build step of the counter be used for codegen.
2336Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
2337
2338/// \brief Iteration space of a single for loop.
2339struct LoopIterationSpace {
2340 /// \brief This expression calculates the number of iterations in the loop.
2341 /// It is always possible to calculate it before starting the loop.
2342 Expr *NumIterations;
2343 /// \brief The loop counter variable.
2344 Expr *CounterVar;
2345 /// \brief This is initializer for the initial value of #CounterVar.
2346 Expr *CounterInit;
2347 /// \brief This is step for the #CounterVar used to generate its update:
2348 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
2349 Expr *CounterStep;
2350 /// \brief Should step be subtracted?
2351 bool Subtract;
2352 /// \brief Source range of the loop init.
2353 SourceRange InitSrcRange;
2354 /// \brief Source range of the loop condition.
2355 SourceRange CondSrcRange;
2356 /// \brief Source range of the loop increment.
2357 SourceRange IncSrcRange;
2358};
2359
Alexey Bataev23b69422014-06-18 07:08:49 +00002360} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002361
2362/// \brief Called on a for stmt to check and extract its iteration space
2363/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00002364static bool CheckOpenMPIterationSpace(
2365 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
2366 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
2367 Expr *NestedLoopCountExpr,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002368 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
2369 LoopIterationSpace &ResultIterSpace) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002370 // OpenMP [2.6, Canonical Loop Form]
2371 // for (init-expr; test-expr; incr-expr) structured-block
2372 auto For = dyn_cast_or_null<ForStmt>(S);
2373 if (!For) {
2374 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002375 << (NestedLoopCountExpr != nullptr) << getOpenMPDirectiveName(DKind)
2376 << NestedLoopCount << (CurrentNestedLoopCount > 0)
2377 << CurrentNestedLoopCount;
2378 if (NestedLoopCount > 1)
2379 SemaRef.Diag(NestedLoopCountExpr->getExprLoc(),
2380 diag::note_omp_collapse_expr)
2381 << NestedLoopCountExpr->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002382 return true;
2383 }
2384 assert(For->getBody());
2385
2386 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
2387
2388 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002389 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002390 if (ISC.CheckInit(Init)) {
2391 return true;
2392 }
2393
2394 bool HasErrors = false;
2395
2396 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002397 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002398
2399 // OpenMP [2.6, Canonical Loop Form]
2400 // Var is one of the following:
2401 // A variable of signed or unsigned integer type.
2402 // For C++, a variable of a random access iterator type.
2403 // For C, a variable of a pointer type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002404 auto VarType = Var->getType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002405 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
2406 !VarType->isPointerType() &&
2407 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
2408 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
2409 << SemaRef.getLangOpts().CPlusPlus;
2410 HasErrors = true;
2411 }
2412
Alexey Bataev4acb8592014-07-07 13:01:15 +00002413 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
2414 // Construct
2415 // The loop iteration variable(s) in the associated for-loop(s) of a for or
2416 // parallel for construct is (are) private.
2417 // The loop iteration variable in the associated for-loop of a simd construct
2418 // with just one associated for-loop is linear with a constant-linear-step
2419 // that is the increment of the associated for-loop.
2420 // Exclude loop var from the list of variables with implicitly defined data
2421 // sharing attributes.
Benjamin Kramerad8e0792014-10-10 15:32:48 +00002422 VarsWithImplicitDSA.erase(Var);
Alexey Bataev4acb8592014-07-07 13:01:15 +00002423
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002424 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
2425 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00002426 // The loop iteration variable in the associated for-loop of a simd construct
2427 // with just one associated for-loop may be listed in a linear clause with a
2428 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002429 // The loop iteration variable(s) in the associated for-loop(s) of a for or
2430 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002431 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002432 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
2433 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
2434 // declared in the loop and it is predetermined as a private.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002435 auto PredeterminedCKind =
2436 isOpenMPSimdDirective(DKind)
2437 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
2438 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002439 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00002440 DVar.CKind != PredeterminedCKind) ||
Alexander Musmanf82886e2014-09-18 05:12:34 +00002441 (isOpenMPWorksharingDirective(DKind) && !isOpenMPSimdDirective(DKind) &&
2442 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private &&
2443 DVar.CKind != OMPC_lastprivate)) &&
Alexander Musman1bb328c2014-06-04 13:06:39 +00002444 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002445 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00002446 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
2447 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002448 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002449 HasErrors = true;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002450 } else if (LoopVarRefExpr != nullptr) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00002451 // Make the loop iteration variable private (for worksharing constructs),
2452 // linear (for simd directives with the only one associated loop) or
2453 // lastprivate (for simd directives with several collapsed loops).
Alexey Bataev9aba41c2014-11-14 04:08:45 +00002454 // FIXME: the next check and error message must be removed once the
2455 // capturing of global variables in loops is fixed.
2456 if (DVar.CKind == OMPC_unknown)
2457 DVar = DSA.hasDSA(Var, isOpenMPPrivate, MatchesAlways(),
2458 /*FromParent=*/false);
2459 if (!Var->hasLocalStorage() && DVar.CKind == OMPC_unknown) {
2460 SemaRef.Diag(Init->getLocStart(), diag::err_omp_global_loop_var_dsa)
2461 << getOpenMPClauseName(PredeterminedCKind)
2462 << getOpenMPDirectiveName(DKind);
2463 HasErrors = true;
2464 } else
2465 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002466 }
2467
Alexey Bataev7ff55242014-06-19 09:13:45 +00002468 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00002469
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002470 // Check test-expr.
2471 HasErrors |= ISC.CheckCond(For->getCond());
2472
2473 // Check incr-expr.
2474 HasErrors |= ISC.CheckInc(For->getInc());
2475
Alexander Musmana5f070a2014-10-01 06:03:56 +00002476 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002477 return HasErrors;
2478
Alexander Musmana5f070a2014-10-01 06:03:56 +00002479 // Build the loop's iteration space representation.
Alexander Musman174b3ca2014-10-06 11:16:29 +00002480 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
2481 DSA.getCurScope(), /* LimitedType */ isOpenMPWorksharingDirective(DKind));
Alexander Musmana5f070a2014-10-01 06:03:56 +00002482 ResultIterSpace.CounterVar = ISC.BuildCounterVar();
2483 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
2484 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
2485 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
2486 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
2487 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
2488 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
2489
2490 HasErrors |= (ResultIterSpace.NumIterations == nullptr ||
2491 ResultIterSpace.CounterVar == nullptr ||
2492 ResultIterSpace.CounterInit == nullptr ||
2493 ResultIterSpace.CounterStep == nullptr);
2494
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002495 return HasErrors;
2496}
2497
Alexander Musmana5f070a2014-10-01 06:03:56 +00002498/// \brief Build a variable declaration for OpenMP loop iteration variable.
2499static VarDecl *BuildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
2500 StringRef Name) {
2501 DeclContext *DC = SemaRef.CurContext;
2502 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
2503 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
2504 VarDecl *Decl =
2505 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
2506 Decl->setImplicit();
2507 return Decl;
2508}
2509
2510/// \brief Build 'VarRef = Start + Iter * Step'.
2511static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S,
2512 SourceLocation Loc, ExprResult VarRef,
2513 ExprResult Start, ExprResult Iter,
2514 ExprResult Step, bool Subtract) {
2515 // Add parentheses (for debugging purposes only).
2516 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
2517 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
2518 !Step.isUsable())
2519 return ExprError();
2520
2521 ExprResult Update = SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(),
2522 Step.get()->IgnoreImplicit());
2523 if (!Update.isUsable())
2524 return ExprError();
2525
2526 // Build 'VarRef = Start + Iter * Step'.
2527 Update = SemaRef.BuildBinOp(S, Loc, (Subtract ? BO_Sub : BO_Add),
2528 Start.get()->IgnoreImplicit(), Update.get());
2529 if (!Update.isUsable())
2530 return ExprError();
2531
2532 Update = SemaRef.PerformImplicitConversion(
2533 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
2534 if (!Update.isUsable())
2535 return ExprError();
2536
2537 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
2538 return Update;
2539}
2540
2541/// \brief Convert integer expression \a E to make it have at least \a Bits
2542/// bits.
2543static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
2544 Sema &SemaRef) {
2545 if (E == nullptr)
2546 return ExprError();
2547 auto &C = SemaRef.Context;
2548 QualType OldType = E->getType();
2549 unsigned HasBits = C.getTypeSize(OldType);
2550 if (HasBits >= Bits)
2551 return ExprResult(E);
2552 // OK to convert to signed, because new type has more bits than old.
2553 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
2554 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
2555 true);
2556}
2557
2558/// \brief Check if the given expression \a E is a constant integer that fits
2559/// into \a Bits bits.
2560static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
2561 if (E == nullptr)
2562 return false;
2563 llvm::APSInt Result;
2564 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
2565 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
2566 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002567}
2568
2569/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00002570/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
2571/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002572static unsigned
2573CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *NestedLoopCountExpr,
2574 Stmt *AStmt, Sema &SemaRef, DSAStackTy &DSA,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002575 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00002576 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002577 unsigned NestedLoopCount = 1;
2578 if (NestedLoopCountExpr) {
2579 // Found 'collapse' clause - calculate collapse number.
2580 llvm::APSInt Result;
2581 if (NestedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
2582 NestedLoopCount = Result.getLimitedValue();
2583 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002584 // This is helper routine for loop directives (e.g., 'for', 'simd',
2585 // 'for simd', etc.).
Alexander Musmana5f070a2014-10-01 06:03:56 +00002586 SmallVector<LoopIterationSpace, 4> IterSpaces;
2587 IterSpaces.resize(NestedLoopCount);
2588 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002589 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002590 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev4acb8592014-07-07 13:01:15 +00002591 NestedLoopCount, NestedLoopCountExpr,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002592 VarsWithImplicitDSA, IterSpaces[Cnt]))
Alexey Bataevabfc0692014-06-25 06:52:00 +00002593 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002594 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00002595 // OpenMP [2.8.1, simd construct, Restrictions]
2596 // All loops associated with the construct must be perfectly nested; that
2597 // is, there must be no intervening code nor any OpenMP directive between
2598 // any two loops.
2599 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002600 }
2601
Alexander Musmana5f070a2014-10-01 06:03:56 +00002602 Built.clear(/* size */ NestedLoopCount);
2603
2604 if (SemaRef.CurContext->isDependentContext())
2605 return NestedLoopCount;
2606
2607 // An example of what is generated for the following code:
2608 //
2609 // #pragma omp simd collapse(2)
2610 // for (i = 0; i < NI; ++i)
2611 // for (j = J0; j < NJ; j+=2) {
2612 // <loop body>
2613 // }
2614 //
2615 // We generate the code below.
2616 // Note: the loop body may be outlined in CodeGen.
2617 // Note: some counters may be C++ classes, operator- is used to find number of
2618 // iterations and operator+= to calculate counter value.
2619 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
2620 // or i64 is currently supported).
2621 //
2622 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
2623 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
2624 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
2625 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
2626 // // similar updates for vars in clauses (e.g. 'linear')
2627 // <loop body (using local i and j)>
2628 // }
2629 // i = NI; // assign final values of counters
2630 // j = NJ;
2631 //
2632
2633 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
2634 // the iteration counts of the collapsed for loops.
2635 auto N0 = IterSpaces[0].NumIterations;
2636 ExprResult LastIteration32 = WidenIterationCount(32 /* Bits */, N0, SemaRef);
2637 ExprResult LastIteration64 = WidenIterationCount(64 /* Bits */, N0, SemaRef);
2638
2639 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
2640 return NestedLoopCount;
2641
2642 auto &C = SemaRef.Context;
2643 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
2644
2645 Scope *CurScope = DSA.getCurScope();
2646 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
2647 auto N = IterSpaces[Cnt].NumIterations;
2648 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
2649 if (LastIteration32.isUsable())
2650 LastIteration32 = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_Mul,
2651 LastIteration32.get(), N);
2652 if (LastIteration64.isUsable())
2653 LastIteration64 = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_Mul,
2654 LastIteration64.get(), N);
2655 }
2656
2657 // Choose either the 32-bit or 64-bit version.
2658 ExprResult LastIteration = LastIteration64;
2659 if (LastIteration32.isUsable() &&
2660 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
2661 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
2662 FitsInto(
2663 32 /* Bits */,
2664 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
2665 LastIteration64.get(), SemaRef)))
2666 LastIteration = LastIteration32;
2667
2668 if (!LastIteration.isUsable())
2669 return 0;
2670
2671 // Save the number of iterations.
2672 ExprResult NumIterations = LastIteration;
2673 {
2674 LastIteration = SemaRef.BuildBinOp(
2675 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
2676 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
2677 if (!LastIteration.isUsable())
2678 return 0;
2679 }
2680
2681 // Calculate the last iteration number beforehand instead of doing this on
2682 // each iteration. Do not do this if the number of iterations may be kfold-ed.
2683 llvm::APSInt Result;
2684 bool IsConstant =
2685 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
2686 ExprResult CalcLastIteration;
2687 if (!IsConstant) {
2688 SourceLocation SaveLoc;
2689 VarDecl *SaveVar =
2690 BuildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(),
2691 ".omp.last.iteration");
2692 ExprResult SaveRef = SemaRef.BuildDeclRefExpr(
2693 SaveVar, LastIteration.get()->getType(), VK_LValue, SaveLoc);
2694 CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign,
2695 SaveRef.get(), LastIteration.get());
2696 LastIteration = SaveRef;
2697
2698 // Prepare SaveRef + 1.
2699 NumIterations = SemaRef.BuildBinOp(
2700 CurScope, SaveLoc, BO_Add, SaveRef.get(),
2701 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
2702 if (!NumIterations.isUsable())
2703 return 0;
2704 }
2705
2706 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
2707
2708 // Precondition tests if there is at least one iteration (LastIteration > 0).
2709 ExprResult PreCond = SemaRef.BuildBinOp(
2710 CurScope, InitLoc, BO_GT, LastIteration.get(),
2711 SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get());
2712
Alexander Musmanc6388682014-12-15 07:07:06 +00002713 QualType VType = LastIteration.get()->getType();
2714 // Build variables passed into runtime, nesessary for worksharing directives.
2715 ExprResult LB, UB, IL, ST, EUB;
2716 if (isOpenMPWorksharingDirective(DKind)) {
2717 // Lower bound variable, initialized with zero.
2718 VarDecl *LBDecl = BuildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
2719 LB = SemaRef.BuildDeclRefExpr(LBDecl, VType, VK_LValue, InitLoc);
2720 SemaRef.AddInitializerToDecl(
2721 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
2722 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
2723
2724 // Upper bound variable, initialized with last iteration number.
2725 VarDecl *UBDecl = BuildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
2726 UB = SemaRef.BuildDeclRefExpr(UBDecl, VType, VK_LValue, InitLoc);
2727 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
2728 /*DirectInit*/ false,
2729 /*TypeMayContainAuto*/ false);
2730
2731 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
2732 // This will be used to implement clause 'lastprivate'.
2733 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
2734 VarDecl *ILDecl = BuildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
2735 IL = SemaRef.BuildDeclRefExpr(ILDecl, Int32Ty, VK_LValue, InitLoc);
2736 SemaRef.AddInitializerToDecl(
2737 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
2738 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
2739
2740 // Stride variable returned by runtime (we initialize it to 1 by default).
2741 VarDecl *STDecl = BuildVarDecl(SemaRef, InitLoc, VType, ".omp.stride");
2742 ST = SemaRef.BuildDeclRefExpr(STDecl, VType, VK_LValue, InitLoc);
2743 SemaRef.AddInitializerToDecl(
2744 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
2745 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
2746
2747 // Build expression: UB = min(UB, LastIteration)
2748 // It is nesessary for CodeGen of directives with static scheduling.
2749 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
2750 UB.get(), LastIteration.get());
2751 ExprResult CondOp = SemaRef.ActOnConditionalOp(
2752 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
2753 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
2754 CondOp.get());
2755 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
2756 }
2757
2758 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00002759 ExprResult IV;
2760 ExprResult Init;
2761 {
Alexander Musmanc6388682014-12-15 07:07:06 +00002762 VarDecl *IVDecl = BuildVarDecl(SemaRef, InitLoc, VType, ".omp.iv");
2763 IV = SemaRef.BuildDeclRefExpr(IVDecl, VType, VK_LValue, InitLoc);
2764 Expr *RHS = isOpenMPWorksharingDirective(DKind)
2765 ? LB.get()
2766 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
2767 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
2768 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00002769 }
2770
Alexander Musmanc6388682014-12-15 07:07:06 +00002771 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00002772 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00002773 ExprResult Cond =
2774 isOpenMPWorksharingDirective(DKind)
2775 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
2776 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
2777 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00002778 // Loop condition with 1 iteration separated (IV < LastIteration)
2779 ExprResult SeparatedCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT,
2780 IV.get(), LastIteration.get());
2781
2782 // Loop increment (IV = IV + 1)
2783 SourceLocation IncLoc;
2784 ExprResult Inc =
2785 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
2786 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
2787 if (!Inc.isUsable())
2788 return 0;
2789 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00002790 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
2791 if (!Inc.isUsable())
2792 return 0;
2793
2794 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
2795 // Used for directives with static scheduling.
2796 ExprResult NextLB, NextUB;
2797 if (isOpenMPWorksharingDirective(DKind)) {
2798 // LB + ST
2799 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
2800 if (!NextLB.isUsable())
2801 return 0;
2802 // LB = LB + ST
2803 NextLB =
2804 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
2805 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
2806 if (!NextLB.isUsable())
2807 return 0;
2808 // UB + ST
2809 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
2810 if (!NextUB.isUsable())
2811 return 0;
2812 // UB = UB + ST
2813 NextUB =
2814 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
2815 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
2816 if (!NextUB.isUsable())
2817 return 0;
2818 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002819
2820 // Build updates and final values of the loop counters.
2821 bool HasErrors = false;
2822 Built.Counters.resize(NestedLoopCount);
2823 Built.Updates.resize(NestedLoopCount);
2824 Built.Finals.resize(NestedLoopCount);
2825 {
2826 ExprResult Div;
2827 // Go from inner nested loop to outer.
2828 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
2829 LoopIterationSpace &IS = IterSpaces[Cnt];
2830 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
2831 // Build: Iter = (IV / Div) % IS.NumIters
2832 // where Div is product of previous iterations' IS.NumIters.
2833 ExprResult Iter;
2834 if (Div.isUsable()) {
2835 Iter =
2836 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
2837 } else {
2838 Iter = IV;
2839 assert((Cnt == (int)NestedLoopCount - 1) &&
2840 "unusable div expected on first iteration only");
2841 }
2842
2843 if (Cnt != 0 && Iter.isUsable())
2844 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
2845 IS.NumIterations);
2846 if (!Iter.isUsable()) {
2847 HasErrors = true;
2848 break;
2849 }
2850
2851 // Build update: IS.CounterVar = IS.Start + Iter * IS.Step
2852 ExprResult Update =
2853 BuildCounterUpdate(SemaRef, CurScope, UpdLoc, IS.CounterVar,
2854 IS.CounterInit, Iter, IS.CounterStep, IS.Subtract);
2855 if (!Update.isUsable()) {
2856 HasErrors = true;
2857 break;
2858 }
2859
2860 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
2861 ExprResult Final = BuildCounterUpdate(
2862 SemaRef, CurScope, UpdLoc, IS.CounterVar, IS.CounterInit,
2863 IS.NumIterations, IS.CounterStep, IS.Subtract);
2864 if (!Final.isUsable()) {
2865 HasErrors = true;
2866 break;
2867 }
2868
2869 // Build Div for the next iteration: Div <- Div * IS.NumIters
2870 if (Cnt != 0) {
2871 if (Div.isUnset())
2872 Div = IS.NumIterations;
2873 else
2874 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
2875 IS.NumIterations);
2876
2877 // Add parentheses (for debugging purposes only).
2878 if (Div.isUsable())
2879 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
2880 if (!Div.isUsable()) {
2881 HasErrors = true;
2882 break;
2883 }
2884 }
2885 if (!Update.isUsable() || !Final.isUsable()) {
2886 HasErrors = true;
2887 break;
2888 }
2889 // Save results
2890 Built.Counters[Cnt] = IS.CounterVar;
2891 Built.Updates[Cnt] = Update.get();
2892 Built.Finals[Cnt] = Final.get();
2893 }
2894 }
2895
2896 if (HasErrors)
2897 return 0;
2898
2899 // Save results
2900 Built.IterationVarRef = IV.get();
2901 Built.LastIteration = LastIteration.get();
2902 Built.CalcLastIteration = CalcLastIteration.get();
2903 Built.PreCond = PreCond.get();
2904 Built.Cond = Cond.get();
2905 Built.SeparatedCond = SeparatedCond.get();
2906 Built.Init = Init.get();
2907 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00002908 Built.LB = LB.get();
2909 Built.UB = UB.get();
2910 Built.IL = IL.get();
2911 Built.ST = ST.get();
2912 Built.EUB = EUB.get();
2913 Built.NLB = NextLB.get();
2914 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00002915
Alexey Bataevabfc0692014-06-25 06:52:00 +00002916 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002917}
2918
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002919static Expr *GetCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002920 auto CollapseFilter = [](const OMPClause *C) -> bool {
2921 return C->getClauseKind() == OMPC_collapse;
2922 };
2923 OMPExecutableDirective::filtered_clause_iterator<decltype(CollapseFilter)> I(
2924 Clauses, CollapseFilter);
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002925 if (I)
2926 return cast<OMPCollapseClause>(*I)->getNumForLoops();
2927 return nullptr;
2928}
2929
Alexey Bataev4acb8592014-07-07 13:01:15 +00002930StmtResult Sema::ActOnOpenMPSimdDirective(
2931 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2932 SourceLocation EndLoc,
2933 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmanc6388682014-12-15 07:07:06 +00002934 OMPLoopDirective::HelperExprs B;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002935 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002936 unsigned NestedLoopCount =
2937 CheckOpenMPLoop(OMPD_simd, GetCollapseNumberExpr(Clauses), AStmt, *this,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002938 *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00002939 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002940 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002941
Alexander Musmana5f070a2014-10-01 06:03:56 +00002942 assert((CurContext->isDependentContext() || B.builtAll()) &&
2943 "omp simd loop exprs were not built");
2944
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002945 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00002946 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
2947 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002948}
2949
Alexey Bataev4acb8592014-07-07 13:01:15 +00002950StmtResult Sema::ActOnOpenMPForDirective(
2951 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2952 SourceLocation EndLoc,
2953 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmanc6388682014-12-15 07:07:06 +00002954 OMPLoopDirective::HelperExprs B;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002955 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002956 unsigned NestedLoopCount =
2957 CheckOpenMPLoop(OMPD_for, GetCollapseNumberExpr(Clauses), AStmt, *this,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002958 *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00002959 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00002960 return StmtError();
2961
Alexander Musmana5f070a2014-10-01 06:03:56 +00002962 assert((CurContext->isDependentContext() || B.builtAll()) &&
2963 "omp for loop exprs were not built");
2964
Alexey Bataevf29276e2014-06-18 04:14:57 +00002965 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00002966 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
2967 Clauses, AStmt, B);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002968}
2969
Alexander Musmanf82886e2014-09-18 05:12:34 +00002970StmtResult Sema::ActOnOpenMPForSimdDirective(
2971 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2972 SourceLocation EndLoc,
2973 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmanc6388682014-12-15 07:07:06 +00002974 OMPLoopDirective::HelperExprs B;
Alexander Musmanf82886e2014-09-18 05:12:34 +00002975 // In presence of clause 'collapse', it will define the nested loops number.
2976 unsigned NestedLoopCount =
2977 CheckOpenMPLoop(OMPD_for_simd, GetCollapseNumberExpr(Clauses), AStmt,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002978 *this, *DSAStack, VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00002979 if (NestedLoopCount == 0)
2980 return StmtError();
2981
Alexander Musmanc6388682014-12-15 07:07:06 +00002982 assert((CurContext->isDependentContext() || B.builtAll()) &&
2983 "omp for simd loop exprs were not built");
2984
Alexander Musmanf82886e2014-09-18 05:12:34 +00002985 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00002986 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
2987 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00002988}
2989
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002990StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
2991 Stmt *AStmt,
2992 SourceLocation StartLoc,
2993 SourceLocation EndLoc) {
2994 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2995 auto BaseStmt = AStmt;
2996 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
2997 BaseStmt = CS->getCapturedStmt();
2998 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
2999 auto S = C->children();
3000 if (!S)
3001 return StmtError();
3002 // All associated statements must be '#pragma omp section' except for
3003 // the first one.
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003004 for (++S; S; ++S) {
3005 auto SectionStmt = *S;
3006 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
3007 if (SectionStmt)
3008 Diag(SectionStmt->getLocStart(),
3009 diag::err_omp_sections_substmt_not_section);
3010 return StmtError();
3011 }
3012 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003013 } else {
3014 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
3015 return StmtError();
3016 }
3017
3018 getCurFunction()->setHasBranchProtectedScope();
3019
3020 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses,
3021 AStmt);
3022}
3023
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003024StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
3025 SourceLocation StartLoc,
3026 SourceLocation EndLoc) {
3027 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3028
3029 getCurFunction()->setHasBranchProtectedScope();
3030
3031 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt);
3032}
3033
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003034StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
3035 Stmt *AStmt,
3036 SourceLocation StartLoc,
3037 SourceLocation EndLoc) {
Alexey Bataev74a05c92014-07-15 02:55:09 +00003038 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3039
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003040 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00003041
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003042 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3043}
3044
Alexander Musman80c22892014-07-17 08:54:58 +00003045StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
3046 SourceLocation StartLoc,
3047 SourceLocation EndLoc) {
3048 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3049
3050 getCurFunction()->setHasBranchProtectedScope();
3051
3052 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
3053}
3054
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003055StmtResult
3056Sema::ActOnOpenMPCriticalDirective(const DeclarationNameInfo &DirName,
3057 Stmt *AStmt, SourceLocation StartLoc,
3058 SourceLocation EndLoc) {
3059 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3060
3061 getCurFunction()->setHasBranchProtectedScope();
3062
3063 return OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
3064 AStmt);
3065}
3066
Alexey Bataev4acb8592014-07-07 13:01:15 +00003067StmtResult Sema::ActOnOpenMPParallelForDirective(
3068 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3069 SourceLocation EndLoc,
3070 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
3071 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3072 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3073 // 1.2.2 OpenMP Language Terminology
3074 // Structured block - An executable statement with a single entry at the
3075 // top and a single exit at the bottom.
3076 // The point of exit cannot be a branch out of the structured block.
3077 // longjmp() and throw() must not violate the entry/exit criteria.
3078 CS->getCapturedDecl()->setNothrow();
3079
Alexander Musmanc6388682014-12-15 07:07:06 +00003080 OMPLoopDirective::HelperExprs B;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003081 // In presence of clause 'collapse', it will define the nested loops number.
3082 unsigned NestedLoopCount =
3083 CheckOpenMPLoop(OMPD_parallel_for, GetCollapseNumberExpr(Clauses), AStmt,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003084 *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003085 if (NestedLoopCount == 0)
3086 return StmtError();
3087
Alexander Musmana5f070a2014-10-01 06:03:56 +00003088 assert((CurContext->isDependentContext() || B.builtAll()) &&
3089 "omp parallel for loop exprs were not built");
3090
Alexey Bataev4acb8592014-07-07 13:01:15 +00003091 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003092 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
3093 NestedLoopCount, Clauses, AStmt, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003094}
3095
Alexander Musmane4e893b2014-09-23 09:33:00 +00003096StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
3097 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3098 SourceLocation EndLoc,
3099 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
3100 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3101 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3102 // 1.2.2 OpenMP Language Terminology
3103 // Structured block - An executable statement with a single entry at the
3104 // top and a single exit at the bottom.
3105 // The point of exit cannot be a branch out of the structured block.
3106 // longjmp() and throw() must not violate the entry/exit criteria.
3107 CS->getCapturedDecl()->setNothrow();
3108
Alexander Musmanc6388682014-12-15 07:07:06 +00003109 OMPLoopDirective::HelperExprs B;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003110 // In presence of clause 'collapse', it will define the nested loops number.
3111 unsigned NestedLoopCount =
3112 CheckOpenMPLoop(OMPD_parallel_for_simd, GetCollapseNumberExpr(Clauses),
Alexander Musmana5f070a2014-10-01 06:03:56 +00003113 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003114 if (NestedLoopCount == 0)
3115 return StmtError();
3116
3117 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003118 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00003119 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003120}
3121
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003122StmtResult
3123Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
3124 Stmt *AStmt, SourceLocation StartLoc,
3125 SourceLocation EndLoc) {
3126 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3127 auto BaseStmt = AStmt;
3128 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
3129 BaseStmt = CS->getCapturedStmt();
3130 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
3131 auto S = C->children();
3132 if (!S)
3133 return StmtError();
3134 // All associated statements must be '#pragma omp section' except for
3135 // the first one.
3136 for (++S; S; ++S) {
3137 auto SectionStmt = *S;
3138 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
3139 if (SectionStmt)
3140 Diag(SectionStmt->getLocStart(),
3141 diag::err_omp_parallel_sections_substmt_not_section);
3142 return StmtError();
3143 }
3144 }
3145 } else {
3146 Diag(AStmt->getLocStart(),
3147 diag::err_omp_parallel_sections_not_compound_stmt);
3148 return StmtError();
3149 }
3150
3151 getCurFunction()->setHasBranchProtectedScope();
3152
3153 return OMPParallelSectionsDirective::Create(Context, StartLoc, EndLoc,
3154 Clauses, AStmt);
3155}
3156
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003157StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
3158 Stmt *AStmt, SourceLocation StartLoc,
3159 SourceLocation EndLoc) {
3160 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3161 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3162 // 1.2.2 OpenMP Language Terminology
3163 // Structured block - An executable statement with a single entry at the
3164 // top and a single exit at the bottom.
3165 // The point of exit cannot be a branch out of the structured block.
3166 // longjmp() and throw() must not violate the entry/exit criteria.
3167 CS->getCapturedDecl()->setNothrow();
3168
3169 getCurFunction()->setHasBranchProtectedScope();
3170
3171 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3172}
3173
Alexey Bataev68446b72014-07-18 07:47:19 +00003174StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
3175 SourceLocation EndLoc) {
3176 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
3177}
3178
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003179StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
3180 SourceLocation EndLoc) {
3181 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
3182}
3183
Alexey Bataev2df347a2014-07-18 10:17:07 +00003184StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
3185 SourceLocation EndLoc) {
3186 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
3187}
3188
Alexey Bataev6125da92014-07-21 11:26:11 +00003189StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
3190 SourceLocation StartLoc,
3191 SourceLocation EndLoc) {
3192 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
3193 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
3194}
3195
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003196StmtResult Sema::ActOnOpenMPOrderedDirective(Stmt *AStmt,
3197 SourceLocation StartLoc,
3198 SourceLocation EndLoc) {
3199 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3200
3201 getCurFunction()->setHasBranchProtectedScope();
3202
3203 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, AStmt);
3204}
3205
Alexey Bataev0162e452014-07-22 10:10:35 +00003206StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
3207 Stmt *AStmt,
3208 SourceLocation StartLoc,
3209 SourceLocation EndLoc) {
3210 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003211 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00003212 // 1.2.2 OpenMP Language Terminology
3213 // Structured block - An executable statement with a single entry at the
3214 // top and a single exit at the bottom.
3215 // The point of exit cannot be a branch out of the structured block.
3216 // longjmp() and throw() must not violate the entry/exit criteria.
3217 // TODO further analysis of associated statements and clauses.
Alexey Bataevdea47612014-07-23 07:46:59 +00003218 OpenMPClauseKind AtomicKind = OMPC_unknown;
3219 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003220 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00003221 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00003222 C->getClauseKind() == OMPC_update ||
3223 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00003224 if (AtomicKind != OMPC_unknown) {
3225 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
3226 << SourceRange(C->getLocStart(), C->getLocEnd());
3227 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
3228 << getOpenMPClauseName(AtomicKind);
3229 } else {
3230 AtomicKind = C->getClauseKind();
3231 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003232 }
3233 }
3234 }
Alexey Bataev62cec442014-11-18 10:14:22 +00003235
Alexey Bataev459dec02014-07-24 06:46:57 +00003236 auto Body = CS->getCapturedStmt();
Alexey Bataev62cec442014-11-18 10:14:22 +00003237 Expr *X = nullptr;
3238 Expr *V = nullptr;
3239 Expr *E = nullptr;
3240 // OpenMP [2.12.6, atomic Construct]
3241 // In the next expressions:
3242 // * x and v (as applicable) are both l-value expressions with scalar type.
3243 // * During the execution of an atomic region, multiple syntactic
3244 // occurrences of x must designate the same storage location.
3245 // * Neither of v and expr (as applicable) may access the storage location
3246 // designated by x.
3247 // * Neither of x and expr (as applicable) may access the storage location
3248 // designated by v.
3249 // * expr is an expression with scalar type.
3250 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
3251 // * binop, binop=, ++, and -- are not overloaded operators.
3252 // * The expression x binop expr must be numerically equivalent to x binop
3253 // (expr). This requirement is satisfied if the operators in expr have
3254 // precedence greater than binop, or by using parentheses around expr or
3255 // subexpressions of expr.
3256 // * The expression expr binop x must be numerically equivalent to (expr)
3257 // binop x. This requirement is satisfied if the operators in expr have
3258 // precedence equal to or greater than binop, or by using parentheses around
3259 // expr or subexpressions of expr.
3260 // * For forms that allow multiple occurrences of x, the number of times
3261 // that x is evaluated is unspecified.
Alexey Bataevf33eba62014-11-28 07:21:40 +00003262 enum {
3263 NotAnExpression,
3264 NotAnAssignmentOp,
3265 NotAScalarType,
3266 NotAnLValue,
3267 NoError
3268 } ErrorFound = NoError;
Alexey Bataevdea47612014-07-23 07:46:59 +00003269 if (AtomicKind == OMPC_read) {
Alexey Bataev62cec442014-11-18 10:14:22 +00003270 SourceLocation ErrorLoc, NoteLoc;
3271 SourceRange ErrorRange, NoteRange;
3272 // If clause is read:
3273 // v = x;
3274 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
3275 auto AtomicBinOp =
3276 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
3277 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
3278 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
3279 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
3280 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
3281 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
3282 if (!X->isLValue() || !V->isLValue()) {
3283 auto NotLValueExpr = X->isLValue() ? V : X;
3284 ErrorFound = NotAnLValue;
3285 ErrorLoc = AtomicBinOp->getExprLoc();
3286 ErrorRange = AtomicBinOp->getSourceRange();
3287 NoteLoc = NotLValueExpr->getExprLoc();
3288 NoteRange = NotLValueExpr->getSourceRange();
3289 }
3290 } else if (!X->isInstantiationDependent() ||
3291 !V->isInstantiationDependent()) {
3292 auto NotScalarExpr =
3293 (X->isInstantiationDependent() || X->getType()->isScalarType())
3294 ? V
3295 : X;
3296 ErrorFound = NotAScalarType;
3297 ErrorLoc = AtomicBinOp->getExprLoc();
3298 ErrorRange = AtomicBinOp->getSourceRange();
3299 NoteLoc = NotScalarExpr->getExprLoc();
3300 NoteRange = NotScalarExpr->getSourceRange();
3301 }
3302 } else {
3303 ErrorFound = NotAnAssignmentOp;
3304 ErrorLoc = AtomicBody->getExprLoc();
3305 ErrorRange = AtomicBody->getSourceRange();
3306 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
3307 : AtomicBody->getExprLoc();
3308 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
3309 : AtomicBody->getSourceRange();
3310 }
3311 } else {
3312 ErrorFound = NotAnExpression;
3313 NoteLoc = ErrorLoc = Body->getLocStart();
3314 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00003315 }
Alexey Bataev62cec442014-11-18 10:14:22 +00003316 if (ErrorFound != NoError) {
3317 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
3318 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00003319 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
3320 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00003321 return StmtError();
3322 } else if (CurContext->isDependentContext())
3323 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00003324 } else if (AtomicKind == OMPC_write) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00003325 SourceLocation ErrorLoc, NoteLoc;
3326 SourceRange ErrorRange, NoteRange;
3327 // If clause is write:
3328 // x = expr;
3329 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
3330 auto AtomicBinOp =
3331 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
3332 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
3333 X = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
3334 E = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
3335 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
3336 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
3337 if (!X->isLValue()) {
3338 ErrorFound = NotAnLValue;
3339 ErrorLoc = AtomicBinOp->getExprLoc();
3340 ErrorRange = AtomicBinOp->getSourceRange();
3341 NoteLoc = X->getExprLoc();
3342 NoteRange = X->getSourceRange();
3343 }
3344 } else if (!X->isInstantiationDependent() ||
3345 !E->isInstantiationDependent()) {
3346 auto NotScalarExpr =
3347 (X->isInstantiationDependent() || X->getType()->isScalarType())
3348 ? E
3349 : X;
3350 ErrorFound = NotAScalarType;
3351 ErrorLoc = AtomicBinOp->getExprLoc();
3352 ErrorRange = AtomicBinOp->getSourceRange();
3353 NoteLoc = NotScalarExpr->getExprLoc();
3354 NoteRange = NotScalarExpr->getSourceRange();
3355 }
3356 } else {
3357 ErrorFound = NotAnAssignmentOp;
3358 ErrorLoc = AtomicBody->getExprLoc();
3359 ErrorRange = AtomicBody->getSourceRange();
3360 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
3361 : AtomicBody->getExprLoc();
3362 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
3363 : AtomicBody->getSourceRange();
3364 }
3365 } else {
3366 ErrorFound = NotAnExpression;
3367 NoteLoc = ErrorLoc = Body->getLocStart();
3368 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00003369 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00003370 if (ErrorFound != NoError) {
3371 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
3372 << ErrorRange;
3373 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
3374 << NoteRange;
3375 return StmtError();
3376 } else if (CurContext->isDependentContext())
3377 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00003378 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev459dec02014-07-24 06:46:57 +00003379 if (!isa<Expr>(Body)) {
3380 Diag(Body->getLocStart(),
Alexey Bataev67a4f222014-07-23 10:25:33 +00003381 diag::err_omp_atomic_update_not_expression_statement)
3382 << (AtomicKind == OMPC_update);
3383 return StmtError();
3384 }
Alexey Bataev459dec02014-07-24 06:46:57 +00003385 } else if (AtomicKind == OMPC_capture) {
3386 if (isa<Expr>(Body) && !isa<BinaryOperator>(Body)) {
3387 Diag(Body->getLocStart(),
3388 diag::err_omp_atomic_capture_not_expression_statement);
3389 return StmtError();
3390 } else if (!isa<Expr>(Body) && !isa<CompoundStmt>(Body)) {
3391 Diag(Body->getLocStart(),
3392 diag::err_omp_atomic_capture_not_compound_statement);
3393 return StmtError();
3394 }
Alexey Bataevdea47612014-07-23 07:46:59 +00003395 }
Alexey Bataev0162e452014-07-22 10:10:35 +00003396
3397 getCurFunction()->setHasBranchProtectedScope();
3398
Alexey Bataev62cec442014-11-18 10:14:22 +00003399 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3400 X, V, E);
Alexey Bataev0162e452014-07-22 10:10:35 +00003401}
3402
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003403StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
3404 Stmt *AStmt,
3405 SourceLocation StartLoc,
3406 SourceLocation EndLoc) {
3407 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3408
Alexey Bataev13314bf2014-10-09 04:18:56 +00003409 // OpenMP [2.16, Nesting of Regions]
3410 // If specified, a teams construct must be contained within a target
3411 // construct. That target construct must contain no statements or directives
3412 // outside of the teams construct.
3413 if (DSAStack->hasInnerTeamsRegion()) {
3414 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
3415 bool OMPTeamsFound = true;
3416 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
3417 auto I = CS->body_begin();
3418 while (I != CS->body_end()) {
3419 auto OED = dyn_cast<OMPExecutableDirective>(*I);
3420 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
3421 OMPTeamsFound = false;
3422 break;
3423 }
3424 ++I;
3425 }
3426 assert(I != CS->body_end() && "Not found statement");
3427 S = *I;
3428 }
3429 if (!OMPTeamsFound) {
3430 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
3431 Diag(DSAStack->getInnerTeamsRegionLoc(),
3432 diag::note_omp_nested_teams_construct_here);
3433 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
3434 << isa<OMPExecutableDirective>(S);
3435 return StmtError();
3436 }
3437 }
3438
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003439 getCurFunction()->setHasBranchProtectedScope();
3440
3441 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3442}
3443
Alexey Bataev13314bf2014-10-09 04:18:56 +00003444StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
3445 Stmt *AStmt, SourceLocation StartLoc,
3446 SourceLocation EndLoc) {
3447 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3448 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3449 // 1.2.2 OpenMP Language Terminology
3450 // Structured block - An executable statement with a single entry at the
3451 // top and a single exit at the bottom.
3452 // The point of exit cannot be a branch out of the structured block.
3453 // longjmp() and throw() must not violate the entry/exit criteria.
3454 CS->getCapturedDecl()->setNothrow();
3455
3456 getCurFunction()->setHasBranchProtectedScope();
3457
3458 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3459}
3460
Alexey Bataeved09d242014-05-28 05:53:51 +00003461OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003462 SourceLocation StartLoc,
3463 SourceLocation LParenLoc,
3464 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003465 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003466 switch (Kind) {
3467 case OMPC_if:
3468 Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc);
3469 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00003470 case OMPC_final:
3471 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
3472 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00003473 case OMPC_num_threads:
3474 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
3475 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00003476 case OMPC_safelen:
3477 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
3478 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00003479 case OMPC_collapse:
3480 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
3481 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003482 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003483 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003484 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003485 case OMPC_private:
3486 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00003487 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003488 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00003489 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00003490 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00003491 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003492 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003493 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003494 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00003495 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003496 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003497 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003498 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00003499 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003500 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00003501 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00003502 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00003503 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003504 case OMPC_seq_cst:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003505 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003506 llvm_unreachable("Clause is not allowed.");
3507 }
3508 return Res;
3509}
3510
Alexey Bataeved09d242014-05-28 05:53:51 +00003511OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003512 SourceLocation LParenLoc,
3513 SourceLocation EndLoc) {
3514 Expr *ValExpr = Condition;
3515 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
3516 !Condition->isInstantiationDependent() &&
3517 !Condition->containsUnexpandedParameterPack()) {
3518 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00003519 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003520 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003521 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003522
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003523 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003524 }
3525
3526 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc);
3527}
3528
Alexey Bataev3778b602014-07-17 07:32:53 +00003529OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
3530 SourceLocation StartLoc,
3531 SourceLocation LParenLoc,
3532 SourceLocation EndLoc) {
3533 Expr *ValExpr = Condition;
3534 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
3535 !Condition->isInstantiationDependent() &&
3536 !Condition->containsUnexpandedParameterPack()) {
3537 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
3538 Condition->getExprLoc(), Condition);
3539 if (Val.isInvalid())
3540 return nullptr;
3541
3542 ValExpr = Val.get();
3543 }
3544
3545 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
3546}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003547ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
3548 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00003549 if (!Op)
3550 return ExprError();
3551
3552 class IntConvertDiagnoser : public ICEConvertDiagnoser {
3553 public:
3554 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00003555 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00003556 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
3557 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003558 return S.Diag(Loc, diag::err_omp_not_integral) << T;
3559 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003560 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
3561 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003562 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
3563 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003564 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
3565 QualType T,
3566 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003567 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
3568 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003569 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
3570 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003571 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00003572 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00003573 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003574 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
3575 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003576 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
3577 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003578 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
3579 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003580 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00003581 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00003582 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003583 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
3584 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003585 llvm_unreachable("conversion functions are permitted");
3586 }
3587 } ConvertDiagnoser;
3588 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
3589}
3590
3591OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
3592 SourceLocation StartLoc,
3593 SourceLocation LParenLoc,
3594 SourceLocation EndLoc) {
3595 Expr *ValExpr = NumThreads;
3596 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
Alexey Bataev568a8332014-03-06 06:15:19 +00003597 !NumThreads->containsUnexpandedParameterPack()) {
3598 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
3599 ExprResult Val =
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003600 PerformOpenMPImplicitIntegerConversion(NumThreadsLoc, NumThreads);
Alexey Bataev568a8332014-03-06 06:15:19 +00003601 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003602 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00003603
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003604 ValExpr = Val.get();
Alexey Bataev568a8332014-03-06 06:15:19 +00003605
3606 // OpenMP [2.5, Restrictions]
3607 // The num_threads expression must evaluate to a positive integer value.
3608 llvm::APSInt Result;
Alexey Bataeved09d242014-05-28 05:53:51 +00003609 if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() &&
3610 !Result.isStrictlyPositive()) {
Alexey Bataev568a8332014-03-06 06:15:19 +00003611 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
3612 << "num_threads" << NumThreads->getSourceRange();
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003613 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00003614 }
3615 }
3616
Alexey Bataeved09d242014-05-28 05:53:51 +00003617 return new (Context)
3618 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00003619}
3620
Alexey Bataev62c87d22014-03-21 04:51:18 +00003621ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
3622 OpenMPClauseKind CKind) {
3623 if (!E)
3624 return ExprError();
3625 if (E->isValueDependent() || E->isTypeDependent() ||
3626 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00003627 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00003628 llvm::APSInt Result;
3629 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
3630 if (ICE.isInvalid())
3631 return ExprError();
3632 if (!Result.isStrictlyPositive()) {
3633 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
3634 << getOpenMPClauseName(CKind) << E->getSourceRange();
3635 return ExprError();
3636 }
Alexander Musman09184fe2014-09-30 05:29:28 +00003637 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
3638 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
3639 << E->getSourceRange();
3640 return ExprError();
3641 }
Alexey Bataev62c87d22014-03-21 04:51:18 +00003642 return ICE;
3643}
3644
3645OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
3646 SourceLocation LParenLoc,
3647 SourceLocation EndLoc) {
3648 // OpenMP [2.8.1, simd construct, Description]
3649 // The parameter of the safelen clause must be a constant
3650 // positive integer expression.
3651 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
3652 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003653 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00003654 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003655 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00003656}
3657
Alexander Musman64d33f12014-06-04 07:53:32 +00003658OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
3659 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00003660 SourceLocation LParenLoc,
3661 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00003662 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00003663 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00003664 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00003665 // The parameter of the collapse clause must be a constant
3666 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00003667 ExprResult NumForLoopsResult =
3668 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
3669 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00003670 return nullptr;
3671 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00003672 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00003673}
3674
Alexey Bataeved09d242014-05-28 05:53:51 +00003675OMPClause *Sema::ActOnOpenMPSimpleClause(
3676 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
3677 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003678 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003679 switch (Kind) {
3680 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00003681 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00003682 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
3683 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003684 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003685 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00003686 Res = ActOnOpenMPProcBindClause(
3687 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
3688 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003689 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003690 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00003691 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00003692 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00003693 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00003694 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003695 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003696 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003697 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00003698 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00003699 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00003700 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00003701 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00003702 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003703 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003704 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003705 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00003706 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003707 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003708 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003709 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00003710 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003711 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00003712 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00003713 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00003714 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003715 case OMPC_seq_cst:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003716 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003717 llvm_unreachable("Clause is not allowed.");
3718 }
3719 return Res;
3720}
3721
3722OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
3723 SourceLocation KindKwLoc,
3724 SourceLocation StartLoc,
3725 SourceLocation LParenLoc,
3726 SourceLocation EndLoc) {
3727 if (Kind == OMPC_DEFAULT_unknown) {
3728 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00003729 static_assert(OMPC_DEFAULT_unknown > 0,
3730 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00003731 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00003732 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003733 Values += "'";
3734 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
3735 Values += "'";
3736 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00003737 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003738 Values += " or ";
3739 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00003740 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003741 break;
3742 default:
3743 Values += Sep;
3744 break;
3745 }
3746 }
3747 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00003748 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003749 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003750 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00003751 switch (Kind) {
3752 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003753 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003754 break;
3755 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003756 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003757 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003758 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003759 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00003760 break;
3761 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003762 return new (Context)
3763 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003764}
3765
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003766OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
3767 SourceLocation KindKwLoc,
3768 SourceLocation StartLoc,
3769 SourceLocation LParenLoc,
3770 SourceLocation EndLoc) {
3771 if (Kind == OMPC_PROC_BIND_unknown) {
3772 std::string Values;
3773 std::string Sep(", ");
3774 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
3775 Values += "'";
3776 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
3777 Values += "'";
3778 switch (i) {
3779 case OMPC_PROC_BIND_unknown - 2:
3780 Values += " or ";
3781 break;
3782 case OMPC_PROC_BIND_unknown - 1:
3783 break;
3784 default:
3785 Values += Sep;
3786 break;
3787 }
3788 }
3789 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00003790 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003791 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003792 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003793 return new (Context)
3794 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003795}
3796
Alexey Bataev56dafe82014-06-20 07:16:17 +00003797OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
3798 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
3799 SourceLocation StartLoc, SourceLocation LParenLoc,
3800 SourceLocation ArgumentLoc, SourceLocation CommaLoc,
3801 SourceLocation EndLoc) {
3802 OMPClause *Res = nullptr;
3803 switch (Kind) {
3804 case OMPC_schedule:
3805 Res = ActOnOpenMPScheduleClause(
3806 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
3807 LParenLoc, ArgumentLoc, CommaLoc, EndLoc);
3808 break;
3809 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00003810 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003811 case OMPC_num_threads:
3812 case OMPC_safelen:
3813 case OMPC_collapse:
3814 case OMPC_default:
3815 case OMPC_proc_bind:
3816 case OMPC_private:
3817 case OMPC_firstprivate:
3818 case OMPC_lastprivate:
3819 case OMPC_shared:
3820 case OMPC_reduction:
3821 case OMPC_linear:
3822 case OMPC_aligned:
3823 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003824 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003825 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00003826 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003827 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003828 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003829 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00003830 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003831 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00003832 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00003833 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00003834 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003835 case OMPC_seq_cst:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003836 case OMPC_unknown:
3837 llvm_unreachable("Clause is not allowed.");
3838 }
3839 return Res;
3840}
3841
3842OMPClause *Sema::ActOnOpenMPScheduleClause(
3843 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
3844 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
3845 SourceLocation EndLoc) {
3846 if (Kind == OMPC_SCHEDULE_unknown) {
3847 std::string Values;
3848 std::string Sep(", ");
3849 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) {
3850 Values += "'";
3851 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i);
3852 Values += "'";
3853 switch (i) {
3854 case OMPC_SCHEDULE_unknown - 2:
3855 Values += " or ";
3856 break;
3857 case OMPC_SCHEDULE_unknown - 1:
3858 break;
3859 default:
3860 Values += Sep;
3861 break;
3862 }
3863 }
3864 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
3865 << Values << getOpenMPClauseName(OMPC_schedule);
3866 return nullptr;
3867 }
3868 Expr *ValExpr = ChunkSize;
3869 if (ChunkSize) {
3870 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
3871 !ChunkSize->isInstantiationDependent() &&
3872 !ChunkSize->containsUnexpandedParameterPack()) {
3873 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
3874 ExprResult Val =
3875 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
3876 if (Val.isInvalid())
3877 return nullptr;
3878
3879 ValExpr = Val.get();
3880
3881 // OpenMP [2.7.1, Restrictions]
3882 // chunk_size must be a loop invariant integer expression with a positive
3883 // value.
3884 llvm::APSInt Result;
3885 if (ValExpr->isIntegerConstantExpr(Result, Context) &&
3886 Result.isSigned() && !Result.isStrictlyPositive()) {
3887 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
3888 << "schedule" << ChunkSize->getSourceRange();
3889 return nullptr;
3890 }
3891 }
3892 }
3893
3894 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc,
3895 EndLoc, Kind, ValExpr);
3896}
3897
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003898OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
3899 SourceLocation StartLoc,
3900 SourceLocation EndLoc) {
3901 OMPClause *Res = nullptr;
3902 switch (Kind) {
3903 case OMPC_ordered:
3904 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
3905 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00003906 case OMPC_nowait:
3907 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
3908 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003909 case OMPC_untied:
3910 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
3911 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003912 case OMPC_mergeable:
3913 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
3914 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003915 case OMPC_read:
3916 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
3917 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00003918 case OMPC_write:
3919 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
3920 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00003921 case OMPC_update:
3922 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
3923 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00003924 case OMPC_capture:
3925 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
3926 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003927 case OMPC_seq_cst:
3928 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
3929 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003930 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00003931 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003932 case OMPC_num_threads:
3933 case OMPC_safelen:
3934 case OMPC_collapse:
3935 case OMPC_schedule:
3936 case OMPC_private:
3937 case OMPC_firstprivate:
3938 case OMPC_lastprivate:
3939 case OMPC_shared:
3940 case OMPC_reduction:
3941 case OMPC_linear:
3942 case OMPC_aligned:
3943 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003944 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003945 case OMPC_default:
3946 case OMPC_proc_bind:
3947 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00003948 case OMPC_flush:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003949 case OMPC_unknown:
3950 llvm_unreachable("Clause is not allowed.");
3951 }
3952 return Res;
3953}
3954
3955OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
3956 SourceLocation EndLoc) {
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003957 DSAStack->setOrderedRegion();
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003958 return new (Context) OMPOrderedClause(StartLoc, EndLoc);
3959}
3960
Alexey Bataev236070f2014-06-20 11:19:47 +00003961OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
3962 SourceLocation EndLoc) {
3963 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
3964}
3965
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003966OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
3967 SourceLocation EndLoc) {
3968 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
3969}
3970
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003971OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
3972 SourceLocation EndLoc) {
3973 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
3974}
3975
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003976OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
3977 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003978 return new (Context) OMPReadClause(StartLoc, EndLoc);
3979}
3980
Alexey Bataevdea47612014-07-23 07:46:59 +00003981OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
3982 SourceLocation EndLoc) {
3983 return new (Context) OMPWriteClause(StartLoc, EndLoc);
3984}
3985
Alexey Bataev67a4f222014-07-23 10:25:33 +00003986OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
3987 SourceLocation EndLoc) {
3988 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
3989}
3990
Alexey Bataev459dec02014-07-24 06:46:57 +00003991OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
3992 SourceLocation EndLoc) {
3993 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
3994}
3995
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003996OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
3997 SourceLocation EndLoc) {
3998 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
3999}
4000
Alexey Bataevc5e02582014-06-16 07:08:35 +00004001OMPClause *Sema::ActOnOpenMPVarListClause(
4002 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
4003 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
4004 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
4005 const DeclarationNameInfo &ReductionId) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004006 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004007 switch (Kind) {
4008 case OMPC_private:
4009 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
4010 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004011 case OMPC_firstprivate:
4012 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
4013 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00004014 case OMPC_lastprivate:
4015 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
4016 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00004017 case OMPC_shared:
4018 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
4019 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00004020 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00004021 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
4022 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00004023 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00004024 case OMPC_linear:
4025 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
4026 ColonLoc, EndLoc);
4027 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00004028 case OMPC_aligned:
4029 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
4030 ColonLoc, EndLoc);
4031 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004032 case OMPC_copyin:
4033 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
4034 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00004035 case OMPC_copyprivate:
4036 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
4037 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00004038 case OMPC_flush:
4039 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
4040 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004041 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00004042 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00004043 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00004044 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00004045 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004046 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004047 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00004048 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004049 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00004050 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00004051 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00004052 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004053 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004054 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00004055 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00004056 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00004057 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00004058 case OMPC_seq_cst:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004059 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004060 llvm_unreachable("Clause is not allowed.");
4061 }
4062 return Res;
4063}
4064
4065OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
4066 SourceLocation StartLoc,
4067 SourceLocation LParenLoc,
4068 SourceLocation EndLoc) {
4069 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00004070 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00004071 for (auto &RefExpr : VarList) {
4072 assert(RefExpr && "NULL expr in OpenMP private clause.");
4073 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004074 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00004075 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00004076 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004077 continue;
4078 }
4079
Alexey Bataeved09d242014-05-28 05:53:51 +00004080 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004081 // OpenMP [2.1, C/C++]
4082 // A list item is a variable name.
4083 // OpenMP [2.9.3.3, Restrictions, p.1]
4084 // A variable that is part of another variable (as an array or
4085 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00004086 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004087 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00004088 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004089 continue;
4090 }
4091 Decl *D = DE->getDecl();
4092 VarDecl *VD = cast<VarDecl>(D);
4093
4094 QualType Type = VD->getType();
4095 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
4096 // It will be analyzed later.
4097 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00004098 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +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_private_incomplete_type)) {
4107 continue;
4108 }
4109 if (Type->isReferenceType()) {
4110 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00004111 << getOpenMPClauseName(OMPC_private) << Type;
4112 bool IsDecl =
4113 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4114 Diag(VD->getLocation(),
4115 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4116 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004117 continue;
4118 }
4119
4120 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
4121 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00004122 // clause requires an accessible, unambiguous default constructor for the
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004123 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00004124 while (Type->isArrayType()) {
4125 Type = cast<ArrayType>(Type.getTypePtr())->getElementType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004126 }
4127
Alexey Bataev758e55e2013-09-06 18:03:48 +00004128 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
4129 // in a Construct]
4130 // Variables with the predetermined data-sharing attributes may not be
4131 // listed in data-sharing attributes clauses, except for the cases
4132 // listed below. For these exceptions only, listing a predetermined
4133 // variable in a data-sharing attribute clause is allowed and overrides
4134 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004135 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004136 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00004137 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
4138 << getOpenMPClauseName(OMPC_private);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004139 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004140 continue;
4141 }
4142
Alexey Bataev03b340a2014-10-21 03:16:40 +00004143 // Generate helper private variable and initialize it with the default
4144 // value. The address of the original variable is replaced by the address of
4145 // the new private variable in CodeGen. This new variable is not added to
4146 // IdResolver, so the code in the OpenMP region uses original variable for
4147 // proper diagnostics.
4148 auto VDPrivate =
4149 VarDecl::Create(Context, CurContext, DE->getLocStart(),
4150 DE->getExprLoc(), VD->getIdentifier(), VD->getType(),
4151 VD->getTypeSourceInfo(), /*S*/ SC_Auto);
4152 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto*/ false);
4153 if (VDPrivate->isInvalidDecl())
4154 continue;
4155 CurContext->addDecl(VDPrivate);
4156 auto VDPrivateRefExpr = DeclRefExpr::Create(
4157 Context, /*QualifierLoc*/ NestedNameSpecifierLoc(),
4158 /*TemplateKWLoc*/ SourceLocation(), VDPrivate,
4159 /*isEnclosingLocal*/ false, /*NameLoc*/ SourceLocation(), DE->getType(),
4160 /*VK*/ VK_LValue);
4161
Alexey Bataev758e55e2013-09-06 18:03:48 +00004162 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004163 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00004164 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004165 }
4166
Alexey Bataeved09d242014-05-28 05:53:51 +00004167 if (Vars.empty())
4168 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004169
Alexey Bataev03b340a2014-10-21 03:16:40 +00004170 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
4171 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004172}
4173
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004174namespace {
4175class DiagsUninitializedSeveretyRAII {
4176private:
4177 DiagnosticsEngine &Diags;
4178 SourceLocation SavedLoc;
4179 bool IsIgnored;
4180
4181public:
4182 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
4183 bool IsIgnored)
4184 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
4185 if (!IsIgnored) {
4186 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
4187 /*Map*/ diag::Severity::Ignored, Loc);
4188 }
4189 }
4190 ~DiagsUninitializedSeveretyRAII() {
4191 if (!IsIgnored)
4192 Diags.popMappings(SavedLoc);
4193 }
4194};
4195}
4196
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004197OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
4198 SourceLocation StartLoc,
4199 SourceLocation LParenLoc,
4200 SourceLocation EndLoc) {
4201 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004202 SmallVector<Expr *, 8> PrivateCopies;
4203 SmallVector<Expr *, 8> Inits;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004204 bool IsImplicitClause =
4205 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
4206 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
4207
Alexey Bataeved09d242014-05-28 05:53:51 +00004208 for (auto &RefExpr : VarList) {
4209 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
4210 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004211 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00004212 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004213 PrivateCopies.push_back(nullptr);
4214 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004215 continue;
4216 }
4217
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004218 SourceLocation ELoc =
4219 IsImplicitClause ? ImplicitClauseLoc : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004220 // OpenMP [2.1, C/C++]
4221 // A list item is a variable name.
4222 // OpenMP [2.9.3.3, Restrictions, p.1]
4223 // A variable that is part of another variable (as an array or
4224 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00004225 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004226 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00004227 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004228 continue;
4229 }
4230 Decl *D = DE->getDecl();
4231 VarDecl *VD = cast<VarDecl>(D);
4232
4233 QualType Type = VD->getType();
4234 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
4235 // It will be analyzed later.
4236 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004237 PrivateCopies.push_back(nullptr);
4238 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004239 continue;
4240 }
4241
4242 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
4243 // A variable that appears in a private clause must not have an incomplete
4244 // type or a reference type.
4245 if (RequireCompleteType(ELoc, Type,
4246 diag::err_omp_firstprivate_incomplete_type)) {
4247 continue;
4248 }
4249 if (Type->isReferenceType()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004250 if (IsImplicitClause) {
4251 Diag(ImplicitClauseLoc,
4252 diag::err_omp_task_predetermined_firstprivate_ref_type_arg)
4253 << Type;
4254 Diag(RefExpr->getExprLoc(), diag::note_used_here);
4255 } else {
4256 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
4257 << getOpenMPClauseName(OMPC_firstprivate) << Type;
4258 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004259 bool IsDecl =
4260 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4261 Diag(VD->getLocation(),
4262 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4263 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004264 continue;
4265 }
4266
4267 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
4268 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00004269 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004270 // class type.
4271 Type = Context.getBaseElementType(Type);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004272
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004273 // If an implicit firstprivate variable found it was checked already.
4274 if (!IsImplicitClause) {
4275 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004276 Type = Type.getNonReferenceType().getCanonicalType();
4277 bool IsConstant = Type.isConstant(Context);
4278 Type = Context.getBaseElementType(Type);
4279 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
4280 // A list item that specifies a given variable may not appear in more
4281 // than one clause on the same directive, except that a variable may be
4282 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004283 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00004284 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004285 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00004286 << getOpenMPClauseName(DVar.CKind)
4287 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004288 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004289 continue;
4290 }
4291
4292 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
4293 // in a Construct]
4294 // Variables with the predetermined data-sharing attributes may not be
4295 // listed in data-sharing attributes clauses, except for the cases
4296 // listed below. For these exceptions only, listing a predetermined
4297 // variable in a data-sharing attribute clause is allowed and overrides
4298 // the variable's predetermined data-sharing attributes.
4299 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
4300 // in a Construct, C/C++, p.2]
4301 // Variables with const-qualified type having no mutable member may be
4302 // listed in a firstprivate clause, even if they are static data members.
4303 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
4304 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
4305 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00004306 << getOpenMPClauseName(DVar.CKind)
4307 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004308 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004309 continue;
4310 }
4311
Alexey Bataevf29276e2014-06-18 04:14:57 +00004312 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004313 // OpenMP [2.9.3.4, Restrictions, p.2]
4314 // A list item that is private within a parallel region must not appear
4315 // in a firstprivate clause on a worksharing construct if any of the
4316 // worksharing regions arising from the worksharing construct ever bind
4317 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00004318 if (isOpenMPWorksharingDirective(CurrDir) &&
4319 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004320 DVar = DSAStack->getImplicitDSA(VD, true);
4321 if (DVar.CKind != OMPC_shared &&
4322 (isOpenMPParallelDirective(DVar.DKind) ||
4323 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00004324 Diag(ELoc, diag::err_omp_required_access)
4325 << getOpenMPClauseName(OMPC_firstprivate)
4326 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004327 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004328 continue;
4329 }
4330 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004331 // OpenMP [2.9.3.4, Restrictions, p.3]
4332 // A list item that appears in a reduction clause of a parallel construct
4333 // must not appear in a firstprivate clause on a worksharing or task
4334 // construct if any of the worksharing or task regions arising from the
4335 // worksharing or task construct ever bind to any of the parallel regions
4336 // arising from the parallel construct.
4337 // OpenMP [2.9.3.4, Restrictions, p.4]
4338 // A list item that appears in a reduction clause in worksharing
4339 // construct must not appear in a firstprivate clause in a task construct
4340 // encountered during execution of any of the worksharing regions arising
4341 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004342 if (CurrDir == OMPD_task) {
4343 DVar =
4344 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
4345 [](OpenMPDirectiveKind K) -> bool {
4346 return isOpenMPParallelDirective(K) ||
4347 isOpenMPWorksharingDirective(K);
4348 },
4349 false);
4350 if (DVar.CKind == OMPC_reduction &&
4351 (isOpenMPParallelDirective(DVar.DKind) ||
4352 isOpenMPWorksharingDirective(DVar.DKind))) {
4353 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
4354 << getOpenMPDirectiveName(DVar.DKind);
4355 ReportOriginalDSA(*this, DSAStack, VD, DVar);
4356 continue;
4357 }
4358 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004359 }
4360
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004361 Type = Type.getUnqualifiedType();
4362 auto VDPrivate = VarDecl::Create(Context, CurContext, DE->getLocStart(),
4363 ELoc, VD->getIdentifier(), VD->getType(),
4364 VD->getTypeSourceInfo(), /*S*/ SC_Auto);
4365 // Generate helper private variable and initialize it with the value of the
4366 // original variable. The address of the original variable is replaced by
4367 // the address of the new private variable in the CodeGen. This new variable
4368 // is not added to IdResolver, so the code in the OpenMP region uses
4369 // original variable for proper diagnostics and variable capturing.
4370 Expr *VDInitRefExpr = nullptr;
4371 // For arrays generate initializer for single element and replace it by the
4372 // original array element in CodeGen.
4373 if (DE->getType()->isArrayType()) {
4374 auto VDInit = VarDecl::Create(Context, CurContext, DE->getLocStart(),
4375 ELoc, VD->getIdentifier(), Type,
4376 VD->getTypeSourceInfo(), /*S*/ SC_Auto);
4377 CurContext->addHiddenDecl(VDInit);
4378 VDInitRefExpr = DeclRefExpr::Create(
4379 Context, /*QualifierLoc*/ NestedNameSpecifierLoc(),
4380 /*TemplateKWLoc*/ SourceLocation(), VDInit,
4381 /*isEnclosingLocal*/ false, ELoc, Type,
4382 /*VK*/ VK_LValue);
4383 VDInit->setIsUsed();
4384 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
4385 InitializedEntity Entity = InitializedEntity::InitializeVariable(VDInit);
4386 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
4387
4388 InitializationSequence InitSeq(*this, Entity, Kind, Init);
4389 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
4390 if (Result.isInvalid())
4391 VDPrivate->setInvalidDecl();
4392 else
4393 VDPrivate->setInit(Result.getAs<Expr>());
4394 } else {
4395 AddInitializerToDecl(VDPrivate, DefaultLvalueConversion(DE).get(),
4396 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4397 }
4398 if (VDPrivate->isInvalidDecl()) {
4399 if (IsImplicitClause) {
4400 Diag(DE->getExprLoc(),
4401 diag::note_omp_task_predetermined_firstprivate_here);
4402 }
4403 continue;
4404 }
4405 CurContext->addDecl(VDPrivate);
4406 auto VDPrivateRefExpr = DeclRefExpr::Create(
4407 Context, /*QualifierLoc*/ NestedNameSpecifierLoc(),
4408 /*TemplateKWLoc*/ SourceLocation(), VDPrivate,
4409 /*isEnclosingLocal*/ false, DE->getLocStart(), DE->getType(),
4410 /*VK*/ VK_LValue);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004411 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
4412 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004413 PrivateCopies.push_back(VDPrivateRefExpr);
4414 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004415 }
4416
Alexey Bataeved09d242014-05-28 05:53:51 +00004417 if (Vars.empty())
4418 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004419
4420 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004421 Vars, PrivateCopies, Inits);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004422}
4423
Alexander Musman1bb328c2014-06-04 13:06:39 +00004424OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
4425 SourceLocation StartLoc,
4426 SourceLocation LParenLoc,
4427 SourceLocation EndLoc) {
4428 SmallVector<Expr *, 8> Vars;
4429 for (auto &RefExpr : VarList) {
4430 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
4431 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
4432 // It will be analyzed later.
4433 Vars.push_back(RefExpr);
4434 continue;
4435 }
4436
4437 SourceLocation ELoc = RefExpr->getExprLoc();
4438 // OpenMP [2.1, C/C++]
4439 // A list item is a variable name.
4440 // OpenMP [2.14.3.5, Restrictions, p.1]
4441 // A variable that is part of another variable (as an array or structure
4442 // element) cannot appear in a lastprivate clause.
4443 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
4444 if (!DE || !isa<VarDecl>(DE->getDecl())) {
4445 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
4446 continue;
4447 }
4448 Decl *D = DE->getDecl();
4449 VarDecl *VD = cast<VarDecl>(D);
4450
4451 QualType Type = VD->getType();
4452 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
4453 // It will be analyzed later.
4454 Vars.push_back(DE);
4455 continue;
4456 }
4457
4458 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
4459 // A variable that appears in a lastprivate clause must not have an
4460 // incomplete type or a reference type.
4461 if (RequireCompleteType(ELoc, Type,
4462 diag::err_omp_lastprivate_incomplete_type)) {
4463 continue;
4464 }
4465 if (Type->isReferenceType()) {
4466 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
4467 << getOpenMPClauseName(OMPC_lastprivate) << Type;
4468 bool IsDecl =
4469 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4470 Diag(VD->getLocation(),
4471 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4472 << VD;
4473 continue;
4474 }
4475
4476 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4477 // in a Construct]
4478 // Variables with the predetermined data-sharing attributes may not be
4479 // listed in data-sharing attributes clauses, except for the cases
4480 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004481 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00004482 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
4483 DVar.CKind != OMPC_firstprivate &&
4484 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
4485 Diag(ELoc, diag::err_omp_wrong_dsa)
4486 << getOpenMPClauseName(DVar.CKind)
4487 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004488 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00004489 continue;
4490 }
4491
Alexey Bataevf29276e2014-06-18 04:14:57 +00004492 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
4493 // OpenMP [2.14.3.5, Restrictions, p.2]
4494 // A list item that is private within a parallel region, or that appears in
4495 // the reduction clause of a parallel construct, must not appear in a
4496 // lastprivate clause on a worksharing construct if any of the corresponding
4497 // worksharing regions ever binds to any of the corresponding parallel
4498 // regions.
Alexey Bataev549210e2014-06-24 04:39:47 +00004499 if (isOpenMPWorksharingDirective(CurrDir) &&
4500 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004501 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004502 if (DVar.CKind != OMPC_shared) {
4503 Diag(ELoc, diag::err_omp_required_access)
4504 << getOpenMPClauseName(OMPC_lastprivate)
4505 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004506 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004507 continue;
4508 }
4509 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00004510 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00004511 // A variable of class type (or array thereof) that appears in a
4512 // lastprivate clause requires an accessible, unambiguous default
4513 // constructor for the class type, unless the list item is also specified
4514 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00004515 // A variable of class type (or array thereof) that appears in a
4516 // lastprivate clause requires an accessible, unambiguous copy assignment
4517 // operator for the class type.
4518 while (Type.getNonReferenceType()->isArrayType())
4519 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
4520 ->getElementType();
4521 CXXRecordDecl *RD = getLangOpts().CPlusPlus
4522 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
4523 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00004524 // FIXME This code must be replaced by actual copying and destructing of the
4525 // lastprivate variable.
Alexander Musman1bb328c2014-06-04 13:06:39 +00004526 if (RD) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00004527 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
4528 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00004529 if (MD) {
4530 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
4531 MD->isDeleted()) {
4532 Diag(ELoc, diag::err_omp_required_method)
4533 << getOpenMPClauseName(OMPC_lastprivate) << 2;
4534 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4535 VarDecl::DeclarationOnly;
4536 Diag(VD->getLocation(),
4537 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4538 << VD;
4539 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4540 continue;
4541 }
4542 MarkFunctionReferenced(ELoc, MD);
4543 DiagnoseUseOfDecl(MD, ELoc);
Alexander Musman1bb328c2014-06-04 13:06:39 +00004544 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00004545
4546 CXXDestructorDecl *DD = RD->getDestructor();
4547 if (DD) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00004548 PartialDiagnostic PD =
4549 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexander Musman1bb328c2014-06-04 13:06:39 +00004550 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
4551 DD->isDeleted()) {
4552 Diag(ELoc, diag::err_omp_required_method)
4553 << getOpenMPClauseName(OMPC_lastprivate) << 4;
4554 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4555 VarDecl::DeclarationOnly;
4556 Diag(VD->getLocation(),
4557 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4558 << VD;
4559 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4560 continue;
4561 }
4562 MarkFunctionReferenced(ELoc, DD);
4563 DiagnoseUseOfDecl(DD, ELoc);
4564 }
4565 }
4566
Alexey Bataevf29276e2014-06-18 04:14:57 +00004567 if (DVar.CKind != OMPC_firstprivate)
4568 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00004569 Vars.push_back(DE);
4570 }
4571
4572 if (Vars.empty())
4573 return nullptr;
4574
4575 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
4576 Vars);
4577}
4578
Alexey Bataev758e55e2013-09-06 18:03:48 +00004579OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
4580 SourceLocation StartLoc,
4581 SourceLocation LParenLoc,
4582 SourceLocation EndLoc) {
4583 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00004584 for (auto &RefExpr : VarList) {
4585 assert(RefExpr && "NULL expr in OpenMP shared clause.");
4586 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00004587 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00004588 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004589 continue;
4590 }
4591
Alexey Bataeved09d242014-05-28 05:53:51 +00004592 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00004593 // OpenMP [2.1, C/C++]
4594 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00004595 // OpenMP [2.14.3.2, Restrictions, p.1]
4596 // A variable that is part of another variable (as an array or structure
4597 // element) cannot appear in a shared unless it is a static data member
4598 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00004599 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004600 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00004601 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00004602 continue;
4603 }
4604 Decl *D = DE->getDecl();
4605 VarDecl *VD = cast<VarDecl>(D);
4606
4607 QualType Type = VD->getType();
4608 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
4609 // It will be analyzed later.
4610 Vars.push_back(DE);
4611 continue;
4612 }
4613
4614 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
4615 // in a Construct]
4616 // Variables with the predetermined data-sharing attributes may not be
4617 // listed in data-sharing attributes clauses, except for the cases
4618 // listed below. For these exceptions only, listing a predetermined
4619 // variable in a data-sharing attribute clause is allowed and overrides
4620 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004621 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00004622 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
4623 DVar.RefExpr) {
4624 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
4625 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004626 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004627 continue;
4628 }
4629
4630 DSAStack->addDSA(VD, DE, OMPC_shared);
4631 Vars.push_back(DE);
4632 }
4633
Alexey Bataeved09d242014-05-28 05:53:51 +00004634 if (Vars.empty())
4635 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00004636
4637 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
4638}
4639
Alexey Bataevc5e02582014-06-16 07:08:35 +00004640namespace {
4641class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
4642 DSAStackTy *Stack;
4643
4644public:
4645 bool VisitDeclRefExpr(DeclRefExpr *E) {
4646 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004647 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00004648 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
4649 return false;
4650 if (DVar.CKind != OMPC_unknown)
4651 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00004652 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004653 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004654 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00004655 return true;
4656 return false;
4657 }
4658 return false;
4659 }
4660 bool VisitStmt(Stmt *S) {
4661 for (auto Child : S->children()) {
4662 if (Child && Visit(Child))
4663 return true;
4664 }
4665 return false;
4666 }
Alexey Bataev23b69422014-06-18 07:08:49 +00004667 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00004668};
Alexey Bataev23b69422014-06-18 07:08:49 +00004669} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00004670
4671OMPClause *Sema::ActOnOpenMPReductionClause(
4672 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
4673 SourceLocation ColonLoc, SourceLocation EndLoc,
4674 CXXScopeSpec &ReductionIdScopeSpec,
4675 const DeclarationNameInfo &ReductionId) {
4676 // TODO: Allow scope specification search when 'declare reduction' is
4677 // supported.
4678 assert(ReductionIdScopeSpec.isEmpty() &&
4679 "No support for scoped reduction identifiers yet.");
4680
4681 auto DN = ReductionId.getName();
4682 auto OOK = DN.getCXXOverloadedOperator();
4683 BinaryOperatorKind BOK = BO_Comma;
4684
4685 // OpenMP [2.14.3.6, reduction clause]
4686 // C
4687 // reduction-identifier is either an identifier or one of the following
4688 // operators: +, -, *, &, |, ^, && and ||
4689 // C++
4690 // reduction-identifier is either an id-expression or one of the following
4691 // operators: +, -, *, &, |, ^, && and ||
4692 // FIXME: Only 'min' and 'max' identifiers are supported for now.
4693 switch (OOK) {
4694 case OO_Plus:
4695 case OO_Minus:
4696 BOK = BO_AddAssign;
4697 break;
4698 case OO_Star:
4699 BOK = BO_MulAssign;
4700 break;
4701 case OO_Amp:
4702 BOK = BO_AndAssign;
4703 break;
4704 case OO_Pipe:
4705 BOK = BO_OrAssign;
4706 break;
4707 case OO_Caret:
4708 BOK = BO_XorAssign;
4709 break;
4710 case OO_AmpAmp:
4711 BOK = BO_LAnd;
4712 break;
4713 case OO_PipePipe:
4714 BOK = BO_LOr;
4715 break;
4716 default:
4717 if (auto II = DN.getAsIdentifierInfo()) {
4718 if (II->isStr("max"))
4719 BOK = BO_GT;
4720 else if (II->isStr("min"))
4721 BOK = BO_LT;
4722 }
4723 break;
4724 }
4725 SourceRange ReductionIdRange;
4726 if (ReductionIdScopeSpec.isValid()) {
4727 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
4728 }
4729 ReductionIdRange.setEnd(ReductionId.getEndLoc());
4730 if (BOK == BO_Comma) {
4731 // Not allowed reduction identifier is found.
4732 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
4733 << ReductionIdRange;
4734 return nullptr;
4735 }
4736
4737 SmallVector<Expr *, 8> Vars;
4738 for (auto RefExpr : VarList) {
4739 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
4740 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
4741 // It will be analyzed later.
4742 Vars.push_back(RefExpr);
4743 continue;
4744 }
4745
4746 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
4747 RefExpr->isInstantiationDependent() ||
4748 RefExpr->containsUnexpandedParameterPack()) {
4749 // It will be analyzed later.
4750 Vars.push_back(RefExpr);
4751 continue;
4752 }
4753
4754 auto ELoc = RefExpr->getExprLoc();
4755 auto ERange = RefExpr->getSourceRange();
4756 // OpenMP [2.1, C/C++]
4757 // A list item is a variable or array section, subject to the restrictions
4758 // specified in Section 2.4 on page 42 and in each of the sections
4759 // describing clauses and directives for which a list appears.
4760 // OpenMP [2.14.3.3, Restrictions, p.1]
4761 // A variable that is part of another variable (as an array or
4762 // structure element) cannot appear in a private clause.
4763 auto DE = dyn_cast<DeclRefExpr>(RefExpr);
4764 if (!DE || !isa<VarDecl>(DE->getDecl())) {
4765 Diag(ELoc, diag::err_omp_expected_var_name) << ERange;
4766 continue;
4767 }
4768 auto D = DE->getDecl();
4769 auto VD = cast<VarDecl>(D);
4770 auto Type = VD->getType();
4771 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
4772 // A variable that appears in a private clause must not have an incomplete
4773 // type or a reference type.
4774 if (RequireCompleteType(ELoc, Type,
4775 diag::err_omp_reduction_incomplete_type))
4776 continue;
4777 // OpenMP [2.14.3.6, reduction clause, Restrictions]
4778 // Arrays may not appear in a reduction clause.
4779 if (Type.getNonReferenceType()->isArrayType()) {
4780 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
4781 bool IsDecl =
4782 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4783 Diag(VD->getLocation(),
4784 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4785 << VD;
4786 continue;
4787 }
4788 // OpenMP [2.14.3.6, reduction clause, Restrictions]
4789 // A list item that appears in a reduction clause must not be
4790 // const-qualified.
4791 if (Type.getNonReferenceType().isConstant(Context)) {
4792 Diag(ELoc, diag::err_omp_const_variable)
4793 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
4794 bool IsDecl =
4795 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4796 Diag(VD->getLocation(),
4797 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4798 << VD;
4799 continue;
4800 }
4801 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
4802 // If a list-item is a reference type then it must bind to the same object
4803 // for all threads of the team.
4804 VarDecl *VDDef = VD->getDefinition();
4805 if (Type->isReferenceType() && VDDef) {
4806 DSARefChecker Check(DSAStack);
4807 if (Check.Visit(VDDef->getInit())) {
4808 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
4809 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
4810 continue;
4811 }
4812 }
4813 // OpenMP [2.14.3.6, reduction clause, Restrictions]
4814 // The type of a list item that appears in a reduction clause must be valid
4815 // for the reduction-identifier. For a max or min reduction in C, the type
4816 // of the list item must be an allowed arithmetic data type: char, int,
4817 // float, double, or _Bool, possibly modified with long, short, signed, or
4818 // unsigned. For a max or min reduction in C++, the type of the list item
4819 // must be an allowed arithmetic data type: char, wchar_t, int, float,
4820 // double, or bool, possibly modified with long, short, signed, or unsigned.
4821 if ((BOK == BO_GT || BOK == BO_LT) &&
4822 !(Type->isScalarType() ||
4823 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
4824 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
4825 << getLangOpts().CPlusPlus;
4826 bool IsDecl =
4827 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4828 Diag(VD->getLocation(),
4829 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4830 << VD;
4831 continue;
4832 }
4833 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
4834 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
4835 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
4836 bool IsDecl =
4837 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4838 Diag(VD->getLocation(),
4839 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4840 << VD;
4841 continue;
4842 }
4843 bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
4844 getDiagnostics().setSuppressAllDiagnostics(true);
4845 ExprResult ReductionOp =
4846 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
4847 RefExpr, RefExpr);
4848 getDiagnostics().setSuppressAllDiagnostics(Suppress);
4849 if (ReductionOp.isInvalid()) {
4850 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
Alexey Bataev23b69422014-06-18 07:08:49 +00004851 << ReductionIdRange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00004852 bool IsDecl =
4853 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4854 Diag(VD->getLocation(),
4855 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4856 << VD;
4857 continue;
4858 }
4859
4860 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4861 // in a Construct]
4862 // Variables with the predetermined data-sharing attributes may not be
4863 // listed in data-sharing attributes clauses, except for the cases
4864 // listed below. For these exceptions only, listing a predetermined
4865 // variable in a data-sharing attribute clause is allowed and overrides
4866 // the variable's predetermined data-sharing attributes.
4867 // OpenMP [2.14.3.6, Restrictions, p.3]
4868 // Any number of reduction clauses can be specified on the directive,
4869 // but a list item can appear only once in the reduction clauses for that
4870 // directive.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004871 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00004872 if (DVar.CKind == OMPC_reduction) {
4873 Diag(ELoc, diag::err_omp_once_referenced)
4874 << getOpenMPClauseName(OMPC_reduction);
4875 if (DVar.RefExpr) {
4876 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
4877 }
4878 } else if (DVar.CKind != OMPC_unknown) {
4879 Diag(ELoc, diag::err_omp_wrong_dsa)
4880 << getOpenMPClauseName(DVar.CKind)
4881 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004882 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00004883 continue;
4884 }
4885
4886 // OpenMP [2.14.3.6, Restrictions, p.1]
4887 // A list item that appears in a reduction clause of a worksharing
4888 // construct must be shared in the parallel regions to which any of the
4889 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf29276e2014-06-18 04:14:57 +00004890 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataev549210e2014-06-24 04:39:47 +00004891 if (isOpenMPWorksharingDirective(CurrDir) &&
4892 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004893 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004894 if (DVar.CKind != OMPC_shared) {
4895 Diag(ELoc, diag::err_omp_required_access)
4896 << getOpenMPClauseName(OMPC_reduction)
4897 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004898 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004899 continue;
4900 }
4901 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00004902
4903 CXXRecordDecl *RD = getLangOpts().CPlusPlus
4904 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
4905 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00004906 // FIXME This code must be replaced by actual constructing/destructing of
4907 // the reduction variable.
Alexey Bataevc5e02582014-06-16 07:08:35 +00004908 if (RD) {
4909 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
4910 PartialDiagnostic PD =
4911 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataev23b69422014-06-18 07:08:49 +00004912 if (!CD ||
4913 CheckConstructorAccess(ELoc, CD,
4914 InitializedEntity::InitializeTemporary(Type),
4915 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevc5e02582014-06-16 07:08:35 +00004916 CD->isDeleted()) {
4917 Diag(ELoc, diag::err_omp_required_method)
4918 << getOpenMPClauseName(OMPC_reduction) << 0;
4919 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4920 VarDecl::DeclarationOnly;
4921 Diag(VD->getLocation(),
4922 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4923 << VD;
4924 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4925 continue;
4926 }
4927 MarkFunctionReferenced(ELoc, CD);
4928 DiagnoseUseOfDecl(CD, ELoc);
4929
4930 CXXDestructorDecl *DD = RD->getDestructor();
4931 if (DD) {
4932 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
4933 DD->isDeleted()) {
4934 Diag(ELoc, diag::err_omp_required_method)
4935 << getOpenMPClauseName(OMPC_reduction) << 4;
4936 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4937 VarDecl::DeclarationOnly;
4938 Diag(VD->getLocation(),
4939 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4940 << VD;
4941 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4942 continue;
4943 }
4944 MarkFunctionReferenced(ELoc, DD);
4945 DiagnoseUseOfDecl(DD, ELoc);
4946 }
4947 }
4948
4949 DSAStack->addDSA(VD, DE, OMPC_reduction);
4950 Vars.push_back(DE);
4951 }
4952
4953 if (Vars.empty())
4954 return nullptr;
4955
4956 return OMPReductionClause::Create(
4957 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
4958 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId);
4959}
4960
Alexander Musman8dba6642014-04-22 13:09:42 +00004961OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step,
4962 SourceLocation StartLoc,
4963 SourceLocation LParenLoc,
4964 SourceLocation ColonLoc,
4965 SourceLocation EndLoc) {
4966 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00004967 for (auto &RefExpr : VarList) {
4968 assert(RefExpr && "NULL expr in OpenMP linear clause.");
4969 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00004970 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00004971 Vars.push_back(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00004972 continue;
4973 }
4974
4975 // OpenMP [2.14.3.7, linear clause]
4976 // A list item that appears in a linear clause is subject to the private
4977 // clause semantics described in Section 2.14.3.3 on page 159 except as
4978 // noted. In addition, the value of the new list item on each iteration
4979 // of the associated loop(s) corresponds to the value of the original
4980 // list item before entering the construct plus the logical number of
4981 // the iteration times linear-step.
4982
Alexey Bataeved09d242014-05-28 05:53:51 +00004983 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00004984 // OpenMP [2.1, C/C++]
4985 // A list item is a variable name.
4986 // OpenMP [2.14.3.3, Restrictions, p.1]
4987 // A variable that is part of another variable (as an array or
4988 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00004989 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00004990 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00004991 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00004992 continue;
4993 }
4994
4995 VarDecl *VD = cast<VarDecl>(DE->getDecl());
4996
4997 // OpenMP [2.14.3.7, linear clause]
4998 // A list-item cannot appear in more than one linear clause.
4999 // A list-item that appears in a linear clause cannot appear in any
5000 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005001 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00005002 if (DVar.RefExpr) {
5003 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
5004 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005005 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00005006 continue;
5007 }
5008
5009 QualType QType = VD->getType();
5010 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
5011 // It will be analyzed later.
5012 Vars.push_back(DE);
5013 continue;
5014 }
5015
5016 // A variable must not have an incomplete type or a reference type.
5017 if (RequireCompleteType(ELoc, QType,
5018 diag::err_omp_linear_incomplete_type)) {
5019 continue;
5020 }
5021 if (QType->isReferenceType()) {
5022 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
5023 << getOpenMPClauseName(OMPC_linear) << QType;
5024 bool IsDecl =
5025 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5026 Diag(VD->getLocation(),
5027 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5028 << VD;
5029 continue;
5030 }
5031
5032 // A list item must not be const-qualified.
5033 if (QType.isConstant(Context)) {
5034 Diag(ELoc, diag::err_omp_const_variable)
5035 << getOpenMPClauseName(OMPC_linear);
5036 bool IsDecl =
5037 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5038 Diag(VD->getLocation(),
5039 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5040 << VD;
5041 continue;
5042 }
5043
5044 // A list item must be of integral or pointer type.
5045 QType = QType.getUnqualifiedType().getCanonicalType();
5046 const Type *Ty = QType.getTypePtrOrNull();
5047 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
5048 !Ty->isPointerType())) {
5049 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
5050 bool IsDecl =
5051 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5052 Diag(VD->getLocation(),
5053 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5054 << VD;
5055 continue;
5056 }
5057
5058 DSAStack->addDSA(VD, DE, OMPC_linear);
5059 Vars.push_back(DE);
5060 }
5061
5062 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005063 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00005064
5065 Expr *StepExpr = Step;
5066 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
5067 !Step->isInstantiationDependent() &&
5068 !Step->containsUnexpandedParameterPack()) {
5069 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005070 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00005071 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005072 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00005073 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00005074
5075 // Warn about zero linear step (it would be probably better specified as
5076 // making corresponding variables 'const').
5077 llvm::APSInt Result;
5078 if (StepExpr->isIntegerConstantExpr(Result, Context) &&
5079 !Result.isNegative() && !Result.isStrictlyPositive())
5080 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
5081 << (Vars.size() > 1);
5082 }
5083
5084 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc,
5085 Vars, StepExpr);
5086}
5087
Alexander Musmanf0d76e72014-05-29 14:36:25 +00005088OMPClause *Sema::ActOnOpenMPAlignedClause(
5089 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
5090 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
5091
5092 SmallVector<Expr *, 8> Vars;
5093 for (auto &RefExpr : VarList) {
5094 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
5095 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
5096 // It will be analyzed later.
5097 Vars.push_back(RefExpr);
5098 continue;
5099 }
5100
5101 SourceLocation ELoc = RefExpr->getExprLoc();
5102 // OpenMP [2.1, C/C++]
5103 // A list item is a variable name.
5104 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
5105 if (!DE || !isa<VarDecl>(DE->getDecl())) {
5106 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
5107 continue;
5108 }
5109
5110 VarDecl *VD = cast<VarDecl>(DE->getDecl());
5111
5112 // OpenMP [2.8.1, simd construct, Restrictions]
5113 // The type of list items appearing in the aligned clause must be
5114 // array, pointer, reference to array, or reference to pointer.
5115 QualType QType = DE->getType()
5116 .getNonReferenceType()
5117 .getUnqualifiedType()
5118 .getCanonicalType();
5119 const Type *Ty = QType.getTypePtrOrNull();
5120 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
5121 !Ty->isPointerType())) {
5122 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
5123 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
5124 bool IsDecl =
5125 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5126 Diag(VD->getLocation(),
5127 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5128 << VD;
5129 continue;
5130 }
5131
5132 // OpenMP [2.8.1, simd construct, Restrictions]
5133 // A list-item cannot appear in more than one aligned clause.
5134 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
5135 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
5136 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
5137 << getOpenMPClauseName(OMPC_aligned);
5138 continue;
5139 }
5140
5141 Vars.push_back(DE);
5142 }
5143
5144 // OpenMP [2.8.1, simd construct, Description]
5145 // The parameter of the aligned clause, alignment, must be a constant
5146 // positive integer expression.
5147 // If no optional parameter is specified, implementation-defined default
5148 // alignments for SIMD instructions on the target platforms are assumed.
5149 if (Alignment != nullptr) {
5150 ExprResult AlignResult =
5151 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
5152 if (AlignResult.isInvalid())
5153 return nullptr;
5154 Alignment = AlignResult.get();
5155 }
5156 if (Vars.empty())
5157 return nullptr;
5158
5159 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
5160 EndLoc, Vars, Alignment);
5161}
5162
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005163OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
5164 SourceLocation StartLoc,
5165 SourceLocation LParenLoc,
5166 SourceLocation EndLoc) {
5167 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00005168 for (auto &RefExpr : VarList) {
5169 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
5170 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005171 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00005172 Vars.push_back(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005173 continue;
5174 }
5175
Alexey Bataeved09d242014-05-28 05:53:51 +00005176 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005177 // OpenMP [2.1, C/C++]
5178 // A list item is a variable name.
5179 // OpenMP [2.14.4.1, Restrictions, p.1]
5180 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00005181 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005182 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005183 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005184 continue;
5185 }
5186
5187 Decl *D = DE->getDecl();
5188 VarDecl *VD = cast<VarDecl>(D);
5189
5190 QualType Type = VD->getType();
5191 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5192 // It will be analyzed later.
5193 Vars.push_back(DE);
5194 continue;
5195 }
5196
5197 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
5198 // A list item that appears in a copyin clause must be threadprivate.
5199 if (!DSAStack->isThreadPrivate(VD)) {
5200 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00005201 << getOpenMPClauseName(OMPC_copyin)
5202 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005203 continue;
5204 }
5205
5206 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
5207 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00005208 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005209 // operator for the class type.
5210 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005211 CXXRecordDecl *RD =
5212 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00005213 // FIXME This code must be replaced by actual assignment of the
5214 // threadprivate variable.
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005215 if (RD) {
5216 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
5217 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00005218 if (MD) {
5219 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
5220 MD->isDeleted()) {
5221 Diag(ELoc, diag::err_omp_required_method)
5222 << getOpenMPClauseName(OMPC_copyin) << 2;
5223 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
5224 VarDecl::DeclarationOnly;
5225 Diag(VD->getLocation(),
5226 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5227 << VD;
5228 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
5229 continue;
5230 }
5231 MarkFunctionReferenced(ELoc, MD);
5232 DiagnoseUseOfDecl(MD, ELoc);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005233 }
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005234 }
5235
5236 DSAStack->addDSA(VD, DE, OMPC_copyin);
5237 Vars.push_back(DE);
5238 }
5239
Alexey Bataeved09d242014-05-28 05:53:51 +00005240 if (Vars.empty())
5241 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005242
5243 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
5244}
5245
Alexey Bataevbae9a792014-06-27 10:37:06 +00005246OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
5247 SourceLocation StartLoc,
5248 SourceLocation LParenLoc,
5249 SourceLocation EndLoc) {
5250 SmallVector<Expr *, 8> Vars;
5251 for (auto &RefExpr : VarList) {
5252 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
5253 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
5254 // It will be analyzed later.
5255 Vars.push_back(RefExpr);
5256 continue;
5257 }
5258
5259 SourceLocation ELoc = RefExpr->getExprLoc();
5260 // OpenMP [2.1, C/C++]
5261 // A list item is a variable name.
5262 // OpenMP [2.14.4.1, Restrictions, p.1]
5263 // A list item that appears in a copyin clause must be threadprivate.
5264 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
5265 if (!DE || !isa<VarDecl>(DE->getDecl())) {
5266 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
5267 continue;
5268 }
5269
5270 Decl *D = DE->getDecl();
5271 VarDecl *VD = cast<VarDecl>(D);
5272
5273 QualType Type = VD->getType();
5274 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5275 // It will be analyzed later.
5276 Vars.push_back(DE);
5277 continue;
5278 }
5279
5280 // OpenMP [2.14.4.2, Restrictions, p.2]
5281 // A list item that appears in a copyprivate clause may not appear in a
5282 // private or firstprivate clause on the single construct.
5283 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005284 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00005285 if (DVar.CKind != OMPC_copyprivate && DVar.CKind != OMPC_unknown &&
5286 !(DVar.CKind == OMPC_private && !DVar.RefExpr)) {
5287 Diag(ELoc, diag::err_omp_wrong_dsa)
5288 << getOpenMPClauseName(DVar.CKind)
5289 << getOpenMPClauseName(OMPC_copyprivate);
5290 ReportOriginalDSA(*this, DSAStack, VD, DVar);
5291 continue;
5292 }
5293
5294 // OpenMP [2.11.4.2, Restrictions, p.1]
5295 // All list items that appear in a copyprivate clause must be either
5296 // threadprivate or private in the enclosing context.
5297 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005298 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00005299 if (DVar.CKind == OMPC_shared) {
5300 Diag(ELoc, diag::err_omp_required_access)
5301 << getOpenMPClauseName(OMPC_copyprivate)
5302 << "threadprivate or private in the enclosing context";
5303 ReportOriginalDSA(*this, DSAStack, VD, DVar);
5304 continue;
5305 }
5306 }
5307 }
5308
5309 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
5310 // A variable of class type (or array thereof) that appears in a
5311 // copyin clause requires an accessible, unambiguous copy assignment
5312 // operator for the class type.
5313 Type = Context.getBaseElementType(Type);
5314 CXXRecordDecl *RD =
5315 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
5316 // FIXME This code must be replaced by actual assignment of the
5317 // threadprivate variable.
5318 if (RD) {
5319 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
5320 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
5321 if (MD) {
5322 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
5323 MD->isDeleted()) {
5324 Diag(ELoc, diag::err_omp_required_method)
5325 << getOpenMPClauseName(OMPC_copyprivate) << 2;
5326 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
5327 VarDecl::DeclarationOnly;
5328 Diag(VD->getLocation(),
5329 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5330 << VD;
5331 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
5332 continue;
5333 }
5334 MarkFunctionReferenced(ELoc, MD);
5335 DiagnoseUseOfDecl(MD, ELoc);
5336 }
5337 }
5338
5339 // No need to mark vars as copyprivate, they are already threadprivate or
5340 // implicitly private.
5341 Vars.push_back(DE);
5342 }
5343
5344 if (Vars.empty())
5345 return nullptr;
5346
5347 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
5348}
5349
Alexey Bataev6125da92014-07-21 11:26:11 +00005350OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
5351 SourceLocation StartLoc,
5352 SourceLocation LParenLoc,
5353 SourceLocation EndLoc) {
5354 if (VarList.empty())
5355 return nullptr;
5356
5357 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
5358}
Alexey Bataevdea47612014-07-23 07:46:59 +00005359