blob: 8dbf52fedb4a5107a827d39073615407376ad4e3 [file] [log] [blame]
Alexey Bataev9959db52014-05-06 10:08:46 +00001//===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===//
Alexey Bataeva769e072013-03-22 06:34:35 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10/// \brief This file implements semantic analysis for OpenMP directives and
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000011/// clauses.
Alexey Bataeva769e072013-03-22 06:34:35 +000012///
13//===----------------------------------------------------------------------===//
14
Alexey Bataev9959db52014-05-06 10:08:46 +000015#include "clang/AST/ASTContext.h"
Alexey Bataev97720002014-11-11 04:05:39 +000016#include "clang/AST/ASTMutationListener.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000017#include "clang/AST/Decl.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000018#include "clang/AST/DeclCXX.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000019#include "clang/AST/DeclOpenMP.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000020#include "clang/AST/StmtCXX.h"
21#include "clang/AST/StmtOpenMP.h"
22#include "clang/AST/StmtVisitor.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000023#include "clang/Basic/OpenMPKinds.h"
24#include "clang/Lex/Preprocessor.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000025#include "clang/Sema/Initialization.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000026#include "clang/Sema/Lookup.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000027#include "clang/Sema/Scope.h"
28#include "clang/Sema/ScopeInfo.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000029#include "clang/Sema/SemaInternal.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000030using namespace clang;
31
Alexey Bataev758e55e2013-09-06 18:03:48 +000032//===----------------------------------------------------------------------===//
33// Stack of data-sharing attributes for variables
34//===----------------------------------------------------------------------===//
35
36namespace {
37/// \brief Default data sharing attributes, which can be applied to directive.
38enum DefaultDataSharingAttributes {
Alexey Bataeved09d242014-05-28 05:53:51 +000039 DSA_unspecified = 0, /// \brief Data sharing attribute not specified.
40 DSA_none = 1 << 0, /// \brief Default data sharing attribute 'none'.
41 DSA_shared = 1 << 1 /// \brief Default data sharing attribute 'shared'.
Alexey Bataev758e55e2013-09-06 18:03:48 +000042};
Alexey Bataev7ff55242014-06-19 09:13:45 +000043
Alexey Bataevf29276e2014-06-18 04:14:57 +000044template <class T> struct MatchesAny {
Alexey Bataev23b69422014-06-18 07:08:49 +000045 explicit MatchesAny(ArrayRef<T> Arr) : Arr(std::move(Arr)) {}
Alexey Bataevf29276e2014-06-18 04:14:57 +000046 bool operator()(T Kind) {
47 for (auto KindEl : Arr)
48 if (KindEl == Kind)
49 return true;
50 return false;
51 }
52
53private:
54 ArrayRef<T> Arr;
55};
Alexey Bataev23b69422014-06-18 07:08:49 +000056struct MatchesAlways {
Alexey Bataevf29276e2014-06-18 04:14:57 +000057 MatchesAlways() {}
Alexey Bataev7ff55242014-06-19 09:13:45 +000058 template <class T> bool operator()(T) { return true; }
Alexey Bataevf29276e2014-06-18 04:14:57 +000059};
60
61typedef MatchesAny<OpenMPClauseKind> MatchesAnyClause;
62typedef MatchesAny<OpenMPDirectiveKind> MatchesAnyDirective;
Alexey Bataev758e55e2013-09-06 18:03:48 +000063
64/// \brief Stack for tracking declarations used in OpenMP directives and
65/// clauses and their data-sharing attributes.
66class DSAStackTy {
67public:
68 struct DSAVarData {
69 OpenMPDirectiveKind DKind;
70 OpenMPClauseKind CKind;
71 DeclRefExpr *RefExpr;
Alexey Bataevbae9a792014-06-27 10:37:06 +000072 SourceLocation ImplicitDSALoc;
73 DSAVarData()
74 : DKind(OMPD_unknown), CKind(OMPC_unknown), RefExpr(nullptr),
75 ImplicitDSALoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000076 };
Alexey Bataeved09d242014-05-28 05:53:51 +000077
Alexey Bataev758e55e2013-09-06 18:03:48 +000078private:
79 struct DSAInfo {
80 OpenMPClauseKind Attributes;
81 DeclRefExpr *RefExpr;
82 };
83 typedef llvm::SmallDenseMap<VarDecl *, DSAInfo, 64> DeclSAMapTy;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000084 typedef llvm::SmallDenseMap<VarDecl *, DeclRefExpr *, 64> AlignedMapTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +000085
86 struct SharingMapTy {
87 DeclSAMapTy SharingMap;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000088 AlignedMapTy AlignedMap;
Alexey Bataev758e55e2013-09-06 18:03:48 +000089 DefaultDataSharingAttributes DefaultAttr;
Alexey Bataevbae9a792014-06-27 10:37:06 +000090 SourceLocation DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +000091 OpenMPDirectiveKind Directive;
92 DeclarationNameInfo DirectiveName;
93 Scope *CurScope;
Alexey Bataevbae9a792014-06-27 10:37:06 +000094 SourceLocation ConstructLoc;
Alexey Bataev9fb6e642014-07-22 06:45:04 +000095 bool OrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +000096 SourceLocation InnerTeamsRegionLoc;
Alexey Bataeved09d242014-05-28 05:53:51 +000097 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataevbae9a792014-06-27 10:37:06 +000098 Scope *CurScope, SourceLocation Loc)
Alexander Musmanf0d76e72014-05-29 14:36:25 +000099 : SharingMap(), AlignedMap(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000100 Directive(DKind), DirectiveName(std::move(Name)), CurScope(CurScope),
Alexey Bataev13314bf2014-10-09 04:18:56 +0000101 ConstructLoc(Loc), OrderedRegion(false), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000102 SharingMapTy()
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000103 : SharingMap(), AlignedMap(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000104 Directive(OMPD_unknown), DirectiveName(), CurScope(nullptr),
Alexey Bataev13314bf2014-10-09 04:18:56 +0000105 ConstructLoc(), OrderedRegion(false), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000106 };
107
108 typedef SmallVector<SharingMapTy, 64> StackTy;
109
110 /// \brief Stack of used declaration and their data-sharing attributes.
111 StackTy Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000112 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000113
114 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
115
116 DSAVarData getDSA(StackTy::reverse_iterator Iter, VarDecl *D);
Alexey Bataevec3da872014-01-31 05:15:34 +0000117
118 /// \brief Checks if the variable is a local for OpenMP region.
119 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
Alexey Bataeved09d242014-05-28 05:53:51 +0000120
Alexey Bataev758e55e2013-09-06 18:03:48 +0000121public:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000122 explicit DSAStackTy(Sema &S) : Stack(1), SemaRef(S) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000123
124 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000125 Scope *CurScope, SourceLocation Loc) {
126 Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc));
127 Stack.back().DefaultAttrLoc = Loc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000128 }
129
130 void pop() {
131 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
132 Stack.pop_back();
133 }
134
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000135 /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000136 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000137 /// for diagnostics.
138 DeclRefExpr *addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE);
139
Alexey Bataev758e55e2013-09-06 18:03:48 +0000140 /// \brief Adds explicit data sharing attribute to the specified declaration.
141 void addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A);
142
Alexey Bataev758e55e2013-09-06 18:03:48 +0000143 /// \brief Returns data sharing attributes from top of the stack for the
144 /// specified declaration.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000145 DSAVarData getTopDSA(VarDecl *D, bool FromParent);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000146 /// \brief Returns data-sharing attributes for the specified declaration.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000147 DSAVarData getImplicitDSA(VarDecl *D, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000148 /// \brief Checks if the specified variables has data-sharing attributes which
149 /// match specified \a CPred predicate in any directive which matches \a DPred
150 /// predicate.
151 template <class ClausesPredicate, class DirectivesPredicate>
152 DSAVarData hasDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000153 DirectivesPredicate DPred, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000154 /// \brief Checks if the specified variables has data-sharing attributes which
155 /// match specified \a CPred predicate in any innermost directive which
156 /// matches \a DPred predicate.
157 template <class ClausesPredicate, class DirectivesPredicate>
158 DSAVarData hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000159 DirectivesPredicate DPred,
160 bool FromParent);
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000161 /// \brief Finds a directive which matches specified \a DPred predicate.
162 template <class NamedDirectivesPredicate>
163 bool hasDirective(NamedDirectivesPredicate DPred, bool FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000164
Alexey Bataev758e55e2013-09-06 18:03:48 +0000165 /// \brief Returns currently analyzed directive.
166 OpenMPDirectiveKind getCurrentDirective() const {
167 return Stack.back().Directive;
168 }
Alexey Bataev549210e2014-06-24 04:39:47 +0000169 /// \brief Returns parent directive.
170 OpenMPDirectiveKind getParentDirective() const {
171 if (Stack.size() > 2)
172 return Stack[Stack.size() - 2].Directive;
173 return OMPD_unknown;
174 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000175
176 /// \brief Set default data sharing attribute to none.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000177 void setDefaultDSANone(SourceLocation Loc) {
178 Stack.back().DefaultAttr = DSA_none;
179 Stack.back().DefaultAttrLoc = Loc;
180 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000181 /// \brief Set default data sharing attribute to shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000182 void setDefaultDSAShared(SourceLocation Loc) {
183 Stack.back().DefaultAttr = DSA_shared;
184 Stack.back().DefaultAttrLoc = Loc;
185 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000186
187 DefaultDataSharingAttributes getDefaultDSA() const {
188 return Stack.back().DefaultAttr;
189 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000190 SourceLocation getDefaultDSALocation() const {
191 return Stack.back().DefaultAttrLoc;
192 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000193
Alexey Bataevf29276e2014-06-18 04:14:57 +0000194 /// \brief Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000195 bool isThreadPrivate(VarDecl *D) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000196 DSAVarData DVar = getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000197 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000198 }
199
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000200 /// \brief Marks current region as ordered (it has an 'ordered' clause).
201 void setOrderedRegion(bool IsOrdered = true) {
202 Stack.back().OrderedRegion = IsOrdered;
203 }
204 /// \brief Returns true, if parent region is ordered (has associated
205 /// 'ordered' clause), false - otherwise.
206 bool isParentOrderedRegion() const {
207 if (Stack.size() > 2)
208 return Stack[Stack.size() - 2].OrderedRegion;
209 return false;
210 }
211
Alexey Bataev13314bf2014-10-09 04:18:56 +0000212 /// \brief Marks current target region as one with closely nested teams
213 /// region.
214 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
215 if (Stack.size() > 2)
216 Stack[Stack.size() - 2].InnerTeamsRegionLoc = TeamsRegionLoc;
217 }
218 /// \brief Returns true, if current region has closely nested teams region.
219 bool hasInnerTeamsRegion() const {
220 return getInnerTeamsRegionLoc().isValid();
221 }
222 /// \brief Returns location of the nested teams region (if any).
223 SourceLocation getInnerTeamsRegionLoc() const {
224 if (Stack.size() > 1)
225 return Stack.back().InnerTeamsRegionLoc;
226 return SourceLocation();
227 }
228
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000229 Scope *getCurScope() const { return Stack.back().CurScope; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000230 Scope *getCurScope() { return Stack.back().CurScope; }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000231 SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000232};
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000233bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
234 return isOpenMPParallelDirective(DKind) || DKind == OMPD_task ||
Alexey Bataev13314bf2014-10-09 04:18:56 +0000235 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000236}
Alexey Bataeved09d242014-05-28 05:53:51 +0000237} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000238
239DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter,
240 VarDecl *D) {
241 DSAVarData DVar;
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000242 if (Iter == std::prev(Stack.rend())) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000243 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
244 // in a region but not in construct]
245 // File-scope or namespace-scope variables referenced in called routines
246 // in the region are shared unless they appear in a threadprivate
247 // directive.
Alexey Bataev8b9cb982014-07-24 02:33:58 +0000248 if (!D->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D))
Alexey Bataev750a58b2014-03-18 12:19:12 +0000249 DVar.CKind = OMPC_shared;
250
251 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
252 // in a region but not in construct]
253 // Variables with static storage duration that are declared in called
254 // routines in the region are shared.
255 if (D->hasGlobalStorage())
256 DVar.CKind = OMPC_shared;
257
Alexey Bataev758e55e2013-09-06 18:03:48 +0000258 return DVar;
259 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000260
Alexey Bataev758e55e2013-09-06 18:03:48 +0000261 DVar.DKind = Iter->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +0000262 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
263 // in a Construct, C/C++, predetermined, p.1]
264 // Variables with automatic storage duration that are declared in a scope
265 // inside the construct are private.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000266 if (isOpenMPLocal(D, Iter) && D->isLocalVarDecl() &&
267 (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) {
268 DVar.CKind = OMPC_private;
269 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000270 }
271
Alexey Bataev758e55e2013-09-06 18:03:48 +0000272 // Explicitly specified attributes and local variables with predetermined
273 // attributes.
274 if (Iter->SharingMap.count(D)) {
275 DVar.RefExpr = Iter->SharingMap[D].RefExpr;
276 DVar.CKind = Iter->SharingMap[D].Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000277 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000278 return DVar;
279 }
280
281 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
282 // in a Construct, C/C++, implicitly determined, p.1]
283 // In a parallel or task construct, the data-sharing attributes of these
284 // variables are determined by the default clause, if present.
285 switch (Iter->DefaultAttr) {
286 case DSA_shared:
287 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000288 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000289 return DVar;
290 case DSA_none:
291 return DVar;
292 case DSA_unspecified:
293 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
294 // in a Construct, implicitly determined, p.2]
295 // In a parallel construct, if no default clause is present, these
296 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000297 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000298 if (isOpenMPParallelDirective(DVar.DKind) ||
299 isOpenMPTeamsDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000300 DVar.CKind = OMPC_shared;
301 return DVar;
302 }
303
304 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
305 // in a Construct, implicitly determined, p.4]
306 // In a task construct, if no default clause is present, a variable that in
307 // the enclosing context is determined to be shared by all implicit tasks
308 // bound to the current team is shared.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000309 if (DVar.DKind == OMPD_task) {
310 DSAVarData DVarTemp;
Benjamin Kramer167e9992014-03-02 12:20:24 +0000311 for (StackTy::reverse_iterator I = std::next(Iter),
312 EE = std::prev(Stack.rend());
Alexey Bataev758e55e2013-09-06 18:03:48 +0000313 I != EE; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000314 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
315 // Referenced
Alexey Bataev758e55e2013-09-06 18:03:48 +0000316 // in a Construct, implicitly determined, p.6]
317 // In a task construct, if no default clause is present, a variable
318 // whose data-sharing attribute is not determined by the rules above is
319 // firstprivate.
320 DVarTemp = getDSA(I, D);
321 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000322 DVar.RefExpr = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000323 DVar.DKind = OMPD_task;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000324 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000325 return DVar;
326 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000327 if (isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000328 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000329 }
330 DVar.DKind = OMPD_task;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000331 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000332 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000333 return DVar;
334 }
335 }
336 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
337 // in a Construct, implicitly determined, p.3]
338 // For constructs other than task, if no default clause is present, these
339 // variables inherit their data-sharing attributes from the enclosing
340 // context.
Benjamin Kramer167e9992014-03-02 12:20:24 +0000341 return getDSA(std::next(Iter), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000342}
343
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000344DeclRefExpr *DSAStackTy::addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE) {
345 assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
346 auto It = Stack.back().AlignedMap.find(D);
347 if (It == Stack.back().AlignedMap.end()) {
348 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
349 Stack.back().AlignedMap[D] = NewDE;
350 return nullptr;
351 } else {
352 assert(It->second && "Unexpected nullptr expr in the aligned map");
353 return It->second;
354 }
355 return nullptr;
356}
357
Alexey Bataev758e55e2013-09-06 18:03:48 +0000358void DSAStackTy::addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A) {
359 if (A == OMPC_threadprivate) {
360 Stack[0].SharingMap[D].Attributes = A;
361 Stack[0].SharingMap[D].RefExpr = E;
362 } else {
363 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
364 Stack.back().SharingMap[D].Attributes = A;
365 Stack.back().SharingMap[D].RefExpr = E;
366 }
367}
368
Alexey Bataeved09d242014-05-28 05:53:51 +0000369bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000370 if (Stack.size() > 2) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000371 reverse_iterator I = Iter, E = std::prev(Stack.rend());
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000372 Scope *TopScope = nullptr;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000373 while (I != E && !isParallelOrTaskRegion(I->Directive)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000374 ++I;
375 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000376 if (I == E)
377 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000378 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000379 Scope *CurScope = getCurScope();
380 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000381 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000382 }
383 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000384 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000385 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000386}
387
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000388DSAStackTy::DSAVarData DSAStackTy::getTopDSA(VarDecl *D, bool FromParent) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000389 DSAVarData DVar;
390
391 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
392 // in a Construct, C/C++, predetermined, p.1]
393 // Variables appearing in threadprivate directives are threadprivate.
394 if (D->getTLSKind() != VarDecl::TLS_None) {
395 DVar.CKind = OMPC_threadprivate;
396 return DVar;
397 }
398 if (Stack[0].SharingMap.count(D)) {
399 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
400 DVar.CKind = OMPC_threadprivate;
401 return DVar;
402 }
403
404 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
405 // in a Construct, C/C++, predetermined, p.1]
406 // Variables with automatic storage duration that are declared in a scope
407 // inside the construct are private.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000408 OpenMPDirectiveKind Kind =
409 FromParent ? getParentDirective() : getCurrentDirective();
410 auto StartI = std::next(Stack.rbegin());
411 auto EndI = std::prev(Stack.rend());
412 if (FromParent && StartI != EndI) {
413 StartI = std::next(StartI);
414 }
415 if (!isParallelOrTaskRegion(Kind)) {
Alexey Bataev8b9cb982014-07-24 02:33:58 +0000416 if (isOpenMPLocal(D, StartI) &&
417 ((D->isLocalVarDecl() && (D->getStorageClass() == SC_Auto ||
418 D->getStorageClass() == SC_None)) ||
419 isa<ParmVarDecl>(D))) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000420 DVar.CKind = OMPC_private;
421 return DVar;
Alexander Musman8dba6642014-04-22 13:09:42 +0000422 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000423 }
424
425 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
426 // in a Construct, C/C++, predetermined, p.4]
Alexey Bataevf29276e2014-06-18 04:14:57 +0000427 // Static data members are shared.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000428 if (D->isStaticDataMember()) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000429 // Variables with const-qualified type having no mutable member may be
Alexey Bataevf29276e2014-06-18 04:14:57 +0000430 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000431 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
432 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000433 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
434 return DVar;
435
Alexey Bataev758e55e2013-09-06 18:03:48 +0000436 DVar.CKind = OMPC_shared;
437 return DVar;
438 }
439
440 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataev7ff55242014-06-19 09:13:45 +0000441 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
Alexey Bataev758e55e2013-09-06 18:03:48 +0000442 while (Type->isArrayType()) {
443 QualType ElemType = cast<ArrayType>(Type.getTypePtr())->getElementType();
444 Type = ElemType.getNonReferenceType().getCanonicalType();
445 }
446 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
447 // in a Construct, C/C++, predetermined, p.6]
448 // Variables with const qualified type having no mutable member are
449 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000450 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000451 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000452 if (IsConstant &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000453 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000454 // Variables with const-qualified type having no mutable member may be
455 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000456 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
457 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000458 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
459 return DVar;
460
Alexey Bataev758e55e2013-09-06 18:03:48 +0000461 DVar.CKind = OMPC_shared;
462 return DVar;
463 }
464
465 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
466 // in a Construct, C/C++, predetermined, p.7]
467 // Variables with static storage duration that are declared in a scope
468 // inside the construct are shared.
Alexey Bataevec3da872014-01-31 05:15:34 +0000469 if (D->isStaticLocal()) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000470 DVar.CKind = OMPC_shared;
471 return DVar;
472 }
473
474 // Explicitly specified attributes and local variables with predetermined
475 // attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000476 auto I = std::prev(StartI);
477 if (I->SharingMap.count(D)) {
478 DVar.RefExpr = I->SharingMap[D].RefExpr;
479 DVar.CKind = I->SharingMap[D].Attributes;
480 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000481 }
482
483 return DVar;
484}
485
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000486DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D, bool FromParent) {
487 auto StartI = Stack.rbegin();
488 auto EndI = std::prev(Stack.rend());
489 if (FromParent && StartI != EndI) {
490 StartI = std::next(StartI);
491 }
492 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000493}
494
Alexey Bataevf29276e2014-06-18 04:14:57 +0000495template <class ClausesPredicate, class DirectivesPredicate>
496DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000497 DirectivesPredicate DPred,
498 bool FromParent) {
499 auto StartI = std::next(Stack.rbegin());
500 auto EndI = std::prev(Stack.rend());
501 if (FromParent && StartI != EndI) {
502 StartI = std::next(StartI);
503 }
504 for (auto I = StartI, EE = EndI; I != EE; ++I) {
505 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000506 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000507 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000508 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000509 return DVar;
510 }
511 return DSAVarData();
512}
513
Alexey Bataevf29276e2014-06-18 04:14:57 +0000514template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000515DSAStackTy::DSAVarData
516DSAStackTy::hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
517 DirectivesPredicate DPred, bool FromParent) {
518 auto StartI = std::next(Stack.rbegin());
519 auto EndI = std::prev(Stack.rend());
520 if (FromParent && StartI != EndI) {
521 StartI = std::next(StartI);
522 }
523 for (auto I = StartI, EE = EndI; I != EE; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000524 if (!DPred(I->Directive))
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000525 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000526 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000527 if (CPred(DVar.CKind))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000528 return DVar;
529 return DSAVarData();
530 }
531 return DSAVarData();
532}
533
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000534template <class NamedDirectivesPredicate>
535bool DSAStackTy::hasDirective(NamedDirectivesPredicate DPred, bool FromParent) {
536 auto StartI = std::next(Stack.rbegin());
537 auto EndI = std::prev(Stack.rend());
538 if (FromParent && StartI != EndI) {
539 StartI = std::next(StartI);
540 }
541 for (auto I = StartI, EE = EndI; I != EE; ++I) {
542 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
543 return true;
544 }
545 return false;
546}
547
Alexey Bataev758e55e2013-09-06 18:03:48 +0000548void Sema::InitDataSharingAttributesStack() {
549 VarDataSharingAttributesStack = new DSAStackTy(*this);
550}
551
552#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
553
Alexey Bataevf841bd92014-12-16 07:00:22 +0000554bool Sema::IsOpenMPCapturedVar(VarDecl *VD) {
555 assert(LangOpts.OpenMP && "OpenMP is not allowed");
556 if (DSAStack->getCurrentDirective() != OMPD_unknown) {
557 auto DVarPrivate = DSAStack->getTopDSA(VD, /*FromParent=*/false);
558 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
559 return true;
560 DVarPrivate = DSAStack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(),
561 /*FromParent=*/false);
562 return DVarPrivate.CKind != OMPC_unknown;
563 }
564 return false;
565}
566
Alexey Bataeved09d242014-05-28 05:53:51 +0000567void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000568
569void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
570 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000571 Scope *CurScope, SourceLocation Loc) {
572 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000573 PushExpressionEvaluationContext(PotentiallyEvaluated);
574}
575
576void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000577 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
578 // A variable of class type (or array thereof) that appears in a lastprivate
579 // clause requires an accessible, unambiguous default constructor for the
580 // class type, unless the list item is also specified in a firstprivate
581 // clause.
582 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
583 for (auto C : D->clauses()) {
584 if (auto Clause = dyn_cast<OMPLastprivateClause>(C)) {
585 for (auto VarRef : Clause->varlists()) {
586 if (VarRef->isValueDependent() || VarRef->isTypeDependent())
587 continue;
588 auto VD = cast<VarDecl>(cast<DeclRefExpr>(VarRef)->getDecl());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000589 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000590 if (DVar.CKind == OMPC_lastprivate) {
591 SourceLocation ELoc = VarRef->getExprLoc();
592 auto Type = VarRef->getType();
593 if (Type->isArrayType())
594 Type = QualType(Type->getArrayElementTypeNoTypeQual(), 0);
595 CXXRecordDecl *RD =
Alexey Bataev23b69422014-06-18 07:08:49 +0000596 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
597 // FIXME This code must be replaced by actual constructing of the
598 // lastprivate variable.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000599 if (RD) {
600 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
601 PartialDiagnostic PD =
602 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
603 if (!CD ||
604 CheckConstructorAccess(
605 ELoc, CD, InitializedEntity::InitializeTemporary(Type),
606 CD->getAccess(), PD) == AR_inaccessible ||
607 CD->isDeleted()) {
608 Diag(ELoc, diag::err_omp_required_method)
609 << getOpenMPClauseName(OMPC_lastprivate) << 0;
610 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
611 VarDecl::DeclarationOnly;
612 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl
613 : diag::note_defined_here)
614 << VD;
615 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
616 continue;
617 }
618 MarkFunctionReferenced(ELoc, CD);
619 DiagnoseUseOfDecl(CD, ELoc);
620 }
621 }
622 }
623 }
624 }
625 }
626
Alexey Bataev758e55e2013-09-06 18:03:48 +0000627 DSAStack->pop();
628 DiscardCleanupsInEvaluationContext();
629 PopExpressionEvaluationContext();
630}
631
Alexey Bataeva769e072013-03-22 06:34:35 +0000632namespace {
633
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000634class VarDeclFilterCCC : public CorrectionCandidateCallback {
635private:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000636 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +0000637
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000638public:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000639 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +0000640 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000641 NamedDecl *ND = Candidate.getCorrectionDecl();
642 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
643 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000644 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
645 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +0000646 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000647 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000648 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000649};
Alexey Bataeved09d242014-05-28 05:53:51 +0000650} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000651
652ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
653 CXXScopeSpec &ScopeSpec,
654 const DeclarationNameInfo &Id) {
655 LookupResult Lookup(*this, Id, LookupOrdinaryName);
656 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
657
658 if (Lookup.isAmbiguous())
659 return ExprError();
660
661 VarDecl *VD;
662 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +0000663 if (TypoCorrection Corrected = CorrectTypo(
664 Id, LookupOrdinaryName, CurScope, nullptr,
665 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +0000666 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000667 PDiag(Lookup.empty()
668 ? diag::err_undeclared_var_use_suggest
669 : diag::err_omp_expected_var_arg_suggest)
670 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +0000671 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000672 } else {
Richard Smithf9b15102013-08-17 00:46:16 +0000673 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
674 : diag::err_omp_expected_var_arg)
675 << Id.getName();
676 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000677 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000678 } else {
679 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000680 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000681 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
682 return ExprError();
683 }
684 }
685 Lookup.suppressDiagnostics();
686
687 // OpenMP [2.9.2, Syntax, C/C++]
688 // Variables must be file-scope, namespace-scope, or static block-scope.
689 if (!VD->hasGlobalStorage()) {
690 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000691 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
692 bool IsDecl =
693 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000694 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +0000695 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
696 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000697 return ExprError();
698 }
699
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000700 VarDecl *CanonicalVD = VD->getCanonicalDecl();
701 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000702 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
703 // A threadprivate directive for file-scope variables must appear outside
704 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000705 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
706 !getCurLexicalContext()->isTranslationUnit()) {
707 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000708 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
709 bool IsDecl =
710 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
711 Diag(VD->getLocation(),
712 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
713 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000714 return ExprError();
715 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000716 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
717 // A threadprivate directive for static class member variables must appear
718 // in the class definition, in the same scope in which the member
719 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000720 if (CanonicalVD->isStaticDataMember() &&
721 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
722 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000723 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
724 bool IsDecl =
725 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
726 Diag(VD->getLocation(),
727 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
728 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000729 return ExprError();
730 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000731 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
732 // A threadprivate directive for namespace-scope variables must appear
733 // outside any definition or declaration other than the namespace
734 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000735 if (CanonicalVD->getDeclContext()->isNamespace() &&
736 (!getCurLexicalContext()->isFileContext() ||
737 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
738 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000739 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
740 bool IsDecl =
741 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
742 Diag(VD->getLocation(),
743 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
744 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000745 return ExprError();
746 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000747 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
748 // A threadprivate directive for static block-scope variables must appear
749 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000750 if (CanonicalVD->isStaticLocal() && CurScope &&
751 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000752 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000753 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
754 bool IsDecl =
755 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
756 Diag(VD->getLocation(),
757 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
758 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000759 return ExprError();
760 }
761
762 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
763 // A threadprivate directive must lexically precede all references to any
764 // of the variables in its list.
765 if (VD->isUsed()) {
766 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +0000767 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000768 return ExprError();
769 }
770
771 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataevd178ad42014-03-07 08:03:37 +0000772 ExprResult DE = BuildDeclRefExpr(VD, ExprType, VK_LValue, Id.getLoc());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000773 return DE;
774}
775
Alexey Bataeved09d242014-05-28 05:53:51 +0000776Sema::DeclGroupPtrTy
777Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
778 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000779 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000780 CurContext->addDecl(D);
781 return DeclGroupPtrTy::make(DeclGroupRef(D));
782 }
783 return DeclGroupPtrTy();
784}
785
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000786namespace {
787class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
788 Sema &SemaRef;
789
790public:
791 bool VisitDeclRefExpr(const DeclRefExpr *E) {
792 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
793 if (VD->hasLocalStorage()) {
794 SemaRef.Diag(E->getLocStart(),
795 diag::err_omp_local_var_in_threadprivate_init)
796 << E->getSourceRange();
797 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
798 << VD << VD->getSourceRange();
799 return true;
800 }
801 }
802 return false;
803 }
804 bool VisitStmt(const Stmt *S) {
805 for (auto Child : S->children()) {
806 if (Child && Visit(Child))
807 return true;
808 }
809 return false;
810 }
Alexey Bataev23b69422014-06-18 07:08:49 +0000811 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000812};
813} // namespace
814
Alexey Bataeved09d242014-05-28 05:53:51 +0000815OMPThreadPrivateDecl *
816Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000817 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +0000818 for (auto &RefExpr : VarList) {
819 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000820 VarDecl *VD = cast<VarDecl>(DE->getDecl());
821 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +0000822
823 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
824 // A threadprivate variable must not have an incomplete type.
825 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000826 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000827 continue;
828 }
829
830 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
831 // A threadprivate variable must not have a reference type.
832 if (VD->getType()->isReferenceType()) {
833 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000834 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
835 bool IsDecl =
836 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
837 Diag(VD->getLocation(),
838 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
839 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000840 continue;
841 }
842
Richard Smithfd3834f2013-04-13 02:43:54 +0000843 // Check if this is a TLS variable.
844 if (VD->getTLSKind()) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000845 Diag(ILoc, diag::err_omp_var_thread_local) << VD;
Alexey Bataeved09d242014-05-28 05:53:51 +0000846 bool IsDecl =
847 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
848 Diag(VD->getLocation(),
849 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
850 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000851 continue;
852 }
853
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000854 // Check if initial value of threadprivate variable reference variable with
855 // local storage (it is not supported by runtime).
856 if (auto Init = VD->getAnyInitializer()) {
857 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000858 if (Checker.Visit(Init))
859 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000860 }
861
Alexey Bataeved09d242014-05-28 05:53:51 +0000862 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +0000863 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +0000864 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
865 Context, SourceRange(Loc, Loc)));
866 if (auto *ML = Context.getASTMutationListener())
867 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +0000868 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000869 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000870 if (!Vars.empty()) {
871 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
872 Vars);
873 D->setAccess(AS_public);
874 }
875 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +0000876}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000877
Alexey Bataev7ff55242014-06-19 09:13:45 +0000878static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
879 const VarDecl *VD, DSAStackTy::DSAVarData DVar,
880 bool IsLoopIterVar = false) {
881 if (DVar.RefExpr) {
882 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
883 << getOpenMPClauseName(DVar.CKind);
884 return;
885 }
886 enum {
887 PDSA_StaticMemberShared,
888 PDSA_StaticLocalVarShared,
889 PDSA_LoopIterVarPrivate,
890 PDSA_LoopIterVarLinear,
891 PDSA_LoopIterVarLastprivate,
892 PDSA_ConstVarShared,
893 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000894 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000895 PDSA_LocalVarPrivate,
896 PDSA_Implicit
897 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000898 bool ReportHint = false;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000899 auto ReportLoc = VD->getLocation();
Alexey Bataev7ff55242014-06-19 09:13:45 +0000900 if (IsLoopIterVar) {
901 if (DVar.CKind == OMPC_private)
902 Reason = PDSA_LoopIterVarPrivate;
903 else if (DVar.CKind == OMPC_lastprivate)
904 Reason = PDSA_LoopIterVarLastprivate;
905 else
906 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000907 } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) {
908 Reason = PDSA_TaskVarFirstprivate;
909 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000910 } else if (VD->isStaticLocal())
911 Reason = PDSA_StaticLocalVarShared;
912 else if (VD->isStaticDataMember())
913 Reason = PDSA_StaticMemberShared;
914 else if (VD->isFileVarDecl())
915 Reason = PDSA_GlobalVarShared;
916 else if (VD->getType().isConstant(SemaRef.getASTContext()))
917 Reason = PDSA_ConstVarShared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000918 else if (VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +0000919 ReportHint = true;
920 Reason = PDSA_LocalVarPrivate;
921 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000922 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000923 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +0000924 << Reason << ReportHint
925 << getOpenMPDirectiveName(Stack->getCurrentDirective());
926 } else if (DVar.ImplicitDSALoc.isValid()) {
927 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
928 << getOpenMPClauseName(DVar.CKind);
929 }
Alexey Bataev7ff55242014-06-19 09:13:45 +0000930}
931
Alexey Bataev758e55e2013-09-06 18:03:48 +0000932namespace {
933class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
934 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000935 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000936 bool ErrorFound;
937 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000938 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000939 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +0000940
Alexey Bataev758e55e2013-09-06 18:03:48 +0000941public:
942 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000943 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000944 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +0000945 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
946 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000947
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000948 auto DVar = Stack->getTopDSA(VD, false);
949 // Check if the variable has explicit DSA set and stop analysis if it so.
950 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000951
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000952 auto ELoc = E->getExprLoc();
953 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000954 // The default(none) clause requires that each variable that is referenced
955 // in the construct, and does not have a predetermined data-sharing
956 // attribute, must have its data-sharing attribute explicitly determined
957 // by being listed in a data-sharing attribute clause.
958 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000959 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +0000960 VarsWithInheritedDSA.count(VD) == 0) {
961 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000962 return;
963 }
964
965 // OpenMP [2.9.3.6, Restrictions, p.2]
966 // A list item that appears in a reduction clause of the innermost
967 // enclosing worksharing or parallel construct may not be accessed in an
968 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000969 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000970 [](OpenMPDirectiveKind K) -> bool {
971 return isOpenMPParallelDirective(K) ||
Alexey Bataev13314bf2014-10-09 04:18:56 +0000972 isOpenMPWorksharingDirective(K) ||
973 isOpenMPTeamsDirective(K);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000974 },
975 false);
Alexey Bataevc5e02582014-06-16 07:08:35 +0000976 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
977 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000978 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
979 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +0000980 return;
981 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000982
983 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000984 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000985 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000986 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000987 }
988 }
989 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000990 for (auto *C : S->clauses()) {
991 // Skip analysis of arguments of implicitly defined firstprivate clause
992 // for task directives.
993 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
994 for (auto *CC : C->children()) {
995 if (CC)
996 Visit(CC);
997 }
998 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000999 }
1000 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001001 for (auto *C : S->children()) {
1002 if (C && !isa<OMPExecutableDirective>(C))
1003 Visit(C);
1004 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001005 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001006
1007 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001008 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001009 llvm::DenseMap<VarDecl *, Expr *> &getVarsWithInheritedDSA() {
1010 return VarsWithInheritedDSA;
1011 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001012
Alexey Bataev7ff55242014-06-19 09:13:45 +00001013 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1014 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00001015};
Alexey Bataeved09d242014-05-28 05:53:51 +00001016} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001017
Alexey Bataevbae9a792014-06-27 10:37:06 +00001018void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001019 switch (DKind) {
1020 case OMPD_parallel: {
1021 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1022 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001023 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001024 std::make_pair(".global_tid.", KmpInt32PtrTy),
1025 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1026 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001027 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001028 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1029 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001030 break;
1031 }
1032 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001033 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001034 std::make_pair(StringRef(), QualType()) // __context with shared vars
1035 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001036 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1037 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001038 break;
1039 }
1040 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001041 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001042 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001043 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001044 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1045 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001046 break;
1047 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001048 case OMPD_for_simd: {
1049 Sema::CapturedParamNameType Params[] = {
1050 std::make_pair(StringRef(), QualType()) // __context with shared vars
1051 };
1052 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1053 Params);
1054 break;
1055 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001056 case OMPD_sections: {
1057 Sema::CapturedParamNameType Params[] = {
1058 std::make_pair(StringRef(), QualType()) // __context with shared vars
1059 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001060 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1061 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001062 break;
1063 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001064 case OMPD_section: {
1065 Sema::CapturedParamNameType Params[] = {
1066 std::make_pair(StringRef(), QualType()) // __context with shared vars
1067 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001068 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1069 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001070 break;
1071 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001072 case OMPD_single: {
1073 Sema::CapturedParamNameType Params[] = {
1074 std::make_pair(StringRef(), QualType()) // __context with shared vars
1075 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001076 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1077 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001078 break;
1079 }
Alexander Musman80c22892014-07-17 08:54:58 +00001080 case OMPD_master: {
1081 Sema::CapturedParamNameType Params[] = {
1082 std::make_pair(StringRef(), QualType()) // __context with shared vars
1083 };
1084 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1085 Params);
1086 break;
1087 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001088 case OMPD_critical: {
1089 Sema::CapturedParamNameType Params[] = {
1090 std::make_pair(StringRef(), QualType()) // __context with shared vars
1091 };
1092 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1093 Params);
1094 break;
1095 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001096 case OMPD_parallel_for: {
1097 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1098 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1099 Sema::CapturedParamNameType Params[] = {
1100 std::make_pair(".global_tid.", KmpInt32PtrTy),
1101 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1102 std::make_pair(StringRef(), QualType()) // __context with shared vars
1103 };
1104 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1105 Params);
1106 break;
1107 }
Alexander Musmane4e893b2014-09-23 09:33:00 +00001108 case OMPD_parallel_for_simd: {
1109 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1110 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1111 Sema::CapturedParamNameType Params[] = {
1112 std::make_pair(".global_tid.", KmpInt32PtrTy),
1113 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1114 std::make_pair(StringRef(), QualType()) // __context with shared vars
1115 };
1116 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1117 Params);
1118 break;
1119 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001120 case OMPD_parallel_sections: {
1121 Sema::CapturedParamNameType Params[] = {
1122 std::make_pair(StringRef(), QualType()) // __context with shared vars
1123 };
1124 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1125 Params);
1126 break;
1127 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001128 case OMPD_task: {
1129 Sema::CapturedParamNameType Params[] = {
1130 std::make_pair(StringRef(), QualType()) // __context with shared vars
1131 };
1132 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1133 Params);
1134 break;
1135 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001136 case OMPD_ordered: {
1137 Sema::CapturedParamNameType Params[] = {
1138 std::make_pair(StringRef(), QualType()) // __context with shared vars
1139 };
1140 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1141 Params);
1142 break;
1143 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001144 case OMPD_atomic: {
1145 Sema::CapturedParamNameType Params[] = {
1146 std::make_pair(StringRef(), QualType()) // __context with shared vars
1147 };
1148 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1149 Params);
1150 break;
1151 }
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001152 case OMPD_target: {
1153 Sema::CapturedParamNameType Params[] = {
1154 std::make_pair(StringRef(), QualType()) // __context with shared vars
1155 };
1156 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1157 Params);
1158 break;
1159 }
Alexey Bataev13314bf2014-10-09 04:18:56 +00001160 case OMPD_teams: {
1161 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1162 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1163 Sema::CapturedParamNameType Params[] = {
1164 std::make_pair(".global_tid.", KmpInt32PtrTy),
1165 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1166 std::make_pair(StringRef(), QualType()) // __context with shared vars
1167 };
1168 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1169 Params);
1170 break;
1171 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001172 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001173 case OMPD_taskyield:
1174 case OMPD_barrier:
1175 case OMPD_taskwait:
1176 case OMPD_flush:
Alexey Bataev9959db52014-05-06 10:08:46 +00001177 llvm_unreachable("OpenMP Directive is not allowed");
1178 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001179 llvm_unreachable("Unknown OpenMP directive");
1180 }
1181}
1182
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001183static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1184 OpenMPDirectiveKind CurrentRegion,
1185 const DeclarationNameInfo &CurrentName,
1186 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001187 // Allowed nesting of constructs
1188 // +------------------+-----------------+------------------------------------+
1189 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1190 // +------------------+-----------------+------------------------------------+
1191 // | parallel | parallel | * |
1192 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001193 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001194 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001195 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001196 // | parallel | simd | * |
1197 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001198 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001199 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001200 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001201 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001202 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001203 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001204 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001205 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001206 // | parallel | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001207 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001208 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001209 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001210 // | parallel | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001211 // | parallel | teams | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001212 // +------------------+-----------------+------------------------------------+
1213 // | for | parallel | * |
1214 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001215 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001216 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001217 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001218 // | for | simd | * |
1219 // | for | sections | + |
1220 // | for | section | + |
1221 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001222 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001223 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001224 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001225 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001226 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001227 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001228 // | for | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001229 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001230 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001231 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001232 // | for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001233 // | for | teams | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001234 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001235 // | master | parallel | * |
1236 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001237 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001238 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001239 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001240 // | master | simd | * |
1241 // | master | sections | + |
1242 // | master | section | + |
1243 // | master | single | + |
1244 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001245 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001246 // | master |parallel sections| * |
1247 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001248 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001249 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001250 // | master | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001251 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001252 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001253 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001254 // | master | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001255 // | master | teams | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001256 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001257 // | critical | parallel | * |
1258 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001259 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001260 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001261 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001262 // | critical | simd | * |
1263 // | critical | sections | + |
1264 // | critical | section | + |
1265 // | critical | single | + |
1266 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001267 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001268 // | critical |parallel sections| * |
1269 // | critical | task | * |
1270 // | critical | taskyield | * |
1271 // | critical | barrier | + |
1272 // | critical | taskwait | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001273 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001274 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001275 // | critical | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001276 // | critical | teams | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001277 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001278 // | simd | parallel | |
1279 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001280 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00001281 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001282 // | simd | critical | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001283 // | simd | simd | |
1284 // | simd | sections | |
1285 // | simd | section | |
1286 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001287 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001288 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001289 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001290 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00001291 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001292 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001293 // | simd | taskwait | |
Alexey Bataev6125da92014-07-21 11:26:11 +00001294 // | simd | flush | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001295 // | simd | ordered | |
Alexey Bataev0162e452014-07-22 10:10:35 +00001296 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001297 // | simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001298 // | simd | teams | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001299 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00001300 // | for simd | parallel | |
1301 // | for simd | for | |
1302 // | for simd | for simd | |
1303 // | for simd | master | |
1304 // | for simd | critical | |
1305 // | for simd | simd | |
1306 // | for simd | sections | |
1307 // | for simd | section | |
1308 // | for simd | single | |
1309 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001310 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001311 // | for simd |parallel sections| |
1312 // | for simd | task | |
1313 // | for simd | taskyield | |
1314 // | for simd | barrier | |
1315 // | for simd | taskwait | |
1316 // | for simd | flush | |
1317 // | for simd | ordered | |
1318 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001319 // | for simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001320 // | for simd | teams | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001321 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00001322 // | parallel for simd| parallel | |
1323 // | parallel for simd| for | |
1324 // | parallel for simd| for simd | |
1325 // | parallel for simd| master | |
1326 // | parallel for simd| critical | |
1327 // | parallel for simd| simd | |
1328 // | parallel for simd| sections | |
1329 // | parallel for simd| section | |
1330 // | parallel for simd| single | |
1331 // | parallel for simd| parallel for | |
1332 // | parallel for simd|parallel for simd| |
1333 // | parallel for simd|parallel sections| |
1334 // | parallel for simd| task | |
1335 // | parallel for simd| taskyield | |
1336 // | parallel for simd| barrier | |
1337 // | parallel for simd| taskwait | |
1338 // | parallel for simd| flush | |
1339 // | parallel for simd| ordered | |
1340 // | parallel for simd| atomic | |
1341 // | parallel for simd| target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001342 // | parallel for simd| teams | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001343 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001344 // | sections | parallel | * |
1345 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001346 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001347 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001348 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001349 // | sections | simd | * |
1350 // | sections | sections | + |
1351 // | sections | section | * |
1352 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001353 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001354 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001355 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001356 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001357 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001358 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001359 // | sections | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001360 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001361 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001362 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001363 // | sections | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001364 // | sections | teams | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001365 // +------------------+-----------------+------------------------------------+
1366 // | section | parallel | * |
1367 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001368 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001369 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001370 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001371 // | section | simd | * |
1372 // | section | sections | + |
1373 // | section | section | + |
1374 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001375 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001376 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001377 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001378 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001379 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001380 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001381 // | section | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001382 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001383 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001384 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001385 // | section | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001386 // | section | teams | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001387 // +------------------+-----------------+------------------------------------+
1388 // | single | parallel | * |
1389 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001390 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001391 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001392 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001393 // | single | simd | * |
1394 // | single | sections | + |
1395 // | single | section | + |
1396 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001397 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001398 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001399 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001400 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001401 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001402 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001403 // | single | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001404 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001405 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001406 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001407 // | single | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001408 // | single | teams | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001409 // +------------------+-----------------+------------------------------------+
1410 // | parallel for | parallel | * |
1411 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001412 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001413 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001414 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001415 // | parallel for | simd | * |
1416 // | parallel for | sections | + |
1417 // | parallel for | section | + |
1418 // | parallel for | single | + |
1419 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001420 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001421 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001422 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001423 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001424 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001425 // | parallel for | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001426 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001427 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001428 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001429 // | parallel for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001430 // | parallel for | teams | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001431 // +------------------+-----------------+------------------------------------+
1432 // | parallel sections| parallel | * |
1433 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001434 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001435 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001436 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001437 // | parallel sections| simd | * |
1438 // | parallel sections| sections | + |
1439 // | parallel sections| section | * |
1440 // | parallel sections| single | + |
1441 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001442 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001443 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001444 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001445 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001446 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001447 // | parallel sections| taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001448 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001449 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001450 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001451 // | parallel sections| target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001452 // | parallel sections| teams | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001453 // +------------------+-----------------+------------------------------------+
1454 // | task | parallel | * |
1455 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001456 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001457 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001458 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001459 // | task | simd | * |
1460 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001461 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001462 // | task | single | + |
1463 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001464 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001465 // | task |parallel sections| * |
1466 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001467 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001468 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001469 // | task | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001470 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001471 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001472 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001473 // | task | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001474 // | task | teams | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001475 // +------------------+-----------------+------------------------------------+
1476 // | ordered | parallel | * |
1477 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001478 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001479 // | ordered | master | * |
1480 // | ordered | critical | * |
1481 // | ordered | simd | * |
1482 // | ordered | sections | + |
1483 // | ordered | section | + |
1484 // | ordered | single | + |
1485 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001486 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001487 // | ordered |parallel sections| * |
1488 // | ordered | task | * |
1489 // | ordered | taskyield | * |
1490 // | ordered | barrier | + |
1491 // | ordered | taskwait | * |
1492 // | ordered | flush | * |
1493 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001494 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001495 // | ordered | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001496 // | ordered | teams | + |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001497 // +------------------+-----------------+------------------------------------+
1498 // | atomic | parallel | |
1499 // | atomic | for | |
1500 // | atomic | for simd | |
1501 // | atomic | master | |
1502 // | atomic | critical | |
1503 // | atomic | simd | |
1504 // | atomic | sections | |
1505 // | atomic | section | |
1506 // | atomic | single | |
1507 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001508 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001509 // | atomic |parallel sections| |
1510 // | atomic | task | |
1511 // | atomic | taskyield | |
1512 // | atomic | barrier | |
1513 // | atomic | taskwait | |
1514 // | atomic | flush | |
1515 // | atomic | ordered | |
1516 // | atomic | atomic | |
1517 // | atomic | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001518 // | atomic | teams | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001519 // +------------------+-----------------+------------------------------------+
1520 // | target | parallel | * |
1521 // | target | for | * |
1522 // | target | for simd | * |
1523 // | target | master | * |
1524 // | target | critical | * |
1525 // | target | simd | * |
1526 // | target | sections | * |
1527 // | target | section | * |
1528 // | target | single | * |
1529 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001530 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001531 // | target |parallel sections| * |
1532 // | target | task | * |
1533 // | target | taskyield | * |
1534 // | target | barrier | * |
1535 // | target | taskwait | * |
1536 // | target | flush | * |
1537 // | target | ordered | * |
1538 // | target | atomic | * |
1539 // | target | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001540 // | target | teams | * |
1541 // +------------------+-----------------+------------------------------------+
1542 // | teams | parallel | * |
1543 // | teams | for | + |
1544 // | teams | for simd | + |
1545 // | teams | master | + |
1546 // | teams | critical | + |
1547 // | teams | simd | + |
1548 // | teams | sections | + |
1549 // | teams | section | + |
1550 // | teams | single | + |
1551 // | teams | parallel for | * |
1552 // | teams |parallel for simd| * |
1553 // | teams |parallel sections| * |
1554 // | teams | task | + |
1555 // | teams | taskyield | + |
1556 // | teams | barrier | + |
1557 // | teams | taskwait | + |
1558 // | teams | flush | + |
1559 // | teams | ordered | + |
1560 // | teams | atomic | + |
1561 // | teams | target | + |
1562 // | teams | teams | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001563 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00001564 if (Stack->getCurScope()) {
1565 auto ParentRegion = Stack->getParentDirective();
1566 bool NestingProhibited = false;
1567 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001568 enum {
1569 NoRecommend,
1570 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00001571 ShouldBeInOrderedRegion,
1572 ShouldBeInTargetRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001573 } Recommend = NoRecommend;
Alexey Bataev549210e2014-06-24 04:39:47 +00001574 if (isOpenMPSimdDirective(ParentRegion)) {
1575 // OpenMP [2.16, Nesting of Regions]
1576 // OpenMP constructs may not be nested inside a simd region.
1577 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
1578 return true;
1579 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001580 if (ParentRegion == OMPD_atomic) {
1581 // OpenMP [2.16, Nesting of Regions]
1582 // OpenMP constructs may not be nested inside an atomic region.
1583 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
1584 return true;
1585 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001586 if (CurrentRegion == OMPD_section) {
1587 // OpenMP [2.7.2, sections Construct, Restrictions]
1588 // Orphaned section directives are prohibited. That is, the section
1589 // directives must appear within the sections construct and must not be
1590 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001591 if (ParentRegion != OMPD_sections &&
1592 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001593 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
1594 << (ParentRegion != OMPD_unknown)
1595 << getOpenMPDirectiveName(ParentRegion);
1596 return true;
1597 }
1598 return false;
1599 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001600 // Allow some constructs to be orphaned (they could be used in functions,
1601 // called from OpenMP regions with the required preconditions).
1602 if (ParentRegion == OMPD_unknown)
1603 return false;
Alexander Musman80c22892014-07-17 08:54:58 +00001604 if (CurrentRegion == OMPD_master) {
1605 // OpenMP [2.16, Nesting of Regions]
1606 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00001607 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00001608 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
1609 ParentRegion == OMPD_task;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001610 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
1611 // OpenMP [2.16, Nesting of Regions]
1612 // A critical region may not be nested (closely or otherwise) inside a
1613 // critical region with the same name. Note that this restriction is not
1614 // sufficient to prevent deadlock.
1615 SourceLocation PreviousCriticalLoc;
1616 bool DeadLock =
1617 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
1618 OpenMPDirectiveKind K,
1619 const DeclarationNameInfo &DNI,
1620 SourceLocation Loc)
1621 ->bool {
1622 if (K == OMPD_critical &&
1623 DNI.getName() == CurrentName.getName()) {
1624 PreviousCriticalLoc = Loc;
1625 return true;
1626 } else
1627 return false;
1628 },
1629 false /* skip top directive */);
1630 if (DeadLock) {
1631 SemaRef.Diag(StartLoc,
1632 diag::err_omp_prohibited_region_critical_same_name)
1633 << CurrentName.getName();
1634 if (PreviousCriticalLoc.isValid())
1635 SemaRef.Diag(PreviousCriticalLoc,
1636 diag::note_omp_previous_critical_region);
1637 return true;
1638 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001639 } else if (CurrentRegion == OMPD_barrier) {
1640 // OpenMP [2.16, Nesting of Regions]
1641 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00001642 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001643 NestingProhibited =
1644 isOpenMPWorksharingDirective(ParentRegion) ||
1645 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1646 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00001647 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00001648 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00001649 // OpenMP [2.16, Nesting of Regions]
1650 // A worksharing region may not be closely nested inside a worksharing,
1651 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001652 NestingProhibited =
Alexander Musmanf82886e2014-09-18 05:12:34 +00001653 isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001654 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1655 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
1656 Recommend = ShouldBeInParallelRegion;
1657 } else if (CurrentRegion == OMPD_ordered) {
1658 // OpenMP [2.16, Nesting of Regions]
1659 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00001660 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001661 // An ordered region must be closely nested inside a loop region (or
1662 // parallel loop region) with an ordered clause.
1663 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00001664 ParentRegion == OMPD_task ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001665 !Stack->isParentOrderedRegion();
1666 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00001667 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
1668 // OpenMP [2.16, Nesting of Regions]
1669 // If specified, a teams construct must be contained within a target
1670 // construct.
1671 NestingProhibited = ParentRegion != OMPD_target;
1672 Recommend = ShouldBeInTargetRegion;
1673 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
1674 }
1675 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
1676 // OpenMP [2.16, Nesting of Regions]
1677 // distribute, parallel, parallel sections, parallel workshare, and the
1678 // parallel loop and parallel loop SIMD constructs are the only OpenMP
1679 // constructs that can be closely nested in the teams region.
1680 // TODO: add distribute directive.
1681 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion);
1682 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00001683 }
1684 if (NestingProhibited) {
1685 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001686 << CloseNesting << getOpenMPDirectiveName(ParentRegion) << Recommend
1687 << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00001688 return true;
1689 }
1690 }
1691 return false;
1692}
1693
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001694StmtResult Sema::ActOnOpenMPExecutableDirective(OpenMPDirectiveKind Kind,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001695 const DeclarationNameInfo &DirName,
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001696 ArrayRef<OMPClause *> Clauses,
1697 Stmt *AStmt,
1698 SourceLocation StartLoc,
1699 SourceLocation EndLoc) {
1700 StmtResult Res = StmtError();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001701 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00001702 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001703
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001704 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev68446b72014-07-18 07:47:19 +00001705 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001706 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00001707 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00001708 if (AStmt) {
1709 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
1710
1711 // Check default data sharing attributes for referenced variables.
1712 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
1713 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
1714 if (DSAChecker.isErrorFound())
1715 return StmtError();
1716 // Generate list of implicitly defined firstprivate variables.
1717 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00001718
1719 if (!DSAChecker.getImplicitFirstprivate().empty()) {
1720 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
1721 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
1722 SourceLocation(), SourceLocation())) {
1723 ClausesWithImplicit.push_back(Implicit);
1724 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
1725 DSAChecker.getImplicitFirstprivate().size();
1726 } else
1727 ErrorFound = true;
1728 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001729 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001730
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001731 switch (Kind) {
1732 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00001733 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
1734 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001735 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001736 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001737 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
1738 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001739 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00001740 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001741 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
1742 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001743 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00001744 case OMPD_for_simd:
1745 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
1746 EndLoc, VarsWithInheritedDSA);
1747 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001748 case OMPD_sections:
1749 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
1750 EndLoc);
1751 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001752 case OMPD_section:
1753 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00001754 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001755 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
1756 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001757 case OMPD_single:
1758 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
1759 EndLoc);
1760 break;
Alexander Musman80c22892014-07-17 08:54:58 +00001761 case OMPD_master:
1762 assert(ClausesWithImplicit.empty() &&
1763 "No clauses are allowed for 'omp master' directive");
1764 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
1765 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001766 case OMPD_critical:
1767 assert(ClausesWithImplicit.empty() &&
1768 "No clauses are allowed for 'omp critical' directive");
1769 Res = ActOnOpenMPCriticalDirective(DirName, AStmt, StartLoc, EndLoc);
1770 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00001771 case OMPD_parallel_for:
1772 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
1773 EndLoc, VarsWithInheritedDSA);
1774 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00001775 case OMPD_parallel_for_simd:
1776 Res = ActOnOpenMPParallelForSimdDirective(
1777 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
1778 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001779 case OMPD_parallel_sections:
1780 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
1781 StartLoc, EndLoc);
1782 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001783 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001784 Res =
1785 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
1786 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00001787 case OMPD_taskyield:
1788 assert(ClausesWithImplicit.empty() &&
1789 "No clauses are allowed for 'omp taskyield' directive");
1790 assert(AStmt == nullptr &&
1791 "No associated statement allowed for 'omp taskyield' directive");
1792 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
1793 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001794 case OMPD_barrier:
1795 assert(ClausesWithImplicit.empty() &&
1796 "No clauses are allowed for 'omp barrier' directive");
1797 assert(AStmt == nullptr &&
1798 "No associated statement allowed for 'omp barrier' directive");
1799 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
1800 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00001801 case OMPD_taskwait:
1802 assert(ClausesWithImplicit.empty() &&
1803 "No clauses are allowed for 'omp taskwait' directive");
1804 assert(AStmt == nullptr &&
1805 "No associated statement allowed for 'omp taskwait' directive");
1806 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
1807 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00001808 case OMPD_flush:
1809 assert(AStmt == nullptr &&
1810 "No associated statement allowed for 'omp flush' directive");
1811 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
1812 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001813 case OMPD_ordered:
1814 assert(ClausesWithImplicit.empty() &&
1815 "No clauses are allowed for 'omp ordered' directive");
1816 Res = ActOnOpenMPOrderedDirective(AStmt, StartLoc, EndLoc);
1817 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00001818 case OMPD_atomic:
1819 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
1820 EndLoc);
1821 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00001822 case OMPD_teams:
1823 Res =
1824 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
1825 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001826 case OMPD_target:
1827 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
1828 EndLoc);
1829 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001830 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001831 llvm_unreachable("OpenMP Directive is not allowed");
1832 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001833 llvm_unreachable("Unknown OpenMP directive");
1834 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001835
Alexey Bataev4acb8592014-07-07 13:01:15 +00001836 for (auto P : VarsWithInheritedDSA) {
1837 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
1838 << P.first << P.second->getSourceRange();
1839 }
1840 if (!VarsWithInheritedDSA.empty())
1841 return StmtError();
1842
Alexey Bataeved09d242014-05-28 05:53:51 +00001843 if (ErrorFound)
1844 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001845 return Res;
1846}
1847
1848StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
1849 Stmt *AStmt,
1850 SourceLocation StartLoc,
1851 SourceLocation EndLoc) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001852 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1853 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
1854 // 1.2.2 OpenMP Language Terminology
1855 // Structured block - An executable statement with a single entry at the
1856 // top and a single exit at the bottom.
1857 // The point of exit cannot be a branch out of the structured block.
1858 // longjmp() and throw() must not violate the entry/exit criteria.
1859 CS->getCapturedDecl()->setNothrow();
1860
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001861 getCurFunction()->setHasBranchProtectedScope();
1862
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001863 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
1864 AStmt);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001865}
1866
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001867namespace {
1868/// \brief Helper class for checking canonical form of the OpenMP loops and
1869/// extracting iteration space of each loop in the loop nest, that will be used
1870/// for IR generation.
1871class OpenMPIterationSpaceChecker {
1872 /// \brief Reference to Sema.
1873 Sema &SemaRef;
1874 /// \brief A location for diagnostics (when there is no some better location).
1875 SourceLocation DefaultLoc;
1876 /// \brief A location for diagnostics (when increment is not compatible).
1877 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00001878 /// \brief A source location for referring to loop init later.
1879 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001880 /// \brief A source location for referring to condition later.
1881 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00001882 /// \brief A source location for referring to increment later.
1883 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001884 /// \brief Loop variable.
1885 VarDecl *Var;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001886 /// \brief Reference to loop variable.
1887 DeclRefExpr *VarRef;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001888 /// \brief Lower bound (initializer for the var).
1889 Expr *LB;
1890 /// \brief Upper bound.
1891 Expr *UB;
1892 /// \brief Loop step (increment).
1893 Expr *Step;
1894 /// \brief This flag is true when condition is one of:
1895 /// Var < UB
1896 /// Var <= UB
1897 /// UB > Var
1898 /// UB >= Var
1899 bool TestIsLessOp;
1900 /// \brief This flag is true when condition is strict ( < or > ).
1901 bool TestIsStrictOp;
1902 /// \brief This flag is true when step is subtracted on each iteration.
1903 bool SubtractStep;
1904
1905public:
1906 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
1907 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
Alexander Musmana5f070a2014-10-01 06:03:56 +00001908 InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()),
1909 IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001910 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
1911 TestIsStrictOp(false), SubtractStep(false) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001912 /// \brief Check init-expr for canonical loop form and save loop counter
1913 /// variable - #Var and its initialization value - #LB.
1914 bool CheckInit(Stmt *S);
1915 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
1916 /// for less/greater and for strict/non-strict comparison.
1917 bool CheckCond(Expr *S);
1918 /// \brief Check incr-expr for canonical loop form and return true if it
1919 /// does not conform, otherwise save loop step (#Step).
1920 bool CheckInc(Expr *S);
1921 /// \brief Return the loop counter variable.
1922 VarDecl *GetLoopVar() const { return Var; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001923 /// \brief Return the reference expression to loop counter variable.
1924 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00001925 /// \brief Source range of the loop init.
1926 SourceRange GetInitSrcRange() const { return InitSrcRange; }
1927 /// \brief Source range of the loop condition.
1928 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
1929 /// \brief Source range of the loop increment.
1930 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
1931 /// \brief True if the step should be subtracted.
1932 bool ShouldSubtractStep() const { return SubtractStep; }
1933 /// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00001934 Expr *BuildNumIterations(Scope *S, const bool LimitedType) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00001935 /// \brief Build reference expression to the counter be used for codegen.
1936 Expr *BuildCounterVar() const;
1937 /// \brief Build initization of the counter be used for codegen.
1938 Expr *BuildCounterInit() const;
1939 /// \brief Build step of the counter be used for codegen.
1940 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001941 /// \brief Return true if any expression is dependent.
1942 bool Dependent() const;
1943
1944private:
1945 /// \brief Check the right-hand side of an assignment in the increment
1946 /// expression.
1947 bool CheckIncRHS(Expr *RHS);
1948 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001949 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001950 /// \brief Helper to set upper bound.
1951 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, const SourceRange &SR,
1952 const SourceLocation &SL);
1953 /// \brief Helper to set loop increment.
1954 bool SetStep(Expr *NewStep, bool Subtract);
1955};
1956
1957bool OpenMPIterationSpaceChecker::Dependent() const {
1958 if (!Var) {
1959 assert(!LB && !UB && !Step);
1960 return false;
1961 }
1962 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
1963 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
1964}
1965
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001966bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
1967 DeclRefExpr *NewVarRefExpr,
1968 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001969 // State consistency checking to ensure correct usage.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001970 assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
1971 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001972 if (!NewVar || !NewLB)
1973 return true;
1974 Var = NewVar;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001975 VarRef = NewVarRefExpr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001976 LB = NewLB;
1977 return false;
1978}
1979
1980bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
1981 const SourceRange &SR,
1982 const SourceLocation &SL) {
1983 // State consistency checking to ensure correct usage.
1984 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
1985 !TestIsLessOp && !TestIsStrictOp);
1986 if (!NewUB)
1987 return true;
1988 UB = NewUB;
1989 TestIsLessOp = LessOp;
1990 TestIsStrictOp = StrictOp;
1991 ConditionSrcRange = SR;
1992 ConditionLoc = SL;
1993 return false;
1994}
1995
1996bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
1997 // State consistency checking to ensure correct usage.
1998 assert(Var != nullptr && LB != nullptr && Step == nullptr);
1999 if (!NewStep)
2000 return true;
2001 if (!NewStep->isValueDependent()) {
2002 // Check that the step is integer expression.
2003 SourceLocation StepLoc = NewStep->getLocStart();
2004 ExprResult Val =
2005 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
2006 if (Val.isInvalid())
2007 return true;
2008 NewStep = Val.get();
2009
2010 // OpenMP [2.6, Canonical Loop Form, Restrictions]
2011 // If test-expr is of form var relational-op b and relational-op is < or
2012 // <= then incr-expr must cause var to increase on each iteration of the
2013 // loop. If test-expr is of form var relational-op b and relational-op is
2014 // > or >= then incr-expr must cause var to decrease on each iteration of
2015 // the loop.
2016 // If test-expr is of form b relational-op var and relational-op is < or
2017 // <= then incr-expr must cause var to decrease on each iteration of the
2018 // loop. If test-expr is of form b relational-op var and relational-op is
2019 // > or >= then incr-expr must cause var to increase on each iteration of
2020 // the loop.
2021 llvm::APSInt Result;
2022 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
2023 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
2024 bool IsConstNeg =
2025 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00002026 bool IsConstPos =
2027 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002028 bool IsConstZero = IsConstant && !Result.getBoolValue();
2029 if (UB && (IsConstZero ||
2030 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00002031 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002032 SemaRef.Diag(NewStep->getExprLoc(),
2033 diag::err_omp_loop_incr_not_compatible)
2034 << Var << TestIsLessOp << NewStep->getSourceRange();
2035 SemaRef.Diag(ConditionLoc,
2036 diag::note_omp_loop_cond_requres_compatible_incr)
2037 << TestIsLessOp << ConditionSrcRange;
2038 return true;
2039 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002040 if (TestIsLessOp == Subtract) {
2041 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
2042 NewStep).get();
2043 Subtract = !Subtract;
2044 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002045 }
2046
2047 Step = NewStep;
2048 SubtractStep = Subtract;
2049 return false;
2050}
2051
2052bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S) {
2053 // Check init-expr for canonical loop form and save loop counter
2054 // variable - #Var and its initialization value - #LB.
2055 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
2056 // var = lb
2057 // integer-type var = lb
2058 // random-access-iterator-type var = lb
2059 // pointer-type var = lb
2060 //
2061 if (!S) {
2062 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
2063 return true;
2064 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002065 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002066 if (Expr *E = dyn_cast<Expr>(S))
2067 S = E->IgnoreParens();
2068 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2069 if (BO->getOpcode() == BO_Assign)
2070 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002071 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002072 BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002073 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
2074 if (DS->isSingleDecl()) {
2075 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
2076 if (Var->hasInit()) {
2077 // Accept non-canonical init form here but emit ext. warning.
2078 if (Var->getInitStyle() != VarDecl::CInit)
2079 SemaRef.Diag(S->getLocStart(),
2080 diag::ext_omp_loop_not_canonical_init)
2081 << S->getSourceRange();
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002082 return SetVarAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002083 }
2084 }
2085 }
2086 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
2087 if (CE->getOperator() == OO_Equal)
2088 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002089 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
2090 CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002091
2092 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
2093 << S->getSourceRange();
2094 return true;
2095}
2096
Alexey Bataev23b69422014-06-18 07:08:49 +00002097/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002098/// variable (which may be the loop variable) if possible.
2099static const VarDecl *GetInitVarDecl(const Expr *E) {
2100 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00002101 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002102 E = E->IgnoreParenImpCasts();
2103 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
2104 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
2105 if (Ctor->isCopyConstructor() && CE->getNumArgs() == 1 &&
2106 CE->getArg(0) != nullptr)
2107 E = CE->getArg(0)->IgnoreParenImpCasts();
2108 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
2109 if (!DRE)
2110 return nullptr;
2111 return dyn_cast<VarDecl>(DRE->getDecl());
2112}
2113
2114bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
2115 // Check test-expr for canonical form, save upper-bound UB, flags for
2116 // less/greater and for strict/non-strict comparison.
2117 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2118 // var relational-op b
2119 // b relational-op var
2120 //
2121 if (!S) {
2122 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
2123 return true;
2124 }
2125 S = S->IgnoreParenImpCasts();
2126 SourceLocation CondLoc = S->getLocStart();
2127 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2128 if (BO->isRelationalOp()) {
2129 if (GetInitVarDecl(BO->getLHS()) == Var)
2130 return SetUB(BO->getRHS(),
2131 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
2132 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2133 BO->getSourceRange(), BO->getOperatorLoc());
2134 if (GetInitVarDecl(BO->getRHS()) == Var)
2135 return SetUB(BO->getLHS(),
2136 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
2137 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2138 BO->getSourceRange(), BO->getOperatorLoc());
2139 }
2140 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2141 if (CE->getNumArgs() == 2) {
2142 auto Op = CE->getOperator();
2143 switch (Op) {
2144 case OO_Greater:
2145 case OO_GreaterEqual:
2146 case OO_Less:
2147 case OO_LessEqual:
2148 if (GetInitVarDecl(CE->getArg(0)) == Var)
2149 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
2150 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2151 CE->getOperatorLoc());
2152 if (GetInitVarDecl(CE->getArg(1)) == Var)
2153 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
2154 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2155 CE->getOperatorLoc());
2156 break;
2157 default:
2158 break;
2159 }
2160 }
2161 }
2162 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
2163 << S->getSourceRange() << Var;
2164 return true;
2165}
2166
2167bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
2168 // RHS of canonical loop form increment can be:
2169 // var + incr
2170 // incr + var
2171 // var - incr
2172 //
2173 RHS = RHS->IgnoreParenImpCasts();
2174 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
2175 if (BO->isAdditiveOp()) {
2176 bool IsAdd = BO->getOpcode() == BO_Add;
2177 if (GetInitVarDecl(BO->getLHS()) == Var)
2178 return SetStep(BO->getRHS(), !IsAdd);
2179 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
2180 return SetStep(BO->getLHS(), false);
2181 }
2182 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
2183 bool IsAdd = CE->getOperator() == OO_Plus;
2184 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
2185 if (GetInitVarDecl(CE->getArg(0)) == Var)
2186 return SetStep(CE->getArg(1), !IsAdd);
2187 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
2188 return SetStep(CE->getArg(0), false);
2189 }
2190 }
2191 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2192 << RHS->getSourceRange() << Var;
2193 return true;
2194}
2195
2196bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
2197 // Check incr-expr for canonical loop form and return true if it
2198 // does not conform.
2199 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2200 // ++var
2201 // var++
2202 // --var
2203 // var--
2204 // var += incr
2205 // var -= incr
2206 // var = var + incr
2207 // var = incr + var
2208 // var = var - incr
2209 //
2210 if (!S) {
2211 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
2212 return true;
2213 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002214 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002215 S = S->IgnoreParens();
2216 if (auto UO = dyn_cast<UnaryOperator>(S)) {
2217 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
2218 return SetStep(
2219 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
2220 (UO->isDecrementOp() ? -1 : 1)).get(),
2221 false);
2222 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
2223 switch (BO->getOpcode()) {
2224 case BO_AddAssign:
2225 case BO_SubAssign:
2226 if (GetInitVarDecl(BO->getLHS()) == Var)
2227 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
2228 break;
2229 case BO_Assign:
2230 if (GetInitVarDecl(BO->getLHS()) == Var)
2231 return CheckIncRHS(BO->getRHS());
2232 break;
2233 default:
2234 break;
2235 }
2236 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2237 switch (CE->getOperator()) {
2238 case OO_PlusPlus:
2239 case OO_MinusMinus:
2240 if (GetInitVarDecl(CE->getArg(0)) == Var)
2241 return SetStep(
2242 SemaRef.ActOnIntegerConstant(
2243 CE->getLocStart(),
2244 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
2245 false);
2246 break;
2247 case OO_PlusEqual:
2248 case OO_MinusEqual:
2249 if (GetInitVarDecl(CE->getArg(0)) == Var)
2250 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
2251 break;
2252 case OO_Equal:
2253 if (GetInitVarDecl(CE->getArg(0)) == Var)
2254 return CheckIncRHS(CE->getArg(1));
2255 break;
2256 default:
2257 break;
2258 }
2259 }
2260 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2261 << S->getSourceRange() << Var;
2262 return true;
2263}
Alexander Musmana5f070a2014-10-01 06:03:56 +00002264
2265/// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00002266Expr *
2267OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S,
2268 const bool LimitedType) const {
Alexander Musmana5f070a2014-10-01 06:03:56 +00002269 ExprResult Diff;
2270 if (Var->getType()->isIntegerType() || Var->getType()->isPointerType() ||
2271 SemaRef.getLangOpts().CPlusPlus) {
2272 // Upper - Lower
2273 Expr *Upper = TestIsLessOp ? UB : LB;
2274 Expr *Lower = TestIsLessOp ? LB : UB;
2275
2276 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
2277
2278 if (!Diff.isUsable() && Var->getType()->getAsCXXRecordDecl()) {
2279 // BuildBinOp already emitted error, this one is to point user to upper
2280 // and lower bound, and to tell what is passed to 'operator-'.
2281 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
2282 << Upper->getSourceRange() << Lower->getSourceRange();
2283 return nullptr;
2284 }
2285 }
2286
2287 if (!Diff.isUsable())
2288 return nullptr;
2289
2290 // Upper - Lower [- 1]
2291 if (TestIsStrictOp)
2292 Diff = SemaRef.BuildBinOp(
2293 S, DefaultLoc, BO_Sub, Diff.get(),
2294 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
2295 if (!Diff.isUsable())
2296 return nullptr;
2297
2298 // Upper - Lower [- 1] + Step
2299 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(),
2300 Step->IgnoreImplicit());
2301 if (!Diff.isUsable())
2302 return nullptr;
2303
2304 // Parentheses (for dumping/debugging purposes only).
2305 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
2306 if (!Diff.isUsable())
2307 return nullptr;
2308
2309 // (Upper - Lower [- 1] + Step) / Step
2310 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(),
2311 Step->IgnoreImplicit());
2312 if (!Diff.isUsable())
2313 return nullptr;
2314
Alexander Musman174b3ca2014-10-06 11:16:29 +00002315 // OpenMP runtime requires 32-bit or 64-bit loop variables.
2316 if (LimitedType) {
2317 auto &C = SemaRef.Context;
2318 QualType Type = Diff.get()->getType();
2319 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
2320 if (NewSize != C.getTypeSize(Type)) {
2321 if (NewSize < C.getTypeSize(Type)) {
2322 assert(NewSize == 64 && "incorrect loop var size");
2323 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
2324 << InitSrcRange << ConditionSrcRange;
2325 }
2326 QualType NewType = C.getIntTypeForBitwidth(
2327 NewSize, Type->hasSignedIntegerRepresentation());
2328 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
2329 Sema::AA_Converting, true);
2330 if (!Diff.isUsable())
2331 return nullptr;
2332 }
2333 }
2334
Alexander Musmana5f070a2014-10-01 06:03:56 +00002335 return Diff.get();
2336}
2337
2338/// \brief Build reference expression to the counter be used for codegen.
2339Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const {
2340 return DeclRefExpr::Create(SemaRef.Context, NestedNameSpecifierLoc(),
2341 GetIncrementSrcRange().getBegin(), Var, false,
2342 DefaultLoc, Var->getType(), VK_LValue);
2343}
2344
2345/// \brief Build initization of the counter be used for codegen.
2346Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
2347
2348/// \brief Build step of the counter be used for codegen.
2349Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
2350
2351/// \brief Iteration space of a single for loop.
2352struct LoopIterationSpace {
2353 /// \brief This expression calculates the number of iterations in the loop.
2354 /// It is always possible to calculate it before starting the loop.
2355 Expr *NumIterations;
2356 /// \brief The loop counter variable.
2357 Expr *CounterVar;
2358 /// \brief This is initializer for the initial value of #CounterVar.
2359 Expr *CounterInit;
2360 /// \brief This is step for the #CounterVar used to generate its update:
2361 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
2362 Expr *CounterStep;
2363 /// \brief Should step be subtracted?
2364 bool Subtract;
2365 /// \brief Source range of the loop init.
2366 SourceRange InitSrcRange;
2367 /// \brief Source range of the loop condition.
2368 SourceRange CondSrcRange;
2369 /// \brief Source range of the loop increment.
2370 SourceRange IncSrcRange;
2371};
2372
Alexey Bataev23b69422014-06-18 07:08:49 +00002373} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002374
2375/// \brief Called on a for stmt to check and extract its iteration space
2376/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00002377static bool CheckOpenMPIterationSpace(
2378 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
2379 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
2380 Expr *NestedLoopCountExpr,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002381 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
2382 LoopIterationSpace &ResultIterSpace) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002383 // OpenMP [2.6, Canonical Loop Form]
2384 // for (init-expr; test-expr; incr-expr) structured-block
2385 auto For = dyn_cast_or_null<ForStmt>(S);
2386 if (!For) {
2387 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002388 << (NestedLoopCountExpr != nullptr) << getOpenMPDirectiveName(DKind)
2389 << NestedLoopCount << (CurrentNestedLoopCount > 0)
2390 << CurrentNestedLoopCount;
2391 if (NestedLoopCount > 1)
2392 SemaRef.Diag(NestedLoopCountExpr->getExprLoc(),
2393 diag::note_omp_collapse_expr)
2394 << NestedLoopCountExpr->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002395 return true;
2396 }
2397 assert(For->getBody());
2398
2399 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
2400
2401 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002402 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002403 if (ISC.CheckInit(Init)) {
2404 return true;
2405 }
2406
2407 bool HasErrors = false;
2408
2409 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002410 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002411
2412 // OpenMP [2.6, Canonical Loop Form]
2413 // Var is one of the following:
2414 // A variable of signed or unsigned integer type.
2415 // For C++, a variable of a random access iterator type.
2416 // For C, a variable of a pointer type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002417 auto VarType = Var->getType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002418 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
2419 !VarType->isPointerType() &&
2420 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
2421 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
2422 << SemaRef.getLangOpts().CPlusPlus;
2423 HasErrors = true;
2424 }
2425
Alexey Bataev4acb8592014-07-07 13:01:15 +00002426 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
2427 // Construct
2428 // The loop iteration variable(s) in the associated for-loop(s) of a for or
2429 // parallel for construct is (are) private.
2430 // The loop iteration variable in the associated for-loop of a simd construct
2431 // with just one associated for-loop is linear with a constant-linear-step
2432 // that is the increment of the associated for-loop.
2433 // Exclude loop var from the list of variables with implicitly defined data
2434 // sharing attributes.
Benjamin Kramerad8e0792014-10-10 15:32:48 +00002435 VarsWithImplicitDSA.erase(Var);
Alexey Bataev4acb8592014-07-07 13:01:15 +00002436
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002437 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
2438 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00002439 // The loop iteration variable in the associated for-loop of a simd construct
2440 // with just one associated for-loop may be listed in a linear clause with a
2441 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002442 // The loop iteration variable(s) in the associated for-loop(s) of a for or
2443 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002444 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002445 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
2446 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
2447 // declared in the loop and it is predetermined as a private.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002448 auto PredeterminedCKind =
2449 isOpenMPSimdDirective(DKind)
2450 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
2451 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002452 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00002453 DVar.CKind != PredeterminedCKind) ||
Alexander Musmanf82886e2014-09-18 05:12:34 +00002454 (isOpenMPWorksharingDirective(DKind) && !isOpenMPSimdDirective(DKind) &&
2455 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private &&
2456 DVar.CKind != OMPC_lastprivate)) &&
Alexander Musman1bb328c2014-06-04 13:06:39 +00002457 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002458 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00002459 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
2460 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002461 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002462 HasErrors = true;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002463 } else if (LoopVarRefExpr != nullptr) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00002464 // Make the loop iteration variable private (for worksharing constructs),
2465 // linear (for simd directives with the only one associated loop) or
2466 // lastprivate (for simd directives with several collapsed loops).
Alexey Bataev9aba41c2014-11-14 04:08:45 +00002467 // FIXME: the next check and error message must be removed once the
2468 // capturing of global variables in loops is fixed.
2469 if (DVar.CKind == OMPC_unknown)
2470 DVar = DSA.hasDSA(Var, isOpenMPPrivate, MatchesAlways(),
2471 /*FromParent=*/false);
2472 if (!Var->hasLocalStorage() && DVar.CKind == OMPC_unknown) {
2473 SemaRef.Diag(Init->getLocStart(), diag::err_omp_global_loop_var_dsa)
2474 << getOpenMPClauseName(PredeterminedCKind)
2475 << getOpenMPDirectiveName(DKind);
2476 HasErrors = true;
2477 } else
2478 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002479 }
2480
Alexey Bataev7ff55242014-06-19 09:13:45 +00002481 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00002482
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002483 // Check test-expr.
2484 HasErrors |= ISC.CheckCond(For->getCond());
2485
2486 // Check incr-expr.
2487 HasErrors |= ISC.CheckInc(For->getInc());
2488
Alexander Musmana5f070a2014-10-01 06:03:56 +00002489 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002490 return HasErrors;
2491
Alexander Musmana5f070a2014-10-01 06:03:56 +00002492 // Build the loop's iteration space representation.
Alexander Musman174b3ca2014-10-06 11:16:29 +00002493 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
2494 DSA.getCurScope(), /* LimitedType */ isOpenMPWorksharingDirective(DKind));
Alexander Musmana5f070a2014-10-01 06:03:56 +00002495 ResultIterSpace.CounterVar = ISC.BuildCounterVar();
2496 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
2497 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
2498 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
2499 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
2500 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
2501 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
2502
2503 HasErrors |= (ResultIterSpace.NumIterations == nullptr ||
2504 ResultIterSpace.CounterVar == nullptr ||
2505 ResultIterSpace.CounterInit == nullptr ||
2506 ResultIterSpace.CounterStep == nullptr);
2507
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002508 return HasErrors;
2509}
2510
Alexander Musmana5f070a2014-10-01 06:03:56 +00002511/// \brief Build a variable declaration for OpenMP loop iteration variable.
2512static VarDecl *BuildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
2513 StringRef Name) {
2514 DeclContext *DC = SemaRef.CurContext;
2515 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
2516 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
2517 VarDecl *Decl =
2518 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
2519 Decl->setImplicit();
2520 return Decl;
2521}
2522
2523/// \brief Build 'VarRef = Start + Iter * Step'.
2524static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S,
2525 SourceLocation Loc, ExprResult VarRef,
2526 ExprResult Start, ExprResult Iter,
2527 ExprResult Step, bool Subtract) {
2528 // Add parentheses (for debugging purposes only).
2529 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
2530 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
2531 !Step.isUsable())
2532 return ExprError();
2533
2534 ExprResult Update = SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(),
2535 Step.get()->IgnoreImplicit());
2536 if (!Update.isUsable())
2537 return ExprError();
2538
2539 // Build 'VarRef = Start + Iter * Step'.
2540 Update = SemaRef.BuildBinOp(S, Loc, (Subtract ? BO_Sub : BO_Add),
2541 Start.get()->IgnoreImplicit(), Update.get());
2542 if (!Update.isUsable())
2543 return ExprError();
2544
2545 Update = SemaRef.PerformImplicitConversion(
2546 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
2547 if (!Update.isUsable())
2548 return ExprError();
2549
2550 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
2551 return Update;
2552}
2553
2554/// \brief Convert integer expression \a E to make it have at least \a Bits
2555/// bits.
2556static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
2557 Sema &SemaRef) {
2558 if (E == nullptr)
2559 return ExprError();
2560 auto &C = SemaRef.Context;
2561 QualType OldType = E->getType();
2562 unsigned HasBits = C.getTypeSize(OldType);
2563 if (HasBits >= Bits)
2564 return ExprResult(E);
2565 // OK to convert to signed, because new type has more bits than old.
2566 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
2567 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
2568 true);
2569}
2570
2571/// \brief Check if the given expression \a E is a constant integer that fits
2572/// into \a Bits bits.
2573static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
2574 if (E == nullptr)
2575 return false;
2576 llvm::APSInt Result;
2577 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
2578 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
2579 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002580}
2581
2582/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00002583/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
2584/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002585static unsigned
2586CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *NestedLoopCountExpr,
2587 Stmt *AStmt, Sema &SemaRef, DSAStackTy &DSA,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002588 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00002589 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002590 unsigned NestedLoopCount = 1;
2591 if (NestedLoopCountExpr) {
2592 // Found 'collapse' clause - calculate collapse number.
2593 llvm::APSInt Result;
2594 if (NestedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
2595 NestedLoopCount = Result.getLimitedValue();
2596 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002597 // This is helper routine for loop directives (e.g., 'for', 'simd',
2598 // 'for simd', etc.).
Alexander Musmana5f070a2014-10-01 06:03:56 +00002599 SmallVector<LoopIterationSpace, 4> IterSpaces;
2600 IterSpaces.resize(NestedLoopCount);
2601 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002602 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002603 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev4acb8592014-07-07 13:01:15 +00002604 NestedLoopCount, NestedLoopCountExpr,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002605 VarsWithImplicitDSA, IterSpaces[Cnt]))
Alexey Bataevabfc0692014-06-25 06:52:00 +00002606 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002607 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00002608 // OpenMP [2.8.1, simd construct, Restrictions]
2609 // All loops associated with the construct must be perfectly nested; that
2610 // is, there must be no intervening code nor any OpenMP directive between
2611 // any two loops.
2612 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002613 }
2614
Alexander Musmana5f070a2014-10-01 06:03:56 +00002615 Built.clear(/* size */ NestedLoopCount);
2616
2617 if (SemaRef.CurContext->isDependentContext())
2618 return NestedLoopCount;
2619
2620 // An example of what is generated for the following code:
2621 //
2622 // #pragma omp simd collapse(2)
2623 // for (i = 0; i < NI; ++i)
2624 // for (j = J0; j < NJ; j+=2) {
2625 // <loop body>
2626 // }
2627 //
2628 // We generate the code below.
2629 // Note: the loop body may be outlined in CodeGen.
2630 // Note: some counters may be C++ classes, operator- is used to find number of
2631 // iterations and operator+= to calculate counter value.
2632 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
2633 // or i64 is currently supported).
2634 //
2635 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
2636 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
2637 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
2638 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
2639 // // similar updates for vars in clauses (e.g. 'linear')
2640 // <loop body (using local i and j)>
2641 // }
2642 // i = NI; // assign final values of counters
2643 // j = NJ;
2644 //
2645
2646 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
2647 // the iteration counts of the collapsed for loops.
2648 auto N0 = IterSpaces[0].NumIterations;
2649 ExprResult LastIteration32 = WidenIterationCount(32 /* Bits */, N0, SemaRef);
2650 ExprResult LastIteration64 = WidenIterationCount(64 /* Bits */, N0, SemaRef);
2651
2652 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
2653 return NestedLoopCount;
2654
2655 auto &C = SemaRef.Context;
2656 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
2657
2658 Scope *CurScope = DSA.getCurScope();
2659 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
2660 auto N = IterSpaces[Cnt].NumIterations;
2661 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
2662 if (LastIteration32.isUsable())
2663 LastIteration32 = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_Mul,
2664 LastIteration32.get(), N);
2665 if (LastIteration64.isUsable())
2666 LastIteration64 = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_Mul,
2667 LastIteration64.get(), N);
2668 }
2669
2670 // Choose either the 32-bit or 64-bit version.
2671 ExprResult LastIteration = LastIteration64;
2672 if (LastIteration32.isUsable() &&
2673 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
2674 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
2675 FitsInto(
2676 32 /* Bits */,
2677 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
2678 LastIteration64.get(), SemaRef)))
2679 LastIteration = LastIteration32;
2680
2681 if (!LastIteration.isUsable())
2682 return 0;
2683
2684 // Save the number of iterations.
2685 ExprResult NumIterations = LastIteration;
2686 {
2687 LastIteration = SemaRef.BuildBinOp(
2688 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
2689 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
2690 if (!LastIteration.isUsable())
2691 return 0;
2692 }
2693
2694 // Calculate the last iteration number beforehand instead of doing this on
2695 // each iteration. Do not do this if the number of iterations may be kfold-ed.
2696 llvm::APSInt Result;
2697 bool IsConstant =
2698 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
2699 ExprResult CalcLastIteration;
2700 if (!IsConstant) {
2701 SourceLocation SaveLoc;
2702 VarDecl *SaveVar =
2703 BuildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(),
2704 ".omp.last.iteration");
2705 ExprResult SaveRef = SemaRef.BuildDeclRefExpr(
2706 SaveVar, LastIteration.get()->getType(), VK_LValue, SaveLoc);
2707 CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign,
2708 SaveRef.get(), LastIteration.get());
2709 LastIteration = SaveRef;
2710
2711 // Prepare SaveRef + 1.
2712 NumIterations = SemaRef.BuildBinOp(
2713 CurScope, SaveLoc, BO_Add, SaveRef.get(),
2714 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
2715 if (!NumIterations.isUsable())
2716 return 0;
2717 }
2718
2719 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
2720
2721 // Precondition tests if there is at least one iteration (LastIteration > 0).
2722 ExprResult PreCond = SemaRef.BuildBinOp(
2723 CurScope, InitLoc, BO_GT, LastIteration.get(),
2724 SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get());
2725
Alexander Musmanc6388682014-12-15 07:07:06 +00002726 QualType VType = LastIteration.get()->getType();
2727 // Build variables passed into runtime, nesessary for worksharing directives.
2728 ExprResult LB, UB, IL, ST, EUB;
2729 if (isOpenMPWorksharingDirective(DKind)) {
2730 // Lower bound variable, initialized with zero.
2731 VarDecl *LBDecl = BuildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
2732 LB = SemaRef.BuildDeclRefExpr(LBDecl, VType, VK_LValue, InitLoc);
2733 SemaRef.AddInitializerToDecl(
2734 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
2735 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
2736
2737 // Upper bound variable, initialized with last iteration number.
2738 VarDecl *UBDecl = BuildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
2739 UB = SemaRef.BuildDeclRefExpr(UBDecl, VType, VK_LValue, InitLoc);
2740 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
2741 /*DirectInit*/ false,
2742 /*TypeMayContainAuto*/ false);
2743
2744 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
2745 // This will be used to implement clause 'lastprivate'.
2746 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
2747 VarDecl *ILDecl = BuildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
2748 IL = SemaRef.BuildDeclRefExpr(ILDecl, Int32Ty, VK_LValue, InitLoc);
2749 SemaRef.AddInitializerToDecl(
2750 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
2751 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
2752
2753 // Stride variable returned by runtime (we initialize it to 1 by default).
2754 VarDecl *STDecl = BuildVarDecl(SemaRef, InitLoc, VType, ".omp.stride");
2755 ST = SemaRef.BuildDeclRefExpr(STDecl, VType, VK_LValue, InitLoc);
2756 SemaRef.AddInitializerToDecl(
2757 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
2758 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
2759
2760 // Build expression: UB = min(UB, LastIteration)
2761 // It is nesessary for CodeGen of directives with static scheduling.
2762 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
2763 UB.get(), LastIteration.get());
2764 ExprResult CondOp = SemaRef.ActOnConditionalOp(
2765 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
2766 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
2767 CondOp.get());
2768 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
2769 }
2770
2771 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00002772 ExprResult IV;
2773 ExprResult Init;
2774 {
Alexander Musmanc6388682014-12-15 07:07:06 +00002775 VarDecl *IVDecl = BuildVarDecl(SemaRef, InitLoc, VType, ".omp.iv");
2776 IV = SemaRef.BuildDeclRefExpr(IVDecl, VType, VK_LValue, InitLoc);
2777 Expr *RHS = isOpenMPWorksharingDirective(DKind)
2778 ? LB.get()
2779 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
2780 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
2781 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00002782 }
2783
Alexander Musmanc6388682014-12-15 07:07:06 +00002784 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00002785 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00002786 ExprResult Cond =
2787 isOpenMPWorksharingDirective(DKind)
2788 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
2789 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
2790 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00002791 // Loop condition with 1 iteration separated (IV < LastIteration)
2792 ExprResult SeparatedCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT,
2793 IV.get(), LastIteration.get());
2794
2795 // Loop increment (IV = IV + 1)
2796 SourceLocation IncLoc;
2797 ExprResult Inc =
2798 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
2799 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
2800 if (!Inc.isUsable())
2801 return 0;
2802 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00002803 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
2804 if (!Inc.isUsable())
2805 return 0;
2806
2807 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
2808 // Used for directives with static scheduling.
2809 ExprResult NextLB, NextUB;
2810 if (isOpenMPWorksharingDirective(DKind)) {
2811 // LB + ST
2812 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
2813 if (!NextLB.isUsable())
2814 return 0;
2815 // LB = LB + ST
2816 NextLB =
2817 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
2818 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
2819 if (!NextLB.isUsable())
2820 return 0;
2821 // UB + ST
2822 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
2823 if (!NextUB.isUsable())
2824 return 0;
2825 // UB = UB + ST
2826 NextUB =
2827 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
2828 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
2829 if (!NextUB.isUsable())
2830 return 0;
2831 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002832
2833 // Build updates and final values of the loop counters.
2834 bool HasErrors = false;
2835 Built.Counters.resize(NestedLoopCount);
2836 Built.Updates.resize(NestedLoopCount);
2837 Built.Finals.resize(NestedLoopCount);
2838 {
2839 ExprResult Div;
2840 // Go from inner nested loop to outer.
2841 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
2842 LoopIterationSpace &IS = IterSpaces[Cnt];
2843 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
2844 // Build: Iter = (IV / Div) % IS.NumIters
2845 // where Div is product of previous iterations' IS.NumIters.
2846 ExprResult Iter;
2847 if (Div.isUsable()) {
2848 Iter =
2849 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
2850 } else {
2851 Iter = IV;
2852 assert((Cnt == (int)NestedLoopCount - 1) &&
2853 "unusable div expected on first iteration only");
2854 }
2855
2856 if (Cnt != 0 && Iter.isUsable())
2857 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
2858 IS.NumIterations);
2859 if (!Iter.isUsable()) {
2860 HasErrors = true;
2861 break;
2862 }
2863
2864 // Build update: IS.CounterVar = IS.Start + Iter * IS.Step
2865 ExprResult Update =
2866 BuildCounterUpdate(SemaRef, CurScope, UpdLoc, IS.CounterVar,
2867 IS.CounterInit, Iter, IS.CounterStep, IS.Subtract);
2868 if (!Update.isUsable()) {
2869 HasErrors = true;
2870 break;
2871 }
2872
2873 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
2874 ExprResult Final = BuildCounterUpdate(
2875 SemaRef, CurScope, UpdLoc, IS.CounterVar, IS.CounterInit,
2876 IS.NumIterations, IS.CounterStep, IS.Subtract);
2877 if (!Final.isUsable()) {
2878 HasErrors = true;
2879 break;
2880 }
2881
2882 // Build Div for the next iteration: Div <- Div * IS.NumIters
2883 if (Cnt != 0) {
2884 if (Div.isUnset())
2885 Div = IS.NumIterations;
2886 else
2887 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
2888 IS.NumIterations);
2889
2890 // Add parentheses (for debugging purposes only).
2891 if (Div.isUsable())
2892 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
2893 if (!Div.isUsable()) {
2894 HasErrors = true;
2895 break;
2896 }
2897 }
2898 if (!Update.isUsable() || !Final.isUsable()) {
2899 HasErrors = true;
2900 break;
2901 }
2902 // Save results
2903 Built.Counters[Cnt] = IS.CounterVar;
2904 Built.Updates[Cnt] = Update.get();
2905 Built.Finals[Cnt] = Final.get();
2906 }
2907 }
2908
2909 if (HasErrors)
2910 return 0;
2911
2912 // Save results
2913 Built.IterationVarRef = IV.get();
2914 Built.LastIteration = LastIteration.get();
2915 Built.CalcLastIteration = CalcLastIteration.get();
2916 Built.PreCond = PreCond.get();
2917 Built.Cond = Cond.get();
2918 Built.SeparatedCond = SeparatedCond.get();
2919 Built.Init = Init.get();
2920 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00002921 Built.LB = LB.get();
2922 Built.UB = UB.get();
2923 Built.IL = IL.get();
2924 Built.ST = ST.get();
2925 Built.EUB = EUB.get();
2926 Built.NLB = NextLB.get();
2927 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00002928
Alexey Bataevabfc0692014-06-25 06:52:00 +00002929 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002930}
2931
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002932static Expr *GetCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002933 auto CollapseFilter = [](const OMPClause *C) -> bool {
2934 return C->getClauseKind() == OMPC_collapse;
2935 };
2936 OMPExecutableDirective::filtered_clause_iterator<decltype(CollapseFilter)> I(
2937 Clauses, CollapseFilter);
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002938 if (I)
2939 return cast<OMPCollapseClause>(*I)->getNumForLoops();
2940 return nullptr;
2941}
2942
Alexey Bataev4acb8592014-07-07 13:01:15 +00002943StmtResult Sema::ActOnOpenMPSimdDirective(
2944 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2945 SourceLocation EndLoc,
2946 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmanc6388682014-12-15 07:07:06 +00002947 OMPLoopDirective::HelperExprs B;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002948 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002949 unsigned NestedLoopCount =
2950 CheckOpenMPLoop(OMPD_simd, GetCollapseNumberExpr(Clauses), AStmt, *this,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002951 *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00002952 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002953 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002954
Alexander Musmana5f070a2014-10-01 06:03:56 +00002955 assert((CurContext->isDependentContext() || B.builtAll()) &&
2956 "omp simd loop exprs were not built");
2957
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002958 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00002959 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
2960 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002961}
2962
Alexey Bataev4acb8592014-07-07 13:01:15 +00002963StmtResult Sema::ActOnOpenMPForDirective(
2964 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2965 SourceLocation EndLoc,
2966 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmanc6388682014-12-15 07:07:06 +00002967 OMPLoopDirective::HelperExprs B;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002968 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002969 unsigned NestedLoopCount =
2970 CheckOpenMPLoop(OMPD_for, GetCollapseNumberExpr(Clauses), AStmt, *this,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002971 *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00002972 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00002973 return StmtError();
2974
Alexander Musmana5f070a2014-10-01 06:03:56 +00002975 assert((CurContext->isDependentContext() || B.builtAll()) &&
2976 "omp for loop exprs were not built");
2977
Alexey Bataevf29276e2014-06-18 04:14:57 +00002978 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00002979 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
2980 Clauses, AStmt, B);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002981}
2982
Alexander Musmanf82886e2014-09-18 05:12:34 +00002983StmtResult Sema::ActOnOpenMPForSimdDirective(
2984 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2985 SourceLocation EndLoc,
2986 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmanc6388682014-12-15 07:07:06 +00002987 OMPLoopDirective::HelperExprs B;
Alexander Musmanf82886e2014-09-18 05:12:34 +00002988 // In presence of clause 'collapse', it will define the nested loops number.
2989 unsigned NestedLoopCount =
2990 CheckOpenMPLoop(OMPD_for_simd, GetCollapseNumberExpr(Clauses), AStmt,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002991 *this, *DSAStack, VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00002992 if (NestedLoopCount == 0)
2993 return StmtError();
2994
Alexander Musmanc6388682014-12-15 07:07:06 +00002995 assert((CurContext->isDependentContext() || B.builtAll()) &&
2996 "omp for simd loop exprs were not built");
2997
Alexander Musmanf82886e2014-09-18 05:12:34 +00002998 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00002999 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
3000 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00003001}
3002
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003003StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
3004 Stmt *AStmt,
3005 SourceLocation StartLoc,
3006 SourceLocation EndLoc) {
3007 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3008 auto BaseStmt = AStmt;
3009 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
3010 BaseStmt = CS->getCapturedStmt();
3011 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
3012 auto S = C->children();
3013 if (!S)
3014 return StmtError();
3015 // All associated statements must be '#pragma omp section' except for
3016 // the first one.
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003017 for (++S; S; ++S) {
3018 auto SectionStmt = *S;
3019 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
3020 if (SectionStmt)
3021 Diag(SectionStmt->getLocStart(),
3022 diag::err_omp_sections_substmt_not_section);
3023 return StmtError();
3024 }
3025 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003026 } else {
3027 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
3028 return StmtError();
3029 }
3030
3031 getCurFunction()->setHasBranchProtectedScope();
3032
3033 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses,
3034 AStmt);
3035}
3036
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003037StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
3038 SourceLocation StartLoc,
3039 SourceLocation EndLoc) {
3040 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3041
3042 getCurFunction()->setHasBranchProtectedScope();
3043
3044 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt);
3045}
3046
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003047StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
3048 Stmt *AStmt,
3049 SourceLocation StartLoc,
3050 SourceLocation EndLoc) {
Alexey Bataev74a05c92014-07-15 02:55:09 +00003051 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3052
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003053 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00003054
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003055 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3056}
3057
Alexander Musman80c22892014-07-17 08:54:58 +00003058StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
3059 SourceLocation StartLoc,
3060 SourceLocation EndLoc) {
3061 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3062
3063 getCurFunction()->setHasBranchProtectedScope();
3064
3065 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
3066}
3067
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003068StmtResult
3069Sema::ActOnOpenMPCriticalDirective(const DeclarationNameInfo &DirName,
3070 Stmt *AStmt, SourceLocation StartLoc,
3071 SourceLocation EndLoc) {
3072 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3073
3074 getCurFunction()->setHasBranchProtectedScope();
3075
3076 return OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
3077 AStmt);
3078}
3079
Alexey Bataev4acb8592014-07-07 13:01:15 +00003080StmtResult Sema::ActOnOpenMPParallelForDirective(
3081 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3082 SourceLocation EndLoc,
3083 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
3084 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3085 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3086 // 1.2.2 OpenMP Language Terminology
3087 // Structured block - An executable statement with a single entry at the
3088 // top and a single exit at the bottom.
3089 // The point of exit cannot be a branch out of the structured block.
3090 // longjmp() and throw() must not violate the entry/exit criteria.
3091 CS->getCapturedDecl()->setNothrow();
3092
Alexander Musmanc6388682014-12-15 07:07:06 +00003093 OMPLoopDirective::HelperExprs B;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003094 // In presence of clause 'collapse', it will define the nested loops number.
3095 unsigned NestedLoopCount =
3096 CheckOpenMPLoop(OMPD_parallel_for, GetCollapseNumberExpr(Clauses), AStmt,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003097 *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003098 if (NestedLoopCount == 0)
3099 return StmtError();
3100
Alexander Musmana5f070a2014-10-01 06:03:56 +00003101 assert((CurContext->isDependentContext() || B.builtAll()) &&
3102 "omp parallel for loop exprs were not built");
3103
Alexey Bataev4acb8592014-07-07 13:01:15 +00003104 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003105 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
3106 NestedLoopCount, Clauses, AStmt, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003107}
3108
Alexander Musmane4e893b2014-09-23 09:33:00 +00003109StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
3110 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3111 SourceLocation EndLoc,
3112 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
3113 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3114 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3115 // 1.2.2 OpenMP Language Terminology
3116 // Structured block - An executable statement with a single entry at the
3117 // top and a single exit at the bottom.
3118 // The point of exit cannot be a branch out of the structured block.
3119 // longjmp() and throw() must not violate the entry/exit criteria.
3120 CS->getCapturedDecl()->setNothrow();
3121
Alexander Musmanc6388682014-12-15 07:07:06 +00003122 OMPLoopDirective::HelperExprs B;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003123 // In presence of clause 'collapse', it will define the nested loops number.
3124 unsigned NestedLoopCount =
3125 CheckOpenMPLoop(OMPD_parallel_for_simd, GetCollapseNumberExpr(Clauses),
Alexander Musmana5f070a2014-10-01 06:03:56 +00003126 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003127 if (NestedLoopCount == 0)
3128 return StmtError();
3129
3130 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003131 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00003132 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003133}
3134
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003135StmtResult
3136Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
3137 Stmt *AStmt, SourceLocation StartLoc,
3138 SourceLocation EndLoc) {
3139 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3140 auto BaseStmt = AStmt;
3141 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
3142 BaseStmt = CS->getCapturedStmt();
3143 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
3144 auto S = C->children();
3145 if (!S)
3146 return StmtError();
3147 // All associated statements must be '#pragma omp section' except for
3148 // the first one.
3149 for (++S; S; ++S) {
3150 auto SectionStmt = *S;
3151 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
3152 if (SectionStmt)
3153 Diag(SectionStmt->getLocStart(),
3154 diag::err_omp_parallel_sections_substmt_not_section);
3155 return StmtError();
3156 }
3157 }
3158 } else {
3159 Diag(AStmt->getLocStart(),
3160 diag::err_omp_parallel_sections_not_compound_stmt);
3161 return StmtError();
3162 }
3163
3164 getCurFunction()->setHasBranchProtectedScope();
3165
3166 return OMPParallelSectionsDirective::Create(Context, StartLoc, EndLoc,
3167 Clauses, AStmt);
3168}
3169
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003170StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
3171 Stmt *AStmt, SourceLocation StartLoc,
3172 SourceLocation EndLoc) {
3173 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3174 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3175 // 1.2.2 OpenMP Language Terminology
3176 // Structured block - An executable statement with a single entry at the
3177 // top and a single exit at the bottom.
3178 // The point of exit cannot be a branch out of the structured block.
3179 // longjmp() and throw() must not violate the entry/exit criteria.
3180 CS->getCapturedDecl()->setNothrow();
3181
3182 getCurFunction()->setHasBranchProtectedScope();
3183
3184 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3185}
3186
Alexey Bataev68446b72014-07-18 07:47:19 +00003187StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
3188 SourceLocation EndLoc) {
3189 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
3190}
3191
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003192StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
3193 SourceLocation EndLoc) {
3194 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
3195}
3196
Alexey Bataev2df347a2014-07-18 10:17:07 +00003197StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
3198 SourceLocation EndLoc) {
3199 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
3200}
3201
Alexey Bataev6125da92014-07-21 11:26:11 +00003202StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
3203 SourceLocation StartLoc,
3204 SourceLocation EndLoc) {
3205 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
3206 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
3207}
3208
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003209StmtResult Sema::ActOnOpenMPOrderedDirective(Stmt *AStmt,
3210 SourceLocation StartLoc,
3211 SourceLocation EndLoc) {
3212 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3213
3214 getCurFunction()->setHasBranchProtectedScope();
3215
3216 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, AStmt);
3217}
3218
Alexey Bataev0162e452014-07-22 10:10:35 +00003219StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
3220 Stmt *AStmt,
3221 SourceLocation StartLoc,
3222 SourceLocation EndLoc) {
3223 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003224 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00003225 // 1.2.2 OpenMP Language Terminology
3226 // Structured block - An executable statement with a single entry at the
3227 // top and a single exit at the bottom.
3228 // The point of exit cannot be a branch out of the structured block.
3229 // longjmp() and throw() must not violate the entry/exit criteria.
3230 // TODO further analysis of associated statements and clauses.
Alexey Bataevdea47612014-07-23 07:46:59 +00003231 OpenMPClauseKind AtomicKind = OMPC_unknown;
3232 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003233 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00003234 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00003235 C->getClauseKind() == OMPC_update ||
3236 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00003237 if (AtomicKind != OMPC_unknown) {
3238 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
3239 << SourceRange(C->getLocStart(), C->getLocEnd());
3240 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
3241 << getOpenMPClauseName(AtomicKind);
3242 } else {
3243 AtomicKind = C->getClauseKind();
3244 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003245 }
3246 }
3247 }
Alexey Bataev62cec442014-11-18 10:14:22 +00003248
Alexey Bataev459dec02014-07-24 06:46:57 +00003249 auto Body = CS->getCapturedStmt();
Alexey Bataev62cec442014-11-18 10:14:22 +00003250 Expr *X = nullptr;
3251 Expr *V = nullptr;
3252 Expr *E = nullptr;
3253 // OpenMP [2.12.6, atomic Construct]
3254 // In the next expressions:
3255 // * x and v (as applicable) are both l-value expressions with scalar type.
3256 // * During the execution of an atomic region, multiple syntactic
3257 // occurrences of x must designate the same storage location.
3258 // * Neither of v and expr (as applicable) may access the storage location
3259 // designated by x.
3260 // * Neither of x and expr (as applicable) may access the storage location
3261 // designated by v.
3262 // * expr is an expression with scalar type.
3263 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
3264 // * binop, binop=, ++, and -- are not overloaded operators.
3265 // * The expression x binop expr must be numerically equivalent to x binop
3266 // (expr). This requirement is satisfied if the operators in expr have
3267 // precedence greater than binop, or by using parentheses around expr or
3268 // subexpressions of expr.
3269 // * The expression expr binop x must be numerically equivalent to (expr)
3270 // binop x. This requirement is satisfied if the operators in expr have
3271 // precedence equal to or greater than binop, or by using parentheses around
3272 // expr or subexpressions of expr.
3273 // * For forms that allow multiple occurrences of x, the number of times
3274 // that x is evaluated is unspecified.
Alexey Bataevf33eba62014-11-28 07:21:40 +00003275 enum {
3276 NotAnExpression,
3277 NotAnAssignmentOp,
3278 NotAScalarType,
3279 NotAnLValue,
3280 NoError
3281 } ErrorFound = NoError;
Alexey Bataevdea47612014-07-23 07:46:59 +00003282 if (AtomicKind == OMPC_read) {
Alexey Bataev62cec442014-11-18 10:14:22 +00003283 SourceLocation ErrorLoc, NoteLoc;
3284 SourceRange ErrorRange, NoteRange;
3285 // If clause is read:
3286 // v = x;
3287 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
3288 auto AtomicBinOp =
3289 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
3290 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
3291 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
3292 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
3293 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
3294 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
3295 if (!X->isLValue() || !V->isLValue()) {
3296 auto NotLValueExpr = X->isLValue() ? V : X;
3297 ErrorFound = NotAnLValue;
3298 ErrorLoc = AtomicBinOp->getExprLoc();
3299 ErrorRange = AtomicBinOp->getSourceRange();
3300 NoteLoc = NotLValueExpr->getExprLoc();
3301 NoteRange = NotLValueExpr->getSourceRange();
3302 }
3303 } else if (!X->isInstantiationDependent() ||
3304 !V->isInstantiationDependent()) {
3305 auto NotScalarExpr =
3306 (X->isInstantiationDependent() || X->getType()->isScalarType())
3307 ? V
3308 : X;
3309 ErrorFound = NotAScalarType;
3310 ErrorLoc = AtomicBinOp->getExprLoc();
3311 ErrorRange = AtomicBinOp->getSourceRange();
3312 NoteLoc = NotScalarExpr->getExprLoc();
3313 NoteRange = NotScalarExpr->getSourceRange();
3314 }
3315 } else {
3316 ErrorFound = NotAnAssignmentOp;
3317 ErrorLoc = AtomicBody->getExprLoc();
3318 ErrorRange = AtomicBody->getSourceRange();
3319 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
3320 : AtomicBody->getExprLoc();
3321 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
3322 : AtomicBody->getSourceRange();
3323 }
3324 } else {
3325 ErrorFound = NotAnExpression;
3326 NoteLoc = ErrorLoc = Body->getLocStart();
3327 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00003328 }
Alexey Bataev62cec442014-11-18 10:14:22 +00003329 if (ErrorFound != NoError) {
3330 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
3331 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00003332 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
3333 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00003334 return StmtError();
3335 } else if (CurContext->isDependentContext())
3336 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00003337 } else if (AtomicKind == OMPC_write) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00003338 SourceLocation ErrorLoc, NoteLoc;
3339 SourceRange ErrorRange, NoteRange;
3340 // If clause is write:
3341 // x = expr;
3342 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
3343 auto AtomicBinOp =
3344 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
3345 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
3346 X = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
3347 E = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
3348 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
3349 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
3350 if (!X->isLValue()) {
3351 ErrorFound = NotAnLValue;
3352 ErrorLoc = AtomicBinOp->getExprLoc();
3353 ErrorRange = AtomicBinOp->getSourceRange();
3354 NoteLoc = X->getExprLoc();
3355 NoteRange = X->getSourceRange();
3356 }
3357 } else if (!X->isInstantiationDependent() ||
3358 !E->isInstantiationDependent()) {
3359 auto NotScalarExpr =
3360 (X->isInstantiationDependent() || X->getType()->isScalarType())
3361 ? E
3362 : X;
3363 ErrorFound = NotAScalarType;
3364 ErrorLoc = AtomicBinOp->getExprLoc();
3365 ErrorRange = AtomicBinOp->getSourceRange();
3366 NoteLoc = NotScalarExpr->getExprLoc();
3367 NoteRange = NotScalarExpr->getSourceRange();
3368 }
3369 } else {
3370 ErrorFound = NotAnAssignmentOp;
3371 ErrorLoc = AtomicBody->getExprLoc();
3372 ErrorRange = AtomicBody->getSourceRange();
3373 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
3374 : AtomicBody->getExprLoc();
3375 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
3376 : AtomicBody->getSourceRange();
3377 }
3378 } else {
3379 ErrorFound = NotAnExpression;
3380 NoteLoc = ErrorLoc = Body->getLocStart();
3381 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00003382 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00003383 if (ErrorFound != NoError) {
3384 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
3385 << ErrorRange;
3386 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
3387 << NoteRange;
3388 return StmtError();
3389 } else if (CurContext->isDependentContext())
3390 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00003391 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev459dec02014-07-24 06:46:57 +00003392 if (!isa<Expr>(Body)) {
3393 Diag(Body->getLocStart(),
Alexey Bataev67a4f222014-07-23 10:25:33 +00003394 diag::err_omp_atomic_update_not_expression_statement)
3395 << (AtomicKind == OMPC_update);
3396 return StmtError();
3397 }
Alexey Bataev459dec02014-07-24 06:46:57 +00003398 } else if (AtomicKind == OMPC_capture) {
3399 if (isa<Expr>(Body) && !isa<BinaryOperator>(Body)) {
3400 Diag(Body->getLocStart(),
3401 diag::err_omp_atomic_capture_not_expression_statement);
3402 return StmtError();
3403 } else if (!isa<Expr>(Body) && !isa<CompoundStmt>(Body)) {
3404 Diag(Body->getLocStart(),
3405 diag::err_omp_atomic_capture_not_compound_statement);
3406 return StmtError();
3407 }
Alexey Bataevdea47612014-07-23 07:46:59 +00003408 }
Alexey Bataev0162e452014-07-22 10:10:35 +00003409
3410 getCurFunction()->setHasBranchProtectedScope();
3411
Alexey Bataev62cec442014-11-18 10:14:22 +00003412 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3413 X, V, E);
Alexey Bataev0162e452014-07-22 10:10:35 +00003414}
3415
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003416StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
3417 Stmt *AStmt,
3418 SourceLocation StartLoc,
3419 SourceLocation EndLoc) {
3420 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3421
Alexey Bataev13314bf2014-10-09 04:18:56 +00003422 // OpenMP [2.16, Nesting of Regions]
3423 // If specified, a teams construct must be contained within a target
3424 // construct. That target construct must contain no statements or directives
3425 // outside of the teams construct.
3426 if (DSAStack->hasInnerTeamsRegion()) {
3427 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
3428 bool OMPTeamsFound = true;
3429 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
3430 auto I = CS->body_begin();
3431 while (I != CS->body_end()) {
3432 auto OED = dyn_cast<OMPExecutableDirective>(*I);
3433 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
3434 OMPTeamsFound = false;
3435 break;
3436 }
3437 ++I;
3438 }
3439 assert(I != CS->body_end() && "Not found statement");
3440 S = *I;
3441 }
3442 if (!OMPTeamsFound) {
3443 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
3444 Diag(DSAStack->getInnerTeamsRegionLoc(),
3445 diag::note_omp_nested_teams_construct_here);
3446 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
3447 << isa<OMPExecutableDirective>(S);
3448 return StmtError();
3449 }
3450 }
3451
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003452 getCurFunction()->setHasBranchProtectedScope();
3453
3454 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3455}
3456
Alexey Bataev13314bf2014-10-09 04:18:56 +00003457StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
3458 Stmt *AStmt, SourceLocation StartLoc,
3459 SourceLocation EndLoc) {
3460 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3461 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3462 // 1.2.2 OpenMP Language Terminology
3463 // Structured block - An executable statement with a single entry at the
3464 // top and a single exit at the bottom.
3465 // The point of exit cannot be a branch out of the structured block.
3466 // longjmp() and throw() must not violate the entry/exit criteria.
3467 CS->getCapturedDecl()->setNothrow();
3468
3469 getCurFunction()->setHasBranchProtectedScope();
3470
3471 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3472}
3473
Alexey Bataeved09d242014-05-28 05:53:51 +00003474OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003475 SourceLocation StartLoc,
3476 SourceLocation LParenLoc,
3477 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003478 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003479 switch (Kind) {
3480 case OMPC_if:
3481 Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc);
3482 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00003483 case OMPC_final:
3484 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
3485 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00003486 case OMPC_num_threads:
3487 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
3488 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00003489 case OMPC_safelen:
3490 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
3491 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00003492 case OMPC_collapse:
3493 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
3494 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003495 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003496 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003497 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003498 case OMPC_private:
3499 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00003500 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003501 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00003502 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00003503 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00003504 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003505 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003506 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003507 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00003508 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003509 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003510 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003511 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00003512 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003513 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00003514 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00003515 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00003516 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003517 case OMPC_seq_cst:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003518 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003519 llvm_unreachable("Clause is not allowed.");
3520 }
3521 return Res;
3522}
3523
Alexey Bataeved09d242014-05-28 05:53:51 +00003524OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003525 SourceLocation LParenLoc,
3526 SourceLocation EndLoc) {
3527 Expr *ValExpr = Condition;
3528 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
3529 !Condition->isInstantiationDependent() &&
3530 !Condition->containsUnexpandedParameterPack()) {
3531 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00003532 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003533 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003534 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003535
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003536 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003537 }
3538
3539 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc);
3540}
3541
Alexey Bataev3778b602014-07-17 07:32:53 +00003542OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
3543 SourceLocation StartLoc,
3544 SourceLocation LParenLoc,
3545 SourceLocation EndLoc) {
3546 Expr *ValExpr = Condition;
3547 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
3548 !Condition->isInstantiationDependent() &&
3549 !Condition->containsUnexpandedParameterPack()) {
3550 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
3551 Condition->getExprLoc(), Condition);
3552 if (Val.isInvalid())
3553 return nullptr;
3554
3555 ValExpr = Val.get();
3556 }
3557
3558 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
3559}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003560ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
3561 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00003562 if (!Op)
3563 return ExprError();
3564
3565 class IntConvertDiagnoser : public ICEConvertDiagnoser {
3566 public:
3567 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00003568 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00003569 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
3570 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003571 return S.Diag(Loc, diag::err_omp_not_integral) << T;
3572 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003573 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
3574 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003575 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
3576 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003577 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
3578 QualType T,
3579 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003580 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
3581 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003582 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
3583 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003584 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00003585 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00003586 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003587 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
3588 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003589 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
3590 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003591 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
3592 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003593 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00003594 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00003595 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003596 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
3597 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003598 llvm_unreachable("conversion functions are permitted");
3599 }
3600 } ConvertDiagnoser;
3601 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
3602}
3603
3604OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
3605 SourceLocation StartLoc,
3606 SourceLocation LParenLoc,
3607 SourceLocation EndLoc) {
3608 Expr *ValExpr = NumThreads;
3609 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
Alexey Bataev568a8332014-03-06 06:15:19 +00003610 !NumThreads->containsUnexpandedParameterPack()) {
3611 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
3612 ExprResult Val =
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003613 PerformOpenMPImplicitIntegerConversion(NumThreadsLoc, NumThreads);
Alexey Bataev568a8332014-03-06 06:15:19 +00003614 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003615 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00003616
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003617 ValExpr = Val.get();
Alexey Bataev568a8332014-03-06 06:15:19 +00003618
3619 // OpenMP [2.5, Restrictions]
3620 // The num_threads expression must evaluate to a positive integer value.
3621 llvm::APSInt Result;
Alexey Bataeved09d242014-05-28 05:53:51 +00003622 if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() &&
3623 !Result.isStrictlyPositive()) {
Alexey Bataev568a8332014-03-06 06:15:19 +00003624 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
3625 << "num_threads" << NumThreads->getSourceRange();
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003626 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00003627 }
3628 }
3629
Alexey Bataeved09d242014-05-28 05:53:51 +00003630 return new (Context)
3631 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00003632}
3633
Alexey Bataev62c87d22014-03-21 04:51:18 +00003634ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
3635 OpenMPClauseKind CKind) {
3636 if (!E)
3637 return ExprError();
3638 if (E->isValueDependent() || E->isTypeDependent() ||
3639 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00003640 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00003641 llvm::APSInt Result;
3642 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
3643 if (ICE.isInvalid())
3644 return ExprError();
3645 if (!Result.isStrictlyPositive()) {
3646 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
3647 << getOpenMPClauseName(CKind) << E->getSourceRange();
3648 return ExprError();
3649 }
Alexander Musman09184fe2014-09-30 05:29:28 +00003650 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
3651 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
3652 << E->getSourceRange();
3653 return ExprError();
3654 }
Alexey Bataev62c87d22014-03-21 04:51:18 +00003655 return ICE;
3656}
3657
3658OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
3659 SourceLocation LParenLoc,
3660 SourceLocation EndLoc) {
3661 // OpenMP [2.8.1, simd construct, Description]
3662 // The parameter of the safelen clause must be a constant
3663 // positive integer expression.
3664 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
3665 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003666 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00003667 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003668 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00003669}
3670
Alexander Musman64d33f12014-06-04 07:53:32 +00003671OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
3672 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00003673 SourceLocation LParenLoc,
3674 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00003675 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00003676 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00003677 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00003678 // The parameter of the collapse clause must be a constant
3679 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00003680 ExprResult NumForLoopsResult =
3681 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
3682 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00003683 return nullptr;
3684 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00003685 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00003686}
3687
Alexey Bataeved09d242014-05-28 05:53:51 +00003688OMPClause *Sema::ActOnOpenMPSimpleClause(
3689 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
3690 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003691 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003692 switch (Kind) {
3693 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00003694 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00003695 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
3696 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003697 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003698 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00003699 Res = ActOnOpenMPProcBindClause(
3700 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
3701 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003702 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003703 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00003704 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00003705 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00003706 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00003707 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003708 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003709 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003710 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00003711 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00003712 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00003713 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00003714 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00003715 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003716 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003717 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003718 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00003719 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003720 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003721 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003722 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00003723 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003724 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00003725 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00003726 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00003727 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003728 case OMPC_seq_cst:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003729 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003730 llvm_unreachable("Clause is not allowed.");
3731 }
3732 return Res;
3733}
3734
3735OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
3736 SourceLocation KindKwLoc,
3737 SourceLocation StartLoc,
3738 SourceLocation LParenLoc,
3739 SourceLocation EndLoc) {
3740 if (Kind == OMPC_DEFAULT_unknown) {
3741 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00003742 static_assert(OMPC_DEFAULT_unknown > 0,
3743 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00003744 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00003745 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003746 Values += "'";
3747 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
3748 Values += "'";
3749 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00003750 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003751 Values += " or ";
3752 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00003753 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003754 break;
3755 default:
3756 Values += Sep;
3757 break;
3758 }
3759 }
3760 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00003761 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003762 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003763 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00003764 switch (Kind) {
3765 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003766 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003767 break;
3768 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003769 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003770 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003771 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003772 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00003773 break;
3774 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003775 return new (Context)
3776 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003777}
3778
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003779OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
3780 SourceLocation KindKwLoc,
3781 SourceLocation StartLoc,
3782 SourceLocation LParenLoc,
3783 SourceLocation EndLoc) {
3784 if (Kind == OMPC_PROC_BIND_unknown) {
3785 std::string Values;
3786 std::string Sep(", ");
3787 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
3788 Values += "'";
3789 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
3790 Values += "'";
3791 switch (i) {
3792 case OMPC_PROC_BIND_unknown - 2:
3793 Values += " or ";
3794 break;
3795 case OMPC_PROC_BIND_unknown - 1:
3796 break;
3797 default:
3798 Values += Sep;
3799 break;
3800 }
3801 }
3802 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00003803 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003804 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003805 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003806 return new (Context)
3807 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003808}
3809
Alexey Bataev56dafe82014-06-20 07:16:17 +00003810OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
3811 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
3812 SourceLocation StartLoc, SourceLocation LParenLoc,
3813 SourceLocation ArgumentLoc, SourceLocation CommaLoc,
3814 SourceLocation EndLoc) {
3815 OMPClause *Res = nullptr;
3816 switch (Kind) {
3817 case OMPC_schedule:
3818 Res = ActOnOpenMPScheduleClause(
3819 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
3820 LParenLoc, ArgumentLoc, CommaLoc, EndLoc);
3821 break;
3822 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00003823 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003824 case OMPC_num_threads:
3825 case OMPC_safelen:
3826 case OMPC_collapse:
3827 case OMPC_default:
3828 case OMPC_proc_bind:
3829 case OMPC_private:
3830 case OMPC_firstprivate:
3831 case OMPC_lastprivate:
3832 case OMPC_shared:
3833 case OMPC_reduction:
3834 case OMPC_linear:
3835 case OMPC_aligned:
3836 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003837 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003838 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00003839 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003840 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003841 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003842 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00003843 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003844 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00003845 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00003846 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00003847 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003848 case OMPC_seq_cst:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003849 case OMPC_unknown:
3850 llvm_unreachable("Clause is not allowed.");
3851 }
3852 return Res;
3853}
3854
3855OMPClause *Sema::ActOnOpenMPScheduleClause(
3856 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
3857 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
3858 SourceLocation EndLoc) {
3859 if (Kind == OMPC_SCHEDULE_unknown) {
3860 std::string Values;
3861 std::string Sep(", ");
3862 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) {
3863 Values += "'";
3864 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i);
3865 Values += "'";
3866 switch (i) {
3867 case OMPC_SCHEDULE_unknown - 2:
3868 Values += " or ";
3869 break;
3870 case OMPC_SCHEDULE_unknown - 1:
3871 break;
3872 default:
3873 Values += Sep;
3874 break;
3875 }
3876 }
3877 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
3878 << Values << getOpenMPClauseName(OMPC_schedule);
3879 return nullptr;
3880 }
3881 Expr *ValExpr = ChunkSize;
3882 if (ChunkSize) {
3883 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
3884 !ChunkSize->isInstantiationDependent() &&
3885 !ChunkSize->containsUnexpandedParameterPack()) {
3886 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
3887 ExprResult Val =
3888 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
3889 if (Val.isInvalid())
3890 return nullptr;
3891
3892 ValExpr = Val.get();
3893
3894 // OpenMP [2.7.1, Restrictions]
3895 // chunk_size must be a loop invariant integer expression with a positive
3896 // value.
3897 llvm::APSInt Result;
3898 if (ValExpr->isIntegerConstantExpr(Result, Context) &&
3899 Result.isSigned() && !Result.isStrictlyPositive()) {
3900 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
3901 << "schedule" << ChunkSize->getSourceRange();
3902 return nullptr;
3903 }
3904 }
3905 }
3906
3907 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc,
3908 EndLoc, Kind, ValExpr);
3909}
3910
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003911OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
3912 SourceLocation StartLoc,
3913 SourceLocation EndLoc) {
3914 OMPClause *Res = nullptr;
3915 switch (Kind) {
3916 case OMPC_ordered:
3917 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
3918 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00003919 case OMPC_nowait:
3920 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
3921 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003922 case OMPC_untied:
3923 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
3924 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003925 case OMPC_mergeable:
3926 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
3927 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003928 case OMPC_read:
3929 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
3930 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00003931 case OMPC_write:
3932 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
3933 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00003934 case OMPC_update:
3935 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
3936 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00003937 case OMPC_capture:
3938 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
3939 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003940 case OMPC_seq_cst:
3941 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
3942 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003943 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00003944 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003945 case OMPC_num_threads:
3946 case OMPC_safelen:
3947 case OMPC_collapse:
3948 case OMPC_schedule:
3949 case OMPC_private:
3950 case OMPC_firstprivate:
3951 case OMPC_lastprivate:
3952 case OMPC_shared:
3953 case OMPC_reduction:
3954 case OMPC_linear:
3955 case OMPC_aligned:
3956 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003957 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003958 case OMPC_default:
3959 case OMPC_proc_bind:
3960 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00003961 case OMPC_flush:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003962 case OMPC_unknown:
3963 llvm_unreachable("Clause is not allowed.");
3964 }
3965 return Res;
3966}
3967
3968OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
3969 SourceLocation EndLoc) {
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003970 DSAStack->setOrderedRegion();
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003971 return new (Context) OMPOrderedClause(StartLoc, EndLoc);
3972}
3973
Alexey Bataev236070f2014-06-20 11:19:47 +00003974OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
3975 SourceLocation EndLoc) {
3976 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
3977}
3978
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003979OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
3980 SourceLocation EndLoc) {
3981 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
3982}
3983
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003984OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
3985 SourceLocation EndLoc) {
3986 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
3987}
3988
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003989OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
3990 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003991 return new (Context) OMPReadClause(StartLoc, EndLoc);
3992}
3993
Alexey Bataevdea47612014-07-23 07:46:59 +00003994OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
3995 SourceLocation EndLoc) {
3996 return new (Context) OMPWriteClause(StartLoc, EndLoc);
3997}
3998
Alexey Bataev67a4f222014-07-23 10:25:33 +00003999OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
4000 SourceLocation EndLoc) {
4001 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
4002}
4003
Alexey Bataev459dec02014-07-24 06:46:57 +00004004OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
4005 SourceLocation EndLoc) {
4006 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
4007}
4008
Alexey Bataev82bad8b2014-07-24 08:55:34 +00004009OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
4010 SourceLocation EndLoc) {
4011 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
4012}
4013
Alexey Bataevc5e02582014-06-16 07:08:35 +00004014OMPClause *Sema::ActOnOpenMPVarListClause(
4015 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
4016 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
4017 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
4018 const DeclarationNameInfo &ReductionId) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004019 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004020 switch (Kind) {
4021 case OMPC_private:
4022 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
4023 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004024 case OMPC_firstprivate:
4025 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
4026 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00004027 case OMPC_lastprivate:
4028 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
4029 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00004030 case OMPC_shared:
4031 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
4032 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00004033 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00004034 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
4035 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00004036 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00004037 case OMPC_linear:
4038 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
4039 ColonLoc, EndLoc);
4040 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00004041 case OMPC_aligned:
4042 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
4043 ColonLoc, EndLoc);
4044 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004045 case OMPC_copyin:
4046 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
4047 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00004048 case OMPC_copyprivate:
4049 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
4050 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00004051 case OMPC_flush:
4052 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
4053 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004054 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00004055 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00004056 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00004057 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00004058 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004059 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004060 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00004061 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004062 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00004063 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00004064 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00004065 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004066 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004067 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00004068 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00004069 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00004070 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00004071 case OMPC_seq_cst:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004072 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004073 llvm_unreachable("Clause is not allowed.");
4074 }
4075 return Res;
4076}
4077
4078OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
4079 SourceLocation StartLoc,
4080 SourceLocation LParenLoc,
4081 SourceLocation EndLoc) {
4082 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00004083 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00004084 for (auto &RefExpr : VarList) {
4085 assert(RefExpr && "NULL expr in OpenMP private clause.");
4086 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004087 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00004088 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00004089 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004090 continue;
4091 }
4092
Alexey Bataeved09d242014-05-28 05:53:51 +00004093 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004094 // OpenMP [2.1, C/C++]
4095 // A list item is a variable name.
4096 // OpenMP [2.9.3.3, Restrictions, p.1]
4097 // A variable that is part of another variable (as an array or
4098 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00004099 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004100 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00004101 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004102 continue;
4103 }
4104 Decl *D = DE->getDecl();
4105 VarDecl *VD = cast<VarDecl>(D);
4106
4107 QualType Type = VD->getType();
4108 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
4109 // It will be analyzed later.
4110 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00004111 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004112 continue;
4113 }
4114
4115 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
4116 // A variable that appears in a private clause must not have an incomplete
4117 // type or a reference type.
4118 if (RequireCompleteType(ELoc, Type,
4119 diag::err_omp_private_incomplete_type)) {
4120 continue;
4121 }
4122 if (Type->isReferenceType()) {
4123 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00004124 << getOpenMPClauseName(OMPC_private) << Type;
4125 bool IsDecl =
4126 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4127 Diag(VD->getLocation(),
4128 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4129 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004130 continue;
4131 }
4132
4133 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
4134 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00004135 // clause requires an accessible, unambiguous default constructor for the
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004136 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00004137 while (Type->isArrayType()) {
4138 Type = cast<ArrayType>(Type.getTypePtr())->getElementType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004139 }
4140
Alexey Bataev758e55e2013-09-06 18:03:48 +00004141 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
4142 // in a Construct]
4143 // Variables with the predetermined data-sharing attributes may not be
4144 // listed in data-sharing attributes clauses, except for the cases
4145 // listed below. For these exceptions only, listing a predetermined
4146 // variable in a data-sharing attribute clause is allowed and overrides
4147 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004148 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004149 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00004150 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
4151 << getOpenMPClauseName(OMPC_private);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004152 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004153 continue;
4154 }
4155
Alexey Bataev03b340a2014-10-21 03:16:40 +00004156 // Generate helper private variable and initialize it with the default
4157 // value. The address of the original variable is replaced by the address of
4158 // the new private variable in CodeGen. This new variable is not added to
4159 // IdResolver, so the code in the OpenMP region uses original variable for
4160 // proper diagnostics.
4161 auto VDPrivate =
4162 VarDecl::Create(Context, CurContext, DE->getLocStart(),
4163 DE->getExprLoc(), VD->getIdentifier(), VD->getType(),
4164 VD->getTypeSourceInfo(), /*S*/ SC_Auto);
4165 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto*/ false);
4166 if (VDPrivate->isInvalidDecl())
4167 continue;
4168 CurContext->addDecl(VDPrivate);
Alexey Bataev07649fb2014-12-16 08:01:48 +00004169 auto VDPrivateRefExpr =
4170 DeclRefExpr::Create(Context, /*QualifierLoc*/ NestedNameSpecifierLoc(),
4171 /*TemplateKWLoc*/ SourceLocation(), VDPrivate,
4172 /*RefersToCapturedVariable*/ false,
4173 /*NameLoc*/ SourceLocation(), DE->getType(),
4174 /*VK*/ VK_LValue);
Alexey Bataev03b340a2014-10-21 03:16:40 +00004175
Alexey Bataev758e55e2013-09-06 18:03:48 +00004176 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004177 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00004178 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004179 }
4180
Alexey Bataeved09d242014-05-28 05:53:51 +00004181 if (Vars.empty())
4182 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004183
Alexey Bataev03b340a2014-10-21 03:16:40 +00004184 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
4185 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004186}
4187
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004188namespace {
4189class DiagsUninitializedSeveretyRAII {
4190private:
4191 DiagnosticsEngine &Diags;
4192 SourceLocation SavedLoc;
4193 bool IsIgnored;
4194
4195public:
4196 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
4197 bool IsIgnored)
4198 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
4199 if (!IsIgnored) {
4200 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
4201 /*Map*/ diag::Severity::Ignored, Loc);
4202 }
4203 }
4204 ~DiagsUninitializedSeveretyRAII() {
4205 if (!IsIgnored)
4206 Diags.popMappings(SavedLoc);
4207 }
4208};
4209}
4210
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004211OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
4212 SourceLocation StartLoc,
4213 SourceLocation LParenLoc,
4214 SourceLocation EndLoc) {
4215 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004216 SmallVector<Expr *, 8> PrivateCopies;
4217 SmallVector<Expr *, 8> Inits;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004218 bool IsImplicitClause =
4219 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
4220 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
4221
Alexey Bataeved09d242014-05-28 05:53:51 +00004222 for (auto &RefExpr : VarList) {
4223 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
4224 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004225 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00004226 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004227 PrivateCopies.push_back(nullptr);
4228 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004229 continue;
4230 }
4231
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004232 SourceLocation ELoc =
4233 IsImplicitClause ? ImplicitClauseLoc : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004234 // OpenMP [2.1, C/C++]
4235 // A list item is a variable name.
4236 // OpenMP [2.9.3.3, Restrictions, p.1]
4237 // A variable that is part of another variable (as an array or
4238 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00004239 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004240 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00004241 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004242 continue;
4243 }
4244 Decl *D = DE->getDecl();
4245 VarDecl *VD = cast<VarDecl>(D);
4246
4247 QualType Type = VD->getType();
4248 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
4249 // It will be analyzed later.
4250 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004251 PrivateCopies.push_back(nullptr);
4252 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004253 continue;
4254 }
4255
4256 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
4257 // A variable that appears in a private clause must not have an incomplete
4258 // type or a reference type.
4259 if (RequireCompleteType(ELoc, Type,
4260 diag::err_omp_firstprivate_incomplete_type)) {
4261 continue;
4262 }
4263 if (Type->isReferenceType()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004264 if (IsImplicitClause) {
4265 Diag(ImplicitClauseLoc,
4266 diag::err_omp_task_predetermined_firstprivate_ref_type_arg)
4267 << Type;
4268 Diag(RefExpr->getExprLoc(), diag::note_used_here);
4269 } else {
4270 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
4271 << getOpenMPClauseName(OMPC_firstprivate) << Type;
4272 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004273 bool IsDecl =
4274 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4275 Diag(VD->getLocation(),
4276 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4277 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004278 continue;
4279 }
4280
4281 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
4282 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00004283 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004284 // class type.
4285 Type = Context.getBaseElementType(Type);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004286
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004287 // If an implicit firstprivate variable found it was checked already.
4288 if (!IsImplicitClause) {
4289 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004290 Type = Type.getNonReferenceType().getCanonicalType();
4291 bool IsConstant = Type.isConstant(Context);
4292 Type = Context.getBaseElementType(Type);
4293 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
4294 // A list item that specifies a given variable may not appear in more
4295 // than one clause on the same directive, except that a variable may be
4296 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004297 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00004298 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004299 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00004300 << getOpenMPClauseName(DVar.CKind)
4301 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004302 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004303 continue;
4304 }
4305
4306 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
4307 // in a Construct]
4308 // Variables with the predetermined data-sharing attributes may not be
4309 // listed in data-sharing attributes clauses, except for the cases
4310 // listed below. For these exceptions only, listing a predetermined
4311 // variable in a data-sharing attribute clause is allowed and overrides
4312 // the variable's predetermined data-sharing attributes.
4313 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
4314 // in a Construct, C/C++, p.2]
4315 // Variables with const-qualified type having no mutable member may be
4316 // listed in a firstprivate clause, even if they are static data members.
4317 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
4318 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
4319 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00004320 << getOpenMPClauseName(DVar.CKind)
4321 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004322 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004323 continue;
4324 }
4325
Alexey Bataevf29276e2014-06-18 04:14:57 +00004326 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004327 // OpenMP [2.9.3.4, Restrictions, p.2]
4328 // A list item that is private within a parallel region must not appear
4329 // in a firstprivate clause on a worksharing construct if any of the
4330 // worksharing regions arising from the worksharing construct ever bind
4331 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00004332 if (isOpenMPWorksharingDirective(CurrDir) &&
4333 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004334 DVar = DSAStack->getImplicitDSA(VD, true);
4335 if (DVar.CKind != OMPC_shared &&
4336 (isOpenMPParallelDirective(DVar.DKind) ||
4337 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00004338 Diag(ELoc, diag::err_omp_required_access)
4339 << getOpenMPClauseName(OMPC_firstprivate)
4340 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004341 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004342 continue;
4343 }
4344 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004345 // OpenMP [2.9.3.4, Restrictions, p.3]
4346 // A list item that appears in a reduction clause of a parallel construct
4347 // must not appear in a firstprivate clause on a worksharing or task
4348 // construct if any of the worksharing or task regions arising from the
4349 // worksharing or task construct ever bind to any of the parallel regions
4350 // arising from the parallel construct.
4351 // OpenMP [2.9.3.4, Restrictions, p.4]
4352 // A list item that appears in a reduction clause in worksharing
4353 // construct must not appear in a firstprivate clause in a task construct
4354 // encountered during execution of any of the worksharing regions arising
4355 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004356 if (CurrDir == OMPD_task) {
4357 DVar =
4358 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
4359 [](OpenMPDirectiveKind K) -> bool {
4360 return isOpenMPParallelDirective(K) ||
4361 isOpenMPWorksharingDirective(K);
4362 },
4363 false);
4364 if (DVar.CKind == OMPC_reduction &&
4365 (isOpenMPParallelDirective(DVar.DKind) ||
4366 isOpenMPWorksharingDirective(DVar.DKind))) {
4367 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
4368 << getOpenMPDirectiveName(DVar.DKind);
4369 ReportOriginalDSA(*this, DSAStack, VD, DVar);
4370 continue;
4371 }
4372 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004373 }
4374
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004375 Type = Type.getUnqualifiedType();
4376 auto VDPrivate = VarDecl::Create(Context, CurContext, DE->getLocStart(),
4377 ELoc, VD->getIdentifier(), VD->getType(),
4378 VD->getTypeSourceInfo(), /*S*/ SC_Auto);
4379 // Generate helper private variable and initialize it with the value of the
4380 // original variable. The address of the original variable is replaced by
4381 // the address of the new private variable in the CodeGen. This new variable
4382 // is not added to IdResolver, so the code in the OpenMP region uses
4383 // original variable for proper diagnostics and variable capturing.
4384 Expr *VDInitRefExpr = nullptr;
4385 // For arrays generate initializer for single element and replace it by the
4386 // original array element in CodeGen.
4387 if (DE->getType()->isArrayType()) {
4388 auto VDInit = VarDecl::Create(Context, CurContext, DE->getLocStart(),
4389 ELoc, VD->getIdentifier(), Type,
4390 VD->getTypeSourceInfo(), /*S*/ SC_Auto);
4391 CurContext->addHiddenDecl(VDInit);
4392 VDInitRefExpr = DeclRefExpr::Create(
4393 Context, /*QualifierLoc*/ NestedNameSpecifierLoc(),
4394 /*TemplateKWLoc*/ SourceLocation(), VDInit,
Alexey Bataev07649fb2014-12-16 08:01:48 +00004395 /*RefersToCapturedVariable*/ true, ELoc, Type,
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004396 /*VK*/ VK_LValue);
4397 VDInit->setIsUsed();
4398 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
4399 InitializedEntity Entity = InitializedEntity::InitializeVariable(VDInit);
4400 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
4401
4402 InitializationSequence InitSeq(*this, Entity, Kind, Init);
4403 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
4404 if (Result.isInvalid())
4405 VDPrivate->setInvalidDecl();
4406 else
4407 VDPrivate->setInit(Result.getAs<Expr>());
4408 } else {
Alexey Bataevf841bd92014-12-16 07:00:22 +00004409 AddInitializerToDecl(
4410 VDPrivate, DefaultLvalueConversion(
4411 DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
4412 SourceLocation(), DE->getDecl(),
Alexey Bataev07649fb2014-12-16 08:01:48 +00004413 /*RefersToCapturedVariable=*/true,
Alexey Bataevf841bd92014-12-16 07:00:22 +00004414 DE->getExprLoc(), DE->getType(),
4415 /*VK=*/VK_LValue)).get(),
4416 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004417 }
4418 if (VDPrivate->isInvalidDecl()) {
4419 if (IsImplicitClause) {
4420 Diag(DE->getExprLoc(),
4421 diag::note_omp_task_predetermined_firstprivate_here);
4422 }
4423 continue;
4424 }
4425 CurContext->addDecl(VDPrivate);
4426 auto VDPrivateRefExpr = DeclRefExpr::Create(
4427 Context, /*QualifierLoc*/ NestedNameSpecifierLoc(),
4428 /*TemplateKWLoc*/ SourceLocation(), VDPrivate,
Alexey Bataev07649fb2014-12-16 08:01:48 +00004429 /*RefersToCapturedVariable*/ false, DE->getLocStart(), DE->getType(),
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004430 /*VK*/ VK_LValue);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004431 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
4432 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004433 PrivateCopies.push_back(VDPrivateRefExpr);
4434 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004435 }
4436
Alexey Bataeved09d242014-05-28 05:53:51 +00004437 if (Vars.empty())
4438 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004439
4440 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004441 Vars, PrivateCopies, Inits);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004442}
4443
Alexander Musman1bb328c2014-06-04 13:06:39 +00004444OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
4445 SourceLocation StartLoc,
4446 SourceLocation LParenLoc,
4447 SourceLocation EndLoc) {
4448 SmallVector<Expr *, 8> Vars;
4449 for (auto &RefExpr : VarList) {
4450 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
4451 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
4452 // It will be analyzed later.
4453 Vars.push_back(RefExpr);
4454 continue;
4455 }
4456
4457 SourceLocation ELoc = RefExpr->getExprLoc();
4458 // OpenMP [2.1, C/C++]
4459 // A list item is a variable name.
4460 // OpenMP [2.14.3.5, Restrictions, p.1]
4461 // A variable that is part of another variable (as an array or structure
4462 // element) cannot appear in a lastprivate clause.
4463 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
4464 if (!DE || !isa<VarDecl>(DE->getDecl())) {
4465 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
4466 continue;
4467 }
4468 Decl *D = DE->getDecl();
4469 VarDecl *VD = cast<VarDecl>(D);
4470
4471 QualType Type = VD->getType();
4472 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
4473 // It will be analyzed later.
4474 Vars.push_back(DE);
4475 continue;
4476 }
4477
4478 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
4479 // A variable that appears in a lastprivate clause must not have an
4480 // incomplete type or a reference type.
4481 if (RequireCompleteType(ELoc, Type,
4482 diag::err_omp_lastprivate_incomplete_type)) {
4483 continue;
4484 }
4485 if (Type->isReferenceType()) {
4486 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
4487 << getOpenMPClauseName(OMPC_lastprivate) << Type;
4488 bool IsDecl =
4489 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4490 Diag(VD->getLocation(),
4491 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4492 << VD;
4493 continue;
4494 }
4495
4496 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4497 // in a Construct]
4498 // Variables with the predetermined data-sharing attributes may not be
4499 // listed in data-sharing attributes clauses, except for the cases
4500 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004501 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00004502 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
4503 DVar.CKind != OMPC_firstprivate &&
4504 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
4505 Diag(ELoc, diag::err_omp_wrong_dsa)
4506 << getOpenMPClauseName(DVar.CKind)
4507 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004508 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00004509 continue;
4510 }
4511
Alexey Bataevf29276e2014-06-18 04:14:57 +00004512 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
4513 // OpenMP [2.14.3.5, Restrictions, p.2]
4514 // A list item that is private within a parallel region, or that appears in
4515 // the reduction clause of a parallel construct, must not appear in a
4516 // lastprivate clause on a worksharing construct if any of the corresponding
4517 // worksharing regions ever binds to any of the corresponding parallel
4518 // regions.
Alexey Bataev549210e2014-06-24 04:39:47 +00004519 if (isOpenMPWorksharingDirective(CurrDir) &&
4520 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004521 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004522 if (DVar.CKind != OMPC_shared) {
4523 Diag(ELoc, diag::err_omp_required_access)
4524 << getOpenMPClauseName(OMPC_lastprivate)
4525 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004526 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004527 continue;
4528 }
4529 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00004530 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00004531 // A variable of class type (or array thereof) that appears in a
4532 // lastprivate clause requires an accessible, unambiguous default
4533 // constructor for the class type, unless the list item is also specified
4534 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00004535 // A variable of class type (or array thereof) that appears in a
4536 // lastprivate clause requires an accessible, unambiguous copy assignment
4537 // operator for the class type.
4538 while (Type.getNonReferenceType()->isArrayType())
4539 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
4540 ->getElementType();
4541 CXXRecordDecl *RD = getLangOpts().CPlusPlus
4542 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
4543 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00004544 // FIXME This code must be replaced by actual copying and destructing of the
4545 // lastprivate variable.
Alexander Musman1bb328c2014-06-04 13:06:39 +00004546 if (RD) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00004547 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
4548 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00004549 if (MD) {
4550 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
4551 MD->isDeleted()) {
4552 Diag(ELoc, diag::err_omp_required_method)
4553 << getOpenMPClauseName(OMPC_lastprivate) << 2;
4554 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4555 VarDecl::DeclarationOnly;
4556 Diag(VD->getLocation(),
4557 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4558 << VD;
4559 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4560 continue;
4561 }
4562 MarkFunctionReferenced(ELoc, MD);
4563 DiagnoseUseOfDecl(MD, ELoc);
Alexander Musman1bb328c2014-06-04 13:06:39 +00004564 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00004565
4566 CXXDestructorDecl *DD = RD->getDestructor();
4567 if (DD) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00004568 PartialDiagnostic PD =
4569 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexander Musman1bb328c2014-06-04 13:06:39 +00004570 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
4571 DD->isDeleted()) {
4572 Diag(ELoc, diag::err_omp_required_method)
4573 << getOpenMPClauseName(OMPC_lastprivate) << 4;
4574 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4575 VarDecl::DeclarationOnly;
4576 Diag(VD->getLocation(),
4577 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4578 << VD;
4579 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4580 continue;
4581 }
4582 MarkFunctionReferenced(ELoc, DD);
4583 DiagnoseUseOfDecl(DD, ELoc);
4584 }
4585 }
4586
Alexey Bataevf29276e2014-06-18 04:14:57 +00004587 if (DVar.CKind != OMPC_firstprivate)
4588 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00004589 Vars.push_back(DE);
4590 }
4591
4592 if (Vars.empty())
4593 return nullptr;
4594
4595 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
4596 Vars);
4597}
4598
Alexey Bataev758e55e2013-09-06 18:03:48 +00004599OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
4600 SourceLocation StartLoc,
4601 SourceLocation LParenLoc,
4602 SourceLocation EndLoc) {
4603 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00004604 for (auto &RefExpr : VarList) {
4605 assert(RefExpr && "NULL expr in OpenMP shared clause.");
4606 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00004607 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00004608 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004609 continue;
4610 }
4611
Alexey Bataeved09d242014-05-28 05:53:51 +00004612 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00004613 // OpenMP [2.1, C/C++]
4614 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00004615 // OpenMP [2.14.3.2, Restrictions, p.1]
4616 // A variable that is part of another variable (as an array or structure
4617 // element) cannot appear in a shared unless it is a static data member
4618 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00004619 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004620 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00004621 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00004622 continue;
4623 }
4624 Decl *D = DE->getDecl();
4625 VarDecl *VD = cast<VarDecl>(D);
4626
4627 QualType Type = VD->getType();
4628 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
4629 // It will be analyzed later.
4630 Vars.push_back(DE);
4631 continue;
4632 }
4633
4634 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
4635 // in a Construct]
4636 // Variables with the predetermined data-sharing attributes may not be
4637 // listed in data-sharing attributes clauses, except for the cases
4638 // listed below. For these exceptions only, listing a predetermined
4639 // variable in a data-sharing attribute clause is allowed and overrides
4640 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004641 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00004642 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
4643 DVar.RefExpr) {
4644 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
4645 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004646 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004647 continue;
4648 }
4649
4650 DSAStack->addDSA(VD, DE, OMPC_shared);
4651 Vars.push_back(DE);
4652 }
4653
Alexey Bataeved09d242014-05-28 05:53:51 +00004654 if (Vars.empty())
4655 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00004656
4657 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
4658}
4659
Alexey Bataevc5e02582014-06-16 07:08:35 +00004660namespace {
4661class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
4662 DSAStackTy *Stack;
4663
4664public:
4665 bool VisitDeclRefExpr(DeclRefExpr *E) {
4666 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004667 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00004668 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
4669 return false;
4670 if (DVar.CKind != OMPC_unknown)
4671 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00004672 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004673 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004674 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00004675 return true;
4676 return false;
4677 }
4678 return false;
4679 }
4680 bool VisitStmt(Stmt *S) {
4681 for (auto Child : S->children()) {
4682 if (Child && Visit(Child))
4683 return true;
4684 }
4685 return false;
4686 }
Alexey Bataev23b69422014-06-18 07:08:49 +00004687 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00004688};
Alexey Bataev23b69422014-06-18 07:08:49 +00004689} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00004690
4691OMPClause *Sema::ActOnOpenMPReductionClause(
4692 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
4693 SourceLocation ColonLoc, SourceLocation EndLoc,
4694 CXXScopeSpec &ReductionIdScopeSpec,
4695 const DeclarationNameInfo &ReductionId) {
4696 // TODO: Allow scope specification search when 'declare reduction' is
4697 // supported.
4698 assert(ReductionIdScopeSpec.isEmpty() &&
4699 "No support for scoped reduction identifiers yet.");
4700
4701 auto DN = ReductionId.getName();
4702 auto OOK = DN.getCXXOverloadedOperator();
4703 BinaryOperatorKind BOK = BO_Comma;
4704
4705 // OpenMP [2.14.3.6, reduction clause]
4706 // C
4707 // reduction-identifier is either an identifier or one of the following
4708 // operators: +, -, *, &, |, ^, && and ||
4709 // C++
4710 // reduction-identifier is either an id-expression or one of the following
4711 // operators: +, -, *, &, |, ^, && and ||
4712 // FIXME: Only 'min' and 'max' identifiers are supported for now.
4713 switch (OOK) {
4714 case OO_Plus:
4715 case OO_Minus:
4716 BOK = BO_AddAssign;
4717 break;
4718 case OO_Star:
4719 BOK = BO_MulAssign;
4720 break;
4721 case OO_Amp:
4722 BOK = BO_AndAssign;
4723 break;
4724 case OO_Pipe:
4725 BOK = BO_OrAssign;
4726 break;
4727 case OO_Caret:
4728 BOK = BO_XorAssign;
4729 break;
4730 case OO_AmpAmp:
4731 BOK = BO_LAnd;
4732 break;
4733 case OO_PipePipe:
4734 BOK = BO_LOr;
4735 break;
4736 default:
4737 if (auto II = DN.getAsIdentifierInfo()) {
4738 if (II->isStr("max"))
4739 BOK = BO_GT;
4740 else if (II->isStr("min"))
4741 BOK = BO_LT;
4742 }
4743 break;
4744 }
4745 SourceRange ReductionIdRange;
4746 if (ReductionIdScopeSpec.isValid()) {
4747 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
4748 }
4749 ReductionIdRange.setEnd(ReductionId.getEndLoc());
4750 if (BOK == BO_Comma) {
4751 // Not allowed reduction identifier is found.
4752 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
4753 << ReductionIdRange;
4754 return nullptr;
4755 }
4756
4757 SmallVector<Expr *, 8> Vars;
4758 for (auto RefExpr : VarList) {
4759 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
4760 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
4761 // It will be analyzed later.
4762 Vars.push_back(RefExpr);
4763 continue;
4764 }
4765
4766 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
4767 RefExpr->isInstantiationDependent() ||
4768 RefExpr->containsUnexpandedParameterPack()) {
4769 // It will be analyzed later.
4770 Vars.push_back(RefExpr);
4771 continue;
4772 }
4773
4774 auto ELoc = RefExpr->getExprLoc();
4775 auto ERange = RefExpr->getSourceRange();
4776 // OpenMP [2.1, C/C++]
4777 // A list item is a variable or array section, subject to the restrictions
4778 // specified in Section 2.4 on page 42 and in each of the sections
4779 // describing clauses and directives for which a list appears.
4780 // OpenMP [2.14.3.3, Restrictions, p.1]
4781 // A variable that is part of another variable (as an array or
4782 // structure element) cannot appear in a private clause.
4783 auto DE = dyn_cast<DeclRefExpr>(RefExpr);
4784 if (!DE || !isa<VarDecl>(DE->getDecl())) {
4785 Diag(ELoc, diag::err_omp_expected_var_name) << ERange;
4786 continue;
4787 }
4788 auto D = DE->getDecl();
4789 auto VD = cast<VarDecl>(D);
4790 auto Type = VD->getType();
4791 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
4792 // A variable that appears in a private clause must not have an incomplete
4793 // type or a reference type.
4794 if (RequireCompleteType(ELoc, Type,
4795 diag::err_omp_reduction_incomplete_type))
4796 continue;
4797 // OpenMP [2.14.3.6, reduction clause, Restrictions]
4798 // Arrays may not appear in a reduction clause.
4799 if (Type.getNonReferenceType()->isArrayType()) {
4800 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
4801 bool IsDecl =
4802 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4803 Diag(VD->getLocation(),
4804 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4805 << VD;
4806 continue;
4807 }
4808 // OpenMP [2.14.3.6, reduction clause, Restrictions]
4809 // A list item that appears in a reduction clause must not be
4810 // const-qualified.
4811 if (Type.getNonReferenceType().isConstant(Context)) {
4812 Diag(ELoc, diag::err_omp_const_variable)
4813 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
4814 bool IsDecl =
4815 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4816 Diag(VD->getLocation(),
4817 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4818 << VD;
4819 continue;
4820 }
4821 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
4822 // If a list-item is a reference type then it must bind to the same object
4823 // for all threads of the team.
4824 VarDecl *VDDef = VD->getDefinition();
4825 if (Type->isReferenceType() && VDDef) {
4826 DSARefChecker Check(DSAStack);
4827 if (Check.Visit(VDDef->getInit())) {
4828 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
4829 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
4830 continue;
4831 }
4832 }
4833 // OpenMP [2.14.3.6, reduction clause, Restrictions]
4834 // The type of a list item that appears in a reduction clause must be valid
4835 // for the reduction-identifier. For a max or min reduction in C, the type
4836 // of the list item must be an allowed arithmetic data type: char, int,
4837 // float, double, or _Bool, possibly modified with long, short, signed, or
4838 // unsigned. For a max or min reduction in C++, the type of the list item
4839 // must be an allowed arithmetic data type: char, wchar_t, int, float,
4840 // double, or bool, possibly modified with long, short, signed, or unsigned.
4841 if ((BOK == BO_GT || BOK == BO_LT) &&
4842 !(Type->isScalarType() ||
4843 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
4844 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
4845 << getLangOpts().CPlusPlus;
4846 bool IsDecl =
4847 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4848 Diag(VD->getLocation(),
4849 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4850 << VD;
4851 continue;
4852 }
4853 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
4854 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
4855 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
4856 bool IsDecl =
4857 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4858 Diag(VD->getLocation(),
4859 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4860 << VD;
4861 continue;
4862 }
4863 bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
4864 getDiagnostics().setSuppressAllDiagnostics(true);
4865 ExprResult ReductionOp =
4866 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
4867 RefExpr, RefExpr);
4868 getDiagnostics().setSuppressAllDiagnostics(Suppress);
4869 if (ReductionOp.isInvalid()) {
4870 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
Alexey Bataev23b69422014-06-18 07:08:49 +00004871 << ReductionIdRange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00004872 bool IsDecl =
4873 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4874 Diag(VD->getLocation(),
4875 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4876 << VD;
4877 continue;
4878 }
4879
4880 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4881 // in a Construct]
4882 // Variables with the predetermined data-sharing attributes may not be
4883 // listed in data-sharing attributes clauses, except for the cases
4884 // listed below. For these exceptions only, listing a predetermined
4885 // variable in a data-sharing attribute clause is allowed and overrides
4886 // the variable's predetermined data-sharing attributes.
4887 // OpenMP [2.14.3.6, Restrictions, p.3]
4888 // Any number of reduction clauses can be specified on the directive,
4889 // but a list item can appear only once in the reduction clauses for that
4890 // directive.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004891 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00004892 if (DVar.CKind == OMPC_reduction) {
4893 Diag(ELoc, diag::err_omp_once_referenced)
4894 << getOpenMPClauseName(OMPC_reduction);
4895 if (DVar.RefExpr) {
4896 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
4897 }
4898 } else if (DVar.CKind != OMPC_unknown) {
4899 Diag(ELoc, diag::err_omp_wrong_dsa)
4900 << getOpenMPClauseName(DVar.CKind)
4901 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004902 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00004903 continue;
4904 }
4905
4906 // OpenMP [2.14.3.6, Restrictions, p.1]
4907 // A list item that appears in a reduction clause of a worksharing
4908 // construct must be shared in the parallel regions to which any of the
4909 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf29276e2014-06-18 04:14:57 +00004910 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataev549210e2014-06-24 04:39:47 +00004911 if (isOpenMPWorksharingDirective(CurrDir) &&
4912 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004913 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004914 if (DVar.CKind != OMPC_shared) {
4915 Diag(ELoc, diag::err_omp_required_access)
4916 << getOpenMPClauseName(OMPC_reduction)
4917 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004918 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004919 continue;
4920 }
4921 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00004922
4923 CXXRecordDecl *RD = getLangOpts().CPlusPlus
4924 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
4925 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00004926 // FIXME This code must be replaced by actual constructing/destructing of
4927 // the reduction variable.
Alexey Bataevc5e02582014-06-16 07:08:35 +00004928 if (RD) {
4929 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
4930 PartialDiagnostic PD =
4931 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataev23b69422014-06-18 07:08:49 +00004932 if (!CD ||
4933 CheckConstructorAccess(ELoc, CD,
4934 InitializedEntity::InitializeTemporary(Type),
4935 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevc5e02582014-06-16 07:08:35 +00004936 CD->isDeleted()) {
4937 Diag(ELoc, diag::err_omp_required_method)
4938 << getOpenMPClauseName(OMPC_reduction) << 0;
4939 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4940 VarDecl::DeclarationOnly;
4941 Diag(VD->getLocation(),
4942 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4943 << VD;
4944 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4945 continue;
4946 }
4947 MarkFunctionReferenced(ELoc, CD);
4948 DiagnoseUseOfDecl(CD, ELoc);
4949
4950 CXXDestructorDecl *DD = RD->getDestructor();
4951 if (DD) {
4952 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
4953 DD->isDeleted()) {
4954 Diag(ELoc, diag::err_omp_required_method)
4955 << getOpenMPClauseName(OMPC_reduction) << 4;
4956 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4957 VarDecl::DeclarationOnly;
4958 Diag(VD->getLocation(),
4959 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4960 << VD;
4961 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4962 continue;
4963 }
4964 MarkFunctionReferenced(ELoc, DD);
4965 DiagnoseUseOfDecl(DD, ELoc);
4966 }
4967 }
4968
4969 DSAStack->addDSA(VD, DE, OMPC_reduction);
4970 Vars.push_back(DE);
4971 }
4972
4973 if (Vars.empty())
4974 return nullptr;
4975
4976 return OMPReductionClause::Create(
4977 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
4978 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId);
4979}
4980
Alexander Musman8dba6642014-04-22 13:09:42 +00004981OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step,
4982 SourceLocation StartLoc,
4983 SourceLocation LParenLoc,
4984 SourceLocation ColonLoc,
4985 SourceLocation EndLoc) {
4986 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00004987 for (auto &RefExpr : VarList) {
4988 assert(RefExpr && "NULL expr in OpenMP linear clause.");
4989 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00004990 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00004991 Vars.push_back(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00004992 continue;
4993 }
4994
4995 // OpenMP [2.14.3.7, linear clause]
4996 // A list item that appears in a linear clause is subject to the private
4997 // clause semantics described in Section 2.14.3.3 on page 159 except as
4998 // noted. In addition, the value of the new list item on each iteration
4999 // of the associated loop(s) corresponds to the value of the original
5000 // list item before entering the construct plus the logical number of
5001 // the iteration times linear-step.
5002
Alexey Bataeved09d242014-05-28 05:53:51 +00005003 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00005004 // OpenMP [2.1, C/C++]
5005 // A list item is a variable name.
5006 // OpenMP [2.14.3.3, Restrictions, p.1]
5007 // A variable that is part of another variable (as an array or
5008 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00005009 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00005010 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005011 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00005012 continue;
5013 }
5014
5015 VarDecl *VD = cast<VarDecl>(DE->getDecl());
5016
5017 // OpenMP [2.14.3.7, linear clause]
5018 // A list-item cannot appear in more than one linear clause.
5019 // A list-item that appears in a linear clause cannot appear in any
5020 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005021 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00005022 if (DVar.RefExpr) {
5023 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
5024 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005025 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00005026 continue;
5027 }
5028
5029 QualType QType = VD->getType();
5030 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
5031 // It will be analyzed later.
5032 Vars.push_back(DE);
5033 continue;
5034 }
5035
5036 // A variable must not have an incomplete type or a reference type.
5037 if (RequireCompleteType(ELoc, QType,
5038 diag::err_omp_linear_incomplete_type)) {
5039 continue;
5040 }
5041 if (QType->isReferenceType()) {
5042 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
5043 << getOpenMPClauseName(OMPC_linear) << QType;
5044 bool IsDecl =
5045 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5046 Diag(VD->getLocation(),
5047 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5048 << VD;
5049 continue;
5050 }
5051
5052 // A list item must not be const-qualified.
5053 if (QType.isConstant(Context)) {
5054 Diag(ELoc, diag::err_omp_const_variable)
5055 << getOpenMPClauseName(OMPC_linear);
5056 bool IsDecl =
5057 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5058 Diag(VD->getLocation(),
5059 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5060 << VD;
5061 continue;
5062 }
5063
5064 // A list item must be of integral or pointer type.
5065 QType = QType.getUnqualifiedType().getCanonicalType();
5066 const Type *Ty = QType.getTypePtrOrNull();
5067 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
5068 !Ty->isPointerType())) {
5069 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
5070 bool IsDecl =
5071 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5072 Diag(VD->getLocation(),
5073 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5074 << VD;
5075 continue;
5076 }
5077
5078 DSAStack->addDSA(VD, DE, OMPC_linear);
5079 Vars.push_back(DE);
5080 }
5081
5082 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005083 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00005084
5085 Expr *StepExpr = Step;
5086 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
5087 !Step->isInstantiationDependent() &&
5088 !Step->containsUnexpandedParameterPack()) {
5089 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005090 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00005091 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005092 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00005093 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00005094
5095 // Warn about zero linear step (it would be probably better specified as
5096 // making corresponding variables 'const').
5097 llvm::APSInt Result;
5098 if (StepExpr->isIntegerConstantExpr(Result, Context) &&
5099 !Result.isNegative() && !Result.isStrictlyPositive())
5100 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
5101 << (Vars.size() > 1);
5102 }
5103
5104 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc,
5105 Vars, StepExpr);
5106}
5107
Alexander Musmanf0d76e72014-05-29 14:36:25 +00005108OMPClause *Sema::ActOnOpenMPAlignedClause(
5109 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
5110 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
5111
5112 SmallVector<Expr *, 8> Vars;
5113 for (auto &RefExpr : VarList) {
5114 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
5115 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
5116 // It will be analyzed later.
5117 Vars.push_back(RefExpr);
5118 continue;
5119 }
5120
5121 SourceLocation ELoc = RefExpr->getExprLoc();
5122 // OpenMP [2.1, C/C++]
5123 // A list item is a variable name.
5124 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
5125 if (!DE || !isa<VarDecl>(DE->getDecl())) {
5126 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
5127 continue;
5128 }
5129
5130 VarDecl *VD = cast<VarDecl>(DE->getDecl());
5131
5132 // OpenMP [2.8.1, simd construct, Restrictions]
5133 // The type of list items appearing in the aligned clause must be
5134 // array, pointer, reference to array, or reference to pointer.
5135 QualType QType = DE->getType()
5136 .getNonReferenceType()
5137 .getUnqualifiedType()
5138 .getCanonicalType();
5139 const Type *Ty = QType.getTypePtrOrNull();
5140 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
5141 !Ty->isPointerType())) {
5142 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
5143 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
5144 bool IsDecl =
5145 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5146 Diag(VD->getLocation(),
5147 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5148 << VD;
5149 continue;
5150 }
5151
5152 // OpenMP [2.8.1, simd construct, Restrictions]
5153 // A list-item cannot appear in more than one aligned clause.
5154 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
5155 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
5156 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
5157 << getOpenMPClauseName(OMPC_aligned);
5158 continue;
5159 }
5160
5161 Vars.push_back(DE);
5162 }
5163
5164 // OpenMP [2.8.1, simd construct, Description]
5165 // The parameter of the aligned clause, alignment, must be a constant
5166 // positive integer expression.
5167 // If no optional parameter is specified, implementation-defined default
5168 // alignments for SIMD instructions on the target platforms are assumed.
5169 if (Alignment != nullptr) {
5170 ExprResult AlignResult =
5171 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
5172 if (AlignResult.isInvalid())
5173 return nullptr;
5174 Alignment = AlignResult.get();
5175 }
5176 if (Vars.empty())
5177 return nullptr;
5178
5179 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
5180 EndLoc, Vars, Alignment);
5181}
5182
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005183OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
5184 SourceLocation StartLoc,
5185 SourceLocation LParenLoc,
5186 SourceLocation EndLoc) {
5187 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00005188 for (auto &RefExpr : VarList) {
5189 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
5190 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005191 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00005192 Vars.push_back(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005193 continue;
5194 }
5195
Alexey Bataeved09d242014-05-28 05:53:51 +00005196 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005197 // OpenMP [2.1, C/C++]
5198 // A list item is a variable name.
5199 // OpenMP [2.14.4.1, Restrictions, p.1]
5200 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00005201 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005202 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005203 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005204 continue;
5205 }
5206
5207 Decl *D = DE->getDecl();
5208 VarDecl *VD = cast<VarDecl>(D);
5209
5210 QualType Type = VD->getType();
5211 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5212 // It will be analyzed later.
5213 Vars.push_back(DE);
5214 continue;
5215 }
5216
5217 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
5218 // A list item that appears in a copyin clause must be threadprivate.
5219 if (!DSAStack->isThreadPrivate(VD)) {
5220 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00005221 << getOpenMPClauseName(OMPC_copyin)
5222 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005223 continue;
5224 }
5225
5226 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
5227 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00005228 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005229 // operator for the class type.
5230 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005231 CXXRecordDecl *RD =
5232 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00005233 // FIXME This code must be replaced by actual assignment of the
5234 // threadprivate variable.
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005235 if (RD) {
5236 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
5237 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00005238 if (MD) {
5239 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
5240 MD->isDeleted()) {
5241 Diag(ELoc, diag::err_omp_required_method)
5242 << getOpenMPClauseName(OMPC_copyin) << 2;
5243 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
5244 VarDecl::DeclarationOnly;
5245 Diag(VD->getLocation(),
5246 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5247 << VD;
5248 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
5249 continue;
5250 }
5251 MarkFunctionReferenced(ELoc, MD);
5252 DiagnoseUseOfDecl(MD, ELoc);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005253 }
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005254 }
5255
5256 DSAStack->addDSA(VD, DE, OMPC_copyin);
5257 Vars.push_back(DE);
5258 }
5259
Alexey Bataeved09d242014-05-28 05:53:51 +00005260 if (Vars.empty())
5261 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005262
5263 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
5264}
5265
Alexey Bataevbae9a792014-06-27 10:37:06 +00005266OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
5267 SourceLocation StartLoc,
5268 SourceLocation LParenLoc,
5269 SourceLocation EndLoc) {
5270 SmallVector<Expr *, 8> Vars;
5271 for (auto &RefExpr : VarList) {
5272 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
5273 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
5274 // It will be analyzed later.
5275 Vars.push_back(RefExpr);
5276 continue;
5277 }
5278
5279 SourceLocation ELoc = RefExpr->getExprLoc();
5280 // OpenMP [2.1, C/C++]
5281 // A list item is a variable name.
5282 // OpenMP [2.14.4.1, Restrictions, p.1]
5283 // A list item that appears in a copyin clause must be threadprivate.
5284 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
5285 if (!DE || !isa<VarDecl>(DE->getDecl())) {
5286 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
5287 continue;
5288 }
5289
5290 Decl *D = DE->getDecl();
5291 VarDecl *VD = cast<VarDecl>(D);
5292
5293 QualType Type = VD->getType();
5294 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5295 // It will be analyzed later.
5296 Vars.push_back(DE);
5297 continue;
5298 }
5299
5300 // OpenMP [2.14.4.2, Restrictions, p.2]
5301 // A list item that appears in a copyprivate clause may not appear in a
5302 // private or firstprivate clause on the single construct.
5303 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005304 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00005305 if (DVar.CKind != OMPC_copyprivate && DVar.CKind != OMPC_unknown &&
5306 !(DVar.CKind == OMPC_private && !DVar.RefExpr)) {
5307 Diag(ELoc, diag::err_omp_wrong_dsa)
5308 << getOpenMPClauseName(DVar.CKind)
5309 << getOpenMPClauseName(OMPC_copyprivate);
5310 ReportOriginalDSA(*this, DSAStack, VD, DVar);
5311 continue;
5312 }
5313
5314 // OpenMP [2.11.4.2, Restrictions, p.1]
5315 // All list items that appear in a copyprivate clause must be either
5316 // threadprivate or private in the enclosing context.
5317 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005318 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00005319 if (DVar.CKind == OMPC_shared) {
5320 Diag(ELoc, diag::err_omp_required_access)
5321 << getOpenMPClauseName(OMPC_copyprivate)
5322 << "threadprivate or private in the enclosing context";
5323 ReportOriginalDSA(*this, DSAStack, VD, DVar);
5324 continue;
5325 }
5326 }
5327 }
5328
5329 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
5330 // A variable of class type (or array thereof) that appears in a
5331 // copyin clause requires an accessible, unambiguous copy assignment
5332 // operator for the class type.
5333 Type = Context.getBaseElementType(Type);
5334 CXXRecordDecl *RD =
5335 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
5336 // FIXME This code must be replaced by actual assignment of the
5337 // threadprivate variable.
5338 if (RD) {
5339 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
5340 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
5341 if (MD) {
5342 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
5343 MD->isDeleted()) {
5344 Diag(ELoc, diag::err_omp_required_method)
5345 << getOpenMPClauseName(OMPC_copyprivate) << 2;
5346 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
5347 VarDecl::DeclarationOnly;
5348 Diag(VD->getLocation(),
5349 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5350 << VD;
5351 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
5352 continue;
5353 }
5354 MarkFunctionReferenced(ELoc, MD);
5355 DiagnoseUseOfDecl(MD, ELoc);
5356 }
5357 }
5358
5359 // No need to mark vars as copyprivate, they are already threadprivate or
5360 // implicitly private.
5361 Vars.push_back(DE);
5362 }
5363
5364 if (Vars.empty())
5365 return nullptr;
5366
5367 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
5368}
5369
Alexey Bataev6125da92014-07-21 11:26:11 +00005370OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
5371 SourceLocation StartLoc,
5372 SourceLocation LParenLoc,
5373 SourceLocation EndLoc) {
5374 if (VarList.empty())
5375 return nullptr;
5376
5377 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
5378}
Alexey Bataevdea47612014-07-23 07:46:59 +00005379