blob: 5407ba66fd83202af1dc415d50b598d93011e1f1 [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 Bataeva769e072013-03-22 06:34:35 +000016#include "clang/AST/Decl.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000017#include "clang/AST/DeclCXX.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000018#include "clang/AST/DeclOpenMP.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000019#include "clang/AST/StmtCXX.h"
20#include "clang/AST/StmtOpenMP.h"
21#include "clang/AST/StmtVisitor.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000022#include "clang/Basic/OpenMPKinds.h"
23#include "clang/Lex/Preprocessor.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000024#include "clang/Sema/Initialization.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000025#include "clang/Sema/Lookup.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000026#include "clang/Sema/Scope.h"
27#include "clang/Sema/ScopeInfo.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000028#include "clang/Sema/SemaInternal.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000029using namespace clang;
30
Alexey Bataev758e55e2013-09-06 18:03:48 +000031//===----------------------------------------------------------------------===//
32// Stack of data-sharing attributes for variables
33//===----------------------------------------------------------------------===//
34
35namespace {
36/// \brief Default data sharing attributes, which can be applied to directive.
37enum DefaultDataSharingAttributes {
Alexey Bataeved09d242014-05-28 05:53:51 +000038 DSA_unspecified = 0, /// \brief Data sharing attribute not specified.
39 DSA_none = 1 << 0, /// \brief Default data sharing attribute 'none'.
40 DSA_shared = 1 << 1 /// \brief Default data sharing attribute 'shared'.
Alexey Bataev758e55e2013-09-06 18:03:48 +000041};
Alexey Bataev7ff55242014-06-19 09:13:45 +000042
Alexey Bataevf29276e2014-06-18 04:14:57 +000043template <class T> struct MatchesAny {
Alexey Bataev23b69422014-06-18 07:08:49 +000044 explicit MatchesAny(ArrayRef<T> Arr) : Arr(std::move(Arr)) {}
Alexey Bataevf29276e2014-06-18 04:14:57 +000045 bool operator()(T Kind) {
46 for (auto KindEl : Arr)
47 if (KindEl == Kind)
48 return true;
49 return false;
50 }
51
52private:
53 ArrayRef<T> Arr;
54};
Alexey Bataev23b69422014-06-18 07:08:49 +000055struct MatchesAlways {
Alexey Bataevf29276e2014-06-18 04:14:57 +000056 MatchesAlways() {}
Alexey Bataev7ff55242014-06-19 09:13:45 +000057 template <class T> bool operator()(T) { return true; }
Alexey Bataevf29276e2014-06-18 04:14:57 +000058};
59
60typedef MatchesAny<OpenMPClauseKind> MatchesAnyClause;
61typedef MatchesAny<OpenMPDirectiveKind> MatchesAnyDirective;
Alexey Bataev758e55e2013-09-06 18:03:48 +000062
63/// \brief Stack for tracking declarations used in OpenMP directives and
64/// clauses and their data-sharing attributes.
65class DSAStackTy {
66public:
67 struct DSAVarData {
68 OpenMPDirectiveKind DKind;
69 OpenMPClauseKind CKind;
70 DeclRefExpr *RefExpr;
Alexey Bataevbae9a792014-06-27 10:37:06 +000071 SourceLocation ImplicitDSALoc;
72 DSAVarData()
73 : DKind(OMPD_unknown), CKind(OMPC_unknown), RefExpr(nullptr),
74 ImplicitDSALoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000075 };
Alexey Bataeved09d242014-05-28 05:53:51 +000076
Alexey Bataev758e55e2013-09-06 18:03:48 +000077private:
78 struct DSAInfo {
79 OpenMPClauseKind Attributes;
80 DeclRefExpr *RefExpr;
81 };
82 typedef llvm::SmallDenseMap<VarDecl *, DSAInfo, 64> DeclSAMapTy;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000083 typedef llvm::SmallDenseMap<VarDecl *, DeclRefExpr *, 64> AlignedMapTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +000084
85 struct SharingMapTy {
86 DeclSAMapTy SharingMap;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000087 AlignedMapTy AlignedMap;
Alexey Bataev758e55e2013-09-06 18:03:48 +000088 DefaultDataSharingAttributes DefaultAttr;
Alexey Bataevbae9a792014-06-27 10:37:06 +000089 SourceLocation DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +000090 OpenMPDirectiveKind Directive;
91 DeclarationNameInfo DirectiveName;
92 Scope *CurScope;
Alexey Bataevbae9a792014-06-27 10:37:06 +000093 SourceLocation ConstructLoc;
Alexey Bataev9fb6e642014-07-22 06:45:04 +000094 bool OrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +000095 SourceLocation InnerTeamsRegionLoc;
Alexey Bataeved09d242014-05-28 05:53:51 +000096 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataevbae9a792014-06-27 10:37:06 +000097 Scope *CurScope, SourceLocation Loc)
Alexander Musmanf0d76e72014-05-29 14:36:25 +000098 : SharingMap(), AlignedMap(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +000099 Directive(DKind), DirectiveName(std::move(Name)), CurScope(CurScope),
Alexey Bataev13314bf2014-10-09 04:18:56 +0000100 ConstructLoc(Loc), OrderedRegion(false), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000101 SharingMapTy()
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000102 : SharingMap(), AlignedMap(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000103 Directive(OMPD_unknown), DirectiveName(), CurScope(nullptr),
Alexey Bataev13314bf2014-10-09 04:18:56 +0000104 ConstructLoc(), OrderedRegion(false), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000105 };
106
107 typedef SmallVector<SharingMapTy, 64> StackTy;
108
109 /// \brief Stack of used declaration and their data-sharing attributes.
110 StackTy Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000111 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000112
113 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
114
115 DSAVarData getDSA(StackTy::reverse_iterator Iter, VarDecl *D);
Alexey Bataevec3da872014-01-31 05:15:34 +0000116
117 /// \brief Checks if the variable is a local for OpenMP region.
118 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
Alexey Bataeved09d242014-05-28 05:53:51 +0000119
Alexey Bataev758e55e2013-09-06 18:03:48 +0000120public:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000121 explicit DSAStackTy(Sema &S) : Stack(1), SemaRef(S) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000122
123 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000124 Scope *CurScope, SourceLocation Loc) {
125 Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc));
126 Stack.back().DefaultAttrLoc = Loc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000127 }
128
129 void pop() {
130 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
131 Stack.pop_back();
132 }
133
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000134 /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000135 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000136 /// for diagnostics.
137 DeclRefExpr *addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE);
138
Alexey Bataev758e55e2013-09-06 18:03:48 +0000139 /// \brief Adds explicit data sharing attribute to the specified declaration.
140 void addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A);
141
Alexey Bataev758e55e2013-09-06 18:03:48 +0000142 /// \brief Returns data sharing attributes from top of the stack for the
143 /// specified declaration.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000144 DSAVarData getTopDSA(VarDecl *D, bool FromParent);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000145 /// \brief Returns data-sharing attributes for the specified declaration.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000146 DSAVarData getImplicitDSA(VarDecl *D, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000147 /// \brief Checks if the specified variables has data-sharing attributes which
148 /// match specified \a CPred predicate in any directive which matches \a DPred
149 /// predicate.
150 template <class ClausesPredicate, class DirectivesPredicate>
151 DSAVarData hasDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000152 DirectivesPredicate DPred, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000153 /// \brief Checks if the specified variables has data-sharing attributes which
154 /// match specified \a CPred predicate in any innermost directive which
155 /// matches \a DPred predicate.
156 template <class ClausesPredicate, class DirectivesPredicate>
157 DSAVarData hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000158 DirectivesPredicate DPred,
159 bool FromParent);
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000160 /// \brief Finds a directive which matches specified \a DPred predicate.
161 template <class NamedDirectivesPredicate>
162 bool hasDirective(NamedDirectivesPredicate DPred, bool FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000163
Alexey Bataev758e55e2013-09-06 18:03:48 +0000164 /// \brief Returns currently analyzed directive.
165 OpenMPDirectiveKind getCurrentDirective() const {
166 return Stack.back().Directive;
167 }
Alexey Bataev549210e2014-06-24 04:39:47 +0000168 /// \brief Returns parent directive.
169 OpenMPDirectiveKind getParentDirective() const {
170 if (Stack.size() > 2)
171 return Stack[Stack.size() - 2].Directive;
172 return OMPD_unknown;
173 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000174
175 /// \brief Set default data sharing attribute to none.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000176 void setDefaultDSANone(SourceLocation Loc) {
177 Stack.back().DefaultAttr = DSA_none;
178 Stack.back().DefaultAttrLoc = Loc;
179 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000180 /// \brief Set default data sharing attribute to shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000181 void setDefaultDSAShared(SourceLocation Loc) {
182 Stack.back().DefaultAttr = DSA_shared;
183 Stack.back().DefaultAttrLoc = Loc;
184 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000185
186 DefaultDataSharingAttributes getDefaultDSA() const {
187 return Stack.back().DefaultAttr;
188 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000189 SourceLocation getDefaultDSALocation() const {
190 return Stack.back().DefaultAttrLoc;
191 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000192
Alexey Bataevf29276e2014-06-18 04:14:57 +0000193 /// \brief Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000194 bool isThreadPrivate(VarDecl *D) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000195 DSAVarData DVar = getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000196 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000197 }
198
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000199 /// \brief Marks current region as ordered (it has an 'ordered' clause).
200 void setOrderedRegion(bool IsOrdered = true) {
201 Stack.back().OrderedRegion = IsOrdered;
202 }
203 /// \brief Returns true, if parent region is ordered (has associated
204 /// 'ordered' clause), false - otherwise.
205 bool isParentOrderedRegion() const {
206 if (Stack.size() > 2)
207 return Stack[Stack.size() - 2].OrderedRegion;
208 return false;
209 }
210
Alexey Bataev13314bf2014-10-09 04:18:56 +0000211 /// \brief Marks current target region as one with closely nested teams
212 /// region.
213 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
214 if (Stack.size() > 2)
215 Stack[Stack.size() - 2].InnerTeamsRegionLoc = TeamsRegionLoc;
216 }
217 /// \brief Returns true, if current region has closely nested teams region.
218 bool hasInnerTeamsRegion() const {
219 return getInnerTeamsRegionLoc().isValid();
220 }
221 /// \brief Returns location of the nested teams region (if any).
222 SourceLocation getInnerTeamsRegionLoc() const {
223 if (Stack.size() > 1)
224 return Stack.back().InnerTeamsRegionLoc;
225 return SourceLocation();
226 }
227
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000228 Scope *getCurScope() const { return Stack.back().CurScope; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000229 Scope *getCurScope() { return Stack.back().CurScope; }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000230 SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000231};
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000232bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
233 return isOpenMPParallelDirective(DKind) || DKind == OMPD_task ||
Alexey Bataev13314bf2014-10-09 04:18:56 +0000234 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000235}
Alexey Bataeved09d242014-05-28 05:53:51 +0000236} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000237
238DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter,
239 VarDecl *D) {
240 DSAVarData DVar;
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000241 if (Iter == std::prev(Stack.rend())) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000242 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
243 // in a region but not in construct]
244 // File-scope or namespace-scope variables referenced in called routines
245 // in the region are shared unless they appear in a threadprivate
246 // directive.
Alexey Bataev8b9cb982014-07-24 02:33:58 +0000247 if (!D->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D))
Alexey Bataev750a58b2014-03-18 12:19:12 +0000248 DVar.CKind = OMPC_shared;
249
250 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
251 // in a region but not in construct]
252 // Variables with static storage duration that are declared in called
253 // routines in the region are shared.
254 if (D->hasGlobalStorage())
255 DVar.CKind = OMPC_shared;
256
Alexey Bataev758e55e2013-09-06 18:03:48 +0000257 return DVar;
258 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000259
Alexey Bataev758e55e2013-09-06 18:03:48 +0000260 DVar.DKind = Iter->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +0000261 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
262 // in a Construct, C/C++, predetermined, p.1]
263 // Variables with automatic storage duration that are declared in a scope
264 // inside the construct are private.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000265 if (isOpenMPLocal(D, Iter) && D->isLocalVarDecl() &&
266 (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) {
267 DVar.CKind = OMPC_private;
268 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000269 }
270
Alexey Bataev758e55e2013-09-06 18:03:48 +0000271 // Explicitly specified attributes and local variables with predetermined
272 // attributes.
273 if (Iter->SharingMap.count(D)) {
274 DVar.RefExpr = Iter->SharingMap[D].RefExpr;
275 DVar.CKind = Iter->SharingMap[D].Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000276 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000277 return DVar;
278 }
279
280 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
281 // in a Construct, C/C++, implicitly determined, p.1]
282 // In a parallel or task construct, the data-sharing attributes of these
283 // variables are determined by the default clause, if present.
284 switch (Iter->DefaultAttr) {
285 case DSA_shared:
286 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000287 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000288 return DVar;
289 case DSA_none:
290 return DVar;
291 case DSA_unspecified:
292 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
293 // in a Construct, implicitly determined, p.2]
294 // In a parallel construct, if no default clause is present, these
295 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000296 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000297 if (isOpenMPParallelDirective(DVar.DKind) ||
298 isOpenMPTeamsDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000299 DVar.CKind = OMPC_shared;
300 return DVar;
301 }
302
303 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
304 // in a Construct, implicitly determined, p.4]
305 // In a task construct, if no default clause is present, a variable that in
306 // the enclosing context is determined to be shared by all implicit tasks
307 // bound to the current team is shared.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000308 if (DVar.DKind == OMPD_task) {
309 DSAVarData DVarTemp;
Benjamin Kramer167e9992014-03-02 12:20:24 +0000310 for (StackTy::reverse_iterator I = std::next(Iter),
311 EE = std::prev(Stack.rend());
Alexey Bataev758e55e2013-09-06 18:03:48 +0000312 I != EE; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000313 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
314 // Referenced
Alexey Bataev758e55e2013-09-06 18:03:48 +0000315 // in a Construct, implicitly determined, p.6]
316 // In a task construct, if no default clause is present, a variable
317 // whose data-sharing attribute is not determined by the rules above is
318 // firstprivate.
319 DVarTemp = getDSA(I, D);
320 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000321 DVar.RefExpr = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000322 DVar.DKind = OMPD_task;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000323 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000324 return DVar;
325 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000326 if (isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000327 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000328 }
329 DVar.DKind = OMPD_task;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000330 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000331 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000332 return DVar;
333 }
334 }
335 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
336 // in a Construct, implicitly determined, p.3]
337 // For constructs other than task, if no default clause is present, these
338 // variables inherit their data-sharing attributes from the enclosing
339 // context.
Benjamin Kramer167e9992014-03-02 12:20:24 +0000340 return getDSA(std::next(Iter), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000341}
342
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000343DeclRefExpr *DSAStackTy::addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE) {
344 assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
345 auto It = Stack.back().AlignedMap.find(D);
346 if (It == Stack.back().AlignedMap.end()) {
347 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
348 Stack.back().AlignedMap[D] = NewDE;
349 return nullptr;
350 } else {
351 assert(It->second && "Unexpected nullptr expr in the aligned map");
352 return It->second;
353 }
354 return nullptr;
355}
356
Alexey Bataev758e55e2013-09-06 18:03:48 +0000357void DSAStackTy::addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A) {
358 if (A == OMPC_threadprivate) {
359 Stack[0].SharingMap[D].Attributes = A;
360 Stack[0].SharingMap[D].RefExpr = E;
361 } else {
362 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
363 Stack.back().SharingMap[D].Attributes = A;
364 Stack.back().SharingMap[D].RefExpr = E;
365 }
366}
367
Alexey Bataeved09d242014-05-28 05:53:51 +0000368bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000369 if (Stack.size() > 2) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000370 reverse_iterator I = Iter, E = std::prev(Stack.rend());
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000371 Scope *TopScope = nullptr;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000372 while (I != E && !isParallelOrTaskRegion(I->Directive)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000373 ++I;
374 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000375 if (I == E)
376 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000377 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000378 Scope *CurScope = getCurScope();
379 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000380 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000381 }
382 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000383 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000384 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000385}
386
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000387DSAStackTy::DSAVarData DSAStackTy::getTopDSA(VarDecl *D, bool FromParent) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000388 DSAVarData DVar;
389
390 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
391 // in a Construct, C/C++, predetermined, p.1]
392 // Variables appearing in threadprivate directives are threadprivate.
393 if (D->getTLSKind() != VarDecl::TLS_None) {
394 DVar.CKind = OMPC_threadprivate;
395 return DVar;
396 }
397 if (Stack[0].SharingMap.count(D)) {
398 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
399 DVar.CKind = OMPC_threadprivate;
400 return DVar;
401 }
402
403 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
404 // in a Construct, C/C++, predetermined, p.1]
405 // Variables with automatic storage duration that are declared in a scope
406 // inside the construct are private.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000407 OpenMPDirectiveKind Kind =
408 FromParent ? getParentDirective() : getCurrentDirective();
409 auto StartI = std::next(Stack.rbegin());
410 auto EndI = std::prev(Stack.rend());
411 if (FromParent && StartI != EndI) {
412 StartI = std::next(StartI);
413 }
414 if (!isParallelOrTaskRegion(Kind)) {
Alexey Bataev8b9cb982014-07-24 02:33:58 +0000415 if (isOpenMPLocal(D, StartI) &&
416 ((D->isLocalVarDecl() && (D->getStorageClass() == SC_Auto ||
417 D->getStorageClass() == SC_None)) ||
418 isa<ParmVarDecl>(D))) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000419 DVar.CKind = OMPC_private;
420 return DVar;
Alexander Musman8dba6642014-04-22 13:09:42 +0000421 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000422 }
423
424 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
425 // in a Construct, C/C++, predetermined, p.4]
Alexey Bataevf29276e2014-06-18 04:14:57 +0000426 // Static data members are shared.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000427 if (D->isStaticDataMember()) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000428 // Variables with const-qualified type having no mutable member may be
Alexey Bataevf29276e2014-06-18 04:14:57 +0000429 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000430 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
431 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000432 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
433 return DVar;
434
Alexey Bataev758e55e2013-09-06 18:03:48 +0000435 DVar.CKind = OMPC_shared;
436 return DVar;
437 }
438
439 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataev7ff55242014-06-19 09:13:45 +0000440 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
Alexey Bataev758e55e2013-09-06 18:03:48 +0000441 while (Type->isArrayType()) {
442 QualType ElemType = cast<ArrayType>(Type.getTypePtr())->getElementType();
443 Type = ElemType.getNonReferenceType().getCanonicalType();
444 }
445 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
446 // in a Construct, C/C++, predetermined, p.6]
447 // Variables with const qualified type having no mutable member are
448 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000449 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000450 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000451 if (IsConstant &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000452 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000453 // Variables with const-qualified type having no mutable member may be
454 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000455 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
456 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000457 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
458 return DVar;
459
Alexey Bataev758e55e2013-09-06 18:03:48 +0000460 DVar.CKind = OMPC_shared;
461 return DVar;
462 }
463
464 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
465 // in a Construct, C/C++, predetermined, p.7]
466 // Variables with static storage duration that are declared in a scope
467 // inside the construct are shared.
Alexey Bataevec3da872014-01-31 05:15:34 +0000468 if (D->isStaticLocal()) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000469 DVar.CKind = OMPC_shared;
470 return DVar;
471 }
472
473 // Explicitly specified attributes and local variables with predetermined
474 // attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000475 auto I = std::prev(StartI);
476 if (I->SharingMap.count(D)) {
477 DVar.RefExpr = I->SharingMap[D].RefExpr;
478 DVar.CKind = I->SharingMap[D].Attributes;
479 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000480 }
481
482 return DVar;
483}
484
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000485DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D, bool FromParent) {
486 auto StartI = Stack.rbegin();
487 auto EndI = std::prev(Stack.rend());
488 if (FromParent && StartI != EndI) {
489 StartI = std::next(StartI);
490 }
491 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000492}
493
Alexey Bataevf29276e2014-06-18 04:14:57 +0000494template <class ClausesPredicate, class DirectivesPredicate>
495DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000496 DirectivesPredicate DPred,
497 bool FromParent) {
498 auto StartI = std::next(Stack.rbegin());
499 auto EndI = std::prev(Stack.rend());
500 if (FromParent && StartI != EndI) {
501 StartI = std::next(StartI);
502 }
503 for (auto I = StartI, EE = EndI; I != EE; ++I) {
504 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000505 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000506 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000507 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000508 return DVar;
509 }
510 return DSAVarData();
511}
512
Alexey Bataevf29276e2014-06-18 04:14:57 +0000513template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000514DSAStackTy::DSAVarData
515DSAStackTy::hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
516 DirectivesPredicate DPred, bool FromParent) {
517 auto StartI = std::next(Stack.rbegin());
518 auto EndI = std::prev(Stack.rend());
519 if (FromParent && StartI != EndI) {
520 StartI = std::next(StartI);
521 }
522 for (auto I = StartI, EE = EndI; I != EE; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000523 if (!DPred(I->Directive))
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000524 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000525 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000526 if (CPred(DVar.CKind))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000527 return DVar;
528 return DSAVarData();
529 }
530 return DSAVarData();
531}
532
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000533template <class NamedDirectivesPredicate>
534bool DSAStackTy::hasDirective(NamedDirectivesPredicate DPred, bool FromParent) {
535 auto StartI = std::next(Stack.rbegin());
536 auto EndI = std::prev(Stack.rend());
537 if (FromParent && StartI != EndI) {
538 StartI = std::next(StartI);
539 }
540 for (auto I = StartI, EE = EndI; I != EE; ++I) {
541 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
542 return true;
543 }
544 return false;
545}
546
Alexey Bataev758e55e2013-09-06 18:03:48 +0000547void Sema::InitDataSharingAttributesStack() {
548 VarDataSharingAttributesStack = new DSAStackTy(*this);
549}
550
551#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
552
Alexey Bataeved09d242014-05-28 05:53:51 +0000553void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000554
555void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
556 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000557 Scope *CurScope, SourceLocation Loc) {
558 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000559 PushExpressionEvaluationContext(PotentiallyEvaluated);
560}
561
562void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000563 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
564 // A variable of class type (or array thereof) that appears in a lastprivate
565 // clause requires an accessible, unambiguous default constructor for the
566 // class type, unless the list item is also specified in a firstprivate
567 // clause.
568 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
569 for (auto C : D->clauses()) {
570 if (auto Clause = dyn_cast<OMPLastprivateClause>(C)) {
571 for (auto VarRef : Clause->varlists()) {
572 if (VarRef->isValueDependent() || VarRef->isTypeDependent())
573 continue;
574 auto VD = cast<VarDecl>(cast<DeclRefExpr>(VarRef)->getDecl());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000575 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000576 if (DVar.CKind == OMPC_lastprivate) {
577 SourceLocation ELoc = VarRef->getExprLoc();
578 auto Type = VarRef->getType();
579 if (Type->isArrayType())
580 Type = QualType(Type->getArrayElementTypeNoTypeQual(), 0);
581 CXXRecordDecl *RD =
Alexey Bataev23b69422014-06-18 07:08:49 +0000582 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
583 // FIXME This code must be replaced by actual constructing of the
584 // lastprivate variable.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000585 if (RD) {
586 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
587 PartialDiagnostic PD =
588 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
589 if (!CD ||
590 CheckConstructorAccess(
591 ELoc, CD, InitializedEntity::InitializeTemporary(Type),
592 CD->getAccess(), PD) == AR_inaccessible ||
593 CD->isDeleted()) {
594 Diag(ELoc, diag::err_omp_required_method)
595 << getOpenMPClauseName(OMPC_lastprivate) << 0;
596 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
597 VarDecl::DeclarationOnly;
598 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl
599 : diag::note_defined_here)
600 << VD;
601 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
602 continue;
603 }
604 MarkFunctionReferenced(ELoc, CD);
605 DiagnoseUseOfDecl(CD, ELoc);
606 }
607 }
608 }
609 }
610 }
611 }
612
Alexey Bataev758e55e2013-09-06 18:03:48 +0000613 DSAStack->pop();
614 DiscardCleanupsInEvaluationContext();
615 PopExpressionEvaluationContext();
616}
617
Alexey Bataeva769e072013-03-22 06:34:35 +0000618namespace {
619
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000620class VarDeclFilterCCC : public CorrectionCandidateCallback {
621private:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000622 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +0000623
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000624public:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000625 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +0000626 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000627 NamedDecl *ND = Candidate.getCorrectionDecl();
628 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
629 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000630 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
631 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +0000632 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000633 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000634 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000635};
Alexey Bataeved09d242014-05-28 05:53:51 +0000636} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000637
638ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
639 CXXScopeSpec &ScopeSpec,
640 const DeclarationNameInfo &Id) {
641 LookupResult Lookup(*this, Id, LookupOrdinaryName);
642 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
643
644 if (Lookup.isAmbiguous())
645 return ExprError();
646
647 VarDecl *VD;
648 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +0000649 if (TypoCorrection Corrected = CorrectTypo(
650 Id, LookupOrdinaryName, CurScope, nullptr,
651 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +0000652 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000653 PDiag(Lookup.empty()
654 ? diag::err_undeclared_var_use_suggest
655 : diag::err_omp_expected_var_arg_suggest)
656 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +0000657 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000658 } else {
Richard Smithf9b15102013-08-17 00:46:16 +0000659 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
660 : diag::err_omp_expected_var_arg)
661 << Id.getName();
662 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000663 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000664 } else {
665 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000666 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000667 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
668 return ExprError();
669 }
670 }
671 Lookup.suppressDiagnostics();
672
673 // OpenMP [2.9.2, Syntax, C/C++]
674 // Variables must be file-scope, namespace-scope, or static block-scope.
675 if (!VD->hasGlobalStorage()) {
676 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000677 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
678 bool IsDecl =
679 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000680 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +0000681 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
682 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000683 return ExprError();
684 }
685
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000686 VarDecl *CanonicalVD = VD->getCanonicalDecl();
687 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000688 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
689 // A threadprivate directive for file-scope variables must appear outside
690 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000691 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
692 !getCurLexicalContext()->isTranslationUnit()) {
693 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000694 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
695 bool IsDecl =
696 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
697 Diag(VD->getLocation(),
698 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
699 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000700 return ExprError();
701 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000702 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
703 // A threadprivate directive for static class member variables must appear
704 // in the class definition, in the same scope in which the member
705 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000706 if (CanonicalVD->isStaticDataMember() &&
707 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
708 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000709 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
710 bool IsDecl =
711 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
712 Diag(VD->getLocation(),
713 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
714 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000715 return ExprError();
716 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000717 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
718 // A threadprivate directive for namespace-scope variables must appear
719 // outside any definition or declaration other than the namespace
720 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000721 if (CanonicalVD->getDeclContext()->isNamespace() &&
722 (!getCurLexicalContext()->isFileContext() ||
723 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
724 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000725 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
726 bool IsDecl =
727 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
728 Diag(VD->getLocation(),
729 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
730 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000731 return ExprError();
732 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000733 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
734 // A threadprivate directive for static block-scope variables must appear
735 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000736 if (CanonicalVD->isStaticLocal() && CurScope &&
737 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000738 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 Bataev6f6f3b42013-05-13 04:18:18 +0000745 return ExprError();
746 }
747
748 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
749 // A threadprivate directive must lexically precede all references to any
750 // of the variables in its list.
751 if (VD->isUsed()) {
752 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +0000753 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000754 return ExprError();
755 }
756
757 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataevd178ad42014-03-07 08:03:37 +0000758 ExprResult DE = BuildDeclRefExpr(VD, ExprType, VK_LValue, Id.getLoc());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000759 return DE;
760}
761
Alexey Bataeved09d242014-05-28 05:53:51 +0000762Sema::DeclGroupPtrTy
763Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
764 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000765 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000766 CurContext->addDecl(D);
767 return DeclGroupPtrTy::make(DeclGroupRef(D));
768 }
769 return DeclGroupPtrTy();
770}
771
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000772namespace {
773class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
774 Sema &SemaRef;
775
776public:
777 bool VisitDeclRefExpr(const DeclRefExpr *E) {
778 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
779 if (VD->hasLocalStorage()) {
780 SemaRef.Diag(E->getLocStart(),
781 diag::err_omp_local_var_in_threadprivate_init)
782 << E->getSourceRange();
783 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
784 << VD << VD->getSourceRange();
785 return true;
786 }
787 }
788 return false;
789 }
790 bool VisitStmt(const Stmt *S) {
791 for (auto Child : S->children()) {
792 if (Child && Visit(Child))
793 return true;
794 }
795 return false;
796 }
Alexey Bataev23b69422014-06-18 07:08:49 +0000797 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000798};
799} // namespace
800
Alexey Bataeved09d242014-05-28 05:53:51 +0000801OMPThreadPrivateDecl *
802Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000803 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +0000804 for (auto &RefExpr : VarList) {
805 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000806 VarDecl *VD = cast<VarDecl>(DE->getDecl());
807 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +0000808
809 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
810 // A threadprivate variable must not have an incomplete type.
811 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000812 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000813 continue;
814 }
815
816 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
817 // A threadprivate variable must not have a reference type.
818 if (VD->getType()->isReferenceType()) {
819 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000820 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
821 bool IsDecl =
822 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
823 Diag(VD->getLocation(),
824 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
825 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000826 continue;
827 }
828
Richard Smithfd3834f2013-04-13 02:43:54 +0000829 // Check if this is a TLS variable.
830 if (VD->getTLSKind()) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000831 Diag(ILoc, diag::err_omp_var_thread_local) << VD;
Alexey Bataeved09d242014-05-28 05:53:51 +0000832 bool IsDecl =
833 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
834 Diag(VD->getLocation(),
835 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
836 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000837 continue;
838 }
839
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000840 // Check if initial value of threadprivate variable reference variable with
841 // local storage (it is not supported by runtime).
842 if (auto Init = VD->getAnyInitializer()) {
843 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000844 if (Checker.Visit(Init))
845 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000846 }
847
Alexey Bataeved09d242014-05-28 05:53:51 +0000848 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +0000849 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataeva769e072013-03-22 06:34:35 +0000850 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000851 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000852 if (!Vars.empty()) {
853 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
854 Vars);
855 D->setAccess(AS_public);
856 }
857 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +0000858}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000859
Alexey Bataev7ff55242014-06-19 09:13:45 +0000860static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
861 const VarDecl *VD, DSAStackTy::DSAVarData DVar,
862 bool IsLoopIterVar = false) {
863 if (DVar.RefExpr) {
864 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
865 << getOpenMPClauseName(DVar.CKind);
866 return;
867 }
868 enum {
869 PDSA_StaticMemberShared,
870 PDSA_StaticLocalVarShared,
871 PDSA_LoopIterVarPrivate,
872 PDSA_LoopIterVarLinear,
873 PDSA_LoopIterVarLastprivate,
874 PDSA_ConstVarShared,
875 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000876 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000877 PDSA_LocalVarPrivate,
878 PDSA_Implicit
879 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000880 bool ReportHint = false;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000881 auto ReportLoc = VD->getLocation();
Alexey Bataev7ff55242014-06-19 09:13:45 +0000882 if (IsLoopIterVar) {
883 if (DVar.CKind == OMPC_private)
884 Reason = PDSA_LoopIterVarPrivate;
885 else if (DVar.CKind == OMPC_lastprivate)
886 Reason = PDSA_LoopIterVarLastprivate;
887 else
888 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000889 } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) {
890 Reason = PDSA_TaskVarFirstprivate;
891 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000892 } else if (VD->isStaticLocal())
893 Reason = PDSA_StaticLocalVarShared;
894 else if (VD->isStaticDataMember())
895 Reason = PDSA_StaticMemberShared;
896 else if (VD->isFileVarDecl())
897 Reason = PDSA_GlobalVarShared;
898 else if (VD->getType().isConstant(SemaRef.getASTContext()))
899 Reason = PDSA_ConstVarShared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000900 else if (VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +0000901 ReportHint = true;
902 Reason = PDSA_LocalVarPrivate;
903 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000904 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000905 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +0000906 << Reason << ReportHint
907 << getOpenMPDirectiveName(Stack->getCurrentDirective());
908 } else if (DVar.ImplicitDSALoc.isValid()) {
909 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
910 << getOpenMPClauseName(DVar.CKind);
911 }
Alexey Bataev7ff55242014-06-19 09:13:45 +0000912}
913
Alexey Bataev758e55e2013-09-06 18:03:48 +0000914namespace {
915class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
916 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000917 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000918 bool ErrorFound;
919 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000920 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000921 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +0000922
Alexey Bataev758e55e2013-09-06 18:03:48 +0000923public:
924 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000925 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000926 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +0000927 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
928 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000929
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000930 auto DVar = Stack->getTopDSA(VD, false);
931 // Check if the variable has explicit DSA set and stop analysis if it so.
932 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000933
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000934 auto ELoc = E->getExprLoc();
935 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000936 // The default(none) clause requires that each variable that is referenced
937 // in the construct, and does not have a predetermined data-sharing
938 // attribute, must have its data-sharing attribute explicitly determined
939 // by being listed in a data-sharing attribute clause.
940 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000941 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +0000942 VarsWithInheritedDSA.count(VD) == 0) {
943 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000944 return;
945 }
946
947 // OpenMP [2.9.3.6, Restrictions, p.2]
948 // A list item that appears in a reduction clause of the innermost
949 // enclosing worksharing or parallel construct may not be accessed in an
950 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000951 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000952 [](OpenMPDirectiveKind K) -> bool {
953 return isOpenMPParallelDirective(K) ||
Alexey Bataev13314bf2014-10-09 04:18:56 +0000954 isOpenMPWorksharingDirective(K) ||
955 isOpenMPTeamsDirective(K);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000956 },
957 false);
Alexey Bataevc5e02582014-06-16 07:08:35 +0000958 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
959 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000960 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
961 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +0000962 return;
963 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000964
965 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000966 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000967 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000968 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000969 }
970 }
971 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000972 for (auto *C : S->clauses()) {
973 // Skip analysis of arguments of implicitly defined firstprivate clause
974 // for task directives.
975 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
976 for (auto *CC : C->children()) {
977 if (CC)
978 Visit(CC);
979 }
980 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000981 }
982 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000983 for (auto *C : S->children()) {
984 if (C && !isa<OMPExecutableDirective>(C))
985 Visit(C);
986 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000987 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000988
989 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000990 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev4acb8592014-07-07 13:01:15 +0000991 llvm::DenseMap<VarDecl *, Expr *> &getVarsWithInheritedDSA() {
992 return VarsWithInheritedDSA;
993 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000994
Alexey Bataev7ff55242014-06-19 09:13:45 +0000995 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
996 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000997};
Alexey Bataeved09d242014-05-28 05:53:51 +0000998} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000999
Alexey Bataevbae9a792014-06-27 10:37:06 +00001000void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001001 switch (DKind) {
1002 case OMPD_parallel: {
1003 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1004 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001005 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001006 std::make_pair(".global_tid.", KmpInt32PtrTy),
1007 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1008 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001009 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001010 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1011 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001012 break;
1013 }
1014 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001015 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001016 std::make_pair(StringRef(), QualType()) // __context with shared vars
1017 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001018 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1019 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001020 break;
1021 }
1022 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001023 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001024 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001025 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001026 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1027 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001028 break;
1029 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001030 case OMPD_for_simd: {
1031 Sema::CapturedParamNameType Params[] = {
1032 std::make_pair(StringRef(), QualType()) // __context with shared vars
1033 };
1034 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1035 Params);
1036 break;
1037 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001038 case OMPD_sections: {
1039 Sema::CapturedParamNameType Params[] = {
1040 std::make_pair(StringRef(), QualType()) // __context with shared vars
1041 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001042 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1043 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001044 break;
1045 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001046 case OMPD_section: {
1047 Sema::CapturedParamNameType Params[] = {
1048 std::make_pair(StringRef(), QualType()) // __context with shared vars
1049 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001050 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1051 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001052 break;
1053 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001054 case OMPD_single: {
1055 Sema::CapturedParamNameType Params[] = {
1056 std::make_pair(StringRef(), QualType()) // __context with shared vars
1057 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001058 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1059 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001060 break;
1061 }
Alexander Musman80c22892014-07-17 08:54:58 +00001062 case OMPD_master: {
1063 Sema::CapturedParamNameType Params[] = {
1064 std::make_pair(StringRef(), QualType()) // __context with shared vars
1065 };
1066 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1067 Params);
1068 break;
1069 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001070 case OMPD_critical: {
1071 Sema::CapturedParamNameType Params[] = {
1072 std::make_pair(StringRef(), QualType()) // __context with shared vars
1073 };
1074 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1075 Params);
1076 break;
1077 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001078 case OMPD_parallel_for: {
1079 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1080 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1081 Sema::CapturedParamNameType Params[] = {
1082 std::make_pair(".global_tid.", KmpInt32PtrTy),
1083 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1084 std::make_pair(StringRef(), QualType()) // __context with shared vars
1085 };
1086 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1087 Params);
1088 break;
1089 }
Alexander Musmane4e893b2014-09-23 09:33:00 +00001090 case OMPD_parallel_for_simd: {
1091 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1092 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1093 Sema::CapturedParamNameType Params[] = {
1094 std::make_pair(".global_tid.", KmpInt32PtrTy),
1095 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1096 std::make_pair(StringRef(), QualType()) // __context with shared vars
1097 };
1098 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1099 Params);
1100 break;
1101 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001102 case OMPD_parallel_sections: {
1103 Sema::CapturedParamNameType Params[] = {
1104 std::make_pair(StringRef(), QualType()) // __context with shared vars
1105 };
1106 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1107 Params);
1108 break;
1109 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001110 case OMPD_task: {
1111 Sema::CapturedParamNameType Params[] = {
1112 std::make_pair(StringRef(), QualType()) // __context with shared vars
1113 };
1114 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1115 Params);
1116 break;
1117 }
Alexey Bataev68446b72014-07-18 07:47:19 +00001118 case OMPD_taskyield: {
1119 Sema::CapturedParamNameType Params[] = {
1120 std::make_pair(StringRef(), QualType()) // __context with shared vars
1121 };
1122 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1123 Params);
1124 break;
1125 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001126 case OMPD_barrier: {
1127 Sema::CapturedParamNameType Params[] = {
1128 std::make_pair(StringRef(), QualType()) // __context with shared vars
1129 };
1130 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1131 Params);
1132 break;
1133 }
Alexey Bataev2df347a2014-07-18 10:17:07 +00001134 case OMPD_taskwait: {
1135 Sema::CapturedParamNameType Params[] = {
1136 std::make_pair(StringRef(), QualType()) // __context with shared vars
1137 };
1138 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1139 Params);
1140 break;
1141 }
Alexey Bataev6125da92014-07-21 11:26:11 +00001142 case OMPD_flush: {
1143 Sema::CapturedParamNameType Params[] = {
1144 std::make_pair(StringRef(), QualType()) // __context with shared vars
1145 };
1146 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1147 Params);
1148 break;
1149 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001150 case OMPD_ordered: {
1151 Sema::CapturedParamNameType Params[] = {
1152 std::make_pair(StringRef(), QualType()) // __context with shared vars
1153 };
1154 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1155 Params);
1156 break;
1157 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001158 case OMPD_atomic: {
1159 Sema::CapturedParamNameType Params[] = {
1160 std::make_pair(StringRef(), QualType()) // __context with shared vars
1161 };
1162 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1163 Params);
1164 break;
1165 }
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001166 case OMPD_target: {
1167 Sema::CapturedParamNameType Params[] = {
1168 std::make_pair(StringRef(), QualType()) // __context with shared vars
1169 };
1170 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1171 Params);
1172 break;
1173 }
Alexey Bataev13314bf2014-10-09 04:18:56 +00001174 case OMPD_teams: {
1175 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1176 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1177 Sema::CapturedParamNameType Params[] = {
1178 std::make_pair(".global_tid.", KmpInt32PtrTy),
1179 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1180 std::make_pair(StringRef(), QualType()) // __context with shared vars
1181 };
1182 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1183 Params);
1184 break;
1185 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001186 case OMPD_threadprivate:
Alexey Bataev9959db52014-05-06 10:08:46 +00001187 llvm_unreachable("OpenMP Directive is not allowed");
1188 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001189 llvm_unreachable("Unknown OpenMP directive");
1190 }
1191}
1192
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001193static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1194 OpenMPDirectiveKind CurrentRegion,
1195 const DeclarationNameInfo &CurrentName,
1196 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001197 // Allowed nesting of constructs
1198 // +------------------+-----------------+------------------------------------+
1199 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1200 // +------------------+-----------------+------------------------------------+
1201 // | parallel | parallel | * |
1202 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001203 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001204 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001205 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001206 // | parallel | simd | * |
1207 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001208 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001209 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001210 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001211 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001212 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001213 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001214 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001215 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001216 // | parallel | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001217 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001218 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001219 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001220 // | parallel | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001221 // | parallel | teams | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001222 // +------------------+-----------------+------------------------------------+
1223 // | for | parallel | * |
1224 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001225 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001226 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001227 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001228 // | for | simd | * |
1229 // | for | sections | + |
1230 // | for | section | + |
1231 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001232 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001233 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001234 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001235 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001236 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001237 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001238 // | for | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001239 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001240 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001241 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001242 // | for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001243 // | for | teams | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001244 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001245 // | master | parallel | * |
1246 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001247 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001248 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001249 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001250 // | master | simd | * |
1251 // | master | sections | + |
1252 // | master | section | + |
1253 // | master | single | + |
1254 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001255 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001256 // | master |parallel sections| * |
1257 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001258 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001259 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001260 // | master | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001261 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001262 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001263 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001264 // | master | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001265 // | master | teams | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001266 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001267 // | critical | parallel | * |
1268 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001269 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001270 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001271 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001272 // | critical | simd | * |
1273 // | critical | sections | + |
1274 // | critical | section | + |
1275 // | critical | single | + |
1276 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001277 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001278 // | critical |parallel sections| * |
1279 // | critical | task | * |
1280 // | critical | taskyield | * |
1281 // | critical | barrier | + |
1282 // | critical | taskwait | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001283 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001284 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001285 // | critical | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001286 // | critical | teams | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001287 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001288 // | simd | parallel | |
1289 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001290 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00001291 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001292 // | simd | critical | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001293 // | simd | simd | |
1294 // | simd | sections | |
1295 // | simd | section | |
1296 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001297 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001298 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001299 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001300 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00001301 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001302 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001303 // | simd | taskwait | |
Alexey Bataev6125da92014-07-21 11:26:11 +00001304 // | simd | flush | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001305 // | simd | ordered | |
Alexey Bataev0162e452014-07-22 10:10:35 +00001306 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001307 // | simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001308 // | simd | teams | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001309 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00001310 // | for simd | parallel | |
1311 // | for simd | for | |
1312 // | for simd | for simd | |
1313 // | for simd | master | |
1314 // | for simd | critical | |
1315 // | for simd | simd | |
1316 // | for simd | sections | |
1317 // | for simd | section | |
1318 // | for simd | single | |
1319 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001320 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001321 // | for simd |parallel sections| |
1322 // | for simd | task | |
1323 // | for simd | taskyield | |
1324 // | for simd | barrier | |
1325 // | for simd | taskwait | |
1326 // | for simd | flush | |
1327 // | for simd | ordered | |
1328 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001329 // | for simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001330 // | for simd | teams | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001331 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00001332 // | parallel for simd| parallel | |
1333 // | parallel for simd| for | |
1334 // | parallel for simd| for simd | |
1335 // | parallel for simd| master | |
1336 // | parallel for simd| critical | |
1337 // | parallel for simd| simd | |
1338 // | parallel for simd| sections | |
1339 // | parallel for simd| section | |
1340 // | parallel for simd| single | |
1341 // | parallel for simd| parallel for | |
1342 // | parallel for simd|parallel for simd| |
1343 // | parallel for simd|parallel sections| |
1344 // | parallel for simd| task | |
1345 // | parallel for simd| taskyield | |
1346 // | parallel for simd| barrier | |
1347 // | parallel for simd| taskwait | |
1348 // | parallel for simd| flush | |
1349 // | parallel for simd| ordered | |
1350 // | parallel for simd| atomic | |
1351 // | parallel for simd| target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001352 // | parallel for simd| teams | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001353 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001354 // | sections | parallel | * |
1355 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001356 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001357 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001358 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001359 // | sections | simd | * |
1360 // | sections | sections | + |
1361 // | sections | section | * |
1362 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001363 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001364 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001365 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001366 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001367 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001368 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001369 // | sections | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001370 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001371 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001372 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001373 // | sections | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001374 // | sections | teams | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001375 // +------------------+-----------------+------------------------------------+
1376 // | section | parallel | * |
1377 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001378 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001379 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001380 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001381 // | section | simd | * |
1382 // | section | sections | + |
1383 // | section | section | + |
1384 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001385 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001386 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001387 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001388 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001389 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001390 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001391 // | section | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001392 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001393 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001394 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001395 // | section | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001396 // | section | teams | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001397 // +------------------+-----------------+------------------------------------+
1398 // | single | parallel | * |
1399 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001400 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001401 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001402 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001403 // | single | simd | * |
1404 // | single | sections | + |
1405 // | single | section | + |
1406 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001407 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001408 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001409 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001410 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001411 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001412 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001413 // | single | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001414 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001415 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001416 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001417 // | single | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001418 // | single | teams | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001419 // +------------------+-----------------+------------------------------------+
1420 // | parallel for | parallel | * |
1421 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001422 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001423 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001424 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001425 // | parallel for | simd | * |
1426 // | parallel for | sections | + |
1427 // | parallel for | section | + |
1428 // | parallel for | single | + |
1429 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001430 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001431 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001432 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001433 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001434 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001435 // | parallel for | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001436 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001437 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001438 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001439 // | parallel for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001440 // | parallel for | teams | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001441 // +------------------+-----------------+------------------------------------+
1442 // | parallel sections| parallel | * |
1443 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001444 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001445 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001446 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001447 // | parallel sections| simd | * |
1448 // | parallel sections| sections | + |
1449 // | parallel sections| section | * |
1450 // | parallel sections| single | + |
1451 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001452 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001453 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001454 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001455 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001456 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001457 // | parallel sections| taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001458 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001459 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001460 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001461 // | parallel sections| target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001462 // | parallel sections| teams | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001463 // +------------------+-----------------+------------------------------------+
1464 // | task | parallel | * |
1465 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001466 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001467 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001468 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001469 // | task | simd | * |
1470 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001471 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001472 // | task | single | + |
1473 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001474 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001475 // | task |parallel sections| * |
1476 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001477 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001478 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001479 // | task | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001480 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001481 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001482 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001483 // | task | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001484 // | task | teams | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001485 // +------------------+-----------------+------------------------------------+
1486 // | ordered | parallel | * |
1487 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001488 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001489 // | ordered | master | * |
1490 // | ordered | critical | * |
1491 // | ordered | simd | * |
1492 // | ordered | sections | + |
1493 // | ordered | section | + |
1494 // | ordered | single | + |
1495 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001496 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001497 // | ordered |parallel sections| * |
1498 // | ordered | task | * |
1499 // | ordered | taskyield | * |
1500 // | ordered | barrier | + |
1501 // | ordered | taskwait | * |
1502 // | ordered | flush | * |
1503 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001504 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001505 // | ordered | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001506 // | ordered | teams | + |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001507 // +------------------+-----------------+------------------------------------+
1508 // | atomic | parallel | |
1509 // | atomic | for | |
1510 // | atomic | for simd | |
1511 // | atomic | master | |
1512 // | atomic | critical | |
1513 // | atomic | simd | |
1514 // | atomic | sections | |
1515 // | atomic | section | |
1516 // | atomic | single | |
1517 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001518 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001519 // | atomic |parallel sections| |
1520 // | atomic | task | |
1521 // | atomic | taskyield | |
1522 // | atomic | barrier | |
1523 // | atomic | taskwait | |
1524 // | atomic | flush | |
1525 // | atomic | ordered | |
1526 // | atomic | atomic | |
1527 // | atomic | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001528 // | atomic | teams | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001529 // +------------------+-----------------+------------------------------------+
1530 // | target | parallel | * |
1531 // | target | for | * |
1532 // | target | for simd | * |
1533 // | target | master | * |
1534 // | target | critical | * |
1535 // | target | simd | * |
1536 // | target | sections | * |
1537 // | target | section | * |
1538 // | target | single | * |
1539 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001540 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001541 // | target |parallel sections| * |
1542 // | target | task | * |
1543 // | target | taskyield | * |
1544 // | target | barrier | * |
1545 // | target | taskwait | * |
1546 // | target | flush | * |
1547 // | target | ordered | * |
1548 // | target | atomic | * |
1549 // | target | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001550 // | target | teams | * |
1551 // +------------------+-----------------+------------------------------------+
1552 // | teams | parallel | * |
1553 // | teams | for | + |
1554 // | teams | for simd | + |
1555 // | teams | master | + |
1556 // | teams | critical | + |
1557 // | teams | simd | + |
1558 // | teams | sections | + |
1559 // | teams | section | + |
1560 // | teams | single | + |
1561 // | teams | parallel for | * |
1562 // | teams |parallel for simd| * |
1563 // | teams |parallel sections| * |
1564 // | teams | task | + |
1565 // | teams | taskyield | + |
1566 // | teams | barrier | + |
1567 // | teams | taskwait | + |
1568 // | teams | flush | + |
1569 // | teams | ordered | + |
1570 // | teams | atomic | + |
1571 // | teams | target | + |
1572 // | teams | teams | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001573 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00001574 if (Stack->getCurScope()) {
1575 auto ParentRegion = Stack->getParentDirective();
1576 bool NestingProhibited = false;
1577 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001578 enum {
1579 NoRecommend,
1580 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00001581 ShouldBeInOrderedRegion,
1582 ShouldBeInTargetRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001583 } Recommend = NoRecommend;
Alexey Bataev549210e2014-06-24 04:39:47 +00001584 if (isOpenMPSimdDirective(ParentRegion)) {
1585 // OpenMP [2.16, Nesting of Regions]
1586 // OpenMP constructs may not be nested inside a simd region.
1587 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
1588 return true;
1589 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001590 if (ParentRegion == OMPD_atomic) {
1591 // OpenMP [2.16, Nesting of Regions]
1592 // OpenMP constructs may not be nested inside an atomic region.
1593 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
1594 return true;
1595 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001596 if (CurrentRegion == OMPD_section) {
1597 // OpenMP [2.7.2, sections Construct, Restrictions]
1598 // Orphaned section directives are prohibited. That is, the section
1599 // directives must appear within the sections construct and must not be
1600 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001601 if (ParentRegion != OMPD_sections &&
1602 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001603 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
1604 << (ParentRegion != OMPD_unknown)
1605 << getOpenMPDirectiveName(ParentRegion);
1606 return true;
1607 }
1608 return false;
1609 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001610 // Allow some constructs to be orphaned (they could be used in functions,
1611 // called from OpenMP regions with the required preconditions).
1612 if (ParentRegion == OMPD_unknown)
1613 return false;
Alexander Musman80c22892014-07-17 08:54:58 +00001614 if (CurrentRegion == OMPD_master) {
1615 // OpenMP [2.16, Nesting of Regions]
1616 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00001617 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00001618 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
1619 ParentRegion == OMPD_task;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001620 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
1621 // OpenMP [2.16, Nesting of Regions]
1622 // A critical region may not be nested (closely or otherwise) inside a
1623 // critical region with the same name. Note that this restriction is not
1624 // sufficient to prevent deadlock.
1625 SourceLocation PreviousCriticalLoc;
1626 bool DeadLock =
1627 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
1628 OpenMPDirectiveKind K,
1629 const DeclarationNameInfo &DNI,
1630 SourceLocation Loc)
1631 ->bool {
1632 if (K == OMPD_critical &&
1633 DNI.getName() == CurrentName.getName()) {
1634 PreviousCriticalLoc = Loc;
1635 return true;
1636 } else
1637 return false;
1638 },
1639 false /* skip top directive */);
1640 if (DeadLock) {
1641 SemaRef.Diag(StartLoc,
1642 diag::err_omp_prohibited_region_critical_same_name)
1643 << CurrentName.getName();
1644 if (PreviousCriticalLoc.isValid())
1645 SemaRef.Diag(PreviousCriticalLoc,
1646 diag::note_omp_previous_critical_region);
1647 return true;
1648 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001649 } else if (CurrentRegion == OMPD_barrier) {
1650 // OpenMP [2.16, Nesting of Regions]
1651 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00001652 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001653 NestingProhibited =
1654 isOpenMPWorksharingDirective(ParentRegion) ||
1655 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1656 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00001657 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00001658 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00001659 // OpenMP [2.16, Nesting of Regions]
1660 // A worksharing region may not be closely nested inside a worksharing,
1661 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001662 NestingProhibited =
Alexander Musmanf82886e2014-09-18 05:12:34 +00001663 isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001664 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1665 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
1666 Recommend = ShouldBeInParallelRegion;
1667 } else if (CurrentRegion == OMPD_ordered) {
1668 // OpenMP [2.16, Nesting of Regions]
1669 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00001670 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001671 // An ordered region must be closely nested inside a loop region (or
1672 // parallel loop region) with an ordered clause.
1673 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00001674 ParentRegion == OMPD_task ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001675 !Stack->isParentOrderedRegion();
1676 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00001677 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
1678 // OpenMP [2.16, Nesting of Regions]
1679 // If specified, a teams construct must be contained within a target
1680 // construct.
1681 NestingProhibited = ParentRegion != OMPD_target;
1682 Recommend = ShouldBeInTargetRegion;
1683 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
1684 }
1685 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
1686 // OpenMP [2.16, Nesting of Regions]
1687 // distribute, parallel, parallel sections, parallel workshare, and the
1688 // parallel loop and parallel loop SIMD constructs are the only OpenMP
1689 // constructs that can be closely nested in the teams region.
1690 // TODO: add distribute directive.
1691 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion);
1692 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00001693 }
1694 if (NestingProhibited) {
1695 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001696 << CloseNesting << getOpenMPDirectiveName(ParentRegion) << Recommend
1697 << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00001698 return true;
1699 }
1700 }
1701 return false;
1702}
1703
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001704StmtResult Sema::ActOnOpenMPExecutableDirective(OpenMPDirectiveKind Kind,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001705 const DeclarationNameInfo &DirName,
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001706 ArrayRef<OMPClause *> Clauses,
1707 Stmt *AStmt,
1708 SourceLocation StartLoc,
1709 SourceLocation EndLoc) {
1710 StmtResult Res = StmtError();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001711 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00001712 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001713
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001714 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev68446b72014-07-18 07:47:19 +00001715 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001716 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00001717 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00001718 if (AStmt) {
1719 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
1720
1721 // Check default data sharing attributes for referenced variables.
1722 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
1723 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
1724 if (DSAChecker.isErrorFound())
1725 return StmtError();
1726 // Generate list of implicitly defined firstprivate variables.
1727 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00001728
1729 if (!DSAChecker.getImplicitFirstprivate().empty()) {
1730 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
1731 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
1732 SourceLocation(), SourceLocation())) {
1733 ClausesWithImplicit.push_back(Implicit);
1734 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
1735 DSAChecker.getImplicitFirstprivate().size();
1736 } else
1737 ErrorFound = true;
1738 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001739 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001740
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001741 switch (Kind) {
1742 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00001743 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
1744 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001745 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001746 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001747 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
1748 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001749 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00001750 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001751 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
1752 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001753 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00001754 case OMPD_for_simd:
1755 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
1756 EndLoc, VarsWithInheritedDSA);
1757 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001758 case OMPD_sections:
1759 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
1760 EndLoc);
1761 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001762 case OMPD_section:
1763 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00001764 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001765 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
1766 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001767 case OMPD_single:
1768 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
1769 EndLoc);
1770 break;
Alexander Musman80c22892014-07-17 08:54:58 +00001771 case OMPD_master:
1772 assert(ClausesWithImplicit.empty() &&
1773 "No clauses are allowed for 'omp master' directive");
1774 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
1775 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001776 case OMPD_critical:
1777 assert(ClausesWithImplicit.empty() &&
1778 "No clauses are allowed for 'omp critical' directive");
1779 Res = ActOnOpenMPCriticalDirective(DirName, AStmt, StartLoc, EndLoc);
1780 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00001781 case OMPD_parallel_for:
1782 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
1783 EndLoc, VarsWithInheritedDSA);
1784 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00001785 case OMPD_parallel_for_simd:
1786 Res = ActOnOpenMPParallelForSimdDirective(
1787 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
1788 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001789 case OMPD_parallel_sections:
1790 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
1791 StartLoc, EndLoc);
1792 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001793 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001794 Res =
1795 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
1796 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00001797 case OMPD_taskyield:
1798 assert(ClausesWithImplicit.empty() &&
1799 "No clauses are allowed for 'omp taskyield' directive");
1800 assert(AStmt == nullptr &&
1801 "No associated statement allowed for 'omp taskyield' directive");
1802 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
1803 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001804 case OMPD_barrier:
1805 assert(ClausesWithImplicit.empty() &&
1806 "No clauses are allowed for 'omp barrier' directive");
1807 assert(AStmt == nullptr &&
1808 "No associated statement allowed for 'omp barrier' directive");
1809 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
1810 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00001811 case OMPD_taskwait:
1812 assert(ClausesWithImplicit.empty() &&
1813 "No clauses are allowed for 'omp taskwait' directive");
1814 assert(AStmt == nullptr &&
1815 "No associated statement allowed for 'omp taskwait' directive");
1816 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
1817 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00001818 case OMPD_flush:
1819 assert(AStmt == nullptr &&
1820 "No associated statement allowed for 'omp flush' directive");
1821 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
1822 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001823 case OMPD_ordered:
1824 assert(ClausesWithImplicit.empty() &&
1825 "No clauses are allowed for 'omp ordered' directive");
1826 Res = ActOnOpenMPOrderedDirective(AStmt, StartLoc, EndLoc);
1827 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00001828 case OMPD_atomic:
1829 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
1830 EndLoc);
1831 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00001832 case OMPD_teams:
1833 Res =
1834 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
1835 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001836 case OMPD_target:
1837 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
1838 EndLoc);
1839 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001840 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001841 llvm_unreachable("OpenMP Directive is not allowed");
1842 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001843 llvm_unreachable("Unknown OpenMP directive");
1844 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001845
Alexey Bataev4acb8592014-07-07 13:01:15 +00001846 for (auto P : VarsWithInheritedDSA) {
1847 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
1848 << P.first << P.second->getSourceRange();
1849 }
1850 if (!VarsWithInheritedDSA.empty())
1851 return StmtError();
1852
Alexey Bataeved09d242014-05-28 05:53:51 +00001853 if (ErrorFound)
1854 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001855 return Res;
1856}
1857
1858StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
1859 Stmt *AStmt,
1860 SourceLocation StartLoc,
1861 SourceLocation EndLoc) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001862 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1863 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
1864 // 1.2.2 OpenMP Language Terminology
1865 // Structured block - An executable statement with a single entry at the
1866 // top and a single exit at the bottom.
1867 // The point of exit cannot be a branch out of the structured block.
1868 // longjmp() and throw() must not violate the entry/exit criteria.
1869 CS->getCapturedDecl()->setNothrow();
1870
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001871 getCurFunction()->setHasBranchProtectedScope();
1872
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001873 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
1874 AStmt);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001875}
1876
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001877namespace {
1878/// \brief Helper class for checking canonical form of the OpenMP loops and
1879/// extracting iteration space of each loop in the loop nest, that will be used
1880/// for IR generation.
1881class OpenMPIterationSpaceChecker {
1882 /// \brief Reference to Sema.
1883 Sema &SemaRef;
1884 /// \brief A location for diagnostics (when there is no some better location).
1885 SourceLocation DefaultLoc;
1886 /// \brief A location for diagnostics (when increment is not compatible).
1887 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00001888 /// \brief A source location for referring to loop init later.
1889 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001890 /// \brief A source location for referring to condition later.
1891 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00001892 /// \brief A source location for referring to increment later.
1893 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001894 /// \brief Loop variable.
1895 VarDecl *Var;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001896 /// \brief Reference to loop variable.
1897 DeclRefExpr *VarRef;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001898 /// \brief Lower bound (initializer for the var).
1899 Expr *LB;
1900 /// \brief Upper bound.
1901 Expr *UB;
1902 /// \brief Loop step (increment).
1903 Expr *Step;
1904 /// \brief This flag is true when condition is one of:
1905 /// Var < UB
1906 /// Var <= UB
1907 /// UB > Var
1908 /// UB >= Var
1909 bool TestIsLessOp;
1910 /// \brief This flag is true when condition is strict ( < or > ).
1911 bool TestIsStrictOp;
1912 /// \brief This flag is true when step is subtracted on each iteration.
1913 bool SubtractStep;
1914
1915public:
1916 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
1917 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
Alexander Musmana5f070a2014-10-01 06:03:56 +00001918 InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()),
1919 IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001920 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
1921 TestIsStrictOp(false), SubtractStep(false) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001922 /// \brief Check init-expr for canonical loop form and save loop counter
1923 /// variable - #Var and its initialization value - #LB.
1924 bool CheckInit(Stmt *S);
1925 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
1926 /// for less/greater and for strict/non-strict comparison.
1927 bool CheckCond(Expr *S);
1928 /// \brief Check incr-expr for canonical loop form and return true if it
1929 /// does not conform, otherwise save loop step (#Step).
1930 bool CheckInc(Expr *S);
1931 /// \brief Return the loop counter variable.
1932 VarDecl *GetLoopVar() const { return Var; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001933 /// \brief Return the reference expression to loop counter variable.
1934 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00001935 /// \brief Source range of the loop init.
1936 SourceRange GetInitSrcRange() const { return InitSrcRange; }
1937 /// \brief Source range of the loop condition.
1938 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
1939 /// \brief Source range of the loop increment.
1940 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
1941 /// \brief True if the step should be subtracted.
1942 bool ShouldSubtractStep() const { return SubtractStep; }
1943 /// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00001944 Expr *BuildNumIterations(Scope *S, const bool LimitedType) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00001945 /// \brief Build reference expression to the counter be used for codegen.
1946 Expr *BuildCounterVar() const;
1947 /// \brief Build initization of the counter be used for codegen.
1948 Expr *BuildCounterInit() const;
1949 /// \brief Build step of the counter be used for codegen.
1950 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001951 /// \brief Return true if any expression is dependent.
1952 bool Dependent() const;
1953
1954private:
1955 /// \brief Check the right-hand side of an assignment in the increment
1956 /// expression.
1957 bool CheckIncRHS(Expr *RHS);
1958 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001959 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001960 /// \brief Helper to set upper bound.
1961 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, const SourceRange &SR,
1962 const SourceLocation &SL);
1963 /// \brief Helper to set loop increment.
1964 bool SetStep(Expr *NewStep, bool Subtract);
1965};
1966
1967bool OpenMPIterationSpaceChecker::Dependent() const {
1968 if (!Var) {
1969 assert(!LB && !UB && !Step);
1970 return false;
1971 }
1972 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
1973 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
1974}
1975
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001976bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
1977 DeclRefExpr *NewVarRefExpr,
1978 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001979 // State consistency checking to ensure correct usage.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001980 assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
1981 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001982 if (!NewVar || !NewLB)
1983 return true;
1984 Var = NewVar;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001985 VarRef = NewVarRefExpr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001986 LB = NewLB;
1987 return false;
1988}
1989
1990bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
1991 const SourceRange &SR,
1992 const SourceLocation &SL) {
1993 // State consistency checking to ensure correct usage.
1994 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
1995 !TestIsLessOp && !TestIsStrictOp);
1996 if (!NewUB)
1997 return true;
1998 UB = NewUB;
1999 TestIsLessOp = LessOp;
2000 TestIsStrictOp = StrictOp;
2001 ConditionSrcRange = SR;
2002 ConditionLoc = SL;
2003 return false;
2004}
2005
2006bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
2007 // State consistency checking to ensure correct usage.
2008 assert(Var != nullptr && LB != nullptr && Step == nullptr);
2009 if (!NewStep)
2010 return true;
2011 if (!NewStep->isValueDependent()) {
2012 // Check that the step is integer expression.
2013 SourceLocation StepLoc = NewStep->getLocStart();
2014 ExprResult Val =
2015 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
2016 if (Val.isInvalid())
2017 return true;
2018 NewStep = Val.get();
2019
2020 // OpenMP [2.6, Canonical Loop Form, Restrictions]
2021 // If test-expr is of form var relational-op b and relational-op is < or
2022 // <= then incr-expr must cause var to increase on each iteration of the
2023 // loop. If test-expr is of form var relational-op b and relational-op is
2024 // > or >= then incr-expr must cause var to decrease on each iteration of
2025 // the loop.
2026 // If test-expr is of form b relational-op var and relational-op is < or
2027 // <= then incr-expr must cause var to decrease on each iteration of the
2028 // loop. If test-expr is of form b relational-op var and relational-op is
2029 // > or >= then incr-expr must cause var to increase on each iteration of
2030 // the loop.
2031 llvm::APSInt Result;
2032 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
2033 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
2034 bool IsConstNeg =
2035 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00002036 bool IsConstPos =
2037 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002038 bool IsConstZero = IsConstant && !Result.getBoolValue();
2039 if (UB && (IsConstZero ||
2040 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00002041 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002042 SemaRef.Diag(NewStep->getExprLoc(),
2043 diag::err_omp_loop_incr_not_compatible)
2044 << Var << TestIsLessOp << NewStep->getSourceRange();
2045 SemaRef.Diag(ConditionLoc,
2046 diag::note_omp_loop_cond_requres_compatible_incr)
2047 << TestIsLessOp << ConditionSrcRange;
2048 return true;
2049 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002050 if (TestIsLessOp == Subtract) {
2051 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
2052 NewStep).get();
2053 Subtract = !Subtract;
2054 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002055 }
2056
2057 Step = NewStep;
2058 SubtractStep = Subtract;
2059 return false;
2060}
2061
2062bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S) {
2063 // Check init-expr for canonical loop form and save loop counter
2064 // variable - #Var and its initialization value - #LB.
2065 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
2066 // var = lb
2067 // integer-type var = lb
2068 // random-access-iterator-type var = lb
2069 // pointer-type var = lb
2070 //
2071 if (!S) {
2072 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
2073 return true;
2074 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002075 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002076 if (Expr *E = dyn_cast<Expr>(S))
2077 S = E->IgnoreParens();
2078 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2079 if (BO->getOpcode() == BO_Assign)
2080 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002081 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002082 BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002083 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
2084 if (DS->isSingleDecl()) {
2085 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
2086 if (Var->hasInit()) {
2087 // Accept non-canonical init form here but emit ext. warning.
2088 if (Var->getInitStyle() != VarDecl::CInit)
2089 SemaRef.Diag(S->getLocStart(),
2090 diag::ext_omp_loop_not_canonical_init)
2091 << S->getSourceRange();
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002092 return SetVarAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002093 }
2094 }
2095 }
2096 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
2097 if (CE->getOperator() == OO_Equal)
2098 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002099 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
2100 CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002101
2102 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
2103 << S->getSourceRange();
2104 return true;
2105}
2106
Alexey Bataev23b69422014-06-18 07:08:49 +00002107/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002108/// variable (which may be the loop variable) if possible.
2109static const VarDecl *GetInitVarDecl(const Expr *E) {
2110 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00002111 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002112 E = E->IgnoreParenImpCasts();
2113 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
2114 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
2115 if (Ctor->isCopyConstructor() && CE->getNumArgs() == 1 &&
2116 CE->getArg(0) != nullptr)
2117 E = CE->getArg(0)->IgnoreParenImpCasts();
2118 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
2119 if (!DRE)
2120 return nullptr;
2121 return dyn_cast<VarDecl>(DRE->getDecl());
2122}
2123
2124bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
2125 // Check test-expr for canonical form, save upper-bound UB, flags for
2126 // less/greater and for strict/non-strict comparison.
2127 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2128 // var relational-op b
2129 // b relational-op var
2130 //
2131 if (!S) {
2132 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
2133 return true;
2134 }
2135 S = S->IgnoreParenImpCasts();
2136 SourceLocation CondLoc = S->getLocStart();
2137 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2138 if (BO->isRelationalOp()) {
2139 if (GetInitVarDecl(BO->getLHS()) == Var)
2140 return SetUB(BO->getRHS(),
2141 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
2142 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2143 BO->getSourceRange(), BO->getOperatorLoc());
2144 if (GetInitVarDecl(BO->getRHS()) == Var)
2145 return SetUB(BO->getLHS(),
2146 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
2147 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2148 BO->getSourceRange(), BO->getOperatorLoc());
2149 }
2150 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2151 if (CE->getNumArgs() == 2) {
2152 auto Op = CE->getOperator();
2153 switch (Op) {
2154 case OO_Greater:
2155 case OO_GreaterEqual:
2156 case OO_Less:
2157 case OO_LessEqual:
2158 if (GetInitVarDecl(CE->getArg(0)) == Var)
2159 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
2160 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2161 CE->getOperatorLoc());
2162 if (GetInitVarDecl(CE->getArg(1)) == Var)
2163 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
2164 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2165 CE->getOperatorLoc());
2166 break;
2167 default:
2168 break;
2169 }
2170 }
2171 }
2172 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
2173 << S->getSourceRange() << Var;
2174 return true;
2175}
2176
2177bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
2178 // RHS of canonical loop form increment can be:
2179 // var + incr
2180 // incr + var
2181 // var - incr
2182 //
2183 RHS = RHS->IgnoreParenImpCasts();
2184 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
2185 if (BO->isAdditiveOp()) {
2186 bool IsAdd = BO->getOpcode() == BO_Add;
2187 if (GetInitVarDecl(BO->getLHS()) == Var)
2188 return SetStep(BO->getRHS(), !IsAdd);
2189 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
2190 return SetStep(BO->getLHS(), false);
2191 }
2192 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
2193 bool IsAdd = CE->getOperator() == OO_Plus;
2194 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
2195 if (GetInitVarDecl(CE->getArg(0)) == Var)
2196 return SetStep(CE->getArg(1), !IsAdd);
2197 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
2198 return SetStep(CE->getArg(0), false);
2199 }
2200 }
2201 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2202 << RHS->getSourceRange() << Var;
2203 return true;
2204}
2205
2206bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
2207 // Check incr-expr for canonical loop form and return true if it
2208 // does not conform.
2209 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2210 // ++var
2211 // var++
2212 // --var
2213 // var--
2214 // var += incr
2215 // var -= incr
2216 // var = var + incr
2217 // var = incr + var
2218 // var = var - incr
2219 //
2220 if (!S) {
2221 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
2222 return true;
2223 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002224 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002225 S = S->IgnoreParens();
2226 if (auto UO = dyn_cast<UnaryOperator>(S)) {
2227 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
2228 return SetStep(
2229 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
2230 (UO->isDecrementOp() ? -1 : 1)).get(),
2231 false);
2232 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
2233 switch (BO->getOpcode()) {
2234 case BO_AddAssign:
2235 case BO_SubAssign:
2236 if (GetInitVarDecl(BO->getLHS()) == Var)
2237 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
2238 break;
2239 case BO_Assign:
2240 if (GetInitVarDecl(BO->getLHS()) == Var)
2241 return CheckIncRHS(BO->getRHS());
2242 break;
2243 default:
2244 break;
2245 }
2246 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2247 switch (CE->getOperator()) {
2248 case OO_PlusPlus:
2249 case OO_MinusMinus:
2250 if (GetInitVarDecl(CE->getArg(0)) == Var)
2251 return SetStep(
2252 SemaRef.ActOnIntegerConstant(
2253 CE->getLocStart(),
2254 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
2255 false);
2256 break;
2257 case OO_PlusEqual:
2258 case OO_MinusEqual:
2259 if (GetInitVarDecl(CE->getArg(0)) == Var)
2260 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
2261 break;
2262 case OO_Equal:
2263 if (GetInitVarDecl(CE->getArg(0)) == Var)
2264 return CheckIncRHS(CE->getArg(1));
2265 break;
2266 default:
2267 break;
2268 }
2269 }
2270 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2271 << S->getSourceRange() << Var;
2272 return true;
2273}
Alexander Musmana5f070a2014-10-01 06:03:56 +00002274
2275/// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00002276Expr *
2277OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S,
2278 const bool LimitedType) const {
Alexander Musmana5f070a2014-10-01 06:03:56 +00002279 ExprResult Diff;
2280 if (Var->getType()->isIntegerType() || Var->getType()->isPointerType() ||
2281 SemaRef.getLangOpts().CPlusPlus) {
2282 // Upper - Lower
2283 Expr *Upper = TestIsLessOp ? UB : LB;
2284 Expr *Lower = TestIsLessOp ? LB : UB;
2285
2286 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
2287
2288 if (!Diff.isUsable() && Var->getType()->getAsCXXRecordDecl()) {
2289 // BuildBinOp already emitted error, this one is to point user to upper
2290 // and lower bound, and to tell what is passed to 'operator-'.
2291 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
2292 << Upper->getSourceRange() << Lower->getSourceRange();
2293 return nullptr;
2294 }
2295 }
2296
2297 if (!Diff.isUsable())
2298 return nullptr;
2299
2300 // Upper - Lower [- 1]
2301 if (TestIsStrictOp)
2302 Diff = SemaRef.BuildBinOp(
2303 S, DefaultLoc, BO_Sub, Diff.get(),
2304 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
2305 if (!Diff.isUsable())
2306 return nullptr;
2307
2308 // Upper - Lower [- 1] + Step
2309 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(),
2310 Step->IgnoreImplicit());
2311 if (!Diff.isUsable())
2312 return nullptr;
2313
2314 // Parentheses (for dumping/debugging purposes only).
2315 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
2316 if (!Diff.isUsable())
2317 return nullptr;
2318
2319 // (Upper - Lower [- 1] + Step) / Step
2320 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(),
2321 Step->IgnoreImplicit());
2322 if (!Diff.isUsable())
2323 return nullptr;
2324
Alexander Musman174b3ca2014-10-06 11:16:29 +00002325 // OpenMP runtime requires 32-bit or 64-bit loop variables.
2326 if (LimitedType) {
2327 auto &C = SemaRef.Context;
2328 QualType Type = Diff.get()->getType();
2329 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
2330 if (NewSize != C.getTypeSize(Type)) {
2331 if (NewSize < C.getTypeSize(Type)) {
2332 assert(NewSize == 64 && "incorrect loop var size");
2333 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
2334 << InitSrcRange << ConditionSrcRange;
2335 }
2336 QualType NewType = C.getIntTypeForBitwidth(
2337 NewSize, Type->hasSignedIntegerRepresentation());
2338 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
2339 Sema::AA_Converting, true);
2340 if (!Diff.isUsable())
2341 return nullptr;
2342 }
2343 }
2344
Alexander Musmana5f070a2014-10-01 06:03:56 +00002345 return Diff.get();
2346}
2347
2348/// \brief Build reference expression to the counter be used for codegen.
2349Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const {
2350 return DeclRefExpr::Create(SemaRef.Context, NestedNameSpecifierLoc(),
2351 GetIncrementSrcRange().getBegin(), Var, false,
2352 DefaultLoc, Var->getType(), VK_LValue);
2353}
2354
2355/// \brief Build initization of the counter be used for codegen.
2356Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
2357
2358/// \brief Build step of the counter be used for codegen.
2359Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
2360
2361/// \brief Iteration space of a single for loop.
2362struct LoopIterationSpace {
2363 /// \brief This expression calculates the number of iterations in the loop.
2364 /// It is always possible to calculate it before starting the loop.
2365 Expr *NumIterations;
2366 /// \brief The loop counter variable.
2367 Expr *CounterVar;
2368 /// \brief This is initializer for the initial value of #CounterVar.
2369 Expr *CounterInit;
2370 /// \brief This is step for the #CounterVar used to generate its update:
2371 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
2372 Expr *CounterStep;
2373 /// \brief Should step be subtracted?
2374 bool Subtract;
2375 /// \brief Source range of the loop init.
2376 SourceRange InitSrcRange;
2377 /// \brief Source range of the loop condition.
2378 SourceRange CondSrcRange;
2379 /// \brief Source range of the loop increment.
2380 SourceRange IncSrcRange;
2381};
2382
2383/// \brief The resulting expressions built for the OpenMP loop CodeGen for the
2384/// whole collapsed loop nest. See class OMPLoopDirective for their description.
2385struct BuiltLoopExprs {
2386 Expr *IterationVarRef;
2387 Expr *LastIteration;
2388 Expr *CalcLastIteration;
2389 Expr *PreCond;
2390 Expr *Cond;
2391 Expr *SeparatedCond;
2392 Expr *Init;
2393 Expr *Inc;
2394 SmallVector<Expr *, 4> Counters;
2395 SmallVector<Expr *, 4> Updates;
2396 SmallVector<Expr *, 4> Finals;
2397
2398 bool builtAll() {
2399 return IterationVarRef != nullptr && LastIteration != nullptr &&
2400 PreCond != nullptr && Cond != nullptr && SeparatedCond != nullptr &&
2401 Init != nullptr && Inc != nullptr;
2402 }
2403 void clear(unsigned size) {
2404 IterationVarRef = nullptr;
2405 LastIteration = nullptr;
2406 CalcLastIteration = nullptr;
2407 PreCond = nullptr;
2408 Cond = nullptr;
2409 SeparatedCond = nullptr;
2410 Init = nullptr;
2411 Inc = nullptr;
2412 Counters.resize(size);
2413 Updates.resize(size);
2414 Finals.resize(size);
2415 for (unsigned i = 0; i < size; ++i) {
2416 Counters[i] = nullptr;
2417 Updates[i] = nullptr;
2418 Finals[i] = nullptr;
2419 }
2420 }
2421};
2422
Alexey Bataev23b69422014-06-18 07:08:49 +00002423} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002424
2425/// \brief Called on a for stmt to check and extract its iteration space
2426/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00002427static bool CheckOpenMPIterationSpace(
2428 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
2429 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
2430 Expr *NestedLoopCountExpr,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002431 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
2432 LoopIterationSpace &ResultIterSpace) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002433 // OpenMP [2.6, Canonical Loop Form]
2434 // for (init-expr; test-expr; incr-expr) structured-block
2435 auto For = dyn_cast_or_null<ForStmt>(S);
2436 if (!For) {
2437 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002438 << (NestedLoopCountExpr != nullptr) << getOpenMPDirectiveName(DKind)
2439 << NestedLoopCount << (CurrentNestedLoopCount > 0)
2440 << CurrentNestedLoopCount;
2441 if (NestedLoopCount > 1)
2442 SemaRef.Diag(NestedLoopCountExpr->getExprLoc(),
2443 diag::note_omp_collapse_expr)
2444 << NestedLoopCountExpr->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002445 return true;
2446 }
2447 assert(For->getBody());
2448
2449 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
2450
2451 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002452 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002453 if (ISC.CheckInit(Init)) {
2454 return true;
2455 }
2456
2457 bool HasErrors = false;
2458
2459 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002460 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002461
2462 // OpenMP [2.6, Canonical Loop Form]
2463 // Var is one of the following:
2464 // A variable of signed or unsigned integer type.
2465 // For C++, a variable of a random access iterator type.
2466 // For C, a variable of a pointer type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002467 auto VarType = Var->getType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002468 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
2469 !VarType->isPointerType() &&
2470 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
2471 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
2472 << SemaRef.getLangOpts().CPlusPlus;
2473 HasErrors = true;
2474 }
2475
Alexey Bataev4acb8592014-07-07 13:01:15 +00002476 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
2477 // Construct
2478 // The loop iteration variable(s) in the associated for-loop(s) of a for or
2479 // parallel for construct is (are) private.
2480 // The loop iteration variable in the associated for-loop of a simd construct
2481 // with just one associated for-loop is linear with a constant-linear-step
2482 // that is the increment of the associated for-loop.
2483 // Exclude loop var from the list of variables with implicitly defined data
2484 // sharing attributes.
Benjamin Kramerad8e0792014-10-10 15:32:48 +00002485 VarsWithImplicitDSA.erase(Var);
Alexey Bataev4acb8592014-07-07 13:01:15 +00002486
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002487 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
2488 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00002489 // The loop iteration variable in the associated for-loop of a simd construct
2490 // with just one associated for-loop may be listed in a linear clause with a
2491 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002492 // The loop iteration variable(s) in the associated for-loop(s) of a for or
2493 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002494 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002495 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
2496 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
2497 // declared in the loop and it is predetermined as a private.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002498 auto PredeterminedCKind =
2499 isOpenMPSimdDirective(DKind)
2500 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
2501 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002502 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00002503 DVar.CKind != PredeterminedCKind) ||
Alexander Musmanf82886e2014-09-18 05:12:34 +00002504 (isOpenMPWorksharingDirective(DKind) && !isOpenMPSimdDirective(DKind) &&
2505 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private &&
2506 DVar.CKind != OMPC_lastprivate)) &&
Alexander Musman1bb328c2014-06-04 13:06:39 +00002507 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002508 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00002509 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
2510 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002511 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002512 HasErrors = true;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002513 } else if (LoopVarRefExpr != nullptr) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00002514 // Make the loop iteration variable private (for worksharing constructs),
2515 // linear (for simd directives with the only one associated loop) or
2516 // lastprivate (for simd directives with several collapsed loops).
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002517 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002518 }
2519
Alexey Bataev7ff55242014-06-19 09:13:45 +00002520 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00002521
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002522 // Check test-expr.
2523 HasErrors |= ISC.CheckCond(For->getCond());
2524
2525 // Check incr-expr.
2526 HasErrors |= ISC.CheckInc(For->getInc());
2527
Alexander Musmana5f070a2014-10-01 06:03:56 +00002528 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002529 return HasErrors;
2530
Alexander Musmana5f070a2014-10-01 06:03:56 +00002531 // Build the loop's iteration space representation.
Alexander Musman174b3ca2014-10-06 11:16:29 +00002532 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
2533 DSA.getCurScope(), /* LimitedType */ isOpenMPWorksharingDirective(DKind));
Alexander Musmana5f070a2014-10-01 06:03:56 +00002534 ResultIterSpace.CounterVar = ISC.BuildCounterVar();
2535 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
2536 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
2537 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
2538 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
2539 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
2540 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
2541
2542 HasErrors |= (ResultIterSpace.NumIterations == nullptr ||
2543 ResultIterSpace.CounterVar == nullptr ||
2544 ResultIterSpace.CounterInit == nullptr ||
2545 ResultIterSpace.CounterStep == nullptr);
2546
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002547 return HasErrors;
2548}
2549
Alexander Musmana5f070a2014-10-01 06:03:56 +00002550/// \brief Build a variable declaration for OpenMP loop iteration variable.
2551static VarDecl *BuildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
2552 StringRef Name) {
2553 DeclContext *DC = SemaRef.CurContext;
2554 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
2555 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
2556 VarDecl *Decl =
2557 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
2558 Decl->setImplicit();
2559 return Decl;
2560}
2561
2562/// \brief Build 'VarRef = Start + Iter * Step'.
2563static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S,
2564 SourceLocation Loc, ExprResult VarRef,
2565 ExprResult Start, ExprResult Iter,
2566 ExprResult Step, bool Subtract) {
2567 // Add parentheses (for debugging purposes only).
2568 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
2569 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
2570 !Step.isUsable())
2571 return ExprError();
2572
2573 ExprResult Update = SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(),
2574 Step.get()->IgnoreImplicit());
2575 if (!Update.isUsable())
2576 return ExprError();
2577
2578 // Build 'VarRef = Start + Iter * Step'.
2579 Update = SemaRef.BuildBinOp(S, Loc, (Subtract ? BO_Sub : BO_Add),
2580 Start.get()->IgnoreImplicit(), Update.get());
2581 if (!Update.isUsable())
2582 return ExprError();
2583
2584 Update = SemaRef.PerformImplicitConversion(
2585 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
2586 if (!Update.isUsable())
2587 return ExprError();
2588
2589 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
2590 return Update;
2591}
2592
2593/// \brief Convert integer expression \a E to make it have at least \a Bits
2594/// bits.
2595static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
2596 Sema &SemaRef) {
2597 if (E == nullptr)
2598 return ExprError();
2599 auto &C = SemaRef.Context;
2600 QualType OldType = E->getType();
2601 unsigned HasBits = C.getTypeSize(OldType);
2602 if (HasBits >= Bits)
2603 return ExprResult(E);
2604 // OK to convert to signed, because new type has more bits than old.
2605 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
2606 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
2607 true);
2608}
2609
2610/// \brief Check if the given expression \a E is a constant integer that fits
2611/// into \a Bits bits.
2612static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
2613 if (E == nullptr)
2614 return false;
2615 llvm::APSInt Result;
2616 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
2617 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
2618 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002619}
2620
2621/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00002622/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
2623/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002624static unsigned
2625CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *NestedLoopCountExpr,
2626 Stmt *AStmt, Sema &SemaRef, DSAStackTy &DSA,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002627 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
2628 BuiltLoopExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002629 unsigned NestedLoopCount = 1;
2630 if (NestedLoopCountExpr) {
2631 // Found 'collapse' clause - calculate collapse number.
2632 llvm::APSInt Result;
2633 if (NestedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
2634 NestedLoopCount = Result.getLimitedValue();
2635 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002636 // This is helper routine for loop directives (e.g., 'for', 'simd',
2637 // 'for simd', etc.).
Alexander Musmana5f070a2014-10-01 06:03:56 +00002638 SmallVector<LoopIterationSpace, 4> IterSpaces;
2639 IterSpaces.resize(NestedLoopCount);
2640 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002641 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002642 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev4acb8592014-07-07 13:01:15 +00002643 NestedLoopCount, NestedLoopCountExpr,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002644 VarsWithImplicitDSA, IterSpaces[Cnt]))
Alexey Bataevabfc0692014-06-25 06:52:00 +00002645 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002646 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00002647 // OpenMP [2.8.1, simd construct, Restrictions]
2648 // All loops associated with the construct must be perfectly nested; that
2649 // is, there must be no intervening code nor any OpenMP directive between
2650 // any two loops.
2651 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002652 }
2653
Alexander Musmana5f070a2014-10-01 06:03:56 +00002654 Built.clear(/* size */ NestedLoopCount);
2655
2656 if (SemaRef.CurContext->isDependentContext())
2657 return NestedLoopCount;
2658
2659 // An example of what is generated for the following code:
2660 //
2661 // #pragma omp simd collapse(2)
2662 // for (i = 0; i < NI; ++i)
2663 // for (j = J0; j < NJ; j+=2) {
2664 // <loop body>
2665 // }
2666 //
2667 // We generate the code below.
2668 // Note: the loop body may be outlined in CodeGen.
2669 // Note: some counters may be C++ classes, operator- is used to find number of
2670 // iterations and operator+= to calculate counter value.
2671 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
2672 // or i64 is currently supported).
2673 //
2674 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
2675 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
2676 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
2677 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
2678 // // similar updates for vars in clauses (e.g. 'linear')
2679 // <loop body (using local i and j)>
2680 // }
2681 // i = NI; // assign final values of counters
2682 // j = NJ;
2683 //
2684
2685 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
2686 // the iteration counts of the collapsed for loops.
2687 auto N0 = IterSpaces[0].NumIterations;
2688 ExprResult LastIteration32 = WidenIterationCount(32 /* Bits */, N0, SemaRef);
2689 ExprResult LastIteration64 = WidenIterationCount(64 /* Bits */, N0, SemaRef);
2690
2691 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
2692 return NestedLoopCount;
2693
2694 auto &C = SemaRef.Context;
2695 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
2696
2697 Scope *CurScope = DSA.getCurScope();
2698 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
2699 auto N = IterSpaces[Cnt].NumIterations;
2700 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
2701 if (LastIteration32.isUsable())
2702 LastIteration32 = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_Mul,
2703 LastIteration32.get(), N);
2704 if (LastIteration64.isUsable())
2705 LastIteration64 = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_Mul,
2706 LastIteration64.get(), N);
2707 }
2708
2709 // Choose either the 32-bit or 64-bit version.
2710 ExprResult LastIteration = LastIteration64;
2711 if (LastIteration32.isUsable() &&
2712 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
2713 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
2714 FitsInto(
2715 32 /* Bits */,
2716 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
2717 LastIteration64.get(), SemaRef)))
2718 LastIteration = LastIteration32;
2719
2720 if (!LastIteration.isUsable())
2721 return 0;
2722
2723 // Save the number of iterations.
2724 ExprResult NumIterations = LastIteration;
2725 {
2726 LastIteration = SemaRef.BuildBinOp(
2727 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
2728 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
2729 if (!LastIteration.isUsable())
2730 return 0;
2731 }
2732
2733 // Calculate the last iteration number beforehand instead of doing this on
2734 // each iteration. Do not do this if the number of iterations may be kfold-ed.
2735 llvm::APSInt Result;
2736 bool IsConstant =
2737 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
2738 ExprResult CalcLastIteration;
2739 if (!IsConstant) {
2740 SourceLocation SaveLoc;
2741 VarDecl *SaveVar =
2742 BuildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(),
2743 ".omp.last.iteration");
2744 ExprResult SaveRef = SemaRef.BuildDeclRefExpr(
2745 SaveVar, LastIteration.get()->getType(), VK_LValue, SaveLoc);
2746 CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign,
2747 SaveRef.get(), LastIteration.get());
2748 LastIteration = SaveRef;
2749
2750 // Prepare SaveRef + 1.
2751 NumIterations = SemaRef.BuildBinOp(
2752 CurScope, SaveLoc, BO_Add, SaveRef.get(),
2753 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
2754 if (!NumIterations.isUsable())
2755 return 0;
2756 }
2757
2758 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
2759
2760 // Precondition tests if there is at least one iteration (LastIteration > 0).
2761 ExprResult PreCond = SemaRef.BuildBinOp(
2762 CurScope, InitLoc, BO_GT, LastIteration.get(),
2763 SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get());
2764
2765 // Build the iteration variable and its initialization to zero before loop.
2766 ExprResult IV;
2767 ExprResult Init;
2768 {
2769 VarDecl *IVDecl = BuildVarDecl(SemaRef, InitLoc,
2770 LastIteration.get()->getType(), ".omp.iv");
2771 IV = SemaRef.BuildDeclRefExpr(IVDecl, LastIteration.get()->getType(),
2772 VK_LValue, InitLoc);
2773 Init = SemaRef.BuildBinOp(
2774 CurScope, InitLoc, BO_Assign, IV.get(),
2775 SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get());
2776 }
2777
2778 // Loop condition (IV < NumIterations)
2779 SourceLocation CondLoc;
2780 ExprResult Cond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
2781 NumIterations.get());
2782 // Loop condition with 1 iteration separated (IV < LastIteration)
2783 ExprResult SeparatedCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT,
2784 IV.get(), LastIteration.get());
2785
2786 // Loop increment (IV = IV + 1)
2787 SourceLocation IncLoc;
2788 ExprResult Inc =
2789 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
2790 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
2791 if (!Inc.isUsable())
2792 return 0;
2793 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
2794
2795 // Build updates and final values of the loop counters.
2796 bool HasErrors = false;
2797 Built.Counters.resize(NestedLoopCount);
2798 Built.Updates.resize(NestedLoopCount);
2799 Built.Finals.resize(NestedLoopCount);
2800 {
2801 ExprResult Div;
2802 // Go from inner nested loop to outer.
2803 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
2804 LoopIterationSpace &IS = IterSpaces[Cnt];
2805 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
2806 // Build: Iter = (IV / Div) % IS.NumIters
2807 // where Div is product of previous iterations' IS.NumIters.
2808 ExprResult Iter;
2809 if (Div.isUsable()) {
2810 Iter =
2811 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
2812 } else {
2813 Iter = IV;
2814 assert((Cnt == (int)NestedLoopCount - 1) &&
2815 "unusable div expected on first iteration only");
2816 }
2817
2818 if (Cnt != 0 && Iter.isUsable())
2819 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
2820 IS.NumIterations);
2821 if (!Iter.isUsable()) {
2822 HasErrors = true;
2823 break;
2824 }
2825
2826 // Build update: IS.CounterVar = IS.Start + Iter * IS.Step
2827 ExprResult Update =
2828 BuildCounterUpdate(SemaRef, CurScope, UpdLoc, IS.CounterVar,
2829 IS.CounterInit, Iter, IS.CounterStep, IS.Subtract);
2830 if (!Update.isUsable()) {
2831 HasErrors = true;
2832 break;
2833 }
2834
2835 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
2836 ExprResult Final = BuildCounterUpdate(
2837 SemaRef, CurScope, UpdLoc, IS.CounterVar, IS.CounterInit,
2838 IS.NumIterations, IS.CounterStep, IS.Subtract);
2839 if (!Final.isUsable()) {
2840 HasErrors = true;
2841 break;
2842 }
2843
2844 // Build Div for the next iteration: Div <- Div * IS.NumIters
2845 if (Cnt != 0) {
2846 if (Div.isUnset())
2847 Div = IS.NumIterations;
2848 else
2849 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
2850 IS.NumIterations);
2851
2852 // Add parentheses (for debugging purposes only).
2853 if (Div.isUsable())
2854 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
2855 if (!Div.isUsable()) {
2856 HasErrors = true;
2857 break;
2858 }
2859 }
2860 if (!Update.isUsable() || !Final.isUsable()) {
2861 HasErrors = true;
2862 break;
2863 }
2864 // Save results
2865 Built.Counters[Cnt] = IS.CounterVar;
2866 Built.Updates[Cnt] = Update.get();
2867 Built.Finals[Cnt] = Final.get();
2868 }
2869 }
2870
2871 if (HasErrors)
2872 return 0;
2873
2874 // Save results
2875 Built.IterationVarRef = IV.get();
2876 Built.LastIteration = LastIteration.get();
2877 Built.CalcLastIteration = CalcLastIteration.get();
2878 Built.PreCond = PreCond.get();
2879 Built.Cond = Cond.get();
2880 Built.SeparatedCond = SeparatedCond.get();
2881 Built.Init = Init.get();
2882 Built.Inc = Inc.get();
2883
Alexey Bataevabfc0692014-06-25 06:52:00 +00002884 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002885}
2886
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002887static Expr *GetCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002888 auto CollapseFilter = [](const OMPClause *C) -> bool {
2889 return C->getClauseKind() == OMPC_collapse;
2890 };
2891 OMPExecutableDirective::filtered_clause_iterator<decltype(CollapseFilter)> I(
2892 Clauses, CollapseFilter);
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002893 if (I)
2894 return cast<OMPCollapseClause>(*I)->getNumForLoops();
2895 return nullptr;
2896}
2897
Alexey Bataev4acb8592014-07-07 13:01:15 +00002898StmtResult Sema::ActOnOpenMPSimdDirective(
2899 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2900 SourceLocation EndLoc,
2901 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00002902 BuiltLoopExprs B;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002903 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002904 unsigned NestedLoopCount =
2905 CheckOpenMPLoop(OMPD_simd, GetCollapseNumberExpr(Clauses), AStmt, *this,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002906 *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00002907 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002908 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002909
Alexander Musmana5f070a2014-10-01 06:03:56 +00002910 assert((CurContext->isDependentContext() || B.builtAll()) &&
2911 "omp simd loop exprs were not built");
2912
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002913 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00002914 return OMPSimdDirective::Create(
2915 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt,
2916 B.IterationVarRef, B.LastIteration, B.CalcLastIteration, B.PreCond,
2917 B.Cond, B.SeparatedCond, B.Init, B.Inc, B.Counters, B.Updates, B.Finals);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002918}
2919
Alexey Bataev4acb8592014-07-07 13:01:15 +00002920StmtResult Sema::ActOnOpenMPForDirective(
2921 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2922 SourceLocation EndLoc,
2923 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00002924 BuiltLoopExprs B;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002925 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002926 unsigned NestedLoopCount =
2927 CheckOpenMPLoop(OMPD_for, GetCollapseNumberExpr(Clauses), AStmt, *this,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002928 *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00002929 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00002930 return StmtError();
2931
Alexander Musmana5f070a2014-10-01 06:03:56 +00002932 assert((CurContext->isDependentContext() || B.builtAll()) &&
2933 "omp for loop exprs were not built");
2934
Alexey Bataevf29276e2014-06-18 04:14:57 +00002935 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00002936 return OMPForDirective::Create(
2937 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt,
2938 B.IterationVarRef, B.LastIteration, B.CalcLastIteration, B.PreCond,
2939 B.Cond, B.SeparatedCond, B.Init, B.Inc, B.Counters, B.Updates, B.Finals);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002940}
2941
Alexander Musmanf82886e2014-09-18 05:12:34 +00002942StmtResult Sema::ActOnOpenMPForSimdDirective(
2943 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2944 SourceLocation EndLoc,
2945 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00002946 BuiltLoopExprs B;
Alexander Musmanf82886e2014-09-18 05:12:34 +00002947 // In presence of clause 'collapse', it will define the nested loops number.
2948 unsigned NestedLoopCount =
2949 CheckOpenMPLoop(OMPD_for_simd, GetCollapseNumberExpr(Clauses), AStmt,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002950 *this, *DSAStack, VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00002951 if (NestedLoopCount == 0)
2952 return StmtError();
2953
2954 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00002955 return OMPForSimdDirective::Create(
2956 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt,
2957 B.IterationVarRef, B.LastIteration, B.CalcLastIteration, B.PreCond,
2958 B.Cond, B.SeparatedCond, B.Init, B.Inc, B.Counters, B.Updates, B.Finals);
Alexander Musmanf82886e2014-09-18 05:12:34 +00002959}
2960
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002961StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
2962 Stmt *AStmt,
2963 SourceLocation StartLoc,
2964 SourceLocation EndLoc) {
2965 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2966 auto BaseStmt = AStmt;
2967 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
2968 BaseStmt = CS->getCapturedStmt();
2969 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
2970 auto S = C->children();
2971 if (!S)
2972 return StmtError();
2973 // All associated statements must be '#pragma omp section' except for
2974 // the first one.
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002975 for (++S; S; ++S) {
2976 auto SectionStmt = *S;
2977 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
2978 if (SectionStmt)
2979 Diag(SectionStmt->getLocStart(),
2980 diag::err_omp_sections_substmt_not_section);
2981 return StmtError();
2982 }
2983 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002984 } else {
2985 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
2986 return StmtError();
2987 }
2988
2989 getCurFunction()->setHasBranchProtectedScope();
2990
2991 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses,
2992 AStmt);
2993}
2994
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002995StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
2996 SourceLocation StartLoc,
2997 SourceLocation EndLoc) {
2998 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2999
3000 getCurFunction()->setHasBranchProtectedScope();
3001
3002 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt);
3003}
3004
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003005StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
3006 Stmt *AStmt,
3007 SourceLocation StartLoc,
3008 SourceLocation EndLoc) {
Alexey Bataev74a05c92014-07-15 02:55:09 +00003009 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3010
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003011 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00003012
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003013 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3014}
3015
Alexander Musman80c22892014-07-17 08:54:58 +00003016StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
3017 SourceLocation StartLoc,
3018 SourceLocation EndLoc) {
3019 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3020
3021 getCurFunction()->setHasBranchProtectedScope();
3022
3023 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
3024}
3025
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003026StmtResult
3027Sema::ActOnOpenMPCriticalDirective(const DeclarationNameInfo &DirName,
3028 Stmt *AStmt, SourceLocation StartLoc,
3029 SourceLocation EndLoc) {
3030 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3031
3032 getCurFunction()->setHasBranchProtectedScope();
3033
3034 return OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
3035 AStmt);
3036}
3037
Alexey Bataev4acb8592014-07-07 13:01:15 +00003038StmtResult Sema::ActOnOpenMPParallelForDirective(
3039 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3040 SourceLocation EndLoc,
3041 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
3042 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3043 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3044 // 1.2.2 OpenMP Language Terminology
3045 // Structured block - An executable statement with a single entry at the
3046 // top and a single exit at the bottom.
3047 // The point of exit cannot be a branch out of the structured block.
3048 // longjmp() and throw() must not violate the entry/exit criteria.
3049 CS->getCapturedDecl()->setNothrow();
3050
Alexander Musmana5f070a2014-10-01 06:03:56 +00003051 BuiltLoopExprs B;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003052 // In presence of clause 'collapse', it will define the nested loops number.
3053 unsigned NestedLoopCount =
3054 CheckOpenMPLoop(OMPD_parallel_for, GetCollapseNumberExpr(Clauses), AStmt,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003055 *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003056 if (NestedLoopCount == 0)
3057 return StmtError();
3058
Alexander Musmana5f070a2014-10-01 06:03:56 +00003059 assert((CurContext->isDependentContext() || B.builtAll()) &&
3060 "omp parallel for loop exprs were not built");
3061
Alexey Bataev4acb8592014-07-07 13:01:15 +00003062 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003063 return OMPParallelForDirective::Create(
3064 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt,
3065 B.IterationVarRef, B.LastIteration, B.CalcLastIteration, B.PreCond,
3066 B.Cond, B.SeparatedCond, B.Init, B.Inc, B.Counters, B.Updates, B.Finals);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003067}
3068
Alexander Musmane4e893b2014-09-23 09:33:00 +00003069StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
3070 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3071 SourceLocation EndLoc,
3072 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
3073 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3074 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3075 // 1.2.2 OpenMP Language Terminology
3076 // Structured block - An executable statement with a single entry at the
3077 // top and a single exit at the bottom.
3078 // The point of exit cannot be a branch out of the structured block.
3079 // longjmp() and throw() must not violate the entry/exit criteria.
3080 CS->getCapturedDecl()->setNothrow();
3081
Alexander Musmana5f070a2014-10-01 06:03:56 +00003082 BuiltLoopExprs B;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003083 // In presence of clause 'collapse', it will define the nested loops number.
3084 unsigned NestedLoopCount =
3085 CheckOpenMPLoop(OMPD_parallel_for_simd, GetCollapseNumberExpr(Clauses),
Alexander Musmana5f070a2014-10-01 06:03:56 +00003086 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003087 if (NestedLoopCount == 0)
3088 return StmtError();
3089
3090 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003091 return OMPParallelForSimdDirective::Create(
3092 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt,
3093 B.IterationVarRef, B.LastIteration, B.CalcLastIteration, B.PreCond,
3094 B.Cond, B.SeparatedCond, B.Init, B.Inc, B.Counters, B.Updates, B.Finals);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003095}
3096
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003097StmtResult
3098Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
3099 Stmt *AStmt, SourceLocation StartLoc,
3100 SourceLocation EndLoc) {
3101 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3102 auto BaseStmt = AStmt;
3103 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
3104 BaseStmt = CS->getCapturedStmt();
3105 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
3106 auto S = C->children();
3107 if (!S)
3108 return StmtError();
3109 // All associated statements must be '#pragma omp section' except for
3110 // the first one.
3111 for (++S; S; ++S) {
3112 auto SectionStmt = *S;
3113 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
3114 if (SectionStmt)
3115 Diag(SectionStmt->getLocStart(),
3116 diag::err_omp_parallel_sections_substmt_not_section);
3117 return StmtError();
3118 }
3119 }
3120 } else {
3121 Diag(AStmt->getLocStart(),
3122 diag::err_omp_parallel_sections_not_compound_stmt);
3123 return StmtError();
3124 }
3125
3126 getCurFunction()->setHasBranchProtectedScope();
3127
3128 return OMPParallelSectionsDirective::Create(Context, StartLoc, EndLoc,
3129 Clauses, AStmt);
3130}
3131
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003132StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
3133 Stmt *AStmt, SourceLocation StartLoc,
3134 SourceLocation EndLoc) {
3135 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3136 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3137 // 1.2.2 OpenMP Language Terminology
3138 // Structured block - An executable statement with a single entry at the
3139 // top and a single exit at the bottom.
3140 // The point of exit cannot be a branch out of the structured block.
3141 // longjmp() and throw() must not violate the entry/exit criteria.
3142 CS->getCapturedDecl()->setNothrow();
3143
3144 getCurFunction()->setHasBranchProtectedScope();
3145
3146 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3147}
3148
Alexey Bataev68446b72014-07-18 07:47:19 +00003149StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
3150 SourceLocation EndLoc) {
3151 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
3152}
3153
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003154StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
3155 SourceLocation EndLoc) {
3156 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
3157}
3158
Alexey Bataev2df347a2014-07-18 10:17:07 +00003159StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
3160 SourceLocation EndLoc) {
3161 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
3162}
3163
Alexey Bataev6125da92014-07-21 11:26:11 +00003164StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
3165 SourceLocation StartLoc,
3166 SourceLocation EndLoc) {
3167 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
3168 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
3169}
3170
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003171StmtResult Sema::ActOnOpenMPOrderedDirective(Stmt *AStmt,
3172 SourceLocation StartLoc,
3173 SourceLocation EndLoc) {
3174 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3175
3176 getCurFunction()->setHasBranchProtectedScope();
3177
3178 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, AStmt);
3179}
3180
Alexey Bataev0162e452014-07-22 10:10:35 +00003181StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
3182 Stmt *AStmt,
3183 SourceLocation StartLoc,
3184 SourceLocation EndLoc) {
3185 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003186 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00003187 // 1.2.2 OpenMP Language Terminology
3188 // Structured block - An executable statement with a single entry at the
3189 // top and a single exit at the bottom.
3190 // The point of exit cannot be a branch out of the structured block.
3191 // longjmp() and throw() must not violate the entry/exit criteria.
3192 // TODO further analysis of associated statements and clauses.
Alexey Bataevdea47612014-07-23 07:46:59 +00003193 OpenMPClauseKind AtomicKind = OMPC_unknown;
3194 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003195 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00003196 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00003197 C->getClauseKind() == OMPC_update ||
3198 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00003199 if (AtomicKind != OMPC_unknown) {
3200 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
3201 << SourceRange(C->getLocStart(), C->getLocEnd());
3202 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
3203 << getOpenMPClauseName(AtomicKind);
3204 } else {
3205 AtomicKind = C->getClauseKind();
3206 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003207 }
3208 }
3209 }
Alexey Bataev459dec02014-07-24 06:46:57 +00003210 auto Body = CS->getCapturedStmt();
Alexey Bataevdea47612014-07-23 07:46:59 +00003211 if (AtomicKind == OMPC_read) {
Alexey Bataev459dec02014-07-24 06:46:57 +00003212 if (!isa<Expr>(Body)) {
3213 Diag(Body->getLocStart(),
Alexey Bataevdea47612014-07-23 07:46:59 +00003214 diag::err_omp_atomic_read_not_expression_statement);
3215 return StmtError();
3216 }
3217 } else if (AtomicKind == OMPC_write) {
Alexey Bataev459dec02014-07-24 06:46:57 +00003218 if (!isa<Expr>(Body)) {
3219 Diag(Body->getLocStart(),
Alexey Bataevdea47612014-07-23 07:46:59 +00003220 diag::err_omp_atomic_write_not_expression_statement);
3221 return StmtError();
3222 }
Alexey Bataev67a4f222014-07-23 10:25:33 +00003223 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev459dec02014-07-24 06:46:57 +00003224 if (!isa<Expr>(Body)) {
3225 Diag(Body->getLocStart(),
Alexey Bataev67a4f222014-07-23 10:25:33 +00003226 diag::err_omp_atomic_update_not_expression_statement)
3227 << (AtomicKind == OMPC_update);
3228 return StmtError();
3229 }
Alexey Bataev459dec02014-07-24 06:46:57 +00003230 } else if (AtomicKind == OMPC_capture) {
3231 if (isa<Expr>(Body) && !isa<BinaryOperator>(Body)) {
3232 Diag(Body->getLocStart(),
3233 diag::err_omp_atomic_capture_not_expression_statement);
3234 return StmtError();
3235 } else if (!isa<Expr>(Body) && !isa<CompoundStmt>(Body)) {
3236 Diag(Body->getLocStart(),
3237 diag::err_omp_atomic_capture_not_compound_statement);
3238 return StmtError();
3239 }
Alexey Bataevdea47612014-07-23 07:46:59 +00003240 }
Alexey Bataev0162e452014-07-22 10:10:35 +00003241
3242 getCurFunction()->setHasBranchProtectedScope();
3243
3244 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3245}
3246
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003247StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
3248 Stmt *AStmt,
3249 SourceLocation StartLoc,
3250 SourceLocation EndLoc) {
3251 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3252
Alexey Bataev13314bf2014-10-09 04:18:56 +00003253 // OpenMP [2.16, Nesting of Regions]
3254 // If specified, a teams construct must be contained within a target
3255 // construct. That target construct must contain no statements or directives
3256 // outside of the teams construct.
3257 if (DSAStack->hasInnerTeamsRegion()) {
3258 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
3259 bool OMPTeamsFound = true;
3260 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
3261 auto I = CS->body_begin();
3262 while (I != CS->body_end()) {
3263 auto OED = dyn_cast<OMPExecutableDirective>(*I);
3264 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
3265 OMPTeamsFound = false;
3266 break;
3267 }
3268 ++I;
3269 }
3270 assert(I != CS->body_end() && "Not found statement");
3271 S = *I;
3272 }
3273 if (!OMPTeamsFound) {
3274 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
3275 Diag(DSAStack->getInnerTeamsRegionLoc(),
3276 diag::note_omp_nested_teams_construct_here);
3277 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
3278 << isa<OMPExecutableDirective>(S);
3279 return StmtError();
3280 }
3281 }
3282
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003283 getCurFunction()->setHasBranchProtectedScope();
3284
3285 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3286}
3287
Alexey Bataev13314bf2014-10-09 04:18:56 +00003288StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
3289 Stmt *AStmt, SourceLocation StartLoc,
3290 SourceLocation EndLoc) {
3291 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3292 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3293 // 1.2.2 OpenMP Language Terminology
3294 // Structured block - An executable statement with a single entry at the
3295 // top and a single exit at the bottom.
3296 // The point of exit cannot be a branch out of the structured block.
3297 // longjmp() and throw() must not violate the entry/exit criteria.
3298 CS->getCapturedDecl()->setNothrow();
3299
3300 getCurFunction()->setHasBranchProtectedScope();
3301
3302 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3303}
3304
Alexey Bataeved09d242014-05-28 05:53:51 +00003305OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003306 SourceLocation StartLoc,
3307 SourceLocation LParenLoc,
3308 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003309 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003310 switch (Kind) {
3311 case OMPC_if:
3312 Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc);
3313 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00003314 case OMPC_final:
3315 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
3316 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00003317 case OMPC_num_threads:
3318 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
3319 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00003320 case OMPC_safelen:
3321 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
3322 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00003323 case OMPC_collapse:
3324 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
3325 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003326 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003327 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003328 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003329 case OMPC_private:
3330 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00003331 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003332 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00003333 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00003334 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00003335 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003336 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003337 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003338 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00003339 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003340 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003341 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003342 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00003343 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003344 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00003345 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00003346 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00003347 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003348 case OMPC_seq_cst:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003349 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003350 llvm_unreachable("Clause is not allowed.");
3351 }
3352 return Res;
3353}
3354
Alexey Bataeved09d242014-05-28 05:53:51 +00003355OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003356 SourceLocation LParenLoc,
3357 SourceLocation EndLoc) {
3358 Expr *ValExpr = Condition;
3359 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
3360 !Condition->isInstantiationDependent() &&
3361 !Condition->containsUnexpandedParameterPack()) {
3362 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00003363 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003364 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003365 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003366
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003367 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003368 }
3369
3370 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc);
3371}
3372
Alexey Bataev3778b602014-07-17 07:32:53 +00003373OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
3374 SourceLocation StartLoc,
3375 SourceLocation LParenLoc,
3376 SourceLocation EndLoc) {
3377 Expr *ValExpr = Condition;
3378 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
3379 !Condition->isInstantiationDependent() &&
3380 !Condition->containsUnexpandedParameterPack()) {
3381 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
3382 Condition->getExprLoc(), Condition);
3383 if (Val.isInvalid())
3384 return nullptr;
3385
3386 ValExpr = Val.get();
3387 }
3388
3389 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
3390}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003391ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
3392 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00003393 if (!Op)
3394 return ExprError();
3395
3396 class IntConvertDiagnoser : public ICEConvertDiagnoser {
3397 public:
3398 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00003399 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00003400 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
3401 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003402 return S.Diag(Loc, diag::err_omp_not_integral) << T;
3403 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003404 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
3405 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003406 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
3407 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003408 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
3409 QualType T,
3410 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003411 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
3412 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003413 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
3414 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003415 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00003416 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00003417 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003418 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
3419 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003420 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
3421 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003422 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
3423 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003424 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00003425 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00003426 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003427 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
3428 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003429 llvm_unreachable("conversion functions are permitted");
3430 }
3431 } ConvertDiagnoser;
3432 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
3433}
3434
3435OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
3436 SourceLocation StartLoc,
3437 SourceLocation LParenLoc,
3438 SourceLocation EndLoc) {
3439 Expr *ValExpr = NumThreads;
3440 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
Alexey Bataev568a8332014-03-06 06:15:19 +00003441 !NumThreads->containsUnexpandedParameterPack()) {
3442 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
3443 ExprResult Val =
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003444 PerformOpenMPImplicitIntegerConversion(NumThreadsLoc, NumThreads);
Alexey Bataev568a8332014-03-06 06:15:19 +00003445 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003446 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00003447
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003448 ValExpr = Val.get();
Alexey Bataev568a8332014-03-06 06:15:19 +00003449
3450 // OpenMP [2.5, Restrictions]
3451 // The num_threads expression must evaluate to a positive integer value.
3452 llvm::APSInt Result;
Alexey Bataeved09d242014-05-28 05:53:51 +00003453 if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() &&
3454 !Result.isStrictlyPositive()) {
Alexey Bataev568a8332014-03-06 06:15:19 +00003455 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
3456 << "num_threads" << NumThreads->getSourceRange();
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003457 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00003458 }
3459 }
3460
Alexey Bataeved09d242014-05-28 05:53:51 +00003461 return new (Context)
3462 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00003463}
3464
Alexey Bataev62c87d22014-03-21 04:51:18 +00003465ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
3466 OpenMPClauseKind CKind) {
3467 if (!E)
3468 return ExprError();
3469 if (E->isValueDependent() || E->isTypeDependent() ||
3470 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00003471 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00003472 llvm::APSInt Result;
3473 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
3474 if (ICE.isInvalid())
3475 return ExprError();
3476 if (!Result.isStrictlyPositive()) {
3477 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
3478 << getOpenMPClauseName(CKind) << E->getSourceRange();
3479 return ExprError();
3480 }
Alexander Musman09184fe2014-09-30 05:29:28 +00003481 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
3482 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
3483 << E->getSourceRange();
3484 return ExprError();
3485 }
Alexey Bataev62c87d22014-03-21 04:51:18 +00003486 return ICE;
3487}
3488
3489OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
3490 SourceLocation LParenLoc,
3491 SourceLocation EndLoc) {
3492 // OpenMP [2.8.1, simd construct, Description]
3493 // The parameter of the safelen clause must be a constant
3494 // positive integer expression.
3495 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
3496 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003497 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00003498 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003499 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00003500}
3501
Alexander Musman64d33f12014-06-04 07:53:32 +00003502OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
3503 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00003504 SourceLocation LParenLoc,
3505 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00003506 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00003507 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00003508 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00003509 // The parameter of the collapse clause must be a constant
3510 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00003511 ExprResult NumForLoopsResult =
3512 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
3513 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00003514 return nullptr;
3515 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00003516 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00003517}
3518
Alexey Bataeved09d242014-05-28 05:53:51 +00003519OMPClause *Sema::ActOnOpenMPSimpleClause(
3520 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
3521 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003522 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003523 switch (Kind) {
3524 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00003525 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00003526 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
3527 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003528 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003529 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00003530 Res = ActOnOpenMPProcBindClause(
3531 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
3532 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003533 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003534 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00003535 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00003536 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00003537 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00003538 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003539 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003540 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003541 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00003542 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00003543 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00003544 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00003545 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00003546 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003547 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003548 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003549 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00003550 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003551 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003552 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003553 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00003554 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003555 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00003556 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00003557 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00003558 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003559 case OMPC_seq_cst:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003560 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003561 llvm_unreachable("Clause is not allowed.");
3562 }
3563 return Res;
3564}
3565
3566OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
3567 SourceLocation KindKwLoc,
3568 SourceLocation StartLoc,
3569 SourceLocation LParenLoc,
3570 SourceLocation EndLoc) {
3571 if (Kind == OMPC_DEFAULT_unknown) {
3572 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00003573 static_assert(OMPC_DEFAULT_unknown > 0,
3574 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00003575 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00003576 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003577 Values += "'";
3578 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
3579 Values += "'";
3580 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00003581 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003582 Values += " or ";
3583 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00003584 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003585 break;
3586 default:
3587 Values += Sep;
3588 break;
3589 }
3590 }
3591 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00003592 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003593 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003594 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00003595 switch (Kind) {
3596 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003597 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003598 break;
3599 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003600 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003601 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003602 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003603 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00003604 break;
3605 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003606 return new (Context)
3607 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003608}
3609
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003610OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
3611 SourceLocation KindKwLoc,
3612 SourceLocation StartLoc,
3613 SourceLocation LParenLoc,
3614 SourceLocation EndLoc) {
3615 if (Kind == OMPC_PROC_BIND_unknown) {
3616 std::string Values;
3617 std::string Sep(", ");
3618 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
3619 Values += "'";
3620 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
3621 Values += "'";
3622 switch (i) {
3623 case OMPC_PROC_BIND_unknown - 2:
3624 Values += " or ";
3625 break;
3626 case OMPC_PROC_BIND_unknown - 1:
3627 break;
3628 default:
3629 Values += Sep;
3630 break;
3631 }
3632 }
3633 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00003634 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003635 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003636 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003637 return new (Context)
3638 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003639}
3640
Alexey Bataev56dafe82014-06-20 07:16:17 +00003641OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
3642 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
3643 SourceLocation StartLoc, SourceLocation LParenLoc,
3644 SourceLocation ArgumentLoc, SourceLocation CommaLoc,
3645 SourceLocation EndLoc) {
3646 OMPClause *Res = nullptr;
3647 switch (Kind) {
3648 case OMPC_schedule:
3649 Res = ActOnOpenMPScheduleClause(
3650 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
3651 LParenLoc, ArgumentLoc, CommaLoc, EndLoc);
3652 break;
3653 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00003654 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003655 case OMPC_num_threads:
3656 case OMPC_safelen:
3657 case OMPC_collapse:
3658 case OMPC_default:
3659 case OMPC_proc_bind:
3660 case OMPC_private:
3661 case OMPC_firstprivate:
3662 case OMPC_lastprivate:
3663 case OMPC_shared:
3664 case OMPC_reduction:
3665 case OMPC_linear:
3666 case OMPC_aligned:
3667 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003668 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003669 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00003670 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003671 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003672 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003673 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00003674 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003675 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00003676 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00003677 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00003678 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003679 case OMPC_seq_cst:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003680 case OMPC_unknown:
3681 llvm_unreachable("Clause is not allowed.");
3682 }
3683 return Res;
3684}
3685
3686OMPClause *Sema::ActOnOpenMPScheduleClause(
3687 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
3688 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
3689 SourceLocation EndLoc) {
3690 if (Kind == OMPC_SCHEDULE_unknown) {
3691 std::string Values;
3692 std::string Sep(", ");
3693 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) {
3694 Values += "'";
3695 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i);
3696 Values += "'";
3697 switch (i) {
3698 case OMPC_SCHEDULE_unknown - 2:
3699 Values += " or ";
3700 break;
3701 case OMPC_SCHEDULE_unknown - 1:
3702 break;
3703 default:
3704 Values += Sep;
3705 break;
3706 }
3707 }
3708 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
3709 << Values << getOpenMPClauseName(OMPC_schedule);
3710 return nullptr;
3711 }
3712 Expr *ValExpr = ChunkSize;
3713 if (ChunkSize) {
3714 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
3715 !ChunkSize->isInstantiationDependent() &&
3716 !ChunkSize->containsUnexpandedParameterPack()) {
3717 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
3718 ExprResult Val =
3719 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
3720 if (Val.isInvalid())
3721 return nullptr;
3722
3723 ValExpr = Val.get();
3724
3725 // OpenMP [2.7.1, Restrictions]
3726 // chunk_size must be a loop invariant integer expression with a positive
3727 // value.
3728 llvm::APSInt Result;
3729 if (ValExpr->isIntegerConstantExpr(Result, Context) &&
3730 Result.isSigned() && !Result.isStrictlyPositive()) {
3731 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
3732 << "schedule" << ChunkSize->getSourceRange();
3733 return nullptr;
3734 }
3735 }
3736 }
3737
3738 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc,
3739 EndLoc, Kind, ValExpr);
3740}
3741
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003742OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
3743 SourceLocation StartLoc,
3744 SourceLocation EndLoc) {
3745 OMPClause *Res = nullptr;
3746 switch (Kind) {
3747 case OMPC_ordered:
3748 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
3749 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00003750 case OMPC_nowait:
3751 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
3752 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003753 case OMPC_untied:
3754 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
3755 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003756 case OMPC_mergeable:
3757 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
3758 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003759 case OMPC_read:
3760 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
3761 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00003762 case OMPC_write:
3763 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
3764 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00003765 case OMPC_update:
3766 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
3767 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00003768 case OMPC_capture:
3769 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
3770 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003771 case OMPC_seq_cst:
3772 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
3773 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003774 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00003775 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003776 case OMPC_num_threads:
3777 case OMPC_safelen:
3778 case OMPC_collapse:
3779 case OMPC_schedule:
3780 case OMPC_private:
3781 case OMPC_firstprivate:
3782 case OMPC_lastprivate:
3783 case OMPC_shared:
3784 case OMPC_reduction:
3785 case OMPC_linear:
3786 case OMPC_aligned:
3787 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003788 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003789 case OMPC_default:
3790 case OMPC_proc_bind:
3791 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00003792 case OMPC_flush:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003793 case OMPC_unknown:
3794 llvm_unreachable("Clause is not allowed.");
3795 }
3796 return Res;
3797}
3798
3799OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
3800 SourceLocation EndLoc) {
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003801 DSAStack->setOrderedRegion();
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003802 return new (Context) OMPOrderedClause(StartLoc, EndLoc);
3803}
3804
Alexey Bataev236070f2014-06-20 11:19:47 +00003805OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
3806 SourceLocation EndLoc) {
3807 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
3808}
3809
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003810OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
3811 SourceLocation EndLoc) {
3812 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
3813}
3814
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003815OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
3816 SourceLocation EndLoc) {
3817 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
3818}
3819
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003820OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
3821 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003822 return new (Context) OMPReadClause(StartLoc, EndLoc);
3823}
3824
Alexey Bataevdea47612014-07-23 07:46:59 +00003825OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
3826 SourceLocation EndLoc) {
3827 return new (Context) OMPWriteClause(StartLoc, EndLoc);
3828}
3829
Alexey Bataev67a4f222014-07-23 10:25:33 +00003830OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
3831 SourceLocation EndLoc) {
3832 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
3833}
3834
Alexey Bataev459dec02014-07-24 06:46:57 +00003835OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
3836 SourceLocation EndLoc) {
3837 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
3838}
3839
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003840OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
3841 SourceLocation EndLoc) {
3842 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
3843}
3844
Alexey Bataevc5e02582014-06-16 07:08:35 +00003845OMPClause *Sema::ActOnOpenMPVarListClause(
3846 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
3847 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
3848 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
3849 const DeclarationNameInfo &ReductionId) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003850 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003851 switch (Kind) {
3852 case OMPC_private:
3853 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
3854 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003855 case OMPC_firstprivate:
3856 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
3857 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00003858 case OMPC_lastprivate:
3859 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
3860 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00003861 case OMPC_shared:
3862 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
3863 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00003864 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00003865 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
3866 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00003867 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00003868 case OMPC_linear:
3869 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
3870 ColonLoc, EndLoc);
3871 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00003872 case OMPC_aligned:
3873 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
3874 ColonLoc, EndLoc);
3875 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003876 case OMPC_copyin:
3877 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
3878 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00003879 case OMPC_copyprivate:
3880 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
3881 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003882 case OMPC_flush:
3883 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
3884 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003885 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00003886 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00003887 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00003888 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00003889 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003890 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003891 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003892 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003893 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00003894 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003895 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003896 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003897 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003898 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00003899 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00003900 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00003901 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003902 case OMPC_seq_cst:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003903 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003904 llvm_unreachable("Clause is not allowed.");
3905 }
3906 return Res;
3907}
3908
3909OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
3910 SourceLocation StartLoc,
3911 SourceLocation LParenLoc,
3912 SourceLocation EndLoc) {
3913 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00003914 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00003915 for (auto &RefExpr : VarList) {
3916 assert(RefExpr && "NULL expr in OpenMP private clause.");
3917 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003918 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00003919 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00003920 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003921 continue;
3922 }
3923
Alexey Bataeved09d242014-05-28 05:53:51 +00003924 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003925 // OpenMP [2.1, C/C++]
3926 // A list item is a variable name.
3927 // OpenMP [2.9.3.3, Restrictions, p.1]
3928 // A variable that is part of another variable (as an array or
3929 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00003930 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003931 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003932 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003933 continue;
3934 }
3935 Decl *D = DE->getDecl();
3936 VarDecl *VD = cast<VarDecl>(D);
3937
3938 QualType Type = VD->getType();
3939 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
3940 // It will be analyzed later.
3941 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00003942 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003943 continue;
3944 }
3945
3946 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
3947 // A variable that appears in a private clause must not have an incomplete
3948 // type or a reference type.
3949 if (RequireCompleteType(ELoc, Type,
3950 diag::err_omp_private_incomplete_type)) {
3951 continue;
3952 }
3953 if (Type->isReferenceType()) {
3954 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00003955 << getOpenMPClauseName(OMPC_private) << Type;
3956 bool IsDecl =
3957 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3958 Diag(VD->getLocation(),
3959 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3960 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003961 continue;
3962 }
3963
3964 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
3965 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00003966 // clause requires an accessible, unambiguous default constructor for the
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003967 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00003968 while (Type->isArrayType()) {
3969 Type = cast<ArrayType>(Type.getTypePtr())->getElementType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003970 }
3971
Alexey Bataev758e55e2013-09-06 18:03:48 +00003972 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
3973 // in a Construct]
3974 // Variables with the predetermined data-sharing attributes may not be
3975 // listed in data-sharing attributes clauses, except for the cases
3976 // listed below. For these exceptions only, listing a predetermined
3977 // variable in a data-sharing attribute clause is allowed and overrides
3978 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003979 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003980 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003981 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
3982 << getOpenMPClauseName(OMPC_private);
Alexey Bataev7ff55242014-06-19 09:13:45 +00003983 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003984 continue;
3985 }
3986
Alexey Bataev03b340a2014-10-21 03:16:40 +00003987 // Generate helper private variable and initialize it with the default
3988 // value. The address of the original variable is replaced by the address of
3989 // the new private variable in CodeGen. This new variable is not added to
3990 // IdResolver, so the code in the OpenMP region uses original variable for
3991 // proper diagnostics.
3992 auto VDPrivate =
3993 VarDecl::Create(Context, CurContext, DE->getLocStart(),
3994 DE->getExprLoc(), VD->getIdentifier(), VD->getType(),
3995 VD->getTypeSourceInfo(), /*S*/ SC_Auto);
3996 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto*/ false);
3997 if (VDPrivate->isInvalidDecl())
3998 continue;
3999 CurContext->addDecl(VDPrivate);
4000 auto VDPrivateRefExpr = DeclRefExpr::Create(
4001 Context, /*QualifierLoc*/ NestedNameSpecifierLoc(),
4002 /*TemplateKWLoc*/ SourceLocation(), VDPrivate,
4003 /*isEnclosingLocal*/ false, /*NameLoc*/ SourceLocation(), DE->getType(),
4004 /*VK*/ VK_LValue);
4005
Alexey Bataev758e55e2013-09-06 18:03:48 +00004006 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004007 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00004008 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004009 }
4010
Alexey Bataeved09d242014-05-28 05:53:51 +00004011 if (Vars.empty())
4012 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004013
Alexey Bataev03b340a2014-10-21 03:16:40 +00004014 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
4015 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004016}
4017
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004018namespace {
4019class DiagsUninitializedSeveretyRAII {
4020private:
4021 DiagnosticsEngine &Diags;
4022 SourceLocation SavedLoc;
4023 bool IsIgnored;
4024
4025public:
4026 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
4027 bool IsIgnored)
4028 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
4029 if (!IsIgnored) {
4030 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
4031 /*Map*/ diag::Severity::Ignored, Loc);
4032 }
4033 }
4034 ~DiagsUninitializedSeveretyRAII() {
4035 if (!IsIgnored)
4036 Diags.popMappings(SavedLoc);
4037 }
4038};
4039}
4040
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004041OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
4042 SourceLocation StartLoc,
4043 SourceLocation LParenLoc,
4044 SourceLocation EndLoc) {
4045 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004046 SmallVector<Expr *, 8> PrivateCopies;
4047 SmallVector<Expr *, 8> Inits;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004048 bool IsImplicitClause =
4049 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
4050 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
4051
Alexey Bataeved09d242014-05-28 05:53:51 +00004052 for (auto &RefExpr : VarList) {
4053 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
4054 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004055 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00004056 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004057 PrivateCopies.push_back(nullptr);
4058 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004059 continue;
4060 }
4061
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004062 SourceLocation ELoc =
4063 IsImplicitClause ? ImplicitClauseLoc : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004064 // OpenMP [2.1, C/C++]
4065 // A list item is a variable name.
4066 // OpenMP [2.9.3.3, Restrictions, p.1]
4067 // A variable that is part of another variable (as an array or
4068 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00004069 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004070 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00004071 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004072 continue;
4073 }
4074 Decl *D = DE->getDecl();
4075 VarDecl *VD = cast<VarDecl>(D);
4076
4077 QualType Type = VD->getType();
4078 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
4079 // It will be analyzed later.
4080 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004081 PrivateCopies.push_back(nullptr);
4082 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004083 continue;
4084 }
4085
4086 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
4087 // A variable that appears in a private clause must not have an incomplete
4088 // type or a reference type.
4089 if (RequireCompleteType(ELoc, Type,
4090 diag::err_omp_firstprivate_incomplete_type)) {
4091 continue;
4092 }
4093 if (Type->isReferenceType()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004094 if (IsImplicitClause) {
4095 Diag(ImplicitClauseLoc,
4096 diag::err_omp_task_predetermined_firstprivate_ref_type_arg)
4097 << Type;
4098 Diag(RefExpr->getExprLoc(), diag::note_used_here);
4099 } else {
4100 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
4101 << getOpenMPClauseName(OMPC_firstprivate) << Type;
4102 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004103 bool IsDecl =
4104 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4105 Diag(VD->getLocation(),
4106 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4107 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004108 continue;
4109 }
4110
4111 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
4112 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00004113 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004114 // class type.
4115 Type = Context.getBaseElementType(Type);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004116
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004117 // If an implicit firstprivate variable found it was checked already.
4118 if (!IsImplicitClause) {
4119 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004120 Type = Type.getNonReferenceType().getCanonicalType();
4121 bool IsConstant = Type.isConstant(Context);
4122 Type = Context.getBaseElementType(Type);
4123 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
4124 // A list item that specifies a given variable may not appear in more
4125 // than one clause on the same directive, except that a variable may be
4126 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004127 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00004128 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004129 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00004130 << getOpenMPClauseName(DVar.CKind)
4131 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004132 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004133 continue;
4134 }
4135
4136 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
4137 // in a Construct]
4138 // Variables with the predetermined data-sharing attributes may not be
4139 // listed in data-sharing attributes clauses, except for the cases
4140 // listed below. For these exceptions only, listing a predetermined
4141 // variable in a data-sharing attribute clause is allowed and overrides
4142 // the variable's predetermined data-sharing attributes.
4143 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
4144 // in a Construct, C/C++, p.2]
4145 // Variables with const-qualified type having no mutable member may be
4146 // listed in a firstprivate clause, even if they are static data members.
4147 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
4148 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
4149 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00004150 << getOpenMPClauseName(DVar.CKind)
4151 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004152 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004153 continue;
4154 }
4155
Alexey Bataevf29276e2014-06-18 04:14:57 +00004156 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004157 // OpenMP [2.9.3.4, Restrictions, p.2]
4158 // A list item that is private within a parallel region must not appear
4159 // in a firstprivate clause on a worksharing construct if any of the
4160 // worksharing regions arising from the worksharing construct ever bind
4161 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00004162 if (isOpenMPWorksharingDirective(CurrDir) &&
4163 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004164 DVar = DSAStack->getImplicitDSA(VD, true);
4165 if (DVar.CKind != OMPC_shared &&
4166 (isOpenMPParallelDirective(DVar.DKind) ||
4167 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00004168 Diag(ELoc, diag::err_omp_required_access)
4169 << getOpenMPClauseName(OMPC_firstprivate)
4170 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004171 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004172 continue;
4173 }
4174 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004175 // OpenMP [2.9.3.4, Restrictions, p.3]
4176 // A list item that appears in a reduction clause of a parallel construct
4177 // must not appear in a firstprivate clause on a worksharing or task
4178 // construct if any of the worksharing or task regions arising from the
4179 // worksharing or task construct ever bind to any of the parallel regions
4180 // arising from the parallel construct.
4181 // OpenMP [2.9.3.4, Restrictions, p.4]
4182 // A list item that appears in a reduction clause in worksharing
4183 // construct must not appear in a firstprivate clause in a task construct
4184 // encountered during execution of any of the worksharing regions arising
4185 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004186 if (CurrDir == OMPD_task) {
4187 DVar =
4188 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
4189 [](OpenMPDirectiveKind K) -> bool {
4190 return isOpenMPParallelDirective(K) ||
4191 isOpenMPWorksharingDirective(K);
4192 },
4193 false);
4194 if (DVar.CKind == OMPC_reduction &&
4195 (isOpenMPParallelDirective(DVar.DKind) ||
4196 isOpenMPWorksharingDirective(DVar.DKind))) {
4197 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
4198 << getOpenMPDirectiveName(DVar.DKind);
4199 ReportOriginalDSA(*this, DSAStack, VD, DVar);
4200 continue;
4201 }
4202 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004203 }
4204
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004205 Type = Type.getUnqualifiedType();
4206 auto VDPrivate = VarDecl::Create(Context, CurContext, DE->getLocStart(),
4207 ELoc, VD->getIdentifier(), VD->getType(),
4208 VD->getTypeSourceInfo(), /*S*/ SC_Auto);
4209 // Generate helper private variable and initialize it with the value of the
4210 // original variable. The address of the original variable is replaced by
4211 // the address of the new private variable in the CodeGen. This new variable
4212 // is not added to IdResolver, so the code in the OpenMP region uses
4213 // original variable for proper diagnostics and variable capturing.
4214 Expr *VDInitRefExpr = nullptr;
4215 // For arrays generate initializer for single element and replace it by the
4216 // original array element in CodeGen.
4217 if (DE->getType()->isArrayType()) {
4218 auto VDInit = VarDecl::Create(Context, CurContext, DE->getLocStart(),
4219 ELoc, VD->getIdentifier(), Type,
4220 VD->getTypeSourceInfo(), /*S*/ SC_Auto);
4221 CurContext->addHiddenDecl(VDInit);
4222 VDInitRefExpr = DeclRefExpr::Create(
4223 Context, /*QualifierLoc*/ NestedNameSpecifierLoc(),
4224 /*TemplateKWLoc*/ SourceLocation(), VDInit,
4225 /*isEnclosingLocal*/ false, ELoc, Type,
4226 /*VK*/ VK_LValue);
4227 VDInit->setIsUsed();
4228 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
4229 InitializedEntity Entity = InitializedEntity::InitializeVariable(VDInit);
4230 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
4231
4232 InitializationSequence InitSeq(*this, Entity, Kind, Init);
4233 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
4234 if (Result.isInvalid())
4235 VDPrivate->setInvalidDecl();
4236 else
4237 VDPrivate->setInit(Result.getAs<Expr>());
4238 } else {
4239 AddInitializerToDecl(VDPrivate, DefaultLvalueConversion(DE).get(),
4240 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4241 }
4242 if (VDPrivate->isInvalidDecl()) {
4243 if (IsImplicitClause) {
4244 Diag(DE->getExprLoc(),
4245 diag::note_omp_task_predetermined_firstprivate_here);
4246 }
4247 continue;
4248 }
4249 CurContext->addDecl(VDPrivate);
4250 auto VDPrivateRefExpr = DeclRefExpr::Create(
4251 Context, /*QualifierLoc*/ NestedNameSpecifierLoc(),
4252 /*TemplateKWLoc*/ SourceLocation(), VDPrivate,
4253 /*isEnclosingLocal*/ false, DE->getLocStart(), DE->getType(),
4254 /*VK*/ VK_LValue);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004255 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
4256 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004257 PrivateCopies.push_back(VDPrivateRefExpr);
4258 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004259 }
4260
Alexey Bataeved09d242014-05-28 05:53:51 +00004261 if (Vars.empty())
4262 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004263
4264 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004265 Vars, PrivateCopies, Inits);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004266}
4267
Alexander Musman1bb328c2014-06-04 13:06:39 +00004268OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
4269 SourceLocation StartLoc,
4270 SourceLocation LParenLoc,
4271 SourceLocation EndLoc) {
4272 SmallVector<Expr *, 8> Vars;
4273 for (auto &RefExpr : VarList) {
4274 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
4275 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
4276 // It will be analyzed later.
4277 Vars.push_back(RefExpr);
4278 continue;
4279 }
4280
4281 SourceLocation ELoc = RefExpr->getExprLoc();
4282 // OpenMP [2.1, C/C++]
4283 // A list item is a variable name.
4284 // OpenMP [2.14.3.5, Restrictions, p.1]
4285 // A variable that is part of another variable (as an array or structure
4286 // element) cannot appear in a lastprivate clause.
4287 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
4288 if (!DE || !isa<VarDecl>(DE->getDecl())) {
4289 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
4290 continue;
4291 }
4292 Decl *D = DE->getDecl();
4293 VarDecl *VD = cast<VarDecl>(D);
4294
4295 QualType Type = VD->getType();
4296 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
4297 // It will be analyzed later.
4298 Vars.push_back(DE);
4299 continue;
4300 }
4301
4302 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
4303 // A variable that appears in a lastprivate clause must not have an
4304 // incomplete type or a reference type.
4305 if (RequireCompleteType(ELoc, Type,
4306 diag::err_omp_lastprivate_incomplete_type)) {
4307 continue;
4308 }
4309 if (Type->isReferenceType()) {
4310 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
4311 << getOpenMPClauseName(OMPC_lastprivate) << Type;
4312 bool IsDecl =
4313 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4314 Diag(VD->getLocation(),
4315 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4316 << VD;
4317 continue;
4318 }
4319
4320 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4321 // in a Construct]
4322 // Variables with the predetermined data-sharing attributes may not be
4323 // listed in data-sharing attributes clauses, except for the cases
4324 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004325 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00004326 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
4327 DVar.CKind != OMPC_firstprivate &&
4328 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
4329 Diag(ELoc, diag::err_omp_wrong_dsa)
4330 << getOpenMPClauseName(DVar.CKind)
4331 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004332 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00004333 continue;
4334 }
4335
Alexey Bataevf29276e2014-06-18 04:14:57 +00004336 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
4337 // OpenMP [2.14.3.5, Restrictions, p.2]
4338 // A list item that is private within a parallel region, or that appears in
4339 // the reduction clause of a parallel construct, must not appear in a
4340 // lastprivate clause on a worksharing construct if any of the corresponding
4341 // worksharing regions ever binds to any of the corresponding parallel
4342 // regions.
Alexey Bataev549210e2014-06-24 04:39:47 +00004343 if (isOpenMPWorksharingDirective(CurrDir) &&
4344 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004345 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004346 if (DVar.CKind != OMPC_shared) {
4347 Diag(ELoc, diag::err_omp_required_access)
4348 << getOpenMPClauseName(OMPC_lastprivate)
4349 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004350 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004351 continue;
4352 }
4353 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00004354 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00004355 // A variable of class type (or array thereof) that appears in a
4356 // lastprivate clause requires an accessible, unambiguous default
4357 // constructor for the class type, unless the list item is also specified
4358 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00004359 // A variable of class type (or array thereof) that appears in a
4360 // lastprivate clause requires an accessible, unambiguous copy assignment
4361 // operator for the class type.
4362 while (Type.getNonReferenceType()->isArrayType())
4363 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
4364 ->getElementType();
4365 CXXRecordDecl *RD = getLangOpts().CPlusPlus
4366 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
4367 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00004368 // FIXME This code must be replaced by actual copying and destructing of the
4369 // lastprivate variable.
Alexander Musman1bb328c2014-06-04 13:06:39 +00004370 if (RD) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00004371 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
4372 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00004373 if (MD) {
4374 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
4375 MD->isDeleted()) {
4376 Diag(ELoc, diag::err_omp_required_method)
4377 << getOpenMPClauseName(OMPC_lastprivate) << 2;
4378 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4379 VarDecl::DeclarationOnly;
4380 Diag(VD->getLocation(),
4381 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4382 << VD;
4383 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4384 continue;
4385 }
4386 MarkFunctionReferenced(ELoc, MD);
4387 DiagnoseUseOfDecl(MD, ELoc);
Alexander Musman1bb328c2014-06-04 13:06:39 +00004388 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00004389
4390 CXXDestructorDecl *DD = RD->getDestructor();
4391 if (DD) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00004392 PartialDiagnostic PD =
4393 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexander Musman1bb328c2014-06-04 13:06:39 +00004394 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
4395 DD->isDeleted()) {
4396 Diag(ELoc, diag::err_omp_required_method)
4397 << getOpenMPClauseName(OMPC_lastprivate) << 4;
4398 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4399 VarDecl::DeclarationOnly;
4400 Diag(VD->getLocation(),
4401 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4402 << VD;
4403 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4404 continue;
4405 }
4406 MarkFunctionReferenced(ELoc, DD);
4407 DiagnoseUseOfDecl(DD, ELoc);
4408 }
4409 }
4410
Alexey Bataevf29276e2014-06-18 04:14:57 +00004411 if (DVar.CKind != OMPC_firstprivate)
4412 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00004413 Vars.push_back(DE);
4414 }
4415
4416 if (Vars.empty())
4417 return nullptr;
4418
4419 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
4420 Vars);
4421}
4422
Alexey Bataev758e55e2013-09-06 18:03:48 +00004423OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
4424 SourceLocation StartLoc,
4425 SourceLocation LParenLoc,
4426 SourceLocation EndLoc) {
4427 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00004428 for (auto &RefExpr : VarList) {
4429 assert(RefExpr && "NULL expr in OpenMP shared clause.");
4430 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00004431 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00004432 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004433 continue;
4434 }
4435
Alexey Bataeved09d242014-05-28 05:53:51 +00004436 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00004437 // OpenMP [2.1, C/C++]
4438 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00004439 // OpenMP [2.14.3.2, Restrictions, p.1]
4440 // A variable that is part of another variable (as an array or structure
4441 // element) cannot appear in a shared unless it is a static data member
4442 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00004443 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004444 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00004445 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00004446 continue;
4447 }
4448 Decl *D = DE->getDecl();
4449 VarDecl *VD = cast<VarDecl>(D);
4450
4451 QualType Type = VD->getType();
4452 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
4453 // It will be analyzed later.
4454 Vars.push_back(DE);
4455 continue;
4456 }
4457
4458 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
4459 // in a Construct]
4460 // Variables with the predetermined data-sharing attributes may not be
4461 // listed in data-sharing attributes clauses, except for the cases
4462 // listed below. For these exceptions only, listing a predetermined
4463 // variable in a data-sharing attribute clause is allowed and overrides
4464 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004465 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00004466 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
4467 DVar.RefExpr) {
4468 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
4469 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004470 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004471 continue;
4472 }
4473
4474 DSAStack->addDSA(VD, DE, OMPC_shared);
4475 Vars.push_back(DE);
4476 }
4477
Alexey Bataeved09d242014-05-28 05:53:51 +00004478 if (Vars.empty())
4479 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00004480
4481 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
4482}
4483
Alexey Bataevc5e02582014-06-16 07:08:35 +00004484namespace {
4485class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
4486 DSAStackTy *Stack;
4487
4488public:
4489 bool VisitDeclRefExpr(DeclRefExpr *E) {
4490 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004491 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00004492 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
4493 return false;
4494 if (DVar.CKind != OMPC_unknown)
4495 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00004496 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004497 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004498 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00004499 return true;
4500 return false;
4501 }
4502 return false;
4503 }
4504 bool VisitStmt(Stmt *S) {
4505 for (auto Child : S->children()) {
4506 if (Child && Visit(Child))
4507 return true;
4508 }
4509 return false;
4510 }
Alexey Bataev23b69422014-06-18 07:08:49 +00004511 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00004512};
Alexey Bataev23b69422014-06-18 07:08:49 +00004513} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00004514
4515OMPClause *Sema::ActOnOpenMPReductionClause(
4516 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
4517 SourceLocation ColonLoc, SourceLocation EndLoc,
4518 CXXScopeSpec &ReductionIdScopeSpec,
4519 const DeclarationNameInfo &ReductionId) {
4520 // TODO: Allow scope specification search when 'declare reduction' is
4521 // supported.
4522 assert(ReductionIdScopeSpec.isEmpty() &&
4523 "No support for scoped reduction identifiers yet.");
4524
4525 auto DN = ReductionId.getName();
4526 auto OOK = DN.getCXXOverloadedOperator();
4527 BinaryOperatorKind BOK = BO_Comma;
4528
4529 // OpenMP [2.14.3.6, reduction clause]
4530 // C
4531 // reduction-identifier is either an identifier or one of the following
4532 // operators: +, -, *, &, |, ^, && and ||
4533 // C++
4534 // reduction-identifier is either an id-expression or one of the following
4535 // operators: +, -, *, &, |, ^, && and ||
4536 // FIXME: Only 'min' and 'max' identifiers are supported for now.
4537 switch (OOK) {
4538 case OO_Plus:
4539 case OO_Minus:
4540 BOK = BO_AddAssign;
4541 break;
4542 case OO_Star:
4543 BOK = BO_MulAssign;
4544 break;
4545 case OO_Amp:
4546 BOK = BO_AndAssign;
4547 break;
4548 case OO_Pipe:
4549 BOK = BO_OrAssign;
4550 break;
4551 case OO_Caret:
4552 BOK = BO_XorAssign;
4553 break;
4554 case OO_AmpAmp:
4555 BOK = BO_LAnd;
4556 break;
4557 case OO_PipePipe:
4558 BOK = BO_LOr;
4559 break;
4560 default:
4561 if (auto II = DN.getAsIdentifierInfo()) {
4562 if (II->isStr("max"))
4563 BOK = BO_GT;
4564 else if (II->isStr("min"))
4565 BOK = BO_LT;
4566 }
4567 break;
4568 }
4569 SourceRange ReductionIdRange;
4570 if (ReductionIdScopeSpec.isValid()) {
4571 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
4572 }
4573 ReductionIdRange.setEnd(ReductionId.getEndLoc());
4574 if (BOK == BO_Comma) {
4575 // Not allowed reduction identifier is found.
4576 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
4577 << ReductionIdRange;
4578 return nullptr;
4579 }
4580
4581 SmallVector<Expr *, 8> Vars;
4582 for (auto RefExpr : VarList) {
4583 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
4584 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
4585 // It will be analyzed later.
4586 Vars.push_back(RefExpr);
4587 continue;
4588 }
4589
4590 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
4591 RefExpr->isInstantiationDependent() ||
4592 RefExpr->containsUnexpandedParameterPack()) {
4593 // It will be analyzed later.
4594 Vars.push_back(RefExpr);
4595 continue;
4596 }
4597
4598 auto ELoc = RefExpr->getExprLoc();
4599 auto ERange = RefExpr->getSourceRange();
4600 // OpenMP [2.1, C/C++]
4601 // A list item is a variable or array section, subject to the restrictions
4602 // specified in Section 2.4 on page 42 and in each of the sections
4603 // describing clauses and directives for which a list appears.
4604 // OpenMP [2.14.3.3, Restrictions, p.1]
4605 // A variable that is part of another variable (as an array or
4606 // structure element) cannot appear in a private clause.
4607 auto DE = dyn_cast<DeclRefExpr>(RefExpr);
4608 if (!DE || !isa<VarDecl>(DE->getDecl())) {
4609 Diag(ELoc, diag::err_omp_expected_var_name) << ERange;
4610 continue;
4611 }
4612 auto D = DE->getDecl();
4613 auto VD = cast<VarDecl>(D);
4614 auto Type = VD->getType();
4615 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
4616 // A variable that appears in a private clause must not have an incomplete
4617 // type or a reference type.
4618 if (RequireCompleteType(ELoc, Type,
4619 diag::err_omp_reduction_incomplete_type))
4620 continue;
4621 // OpenMP [2.14.3.6, reduction clause, Restrictions]
4622 // Arrays may not appear in a reduction clause.
4623 if (Type.getNonReferenceType()->isArrayType()) {
4624 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
4625 bool IsDecl =
4626 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4627 Diag(VD->getLocation(),
4628 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4629 << VD;
4630 continue;
4631 }
4632 // OpenMP [2.14.3.6, reduction clause, Restrictions]
4633 // A list item that appears in a reduction clause must not be
4634 // const-qualified.
4635 if (Type.getNonReferenceType().isConstant(Context)) {
4636 Diag(ELoc, diag::err_omp_const_variable)
4637 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
4638 bool IsDecl =
4639 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4640 Diag(VD->getLocation(),
4641 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4642 << VD;
4643 continue;
4644 }
4645 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
4646 // If a list-item is a reference type then it must bind to the same object
4647 // for all threads of the team.
4648 VarDecl *VDDef = VD->getDefinition();
4649 if (Type->isReferenceType() && VDDef) {
4650 DSARefChecker Check(DSAStack);
4651 if (Check.Visit(VDDef->getInit())) {
4652 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
4653 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
4654 continue;
4655 }
4656 }
4657 // OpenMP [2.14.3.6, reduction clause, Restrictions]
4658 // The type of a list item that appears in a reduction clause must be valid
4659 // for the reduction-identifier. For a max or min reduction in C, the type
4660 // of the list item must be an allowed arithmetic data type: char, int,
4661 // float, double, or _Bool, possibly modified with long, short, signed, or
4662 // unsigned. For a max or min reduction in C++, the type of the list item
4663 // must be an allowed arithmetic data type: char, wchar_t, int, float,
4664 // double, or bool, possibly modified with long, short, signed, or unsigned.
4665 if ((BOK == BO_GT || BOK == BO_LT) &&
4666 !(Type->isScalarType() ||
4667 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
4668 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
4669 << getLangOpts().CPlusPlus;
4670 bool IsDecl =
4671 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4672 Diag(VD->getLocation(),
4673 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4674 << VD;
4675 continue;
4676 }
4677 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
4678 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
4679 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
4680 bool IsDecl =
4681 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4682 Diag(VD->getLocation(),
4683 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4684 << VD;
4685 continue;
4686 }
4687 bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
4688 getDiagnostics().setSuppressAllDiagnostics(true);
4689 ExprResult ReductionOp =
4690 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
4691 RefExpr, RefExpr);
4692 getDiagnostics().setSuppressAllDiagnostics(Suppress);
4693 if (ReductionOp.isInvalid()) {
4694 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
Alexey Bataev23b69422014-06-18 07:08:49 +00004695 << ReductionIdRange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00004696 bool IsDecl =
4697 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4698 Diag(VD->getLocation(),
4699 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4700 << VD;
4701 continue;
4702 }
4703
4704 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4705 // in a Construct]
4706 // Variables with the predetermined data-sharing attributes may not be
4707 // listed in data-sharing attributes clauses, except for the cases
4708 // listed below. For these exceptions only, listing a predetermined
4709 // variable in a data-sharing attribute clause is allowed and overrides
4710 // the variable's predetermined data-sharing attributes.
4711 // OpenMP [2.14.3.6, Restrictions, p.3]
4712 // Any number of reduction clauses can be specified on the directive,
4713 // but a list item can appear only once in the reduction clauses for that
4714 // directive.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004715 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00004716 if (DVar.CKind == OMPC_reduction) {
4717 Diag(ELoc, diag::err_omp_once_referenced)
4718 << getOpenMPClauseName(OMPC_reduction);
4719 if (DVar.RefExpr) {
4720 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
4721 }
4722 } else if (DVar.CKind != OMPC_unknown) {
4723 Diag(ELoc, diag::err_omp_wrong_dsa)
4724 << getOpenMPClauseName(DVar.CKind)
4725 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004726 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00004727 continue;
4728 }
4729
4730 // OpenMP [2.14.3.6, Restrictions, p.1]
4731 // A list item that appears in a reduction clause of a worksharing
4732 // construct must be shared in the parallel regions to which any of the
4733 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf29276e2014-06-18 04:14:57 +00004734 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataev549210e2014-06-24 04:39:47 +00004735 if (isOpenMPWorksharingDirective(CurrDir) &&
4736 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004737 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004738 if (DVar.CKind != OMPC_shared) {
4739 Diag(ELoc, diag::err_omp_required_access)
4740 << getOpenMPClauseName(OMPC_reduction)
4741 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004742 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004743 continue;
4744 }
4745 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00004746
4747 CXXRecordDecl *RD = getLangOpts().CPlusPlus
4748 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
4749 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00004750 // FIXME This code must be replaced by actual constructing/destructing of
4751 // the reduction variable.
Alexey Bataevc5e02582014-06-16 07:08:35 +00004752 if (RD) {
4753 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
4754 PartialDiagnostic PD =
4755 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataev23b69422014-06-18 07:08:49 +00004756 if (!CD ||
4757 CheckConstructorAccess(ELoc, CD,
4758 InitializedEntity::InitializeTemporary(Type),
4759 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevc5e02582014-06-16 07:08:35 +00004760 CD->isDeleted()) {
4761 Diag(ELoc, diag::err_omp_required_method)
4762 << getOpenMPClauseName(OMPC_reduction) << 0;
4763 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4764 VarDecl::DeclarationOnly;
4765 Diag(VD->getLocation(),
4766 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4767 << VD;
4768 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4769 continue;
4770 }
4771 MarkFunctionReferenced(ELoc, CD);
4772 DiagnoseUseOfDecl(CD, ELoc);
4773
4774 CXXDestructorDecl *DD = RD->getDestructor();
4775 if (DD) {
4776 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
4777 DD->isDeleted()) {
4778 Diag(ELoc, diag::err_omp_required_method)
4779 << getOpenMPClauseName(OMPC_reduction) << 4;
4780 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4781 VarDecl::DeclarationOnly;
4782 Diag(VD->getLocation(),
4783 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4784 << VD;
4785 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4786 continue;
4787 }
4788 MarkFunctionReferenced(ELoc, DD);
4789 DiagnoseUseOfDecl(DD, ELoc);
4790 }
4791 }
4792
4793 DSAStack->addDSA(VD, DE, OMPC_reduction);
4794 Vars.push_back(DE);
4795 }
4796
4797 if (Vars.empty())
4798 return nullptr;
4799
4800 return OMPReductionClause::Create(
4801 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
4802 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId);
4803}
4804
Alexander Musman8dba6642014-04-22 13:09:42 +00004805OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step,
4806 SourceLocation StartLoc,
4807 SourceLocation LParenLoc,
4808 SourceLocation ColonLoc,
4809 SourceLocation EndLoc) {
4810 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00004811 for (auto &RefExpr : VarList) {
4812 assert(RefExpr && "NULL expr in OpenMP linear clause.");
4813 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00004814 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00004815 Vars.push_back(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00004816 continue;
4817 }
4818
4819 // OpenMP [2.14.3.7, linear clause]
4820 // A list item that appears in a linear clause is subject to the private
4821 // clause semantics described in Section 2.14.3.3 on page 159 except as
4822 // noted. In addition, the value of the new list item on each iteration
4823 // of the associated loop(s) corresponds to the value of the original
4824 // list item before entering the construct plus the logical number of
4825 // the iteration times linear-step.
4826
Alexey Bataeved09d242014-05-28 05:53:51 +00004827 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00004828 // OpenMP [2.1, C/C++]
4829 // A list item is a variable name.
4830 // OpenMP [2.14.3.3, Restrictions, p.1]
4831 // A variable that is part of another variable (as an array or
4832 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00004833 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00004834 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00004835 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00004836 continue;
4837 }
4838
4839 VarDecl *VD = cast<VarDecl>(DE->getDecl());
4840
4841 // OpenMP [2.14.3.7, linear clause]
4842 // A list-item cannot appear in more than one linear clause.
4843 // A list-item that appears in a linear clause cannot appear in any
4844 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004845 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00004846 if (DVar.RefExpr) {
4847 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
4848 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004849 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00004850 continue;
4851 }
4852
4853 QualType QType = VD->getType();
4854 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
4855 // It will be analyzed later.
4856 Vars.push_back(DE);
4857 continue;
4858 }
4859
4860 // A variable must not have an incomplete type or a reference type.
4861 if (RequireCompleteType(ELoc, QType,
4862 diag::err_omp_linear_incomplete_type)) {
4863 continue;
4864 }
4865 if (QType->isReferenceType()) {
4866 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
4867 << getOpenMPClauseName(OMPC_linear) << QType;
4868 bool IsDecl =
4869 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4870 Diag(VD->getLocation(),
4871 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4872 << VD;
4873 continue;
4874 }
4875
4876 // A list item must not be const-qualified.
4877 if (QType.isConstant(Context)) {
4878 Diag(ELoc, diag::err_omp_const_variable)
4879 << getOpenMPClauseName(OMPC_linear);
4880 bool IsDecl =
4881 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4882 Diag(VD->getLocation(),
4883 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4884 << VD;
4885 continue;
4886 }
4887
4888 // A list item must be of integral or pointer type.
4889 QType = QType.getUnqualifiedType().getCanonicalType();
4890 const Type *Ty = QType.getTypePtrOrNull();
4891 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
4892 !Ty->isPointerType())) {
4893 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
4894 bool IsDecl =
4895 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4896 Diag(VD->getLocation(),
4897 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4898 << VD;
4899 continue;
4900 }
4901
4902 DSAStack->addDSA(VD, DE, OMPC_linear);
4903 Vars.push_back(DE);
4904 }
4905
4906 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004907 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00004908
4909 Expr *StepExpr = Step;
4910 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
4911 !Step->isInstantiationDependent() &&
4912 !Step->containsUnexpandedParameterPack()) {
4913 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004914 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00004915 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004916 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00004917 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00004918
4919 // Warn about zero linear step (it would be probably better specified as
4920 // making corresponding variables 'const').
4921 llvm::APSInt Result;
4922 if (StepExpr->isIntegerConstantExpr(Result, Context) &&
4923 !Result.isNegative() && !Result.isStrictlyPositive())
4924 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
4925 << (Vars.size() > 1);
4926 }
4927
4928 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc,
4929 Vars, StepExpr);
4930}
4931
Alexander Musmanf0d76e72014-05-29 14:36:25 +00004932OMPClause *Sema::ActOnOpenMPAlignedClause(
4933 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
4934 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
4935
4936 SmallVector<Expr *, 8> Vars;
4937 for (auto &RefExpr : VarList) {
4938 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
4939 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
4940 // It will be analyzed later.
4941 Vars.push_back(RefExpr);
4942 continue;
4943 }
4944
4945 SourceLocation ELoc = RefExpr->getExprLoc();
4946 // OpenMP [2.1, C/C++]
4947 // A list item is a variable name.
4948 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
4949 if (!DE || !isa<VarDecl>(DE->getDecl())) {
4950 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
4951 continue;
4952 }
4953
4954 VarDecl *VD = cast<VarDecl>(DE->getDecl());
4955
4956 // OpenMP [2.8.1, simd construct, Restrictions]
4957 // The type of list items appearing in the aligned clause must be
4958 // array, pointer, reference to array, or reference to pointer.
4959 QualType QType = DE->getType()
4960 .getNonReferenceType()
4961 .getUnqualifiedType()
4962 .getCanonicalType();
4963 const Type *Ty = QType.getTypePtrOrNull();
4964 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
4965 !Ty->isPointerType())) {
4966 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
4967 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
4968 bool IsDecl =
4969 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4970 Diag(VD->getLocation(),
4971 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4972 << VD;
4973 continue;
4974 }
4975
4976 // OpenMP [2.8.1, simd construct, Restrictions]
4977 // A list-item cannot appear in more than one aligned clause.
4978 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
4979 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
4980 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
4981 << getOpenMPClauseName(OMPC_aligned);
4982 continue;
4983 }
4984
4985 Vars.push_back(DE);
4986 }
4987
4988 // OpenMP [2.8.1, simd construct, Description]
4989 // The parameter of the aligned clause, alignment, must be a constant
4990 // positive integer expression.
4991 // If no optional parameter is specified, implementation-defined default
4992 // alignments for SIMD instructions on the target platforms are assumed.
4993 if (Alignment != nullptr) {
4994 ExprResult AlignResult =
4995 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
4996 if (AlignResult.isInvalid())
4997 return nullptr;
4998 Alignment = AlignResult.get();
4999 }
5000 if (Vars.empty())
5001 return nullptr;
5002
5003 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
5004 EndLoc, Vars, Alignment);
5005}
5006
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005007OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
5008 SourceLocation StartLoc,
5009 SourceLocation LParenLoc,
5010 SourceLocation EndLoc) {
5011 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00005012 for (auto &RefExpr : VarList) {
5013 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
5014 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005015 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00005016 Vars.push_back(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005017 continue;
5018 }
5019
Alexey Bataeved09d242014-05-28 05:53:51 +00005020 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005021 // OpenMP [2.1, C/C++]
5022 // A list item is a variable name.
5023 // OpenMP [2.14.4.1, Restrictions, p.1]
5024 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00005025 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005026 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005027 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005028 continue;
5029 }
5030
5031 Decl *D = DE->getDecl();
5032 VarDecl *VD = cast<VarDecl>(D);
5033
5034 QualType Type = VD->getType();
5035 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5036 // It will be analyzed later.
5037 Vars.push_back(DE);
5038 continue;
5039 }
5040
5041 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
5042 // A list item that appears in a copyin clause must be threadprivate.
5043 if (!DSAStack->isThreadPrivate(VD)) {
5044 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00005045 << getOpenMPClauseName(OMPC_copyin)
5046 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005047 continue;
5048 }
5049
5050 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
5051 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00005052 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005053 // operator for the class type.
5054 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005055 CXXRecordDecl *RD =
5056 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00005057 // FIXME This code must be replaced by actual assignment of the
5058 // threadprivate variable.
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005059 if (RD) {
5060 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
5061 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00005062 if (MD) {
5063 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
5064 MD->isDeleted()) {
5065 Diag(ELoc, diag::err_omp_required_method)
5066 << getOpenMPClauseName(OMPC_copyin) << 2;
5067 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
5068 VarDecl::DeclarationOnly;
5069 Diag(VD->getLocation(),
5070 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5071 << VD;
5072 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
5073 continue;
5074 }
5075 MarkFunctionReferenced(ELoc, MD);
5076 DiagnoseUseOfDecl(MD, ELoc);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005077 }
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005078 }
5079
5080 DSAStack->addDSA(VD, DE, OMPC_copyin);
5081 Vars.push_back(DE);
5082 }
5083
Alexey Bataeved09d242014-05-28 05:53:51 +00005084 if (Vars.empty())
5085 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005086
5087 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
5088}
5089
Alexey Bataevbae9a792014-06-27 10:37:06 +00005090OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
5091 SourceLocation StartLoc,
5092 SourceLocation LParenLoc,
5093 SourceLocation EndLoc) {
5094 SmallVector<Expr *, 8> Vars;
5095 for (auto &RefExpr : VarList) {
5096 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
5097 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
5098 // It will be analyzed later.
5099 Vars.push_back(RefExpr);
5100 continue;
5101 }
5102
5103 SourceLocation ELoc = RefExpr->getExprLoc();
5104 // OpenMP [2.1, C/C++]
5105 // A list item is a variable name.
5106 // OpenMP [2.14.4.1, Restrictions, p.1]
5107 // A list item that appears in a copyin clause must be threadprivate.
5108 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
5109 if (!DE || !isa<VarDecl>(DE->getDecl())) {
5110 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
5111 continue;
5112 }
5113
5114 Decl *D = DE->getDecl();
5115 VarDecl *VD = cast<VarDecl>(D);
5116
5117 QualType Type = VD->getType();
5118 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5119 // It will be analyzed later.
5120 Vars.push_back(DE);
5121 continue;
5122 }
5123
5124 // OpenMP [2.14.4.2, Restrictions, p.2]
5125 // A list item that appears in a copyprivate clause may not appear in a
5126 // private or firstprivate clause on the single construct.
5127 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005128 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00005129 if (DVar.CKind != OMPC_copyprivate && DVar.CKind != OMPC_unknown &&
5130 !(DVar.CKind == OMPC_private && !DVar.RefExpr)) {
5131 Diag(ELoc, diag::err_omp_wrong_dsa)
5132 << getOpenMPClauseName(DVar.CKind)
5133 << getOpenMPClauseName(OMPC_copyprivate);
5134 ReportOriginalDSA(*this, DSAStack, VD, DVar);
5135 continue;
5136 }
5137
5138 // OpenMP [2.11.4.2, Restrictions, p.1]
5139 // All list items that appear in a copyprivate clause must be either
5140 // threadprivate or private in the enclosing context.
5141 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005142 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00005143 if (DVar.CKind == OMPC_shared) {
5144 Diag(ELoc, diag::err_omp_required_access)
5145 << getOpenMPClauseName(OMPC_copyprivate)
5146 << "threadprivate or private in the enclosing context";
5147 ReportOriginalDSA(*this, DSAStack, VD, DVar);
5148 continue;
5149 }
5150 }
5151 }
5152
5153 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
5154 // A variable of class type (or array thereof) that appears in a
5155 // copyin clause requires an accessible, unambiguous copy assignment
5156 // operator for the class type.
5157 Type = Context.getBaseElementType(Type);
5158 CXXRecordDecl *RD =
5159 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
5160 // FIXME This code must be replaced by actual assignment of the
5161 // threadprivate variable.
5162 if (RD) {
5163 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
5164 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
5165 if (MD) {
5166 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
5167 MD->isDeleted()) {
5168 Diag(ELoc, diag::err_omp_required_method)
5169 << getOpenMPClauseName(OMPC_copyprivate) << 2;
5170 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
5171 VarDecl::DeclarationOnly;
5172 Diag(VD->getLocation(),
5173 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5174 << VD;
5175 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
5176 continue;
5177 }
5178 MarkFunctionReferenced(ELoc, MD);
5179 DiagnoseUseOfDecl(MD, ELoc);
5180 }
5181 }
5182
5183 // No need to mark vars as copyprivate, they are already threadprivate or
5184 // implicitly private.
5185 Vars.push_back(DE);
5186 }
5187
5188 if (Vars.empty())
5189 return nullptr;
5190
5191 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
5192}
5193
Alexey Bataev6125da92014-07-21 11:26:11 +00005194OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
5195 SourceLocation StartLoc,
5196 SourceLocation LParenLoc,
5197 SourceLocation EndLoc) {
5198 if (VarList.empty())
5199 return nullptr;
5200
5201 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
5202}
Alexey Bataevdea47612014-07-23 07:46:59 +00005203