blob: 0d349068860b6a643a7742db082a9369a9c85502 [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.
Alexey Bataev26a39242015-01-13 03:35:30 +0000394 if (D->getTLSKind() != VarDecl::TLS_None ||
395 D->getStorageClass() == SC_Register) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000396 DVar.CKind = OMPC_threadprivate;
397 return DVar;
398 }
399 if (Stack[0].SharingMap.count(D)) {
400 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
401 DVar.CKind = OMPC_threadprivate;
402 return DVar;
403 }
404
405 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
406 // in a Construct, C/C++, predetermined, p.1]
407 // Variables with automatic storage duration that are declared in a scope
408 // inside the construct are private.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000409 OpenMPDirectiveKind Kind =
410 FromParent ? getParentDirective() : getCurrentDirective();
411 auto StartI = std::next(Stack.rbegin());
412 auto EndI = std::prev(Stack.rend());
413 if (FromParent && StartI != EndI) {
414 StartI = std::next(StartI);
415 }
416 if (!isParallelOrTaskRegion(Kind)) {
Alexey Bataev8b9cb982014-07-24 02:33:58 +0000417 if (isOpenMPLocal(D, StartI) &&
418 ((D->isLocalVarDecl() && (D->getStorageClass() == SC_Auto ||
419 D->getStorageClass() == SC_None)) ||
420 isa<ParmVarDecl>(D))) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000421 DVar.CKind = OMPC_private;
422 return DVar;
Alexander Musman8dba6642014-04-22 13:09:42 +0000423 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000424
Alexey Bataev24b04aa2015-01-16 07:11:33 +0000425 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
426 // in a Construct, C/C++, predetermined, p.4]
427 // Static data members are shared.
428 if (D->isStaticDataMember()) {
429 DVar.CKind = OMPC_shared;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000430 return DVar;
Alexey Bataev24b04aa2015-01-16 07:11:33 +0000431 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000432
Alexey Bataev24b04aa2015-01-16 07:11:33 +0000433 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
434 // in a Construct, C/C++, predetermined, p.7]
435 // Variables with static storage duration that are declared in a scope
436 // inside the construct are shared.
437 if (D->isStaticLocal()) {
438 DVar.CKind = OMPC_shared;
439 return DVar;
440 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000441 }
442
443 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataev7ff55242014-06-19 09:13:45 +0000444 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
Alexey Bataev758e55e2013-09-06 18:03:48 +0000445 while (Type->isArrayType()) {
446 QualType ElemType = cast<ArrayType>(Type.getTypePtr())->getElementType();
447 Type = ElemType.getNonReferenceType().getCanonicalType();
448 }
449 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
450 // in a Construct, C/C++, predetermined, p.6]
451 // Variables with const qualified type having no mutable member are
452 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000453 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000454 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000455 if (IsConstant &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000456 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000457 // Variables with const-qualified type having no mutable member may be
458 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000459 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
460 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000461 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
462 return DVar;
463
Alexey Bataev758e55e2013-09-06 18:03:48 +0000464 DVar.CKind = OMPC_shared;
465 return DVar;
466 }
467
Alexey Bataev758e55e2013-09-06 18:03:48 +0000468 // Explicitly specified attributes and local variables with predetermined
469 // attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000470 auto I = std::prev(StartI);
471 if (I->SharingMap.count(D)) {
472 DVar.RefExpr = I->SharingMap[D].RefExpr;
473 DVar.CKind = I->SharingMap[D].Attributes;
474 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000475 }
476
477 return DVar;
478}
479
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000480DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D, bool FromParent) {
481 auto StartI = Stack.rbegin();
482 auto EndI = std::prev(Stack.rend());
483 if (FromParent && StartI != EndI) {
484 StartI = std::next(StartI);
485 }
486 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000487}
488
Alexey Bataevf29276e2014-06-18 04:14:57 +0000489template <class ClausesPredicate, class DirectivesPredicate>
490DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000491 DirectivesPredicate DPred,
492 bool FromParent) {
493 auto StartI = std::next(Stack.rbegin());
494 auto EndI = std::prev(Stack.rend());
495 if (FromParent && StartI != EndI) {
496 StartI = std::next(StartI);
497 }
498 for (auto I = StartI, EE = EndI; I != EE; ++I) {
499 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000500 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000501 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000502 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000503 return DVar;
504 }
505 return DSAVarData();
506}
507
Alexey Bataevf29276e2014-06-18 04:14:57 +0000508template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000509DSAStackTy::DSAVarData
510DSAStackTy::hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
511 DirectivesPredicate DPred, bool FromParent) {
512 auto StartI = std::next(Stack.rbegin());
513 auto EndI = std::prev(Stack.rend());
514 if (FromParent && StartI != EndI) {
515 StartI = std::next(StartI);
516 }
517 for (auto I = StartI, EE = EndI; I != EE; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000518 if (!DPred(I->Directive))
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000519 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000520 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000521 if (CPred(DVar.CKind))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000522 return DVar;
523 return DSAVarData();
524 }
525 return DSAVarData();
526}
527
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000528template <class NamedDirectivesPredicate>
529bool DSAStackTy::hasDirective(NamedDirectivesPredicate DPred, bool FromParent) {
530 auto StartI = std::next(Stack.rbegin());
531 auto EndI = std::prev(Stack.rend());
532 if (FromParent && StartI != EndI) {
533 StartI = std::next(StartI);
534 }
535 for (auto I = StartI, EE = EndI; I != EE; ++I) {
536 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
537 return true;
538 }
539 return false;
540}
541
Alexey Bataev758e55e2013-09-06 18:03:48 +0000542void Sema::InitDataSharingAttributesStack() {
543 VarDataSharingAttributesStack = new DSAStackTy(*this);
544}
545
546#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
547
Alexey Bataevf841bd92014-12-16 07:00:22 +0000548bool Sema::IsOpenMPCapturedVar(VarDecl *VD) {
549 assert(LangOpts.OpenMP && "OpenMP is not allowed");
550 if (DSAStack->getCurrentDirective() != OMPD_unknown) {
551 auto DVarPrivate = DSAStack->getTopDSA(VD, /*FromParent=*/false);
552 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
553 return true;
554 DVarPrivate = DSAStack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(),
555 /*FromParent=*/false);
556 return DVarPrivate.CKind != OMPC_unknown;
557 }
558 return false;
559}
560
Alexey Bataeved09d242014-05-28 05:53:51 +0000561void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000562
563void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
564 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000565 Scope *CurScope, SourceLocation Loc) {
566 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000567 PushExpressionEvaluationContext(PotentiallyEvaluated);
568}
569
570void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000571 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
572 // A variable of class type (or array thereof) that appears in a lastprivate
573 // clause requires an accessible, unambiguous default constructor for the
574 // class type, unless the list item is also specified in a firstprivate
575 // clause.
576 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
577 for (auto C : D->clauses()) {
578 if (auto Clause = dyn_cast<OMPLastprivateClause>(C)) {
579 for (auto VarRef : Clause->varlists()) {
580 if (VarRef->isValueDependent() || VarRef->isTypeDependent())
581 continue;
582 auto VD = cast<VarDecl>(cast<DeclRefExpr>(VarRef)->getDecl());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000583 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000584 if (DVar.CKind == OMPC_lastprivate) {
585 SourceLocation ELoc = VarRef->getExprLoc();
586 auto Type = VarRef->getType();
587 if (Type->isArrayType())
588 Type = QualType(Type->getArrayElementTypeNoTypeQual(), 0);
589 CXXRecordDecl *RD =
Alexey Bataev23b69422014-06-18 07:08:49 +0000590 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
591 // FIXME This code must be replaced by actual constructing of the
592 // lastprivate variable.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000593 if (RD) {
594 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
595 PartialDiagnostic PD =
596 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
597 if (!CD ||
598 CheckConstructorAccess(
599 ELoc, CD, InitializedEntity::InitializeTemporary(Type),
600 CD->getAccess(), PD) == AR_inaccessible ||
601 CD->isDeleted()) {
602 Diag(ELoc, diag::err_omp_required_method)
603 << getOpenMPClauseName(OMPC_lastprivate) << 0;
604 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
605 VarDecl::DeclarationOnly;
606 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl
607 : diag::note_defined_here)
608 << VD;
609 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
610 continue;
611 }
612 MarkFunctionReferenced(ELoc, CD);
613 DiagnoseUseOfDecl(CD, ELoc);
614 }
615 }
616 }
617 }
618 }
619 }
620
Alexey Bataev758e55e2013-09-06 18:03:48 +0000621 DSAStack->pop();
622 DiscardCleanupsInEvaluationContext();
623 PopExpressionEvaluationContext();
624}
625
Alexey Bataeva769e072013-03-22 06:34:35 +0000626namespace {
627
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000628class VarDeclFilterCCC : public CorrectionCandidateCallback {
629private:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000630 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +0000631
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000632public:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000633 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +0000634 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000635 NamedDecl *ND = Candidate.getCorrectionDecl();
636 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
637 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000638 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
639 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +0000640 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000641 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000642 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000643};
Alexey Bataeved09d242014-05-28 05:53:51 +0000644} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000645
646ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
647 CXXScopeSpec &ScopeSpec,
648 const DeclarationNameInfo &Id) {
649 LookupResult Lookup(*this, Id, LookupOrdinaryName);
650 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
651
652 if (Lookup.isAmbiguous())
653 return ExprError();
654
655 VarDecl *VD;
656 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +0000657 if (TypoCorrection Corrected = CorrectTypo(
658 Id, LookupOrdinaryName, CurScope, nullptr,
659 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +0000660 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000661 PDiag(Lookup.empty()
662 ? diag::err_undeclared_var_use_suggest
663 : diag::err_omp_expected_var_arg_suggest)
664 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +0000665 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000666 } else {
Richard Smithf9b15102013-08-17 00:46:16 +0000667 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
668 : diag::err_omp_expected_var_arg)
669 << Id.getName();
670 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000671 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000672 } else {
673 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000674 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000675 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
676 return ExprError();
677 }
678 }
679 Lookup.suppressDiagnostics();
680
681 // OpenMP [2.9.2, Syntax, C/C++]
682 // Variables must be file-scope, namespace-scope, or static block-scope.
683 if (!VD->hasGlobalStorage()) {
684 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000685 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
686 bool IsDecl =
687 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000688 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +0000689 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
690 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000691 return ExprError();
692 }
693
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000694 VarDecl *CanonicalVD = VD->getCanonicalDecl();
695 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000696 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
697 // A threadprivate directive for file-scope variables must appear outside
698 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000699 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
700 !getCurLexicalContext()->isTranslationUnit()) {
701 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000702 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
703 bool IsDecl =
704 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
705 Diag(VD->getLocation(),
706 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
707 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000708 return ExprError();
709 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000710 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
711 // A threadprivate directive for static class member variables must appear
712 // in the class definition, in the same scope in which the member
713 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000714 if (CanonicalVD->isStaticDataMember() &&
715 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
716 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000717 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
718 bool IsDecl =
719 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
720 Diag(VD->getLocation(),
721 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
722 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000723 return ExprError();
724 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000725 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
726 // A threadprivate directive for namespace-scope variables must appear
727 // outside any definition or declaration other than the namespace
728 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000729 if (CanonicalVD->getDeclContext()->isNamespace() &&
730 (!getCurLexicalContext()->isFileContext() ||
731 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
732 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000733 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
734 bool IsDecl =
735 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
736 Diag(VD->getLocation(),
737 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
738 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000739 return ExprError();
740 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000741 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
742 // A threadprivate directive for static block-scope variables must appear
743 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000744 if (CanonicalVD->isStaticLocal() && CurScope &&
745 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000746 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000747 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
748 bool IsDecl =
749 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
750 Diag(VD->getLocation(),
751 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
752 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000753 return ExprError();
754 }
755
756 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
757 // A threadprivate directive must lexically precede all references to any
758 // of the variables in its list.
759 if (VD->isUsed()) {
760 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +0000761 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000762 return ExprError();
763 }
764
765 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataevd178ad42014-03-07 08:03:37 +0000766 ExprResult DE = BuildDeclRefExpr(VD, ExprType, VK_LValue, Id.getLoc());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000767 return DE;
768}
769
Alexey Bataeved09d242014-05-28 05:53:51 +0000770Sema::DeclGroupPtrTy
771Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
772 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000773 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000774 CurContext->addDecl(D);
775 return DeclGroupPtrTy::make(DeclGroupRef(D));
776 }
777 return DeclGroupPtrTy();
778}
779
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000780namespace {
781class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
782 Sema &SemaRef;
783
784public:
785 bool VisitDeclRefExpr(const DeclRefExpr *E) {
786 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
787 if (VD->hasLocalStorage()) {
788 SemaRef.Diag(E->getLocStart(),
789 diag::err_omp_local_var_in_threadprivate_init)
790 << E->getSourceRange();
791 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
792 << VD << VD->getSourceRange();
793 return true;
794 }
795 }
796 return false;
797 }
798 bool VisitStmt(const Stmt *S) {
799 for (auto Child : S->children()) {
800 if (Child && Visit(Child))
801 return true;
802 }
803 return false;
804 }
Alexey Bataev23b69422014-06-18 07:08:49 +0000805 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000806};
807} // namespace
808
Alexey Bataeved09d242014-05-28 05:53:51 +0000809OMPThreadPrivateDecl *
810Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000811 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +0000812 for (auto &RefExpr : VarList) {
813 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000814 VarDecl *VD = cast<VarDecl>(DE->getDecl());
815 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +0000816
817 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
818 // A threadprivate variable must not have an incomplete type.
819 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000820 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000821 continue;
822 }
823
824 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
825 // A threadprivate variable must not have a reference type.
826 if (VD->getType()->isReferenceType()) {
827 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000828 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
829 bool IsDecl =
830 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
831 Diag(VD->getLocation(),
832 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
833 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000834 continue;
835 }
836
Richard Smithfd3834f2013-04-13 02:43:54 +0000837 // Check if this is a TLS variable.
Alexey Bataev26a39242015-01-13 03:35:30 +0000838 if (VD->getTLSKind() != VarDecl::TLS_None ||
839 VD->getStorageClass() == SC_Register) {
840 Diag(ILoc, diag::err_omp_var_thread_local)
841 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +0000842 bool IsDecl =
843 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
844 Diag(VD->getLocation(),
845 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
846 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000847 continue;
848 }
849
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000850 // Check if initial value of threadprivate variable reference variable with
851 // local storage (it is not supported by runtime).
852 if (auto Init = VD->getAnyInitializer()) {
853 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000854 if (Checker.Visit(Init))
855 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000856 }
857
Alexey Bataeved09d242014-05-28 05:53:51 +0000858 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +0000859 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +0000860 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
861 Context, SourceRange(Loc, Loc)));
862 if (auto *ML = Context.getASTMutationListener())
863 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +0000864 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000865 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000866 if (!Vars.empty()) {
867 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
868 Vars);
869 D->setAccess(AS_public);
870 }
871 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +0000872}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000873
Alexey Bataev7ff55242014-06-19 09:13:45 +0000874static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
875 const VarDecl *VD, DSAStackTy::DSAVarData DVar,
876 bool IsLoopIterVar = false) {
877 if (DVar.RefExpr) {
878 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
879 << getOpenMPClauseName(DVar.CKind);
880 return;
881 }
882 enum {
883 PDSA_StaticMemberShared,
884 PDSA_StaticLocalVarShared,
885 PDSA_LoopIterVarPrivate,
886 PDSA_LoopIterVarLinear,
887 PDSA_LoopIterVarLastprivate,
888 PDSA_ConstVarShared,
889 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000890 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000891 PDSA_LocalVarPrivate,
892 PDSA_Implicit
893 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000894 bool ReportHint = false;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000895 auto ReportLoc = VD->getLocation();
Alexey Bataev7ff55242014-06-19 09:13:45 +0000896 if (IsLoopIterVar) {
897 if (DVar.CKind == OMPC_private)
898 Reason = PDSA_LoopIterVarPrivate;
899 else if (DVar.CKind == OMPC_lastprivate)
900 Reason = PDSA_LoopIterVarLastprivate;
901 else
902 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000903 } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) {
904 Reason = PDSA_TaskVarFirstprivate;
905 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000906 } else if (VD->isStaticLocal())
907 Reason = PDSA_StaticLocalVarShared;
908 else if (VD->isStaticDataMember())
909 Reason = PDSA_StaticMemberShared;
910 else if (VD->isFileVarDecl())
911 Reason = PDSA_GlobalVarShared;
912 else if (VD->getType().isConstant(SemaRef.getASTContext()))
913 Reason = PDSA_ConstVarShared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000914 else if (VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +0000915 ReportHint = true;
916 Reason = PDSA_LocalVarPrivate;
917 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000918 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000919 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +0000920 << Reason << ReportHint
921 << getOpenMPDirectiveName(Stack->getCurrentDirective());
922 } else if (DVar.ImplicitDSALoc.isValid()) {
923 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
924 << getOpenMPClauseName(DVar.CKind);
925 }
Alexey Bataev7ff55242014-06-19 09:13:45 +0000926}
927
Alexey Bataev758e55e2013-09-06 18:03:48 +0000928namespace {
929class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
930 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000931 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000932 bool ErrorFound;
933 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000934 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000935 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +0000936
Alexey Bataev758e55e2013-09-06 18:03:48 +0000937public:
938 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000939 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000940 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +0000941 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
942 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000943
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000944 auto DVar = Stack->getTopDSA(VD, false);
945 // Check if the variable has explicit DSA set and stop analysis if it so.
946 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000947
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000948 auto ELoc = E->getExprLoc();
949 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000950 // The default(none) clause requires that each variable that is referenced
951 // in the construct, and does not have a predetermined data-sharing
952 // attribute, must have its data-sharing attribute explicitly determined
953 // by being listed in a data-sharing attribute clause.
954 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000955 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +0000956 VarsWithInheritedDSA.count(VD) == 0) {
957 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000958 return;
959 }
960
961 // OpenMP [2.9.3.6, Restrictions, p.2]
962 // A list item that appears in a reduction clause of the innermost
963 // enclosing worksharing or parallel construct may not be accessed in an
964 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000965 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000966 [](OpenMPDirectiveKind K) -> bool {
967 return isOpenMPParallelDirective(K) ||
Alexey Bataev13314bf2014-10-09 04:18:56 +0000968 isOpenMPWorksharingDirective(K) ||
969 isOpenMPTeamsDirective(K);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000970 },
971 false);
Alexey Bataevc5e02582014-06-16 07:08:35 +0000972 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
973 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000974 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
975 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +0000976 return;
977 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000978
979 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000980 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000981 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000982 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000983 }
984 }
985 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000986 for (auto *C : S->clauses()) {
987 // Skip analysis of arguments of implicitly defined firstprivate clause
988 // for task directives.
989 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
990 for (auto *CC : C->children()) {
991 if (CC)
992 Visit(CC);
993 }
994 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000995 }
996 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000997 for (auto *C : S->children()) {
998 if (C && !isa<OMPExecutableDirective>(C))
999 Visit(C);
1000 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001001 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001002
1003 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001004 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001005 llvm::DenseMap<VarDecl *, Expr *> &getVarsWithInheritedDSA() {
1006 return VarsWithInheritedDSA;
1007 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001008
Alexey Bataev7ff55242014-06-19 09:13:45 +00001009 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1010 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00001011};
Alexey Bataeved09d242014-05-28 05:53:51 +00001012} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001013
Alexey Bataevbae9a792014-06-27 10:37:06 +00001014void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001015 switch (DKind) {
1016 case OMPD_parallel: {
1017 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1018 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001019 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001020 std::make_pair(".global_tid.", KmpInt32PtrTy),
1021 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1022 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001023 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001024 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1025 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001026 break;
1027 }
1028 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001029 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001030 std::make_pair(StringRef(), QualType()) // __context with shared vars
1031 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001032 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1033 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001034 break;
1035 }
1036 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001037 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001038 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001039 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001040 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1041 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001042 break;
1043 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001044 case OMPD_for_simd: {
1045 Sema::CapturedParamNameType Params[] = {
1046 std::make_pair(StringRef(), QualType()) // __context with shared vars
1047 };
1048 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1049 Params);
1050 break;
1051 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001052 case OMPD_sections: {
1053 Sema::CapturedParamNameType Params[] = {
1054 std::make_pair(StringRef(), QualType()) // __context with shared vars
1055 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001056 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1057 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001058 break;
1059 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001060 case OMPD_section: {
1061 Sema::CapturedParamNameType Params[] = {
1062 std::make_pair(StringRef(), QualType()) // __context with shared vars
1063 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001064 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1065 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001066 break;
1067 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001068 case OMPD_single: {
1069 Sema::CapturedParamNameType Params[] = {
1070 std::make_pair(StringRef(), QualType()) // __context with shared vars
1071 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001072 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1073 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001074 break;
1075 }
Alexander Musman80c22892014-07-17 08:54:58 +00001076 case OMPD_master: {
1077 Sema::CapturedParamNameType Params[] = {
1078 std::make_pair(StringRef(), QualType()) // __context with shared vars
1079 };
1080 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1081 Params);
1082 break;
1083 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001084 case OMPD_critical: {
1085 Sema::CapturedParamNameType Params[] = {
1086 std::make_pair(StringRef(), QualType()) // __context with shared vars
1087 };
1088 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1089 Params);
1090 break;
1091 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001092 case OMPD_parallel_for: {
1093 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1094 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1095 Sema::CapturedParamNameType Params[] = {
1096 std::make_pair(".global_tid.", KmpInt32PtrTy),
1097 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1098 std::make_pair(StringRef(), QualType()) // __context with shared vars
1099 };
1100 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1101 Params);
1102 break;
1103 }
Alexander Musmane4e893b2014-09-23 09:33:00 +00001104 case OMPD_parallel_for_simd: {
1105 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1106 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1107 Sema::CapturedParamNameType Params[] = {
1108 std::make_pair(".global_tid.", KmpInt32PtrTy),
1109 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1110 std::make_pair(StringRef(), QualType()) // __context with shared vars
1111 };
1112 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1113 Params);
1114 break;
1115 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001116 case OMPD_parallel_sections: {
1117 Sema::CapturedParamNameType Params[] = {
1118 std::make_pair(StringRef(), QualType()) // __context with shared vars
1119 };
1120 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1121 Params);
1122 break;
1123 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001124 case OMPD_task: {
1125 Sema::CapturedParamNameType Params[] = {
1126 std::make_pair(StringRef(), QualType()) // __context with shared vars
1127 };
1128 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1129 Params);
1130 break;
1131 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001132 case OMPD_ordered: {
1133 Sema::CapturedParamNameType Params[] = {
1134 std::make_pair(StringRef(), QualType()) // __context with shared vars
1135 };
1136 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1137 Params);
1138 break;
1139 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001140 case OMPD_atomic: {
1141 Sema::CapturedParamNameType Params[] = {
1142 std::make_pair(StringRef(), QualType()) // __context with shared vars
1143 };
1144 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1145 Params);
1146 break;
1147 }
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001148 case OMPD_target: {
1149 Sema::CapturedParamNameType Params[] = {
1150 std::make_pair(StringRef(), QualType()) // __context with shared vars
1151 };
1152 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1153 Params);
1154 break;
1155 }
Alexey Bataev13314bf2014-10-09 04:18:56 +00001156 case OMPD_teams: {
1157 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1158 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1159 Sema::CapturedParamNameType Params[] = {
1160 std::make_pair(".global_tid.", KmpInt32PtrTy),
1161 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1162 std::make_pair(StringRef(), QualType()) // __context with shared vars
1163 };
1164 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1165 Params);
1166 break;
1167 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001168 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001169 case OMPD_taskyield:
1170 case OMPD_barrier:
1171 case OMPD_taskwait:
1172 case OMPD_flush:
Alexey Bataev9959db52014-05-06 10:08:46 +00001173 llvm_unreachable("OpenMP Directive is not allowed");
1174 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001175 llvm_unreachable("Unknown OpenMP directive");
1176 }
1177}
1178
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001179static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1180 OpenMPDirectiveKind CurrentRegion,
1181 const DeclarationNameInfo &CurrentName,
1182 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001183 // Allowed nesting of constructs
1184 // +------------------+-----------------+------------------------------------+
1185 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1186 // +------------------+-----------------+------------------------------------+
1187 // | parallel | parallel | * |
1188 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001189 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001190 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001191 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001192 // | parallel | simd | * |
1193 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001194 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001195 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001196 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001197 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001198 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001199 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001200 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001201 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001202 // | parallel | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001203 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001204 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001205 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001206 // | parallel | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001207 // | parallel | teams | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001208 // +------------------+-----------------+------------------------------------+
1209 // | for | parallel | * |
1210 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001211 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001212 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001213 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001214 // | for | simd | * |
1215 // | for | sections | + |
1216 // | for | section | + |
1217 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001218 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001219 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001220 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001221 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001222 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001223 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001224 // | for | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001225 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001226 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001227 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001228 // | for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001229 // | for | teams | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001230 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001231 // | master | parallel | * |
1232 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001233 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001234 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001235 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001236 // | master | simd | * |
1237 // | master | sections | + |
1238 // | master | section | + |
1239 // | master | single | + |
1240 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001241 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001242 // | master |parallel sections| * |
1243 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001244 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001245 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001246 // | master | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001247 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001248 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001249 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001250 // | master | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001251 // | master | teams | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001252 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001253 // | critical | parallel | * |
1254 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001255 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001256 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001257 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001258 // | critical | simd | * |
1259 // | critical | sections | + |
1260 // | critical | section | + |
1261 // | critical | single | + |
1262 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001263 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001264 // | critical |parallel sections| * |
1265 // | critical | task | * |
1266 // | critical | taskyield | * |
1267 // | critical | barrier | + |
1268 // | critical | taskwait | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001269 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001270 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001271 // | critical | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001272 // | critical | teams | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001273 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001274 // | simd | parallel | |
1275 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001276 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00001277 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001278 // | simd | critical | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001279 // | simd | simd | |
1280 // | simd | sections | |
1281 // | simd | section | |
1282 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001283 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001284 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001285 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001286 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00001287 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001288 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001289 // | simd | taskwait | |
Alexey Bataev6125da92014-07-21 11:26:11 +00001290 // | simd | flush | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001291 // | simd | ordered | |
Alexey Bataev0162e452014-07-22 10:10:35 +00001292 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001293 // | simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001294 // | simd | teams | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001295 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00001296 // | for simd | parallel | |
1297 // | for simd | for | |
1298 // | for simd | for simd | |
1299 // | for simd | master | |
1300 // | for simd | critical | |
1301 // | for simd | simd | |
1302 // | for simd | sections | |
1303 // | for simd | section | |
1304 // | for simd | single | |
1305 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001306 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001307 // | for simd |parallel sections| |
1308 // | for simd | task | |
1309 // | for simd | taskyield | |
1310 // | for simd | barrier | |
1311 // | for simd | taskwait | |
1312 // | for simd | flush | |
1313 // | for simd | ordered | |
1314 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001315 // | for simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001316 // | for simd | teams | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001317 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00001318 // | parallel for simd| parallel | |
1319 // | parallel for simd| for | |
1320 // | parallel for simd| for simd | |
1321 // | parallel for simd| master | |
1322 // | parallel for simd| critical | |
1323 // | parallel for simd| simd | |
1324 // | parallel for simd| sections | |
1325 // | parallel for simd| section | |
1326 // | parallel for simd| single | |
1327 // | parallel for simd| parallel for | |
1328 // | parallel for simd|parallel for simd| |
1329 // | parallel for simd|parallel sections| |
1330 // | parallel for simd| task | |
1331 // | parallel for simd| taskyield | |
1332 // | parallel for simd| barrier | |
1333 // | parallel for simd| taskwait | |
1334 // | parallel for simd| flush | |
1335 // | parallel for simd| ordered | |
1336 // | parallel for simd| atomic | |
1337 // | parallel for simd| target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001338 // | parallel for simd| teams | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001339 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001340 // | sections | parallel | * |
1341 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001342 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001343 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001344 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001345 // | sections | simd | * |
1346 // | sections | sections | + |
1347 // | sections | section | * |
1348 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001349 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001350 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001351 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001352 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001353 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001354 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001355 // | sections | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001356 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001357 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001358 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001359 // | sections | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001360 // | sections | teams | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001361 // +------------------+-----------------+------------------------------------+
1362 // | section | parallel | * |
1363 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001364 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001365 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001366 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001367 // | section | simd | * |
1368 // | section | sections | + |
1369 // | section | section | + |
1370 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001371 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001372 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001373 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001374 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001375 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001376 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001377 // | section | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001378 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001379 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001380 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001381 // | section | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001382 // | section | teams | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001383 // +------------------+-----------------+------------------------------------+
1384 // | single | parallel | * |
1385 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001386 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001387 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001388 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001389 // | single | simd | * |
1390 // | single | sections | + |
1391 // | single | section | + |
1392 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001393 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001394 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001395 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001396 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001397 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001398 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001399 // | single | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001400 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001401 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001402 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001403 // | single | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001404 // | single | teams | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001405 // +------------------+-----------------+------------------------------------+
1406 // | parallel for | parallel | * |
1407 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001408 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001409 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001410 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001411 // | parallel for | simd | * |
1412 // | parallel for | sections | + |
1413 // | parallel for | section | + |
1414 // | parallel for | single | + |
1415 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001416 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001417 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001418 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001419 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001420 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001421 // | parallel for | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001422 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001423 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001424 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001425 // | parallel for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001426 // | parallel for | teams | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001427 // +------------------+-----------------+------------------------------------+
1428 // | parallel sections| parallel | * |
1429 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001430 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001431 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001432 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001433 // | parallel sections| simd | * |
1434 // | parallel sections| sections | + |
1435 // | parallel sections| section | * |
1436 // | parallel sections| single | + |
1437 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001438 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001439 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001440 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001441 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001442 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001443 // | parallel sections| taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001444 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001445 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001446 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001447 // | parallel sections| target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001448 // | parallel sections| teams | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001449 // +------------------+-----------------+------------------------------------+
1450 // | task | parallel | * |
1451 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001452 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001453 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001454 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001455 // | task | simd | * |
1456 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001457 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001458 // | task | single | + |
1459 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001460 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001461 // | task |parallel sections| * |
1462 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001463 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001464 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001465 // | task | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001466 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001467 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001468 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001469 // | task | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001470 // | task | teams | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001471 // +------------------+-----------------+------------------------------------+
1472 // | ordered | parallel | * |
1473 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001474 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001475 // | ordered | master | * |
1476 // | ordered | critical | * |
1477 // | ordered | simd | * |
1478 // | ordered | sections | + |
1479 // | ordered | section | + |
1480 // | ordered | single | + |
1481 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001482 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001483 // | ordered |parallel sections| * |
1484 // | ordered | task | * |
1485 // | ordered | taskyield | * |
1486 // | ordered | barrier | + |
1487 // | ordered | taskwait | * |
1488 // | ordered | flush | * |
1489 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001490 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001491 // | ordered | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001492 // | ordered | teams | + |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001493 // +------------------+-----------------+------------------------------------+
1494 // | atomic | parallel | |
1495 // | atomic | for | |
1496 // | atomic | for simd | |
1497 // | atomic | master | |
1498 // | atomic | critical | |
1499 // | atomic | simd | |
1500 // | atomic | sections | |
1501 // | atomic | section | |
1502 // | atomic | single | |
1503 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001504 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001505 // | atomic |parallel sections| |
1506 // | atomic | task | |
1507 // | atomic | taskyield | |
1508 // | atomic | barrier | |
1509 // | atomic | taskwait | |
1510 // | atomic | flush | |
1511 // | atomic | ordered | |
1512 // | atomic | atomic | |
1513 // | atomic | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001514 // | atomic | teams | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001515 // +------------------+-----------------+------------------------------------+
1516 // | target | parallel | * |
1517 // | target | for | * |
1518 // | target | for simd | * |
1519 // | target | master | * |
1520 // | target | critical | * |
1521 // | target | simd | * |
1522 // | target | sections | * |
1523 // | target | section | * |
1524 // | target | single | * |
1525 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001526 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001527 // | target |parallel sections| * |
1528 // | target | task | * |
1529 // | target | taskyield | * |
1530 // | target | barrier | * |
1531 // | target | taskwait | * |
1532 // | target | flush | * |
1533 // | target | ordered | * |
1534 // | target | atomic | * |
1535 // | target | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001536 // | target | teams | * |
1537 // +------------------+-----------------+------------------------------------+
1538 // | teams | parallel | * |
1539 // | teams | for | + |
1540 // | teams | for simd | + |
1541 // | teams | master | + |
1542 // | teams | critical | + |
1543 // | teams | simd | + |
1544 // | teams | sections | + |
1545 // | teams | section | + |
1546 // | teams | single | + |
1547 // | teams | parallel for | * |
1548 // | teams |parallel for simd| * |
1549 // | teams |parallel sections| * |
1550 // | teams | task | + |
1551 // | teams | taskyield | + |
1552 // | teams | barrier | + |
1553 // | teams | taskwait | + |
1554 // | teams | flush | + |
1555 // | teams | ordered | + |
1556 // | teams | atomic | + |
1557 // | teams | target | + |
1558 // | teams | teams | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001559 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00001560 if (Stack->getCurScope()) {
1561 auto ParentRegion = Stack->getParentDirective();
1562 bool NestingProhibited = false;
1563 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001564 enum {
1565 NoRecommend,
1566 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00001567 ShouldBeInOrderedRegion,
1568 ShouldBeInTargetRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001569 } Recommend = NoRecommend;
Alexey Bataev549210e2014-06-24 04:39:47 +00001570 if (isOpenMPSimdDirective(ParentRegion)) {
1571 // OpenMP [2.16, Nesting of Regions]
1572 // OpenMP constructs may not be nested inside a simd region.
1573 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
1574 return true;
1575 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001576 if (ParentRegion == OMPD_atomic) {
1577 // OpenMP [2.16, Nesting of Regions]
1578 // OpenMP constructs may not be nested inside an atomic region.
1579 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
1580 return true;
1581 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001582 if (CurrentRegion == OMPD_section) {
1583 // OpenMP [2.7.2, sections Construct, Restrictions]
1584 // Orphaned section directives are prohibited. That is, the section
1585 // directives must appear within the sections construct and must not be
1586 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001587 if (ParentRegion != OMPD_sections &&
1588 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001589 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
1590 << (ParentRegion != OMPD_unknown)
1591 << getOpenMPDirectiveName(ParentRegion);
1592 return true;
1593 }
1594 return false;
1595 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001596 // Allow some constructs to be orphaned (they could be used in functions,
1597 // called from OpenMP regions with the required preconditions).
1598 if (ParentRegion == OMPD_unknown)
1599 return false;
Alexander Musman80c22892014-07-17 08:54:58 +00001600 if (CurrentRegion == OMPD_master) {
1601 // OpenMP [2.16, Nesting of Regions]
1602 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00001603 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00001604 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
1605 ParentRegion == OMPD_task;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001606 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
1607 // OpenMP [2.16, Nesting of Regions]
1608 // A critical region may not be nested (closely or otherwise) inside a
1609 // critical region with the same name. Note that this restriction is not
1610 // sufficient to prevent deadlock.
1611 SourceLocation PreviousCriticalLoc;
1612 bool DeadLock =
1613 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
1614 OpenMPDirectiveKind K,
1615 const DeclarationNameInfo &DNI,
1616 SourceLocation Loc)
1617 ->bool {
1618 if (K == OMPD_critical &&
1619 DNI.getName() == CurrentName.getName()) {
1620 PreviousCriticalLoc = Loc;
1621 return true;
1622 } else
1623 return false;
1624 },
1625 false /* skip top directive */);
1626 if (DeadLock) {
1627 SemaRef.Diag(StartLoc,
1628 diag::err_omp_prohibited_region_critical_same_name)
1629 << CurrentName.getName();
1630 if (PreviousCriticalLoc.isValid())
1631 SemaRef.Diag(PreviousCriticalLoc,
1632 diag::note_omp_previous_critical_region);
1633 return true;
1634 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001635 } else if (CurrentRegion == OMPD_barrier) {
1636 // OpenMP [2.16, Nesting of Regions]
1637 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00001638 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001639 NestingProhibited =
1640 isOpenMPWorksharingDirective(ParentRegion) ||
1641 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1642 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00001643 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00001644 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00001645 // OpenMP [2.16, Nesting of Regions]
1646 // A worksharing region may not be closely nested inside a worksharing,
1647 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001648 NestingProhibited =
Alexander Musmanf82886e2014-09-18 05:12:34 +00001649 isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001650 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1651 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
1652 Recommend = ShouldBeInParallelRegion;
1653 } else if (CurrentRegion == OMPD_ordered) {
1654 // OpenMP [2.16, Nesting of Regions]
1655 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00001656 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001657 // An ordered region must be closely nested inside a loop region (or
1658 // parallel loop region) with an ordered clause.
1659 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00001660 ParentRegion == OMPD_task ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001661 !Stack->isParentOrderedRegion();
1662 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00001663 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
1664 // OpenMP [2.16, Nesting of Regions]
1665 // If specified, a teams construct must be contained within a target
1666 // construct.
1667 NestingProhibited = ParentRegion != OMPD_target;
1668 Recommend = ShouldBeInTargetRegion;
1669 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
1670 }
1671 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
1672 // OpenMP [2.16, Nesting of Regions]
1673 // distribute, parallel, parallel sections, parallel workshare, and the
1674 // parallel loop and parallel loop SIMD constructs are the only OpenMP
1675 // constructs that can be closely nested in the teams region.
1676 // TODO: add distribute directive.
1677 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion);
1678 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00001679 }
1680 if (NestingProhibited) {
1681 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001682 << CloseNesting << getOpenMPDirectiveName(ParentRegion) << Recommend
1683 << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00001684 return true;
1685 }
1686 }
1687 return false;
1688}
1689
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001690StmtResult Sema::ActOnOpenMPExecutableDirective(OpenMPDirectiveKind Kind,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001691 const DeclarationNameInfo &DirName,
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001692 ArrayRef<OMPClause *> Clauses,
1693 Stmt *AStmt,
1694 SourceLocation StartLoc,
1695 SourceLocation EndLoc) {
1696 StmtResult Res = StmtError();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001697 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00001698 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001699
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001700 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev68446b72014-07-18 07:47:19 +00001701 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001702 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00001703 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00001704 if (AStmt) {
1705 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
1706
1707 // Check default data sharing attributes for referenced variables.
1708 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
1709 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
1710 if (DSAChecker.isErrorFound())
1711 return StmtError();
1712 // Generate list of implicitly defined firstprivate variables.
1713 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00001714
1715 if (!DSAChecker.getImplicitFirstprivate().empty()) {
1716 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
1717 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
1718 SourceLocation(), SourceLocation())) {
1719 ClausesWithImplicit.push_back(Implicit);
1720 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
1721 DSAChecker.getImplicitFirstprivate().size();
1722 } else
1723 ErrorFound = true;
1724 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001725 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001726
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001727 switch (Kind) {
1728 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00001729 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
1730 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001731 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001732 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001733 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
1734 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001735 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00001736 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001737 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
1738 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001739 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00001740 case OMPD_for_simd:
1741 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
1742 EndLoc, VarsWithInheritedDSA);
1743 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001744 case OMPD_sections:
1745 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
1746 EndLoc);
1747 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001748 case OMPD_section:
1749 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00001750 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001751 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
1752 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001753 case OMPD_single:
1754 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
1755 EndLoc);
1756 break;
Alexander Musman80c22892014-07-17 08:54:58 +00001757 case OMPD_master:
1758 assert(ClausesWithImplicit.empty() &&
1759 "No clauses are allowed for 'omp master' directive");
1760 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
1761 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001762 case OMPD_critical:
1763 assert(ClausesWithImplicit.empty() &&
1764 "No clauses are allowed for 'omp critical' directive");
1765 Res = ActOnOpenMPCriticalDirective(DirName, AStmt, StartLoc, EndLoc);
1766 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00001767 case OMPD_parallel_for:
1768 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
1769 EndLoc, VarsWithInheritedDSA);
1770 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00001771 case OMPD_parallel_for_simd:
1772 Res = ActOnOpenMPParallelForSimdDirective(
1773 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
1774 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001775 case OMPD_parallel_sections:
1776 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
1777 StartLoc, EndLoc);
1778 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001779 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001780 Res =
1781 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
1782 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00001783 case OMPD_taskyield:
1784 assert(ClausesWithImplicit.empty() &&
1785 "No clauses are allowed for 'omp taskyield' directive");
1786 assert(AStmt == nullptr &&
1787 "No associated statement allowed for 'omp taskyield' directive");
1788 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
1789 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001790 case OMPD_barrier:
1791 assert(ClausesWithImplicit.empty() &&
1792 "No clauses are allowed for 'omp barrier' directive");
1793 assert(AStmt == nullptr &&
1794 "No associated statement allowed for 'omp barrier' directive");
1795 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
1796 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00001797 case OMPD_taskwait:
1798 assert(ClausesWithImplicit.empty() &&
1799 "No clauses are allowed for 'omp taskwait' directive");
1800 assert(AStmt == nullptr &&
1801 "No associated statement allowed for 'omp taskwait' directive");
1802 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
1803 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00001804 case OMPD_flush:
1805 assert(AStmt == nullptr &&
1806 "No associated statement allowed for 'omp flush' directive");
1807 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
1808 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001809 case OMPD_ordered:
1810 assert(ClausesWithImplicit.empty() &&
1811 "No clauses are allowed for 'omp ordered' directive");
1812 Res = ActOnOpenMPOrderedDirective(AStmt, StartLoc, EndLoc);
1813 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00001814 case OMPD_atomic:
1815 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
1816 EndLoc);
1817 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00001818 case OMPD_teams:
1819 Res =
1820 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
1821 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001822 case OMPD_target:
1823 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
1824 EndLoc);
1825 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001826 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001827 llvm_unreachable("OpenMP Directive is not allowed");
1828 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001829 llvm_unreachable("Unknown OpenMP directive");
1830 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001831
Alexey Bataev4acb8592014-07-07 13:01:15 +00001832 for (auto P : VarsWithInheritedDSA) {
1833 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
1834 << P.first << P.second->getSourceRange();
1835 }
1836 if (!VarsWithInheritedDSA.empty())
1837 return StmtError();
1838
Alexey Bataeved09d242014-05-28 05:53:51 +00001839 if (ErrorFound)
1840 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001841 return Res;
1842}
1843
1844StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
1845 Stmt *AStmt,
1846 SourceLocation StartLoc,
1847 SourceLocation EndLoc) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001848 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1849 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
1850 // 1.2.2 OpenMP Language Terminology
1851 // Structured block - An executable statement with a single entry at the
1852 // top and a single exit at the bottom.
1853 // The point of exit cannot be a branch out of the structured block.
1854 // longjmp() and throw() must not violate the entry/exit criteria.
1855 CS->getCapturedDecl()->setNothrow();
1856
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001857 getCurFunction()->setHasBranchProtectedScope();
1858
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001859 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
1860 AStmt);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001861}
1862
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001863namespace {
1864/// \brief Helper class for checking canonical form of the OpenMP loops and
1865/// extracting iteration space of each loop in the loop nest, that will be used
1866/// for IR generation.
1867class OpenMPIterationSpaceChecker {
1868 /// \brief Reference to Sema.
1869 Sema &SemaRef;
1870 /// \brief A location for diagnostics (when there is no some better location).
1871 SourceLocation DefaultLoc;
1872 /// \brief A location for diagnostics (when increment is not compatible).
1873 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00001874 /// \brief A source location for referring to loop init later.
1875 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001876 /// \brief A source location for referring to condition later.
1877 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00001878 /// \brief A source location for referring to increment later.
1879 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001880 /// \brief Loop variable.
1881 VarDecl *Var;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001882 /// \brief Reference to loop variable.
1883 DeclRefExpr *VarRef;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001884 /// \brief Lower bound (initializer for the var).
1885 Expr *LB;
1886 /// \brief Upper bound.
1887 Expr *UB;
1888 /// \brief Loop step (increment).
1889 Expr *Step;
1890 /// \brief This flag is true when condition is one of:
1891 /// Var < UB
1892 /// Var <= UB
1893 /// UB > Var
1894 /// UB >= Var
1895 bool TestIsLessOp;
1896 /// \brief This flag is true when condition is strict ( < or > ).
1897 bool TestIsStrictOp;
1898 /// \brief This flag is true when step is subtracted on each iteration.
1899 bool SubtractStep;
1900
1901public:
1902 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
1903 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
Alexander Musmana5f070a2014-10-01 06:03:56 +00001904 InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()),
1905 IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001906 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
1907 TestIsStrictOp(false), SubtractStep(false) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001908 /// \brief Check init-expr for canonical loop form and save loop counter
1909 /// variable - #Var and its initialization value - #LB.
1910 bool CheckInit(Stmt *S);
1911 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
1912 /// for less/greater and for strict/non-strict comparison.
1913 bool CheckCond(Expr *S);
1914 /// \brief Check incr-expr for canonical loop form and return true if it
1915 /// does not conform, otherwise save loop step (#Step).
1916 bool CheckInc(Expr *S);
1917 /// \brief Return the loop counter variable.
1918 VarDecl *GetLoopVar() const { return Var; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001919 /// \brief Return the reference expression to loop counter variable.
1920 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00001921 /// \brief Source range of the loop init.
1922 SourceRange GetInitSrcRange() const { return InitSrcRange; }
1923 /// \brief Source range of the loop condition.
1924 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
1925 /// \brief Source range of the loop increment.
1926 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
1927 /// \brief True if the step should be subtracted.
1928 bool ShouldSubtractStep() const { return SubtractStep; }
1929 /// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00001930 Expr *BuildNumIterations(Scope *S, const bool LimitedType) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00001931 /// \brief Build reference expression to the counter be used for codegen.
1932 Expr *BuildCounterVar() const;
1933 /// \brief Build initization of the counter be used for codegen.
1934 Expr *BuildCounterInit() const;
1935 /// \brief Build step of the counter be used for codegen.
1936 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001937 /// \brief Return true if any expression is dependent.
1938 bool Dependent() const;
1939
1940private:
1941 /// \brief Check the right-hand side of an assignment in the increment
1942 /// expression.
1943 bool CheckIncRHS(Expr *RHS);
1944 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001945 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001946 /// \brief Helper to set upper bound.
1947 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, const SourceRange &SR,
1948 const SourceLocation &SL);
1949 /// \brief Helper to set loop increment.
1950 bool SetStep(Expr *NewStep, bool Subtract);
1951};
1952
1953bool OpenMPIterationSpaceChecker::Dependent() const {
1954 if (!Var) {
1955 assert(!LB && !UB && !Step);
1956 return false;
1957 }
1958 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
1959 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
1960}
1961
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001962bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
1963 DeclRefExpr *NewVarRefExpr,
1964 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001965 // State consistency checking to ensure correct usage.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001966 assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
1967 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001968 if (!NewVar || !NewLB)
1969 return true;
1970 Var = NewVar;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001971 VarRef = NewVarRefExpr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001972 LB = NewLB;
1973 return false;
1974}
1975
1976bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
1977 const SourceRange &SR,
1978 const SourceLocation &SL) {
1979 // State consistency checking to ensure correct usage.
1980 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
1981 !TestIsLessOp && !TestIsStrictOp);
1982 if (!NewUB)
1983 return true;
1984 UB = NewUB;
1985 TestIsLessOp = LessOp;
1986 TestIsStrictOp = StrictOp;
1987 ConditionSrcRange = SR;
1988 ConditionLoc = SL;
1989 return false;
1990}
1991
1992bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
1993 // State consistency checking to ensure correct usage.
1994 assert(Var != nullptr && LB != nullptr && Step == nullptr);
1995 if (!NewStep)
1996 return true;
1997 if (!NewStep->isValueDependent()) {
1998 // Check that the step is integer expression.
1999 SourceLocation StepLoc = NewStep->getLocStart();
2000 ExprResult Val =
2001 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
2002 if (Val.isInvalid())
2003 return true;
2004 NewStep = Val.get();
2005
2006 // OpenMP [2.6, Canonical Loop Form, Restrictions]
2007 // If test-expr is of form var relational-op b and relational-op is < or
2008 // <= then incr-expr must cause var to increase on each iteration of the
2009 // loop. If test-expr is of form var relational-op b and relational-op is
2010 // > or >= then incr-expr must cause var to decrease on each iteration of
2011 // the loop.
2012 // If test-expr is of form b relational-op var and relational-op is < or
2013 // <= then incr-expr must cause var to decrease on each iteration of the
2014 // loop. If test-expr is of form b relational-op var and relational-op is
2015 // > or >= then incr-expr must cause var to increase on each iteration of
2016 // the loop.
2017 llvm::APSInt Result;
2018 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
2019 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
2020 bool IsConstNeg =
2021 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00002022 bool IsConstPos =
2023 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002024 bool IsConstZero = IsConstant && !Result.getBoolValue();
2025 if (UB && (IsConstZero ||
2026 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00002027 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002028 SemaRef.Diag(NewStep->getExprLoc(),
2029 diag::err_omp_loop_incr_not_compatible)
2030 << Var << TestIsLessOp << NewStep->getSourceRange();
2031 SemaRef.Diag(ConditionLoc,
2032 diag::note_omp_loop_cond_requres_compatible_incr)
2033 << TestIsLessOp << ConditionSrcRange;
2034 return true;
2035 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002036 if (TestIsLessOp == Subtract) {
2037 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
2038 NewStep).get();
2039 Subtract = !Subtract;
2040 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002041 }
2042
2043 Step = NewStep;
2044 SubtractStep = Subtract;
2045 return false;
2046}
2047
2048bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S) {
2049 // Check init-expr for canonical loop form and save loop counter
2050 // variable - #Var and its initialization value - #LB.
2051 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
2052 // var = lb
2053 // integer-type var = lb
2054 // random-access-iterator-type var = lb
2055 // pointer-type var = lb
2056 //
2057 if (!S) {
2058 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
2059 return true;
2060 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002061 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002062 if (Expr *E = dyn_cast<Expr>(S))
2063 S = E->IgnoreParens();
2064 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2065 if (BO->getOpcode() == BO_Assign)
2066 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002067 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002068 BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002069 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
2070 if (DS->isSingleDecl()) {
2071 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
2072 if (Var->hasInit()) {
2073 // Accept non-canonical init form here but emit ext. warning.
2074 if (Var->getInitStyle() != VarDecl::CInit)
2075 SemaRef.Diag(S->getLocStart(),
2076 diag::ext_omp_loop_not_canonical_init)
2077 << S->getSourceRange();
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002078 return SetVarAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002079 }
2080 }
2081 }
2082 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
2083 if (CE->getOperator() == OO_Equal)
2084 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002085 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
2086 CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002087
2088 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
2089 << S->getSourceRange();
2090 return true;
2091}
2092
Alexey Bataev23b69422014-06-18 07:08:49 +00002093/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002094/// variable (which may be the loop variable) if possible.
2095static const VarDecl *GetInitVarDecl(const Expr *E) {
2096 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00002097 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002098 E = E->IgnoreParenImpCasts();
2099 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
2100 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
2101 if (Ctor->isCopyConstructor() && CE->getNumArgs() == 1 &&
2102 CE->getArg(0) != nullptr)
2103 E = CE->getArg(0)->IgnoreParenImpCasts();
2104 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
2105 if (!DRE)
2106 return nullptr;
2107 return dyn_cast<VarDecl>(DRE->getDecl());
2108}
2109
2110bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
2111 // Check test-expr for canonical form, save upper-bound UB, flags for
2112 // less/greater and for strict/non-strict comparison.
2113 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2114 // var relational-op b
2115 // b relational-op var
2116 //
2117 if (!S) {
2118 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
2119 return true;
2120 }
2121 S = S->IgnoreParenImpCasts();
2122 SourceLocation CondLoc = S->getLocStart();
2123 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2124 if (BO->isRelationalOp()) {
2125 if (GetInitVarDecl(BO->getLHS()) == Var)
2126 return SetUB(BO->getRHS(),
2127 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
2128 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2129 BO->getSourceRange(), BO->getOperatorLoc());
2130 if (GetInitVarDecl(BO->getRHS()) == Var)
2131 return SetUB(BO->getLHS(),
2132 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
2133 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2134 BO->getSourceRange(), BO->getOperatorLoc());
2135 }
2136 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2137 if (CE->getNumArgs() == 2) {
2138 auto Op = CE->getOperator();
2139 switch (Op) {
2140 case OO_Greater:
2141 case OO_GreaterEqual:
2142 case OO_Less:
2143 case OO_LessEqual:
2144 if (GetInitVarDecl(CE->getArg(0)) == Var)
2145 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
2146 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2147 CE->getOperatorLoc());
2148 if (GetInitVarDecl(CE->getArg(1)) == Var)
2149 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
2150 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2151 CE->getOperatorLoc());
2152 break;
2153 default:
2154 break;
2155 }
2156 }
2157 }
2158 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
2159 << S->getSourceRange() << Var;
2160 return true;
2161}
2162
2163bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
2164 // RHS of canonical loop form increment can be:
2165 // var + incr
2166 // incr + var
2167 // var - incr
2168 //
2169 RHS = RHS->IgnoreParenImpCasts();
2170 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
2171 if (BO->isAdditiveOp()) {
2172 bool IsAdd = BO->getOpcode() == BO_Add;
2173 if (GetInitVarDecl(BO->getLHS()) == Var)
2174 return SetStep(BO->getRHS(), !IsAdd);
2175 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
2176 return SetStep(BO->getLHS(), false);
2177 }
2178 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
2179 bool IsAdd = CE->getOperator() == OO_Plus;
2180 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
2181 if (GetInitVarDecl(CE->getArg(0)) == Var)
2182 return SetStep(CE->getArg(1), !IsAdd);
2183 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
2184 return SetStep(CE->getArg(0), false);
2185 }
2186 }
2187 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2188 << RHS->getSourceRange() << Var;
2189 return true;
2190}
2191
2192bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
2193 // Check incr-expr for canonical loop form and return true if it
2194 // does not conform.
2195 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2196 // ++var
2197 // var++
2198 // --var
2199 // var--
2200 // var += incr
2201 // var -= incr
2202 // var = var + incr
2203 // var = incr + var
2204 // var = var - incr
2205 //
2206 if (!S) {
2207 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
2208 return true;
2209 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002210 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002211 S = S->IgnoreParens();
2212 if (auto UO = dyn_cast<UnaryOperator>(S)) {
2213 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
2214 return SetStep(
2215 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
2216 (UO->isDecrementOp() ? -1 : 1)).get(),
2217 false);
2218 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
2219 switch (BO->getOpcode()) {
2220 case BO_AddAssign:
2221 case BO_SubAssign:
2222 if (GetInitVarDecl(BO->getLHS()) == Var)
2223 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
2224 break;
2225 case BO_Assign:
2226 if (GetInitVarDecl(BO->getLHS()) == Var)
2227 return CheckIncRHS(BO->getRHS());
2228 break;
2229 default:
2230 break;
2231 }
2232 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2233 switch (CE->getOperator()) {
2234 case OO_PlusPlus:
2235 case OO_MinusMinus:
2236 if (GetInitVarDecl(CE->getArg(0)) == Var)
2237 return SetStep(
2238 SemaRef.ActOnIntegerConstant(
2239 CE->getLocStart(),
2240 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
2241 false);
2242 break;
2243 case OO_PlusEqual:
2244 case OO_MinusEqual:
2245 if (GetInitVarDecl(CE->getArg(0)) == Var)
2246 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
2247 break;
2248 case OO_Equal:
2249 if (GetInitVarDecl(CE->getArg(0)) == Var)
2250 return CheckIncRHS(CE->getArg(1));
2251 break;
2252 default:
2253 break;
2254 }
2255 }
2256 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2257 << S->getSourceRange() << Var;
2258 return true;
2259}
Alexander Musmana5f070a2014-10-01 06:03:56 +00002260
2261/// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00002262Expr *
2263OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S,
2264 const bool LimitedType) const {
Alexander Musmana5f070a2014-10-01 06:03:56 +00002265 ExprResult Diff;
2266 if (Var->getType()->isIntegerType() || Var->getType()->isPointerType() ||
2267 SemaRef.getLangOpts().CPlusPlus) {
2268 // Upper - Lower
2269 Expr *Upper = TestIsLessOp ? UB : LB;
2270 Expr *Lower = TestIsLessOp ? LB : UB;
2271
2272 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
2273
2274 if (!Diff.isUsable() && Var->getType()->getAsCXXRecordDecl()) {
2275 // BuildBinOp already emitted error, this one is to point user to upper
2276 // and lower bound, and to tell what is passed to 'operator-'.
2277 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
2278 << Upper->getSourceRange() << Lower->getSourceRange();
2279 return nullptr;
2280 }
2281 }
2282
2283 if (!Diff.isUsable())
2284 return nullptr;
2285
2286 // Upper - Lower [- 1]
2287 if (TestIsStrictOp)
2288 Diff = SemaRef.BuildBinOp(
2289 S, DefaultLoc, BO_Sub, Diff.get(),
2290 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
2291 if (!Diff.isUsable())
2292 return nullptr;
2293
2294 // Upper - Lower [- 1] + Step
2295 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(),
2296 Step->IgnoreImplicit());
2297 if (!Diff.isUsable())
2298 return nullptr;
2299
2300 // Parentheses (for dumping/debugging purposes only).
2301 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
2302 if (!Diff.isUsable())
2303 return nullptr;
2304
2305 // (Upper - Lower [- 1] + Step) / Step
2306 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(),
2307 Step->IgnoreImplicit());
2308 if (!Diff.isUsable())
2309 return nullptr;
2310
Alexander Musman174b3ca2014-10-06 11:16:29 +00002311 // OpenMP runtime requires 32-bit or 64-bit loop variables.
2312 if (LimitedType) {
2313 auto &C = SemaRef.Context;
2314 QualType Type = Diff.get()->getType();
2315 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
2316 if (NewSize != C.getTypeSize(Type)) {
2317 if (NewSize < C.getTypeSize(Type)) {
2318 assert(NewSize == 64 && "incorrect loop var size");
2319 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
2320 << InitSrcRange << ConditionSrcRange;
2321 }
2322 QualType NewType = C.getIntTypeForBitwidth(
2323 NewSize, Type->hasSignedIntegerRepresentation());
2324 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
2325 Sema::AA_Converting, true);
2326 if (!Diff.isUsable())
2327 return nullptr;
2328 }
2329 }
2330
Alexander Musmana5f070a2014-10-01 06:03:56 +00002331 return Diff.get();
2332}
2333
2334/// \brief Build reference expression to the counter be used for codegen.
2335Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const {
2336 return DeclRefExpr::Create(SemaRef.Context, NestedNameSpecifierLoc(),
2337 GetIncrementSrcRange().getBegin(), Var, false,
2338 DefaultLoc, Var->getType(), VK_LValue);
2339}
2340
2341/// \brief Build initization of the counter be used for codegen.
2342Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
2343
2344/// \brief Build step of the counter be used for codegen.
2345Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
2346
2347/// \brief Iteration space of a single for loop.
2348struct LoopIterationSpace {
2349 /// \brief This expression calculates the number of iterations in the loop.
2350 /// It is always possible to calculate it before starting the loop.
2351 Expr *NumIterations;
2352 /// \brief The loop counter variable.
2353 Expr *CounterVar;
2354 /// \brief This is initializer for the initial value of #CounterVar.
2355 Expr *CounterInit;
2356 /// \brief This is step for the #CounterVar used to generate its update:
2357 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
2358 Expr *CounterStep;
2359 /// \brief Should step be subtracted?
2360 bool Subtract;
2361 /// \brief Source range of the loop init.
2362 SourceRange InitSrcRange;
2363 /// \brief Source range of the loop condition.
2364 SourceRange CondSrcRange;
2365 /// \brief Source range of the loop increment.
2366 SourceRange IncSrcRange;
2367};
2368
Alexey Bataev23b69422014-06-18 07:08:49 +00002369} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002370
2371/// \brief Called on a for stmt to check and extract its iteration space
2372/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00002373static bool CheckOpenMPIterationSpace(
2374 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
2375 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
2376 Expr *NestedLoopCountExpr,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002377 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
2378 LoopIterationSpace &ResultIterSpace) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002379 // OpenMP [2.6, Canonical Loop Form]
2380 // for (init-expr; test-expr; incr-expr) structured-block
2381 auto For = dyn_cast_or_null<ForStmt>(S);
2382 if (!For) {
2383 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002384 << (NestedLoopCountExpr != nullptr) << getOpenMPDirectiveName(DKind)
2385 << NestedLoopCount << (CurrentNestedLoopCount > 0)
2386 << CurrentNestedLoopCount;
2387 if (NestedLoopCount > 1)
2388 SemaRef.Diag(NestedLoopCountExpr->getExprLoc(),
2389 diag::note_omp_collapse_expr)
2390 << NestedLoopCountExpr->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002391 return true;
2392 }
2393 assert(For->getBody());
2394
2395 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
2396
2397 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002398 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002399 if (ISC.CheckInit(Init)) {
2400 return true;
2401 }
2402
2403 bool HasErrors = false;
2404
2405 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002406 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002407
2408 // OpenMP [2.6, Canonical Loop Form]
2409 // Var is one of the following:
2410 // A variable of signed or unsigned integer type.
2411 // For C++, a variable of a random access iterator type.
2412 // For C, a variable of a pointer type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002413 auto VarType = Var->getType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002414 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
2415 !VarType->isPointerType() &&
2416 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
2417 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
2418 << SemaRef.getLangOpts().CPlusPlus;
2419 HasErrors = true;
2420 }
2421
Alexey Bataev4acb8592014-07-07 13:01:15 +00002422 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
2423 // Construct
2424 // The loop iteration variable(s) in the associated for-loop(s) of a for or
2425 // parallel for construct is (are) private.
2426 // The loop iteration variable in the associated for-loop of a simd construct
2427 // with just one associated for-loop is linear with a constant-linear-step
2428 // that is the increment of the associated for-loop.
2429 // Exclude loop var from the list of variables with implicitly defined data
2430 // sharing attributes.
Benjamin Kramerad8e0792014-10-10 15:32:48 +00002431 VarsWithImplicitDSA.erase(Var);
Alexey Bataev4acb8592014-07-07 13:01:15 +00002432
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002433 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
2434 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00002435 // The loop iteration variable in the associated for-loop of a simd construct
2436 // with just one associated for-loop may be listed in a linear clause with a
2437 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002438 // The loop iteration variable(s) in the associated for-loop(s) of a for or
2439 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002440 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002441 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
2442 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
2443 // declared in the loop and it is predetermined as a private.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002444 auto PredeterminedCKind =
2445 isOpenMPSimdDirective(DKind)
2446 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
2447 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002448 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00002449 DVar.CKind != PredeterminedCKind) ||
Alexander Musmanf82886e2014-09-18 05:12:34 +00002450 (isOpenMPWorksharingDirective(DKind) && !isOpenMPSimdDirective(DKind) &&
2451 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private &&
2452 DVar.CKind != OMPC_lastprivate)) &&
Alexander Musman1bb328c2014-06-04 13:06:39 +00002453 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002454 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00002455 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
2456 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002457 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002458 HasErrors = true;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002459 } else if (LoopVarRefExpr != nullptr) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00002460 // Make the loop iteration variable private (for worksharing constructs),
2461 // linear (for simd directives with the only one associated loop) or
2462 // lastprivate (for simd directives with several collapsed loops).
Alexey Bataev9aba41c2014-11-14 04:08:45 +00002463 // FIXME: the next check and error message must be removed once the
2464 // capturing of global variables in loops is fixed.
2465 if (DVar.CKind == OMPC_unknown)
2466 DVar = DSA.hasDSA(Var, isOpenMPPrivate, MatchesAlways(),
2467 /*FromParent=*/false);
2468 if (!Var->hasLocalStorage() && DVar.CKind == OMPC_unknown) {
2469 SemaRef.Diag(Init->getLocStart(), diag::err_omp_global_loop_var_dsa)
2470 << getOpenMPClauseName(PredeterminedCKind)
2471 << getOpenMPDirectiveName(DKind);
2472 HasErrors = true;
2473 } else
2474 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002475 }
2476
Alexey Bataev7ff55242014-06-19 09:13:45 +00002477 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00002478
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002479 // Check test-expr.
2480 HasErrors |= ISC.CheckCond(For->getCond());
2481
2482 // Check incr-expr.
2483 HasErrors |= ISC.CheckInc(For->getInc());
2484
Alexander Musmana5f070a2014-10-01 06:03:56 +00002485 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002486 return HasErrors;
2487
Alexander Musmana5f070a2014-10-01 06:03:56 +00002488 // Build the loop's iteration space representation.
Alexander Musman174b3ca2014-10-06 11:16:29 +00002489 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
2490 DSA.getCurScope(), /* LimitedType */ isOpenMPWorksharingDirective(DKind));
Alexander Musmana5f070a2014-10-01 06:03:56 +00002491 ResultIterSpace.CounterVar = ISC.BuildCounterVar();
2492 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
2493 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
2494 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
2495 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
2496 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
2497 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
2498
2499 HasErrors |= (ResultIterSpace.NumIterations == nullptr ||
2500 ResultIterSpace.CounterVar == nullptr ||
2501 ResultIterSpace.CounterInit == nullptr ||
2502 ResultIterSpace.CounterStep == nullptr);
2503
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002504 return HasErrors;
2505}
2506
Alexander Musmana5f070a2014-10-01 06:03:56 +00002507/// \brief Build a variable declaration for OpenMP loop iteration variable.
2508static VarDecl *BuildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
2509 StringRef Name) {
2510 DeclContext *DC = SemaRef.CurContext;
2511 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
2512 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
2513 VarDecl *Decl =
2514 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
2515 Decl->setImplicit();
2516 return Decl;
2517}
2518
2519/// \brief Build 'VarRef = Start + Iter * Step'.
2520static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S,
2521 SourceLocation Loc, ExprResult VarRef,
2522 ExprResult Start, ExprResult Iter,
2523 ExprResult Step, bool Subtract) {
2524 // Add parentheses (for debugging purposes only).
2525 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
2526 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
2527 !Step.isUsable())
2528 return ExprError();
2529
2530 ExprResult Update = SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(),
2531 Step.get()->IgnoreImplicit());
2532 if (!Update.isUsable())
2533 return ExprError();
2534
2535 // Build 'VarRef = Start + Iter * Step'.
2536 Update = SemaRef.BuildBinOp(S, Loc, (Subtract ? BO_Sub : BO_Add),
2537 Start.get()->IgnoreImplicit(), Update.get());
2538 if (!Update.isUsable())
2539 return ExprError();
2540
2541 Update = SemaRef.PerformImplicitConversion(
2542 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
2543 if (!Update.isUsable())
2544 return ExprError();
2545
2546 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
2547 return Update;
2548}
2549
2550/// \brief Convert integer expression \a E to make it have at least \a Bits
2551/// bits.
2552static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
2553 Sema &SemaRef) {
2554 if (E == nullptr)
2555 return ExprError();
2556 auto &C = SemaRef.Context;
2557 QualType OldType = E->getType();
2558 unsigned HasBits = C.getTypeSize(OldType);
2559 if (HasBits >= Bits)
2560 return ExprResult(E);
2561 // OK to convert to signed, because new type has more bits than old.
2562 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
2563 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
2564 true);
2565}
2566
2567/// \brief Check if the given expression \a E is a constant integer that fits
2568/// into \a Bits bits.
2569static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
2570 if (E == nullptr)
2571 return false;
2572 llvm::APSInt Result;
2573 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
2574 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
2575 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002576}
2577
2578/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00002579/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
2580/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002581static unsigned
2582CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *NestedLoopCountExpr,
2583 Stmt *AStmt, Sema &SemaRef, DSAStackTy &DSA,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002584 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00002585 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002586 unsigned NestedLoopCount = 1;
2587 if (NestedLoopCountExpr) {
2588 // Found 'collapse' clause - calculate collapse number.
2589 llvm::APSInt Result;
2590 if (NestedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
2591 NestedLoopCount = Result.getLimitedValue();
2592 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002593 // This is helper routine for loop directives (e.g., 'for', 'simd',
2594 // 'for simd', etc.).
Alexander Musmana5f070a2014-10-01 06:03:56 +00002595 SmallVector<LoopIterationSpace, 4> IterSpaces;
2596 IterSpaces.resize(NestedLoopCount);
2597 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002598 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002599 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev4acb8592014-07-07 13:01:15 +00002600 NestedLoopCount, NestedLoopCountExpr,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002601 VarsWithImplicitDSA, IterSpaces[Cnt]))
Alexey Bataevabfc0692014-06-25 06:52:00 +00002602 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002603 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00002604 // OpenMP [2.8.1, simd construct, Restrictions]
2605 // All loops associated with the construct must be perfectly nested; that
2606 // is, there must be no intervening code nor any OpenMP directive between
2607 // any two loops.
2608 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002609 }
2610
Alexander Musmana5f070a2014-10-01 06:03:56 +00002611 Built.clear(/* size */ NestedLoopCount);
2612
2613 if (SemaRef.CurContext->isDependentContext())
2614 return NestedLoopCount;
2615
2616 // An example of what is generated for the following code:
2617 //
2618 // #pragma omp simd collapse(2)
2619 // for (i = 0; i < NI; ++i)
2620 // for (j = J0; j < NJ; j+=2) {
2621 // <loop body>
2622 // }
2623 //
2624 // We generate the code below.
2625 // Note: the loop body may be outlined in CodeGen.
2626 // Note: some counters may be C++ classes, operator- is used to find number of
2627 // iterations and operator+= to calculate counter value.
2628 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
2629 // or i64 is currently supported).
2630 //
2631 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
2632 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
2633 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
2634 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
2635 // // similar updates for vars in clauses (e.g. 'linear')
2636 // <loop body (using local i and j)>
2637 // }
2638 // i = NI; // assign final values of counters
2639 // j = NJ;
2640 //
2641
2642 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
2643 // the iteration counts of the collapsed for loops.
2644 auto N0 = IterSpaces[0].NumIterations;
2645 ExprResult LastIteration32 = WidenIterationCount(32 /* Bits */, N0, SemaRef);
2646 ExprResult LastIteration64 = WidenIterationCount(64 /* Bits */, N0, SemaRef);
2647
2648 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
2649 return NestedLoopCount;
2650
2651 auto &C = SemaRef.Context;
2652 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
2653
2654 Scope *CurScope = DSA.getCurScope();
2655 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
2656 auto N = IterSpaces[Cnt].NumIterations;
2657 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
2658 if (LastIteration32.isUsable())
2659 LastIteration32 = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_Mul,
2660 LastIteration32.get(), N);
2661 if (LastIteration64.isUsable())
2662 LastIteration64 = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_Mul,
2663 LastIteration64.get(), N);
2664 }
2665
2666 // Choose either the 32-bit or 64-bit version.
2667 ExprResult LastIteration = LastIteration64;
2668 if (LastIteration32.isUsable() &&
2669 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
2670 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
2671 FitsInto(
2672 32 /* Bits */,
2673 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
2674 LastIteration64.get(), SemaRef)))
2675 LastIteration = LastIteration32;
2676
2677 if (!LastIteration.isUsable())
2678 return 0;
2679
2680 // Save the number of iterations.
2681 ExprResult NumIterations = LastIteration;
2682 {
2683 LastIteration = SemaRef.BuildBinOp(
2684 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
2685 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
2686 if (!LastIteration.isUsable())
2687 return 0;
2688 }
2689
2690 // Calculate the last iteration number beforehand instead of doing this on
2691 // each iteration. Do not do this if the number of iterations may be kfold-ed.
2692 llvm::APSInt Result;
2693 bool IsConstant =
2694 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
2695 ExprResult CalcLastIteration;
2696 if (!IsConstant) {
2697 SourceLocation SaveLoc;
2698 VarDecl *SaveVar =
2699 BuildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(),
2700 ".omp.last.iteration");
2701 ExprResult SaveRef = SemaRef.BuildDeclRefExpr(
2702 SaveVar, LastIteration.get()->getType(), VK_LValue, SaveLoc);
2703 CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign,
2704 SaveRef.get(), LastIteration.get());
2705 LastIteration = SaveRef;
2706
2707 // Prepare SaveRef + 1.
2708 NumIterations = SemaRef.BuildBinOp(
2709 CurScope, SaveLoc, BO_Add, SaveRef.get(),
2710 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
2711 if (!NumIterations.isUsable())
2712 return 0;
2713 }
2714
2715 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
2716
2717 // Precondition tests if there is at least one iteration (LastIteration > 0).
2718 ExprResult PreCond = SemaRef.BuildBinOp(
2719 CurScope, InitLoc, BO_GT, LastIteration.get(),
2720 SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get());
2721
Alexander Musmanc6388682014-12-15 07:07:06 +00002722 QualType VType = LastIteration.get()->getType();
2723 // Build variables passed into runtime, nesessary for worksharing directives.
2724 ExprResult LB, UB, IL, ST, EUB;
2725 if (isOpenMPWorksharingDirective(DKind)) {
2726 // Lower bound variable, initialized with zero.
2727 VarDecl *LBDecl = BuildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
2728 LB = SemaRef.BuildDeclRefExpr(LBDecl, VType, VK_LValue, InitLoc);
2729 SemaRef.AddInitializerToDecl(
2730 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
2731 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
2732
2733 // Upper bound variable, initialized with last iteration number.
2734 VarDecl *UBDecl = BuildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
2735 UB = SemaRef.BuildDeclRefExpr(UBDecl, VType, VK_LValue, InitLoc);
2736 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
2737 /*DirectInit*/ false,
2738 /*TypeMayContainAuto*/ false);
2739
2740 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
2741 // This will be used to implement clause 'lastprivate'.
2742 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
2743 VarDecl *ILDecl = BuildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
2744 IL = SemaRef.BuildDeclRefExpr(ILDecl, Int32Ty, VK_LValue, InitLoc);
2745 SemaRef.AddInitializerToDecl(
2746 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
2747 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
2748
2749 // Stride variable returned by runtime (we initialize it to 1 by default).
2750 VarDecl *STDecl = BuildVarDecl(SemaRef, InitLoc, VType, ".omp.stride");
2751 ST = SemaRef.BuildDeclRefExpr(STDecl, VType, VK_LValue, InitLoc);
2752 SemaRef.AddInitializerToDecl(
2753 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
2754 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
2755
2756 // Build expression: UB = min(UB, LastIteration)
2757 // It is nesessary for CodeGen of directives with static scheduling.
2758 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
2759 UB.get(), LastIteration.get());
2760 ExprResult CondOp = SemaRef.ActOnConditionalOp(
2761 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
2762 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
2763 CondOp.get());
2764 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
2765 }
2766
2767 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00002768 ExprResult IV;
2769 ExprResult Init;
2770 {
Alexander Musmanc6388682014-12-15 07:07:06 +00002771 VarDecl *IVDecl = BuildVarDecl(SemaRef, InitLoc, VType, ".omp.iv");
2772 IV = SemaRef.BuildDeclRefExpr(IVDecl, VType, VK_LValue, InitLoc);
2773 Expr *RHS = isOpenMPWorksharingDirective(DKind)
2774 ? LB.get()
2775 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
2776 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
2777 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00002778 }
2779
Alexander Musmanc6388682014-12-15 07:07:06 +00002780 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00002781 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00002782 ExprResult Cond =
2783 isOpenMPWorksharingDirective(DKind)
2784 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
2785 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
2786 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00002787 // Loop condition with 1 iteration separated (IV < LastIteration)
2788 ExprResult SeparatedCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT,
2789 IV.get(), LastIteration.get());
2790
2791 // Loop increment (IV = IV + 1)
2792 SourceLocation IncLoc;
2793 ExprResult Inc =
2794 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
2795 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
2796 if (!Inc.isUsable())
2797 return 0;
2798 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00002799 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
2800 if (!Inc.isUsable())
2801 return 0;
2802
2803 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
2804 // Used for directives with static scheduling.
2805 ExprResult NextLB, NextUB;
2806 if (isOpenMPWorksharingDirective(DKind)) {
2807 // LB + ST
2808 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
2809 if (!NextLB.isUsable())
2810 return 0;
2811 // LB = LB + ST
2812 NextLB =
2813 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
2814 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
2815 if (!NextLB.isUsable())
2816 return 0;
2817 // UB + ST
2818 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
2819 if (!NextUB.isUsable())
2820 return 0;
2821 // UB = UB + ST
2822 NextUB =
2823 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
2824 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
2825 if (!NextUB.isUsable())
2826 return 0;
2827 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002828
2829 // Build updates and final values of the loop counters.
2830 bool HasErrors = false;
2831 Built.Counters.resize(NestedLoopCount);
2832 Built.Updates.resize(NestedLoopCount);
2833 Built.Finals.resize(NestedLoopCount);
2834 {
2835 ExprResult Div;
2836 // Go from inner nested loop to outer.
2837 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
2838 LoopIterationSpace &IS = IterSpaces[Cnt];
2839 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
2840 // Build: Iter = (IV / Div) % IS.NumIters
2841 // where Div is product of previous iterations' IS.NumIters.
2842 ExprResult Iter;
2843 if (Div.isUsable()) {
2844 Iter =
2845 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
2846 } else {
2847 Iter = IV;
2848 assert((Cnt == (int)NestedLoopCount - 1) &&
2849 "unusable div expected on first iteration only");
2850 }
2851
2852 if (Cnt != 0 && Iter.isUsable())
2853 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
2854 IS.NumIterations);
2855 if (!Iter.isUsable()) {
2856 HasErrors = true;
2857 break;
2858 }
2859
2860 // Build update: IS.CounterVar = IS.Start + Iter * IS.Step
2861 ExprResult Update =
2862 BuildCounterUpdate(SemaRef, CurScope, UpdLoc, IS.CounterVar,
2863 IS.CounterInit, Iter, IS.CounterStep, IS.Subtract);
2864 if (!Update.isUsable()) {
2865 HasErrors = true;
2866 break;
2867 }
2868
2869 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
2870 ExprResult Final = BuildCounterUpdate(
2871 SemaRef, CurScope, UpdLoc, IS.CounterVar, IS.CounterInit,
2872 IS.NumIterations, IS.CounterStep, IS.Subtract);
2873 if (!Final.isUsable()) {
2874 HasErrors = true;
2875 break;
2876 }
2877
2878 // Build Div for the next iteration: Div <- Div * IS.NumIters
2879 if (Cnt != 0) {
2880 if (Div.isUnset())
2881 Div = IS.NumIterations;
2882 else
2883 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
2884 IS.NumIterations);
2885
2886 // Add parentheses (for debugging purposes only).
2887 if (Div.isUsable())
2888 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
2889 if (!Div.isUsable()) {
2890 HasErrors = true;
2891 break;
2892 }
2893 }
2894 if (!Update.isUsable() || !Final.isUsable()) {
2895 HasErrors = true;
2896 break;
2897 }
2898 // Save results
2899 Built.Counters[Cnt] = IS.CounterVar;
2900 Built.Updates[Cnt] = Update.get();
2901 Built.Finals[Cnt] = Final.get();
2902 }
2903 }
2904
2905 if (HasErrors)
2906 return 0;
2907
2908 // Save results
2909 Built.IterationVarRef = IV.get();
2910 Built.LastIteration = LastIteration.get();
2911 Built.CalcLastIteration = CalcLastIteration.get();
2912 Built.PreCond = PreCond.get();
2913 Built.Cond = Cond.get();
2914 Built.SeparatedCond = SeparatedCond.get();
2915 Built.Init = Init.get();
2916 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00002917 Built.LB = LB.get();
2918 Built.UB = UB.get();
2919 Built.IL = IL.get();
2920 Built.ST = ST.get();
2921 Built.EUB = EUB.get();
2922 Built.NLB = NextLB.get();
2923 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00002924
Alexey Bataevabfc0692014-06-25 06:52:00 +00002925 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002926}
2927
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002928static Expr *GetCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002929 auto CollapseFilter = [](const OMPClause *C) -> bool {
2930 return C->getClauseKind() == OMPC_collapse;
2931 };
2932 OMPExecutableDirective::filtered_clause_iterator<decltype(CollapseFilter)> I(
2933 Clauses, CollapseFilter);
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002934 if (I)
2935 return cast<OMPCollapseClause>(*I)->getNumForLoops();
2936 return nullptr;
2937}
2938
Alexey Bataev4acb8592014-07-07 13:01:15 +00002939StmtResult Sema::ActOnOpenMPSimdDirective(
2940 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2941 SourceLocation EndLoc,
2942 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmanc6388682014-12-15 07:07:06 +00002943 OMPLoopDirective::HelperExprs B;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002944 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002945 unsigned NestedLoopCount =
2946 CheckOpenMPLoop(OMPD_simd, GetCollapseNumberExpr(Clauses), AStmt, *this,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002947 *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00002948 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002949 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002950
Alexander Musmana5f070a2014-10-01 06:03:56 +00002951 assert((CurContext->isDependentContext() || B.builtAll()) &&
2952 "omp simd loop exprs were not built");
2953
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002954 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00002955 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
2956 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002957}
2958
Alexey Bataev4acb8592014-07-07 13:01:15 +00002959StmtResult Sema::ActOnOpenMPForDirective(
2960 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2961 SourceLocation EndLoc,
2962 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmanc6388682014-12-15 07:07:06 +00002963 OMPLoopDirective::HelperExprs B;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002964 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002965 unsigned NestedLoopCount =
2966 CheckOpenMPLoop(OMPD_for, GetCollapseNumberExpr(Clauses), AStmt, *this,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002967 *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00002968 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00002969 return StmtError();
2970
Alexander Musmana5f070a2014-10-01 06:03:56 +00002971 assert((CurContext->isDependentContext() || B.builtAll()) &&
2972 "omp for loop exprs were not built");
2973
Alexey Bataevf29276e2014-06-18 04:14:57 +00002974 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00002975 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
2976 Clauses, AStmt, B);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002977}
2978
Alexander Musmanf82886e2014-09-18 05:12:34 +00002979StmtResult Sema::ActOnOpenMPForSimdDirective(
2980 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2981 SourceLocation EndLoc,
2982 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmanc6388682014-12-15 07:07:06 +00002983 OMPLoopDirective::HelperExprs B;
Alexander Musmanf82886e2014-09-18 05:12:34 +00002984 // In presence of clause 'collapse', it will define the nested loops number.
2985 unsigned NestedLoopCount =
2986 CheckOpenMPLoop(OMPD_for_simd, GetCollapseNumberExpr(Clauses), AStmt,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002987 *this, *DSAStack, VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00002988 if (NestedLoopCount == 0)
2989 return StmtError();
2990
Alexander Musmanc6388682014-12-15 07:07:06 +00002991 assert((CurContext->isDependentContext() || B.builtAll()) &&
2992 "omp for simd loop exprs were not built");
2993
Alexander Musmanf82886e2014-09-18 05:12:34 +00002994 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00002995 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
2996 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00002997}
2998
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002999StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
3000 Stmt *AStmt,
3001 SourceLocation StartLoc,
3002 SourceLocation EndLoc) {
3003 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3004 auto BaseStmt = AStmt;
3005 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
3006 BaseStmt = CS->getCapturedStmt();
3007 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
3008 auto S = C->children();
3009 if (!S)
3010 return StmtError();
3011 // All associated statements must be '#pragma omp section' except for
3012 // the first one.
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003013 for (++S; S; ++S) {
3014 auto SectionStmt = *S;
3015 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
3016 if (SectionStmt)
3017 Diag(SectionStmt->getLocStart(),
3018 diag::err_omp_sections_substmt_not_section);
3019 return StmtError();
3020 }
3021 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003022 } else {
3023 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
3024 return StmtError();
3025 }
3026
3027 getCurFunction()->setHasBranchProtectedScope();
3028
3029 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses,
3030 AStmt);
3031}
3032
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003033StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
3034 SourceLocation StartLoc,
3035 SourceLocation EndLoc) {
3036 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3037
3038 getCurFunction()->setHasBranchProtectedScope();
3039
3040 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt);
3041}
3042
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003043StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
3044 Stmt *AStmt,
3045 SourceLocation StartLoc,
3046 SourceLocation EndLoc) {
Alexey Bataev74a05c92014-07-15 02:55:09 +00003047 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3048
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003049 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00003050
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003051 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3052}
3053
Alexander Musman80c22892014-07-17 08:54:58 +00003054StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
3055 SourceLocation StartLoc,
3056 SourceLocation EndLoc) {
3057 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3058
3059 getCurFunction()->setHasBranchProtectedScope();
3060
3061 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
3062}
3063
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003064StmtResult
3065Sema::ActOnOpenMPCriticalDirective(const DeclarationNameInfo &DirName,
3066 Stmt *AStmt, SourceLocation StartLoc,
3067 SourceLocation EndLoc) {
3068 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3069
3070 getCurFunction()->setHasBranchProtectedScope();
3071
3072 return OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
3073 AStmt);
3074}
3075
Alexey Bataev4acb8592014-07-07 13:01:15 +00003076StmtResult Sema::ActOnOpenMPParallelForDirective(
3077 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3078 SourceLocation EndLoc,
3079 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
3080 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3081 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3082 // 1.2.2 OpenMP Language Terminology
3083 // Structured block - An executable statement with a single entry at the
3084 // top and a single exit at the bottom.
3085 // The point of exit cannot be a branch out of the structured block.
3086 // longjmp() and throw() must not violate the entry/exit criteria.
3087 CS->getCapturedDecl()->setNothrow();
3088
Alexander Musmanc6388682014-12-15 07:07:06 +00003089 OMPLoopDirective::HelperExprs B;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003090 // In presence of clause 'collapse', it will define the nested loops number.
3091 unsigned NestedLoopCount =
3092 CheckOpenMPLoop(OMPD_parallel_for, GetCollapseNumberExpr(Clauses), AStmt,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003093 *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003094 if (NestedLoopCount == 0)
3095 return StmtError();
3096
Alexander Musmana5f070a2014-10-01 06:03:56 +00003097 assert((CurContext->isDependentContext() || B.builtAll()) &&
3098 "omp parallel for loop exprs were not built");
3099
Alexey Bataev4acb8592014-07-07 13:01:15 +00003100 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003101 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
3102 NestedLoopCount, Clauses, AStmt, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003103}
3104
Alexander Musmane4e893b2014-09-23 09:33:00 +00003105StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
3106 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3107 SourceLocation EndLoc,
3108 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
3109 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3110 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3111 // 1.2.2 OpenMP Language Terminology
3112 // Structured block - An executable statement with a single entry at the
3113 // top and a single exit at the bottom.
3114 // The point of exit cannot be a branch out of the structured block.
3115 // longjmp() and throw() must not violate the entry/exit criteria.
3116 CS->getCapturedDecl()->setNothrow();
3117
Alexander Musmanc6388682014-12-15 07:07:06 +00003118 OMPLoopDirective::HelperExprs B;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003119 // In presence of clause 'collapse', it will define the nested loops number.
3120 unsigned NestedLoopCount =
3121 CheckOpenMPLoop(OMPD_parallel_for_simd, GetCollapseNumberExpr(Clauses),
Alexander Musmana5f070a2014-10-01 06:03:56 +00003122 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003123 if (NestedLoopCount == 0)
3124 return StmtError();
3125
3126 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003127 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00003128 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003129}
3130
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003131StmtResult
3132Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
3133 Stmt *AStmt, SourceLocation StartLoc,
3134 SourceLocation EndLoc) {
3135 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3136 auto BaseStmt = AStmt;
3137 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
3138 BaseStmt = CS->getCapturedStmt();
3139 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
3140 auto S = C->children();
3141 if (!S)
3142 return StmtError();
3143 // All associated statements must be '#pragma omp section' except for
3144 // the first one.
3145 for (++S; S; ++S) {
3146 auto SectionStmt = *S;
3147 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
3148 if (SectionStmt)
3149 Diag(SectionStmt->getLocStart(),
3150 diag::err_omp_parallel_sections_substmt_not_section);
3151 return StmtError();
3152 }
3153 }
3154 } else {
3155 Diag(AStmt->getLocStart(),
3156 diag::err_omp_parallel_sections_not_compound_stmt);
3157 return StmtError();
3158 }
3159
3160 getCurFunction()->setHasBranchProtectedScope();
3161
3162 return OMPParallelSectionsDirective::Create(Context, StartLoc, EndLoc,
3163 Clauses, AStmt);
3164}
3165
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003166StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
3167 Stmt *AStmt, SourceLocation StartLoc,
3168 SourceLocation EndLoc) {
3169 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3170 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3171 // 1.2.2 OpenMP Language Terminology
3172 // Structured block - An executable statement with a single entry at the
3173 // top and a single exit at the bottom.
3174 // The point of exit cannot be a branch out of the structured block.
3175 // longjmp() and throw() must not violate the entry/exit criteria.
3176 CS->getCapturedDecl()->setNothrow();
3177
3178 getCurFunction()->setHasBranchProtectedScope();
3179
3180 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3181}
3182
Alexey Bataev68446b72014-07-18 07:47:19 +00003183StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
3184 SourceLocation EndLoc) {
3185 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
3186}
3187
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003188StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
3189 SourceLocation EndLoc) {
3190 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
3191}
3192
Alexey Bataev2df347a2014-07-18 10:17:07 +00003193StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
3194 SourceLocation EndLoc) {
3195 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
3196}
3197
Alexey Bataev6125da92014-07-21 11:26:11 +00003198StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
3199 SourceLocation StartLoc,
3200 SourceLocation EndLoc) {
3201 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
3202 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
3203}
3204
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003205StmtResult Sema::ActOnOpenMPOrderedDirective(Stmt *AStmt,
3206 SourceLocation StartLoc,
3207 SourceLocation EndLoc) {
3208 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3209
3210 getCurFunction()->setHasBranchProtectedScope();
3211
3212 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, AStmt);
3213}
3214
Alexey Bataev0162e452014-07-22 10:10:35 +00003215StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
3216 Stmt *AStmt,
3217 SourceLocation StartLoc,
3218 SourceLocation EndLoc) {
3219 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003220 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00003221 // 1.2.2 OpenMP Language Terminology
3222 // Structured block - An executable statement with a single entry at the
3223 // top and a single exit at the bottom.
3224 // The point of exit cannot be a branch out of the structured block.
3225 // longjmp() and throw() must not violate the entry/exit criteria.
3226 // TODO further analysis of associated statements and clauses.
Alexey Bataevdea47612014-07-23 07:46:59 +00003227 OpenMPClauseKind AtomicKind = OMPC_unknown;
3228 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003229 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00003230 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00003231 C->getClauseKind() == OMPC_update ||
3232 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00003233 if (AtomicKind != OMPC_unknown) {
3234 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
3235 << SourceRange(C->getLocStart(), C->getLocEnd());
3236 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
3237 << getOpenMPClauseName(AtomicKind);
3238 } else {
3239 AtomicKind = C->getClauseKind();
3240 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003241 }
3242 }
3243 }
Alexey Bataev62cec442014-11-18 10:14:22 +00003244
Alexey Bataev459dec02014-07-24 06:46:57 +00003245 auto Body = CS->getCapturedStmt();
Alexey Bataev62cec442014-11-18 10:14:22 +00003246 Expr *X = nullptr;
3247 Expr *V = nullptr;
3248 Expr *E = nullptr;
3249 // OpenMP [2.12.6, atomic Construct]
3250 // In the next expressions:
3251 // * x and v (as applicable) are both l-value expressions with scalar type.
3252 // * During the execution of an atomic region, multiple syntactic
3253 // occurrences of x must designate the same storage location.
3254 // * Neither of v and expr (as applicable) may access the storage location
3255 // designated by x.
3256 // * Neither of x and expr (as applicable) may access the storage location
3257 // designated by v.
3258 // * expr is an expression with scalar type.
3259 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
3260 // * binop, binop=, ++, and -- are not overloaded operators.
3261 // * The expression x binop expr must be numerically equivalent to x binop
3262 // (expr). This requirement is satisfied if the operators in expr have
3263 // precedence greater than binop, or by using parentheses around expr or
3264 // subexpressions of expr.
3265 // * The expression expr binop x must be numerically equivalent to (expr)
3266 // binop x. This requirement is satisfied if the operators in expr have
3267 // precedence equal to or greater than binop, or by using parentheses around
3268 // expr or subexpressions of expr.
3269 // * For forms that allow multiple occurrences of x, the number of times
3270 // that x is evaluated is unspecified.
Alexey Bataevf33eba62014-11-28 07:21:40 +00003271 enum {
3272 NotAnExpression,
3273 NotAnAssignmentOp,
3274 NotAScalarType,
3275 NotAnLValue,
3276 NoError
3277 } ErrorFound = NoError;
Alexey Bataevdea47612014-07-23 07:46:59 +00003278 if (AtomicKind == OMPC_read) {
Alexey Bataev62cec442014-11-18 10:14:22 +00003279 SourceLocation ErrorLoc, NoteLoc;
3280 SourceRange ErrorRange, NoteRange;
3281 // If clause is read:
3282 // v = x;
3283 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
3284 auto AtomicBinOp =
3285 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
3286 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
3287 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
3288 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
3289 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
3290 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
3291 if (!X->isLValue() || !V->isLValue()) {
3292 auto NotLValueExpr = X->isLValue() ? V : X;
3293 ErrorFound = NotAnLValue;
3294 ErrorLoc = AtomicBinOp->getExprLoc();
3295 ErrorRange = AtomicBinOp->getSourceRange();
3296 NoteLoc = NotLValueExpr->getExprLoc();
3297 NoteRange = NotLValueExpr->getSourceRange();
3298 }
3299 } else if (!X->isInstantiationDependent() ||
3300 !V->isInstantiationDependent()) {
3301 auto NotScalarExpr =
3302 (X->isInstantiationDependent() || X->getType()->isScalarType())
3303 ? V
3304 : X;
3305 ErrorFound = NotAScalarType;
3306 ErrorLoc = AtomicBinOp->getExprLoc();
3307 ErrorRange = AtomicBinOp->getSourceRange();
3308 NoteLoc = NotScalarExpr->getExprLoc();
3309 NoteRange = NotScalarExpr->getSourceRange();
3310 }
3311 } else {
3312 ErrorFound = NotAnAssignmentOp;
3313 ErrorLoc = AtomicBody->getExprLoc();
3314 ErrorRange = AtomicBody->getSourceRange();
3315 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
3316 : AtomicBody->getExprLoc();
3317 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
3318 : AtomicBody->getSourceRange();
3319 }
3320 } else {
3321 ErrorFound = NotAnExpression;
3322 NoteLoc = ErrorLoc = Body->getLocStart();
3323 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00003324 }
Alexey Bataev62cec442014-11-18 10:14:22 +00003325 if (ErrorFound != NoError) {
3326 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
3327 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00003328 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
3329 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00003330 return StmtError();
3331 } else if (CurContext->isDependentContext())
3332 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00003333 } else if (AtomicKind == OMPC_write) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00003334 SourceLocation ErrorLoc, NoteLoc;
3335 SourceRange ErrorRange, NoteRange;
3336 // If clause is write:
3337 // x = expr;
3338 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
3339 auto AtomicBinOp =
3340 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
3341 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
3342 X = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
3343 E = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
3344 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
3345 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
3346 if (!X->isLValue()) {
3347 ErrorFound = NotAnLValue;
3348 ErrorLoc = AtomicBinOp->getExprLoc();
3349 ErrorRange = AtomicBinOp->getSourceRange();
3350 NoteLoc = X->getExprLoc();
3351 NoteRange = X->getSourceRange();
3352 }
3353 } else if (!X->isInstantiationDependent() ||
3354 !E->isInstantiationDependent()) {
3355 auto NotScalarExpr =
3356 (X->isInstantiationDependent() || X->getType()->isScalarType())
3357 ? E
3358 : X;
3359 ErrorFound = NotAScalarType;
3360 ErrorLoc = AtomicBinOp->getExprLoc();
3361 ErrorRange = AtomicBinOp->getSourceRange();
3362 NoteLoc = NotScalarExpr->getExprLoc();
3363 NoteRange = NotScalarExpr->getSourceRange();
3364 }
3365 } else {
3366 ErrorFound = NotAnAssignmentOp;
3367 ErrorLoc = AtomicBody->getExprLoc();
3368 ErrorRange = AtomicBody->getSourceRange();
3369 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
3370 : AtomicBody->getExprLoc();
3371 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
3372 : AtomicBody->getSourceRange();
3373 }
3374 } else {
3375 ErrorFound = NotAnExpression;
3376 NoteLoc = ErrorLoc = Body->getLocStart();
3377 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00003378 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00003379 if (ErrorFound != NoError) {
3380 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
3381 << ErrorRange;
3382 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
3383 << NoteRange;
3384 return StmtError();
3385 } else if (CurContext->isDependentContext())
3386 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00003387 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev459dec02014-07-24 06:46:57 +00003388 if (!isa<Expr>(Body)) {
3389 Diag(Body->getLocStart(),
Alexey Bataev67a4f222014-07-23 10:25:33 +00003390 diag::err_omp_atomic_update_not_expression_statement)
3391 << (AtomicKind == OMPC_update);
3392 return StmtError();
3393 }
Alexey Bataev459dec02014-07-24 06:46:57 +00003394 } else if (AtomicKind == OMPC_capture) {
3395 if (isa<Expr>(Body) && !isa<BinaryOperator>(Body)) {
3396 Diag(Body->getLocStart(),
3397 diag::err_omp_atomic_capture_not_expression_statement);
3398 return StmtError();
3399 } else if (!isa<Expr>(Body) && !isa<CompoundStmt>(Body)) {
3400 Diag(Body->getLocStart(),
3401 diag::err_omp_atomic_capture_not_compound_statement);
3402 return StmtError();
3403 }
Alexey Bataevdea47612014-07-23 07:46:59 +00003404 }
Alexey Bataev0162e452014-07-22 10:10:35 +00003405
3406 getCurFunction()->setHasBranchProtectedScope();
3407
Alexey Bataev62cec442014-11-18 10:14:22 +00003408 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3409 X, V, E);
Alexey Bataev0162e452014-07-22 10:10:35 +00003410}
3411
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003412StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
3413 Stmt *AStmt,
3414 SourceLocation StartLoc,
3415 SourceLocation EndLoc) {
3416 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3417
Alexey Bataev13314bf2014-10-09 04:18:56 +00003418 // OpenMP [2.16, Nesting of Regions]
3419 // If specified, a teams construct must be contained within a target
3420 // construct. That target construct must contain no statements or directives
3421 // outside of the teams construct.
3422 if (DSAStack->hasInnerTeamsRegion()) {
3423 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
3424 bool OMPTeamsFound = true;
3425 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
3426 auto I = CS->body_begin();
3427 while (I != CS->body_end()) {
3428 auto OED = dyn_cast<OMPExecutableDirective>(*I);
3429 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
3430 OMPTeamsFound = false;
3431 break;
3432 }
3433 ++I;
3434 }
3435 assert(I != CS->body_end() && "Not found statement");
3436 S = *I;
3437 }
3438 if (!OMPTeamsFound) {
3439 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
3440 Diag(DSAStack->getInnerTeamsRegionLoc(),
3441 diag::note_omp_nested_teams_construct_here);
3442 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
3443 << isa<OMPExecutableDirective>(S);
3444 return StmtError();
3445 }
3446 }
3447
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003448 getCurFunction()->setHasBranchProtectedScope();
3449
3450 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3451}
3452
Alexey Bataev13314bf2014-10-09 04:18:56 +00003453StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
3454 Stmt *AStmt, SourceLocation StartLoc,
3455 SourceLocation EndLoc) {
3456 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3457 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3458 // 1.2.2 OpenMP Language Terminology
3459 // Structured block - An executable statement with a single entry at the
3460 // top and a single exit at the bottom.
3461 // The point of exit cannot be a branch out of the structured block.
3462 // longjmp() and throw() must not violate the entry/exit criteria.
3463 CS->getCapturedDecl()->setNothrow();
3464
3465 getCurFunction()->setHasBranchProtectedScope();
3466
3467 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3468}
3469
Alexey Bataeved09d242014-05-28 05:53:51 +00003470OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003471 SourceLocation StartLoc,
3472 SourceLocation LParenLoc,
3473 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003474 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003475 switch (Kind) {
3476 case OMPC_if:
3477 Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc);
3478 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00003479 case OMPC_final:
3480 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
3481 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00003482 case OMPC_num_threads:
3483 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
3484 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00003485 case OMPC_safelen:
3486 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
3487 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00003488 case OMPC_collapse:
3489 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
3490 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003491 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003492 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003493 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003494 case OMPC_private:
3495 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00003496 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003497 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00003498 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00003499 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00003500 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003501 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003502 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003503 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00003504 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003505 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003506 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003507 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00003508 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003509 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00003510 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00003511 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00003512 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003513 case OMPC_seq_cst:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003514 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003515 llvm_unreachable("Clause is not allowed.");
3516 }
3517 return Res;
3518}
3519
Alexey Bataeved09d242014-05-28 05:53:51 +00003520OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003521 SourceLocation LParenLoc,
3522 SourceLocation EndLoc) {
3523 Expr *ValExpr = Condition;
3524 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
3525 !Condition->isInstantiationDependent() &&
3526 !Condition->containsUnexpandedParameterPack()) {
3527 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00003528 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003529 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003530 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003531
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003532 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003533 }
3534
3535 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc);
3536}
3537
Alexey Bataev3778b602014-07-17 07:32:53 +00003538OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
3539 SourceLocation StartLoc,
3540 SourceLocation LParenLoc,
3541 SourceLocation EndLoc) {
3542 Expr *ValExpr = Condition;
3543 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
3544 !Condition->isInstantiationDependent() &&
3545 !Condition->containsUnexpandedParameterPack()) {
3546 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
3547 Condition->getExprLoc(), Condition);
3548 if (Val.isInvalid())
3549 return nullptr;
3550
3551 ValExpr = Val.get();
3552 }
3553
3554 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
3555}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003556ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
3557 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00003558 if (!Op)
3559 return ExprError();
3560
3561 class IntConvertDiagnoser : public ICEConvertDiagnoser {
3562 public:
3563 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00003564 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00003565 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
3566 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003567 return S.Diag(Loc, diag::err_omp_not_integral) << T;
3568 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003569 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
3570 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003571 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
3572 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003573 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
3574 QualType T,
3575 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003576 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
3577 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003578 SemaDiagnosticBuilder noteExplicitConv(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 diagnoseAmbiguous(Sema &S, SourceLocation Loc,
3584 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003585 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
3586 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003587 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
3588 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003589 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00003590 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00003591 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003592 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
3593 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003594 llvm_unreachable("conversion functions are permitted");
3595 }
3596 } ConvertDiagnoser;
3597 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
3598}
3599
3600OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
3601 SourceLocation StartLoc,
3602 SourceLocation LParenLoc,
3603 SourceLocation EndLoc) {
3604 Expr *ValExpr = NumThreads;
3605 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
Alexey Bataev568a8332014-03-06 06:15:19 +00003606 !NumThreads->containsUnexpandedParameterPack()) {
3607 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
3608 ExprResult Val =
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003609 PerformOpenMPImplicitIntegerConversion(NumThreadsLoc, NumThreads);
Alexey Bataev568a8332014-03-06 06:15:19 +00003610 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003611 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00003612
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003613 ValExpr = Val.get();
Alexey Bataev568a8332014-03-06 06:15:19 +00003614
3615 // OpenMP [2.5, Restrictions]
3616 // The num_threads expression must evaluate to a positive integer value.
3617 llvm::APSInt Result;
Alexey Bataeved09d242014-05-28 05:53:51 +00003618 if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() &&
3619 !Result.isStrictlyPositive()) {
Alexey Bataev568a8332014-03-06 06:15:19 +00003620 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
3621 << "num_threads" << NumThreads->getSourceRange();
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003622 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00003623 }
3624 }
3625
Alexey Bataeved09d242014-05-28 05:53:51 +00003626 return new (Context)
3627 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00003628}
3629
Alexey Bataev62c87d22014-03-21 04:51:18 +00003630ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
3631 OpenMPClauseKind CKind) {
3632 if (!E)
3633 return ExprError();
3634 if (E->isValueDependent() || E->isTypeDependent() ||
3635 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00003636 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00003637 llvm::APSInt Result;
3638 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
3639 if (ICE.isInvalid())
3640 return ExprError();
3641 if (!Result.isStrictlyPositive()) {
3642 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
3643 << getOpenMPClauseName(CKind) << E->getSourceRange();
3644 return ExprError();
3645 }
Alexander Musman09184fe2014-09-30 05:29:28 +00003646 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
3647 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
3648 << E->getSourceRange();
3649 return ExprError();
3650 }
Alexey Bataev62c87d22014-03-21 04:51:18 +00003651 return ICE;
3652}
3653
3654OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
3655 SourceLocation LParenLoc,
3656 SourceLocation EndLoc) {
3657 // OpenMP [2.8.1, simd construct, Description]
3658 // The parameter of the safelen clause must be a constant
3659 // positive integer expression.
3660 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
3661 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003662 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00003663 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003664 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00003665}
3666
Alexander Musman64d33f12014-06-04 07:53:32 +00003667OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
3668 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00003669 SourceLocation LParenLoc,
3670 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00003671 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00003672 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00003673 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00003674 // The parameter of the collapse clause must be a constant
3675 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00003676 ExprResult NumForLoopsResult =
3677 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
3678 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00003679 return nullptr;
3680 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00003681 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00003682}
3683
Alexey Bataeved09d242014-05-28 05:53:51 +00003684OMPClause *Sema::ActOnOpenMPSimpleClause(
3685 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
3686 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003687 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003688 switch (Kind) {
3689 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00003690 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00003691 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
3692 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003693 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003694 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00003695 Res = ActOnOpenMPProcBindClause(
3696 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
3697 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003698 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003699 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00003700 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00003701 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00003702 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00003703 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003704 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003705 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003706 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00003707 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00003708 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00003709 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00003710 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00003711 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003712 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003713 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003714 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00003715 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003716 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003717 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003718 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00003719 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003720 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00003721 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00003722 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00003723 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003724 case OMPC_seq_cst:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003725 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003726 llvm_unreachable("Clause is not allowed.");
3727 }
3728 return Res;
3729}
3730
3731OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
3732 SourceLocation KindKwLoc,
3733 SourceLocation StartLoc,
3734 SourceLocation LParenLoc,
3735 SourceLocation EndLoc) {
3736 if (Kind == OMPC_DEFAULT_unknown) {
3737 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00003738 static_assert(OMPC_DEFAULT_unknown > 0,
3739 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00003740 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00003741 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003742 Values += "'";
3743 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
3744 Values += "'";
3745 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00003746 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003747 Values += " or ";
3748 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00003749 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003750 break;
3751 default:
3752 Values += Sep;
3753 break;
3754 }
3755 }
3756 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00003757 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003758 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003759 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00003760 switch (Kind) {
3761 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003762 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003763 break;
3764 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003765 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003766 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003767 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003768 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00003769 break;
3770 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003771 return new (Context)
3772 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003773}
3774
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003775OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
3776 SourceLocation KindKwLoc,
3777 SourceLocation StartLoc,
3778 SourceLocation LParenLoc,
3779 SourceLocation EndLoc) {
3780 if (Kind == OMPC_PROC_BIND_unknown) {
3781 std::string Values;
3782 std::string Sep(", ");
3783 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
3784 Values += "'";
3785 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
3786 Values += "'";
3787 switch (i) {
3788 case OMPC_PROC_BIND_unknown - 2:
3789 Values += " or ";
3790 break;
3791 case OMPC_PROC_BIND_unknown - 1:
3792 break;
3793 default:
3794 Values += Sep;
3795 break;
3796 }
3797 }
3798 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00003799 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003800 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003801 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003802 return new (Context)
3803 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003804}
3805
Alexey Bataev56dafe82014-06-20 07:16:17 +00003806OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
3807 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
3808 SourceLocation StartLoc, SourceLocation LParenLoc,
3809 SourceLocation ArgumentLoc, SourceLocation CommaLoc,
3810 SourceLocation EndLoc) {
3811 OMPClause *Res = nullptr;
3812 switch (Kind) {
3813 case OMPC_schedule:
3814 Res = ActOnOpenMPScheduleClause(
3815 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
3816 LParenLoc, ArgumentLoc, CommaLoc, EndLoc);
3817 break;
3818 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00003819 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003820 case OMPC_num_threads:
3821 case OMPC_safelen:
3822 case OMPC_collapse:
3823 case OMPC_default:
3824 case OMPC_proc_bind:
3825 case OMPC_private:
3826 case OMPC_firstprivate:
3827 case OMPC_lastprivate:
3828 case OMPC_shared:
3829 case OMPC_reduction:
3830 case OMPC_linear:
3831 case OMPC_aligned:
3832 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003833 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003834 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00003835 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003836 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003837 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003838 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00003839 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003840 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00003841 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00003842 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00003843 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003844 case OMPC_seq_cst:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003845 case OMPC_unknown:
3846 llvm_unreachable("Clause is not allowed.");
3847 }
3848 return Res;
3849}
3850
3851OMPClause *Sema::ActOnOpenMPScheduleClause(
3852 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
3853 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
3854 SourceLocation EndLoc) {
3855 if (Kind == OMPC_SCHEDULE_unknown) {
3856 std::string Values;
3857 std::string Sep(", ");
3858 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) {
3859 Values += "'";
3860 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i);
3861 Values += "'";
3862 switch (i) {
3863 case OMPC_SCHEDULE_unknown - 2:
3864 Values += " or ";
3865 break;
3866 case OMPC_SCHEDULE_unknown - 1:
3867 break;
3868 default:
3869 Values += Sep;
3870 break;
3871 }
3872 }
3873 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
3874 << Values << getOpenMPClauseName(OMPC_schedule);
3875 return nullptr;
3876 }
3877 Expr *ValExpr = ChunkSize;
3878 if (ChunkSize) {
3879 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
3880 !ChunkSize->isInstantiationDependent() &&
3881 !ChunkSize->containsUnexpandedParameterPack()) {
3882 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
3883 ExprResult Val =
3884 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
3885 if (Val.isInvalid())
3886 return nullptr;
3887
3888 ValExpr = Val.get();
3889
3890 // OpenMP [2.7.1, Restrictions]
3891 // chunk_size must be a loop invariant integer expression with a positive
3892 // value.
3893 llvm::APSInt Result;
3894 if (ValExpr->isIntegerConstantExpr(Result, Context) &&
3895 Result.isSigned() && !Result.isStrictlyPositive()) {
3896 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
3897 << "schedule" << ChunkSize->getSourceRange();
3898 return nullptr;
3899 }
3900 }
3901 }
3902
3903 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc,
3904 EndLoc, Kind, ValExpr);
3905}
3906
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003907OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
3908 SourceLocation StartLoc,
3909 SourceLocation EndLoc) {
3910 OMPClause *Res = nullptr;
3911 switch (Kind) {
3912 case OMPC_ordered:
3913 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
3914 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00003915 case OMPC_nowait:
3916 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
3917 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003918 case OMPC_untied:
3919 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
3920 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003921 case OMPC_mergeable:
3922 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
3923 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003924 case OMPC_read:
3925 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
3926 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00003927 case OMPC_write:
3928 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
3929 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00003930 case OMPC_update:
3931 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
3932 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00003933 case OMPC_capture:
3934 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
3935 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003936 case OMPC_seq_cst:
3937 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
3938 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003939 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00003940 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003941 case OMPC_num_threads:
3942 case OMPC_safelen:
3943 case OMPC_collapse:
3944 case OMPC_schedule:
3945 case OMPC_private:
3946 case OMPC_firstprivate:
3947 case OMPC_lastprivate:
3948 case OMPC_shared:
3949 case OMPC_reduction:
3950 case OMPC_linear:
3951 case OMPC_aligned:
3952 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003953 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003954 case OMPC_default:
3955 case OMPC_proc_bind:
3956 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00003957 case OMPC_flush:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003958 case OMPC_unknown:
3959 llvm_unreachable("Clause is not allowed.");
3960 }
3961 return Res;
3962}
3963
3964OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
3965 SourceLocation EndLoc) {
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003966 DSAStack->setOrderedRegion();
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003967 return new (Context) OMPOrderedClause(StartLoc, EndLoc);
3968}
3969
Alexey Bataev236070f2014-06-20 11:19:47 +00003970OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
3971 SourceLocation EndLoc) {
3972 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
3973}
3974
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003975OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
3976 SourceLocation EndLoc) {
3977 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
3978}
3979
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003980OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
3981 SourceLocation EndLoc) {
3982 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
3983}
3984
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003985OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
3986 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003987 return new (Context) OMPReadClause(StartLoc, EndLoc);
3988}
3989
Alexey Bataevdea47612014-07-23 07:46:59 +00003990OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
3991 SourceLocation EndLoc) {
3992 return new (Context) OMPWriteClause(StartLoc, EndLoc);
3993}
3994
Alexey Bataev67a4f222014-07-23 10:25:33 +00003995OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
3996 SourceLocation EndLoc) {
3997 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
3998}
3999
Alexey Bataev459dec02014-07-24 06:46:57 +00004000OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
4001 SourceLocation EndLoc) {
4002 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
4003}
4004
Alexey Bataev82bad8b2014-07-24 08:55:34 +00004005OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
4006 SourceLocation EndLoc) {
4007 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
4008}
4009
Alexey Bataevc5e02582014-06-16 07:08:35 +00004010OMPClause *Sema::ActOnOpenMPVarListClause(
4011 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
4012 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
4013 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
4014 const DeclarationNameInfo &ReductionId) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004015 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004016 switch (Kind) {
4017 case OMPC_private:
4018 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
4019 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004020 case OMPC_firstprivate:
4021 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
4022 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00004023 case OMPC_lastprivate:
4024 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
4025 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00004026 case OMPC_shared:
4027 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
4028 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00004029 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00004030 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
4031 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00004032 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00004033 case OMPC_linear:
4034 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
4035 ColonLoc, EndLoc);
4036 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00004037 case OMPC_aligned:
4038 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
4039 ColonLoc, EndLoc);
4040 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004041 case OMPC_copyin:
4042 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
4043 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00004044 case OMPC_copyprivate:
4045 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
4046 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00004047 case OMPC_flush:
4048 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
4049 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004050 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00004051 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00004052 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00004053 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00004054 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004055 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004056 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00004057 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004058 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00004059 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00004060 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00004061 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004062 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004063 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00004064 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00004065 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00004066 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00004067 case OMPC_seq_cst:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004068 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004069 llvm_unreachable("Clause is not allowed.");
4070 }
4071 return Res;
4072}
4073
4074OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
4075 SourceLocation StartLoc,
4076 SourceLocation LParenLoc,
4077 SourceLocation EndLoc) {
4078 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00004079 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00004080 for (auto &RefExpr : VarList) {
4081 assert(RefExpr && "NULL expr in OpenMP private clause.");
4082 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004083 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00004084 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00004085 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004086 continue;
4087 }
4088
Alexey Bataeved09d242014-05-28 05:53:51 +00004089 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004090 // OpenMP [2.1, C/C++]
4091 // A list item is a variable name.
4092 // OpenMP [2.9.3.3, Restrictions, p.1]
4093 // A variable that is part of another variable (as an array or
4094 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00004095 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004096 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00004097 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004098 continue;
4099 }
4100 Decl *D = DE->getDecl();
4101 VarDecl *VD = cast<VarDecl>(D);
4102
4103 QualType Type = VD->getType();
4104 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
4105 // It will be analyzed later.
4106 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00004107 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004108 continue;
4109 }
4110
4111 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
4112 // A variable that appears in a private clause must not have an incomplete
4113 // type or a reference type.
4114 if (RequireCompleteType(ELoc, Type,
4115 diag::err_omp_private_incomplete_type)) {
4116 continue;
4117 }
4118 if (Type->isReferenceType()) {
4119 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00004120 << getOpenMPClauseName(OMPC_private) << Type;
4121 bool IsDecl =
4122 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4123 Diag(VD->getLocation(),
4124 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4125 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004126 continue;
4127 }
4128
4129 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
4130 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00004131 // clause requires an accessible, unambiguous default constructor for the
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004132 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00004133 while (Type->isArrayType()) {
4134 Type = cast<ArrayType>(Type.getTypePtr())->getElementType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004135 }
4136
Alexey Bataev758e55e2013-09-06 18:03:48 +00004137 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
4138 // in a Construct]
4139 // Variables with the predetermined data-sharing attributes may not be
4140 // listed in data-sharing attributes clauses, except for the cases
4141 // listed below. For these exceptions only, listing a predetermined
4142 // variable in a data-sharing attribute clause is allowed and overrides
4143 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004144 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004145 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00004146 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
4147 << getOpenMPClauseName(OMPC_private);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004148 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004149 continue;
4150 }
4151
Alexey Bataev03b340a2014-10-21 03:16:40 +00004152 // Generate helper private variable and initialize it with the default
4153 // value. The address of the original variable is replaced by the address of
4154 // the new private variable in CodeGen. This new variable is not added to
4155 // IdResolver, so the code in the OpenMP region uses original variable for
4156 // proper diagnostics.
4157 auto VDPrivate =
4158 VarDecl::Create(Context, CurContext, DE->getLocStart(),
4159 DE->getExprLoc(), VD->getIdentifier(), VD->getType(),
4160 VD->getTypeSourceInfo(), /*S*/ SC_Auto);
4161 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto*/ false);
4162 if (VDPrivate->isInvalidDecl())
4163 continue;
4164 CurContext->addDecl(VDPrivate);
Alexey Bataev07649fb2014-12-16 08:01:48 +00004165 auto VDPrivateRefExpr =
4166 DeclRefExpr::Create(Context, /*QualifierLoc*/ NestedNameSpecifierLoc(),
4167 /*TemplateKWLoc*/ SourceLocation(), VDPrivate,
Alexey Bataev19acc3d2015-01-12 10:17:46 +00004168 /*RefersToEnclosingVariableOrCapture*/ false,
Alexey Bataev07649fb2014-12-16 08:01:48 +00004169 /*NameLoc*/ SourceLocation(), DE->getType(),
4170 /*VK*/ VK_LValue);
Alexey Bataev03b340a2014-10-21 03:16:40 +00004171
Alexey Bataev758e55e2013-09-06 18:03:48 +00004172 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004173 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00004174 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004175 }
4176
Alexey Bataeved09d242014-05-28 05:53:51 +00004177 if (Vars.empty())
4178 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004179
Alexey Bataev03b340a2014-10-21 03:16:40 +00004180 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
4181 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004182}
4183
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004184namespace {
4185class DiagsUninitializedSeveretyRAII {
4186private:
4187 DiagnosticsEngine &Diags;
4188 SourceLocation SavedLoc;
4189 bool IsIgnored;
4190
4191public:
4192 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
4193 bool IsIgnored)
4194 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
4195 if (!IsIgnored) {
4196 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
4197 /*Map*/ diag::Severity::Ignored, Loc);
4198 }
4199 }
4200 ~DiagsUninitializedSeveretyRAII() {
4201 if (!IsIgnored)
4202 Diags.popMappings(SavedLoc);
4203 }
4204};
4205}
4206
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004207OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
4208 SourceLocation StartLoc,
4209 SourceLocation LParenLoc,
4210 SourceLocation EndLoc) {
4211 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004212 SmallVector<Expr *, 8> PrivateCopies;
4213 SmallVector<Expr *, 8> Inits;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004214 bool IsImplicitClause =
4215 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
4216 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
4217
Alexey Bataeved09d242014-05-28 05:53:51 +00004218 for (auto &RefExpr : VarList) {
4219 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
4220 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004221 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00004222 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004223 PrivateCopies.push_back(nullptr);
4224 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004225 continue;
4226 }
4227
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004228 SourceLocation ELoc =
4229 IsImplicitClause ? ImplicitClauseLoc : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004230 // OpenMP [2.1, C/C++]
4231 // A list item is a variable name.
4232 // OpenMP [2.9.3.3, Restrictions, p.1]
4233 // A variable that is part of another variable (as an array or
4234 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00004235 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004236 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00004237 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004238 continue;
4239 }
4240 Decl *D = DE->getDecl();
4241 VarDecl *VD = cast<VarDecl>(D);
4242
4243 QualType Type = VD->getType();
4244 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
4245 // It will be analyzed later.
4246 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004247 PrivateCopies.push_back(nullptr);
4248 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004249 continue;
4250 }
4251
4252 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
4253 // A variable that appears in a private clause must not have an incomplete
4254 // type or a reference type.
4255 if (RequireCompleteType(ELoc, Type,
4256 diag::err_omp_firstprivate_incomplete_type)) {
4257 continue;
4258 }
4259 if (Type->isReferenceType()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004260 if (IsImplicitClause) {
4261 Diag(ImplicitClauseLoc,
4262 diag::err_omp_task_predetermined_firstprivate_ref_type_arg)
4263 << Type;
4264 Diag(RefExpr->getExprLoc(), diag::note_used_here);
4265 } else {
4266 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
4267 << getOpenMPClauseName(OMPC_firstprivate) << Type;
4268 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004269 bool IsDecl =
4270 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4271 Diag(VD->getLocation(),
4272 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4273 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004274 continue;
4275 }
4276
4277 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
4278 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00004279 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004280 // class type.
4281 Type = Context.getBaseElementType(Type);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004282
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004283 // If an implicit firstprivate variable found it was checked already.
4284 if (!IsImplicitClause) {
4285 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004286 Type = Type.getNonReferenceType().getCanonicalType();
4287 bool IsConstant = Type.isConstant(Context);
4288 Type = Context.getBaseElementType(Type);
4289 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
4290 // A list item that specifies a given variable may not appear in more
4291 // than one clause on the same directive, except that a variable may be
4292 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004293 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00004294 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004295 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00004296 << getOpenMPClauseName(DVar.CKind)
4297 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004298 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004299 continue;
4300 }
4301
4302 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
4303 // in a Construct]
4304 // Variables with the predetermined data-sharing attributes may not be
4305 // listed in data-sharing attributes clauses, except for the cases
4306 // listed below. For these exceptions only, listing a predetermined
4307 // variable in a data-sharing attribute clause is allowed and overrides
4308 // the variable's predetermined data-sharing attributes.
4309 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
4310 // in a Construct, C/C++, p.2]
4311 // Variables with const-qualified type having no mutable member may be
4312 // listed in a firstprivate clause, even if they are static data members.
4313 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
4314 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
4315 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00004316 << getOpenMPClauseName(DVar.CKind)
4317 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004318 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004319 continue;
4320 }
4321
Alexey Bataevf29276e2014-06-18 04:14:57 +00004322 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004323 // OpenMP [2.9.3.4, Restrictions, p.2]
4324 // A list item that is private within a parallel region must not appear
4325 // in a firstprivate clause on a worksharing construct if any of the
4326 // worksharing regions arising from the worksharing construct ever bind
4327 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00004328 if (isOpenMPWorksharingDirective(CurrDir) &&
4329 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004330 DVar = DSAStack->getImplicitDSA(VD, true);
4331 if (DVar.CKind != OMPC_shared &&
4332 (isOpenMPParallelDirective(DVar.DKind) ||
4333 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00004334 Diag(ELoc, diag::err_omp_required_access)
4335 << getOpenMPClauseName(OMPC_firstprivate)
4336 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004337 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004338 continue;
4339 }
4340 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004341 // OpenMP [2.9.3.4, Restrictions, p.3]
4342 // A list item that appears in a reduction clause of a parallel construct
4343 // must not appear in a firstprivate clause on a worksharing or task
4344 // construct if any of the worksharing or task regions arising from the
4345 // worksharing or task construct ever bind to any of the parallel regions
4346 // arising from the parallel construct.
4347 // OpenMP [2.9.3.4, Restrictions, p.4]
4348 // A list item that appears in a reduction clause in worksharing
4349 // construct must not appear in a firstprivate clause in a task construct
4350 // encountered during execution of any of the worksharing regions arising
4351 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004352 if (CurrDir == OMPD_task) {
4353 DVar =
4354 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
4355 [](OpenMPDirectiveKind K) -> bool {
4356 return isOpenMPParallelDirective(K) ||
4357 isOpenMPWorksharingDirective(K);
4358 },
4359 false);
4360 if (DVar.CKind == OMPC_reduction &&
4361 (isOpenMPParallelDirective(DVar.DKind) ||
4362 isOpenMPWorksharingDirective(DVar.DKind))) {
4363 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
4364 << getOpenMPDirectiveName(DVar.DKind);
4365 ReportOriginalDSA(*this, DSAStack, VD, DVar);
4366 continue;
4367 }
4368 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004369 }
4370
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004371 Type = Type.getUnqualifiedType();
4372 auto VDPrivate = VarDecl::Create(Context, CurContext, DE->getLocStart(),
4373 ELoc, VD->getIdentifier(), VD->getType(),
4374 VD->getTypeSourceInfo(), /*S*/ SC_Auto);
4375 // Generate helper private variable and initialize it with the value of the
4376 // original variable. The address of the original variable is replaced by
4377 // the address of the new private variable in the CodeGen. This new variable
4378 // is not added to IdResolver, so the code in the OpenMP region uses
4379 // original variable for proper diagnostics and variable capturing.
4380 Expr *VDInitRefExpr = nullptr;
4381 // For arrays generate initializer for single element and replace it by the
4382 // original array element in CodeGen.
4383 if (DE->getType()->isArrayType()) {
4384 auto VDInit = VarDecl::Create(Context, CurContext, DE->getLocStart(),
4385 ELoc, VD->getIdentifier(), Type,
4386 VD->getTypeSourceInfo(), /*S*/ SC_Auto);
4387 CurContext->addHiddenDecl(VDInit);
4388 VDInitRefExpr = DeclRefExpr::Create(
4389 Context, /*QualifierLoc*/ NestedNameSpecifierLoc(),
4390 /*TemplateKWLoc*/ SourceLocation(), VDInit,
Alexey Bataev19acc3d2015-01-12 10:17:46 +00004391 /*RefersToEnclosingVariableOrCapture*/ true, ELoc, Type,
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004392 /*VK*/ VK_LValue);
4393 VDInit->setIsUsed();
4394 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
4395 InitializedEntity Entity = InitializedEntity::InitializeVariable(VDInit);
4396 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
4397
4398 InitializationSequence InitSeq(*this, Entity, Kind, Init);
4399 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
4400 if (Result.isInvalid())
4401 VDPrivate->setInvalidDecl();
4402 else
4403 VDPrivate->setInit(Result.getAs<Expr>());
4404 } else {
Alexey Bataevf841bd92014-12-16 07:00:22 +00004405 AddInitializerToDecl(
Alexey Bataev19acc3d2015-01-12 10:17:46 +00004406 VDPrivate,
4407 DefaultLvalueConversion(
4408 DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
4409 SourceLocation(), DE->getDecl(),
4410 /*RefersToEnclosingVariableOrCapture=*/true,
4411 DE->getExprLoc(), DE->getType(),
4412 /*VK=*/VK_LValue)).get(),
Alexey Bataevf841bd92014-12-16 07:00:22 +00004413 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004414 }
4415 if (VDPrivate->isInvalidDecl()) {
4416 if (IsImplicitClause) {
4417 Diag(DE->getExprLoc(),
4418 diag::note_omp_task_predetermined_firstprivate_here);
4419 }
4420 continue;
4421 }
4422 CurContext->addDecl(VDPrivate);
Alexey Bataev19acc3d2015-01-12 10:17:46 +00004423 auto VDPrivateRefExpr =
4424 DeclRefExpr::Create(Context, /*QualifierLoc*/ NestedNameSpecifierLoc(),
4425 /*TemplateKWLoc*/ SourceLocation(), VDPrivate,
4426 /*RefersToEnclosingVariableOrCapture*/ false,
4427 DE->getLocStart(), DE->getType(),
4428 /*VK*/ VK_LValue);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004429 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
4430 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004431 PrivateCopies.push_back(VDPrivateRefExpr);
4432 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004433 }
4434
Alexey Bataeved09d242014-05-28 05:53:51 +00004435 if (Vars.empty())
4436 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004437
4438 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004439 Vars, PrivateCopies, Inits);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004440}
4441
Alexander Musman1bb328c2014-06-04 13:06:39 +00004442OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
4443 SourceLocation StartLoc,
4444 SourceLocation LParenLoc,
4445 SourceLocation EndLoc) {
4446 SmallVector<Expr *, 8> Vars;
4447 for (auto &RefExpr : VarList) {
4448 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
4449 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
4450 // It will be analyzed later.
4451 Vars.push_back(RefExpr);
4452 continue;
4453 }
4454
4455 SourceLocation ELoc = RefExpr->getExprLoc();
4456 // OpenMP [2.1, C/C++]
4457 // A list item is a variable name.
4458 // OpenMP [2.14.3.5, Restrictions, p.1]
4459 // A variable that is part of another variable (as an array or structure
4460 // element) cannot appear in a lastprivate clause.
4461 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
4462 if (!DE || !isa<VarDecl>(DE->getDecl())) {
4463 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
4464 continue;
4465 }
4466 Decl *D = DE->getDecl();
4467 VarDecl *VD = cast<VarDecl>(D);
4468
4469 QualType Type = VD->getType();
4470 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
4471 // It will be analyzed later.
4472 Vars.push_back(DE);
4473 continue;
4474 }
4475
4476 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
4477 // A variable that appears in a lastprivate clause must not have an
4478 // incomplete type or a reference type.
4479 if (RequireCompleteType(ELoc, Type,
4480 diag::err_omp_lastprivate_incomplete_type)) {
4481 continue;
4482 }
4483 if (Type->isReferenceType()) {
4484 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
4485 << getOpenMPClauseName(OMPC_lastprivate) << Type;
4486 bool IsDecl =
4487 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4488 Diag(VD->getLocation(),
4489 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4490 << VD;
4491 continue;
4492 }
4493
4494 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4495 // in a Construct]
4496 // Variables with the predetermined data-sharing attributes may not be
4497 // listed in data-sharing attributes clauses, except for the cases
4498 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004499 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00004500 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
4501 DVar.CKind != OMPC_firstprivate &&
4502 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
4503 Diag(ELoc, diag::err_omp_wrong_dsa)
4504 << getOpenMPClauseName(DVar.CKind)
4505 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004506 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00004507 continue;
4508 }
4509
Alexey Bataevf29276e2014-06-18 04:14:57 +00004510 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
4511 // OpenMP [2.14.3.5, Restrictions, p.2]
4512 // A list item that is private within a parallel region, or that appears in
4513 // the reduction clause of a parallel construct, must not appear in a
4514 // lastprivate clause on a worksharing construct if any of the corresponding
4515 // worksharing regions ever binds to any of the corresponding parallel
4516 // regions.
Alexey Bataev549210e2014-06-24 04:39:47 +00004517 if (isOpenMPWorksharingDirective(CurrDir) &&
4518 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004519 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004520 if (DVar.CKind != OMPC_shared) {
4521 Diag(ELoc, diag::err_omp_required_access)
4522 << getOpenMPClauseName(OMPC_lastprivate)
4523 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004524 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004525 continue;
4526 }
4527 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00004528 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00004529 // A variable of class type (or array thereof) that appears in a
4530 // lastprivate clause requires an accessible, unambiguous default
4531 // constructor for the class type, unless the list item is also specified
4532 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00004533 // A variable of class type (or array thereof) that appears in a
4534 // lastprivate clause requires an accessible, unambiguous copy assignment
4535 // operator for the class type.
4536 while (Type.getNonReferenceType()->isArrayType())
4537 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
4538 ->getElementType();
4539 CXXRecordDecl *RD = getLangOpts().CPlusPlus
4540 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
4541 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00004542 // FIXME This code must be replaced by actual copying and destructing of the
4543 // lastprivate variable.
Alexander Musman1bb328c2014-06-04 13:06:39 +00004544 if (RD) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00004545 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
4546 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00004547 if (MD) {
4548 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
4549 MD->isDeleted()) {
4550 Diag(ELoc, diag::err_omp_required_method)
4551 << getOpenMPClauseName(OMPC_lastprivate) << 2;
4552 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4553 VarDecl::DeclarationOnly;
4554 Diag(VD->getLocation(),
4555 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4556 << VD;
4557 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4558 continue;
4559 }
4560 MarkFunctionReferenced(ELoc, MD);
4561 DiagnoseUseOfDecl(MD, ELoc);
Alexander Musman1bb328c2014-06-04 13:06:39 +00004562 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00004563
4564 CXXDestructorDecl *DD = RD->getDestructor();
4565 if (DD) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00004566 PartialDiagnostic PD =
4567 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexander Musman1bb328c2014-06-04 13:06:39 +00004568 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
4569 DD->isDeleted()) {
4570 Diag(ELoc, diag::err_omp_required_method)
4571 << getOpenMPClauseName(OMPC_lastprivate) << 4;
4572 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4573 VarDecl::DeclarationOnly;
4574 Diag(VD->getLocation(),
4575 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4576 << VD;
4577 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4578 continue;
4579 }
4580 MarkFunctionReferenced(ELoc, DD);
4581 DiagnoseUseOfDecl(DD, ELoc);
4582 }
4583 }
4584
Alexey Bataevf29276e2014-06-18 04:14:57 +00004585 if (DVar.CKind != OMPC_firstprivate)
4586 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00004587 Vars.push_back(DE);
4588 }
4589
4590 if (Vars.empty())
4591 return nullptr;
4592
4593 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
4594 Vars);
4595}
4596
Alexey Bataev758e55e2013-09-06 18:03:48 +00004597OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
4598 SourceLocation StartLoc,
4599 SourceLocation LParenLoc,
4600 SourceLocation EndLoc) {
4601 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00004602 for (auto &RefExpr : VarList) {
4603 assert(RefExpr && "NULL expr in OpenMP shared clause.");
4604 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00004605 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00004606 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004607 continue;
4608 }
4609
Alexey Bataeved09d242014-05-28 05:53:51 +00004610 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00004611 // OpenMP [2.1, C/C++]
4612 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00004613 // OpenMP [2.14.3.2, Restrictions, p.1]
4614 // A variable that is part of another variable (as an array or structure
4615 // element) cannot appear in a shared unless it is a static data member
4616 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00004617 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004618 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00004619 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00004620 continue;
4621 }
4622 Decl *D = DE->getDecl();
4623 VarDecl *VD = cast<VarDecl>(D);
4624
4625 QualType Type = VD->getType();
4626 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
4627 // It will be analyzed later.
4628 Vars.push_back(DE);
4629 continue;
4630 }
4631
4632 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
4633 // in a Construct]
4634 // Variables with the predetermined data-sharing attributes may not be
4635 // listed in data-sharing attributes clauses, except for the cases
4636 // listed below. For these exceptions only, listing a predetermined
4637 // variable in a data-sharing attribute clause is allowed and overrides
4638 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004639 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00004640 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
4641 DVar.RefExpr) {
4642 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
4643 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004644 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004645 continue;
4646 }
4647
4648 DSAStack->addDSA(VD, DE, OMPC_shared);
4649 Vars.push_back(DE);
4650 }
4651
Alexey Bataeved09d242014-05-28 05:53:51 +00004652 if (Vars.empty())
4653 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00004654
4655 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
4656}
4657
Alexey Bataevc5e02582014-06-16 07:08:35 +00004658namespace {
4659class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
4660 DSAStackTy *Stack;
4661
4662public:
4663 bool VisitDeclRefExpr(DeclRefExpr *E) {
4664 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004665 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00004666 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
4667 return false;
4668 if (DVar.CKind != OMPC_unknown)
4669 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00004670 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004671 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004672 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00004673 return true;
4674 return false;
4675 }
4676 return false;
4677 }
4678 bool VisitStmt(Stmt *S) {
4679 for (auto Child : S->children()) {
4680 if (Child && Visit(Child))
4681 return true;
4682 }
4683 return false;
4684 }
Alexey Bataev23b69422014-06-18 07:08:49 +00004685 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00004686};
Alexey Bataev23b69422014-06-18 07:08:49 +00004687} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00004688
4689OMPClause *Sema::ActOnOpenMPReductionClause(
4690 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
4691 SourceLocation ColonLoc, SourceLocation EndLoc,
4692 CXXScopeSpec &ReductionIdScopeSpec,
4693 const DeclarationNameInfo &ReductionId) {
4694 // TODO: Allow scope specification search when 'declare reduction' is
4695 // supported.
4696 assert(ReductionIdScopeSpec.isEmpty() &&
4697 "No support for scoped reduction identifiers yet.");
4698
4699 auto DN = ReductionId.getName();
4700 auto OOK = DN.getCXXOverloadedOperator();
4701 BinaryOperatorKind BOK = BO_Comma;
4702
4703 // OpenMP [2.14.3.6, reduction clause]
4704 // C
4705 // reduction-identifier is either an identifier or one of the following
4706 // operators: +, -, *, &, |, ^, && and ||
4707 // C++
4708 // reduction-identifier is either an id-expression or one of the following
4709 // operators: +, -, *, &, |, ^, && and ||
4710 // FIXME: Only 'min' and 'max' identifiers are supported for now.
4711 switch (OOK) {
4712 case OO_Plus:
4713 case OO_Minus:
4714 BOK = BO_AddAssign;
4715 break;
4716 case OO_Star:
4717 BOK = BO_MulAssign;
4718 break;
4719 case OO_Amp:
4720 BOK = BO_AndAssign;
4721 break;
4722 case OO_Pipe:
4723 BOK = BO_OrAssign;
4724 break;
4725 case OO_Caret:
4726 BOK = BO_XorAssign;
4727 break;
4728 case OO_AmpAmp:
4729 BOK = BO_LAnd;
4730 break;
4731 case OO_PipePipe:
4732 BOK = BO_LOr;
4733 break;
4734 default:
4735 if (auto II = DN.getAsIdentifierInfo()) {
4736 if (II->isStr("max"))
4737 BOK = BO_GT;
4738 else if (II->isStr("min"))
4739 BOK = BO_LT;
4740 }
4741 break;
4742 }
4743 SourceRange ReductionIdRange;
4744 if (ReductionIdScopeSpec.isValid()) {
4745 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
4746 }
4747 ReductionIdRange.setEnd(ReductionId.getEndLoc());
4748 if (BOK == BO_Comma) {
4749 // Not allowed reduction identifier is found.
4750 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
4751 << ReductionIdRange;
4752 return nullptr;
4753 }
4754
4755 SmallVector<Expr *, 8> Vars;
4756 for (auto RefExpr : VarList) {
4757 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
4758 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
4759 // It will be analyzed later.
4760 Vars.push_back(RefExpr);
4761 continue;
4762 }
4763
4764 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
4765 RefExpr->isInstantiationDependent() ||
4766 RefExpr->containsUnexpandedParameterPack()) {
4767 // It will be analyzed later.
4768 Vars.push_back(RefExpr);
4769 continue;
4770 }
4771
4772 auto ELoc = RefExpr->getExprLoc();
4773 auto ERange = RefExpr->getSourceRange();
4774 // OpenMP [2.1, C/C++]
4775 // A list item is a variable or array section, subject to the restrictions
4776 // specified in Section 2.4 on page 42 and in each of the sections
4777 // describing clauses and directives for which a list appears.
4778 // OpenMP [2.14.3.3, Restrictions, p.1]
4779 // A variable that is part of another variable (as an array or
4780 // structure element) cannot appear in a private clause.
4781 auto DE = dyn_cast<DeclRefExpr>(RefExpr);
4782 if (!DE || !isa<VarDecl>(DE->getDecl())) {
4783 Diag(ELoc, diag::err_omp_expected_var_name) << ERange;
4784 continue;
4785 }
4786 auto D = DE->getDecl();
4787 auto VD = cast<VarDecl>(D);
4788 auto Type = VD->getType();
4789 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
4790 // A variable that appears in a private clause must not have an incomplete
4791 // type or a reference type.
4792 if (RequireCompleteType(ELoc, Type,
4793 diag::err_omp_reduction_incomplete_type))
4794 continue;
4795 // OpenMP [2.14.3.6, reduction clause, Restrictions]
4796 // Arrays may not appear in a reduction clause.
4797 if (Type.getNonReferenceType()->isArrayType()) {
4798 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
4799 bool IsDecl =
4800 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4801 Diag(VD->getLocation(),
4802 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4803 << VD;
4804 continue;
4805 }
4806 // OpenMP [2.14.3.6, reduction clause, Restrictions]
4807 // A list item that appears in a reduction clause must not be
4808 // const-qualified.
4809 if (Type.getNonReferenceType().isConstant(Context)) {
4810 Diag(ELoc, diag::err_omp_const_variable)
4811 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
4812 bool IsDecl =
4813 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4814 Diag(VD->getLocation(),
4815 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4816 << VD;
4817 continue;
4818 }
4819 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
4820 // If a list-item is a reference type then it must bind to the same object
4821 // for all threads of the team.
4822 VarDecl *VDDef = VD->getDefinition();
4823 if (Type->isReferenceType() && VDDef) {
4824 DSARefChecker Check(DSAStack);
4825 if (Check.Visit(VDDef->getInit())) {
4826 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
4827 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
4828 continue;
4829 }
4830 }
4831 // OpenMP [2.14.3.6, reduction clause, Restrictions]
4832 // The type of a list item that appears in a reduction clause must be valid
4833 // for the reduction-identifier. For a max or min reduction in C, the type
4834 // of the list item must be an allowed arithmetic data type: char, int,
4835 // float, double, or _Bool, possibly modified with long, short, signed, or
4836 // unsigned. For a max or min reduction in C++, the type of the list item
4837 // must be an allowed arithmetic data type: char, wchar_t, int, float,
4838 // double, or bool, possibly modified with long, short, signed, or unsigned.
4839 if ((BOK == BO_GT || BOK == BO_LT) &&
4840 !(Type->isScalarType() ||
4841 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
4842 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
4843 << getLangOpts().CPlusPlus;
4844 bool IsDecl =
4845 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4846 Diag(VD->getLocation(),
4847 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4848 << VD;
4849 continue;
4850 }
4851 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
4852 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
4853 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
4854 bool IsDecl =
4855 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4856 Diag(VD->getLocation(),
4857 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4858 << VD;
4859 continue;
4860 }
4861 bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
4862 getDiagnostics().setSuppressAllDiagnostics(true);
4863 ExprResult ReductionOp =
4864 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
4865 RefExpr, RefExpr);
4866 getDiagnostics().setSuppressAllDiagnostics(Suppress);
4867 if (ReductionOp.isInvalid()) {
4868 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
Alexey Bataev23b69422014-06-18 07:08:49 +00004869 << ReductionIdRange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00004870 bool IsDecl =
4871 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4872 Diag(VD->getLocation(),
4873 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4874 << VD;
4875 continue;
4876 }
4877
4878 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4879 // in a Construct]
4880 // Variables with the predetermined data-sharing attributes may not be
4881 // listed in data-sharing attributes clauses, except for the cases
4882 // listed below. For these exceptions only, listing a predetermined
4883 // variable in a data-sharing attribute clause is allowed and overrides
4884 // the variable's predetermined data-sharing attributes.
4885 // OpenMP [2.14.3.6, Restrictions, p.3]
4886 // Any number of reduction clauses can be specified on the directive,
4887 // but a list item can appear only once in the reduction clauses for that
4888 // directive.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004889 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00004890 if (DVar.CKind == OMPC_reduction) {
4891 Diag(ELoc, diag::err_omp_once_referenced)
4892 << getOpenMPClauseName(OMPC_reduction);
4893 if (DVar.RefExpr) {
4894 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
4895 }
4896 } else if (DVar.CKind != OMPC_unknown) {
4897 Diag(ELoc, diag::err_omp_wrong_dsa)
4898 << getOpenMPClauseName(DVar.CKind)
4899 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004900 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00004901 continue;
4902 }
4903
4904 // OpenMP [2.14.3.6, Restrictions, p.1]
4905 // A list item that appears in a reduction clause of a worksharing
4906 // construct must be shared in the parallel regions to which any of the
4907 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf29276e2014-06-18 04:14:57 +00004908 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataev549210e2014-06-24 04:39:47 +00004909 if (isOpenMPWorksharingDirective(CurrDir) &&
4910 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004911 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004912 if (DVar.CKind != OMPC_shared) {
4913 Diag(ELoc, diag::err_omp_required_access)
4914 << getOpenMPClauseName(OMPC_reduction)
4915 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004916 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004917 continue;
4918 }
4919 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00004920
4921 CXXRecordDecl *RD = getLangOpts().CPlusPlus
4922 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
4923 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00004924 // FIXME This code must be replaced by actual constructing/destructing of
4925 // the reduction variable.
Alexey Bataevc5e02582014-06-16 07:08:35 +00004926 if (RD) {
4927 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
4928 PartialDiagnostic PD =
4929 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataev23b69422014-06-18 07:08:49 +00004930 if (!CD ||
4931 CheckConstructorAccess(ELoc, CD,
4932 InitializedEntity::InitializeTemporary(Type),
4933 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevc5e02582014-06-16 07:08:35 +00004934 CD->isDeleted()) {
4935 Diag(ELoc, diag::err_omp_required_method)
4936 << getOpenMPClauseName(OMPC_reduction) << 0;
4937 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4938 VarDecl::DeclarationOnly;
4939 Diag(VD->getLocation(),
4940 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4941 << VD;
4942 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4943 continue;
4944 }
4945 MarkFunctionReferenced(ELoc, CD);
4946 DiagnoseUseOfDecl(CD, ELoc);
4947
4948 CXXDestructorDecl *DD = RD->getDestructor();
4949 if (DD) {
4950 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
4951 DD->isDeleted()) {
4952 Diag(ELoc, diag::err_omp_required_method)
4953 << getOpenMPClauseName(OMPC_reduction) << 4;
4954 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4955 VarDecl::DeclarationOnly;
4956 Diag(VD->getLocation(),
4957 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4958 << VD;
4959 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4960 continue;
4961 }
4962 MarkFunctionReferenced(ELoc, DD);
4963 DiagnoseUseOfDecl(DD, ELoc);
4964 }
4965 }
4966
4967 DSAStack->addDSA(VD, DE, OMPC_reduction);
4968 Vars.push_back(DE);
4969 }
4970
4971 if (Vars.empty())
4972 return nullptr;
4973
4974 return OMPReductionClause::Create(
4975 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
4976 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId);
4977}
4978
Alexander Musman8dba6642014-04-22 13:09:42 +00004979OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step,
4980 SourceLocation StartLoc,
4981 SourceLocation LParenLoc,
4982 SourceLocation ColonLoc,
4983 SourceLocation EndLoc) {
4984 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00004985 for (auto &RefExpr : VarList) {
4986 assert(RefExpr && "NULL expr in OpenMP linear clause.");
4987 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00004988 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00004989 Vars.push_back(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00004990 continue;
4991 }
4992
4993 // OpenMP [2.14.3.7, linear clause]
4994 // A list item that appears in a linear clause is subject to the private
4995 // clause semantics described in Section 2.14.3.3 on page 159 except as
4996 // noted. In addition, the value of the new list item on each iteration
4997 // of the associated loop(s) corresponds to the value of the original
4998 // list item before entering the construct plus the logical number of
4999 // the iteration times linear-step.
5000
Alexey Bataeved09d242014-05-28 05:53:51 +00005001 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00005002 // OpenMP [2.1, C/C++]
5003 // A list item is a variable name.
5004 // OpenMP [2.14.3.3, Restrictions, p.1]
5005 // A variable that is part of another variable (as an array or
5006 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00005007 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00005008 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005009 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00005010 continue;
5011 }
5012
5013 VarDecl *VD = cast<VarDecl>(DE->getDecl());
5014
5015 // OpenMP [2.14.3.7, linear clause]
5016 // A list-item cannot appear in more than one linear clause.
5017 // A list-item that appears in a linear clause cannot appear in any
5018 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005019 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00005020 if (DVar.RefExpr) {
5021 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
5022 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005023 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00005024 continue;
5025 }
5026
5027 QualType QType = VD->getType();
5028 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
5029 // It will be analyzed later.
5030 Vars.push_back(DE);
5031 continue;
5032 }
5033
5034 // A variable must not have an incomplete type or a reference type.
5035 if (RequireCompleteType(ELoc, QType,
5036 diag::err_omp_linear_incomplete_type)) {
5037 continue;
5038 }
5039 if (QType->isReferenceType()) {
5040 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
5041 << getOpenMPClauseName(OMPC_linear) << QType;
5042 bool IsDecl =
5043 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5044 Diag(VD->getLocation(),
5045 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5046 << VD;
5047 continue;
5048 }
5049
5050 // A list item must not be const-qualified.
5051 if (QType.isConstant(Context)) {
5052 Diag(ELoc, diag::err_omp_const_variable)
5053 << getOpenMPClauseName(OMPC_linear);
5054 bool IsDecl =
5055 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5056 Diag(VD->getLocation(),
5057 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5058 << VD;
5059 continue;
5060 }
5061
5062 // A list item must be of integral or pointer type.
5063 QType = QType.getUnqualifiedType().getCanonicalType();
5064 const Type *Ty = QType.getTypePtrOrNull();
5065 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
5066 !Ty->isPointerType())) {
5067 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
5068 bool IsDecl =
5069 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5070 Diag(VD->getLocation(),
5071 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5072 << VD;
5073 continue;
5074 }
5075
5076 DSAStack->addDSA(VD, DE, OMPC_linear);
5077 Vars.push_back(DE);
5078 }
5079
5080 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005081 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00005082
5083 Expr *StepExpr = Step;
5084 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
5085 !Step->isInstantiationDependent() &&
5086 !Step->containsUnexpandedParameterPack()) {
5087 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005088 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00005089 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005090 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00005091 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00005092
5093 // Warn about zero linear step (it would be probably better specified as
5094 // making corresponding variables 'const').
5095 llvm::APSInt Result;
5096 if (StepExpr->isIntegerConstantExpr(Result, Context) &&
5097 !Result.isNegative() && !Result.isStrictlyPositive())
5098 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
5099 << (Vars.size() > 1);
5100 }
5101
5102 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc,
5103 Vars, StepExpr);
5104}
5105
Alexander Musmanf0d76e72014-05-29 14:36:25 +00005106OMPClause *Sema::ActOnOpenMPAlignedClause(
5107 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
5108 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
5109
5110 SmallVector<Expr *, 8> Vars;
5111 for (auto &RefExpr : VarList) {
5112 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
5113 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
5114 // It will be analyzed later.
5115 Vars.push_back(RefExpr);
5116 continue;
5117 }
5118
5119 SourceLocation ELoc = RefExpr->getExprLoc();
5120 // OpenMP [2.1, C/C++]
5121 // A list item is a variable name.
5122 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
5123 if (!DE || !isa<VarDecl>(DE->getDecl())) {
5124 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
5125 continue;
5126 }
5127
5128 VarDecl *VD = cast<VarDecl>(DE->getDecl());
5129
5130 // OpenMP [2.8.1, simd construct, Restrictions]
5131 // The type of list items appearing in the aligned clause must be
5132 // array, pointer, reference to array, or reference to pointer.
5133 QualType QType = DE->getType()
5134 .getNonReferenceType()
5135 .getUnqualifiedType()
5136 .getCanonicalType();
5137 const Type *Ty = QType.getTypePtrOrNull();
5138 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
5139 !Ty->isPointerType())) {
5140 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
5141 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
5142 bool IsDecl =
5143 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5144 Diag(VD->getLocation(),
5145 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5146 << VD;
5147 continue;
5148 }
5149
5150 // OpenMP [2.8.1, simd construct, Restrictions]
5151 // A list-item cannot appear in more than one aligned clause.
5152 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
5153 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
5154 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
5155 << getOpenMPClauseName(OMPC_aligned);
5156 continue;
5157 }
5158
5159 Vars.push_back(DE);
5160 }
5161
5162 // OpenMP [2.8.1, simd construct, Description]
5163 // The parameter of the aligned clause, alignment, must be a constant
5164 // positive integer expression.
5165 // If no optional parameter is specified, implementation-defined default
5166 // alignments for SIMD instructions on the target platforms are assumed.
5167 if (Alignment != nullptr) {
5168 ExprResult AlignResult =
5169 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
5170 if (AlignResult.isInvalid())
5171 return nullptr;
5172 Alignment = AlignResult.get();
5173 }
5174 if (Vars.empty())
5175 return nullptr;
5176
5177 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
5178 EndLoc, Vars, Alignment);
5179}
5180
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005181OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
5182 SourceLocation StartLoc,
5183 SourceLocation LParenLoc,
5184 SourceLocation EndLoc) {
5185 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00005186 for (auto &RefExpr : VarList) {
5187 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
5188 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005189 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00005190 Vars.push_back(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005191 continue;
5192 }
5193
Alexey Bataeved09d242014-05-28 05:53:51 +00005194 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005195 // OpenMP [2.1, C/C++]
5196 // A list item is a variable name.
5197 // OpenMP [2.14.4.1, Restrictions, p.1]
5198 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00005199 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005200 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005201 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005202 continue;
5203 }
5204
5205 Decl *D = DE->getDecl();
5206 VarDecl *VD = cast<VarDecl>(D);
5207
5208 QualType Type = VD->getType();
5209 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5210 // It will be analyzed later.
5211 Vars.push_back(DE);
5212 continue;
5213 }
5214
5215 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
5216 // A list item that appears in a copyin clause must be threadprivate.
5217 if (!DSAStack->isThreadPrivate(VD)) {
5218 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00005219 << getOpenMPClauseName(OMPC_copyin)
5220 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005221 continue;
5222 }
5223
5224 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
5225 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00005226 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005227 // operator for the class type.
5228 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005229 CXXRecordDecl *RD =
5230 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00005231 // FIXME This code must be replaced by actual assignment of the
5232 // threadprivate variable.
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005233 if (RD) {
5234 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
5235 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00005236 if (MD) {
5237 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
5238 MD->isDeleted()) {
5239 Diag(ELoc, diag::err_omp_required_method)
5240 << getOpenMPClauseName(OMPC_copyin) << 2;
5241 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
5242 VarDecl::DeclarationOnly;
5243 Diag(VD->getLocation(),
5244 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5245 << VD;
5246 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
5247 continue;
5248 }
5249 MarkFunctionReferenced(ELoc, MD);
5250 DiagnoseUseOfDecl(MD, ELoc);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005251 }
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005252 }
5253
5254 DSAStack->addDSA(VD, DE, OMPC_copyin);
5255 Vars.push_back(DE);
5256 }
5257
Alexey Bataeved09d242014-05-28 05:53:51 +00005258 if (Vars.empty())
5259 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005260
5261 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
5262}
5263
Alexey Bataevbae9a792014-06-27 10:37:06 +00005264OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
5265 SourceLocation StartLoc,
5266 SourceLocation LParenLoc,
5267 SourceLocation EndLoc) {
5268 SmallVector<Expr *, 8> Vars;
5269 for (auto &RefExpr : VarList) {
5270 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
5271 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
5272 // It will be analyzed later.
5273 Vars.push_back(RefExpr);
5274 continue;
5275 }
5276
5277 SourceLocation ELoc = RefExpr->getExprLoc();
5278 // OpenMP [2.1, C/C++]
5279 // A list item is a variable name.
5280 // OpenMP [2.14.4.1, Restrictions, p.1]
5281 // A list item that appears in a copyin clause must be threadprivate.
5282 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
5283 if (!DE || !isa<VarDecl>(DE->getDecl())) {
5284 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
5285 continue;
5286 }
5287
5288 Decl *D = DE->getDecl();
5289 VarDecl *VD = cast<VarDecl>(D);
5290
5291 QualType Type = VD->getType();
5292 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5293 // It will be analyzed later.
5294 Vars.push_back(DE);
5295 continue;
5296 }
5297
5298 // OpenMP [2.14.4.2, Restrictions, p.2]
5299 // A list item that appears in a copyprivate clause may not appear in a
5300 // private or firstprivate clause on the single construct.
5301 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005302 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00005303 if (DVar.CKind != OMPC_copyprivate && DVar.CKind != OMPC_unknown &&
5304 !(DVar.CKind == OMPC_private && !DVar.RefExpr)) {
5305 Diag(ELoc, diag::err_omp_wrong_dsa)
5306 << getOpenMPClauseName(DVar.CKind)
5307 << getOpenMPClauseName(OMPC_copyprivate);
5308 ReportOriginalDSA(*this, DSAStack, VD, DVar);
5309 continue;
5310 }
5311
5312 // OpenMP [2.11.4.2, Restrictions, p.1]
5313 // All list items that appear in a copyprivate clause must be either
5314 // threadprivate or private in the enclosing context.
5315 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005316 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00005317 if (DVar.CKind == OMPC_shared) {
5318 Diag(ELoc, diag::err_omp_required_access)
5319 << getOpenMPClauseName(OMPC_copyprivate)
5320 << "threadprivate or private in the enclosing context";
5321 ReportOriginalDSA(*this, DSAStack, VD, DVar);
5322 continue;
5323 }
5324 }
5325 }
5326
5327 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
5328 // A variable of class type (or array thereof) that appears in a
5329 // copyin clause requires an accessible, unambiguous copy assignment
5330 // operator for the class type.
5331 Type = Context.getBaseElementType(Type);
5332 CXXRecordDecl *RD =
5333 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
5334 // FIXME This code must be replaced by actual assignment of the
5335 // threadprivate variable.
5336 if (RD) {
5337 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
5338 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
5339 if (MD) {
5340 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
5341 MD->isDeleted()) {
5342 Diag(ELoc, diag::err_omp_required_method)
5343 << getOpenMPClauseName(OMPC_copyprivate) << 2;
5344 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
5345 VarDecl::DeclarationOnly;
5346 Diag(VD->getLocation(),
5347 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5348 << VD;
5349 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
5350 continue;
5351 }
5352 MarkFunctionReferenced(ELoc, MD);
5353 DiagnoseUseOfDecl(MD, ELoc);
5354 }
5355 }
5356
5357 // No need to mark vars as copyprivate, they are already threadprivate or
5358 // implicitly private.
5359 Vars.push_back(DE);
5360 }
5361
5362 if (Vars.empty())
5363 return nullptr;
5364
5365 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
5366}
5367
Alexey Bataev6125da92014-07-21 11:26:11 +00005368OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
5369 SourceLocation StartLoc,
5370 SourceLocation LParenLoc,
5371 SourceLocation EndLoc) {
5372 if (VarList.empty())
5373 return nullptr;
5374
5375 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
5376}
Alexey Bataevdea47612014-07-23 07:46:59 +00005377