blob: 23f5f13e8fdca8199ad05652c2271c6e759748f2 [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()) {
649 VarDeclFilterCCC Validator(*this);
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000650 if (TypoCorrection Corrected =
651 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr, Validator,
652 CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +0000653 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000654 PDiag(Lookup.empty()
655 ? diag::err_undeclared_var_use_suggest
656 : diag::err_omp_expected_var_arg_suggest)
657 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +0000658 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000659 } else {
Richard Smithf9b15102013-08-17 00:46:16 +0000660 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
661 : diag::err_omp_expected_var_arg)
662 << Id.getName();
663 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000664 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000665 } else {
666 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000667 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000668 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
669 return ExprError();
670 }
671 }
672 Lookup.suppressDiagnostics();
673
674 // OpenMP [2.9.2, Syntax, C/C++]
675 // Variables must be file-scope, namespace-scope, or static block-scope.
676 if (!VD->hasGlobalStorage()) {
677 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000678 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
679 bool IsDecl =
680 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000681 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +0000682 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
683 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000684 return ExprError();
685 }
686
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000687 VarDecl *CanonicalVD = VD->getCanonicalDecl();
688 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000689 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
690 // A threadprivate directive for file-scope variables must appear outside
691 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000692 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
693 !getCurLexicalContext()->isTranslationUnit()) {
694 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000695 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
696 bool IsDecl =
697 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
698 Diag(VD->getLocation(),
699 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
700 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000701 return ExprError();
702 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000703 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
704 // A threadprivate directive for static class member variables must appear
705 // in the class definition, in the same scope in which the member
706 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000707 if (CanonicalVD->isStaticDataMember() &&
708 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
709 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000710 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
711 bool IsDecl =
712 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
713 Diag(VD->getLocation(),
714 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
715 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000716 return ExprError();
717 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000718 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
719 // A threadprivate directive for namespace-scope variables must appear
720 // outside any definition or declaration other than the namespace
721 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000722 if (CanonicalVD->getDeclContext()->isNamespace() &&
723 (!getCurLexicalContext()->isFileContext() ||
724 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
725 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000726 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
727 bool IsDecl =
728 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
729 Diag(VD->getLocation(),
730 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
731 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000732 return ExprError();
733 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000734 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
735 // A threadprivate directive for static block-scope variables must appear
736 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000737 if (CanonicalVD->isStaticLocal() && CurScope &&
738 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000739 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000740 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
741 bool IsDecl =
742 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
743 Diag(VD->getLocation(),
744 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
745 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000746 return ExprError();
747 }
748
749 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
750 // A threadprivate directive must lexically precede all references to any
751 // of the variables in its list.
752 if (VD->isUsed()) {
753 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +0000754 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000755 return ExprError();
756 }
757
758 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataevd178ad42014-03-07 08:03:37 +0000759 ExprResult DE = BuildDeclRefExpr(VD, ExprType, VK_LValue, Id.getLoc());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000760 return DE;
761}
762
Alexey Bataeved09d242014-05-28 05:53:51 +0000763Sema::DeclGroupPtrTy
764Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
765 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000766 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000767 CurContext->addDecl(D);
768 return DeclGroupPtrTy::make(DeclGroupRef(D));
769 }
770 return DeclGroupPtrTy();
771}
772
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000773namespace {
774class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
775 Sema &SemaRef;
776
777public:
778 bool VisitDeclRefExpr(const DeclRefExpr *E) {
779 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
780 if (VD->hasLocalStorage()) {
781 SemaRef.Diag(E->getLocStart(),
782 diag::err_omp_local_var_in_threadprivate_init)
783 << E->getSourceRange();
784 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
785 << VD << VD->getSourceRange();
786 return true;
787 }
788 }
789 return false;
790 }
791 bool VisitStmt(const Stmt *S) {
792 for (auto Child : S->children()) {
793 if (Child && Visit(Child))
794 return true;
795 }
796 return false;
797 }
Alexey Bataev23b69422014-06-18 07:08:49 +0000798 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000799};
800} // namespace
801
Alexey Bataeved09d242014-05-28 05:53:51 +0000802OMPThreadPrivateDecl *
803Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000804 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +0000805 for (auto &RefExpr : VarList) {
806 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000807 VarDecl *VD = cast<VarDecl>(DE->getDecl());
808 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +0000809
810 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
811 // A threadprivate variable must not have an incomplete type.
812 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000813 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000814 continue;
815 }
816
817 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
818 // A threadprivate variable must not have a reference type.
819 if (VD->getType()->isReferenceType()) {
820 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000821 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
822 bool IsDecl =
823 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
824 Diag(VD->getLocation(),
825 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
826 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000827 continue;
828 }
829
Richard Smithfd3834f2013-04-13 02:43:54 +0000830 // Check if this is a TLS variable.
831 if (VD->getTLSKind()) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000832 Diag(ILoc, diag::err_omp_var_thread_local) << VD;
Alexey Bataeved09d242014-05-28 05:53:51 +0000833 bool IsDecl =
834 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
835 Diag(VD->getLocation(),
836 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
837 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000838 continue;
839 }
840
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000841 // Check if initial value of threadprivate variable reference variable with
842 // local storage (it is not supported by runtime).
843 if (auto Init = VD->getAnyInitializer()) {
844 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000845 if (Checker.Visit(Init))
846 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000847 }
848
Alexey Bataeved09d242014-05-28 05:53:51 +0000849 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +0000850 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataeva769e072013-03-22 06:34:35 +0000851 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000852 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000853 if (!Vars.empty()) {
854 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
855 Vars);
856 D->setAccess(AS_public);
857 }
858 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +0000859}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000860
Alexey Bataev7ff55242014-06-19 09:13:45 +0000861static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
862 const VarDecl *VD, DSAStackTy::DSAVarData DVar,
863 bool IsLoopIterVar = false) {
864 if (DVar.RefExpr) {
865 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
866 << getOpenMPClauseName(DVar.CKind);
867 return;
868 }
869 enum {
870 PDSA_StaticMemberShared,
871 PDSA_StaticLocalVarShared,
872 PDSA_LoopIterVarPrivate,
873 PDSA_LoopIterVarLinear,
874 PDSA_LoopIterVarLastprivate,
875 PDSA_ConstVarShared,
876 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000877 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000878 PDSA_LocalVarPrivate,
879 PDSA_Implicit
880 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000881 bool ReportHint = false;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000882 auto ReportLoc = VD->getLocation();
Alexey Bataev7ff55242014-06-19 09:13:45 +0000883 if (IsLoopIterVar) {
884 if (DVar.CKind == OMPC_private)
885 Reason = PDSA_LoopIterVarPrivate;
886 else if (DVar.CKind == OMPC_lastprivate)
887 Reason = PDSA_LoopIterVarLastprivate;
888 else
889 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000890 } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) {
891 Reason = PDSA_TaskVarFirstprivate;
892 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000893 } else if (VD->isStaticLocal())
894 Reason = PDSA_StaticLocalVarShared;
895 else if (VD->isStaticDataMember())
896 Reason = PDSA_StaticMemberShared;
897 else if (VD->isFileVarDecl())
898 Reason = PDSA_GlobalVarShared;
899 else if (VD->getType().isConstant(SemaRef.getASTContext()))
900 Reason = PDSA_ConstVarShared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000901 else if (VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +0000902 ReportHint = true;
903 Reason = PDSA_LocalVarPrivate;
904 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000905 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000906 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +0000907 << Reason << ReportHint
908 << getOpenMPDirectiveName(Stack->getCurrentDirective());
909 } else if (DVar.ImplicitDSALoc.isValid()) {
910 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
911 << getOpenMPClauseName(DVar.CKind);
912 }
Alexey Bataev7ff55242014-06-19 09:13:45 +0000913}
914
Alexey Bataev758e55e2013-09-06 18:03:48 +0000915namespace {
916class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
917 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000918 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000919 bool ErrorFound;
920 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000921 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000922 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +0000923
Alexey Bataev758e55e2013-09-06 18:03:48 +0000924public:
925 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000926 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000927 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +0000928 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
929 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000930
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000931 auto DVar = Stack->getTopDSA(VD, false);
932 // Check if the variable has explicit DSA set and stop analysis if it so.
933 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000934
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000935 auto ELoc = E->getExprLoc();
936 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000937 // The default(none) clause requires that each variable that is referenced
938 // in the construct, and does not have a predetermined data-sharing
939 // attribute, must have its data-sharing attribute explicitly determined
940 // by being listed in a data-sharing attribute clause.
941 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000942 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +0000943 VarsWithInheritedDSA.count(VD) == 0) {
944 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000945 return;
946 }
947
948 // OpenMP [2.9.3.6, Restrictions, p.2]
949 // A list item that appears in a reduction clause of the innermost
950 // enclosing worksharing or parallel construct may not be accessed in an
951 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000952 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000953 [](OpenMPDirectiveKind K) -> bool {
954 return isOpenMPParallelDirective(K) ||
Alexey Bataev13314bf2014-10-09 04:18:56 +0000955 isOpenMPWorksharingDirective(K) ||
956 isOpenMPTeamsDirective(K);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000957 },
958 false);
Alexey Bataevc5e02582014-06-16 07:08:35 +0000959 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
960 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000961 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
962 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +0000963 return;
964 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000965
966 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000967 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000968 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000969 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000970 }
971 }
972 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000973 for (auto *C : S->clauses()) {
974 // Skip analysis of arguments of implicitly defined firstprivate clause
975 // for task directives.
976 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
977 for (auto *CC : C->children()) {
978 if (CC)
979 Visit(CC);
980 }
981 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000982 }
983 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000984 for (auto *C : S->children()) {
985 if (C && !isa<OMPExecutableDirective>(C))
986 Visit(C);
987 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000988 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000989
990 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000991 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev4acb8592014-07-07 13:01:15 +0000992 llvm::DenseMap<VarDecl *, Expr *> &getVarsWithInheritedDSA() {
993 return VarsWithInheritedDSA;
994 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000995
Alexey Bataev7ff55242014-06-19 09:13:45 +0000996 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
997 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000998};
Alexey Bataeved09d242014-05-28 05:53:51 +0000999} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001000
Alexey Bataevbae9a792014-06-27 10:37:06 +00001001void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001002 switch (DKind) {
1003 case OMPD_parallel: {
1004 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1005 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001006 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001007 std::make_pair(".global_tid.", KmpInt32PtrTy),
1008 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1009 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001010 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001011 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1012 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001013 break;
1014 }
1015 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001016 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001017 std::make_pair(StringRef(), QualType()) // __context with shared vars
1018 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001019 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1020 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001021 break;
1022 }
1023 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001024 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001025 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001026 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001027 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1028 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001029 break;
1030 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001031 case OMPD_for_simd: {
1032 Sema::CapturedParamNameType Params[] = {
1033 std::make_pair(StringRef(), QualType()) // __context with shared vars
1034 };
1035 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1036 Params);
1037 break;
1038 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001039 case OMPD_sections: {
1040 Sema::CapturedParamNameType Params[] = {
1041 std::make_pair(StringRef(), QualType()) // __context with shared vars
1042 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001043 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1044 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001045 break;
1046 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001047 case OMPD_section: {
1048 Sema::CapturedParamNameType Params[] = {
1049 std::make_pair(StringRef(), QualType()) // __context with shared vars
1050 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001051 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1052 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001053 break;
1054 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001055 case OMPD_single: {
1056 Sema::CapturedParamNameType Params[] = {
1057 std::make_pair(StringRef(), QualType()) // __context with shared vars
1058 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001059 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1060 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001061 break;
1062 }
Alexander Musman80c22892014-07-17 08:54:58 +00001063 case OMPD_master: {
1064 Sema::CapturedParamNameType Params[] = {
1065 std::make_pair(StringRef(), QualType()) // __context with shared vars
1066 };
1067 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1068 Params);
1069 break;
1070 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001071 case OMPD_critical: {
1072 Sema::CapturedParamNameType Params[] = {
1073 std::make_pair(StringRef(), QualType()) // __context with shared vars
1074 };
1075 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1076 Params);
1077 break;
1078 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001079 case OMPD_parallel_for: {
1080 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1081 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1082 Sema::CapturedParamNameType Params[] = {
1083 std::make_pair(".global_tid.", KmpInt32PtrTy),
1084 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1085 std::make_pair(StringRef(), QualType()) // __context with shared vars
1086 };
1087 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1088 Params);
1089 break;
1090 }
Alexander Musmane4e893b2014-09-23 09:33:00 +00001091 case OMPD_parallel_for_simd: {
1092 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1093 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1094 Sema::CapturedParamNameType Params[] = {
1095 std::make_pair(".global_tid.", KmpInt32PtrTy),
1096 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1097 std::make_pair(StringRef(), QualType()) // __context with shared vars
1098 };
1099 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1100 Params);
1101 break;
1102 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001103 case OMPD_parallel_sections: {
1104 Sema::CapturedParamNameType Params[] = {
1105 std::make_pair(StringRef(), QualType()) // __context with shared vars
1106 };
1107 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1108 Params);
1109 break;
1110 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001111 case OMPD_task: {
1112 Sema::CapturedParamNameType Params[] = {
1113 std::make_pair(StringRef(), QualType()) // __context with shared vars
1114 };
1115 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1116 Params);
1117 break;
1118 }
Alexey Bataev68446b72014-07-18 07:47:19 +00001119 case OMPD_taskyield: {
1120 Sema::CapturedParamNameType Params[] = {
1121 std::make_pair(StringRef(), QualType()) // __context with shared vars
1122 };
1123 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1124 Params);
1125 break;
1126 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001127 case OMPD_barrier: {
1128 Sema::CapturedParamNameType Params[] = {
1129 std::make_pair(StringRef(), QualType()) // __context with shared vars
1130 };
1131 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1132 Params);
1133 break;
1134 }
Alexey Bataev2df347a2014-07-18 10:17:07 +00001135 case OMPD_taskwait: {
1136 Sema::CapturedParamNameType Params[] = {
1137 std::make_pair(StringRef(), QualType()) // __context with shared vars
1138 };
1139 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1140 Params);
1141 break;
1142 }
Alexey Bataev6125da92014-07-21 11:26:11 +00001143 case OMPD_flush: {
1144 Sema::CapturedParamNameType Params[] = {
1145 std::make_pair(StringRef(), QualType()) // __context with shared vars
1146 };
1147 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1148 Params);
1149 break;
1150 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001151 case OMPD_ordered: {
1152 Sema::CapturedParamNameType Params[] = {
1153 std::make_pair(StringRef(), QualType()) // __context with shared vars
1154 };
1155 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1156 Params);
1157 break;
1158 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001159 case OMPD_atomic: {
1160 Sema::CapturedParamNameType Params[] = {
1161 std::make_pair(StringRef(), QualType()) // __context with shared vars
1162 };
1163 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1164 Params);
1165 break;
1166 }
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001167 case OMPD_target: {
1168 Sema::CapturedParamNameType Params[] = {
1169 std::make_pair(StringRef(), QualType()) // __context with shared vars
1170 };
1171 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1172 Params);
1173 break;
1174 }
Alexey Bataev13314bf2014-10-09 04:18:56 +00001175 case OMPD_teams: {
1176 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1177 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1178 Sema::CapturedParamNameType Params[] = {
1179 std::make_pair(".global_tid.", KmpInt32PtrTy),
1180 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1181 std::make_pair(StringRef(), QualType()) // __context with shared vars
1182 };
1183 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1184 Params);
1185 break;
1186 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001187 case OMPD_threadprivate:
Alexey Bataev9959db52014-05-06 10:08:46 +00001188 llvm_unreachable("OpenMP Directive is not allowed");
1189 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001190 llvm_unreachable("Unknown OpenMP directive");
1191 }
1192}
1193
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001194static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1195 OpenMPDirectiveKind CurrentRegion,
1196 const DeclarationNameInfo &CurrentName,
1197 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001198 // Allowed nesting of constructs
1199 // +------------------+-----------------+------------------------------------+
1200 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1201 // +------------------+-----------------+------------------------------------+
1202 // | parallel | parallel | * |
1203 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001204 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001205 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001206 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001207 // | parallel | simd | * |
1208 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001209 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001210 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001211 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001212 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001213 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001214 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001215 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001216 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001217 // | parallel | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001218 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001219 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001220 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001221 // | parallel | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001222 // | parallel | teams | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001223 // +------------------+-----------------+------------------------------------+
1224 // | for | parallel | * |
1225 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001226 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001227 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001228 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001229 // | for | simd | * |
1230 // | for | sections | + |
1231 // | for | section | + |
1232 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001233 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001234 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001235 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001236 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001237 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001238 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001239 // | for | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001240 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001241 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001242 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001243 // | for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001244 // | for | teams | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001245 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001246 // | master | parallel | * |
1247 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001248 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001249 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001250 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001251 // | master | simd | * |
1252 // | master | sections | + |
1253 // | master | section | + |
1254 // | master | single | + |
1255 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001256 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001257 // | master |parallel sections| * |
1258 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001259 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001260 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001261 // | master | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001262 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001263 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001264 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001265 // | master | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001266 // | master | teams | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001267 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001268 // | critical | parallel | * |
1269 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001270 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001271 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001272 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001273 // | critical | simd | * |
1274 // | critical | sections | + |
1275 // | critical | section | + |
1276 // | critical | single | + |
1277 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001278 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001279 // | critical |parallel sections| * |
1280 // | critical | task | * |
1281 // | critical | taskyield | * |
1282 // | critical | barrier | + |
1283 // | critical | taskwait | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001284 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001285 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001286 // | critical | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001287 // | critical | teams | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001288 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001289 // | simd | parallel | |
1290 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001291 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00001292 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001293 // | simd | critical | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001294 // | simd | simd | |
1295 // | simd | sections | |
1296 // | simd | section | |
1297 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001298 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001299 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001300 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001301 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00001302 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001303 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001304 // | simd | taskwait | |
Alexey Bataev6125da92014-07-21 11:26:11 +00001305 // | simd | flush | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001306 // | simd | ordered | |
Alexey Bataev0162e452014-07-22 10:10:35 +00001307 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001308 // | simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001309 // | simd | teams | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001310 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00001311 // | for simd | parallel | |
1312 // | for simd | for | |
1313 // | for simd | for simd | |
1314 // | for simd | master | |
1315 // | for simd | critical | |
1316 // | for simd | simd | |
1317 // | for simd | sections | |
1318 // | for simd | section | |
1319 // | for simd | single | |
1320 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001321 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001322 // | for simd |parallel sections| |
1323 // | for simd | task | |
1324 // | for simd | taskyield | |
1325 // | for simd | barrier | |
1326 // | for simd | taskwait | |
1327 // | for simd | flush | |
1328 // | for simd | ordered | |
1329 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001330 // | for simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001331 // | for simd | teams | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001332 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00001333 // | parallel for simd| parallel | |
1334 // | parallel for simd| for | |
1335 // | parallel for simd| for simd | |
1336 // | parallel for simd| master | |
1337 // | parallel for simd| critical | |
1338 // | parallel for simd| simd | |
1339 // | parallel for simd| sections | |
1340 // | parallel for simd| section | |
1341 // | parallel for simd| single | |
1342 // | parallel for simd| parallel for | |
1343 // | parallel for simd|parallel for simd| |
1344 // | parallel for simd|parallel sections| |
1345 // | parallel for simd| task | |
1346 // | parallel for simd| taskyield | |
1347 // | parallel for simd| barrier | |
1348 // | parallel for simd| taskwait | |
1349 // | parallel for simd| flush | |
1350 // | parallel for simd| ordered | |
1351 // | parallel for simd| atomic | |
1352 // | parallel for simd| target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001353 // | parallel for simd| teams | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001354 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001355 // | sections | parallel | * |
1356 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001357 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001358 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001359 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001360 // | sections | simd | * |
1361 // | sections | sections | + |
1362 // | sections | section | * |
1363 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001364 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001365 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001366 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001367 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001368 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001369 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001370 // | sections | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001371 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001372 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001373 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001374 // | sections | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001375 // | sections | teams | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001376 // +------------------+-----------------+------------------------------------+
1377 // | section | parallel | * |
1378 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001379 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001380 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001381 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001382 // | section | simd | * |
1383 // | section | sections | + |
1384 // | section | section | + |
1385 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001386 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001387 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001388 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001389 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001390 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001391 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001392 // | section | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001393 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001394 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001395 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001396 // | section | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001397 // | section | teams | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001398 // +------------------+-----------------+------------------------------------+
1399 // | single | parallel | * |
1400 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001401 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001402 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001403 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001404 // | single | simd | * |
1405 // | single | sections | + |
1406 // | single | section | + |
1407 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001408 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001409 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001410 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001411 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001412 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001413 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001414 // | single | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001415 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001416 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001417 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001418 // | single | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001419 // | single | teams | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001420 // +------------------+-----------------+------------------------------------+
1421 // | parallel for | parallel | * |
1422 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001423 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001424 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001425 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001426 // | parallel for | simd | * |
1427 // | parallel for | sections | + |
1428 // | parallel for | section | + |
1429 // | parallel for | single | + |
1430 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001431 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001432 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001433 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001434 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001435 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001436 // | parallel for | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001437 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001438 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001439 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001440 // | parallel for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001441 // | parallel for | teams | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001442 // +------------------+-----------------+------------------------------------+
1443 // | parallel sections| parallel | * |
1444 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001445 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001446 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001447 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001448 // | parallel sections| simd | * |
1449 // | parallel sections| sections | + |
1450 // | parallel sections| section | * |
1451 // | parallel sections| single | + |
1452 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001453 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001454 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001455 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001456 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001457 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001458 // | parallel sections| taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001459 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001460 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001461 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001462 // | parallel sections| target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001463 // | parallel sections| teams | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001464 // +------------------+-----------------+------------------------------------+
1465 // | task | parallel | * |
1466 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001467 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001468 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001469 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001470 // | task | simd | * |
1471 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001472 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001473 // | task | single | + |
1474 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001475 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001476 // | task |parallel sections| * |
1477 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001478 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001479 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001480 // | task | taskwait | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001481 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001482 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001483 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001484 // | task | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001485 // | task | teams | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001486 // +------------------+-----------------+------------------------------------+
1487 // | ordered | parallel | * |
1488 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001489 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001490 // | ordered | master | * |
1491 // | ordered | critical | * |
1492 // | ordered | simd | * |
1493 // | ordered | sections | + |
1494 // | ordered | section | + |
1495 // | ordered | single | + |
1496 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001497 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001498 // | ordered |parallel sections| * |
1499 // | ordered | task | * |
1500 // | ordered | taskyield | * |
1501 // | ordered | barrier | + |
1502 // | ordered | taskwait | * |
1503 // | ordered | flush | * |
1504 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001505 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001506 // | ordered | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001507 // | ordered | teams | + |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001508 // +------------------+-----------------+------------------------------------+
1509 // | atomic | parallel | |
1510 // | atomic | for | |
1511 // | atomic | for simd | |
1512 // | atomic | master | |
1513 // | atomic | critical | |
1514 // | atomic | simd | |
1515 // | atomic | sections | |
1516 // | atomic | section | |
1517 // | atomic | single | |
1518 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001519 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001520 // | atomic |parallel sections| |
1521 // | atomic | task | |
1522 // | atomic | taskyield | |
1523 // | atomic | barrier | |
1524 // | atomic | taskwait | |
1525 // | atomic | flush | |
1526 // | atomic | ordered | |
1527 // | atomic | atomic | |
1528 // | atomic | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001529 // | atomic | teams | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001530 // +------------------+-----------------+------------------------------------+
1531 // | target | parallel | * |
1532 // | target | for | * |
1533 // | target | for simd | * |
1534 // | target | master | * |
1535 // | target | critical | * |
1536 // | target | simd | * |
1537 // | target | sections | * |
1538 // | target | section | * |
1539 // | target | single | * |
1540 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001541 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001542 // | target |parallel sections| * |
1543 // | target | task | * |
1544 // | target | taskyield | * |
1545 // | target | barrier | * |
1546 // | target | taskwait | * |
1547 // | target | flush | * |
1548 // | target | ordered | * |
1549 // | target | atomic | * |
1550 // | target | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001551 // | target | teams | * |
1552 // +------------------+-----------------+------------------------------------+
1553 // | teams | parallel | * |
1554 // | teams | for | + |
1555 // | teams | for simd | + |
1556 // | teams | master | + |
1557 // | teams | critical | + |
1558 // | teams | simd | + |
1559 // | teams | sections | + |
1560 // | teams | section | + |
1561 // | teams | single | + |
1562 // | teams | parallel for | * |
1563 // | teams |parallel for simd| * |
1564 // | teams |parallel sections| * |
1565 // | teams | task | + |
1566 // | teams | taskyield | + |
1567 // | teams | barrier | + |
1568 // | teams | taskwait | + |
1569 // | teams | flush | + |
1570 // | teams | ordered | + |
1571 // | teams | atomic | + |
1572 // | teams | target | + |
1573 // | teams | teams | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001574 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00001575 if (Stack->getCurScope()) {
1576 auto ParentRegion = Stack->getParentDirective();
1577 bool NestingProhibited = false;
1578 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001579 enum {
1580 NoRecommend,
1581 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00001582 ShouldBeInOrderedRegion,
1583 ShouldBeInTargetRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001584 } Recommend = NoRecommend;
Alexey Bataev549210e2014-06-24 04:39:47 +00001585 if (isOpenMPSimdDirective(ParentRegion)) {
1586 // OpenMP [2.16, Nesting of Regions]
1587 // OpenMP constructs may not be nested inside a simd region.
1588 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
1589 return true;
1590 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001591 if (ParentRegion == OMPD_atomic) {
1592 // OpenMP [2.16, Nesting of Regions]
1593 // OpenMP constructs may not be nested inside an atomic region.
1594 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
1595 return true;
1596 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001597 if (CurrentRegion == OMPD_section) {
1598 // OpenMP [2.7.2, sections Construct, Restrictions]
1599 // Orphaned section directives are prohibited. That is, the section
1600 // directives must appear within the sections construct and must not be
1601 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001602 if (ParentRegion != OMPD_sections &&
1603 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001604 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
1605 << (ParentRegion != OMPD_unknown)
1606 << getOpenMPDirectiveName(ParentRegion);
1607 return true;
1608 }
1609 return false;
1610 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001611 // Allow some constructs to be orphaned (they could be used in functions,
1612 // called from OpenMP regions with the required preconditions).
1613 if (ParentRegion == OMPD_unknown)
1614 return false;
Alexander Musman80c22892014-07-17 08:54:58 +00001615 if (CurrentRegion == OMPD_master) {
1616 // OpenMP [2.16, Nesting of Regions]
1617 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00001618 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00001619 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
1620 ParentRegion == OMPD_task;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001621 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
1622 // OpenMP [2.16, Nesting of Regions]
1623 // A critical region may not be nested (closely or otherwise) inside a
1624 // critical region with the same name. Note that this restriction is not
1625 // sufficient to prevent deadlock.
1626 SourceLocation PreviousCriticalLoc;
1627 bool DeadLock =
1628 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
1629 OpenMPDirectiveKind K,
1630 const DeclarationNameInfo &DNI,
1631 SourceLocation Loc)
1632 ->bool {
1633 if (K == OMPD_critical &&
1634 DNI.getName() == CurrentName.getName()) {
1635 PreviousCriticalLoc = Loc;
1636 return true;
1637 } else
1638 return false;
1639 },
1640 false /* skip top directive */);
1641 if (DeadLock) {
1642 SemaRef.Diag(StartLoc,
1643 diag::err_omp_prohibited_region_critical_same_name)
1644 << CurrentName.getName();
1645 if (PreviousCriticalLoc.isValid())
1646 SemaRef.Diag(PreviousCriticalLoc,
1647 diag::note_omp_previous_critical_region);
1648 return true;
1649 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001650 } else if (CurrentRegion == OMPD_barrier) {
1651 // OpenMP [2.16, Nesting of Regions]
1652 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00001653 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001654 NestingProhibited =
1655 isOpenMPWorksharingDirective(ParentRegion) ||
1656 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1657 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00001658 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00001659 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00001660 // OpenMP [2.16, Nesting of Regions]
1661 // A worksharing region may not be closely nested inside a worksharing,
1662 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001663 NestingProhibited =
Alexander Musmanf82886e2014-09-18 05:12:34 +00001664 isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001665 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1666 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
1667 Recommend = ShouldBeInParallelRegion;
1668 } else if (CurrentRegion == OMPD_ordered) {
1669 // OpenMP [2.16, Nesting of Regions]
1670 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00001671 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001672 // An ordered region must be closely nested inside a loop region (or
1673 // parallel loop region) with an ordered clause.
1674 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00001675 ParentRegion == OMPD_task ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001676 !Stack->isParentOrderedRegion();
1677 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00001678 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
1679 // OpenMP [2.16, Nesting of Regions]
1680 // If specified, a teams construct must be contained within a target
1681 // construct.
1682 NestingProhibited = ParentRegion != OMPD_target;
1683 Recommend = ShouldBeInTargetRegion;
1684 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
1685 }
1686 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
1687 // OpenMP [2.16, Nesting of Regions]
1688 // distribute, parallel, parallel sections, parallel workshare, and the
1689 // parallel loop and parallel loop SIMD constructs are the only OpenMP
1690 // constructs that can be closely nested in the teams region.
1691 // TODO: add distribute directive.
1692 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion);
1693 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00001694 }
1695 if (NestingProhibited) {
1696 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001697 << CloseNesting << getOpenMPDirectiveName(ParentRegion) << Recommend
1698 << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00001699 return true;
1700 }
1701 }
1702 return false;
1703}
1704
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001705StmtResult Sema::ActOnOpenMPExecutableDirective(OpenMPDirectiveKind Kind,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001706 const DeclarationNameInfo &DirName,
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001707 ArrayRef<OMPClause *> Clauses,
1708 Stmt *AStmt,
1709 SourceLocation StartLoc,
1710 SourceLocation EndLoc) {
1711 StmtResult Res = StmtError();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001712 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00001713 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001714
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001715 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev68446b72014-07-18 07:47:19 +00001716 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001717 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00001718 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00001719 if (AStmt) {
1720 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
1721
1722 // Check default data sharing attributes for referenced variables.
1723 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
1724 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
1725 if (DSAChecker.isErrorFound())
1726 return StmtError();
1727 // Generate list of implicitly defined firstprivate variables.
1728 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00001729
1730 if (!DSAChecker.getImplicitFirstprivate().empty()) {
1731 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
1732 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
1733 SourceLocation(), SourceLocation())) {
1734 ClausesWithImplicit.push_back(Implicit);
1735 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
1736 DSAChecker.getImplicitFirstprivate().size();
1737 } else
1738 ErrorFound = true;
1739 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001740 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001741
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001742 switch (Kind) {
1743 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00001744 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
1745 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001746 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001747 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001748 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
1749 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00001750 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00001751 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00001752 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
1753 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001754 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00001755 case OMPD_for_simd:
1756 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
1757 EndLoc, VarsWithInheritedDSA);
1758 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001759 case OMPD_sections:
1760 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
1761 EndLoc);
1762 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001763 case OMPD_section:
1764 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00001765 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001766 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
1767 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001768 case OMPD_single:
1769 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
1770 EndLoc);
1771 break;
Alexander Musman80c22892014-07-17 08:54:58 +00001772 case OMPD_master:
1773 assert(ClausesWithImplicit.empty() &&
1774 "No clauses are allowed for 'omp master' directive");
1775 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
1776 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001777 case OMPD_critical:
1778 assert(ClausesWithImplicit.empty() &&
1779 "No clauses are allowed for 'omp critical' directive");
1780 Res = ActOnOpenMPCriticalDirective(DirName, AStmt, StartLoc, EndLoc);
1781 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00001782 case OMPD_parallel_for:
1783 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
1784 EndLoc, VarsWithInheritedDSA);
1785 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00001786 case OMPD_parallel_for_simd:
1787 Res = ActOnOpenMPParallelForSimdDirective(
1788 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
1789 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001790 case OMPD_parallel_sections:
1791 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
1792 StartLoc, EndLoc);
1793 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001794 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001795 Res =
1796 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
1797 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00001798 case OMPD_taskyield:
1799 assert(ClausesWithImplicit.empty() &&
1800 "No clauses are allowed for 'omp taskyield' directive");
1801 assert(AStmt == nullptr &&
1802 "No associated statement allowed for 'omp taskyield' directive");
1803 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
1804 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001805 case OMPD_barrier:
1806 assert(ClausesWithImplicit.empty() &&
1807 "No clauses are allowed for 'omp barrier' directive");
1808 assert(AStmt == nullptr &&
1809 "No associated statement allowed for 'omp barrier' directive");
1810 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
1811 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00001812 case OMPD_taskwait:
1813 assert(ClausesWithImplicit.empty() &&
1814 "No clauses are allowed for 'omp taskwait' directive");
1815 assert(AStmt == nullptr &&
1816 "No associated statement allowed for 'omp taskwait' directive");
1817 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
1818 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00001819 case OMPD_flush:
1820 assert(AStmt == nullptr &&
1821 "No associated statement allowed for 'omp flush' directive");
1822 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
1823 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001824 case OMPD_ordered:
1825 assert(ClausesWithImplicit.empty() &&
1826 "No clauses are allowed for 'omp ordered' directive");
1827 Res = ActOnOpenMPOrderedDirective(AStmt, StartLoc, EndLoc);
1828 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00001829 case OMPD_atomic:
1830 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
1831 EndLoc);
1832 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00001833 case OMPD_teams:
1834 Res =
1835 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
1836 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001837 case OMPD_target:
1838 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
1839 EndLoc);
1840 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001841 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001842 llvm_unreachable("OpenMP Directive is not allowed");
1843 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001844 llvm_unreachable("Unknown OpenMP directive");
1845 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001846
Alexey Bataev4acb8592014-07-07 13:01:15 +00001847 for (auto P : VarsWithInheritedDSA) {
1848 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
1849 << P.first << P.second->getSourceRange();
1850 }
1851 if (!VarsWithInheritedDSA.empty())
1852 return StmtError();
1853
Alexey Bataeved09d242014-05-28 05:53:51 +00001854 if (ErrorFound)
1855 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001856 return Res;
1857}
1858
1859StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
1860 Stmt *AStmt,
1861 SourceLocation StartLoc,
1862 SourceLocation EndLoc) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001863 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
1864 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
1865 // 1.2.2 OpenMP Language Terminology
1866 // Structured block - An executable statement with a single entry at the
1867 // top and a single exit at the bottom.
1868 // The point of exit cannot be a branch out of the structured block.
1869 // longjmp() and throw() must not violate the entry/exit criteria.
1870 CS->getCapturedDecl()->setNothrow();
1871
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001872 getCurFunction()->setHasBranchProtectedScope();
1873
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00001874 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
1875 AStmt);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001876}
1877
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001878namespace {
1879/// \brief Helper class for checking canonical form of the OpenMP loops and
1880/// extracting iteration space of each loop in the loop nest, that will be used
1881/// for IR generation.
1882class OpenMPIterationSpaceChecker {
1883 /// \brief Reference to Sema.
1884 Sema &SemaRef;
1885 /// \brief A location for diagnostics (when there is no some better location).
1886 SourceLocation DefaultLoc;
1887 /// \brief A location for diagnostics (when increment is not compatible).
1888 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00001889 /// \brief A source location for referring to loop init later.
1890 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001891 /// \brief A source location for referring to condition later.
1892 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00001893 /// \brief A source location for referring to increment later.
1894 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001895 /// \brief Loop variable.
1896 VarDecl *Var;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001897 /// \brief Reference to loop variable.
1898 DeclRefExpr *VarRef;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001899 /// \brief Lower bound (initializer for the var).
1900 Expr *LB;
1901 /// \brief Upper bound.
1902 Expr *UB;
1903 /// \brief Loop step (increment).
1904 Expr *Step;
1905 /// \brief This flag is true when condition is one of:
1906 /// Var < UB
1907 /// Var <= UB
1908 /// UB > Var
1909 /// UB >= Var
1910 bool TestIsLessOp;
1911 /// \brief This flag is true when condition is strict ( < or > ).
1912 bool TestIsStrictOp;
1913 /// \brief This flag is true when step is subtracted on each iteration.
1914 bool SubtractStep;
1915
1916public:
1917 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
1918 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
Alexander Musmana5f070a2014-10-01 06:03:56 +00001919 InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()),
1920 IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001921 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
1922 TestIsStrictOp(false), SubtractStep(false) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001923 /// \brief Check init-expr for canonical loop form and save loop counter
1924 /// variable - #Var and its initialization value - #LB.
1925 bool CheckInit(Stmt *S);
1926 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
1927 /// for less/greater and for strict/non-strict comparison.
1928 bool CheckCond(Expr *S);
1929 /// \brief Check incr-expr for canonical loop form and return true if it
1930 /// does not conform, otherwise save loop step (#Step).
1931 bool CheckInc(Expr *S);
1932 /// \brief Return the loop counter variable.
1933 VarDecl *GetLoopVar() const { return Var; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001934 /// \brief Return the reference expression to loop counter variable.
1935 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00001936 /// \brief Source range of the loop init.
1937 SourceRange GetInitSrcRange() const { return InitSrcRange; }
1938 /// \brief Source range of the loop condition.
1939 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
1940 /// \brief Source range of the loop increment.
1941 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
1942 /// \brief True if the step should be subtracted.
1943 bool ShouldSubtractStep() const { return SubtractStep; }
1944 /// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00001945 Expr *BuildNumIterations(Scope *S, const bool LimitedType) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00001946 /// \brief Build reference expression to the counter be used for codegen.
1947 Expr *BuildCounterVar() const;
1948 /// \brief Build initization of the counter be used for codegen.
1949 Expr *BuildCounterInit() const;
1950 /// \brief Build step of the counter be used for codegen.
1951 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001952 /// \brief Return true if any expression is dependent.
1953 bool Dependent() const;
1954
1955private:
1956 /// \brief Check the right-hand side of an assignment in the increment
1957 /// expression.
1958 bool CheckIncRHS(Expr *RHS);
1959 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001960 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001961 /// \brief Helper to set upper bound.
1962 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, const SourceRange &SR,
1963 const SourceLocation &SL);
1964 /// \brief Helper to set loop increment.
1965 bool SetStep(Expr *NewStep, bool Subtract);
1966};
1967
1968bool OpenMPIterationSpaceChecker::Dependent() const {
1969 if (!Var) {
1970 assert(!LB && !UB && !Step);
1971 return false;
1972 }
1973 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
1974 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
1975}
1976
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001977bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
1978 DeclRefExpr *NewVarRefExpr,
1979 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001980 // State consistency checking to ensure correct usage.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001981 assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
1982 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001983 if (!NewVar || !NewLB)
1984 return true;
1985 Var = NewVar;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00001986 VarRef = NewVarRefExpr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00001987 LB = NewLB;
1988 return false;
1989}
1990
1991bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
1992 const SourceRange &SR,
1993 const SourceLocation &SL) {
1994 // State consistency checking to ensure correct usage.
1995 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
1996 !TestIsLessOp && !TestIsStrictOp);
1997 if (!NewUB)
1998 return true;
1999 UB = NewUB;
2000 TestIsLessOp = LessOp;
2001 TestIsStrictOp = StrictOp;
2002 ConditionSrcRange = SR;
2003 ConditionLoc = SL;
2004 return false;
2005}
2006
2007bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
2008 // State consistency checking to ensure correct usage.
2009 assert(Var != nullptr && LB != nullptr && Step == nullptr);
2010 if (!NewStep)
2011 return true;
2012 if (!NewStep->isValueDependent()) {
2013 // Check that the step is integer expression.
2014 SourceLocation StepLoc = NewStep->getLocStart();
2015 ExprResult Val =
2016 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
2017 if (Val.isInvalid())
2018 return true;
2019 NewStep = Val.get();
2020
2021 // OpenMP [2.6, Canonical Loop Form, Restrictions]
2022 // If test-expr is of form var relational-op b and relational-op is < or
2023 // <= then incr-expr must cause var to increase on each iteration of the
2024 // loop. If test-expr is of form var relational-op b and relational-op is
2025 // > or >= then incr-expr must cause var to decrease on each iteration of
2026 // the loop.
2027 // If test-expr is of form b relational-op var and relational-op is < or
2028 // <= then incr-expr must cause var to decrease on each iteration of the
2029 // loop. If test-expr is of form b relational-op var and relational-op is
2030 // > or >= then incr-expr must cause var to increase on each iteration of
2031 // the loop.
2032 llvm::APSInt Result;
2033 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
2034 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
2035 bool IsConstNeg =
2036 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00002037 bool IsConstPos =
2038 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002039 bool IsConstZero = IsConstant && !Result.getBoolValue();
2040 if (UB && (IsConstZero ||
2041 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00002042 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002043 SemaRef.Diag(NewStep->getExprLoc(),
2044 diag::err_omp_loop_incr_not_compatible)
2045 << Var << TestIsLessOp << NewStep->getSourceRange();
2046 SemaRef.Diag(ConditionLoc,
2047 diag::note_omp_loop_cond_requres_compatible_incr)
2048 << TestIsLessOp << ConditionSrcRange;
2049 return true;
2050 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002051 if (TestIsLessOp == Subtract) {
2052 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
2053 NewStep).get();
2054 Subtract = !Subtract;
2055 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002056 }
2057
2058 Step = NewStep;
2059 SubtractStep = Subtract;
2060 return false;
2061}
2062
2063bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S) {
2064 // Check init-expr for canonical loop form and save loop counter
2065 // variable - #Var and its initialization value - #LB.
2066 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
2067 // var = lb
2068 // integer-type var = lb
2069 // random-access-iterator-type var = lb
2070 // pointer-type var = lb
2071 //
2072 if (!S) {
2073 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
2074 return true;
2075 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002076 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002077 if (Expr *E = dyn_cast<Expr>(S))
2078 S = E->IgnoreParens();
2079 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2080 if (BO->getOpcode() == BO_Assign)
2081 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002082 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002083 BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002084 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
2085 if (DS->isSingleDecl()) {
2086 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
2087 if (Var->hasInit()) {
2088 // Accept non-canonical init form here but emit ext. warning.
2089 if (Var->getInitStyle() != VarDecl::CInit)
2090 SemaRef.Diag(S->getLocStart(),
2091 diag::ext_omp_loop_not_canonical_init)
2092 << S->getSourceRange();
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002093 return SetVarAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002094 }
2095 }
2096 }
2097 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
2098 if (CE->getOperator() == OO_Equal)
2099 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002100 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
2101 CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002102
2103 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
2104 << S->getSourceRange();
2105 return true;
2106}
2107
Alexey Bataev23b69422014-06-18 07:08:49 +00002108/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002109/// variable (which may be the loop variable) if possible.
2110static const VarDecl *GetInitVarDecl(const Expr *E) {
2111 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00002112 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002113 E = E->IgnoreParenImpCasts();
2114 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
2115 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
2116 if (Ctor->isCopyConstructor() && CE->getNumArgs() == 1 &&
2117 CE->getArg(0) != nullptr)
2118 E = CE->getArg(0)->IgnoreParenImpCasts();
2119 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
2120 if (!DRE)
2121 return nullptr;
2122 return dyn_cast<VarDecl>(DRE->getDecl());
2123}
2124
2125bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
2126 // Check test-expr for canonical form, save upper-bound UB, flags for
2127 // less/greater and for strict/non-strict comparison.
2128 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2129 // var relational-op b
2130 // b relational-op var
2131 //
2132 if (!S) {
2133 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
2134 return true;
2135 }
2136 S = S->IgnoreParenImpCasts();
2137 SourceLocation CondLoc = S->getLocStart();
2138 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2139 if (BO->isRelationalOp()) {
2140 if (GetInitVarDecl(BO->getLHS()) == Var)
2141 return SetUB(BO->getRHS(),
2142 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
2143 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2144 BO->getSourceRange(), BO->getOperatorLoc());
2145 if (GetInitVarDecl(BO->getRHS()) == Var)
2146 return SetUB(BO->getLHS(),
2147 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
2148 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2149 BO->getSourceRange(), BO->getOperatorLoc());
2150 }
2151 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2152 if (CE->getNumArgs() == 2) {
2153 auto Op = CE->getOperator();
2154 switch (Op) {
2155 case OO_Greater:
2156 case OO_GreaterEqual:
2157 case OO_Less:
2158 case OO_LessEqual:
2159 if (GetInitVarDecl(CE->getArg(0)) == Var)
2160 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
2161 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2162 CE->getOperatorLoc());
2163 if (GetInitVarDecl(CE->getArg(1)) == Var)
2164 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
2165 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2166 CE->getOperatorLoc());
2167 break;
2168 default:
2169 break;
2170 }
2171 }
2172 }
2173 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
2174 << S->getSourceRange() << Var;
2175 return true;
2176}
2177
2178bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
2179 // RHS of canonical loop form increment can be:
2180 // var + incr
2181 // incr + var
2182 // var - incr
2183 //
2184 RHS = RHS->IgnoreParenImpCasts();
2185 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
2186 if (BO->isAdditiveOp()) {
2187 bool IsAdd = BO->getOpcode() == BO_Add;
2188 if (GetInitVarDecl(BO->getLHS()) == Var)
2189 return SetStep(BO->getRHS(), !IsAdd);
2190 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
2191 return SetStep(BO->getLHS(), false);
2192 }
2193 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
2194 bool IsAdd = CE->getOperator() == OO_Plus;
2195 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
2196 if (GetInitVarDecl(CE->getArg(0)) == Var)
2197 return SetStep(CE->getArg(1), !IsAdd);
2198 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
2199 return SetStep(CE->getArg(0), false);
2200 }
2201 }
2202 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2203 << RHS->getSourceRange() << Var;
2204 return true;
2205}
2206
2207bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
2208 // Check incr-expr for canonical loop form and return true if it
2209 // does not conform.
2210 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2211 // ++var
2212 // var++
2213 // --var
2214 // var--
2215 // var += incr
2216 // var -= incr
2217 // var = var + incr
2218 // var = incr + var
2219 // var = var - incr
2220 //
2221 if (!S) {
2222 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
2223 return true;
2224 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002225 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002226 S = S->IgnoreParens();
2227 if (auto UO = dyn_cast<UnaryOperator>(S)) {
2228 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
2229 return SetStep(
2230 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
2231 (UO->isDecrementOp() ? -1 : 1)).get(),
2232 false);
2233 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
2234 switch (BO->getOpcode()) {
2235 case BO_AddAssign:
2236 case BO_SubAssign:
2237 if (GetInitVarDecl(BO->getLHS()) == Var)
2238 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
2239 break;
2240 case BO_Assign:
2241 if (GetInitVarDecl(BO->getLHS()) == Var)
2242 return CheckIncRHS(BO->getRHS());
2243 break;
2244 default:
2245 break;
2246 }
2247 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2248 switch (CE->getOperator()) {
2249 case OO_PlusPlus:
2250 case OO_MinusMinus:
2251 if (GetInitVarDecl(CE->getArg(0)) == Var)
2252 return SetStep(
2253 SemaRef.ActOnIntegerConstant(
2254 CE->getLocStart(),
2255 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
2256 false);
2257 break;
2258 case OO_PlusEqual:
2259 case OO_MinusEqual:
2260 if (GetInitVarDecl(CE->getArg(0)) == Var)
2261 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
2262 break;
2263 case OO_Equal:
2264 if (GetInitVarDecl(CE->getArg(0)) == Var)
2265 return CheckIncRHS(CE->getArg(1));
2266 break;
2267 default:
2268 break;
2269 }
2270 }
2271 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2272 << S->getSourceRange() << Var;
2273 return true;
2274}
Alexander Musmana5f070a2014-10-01 06:03:56 +00002275
2276/// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00002277Expr *
2278OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S,
2279 const bool LimitedType) const {
Alexander Musmana5f070a2014-10-01 06:03:56 +00002280 ExprResult Diff;
2281 if (Var->getType()->isIntegerType() || Var->getType()->isPointerType() ||
2282 SemaRef.getLangOpts().CPlusPlus) {
2283 // Upper - Lower
2284 Expr *Upper = TestIsLessOp ? UB : LB;
2285 Expr *Lower = TestIsLessOp ? LB : UB;
2286
2287 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
2288
2289 if (!Diff.isUsable() && Var->getType()->getAsCXXRecordDecl()) {
2290 // BuildBinOp already emitted error, this one is to point user to upper
2291 // and lower bound, and to tell what is passed to 'operator-'.
2292 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
2293 << Upper->getSourceRange() << Lower->getSourceRange();
2294 return nullptr;
2295 }
2296 }
2297
2298 if (!Diff.isUsable())
2299 return nullptr;
2300
2301 // Upper - Lower [- 1]
2302 if (TestIsStrictOp)
2303 Diff = SemaRef.BuildBinOp(
2304 S, DefaultLoc, BO_Sub, Diff.get(),
2305 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
2306 if (!Diff.isUsable())
2307 return nullptr;
2308
2309 // Upper - Lower [- 1] + Step
2310 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(),
2311 Step->IgnoreImplicit());
2312 if (!Diff.isUsable())
2313 return nullptr;
2314
2315 // Parentheses (for dumping/debugging purposes only).
2316 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
2317 if (!Diff.isUsable())
2318 return nullptr;
2319
2320 // (Upper - Lower [- 1] + Step) / Step
2321 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(),
2322 Step->IgnoreImplicit());
2323 if (!Diff.isUsable())
2324 return nullptr;
2325
Alexander Musman174b3ca2014-10-06 11:16:29 +00002326 // OpenMP runtime requires 32-bit or 64-bit loop variables.
2327 if (LimitedType) {
2328 auto &C = SemaRef.Context;
2329 QualType Type = Diff.get()->getType();
2330 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
2331 if (NewSize != C.getTypeSize(Type)) {
2332 if (NewSize < C.getTypeSize(Type)) {
2333 assert(NewSize == 64 && "incorrect loop var size");
2334 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
2335 << InitSrcRange << ConditionSrcRange;
2336 }
2337 QualType NewType = C.getIntTypeForBitwidth(
2338 NewSize, Type->hasSignedIntegerRepresentation());
2339 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
2340 Sema::AA_Converting, true);
2341 if (!Diff.isUsable())
2342 return nullptr;
2343 }
2344 }
2345
Alexander Musmana5f070a2014-10-01 06:03:56 +00002346 return Diff.get();
2347}
2348
2349/// \brief Build reference expression to the counter be used for codegen.
2350Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const {
2351 return DeclRefExpr::Create(SemaRef.Context, NestedNameSpecifierLoc(),
2352 GetIncrementSrcRange().getBegin(), Var, false,
2353 DefaultLoc, Var->getType(), VK_LValue);
2354}
2355
2356/// \brief Build initization of the counter be used for codegen.
2357Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
2358
2359/// \brief Build step of the counter be used for codegen.
2360Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
2361
2362/// \brief Iteration space of a single for loop.
2363struct LoopIterationSpace {
2364 /// \brief This expression calculates the number of iterations in the loop.
2365 /// It is always possible to calculate it before starting the loop.
2366 Expr *NumIterations;
2367 /// \brief The loop counter variable.
2368 Expr *CounterVar;
2369 /// \brief This is initializer for the initial value of #CounterVar.
2370 Expr *CounterInit;
2371 /// \brief This is step for the #CounterVar used to generate its update:
2372 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
2373 Expr *CounterStep;
2374 /// \brief Should step be subtracted?
2375 bool Subtract;
2376 /// \brief Source range of the loop init.
2377 SourceRange InitSrcRange;
2378 /// \brief Source range of the loop condition.
2379 SourceRange CondSrcRange;
2380 /// \brief Source range of the loop increment.
2381 SourceRange IncSrcRange;
2382};
2383
2384/// \brief The resulting expressions built for the OpenMP loop CodeGen for the
2385/// whole collapsed loop nest. See class OMPLoopDirective for their description.
2386struct BuiltLoopExprs {
2387 Expr *IterationVarRef;
2388 Expr *LastIteration;
2389 Expr *CalcLastIteration;
2390 Expr *PreCond;
2391 Expr *Cond;
2392 Expr *SeparatedCond;
2393 Expr *Init;
2394 Expr *Inc;
2395 SmallVector<Expr *, 4> Counters;
2396 SmallVector<Expr *, 4> Updates;
2397 SmallVector<Expr *, 4> Finals;
2398
2399 bool builtAll() {
2400 return IterationVarRef != nullptr && LastIteration != nullptr &&
2401 PreCond != nullptr && Cond != nullptr && SeparatedCond != nullptr &&
2402 Init != nullptr && Inc != nullptr;
2403 }
2404 void clear(unsigned size) {
2405 IterationVarRef = nullptr;
2406 LastIteration = nullptr;
2407 CalcLastIteration = nullptr;
2408 PreCond = nullptr;
2409 Cond = nullptr;
2410 SeparatedCond = nullptr;
2411 Init = nullptr;
2412 Inc = nullptr;
2413 Counters.resize(size);
2414 Updates.resize(size);
2415 Finals.resize(size);
2416 for (unsigned i = 0; i < size; ++i) {
2417 Counters[i] = nullptr;
2418 Updates[i] = nullptr;
2419 Finals[i] = nullptr;
2420 }
2421 }
2422};
2423
Alexey Bataev23b69422014-06-18 07:08:49 +00002424} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002425
2426/// \brief Called on a for stmt to check and extract its iteration space
2427/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00002428static bool CheckOpenMPIterationSpace(
2429 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
2430 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
2431 Expr *NestedLoopCountExpr,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002432 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
2433 LoopIterationSpace &ResultIterSpace) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002434 // OpenMP [2.6, Canonical Loop Form]
2435 // for (init-expr; test-expr; incr-expr) structured-block
2436 auto For = dyn_cast_or_null<ForStmt>(S);
2437 if (!For) {
2438 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002439 << (NestedLoopCountExpr != nullptr) << getOpenMPDirectiveName(DKind)
2440 << NestedLoopCount << (CurrentNestedLoopCount > 0)
2441 << CurrentNestedLoopCount;
2442 if (NestedLoopCount > 1)
2443 SemaRef.Diag(NestedLoopCountExpr->getExprLoc(),
2444 diag::note_omp_collapse_expr)
2445 << NestedLoopCountExpr->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002446 return true;
2447 }
2448 assert(For->getBody());
2449
2450 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
2451
2452 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002453 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002454 if (ISC.CheckInit(Init)) {
2455 return true;
2456 }
2457
2458 bool HasErrors = false;
2459
2460 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002461 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002462
2463 // OpenMP [2.6, Canonical Loop Form]
2464 // Var is one of the following:
2465 // A variable of signed or unsigned integer type.
2466 // For C++, a variable of a random access iterator type.
2467 // For C, a variable of a pointer type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002468 auto VarType = Var->getType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002469 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
2470 !VarType->isPointerType() &&
2471 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
2472 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
2473 << SemaRef.getLangOpts().CPlusPlus;
2474 HasErrors = true;
2475 }
2476
Alexey Bataev4acb8592014-07-07 13:01:15 +00002477 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
2478 // Construct
2479 // The loop iteration variable(s) in the associated for-loop(s) of a for or
2480 // parallel for construct is (are) private.
2481 // The loop iteration variable in the associated for-loop of a simd construct
2482 // with just one associated for-loop is linear with a constant-linear-step
2483 // that is the increment of the associated for-loop.
2484 // Exclude loop var from the list of variables with implicitly defined data
2485 // sharing attributes.
Benjamin Kramerad8e0792014-10-10 15:32:48 +00002486 VarsWithImplicitDSA.erase(Var);
Alexey Bataev4acb8592014-07-07 13:01:15 +00002487
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002488 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
2489 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00002490 // The loop iteration variable in the associated for-loop of a simd construct
2491 // with just one associated for-loop may be listed in a linear clause with a
2492 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002493 // The loop iteration variable(s) in the associated for-loop(s) of a for or
2494 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002495 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002496 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
2497 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
2498 // declared in the loop and it is predetermined as a private.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002499 auto PredeterminedCKind =
2500 isOpenMPSimdDirective(DKind)
2501 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
2502 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002503 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00002504 DVar.CKind != PredeterminedCKind) ||
Alexander Musmanf82886e2014-09-18 05:12:34 +00002505 (isOpenMPWorksharingDirective(DKind) && !isOpenMPSimdDirective(DKind) &&
2506 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private &&
2507 DVar.CKind != OMPC_lastprivate)) &&
Alexander Musman1bb328c2014-06-04 13:06:39 +00002508 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002509 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00002510 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
2511 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002512 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002513 HasErrors = true;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002514 } else if (LoopVarRefExpr != nullptr) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00002515 // Make the loop iteration variable private (for worksharing constructs),
2516 // linear (for simd directives with the only one associated loop) or
2517 // lastprivate (for simd directives with several collapsed loops).
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002518 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002519 }
2520
Alexey Bataev7ff55242014-06-19 09:13:45 +00002521 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00002522
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002523 // Check test-expr.
2524 HasErrors |= ISC.CheckCond(For->getCond());
2525
2526 // Check incr-expr.
2527 HasErrors |= ISC.CheckInc(For->getInc());
2528
Alexander Musmana5f070a2014-10-01 06:03:56 +00002529 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002530 return HasErrors;
2531
Alexander Musmana5f070a2014-10-01 06:03:56 +00002532 // Build the loop's iteration space representation.
Alexander Musman174b3ca2014-10-06 11:16:29 +00002533 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
2534 DSA.getCurScope(), /* LimitedType */ isOpenMPWorksharingDirective(DKind));
Alexander Musmana5f070a2014-10-01 06:03:56 +00002535 ResultIterSpace.CounterVar = ISC.BuildCounterVar();
2536 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
2537 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
2538 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
2539 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
2540 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
2541 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
2542
2543 HasErrors |= (ResultIterSpace.NumIterations == nullptr ||
2544 ResultIterSpace.CounterVar == nullptr ||
2545 ResultIterSpace.CounterInit == nullptr ||
2546 ResultIterSpace.CounterStep == nullptr);
2547
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002548 return HasErrors;
2549}
2550
Alexander Musmana5f070a2014-10-01 06:03:56 +00002551/// \brief Build a variable declaration for OpenMP loop iteration variable.
2552static VarDecl *BuildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
2553 StringRef Name) {
2554 DeclContext *DC = SemaRef.CurContext;
2555 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
2556 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
2557 VarDecl *Decl =
2558 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
2559 Decl->setImplicit();
2560 return Decl;
2561}
2562
2563/// \brief Build 'VarRef = Start + Iter * Step'.
2564static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S,
2565 SourceLocation Loc, ExprResult VarRef,
2566 ExprResult Start, ExprResult Iter,
2567 ExprResult Step, bool Subtract) {
2568 // Add parentheses (for debugging purposes only).
2569 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
2570 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
2571 !Step.isUsable())
2572 return ExprError();
2573
2574 ExprResult Update = SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(),
2575 Step.get()->IgnoreImplicit());
2576 if (!Update.isUsable())
2577 return ExprError();
2578
2579 // Build 'VarRef = Start + Iter * Step'.
2580 Update = SemaRef.BuildBinOp(S, Loc, (Subtract ? BO_Sub : BO_Add),
2581 Start.get()->IgnoreImplicit(), Update.get());
2582 if (!Update.isUsable())
2583 return ExprError();
2584
2585 Update = SemaRef.PerformImplicitConversion(
2586 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
2587 if (!Update.isUsable())
2588 return ExprError();
2589
2590 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
2591 return Update;
2592}
2593
2594/// \brief Convert integer expression \a E to make it have at least \a Bits
2595/// bits.
2596static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
2597 Sema &SemaRef) {
2598 if (E == nullptr)
2599 return ExprError();
2600 auto &C = SemaRef.Context;
2601 QualType OldType = E->getType();
2602 unsigned HasBits = C.getTypeSize(OldType);
2603 if (HasBits >= Bits)
2604 return ExprResult(E);
2605 // OK to convert to signed, because new type has more bits than old.
2606 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
2607 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
2608 true);
2609}
2610
2611/// \brief Check if the given expression \a E is a constant integer that fits
2612/// into \a Bits bits.
2613static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
2614 if (E == nullptr)
2615 return false;
2616 llvm::APSInt Result;
2617 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
2618 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
2619 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002620}
2621
2622/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00002623/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
2624/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002625static unsigned
2626CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *NestedLoopCountExpr,
2627 Stmt *AStmt, Sema &SemaRef, DSAStackTy &DSA,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002628 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
2629 BuiltLoopExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002630 unsigned NestedLoopCount = 1;
2631 if (NestedLoopCountExpr) {
2632 // Found 'collapse' clause - calculate collapse number.
2633 llvm::APSInt Result;
2634 if (NestedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
2635 NestedLoopCount = Result.getLimitedValue();
2636 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002637 // This is helper routine for loop directives (e.g., 'for', 'simd',
2638 // 'for simd', etc.).
Alexander Musmana5f070a2014-10-01 06:03:56 +00002639 SmallVector<LoopIterationSpace, 4> IterSpaces;
2640 IterSpaces.resize(NestedLoopCount);
2641 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002642 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002643 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev4acb8592014-07-07 13:01:15 +00002644 NestedLoopCount, NestedLoopCountExpr,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002645 VarsWithImplicitDSA, IterSpaces[Cnt]))
Alexey Bataevabfc0692014-06-25 06:52:00 +00002646 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002647 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00002648 // OpenMP [2.8.1, simd construct, Restrictions]
2649 // All loops associated with the construct must be perfectly nested; that
2650 // is, there must be no intervening code nor any OpenMP directive between
2651 // any two loops.
2652 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002653 }
2654
Alexander Musmana5f070a2014-10-01 06:03:56 +00002655 Built.clear(/* size */ NestedLoopCount);
2656
2657 if (SemaRef.CurContext->isDependentContext())
2658 return NestedLoopCount;
2659
2660 // An example of what is generated for the following code:
2661 //
2662 // #pragma omp simd collapse(2)
2663 // for (i = 0; i < NI; ++i)
2664 // for (j = J0; j < NJ; j+=2) {
2665 // <loop body>
2666 // }
2667 //
2668 // We generate the code below.
2669 // Note: the loop body may be outlined in CodeGen.
2670 // Note: some counters may be C++ classes, operator- is used to find number of
2671 // iterations and operator+= to calculate counter value.
2672 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
2673 // or i64 is currently supported).
2674 //
2675 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
2676 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
2677 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
2678 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
2679 // // similar updates for vars in clauses (e.g. 'linear')
2680 // <loop body (using local i and j)>
2681 // }
2682 // i = NI; // assign final values of counters
2683 // j = NJ;
2684 //
2685
2686 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
2687 // the iteration counts of the collapsed for loops.
2688 auto N0 = IterSpaces[0].NumIterations;
2689 ExprResult LastIteration32 = WidenIterationCount(32 /* Bits */, N0, SemaRef);
2690 ExprResult LastIteration64 = WidenIterationCount(64 /* Bits */, N0, SemaRef);
2691
2692 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
2693 return NestedLoopCount;
2694
2695 auto &C = SemaRef.Context;
2696 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
2697
2698 Scope *CurScope = DSA.getCurScope();
2699 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
2700 auto N = IterSpaces[Cnt].NumIterations;
2701 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
2702 if (LastIteration32.isUsable())
2703 LastIteration32 = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_Mul,
2704 LastIteration32.get(), N);
2705 if (LastIteration64.isUsable())
2706 LastIteration64 = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_Mul,
2707 LastIteration64.get(), N);
2708 }
2709
2710 // Choose either the 32-bit or 64-bit version.
2711 ExprResult LastIteration = LastIteration64;
2712 if (LastIteration32.isUsable() &&
2713 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
2714 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
2715 FitsInto(
2716 32 /* Bits */,
2717 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
2718 LastIteration64.get(), SemaRef)))
2719 LastIteration = LastIteration32;
2720
2721 if (!LastIteration.isUsable())
2722 return 0;
2723
2724 // Save the number of iterations.
2725 ExprResult NumIterations = LastIteration;
2726 {
2727 LastIteration = SemaRef.BuildBinOp(
2728 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
2729 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
2730 if (!LastIteration.isUsable())
2731 return 0;
2732 }
2733
2734 // Calculate the last iteration number beforehand instead of doing this on
2735 // each iteration. Do not do this if the number of iterations may be kfold-ed.
2736 llvm::APSInt Result;
2737 bool IsConstant =
2738 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
2739 ExprResult CalcLastIteration;
2740 if (!IsConstant) {
2741 SourceLocation SaveLoc;
2742 VarDecl *SaveVar =
2743 BuildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(),
2744 ".omp.last.iteration");
2745 ExprResult SaveRef = SemaRef.BuildDeclRefExpr(
2746 SaveVar, LastIteration.get()->getType(), VK_LValue, SaveLoc);
2747 CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign,
2748 SaveRef.get(), LastIteration.get());
2749 LastIteration = SaveRef;
2750
2751 // Prepare SaveRef + 1.
2752 NumIterations = SemaRef.BuildBinOp(
2753 CurScope, SaveLoc, BO_Add, SaveRef.get(),
2754 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
2755 if (!NumIterations.isUsable())
2756 return 0;
2757 }
2758
2759 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
2760
2761 // Precondition tests if there is at least one iteration (LastIteration > 0).
2762 ExprResult PreCond = SemaRef.BuildBinOp(
2763 CurScope, InitLoc, BO_GT, LastIteration.get(),
2764 SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get());
2765
2766 // Build the iteration variable and its initialization to zero before loop.
2767 ExprResult IV;
2768 ExprResult Init;
2769 {
2770 VarDecl *IVDecl = BuildVarDecl(SemaRef, InitLoc,
2771 LastIteration.get()->getType(), ".omp.iv");
2772 IV = SemaRef.BuildDeclRefExpr(IVDecl, LastIteration.get()->getType(),
2773 VK_LValue, InitLoc);
2774 Init = SemaRef.BuildBinOp(
2775 CurScope, InitLoc, BO_Assign, IV.get(),
2776 SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get());
2777 }
2778
2779 // Loop condition (IV < NumIterations)
2780 SourceLocation CondLoc;
2781 ExprResult Cond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
2782 NumIterations.get());
2783 // Loop condition with 1 iteration separated (IV < LastIteration)
2784 ExprResult SeparatedCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT,
2785 IV.get(), LastIteration.get());
2786
2787 // Loop increment (IV = IV + 1)
2788 SourceLocation IncLoc;
2789 ExprResult Inc =
2790 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
2791 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
2792 if (!Inc.isUsable())
2793 return 0;
2794 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
2795
2796 // Build updates and final values of the loop counters.
2797 bool HasErrors = false;
2798 Built.Counters.resize(NestedLoopCount);
2799 Built.Updates.resize(NestedLoopCount);
2800 Built.Finals.resize(NestedLoopCount);
2801 {
2802 ExprResult Div;
2803 // Go from inner nested loop to outer.
2804 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
2805 LoopIterationSpace &IS = IterSpaces[Cnt];
2806 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
2807 // Build: Iter = (IV / Div) % IS.NumIters
2808 // where Div is product of previous iterations' IS.NumIters.
2809 ExprResult Iter;
2810 if (Div.isUsable()) {
2811 Iter =
2812 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
2813 } else {
2814 Iter = IV;
2815 assert((Cnt == (int)NestedLoopCount - 1) &&
2816 "unusable div expected on first iteration only");
2817 }
2818
2819 if (Cnt != 0 && Iter.isUsable())
2820 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
2821 IS.NumIterations);
2822 if (!Iter.isUsable()) {
2823 HasErrors = true;
2824 break;
2825 }
2826
2827 // Build update: IS.CounterVar = IS.Start + Iter * IS.Step
2828 ExprResult Update =
2829 BuildCounterUpdate(SemaRef, CurScope, UpdLoc, IS.CounterVar,
2830 IS.CounterInit, Iter, IS.CounterStep, IS.Subtract);
2831 if (!Update.isUsable()) {
2832 HasErrors = true;
2833 break;
2834 }
2835
2836 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
2837 ExprResult Final = BuildCounterUpdate(
2838 SemaRef, CurScope, UpdLoc, IS.CounterVar, IS.CounterInit,
2839 IS.NumIterations, IS.CounterStep, IS.Subtract);
2840 if (!Final.isUsable()) {
2841 HasErrors = true;
2842 break;
2843 }
2844
2845 // Build Div for the next iteration: Div <- Div * IS.NumIters
2846 if (Cnt != 0) {
2847 if (Div.isUnset())
2848 Div = IS.NumIterations;
2849 else
2850 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
2851 IS.NumIterations);
2852
2853 // Add parentheses (for debugging purposes only).
2854 if (Div.isUsable())
2855 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
2856 if (!Div.isUsable()) {
2857 HasErrors = true;
2858 break;
2859 }
2860 }
2861 if (!Update.isUsable() || !Final.isUsable()) {
2862 HasErrors = true;
2863 break;
2864 }
2865 // Save results
2866 Built.Counters[Cnt] = IS.CounterVar;
2867 Built.Updates[Cnt] = Update.get();
2868 Built.Finals[Cnt] = Final.get();
2869 }
2870 }
2871
2872 if (HasErrors)
2873 return 0;
2874
2875 // Save results
2876 Built.IterationVarRef = IV.get();
2877 Built.LastIteration = LastIteration.get();
2878 Built.CalcLastIteration = CalcLastIteration.get();
2879 Built.PreCond = PreCond.get();
2880 Built.Cond = Cond.get();
2881 Built.SeparatedCond = SeparatedCond.get();
2882 Built.Init = Init.get();
2883 Built.Inc = Inc.get();
2884
Alexey Bataevabfc0692014-06-25 06:52:00 +00002885 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002886}
2887
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002888static Expr *GetCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002889 auto CollapseFilter = [](const OMPClause *C) -> bool {
2890 return C->getClauseKind() == OMPC_collapse;
2891 };
2892 OMPExecutableDirective::filtered_clause_iterator<decltype(CollapseFilter)> I(
2893 Clauses, CollapseFilter);
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002894 if (I)
2895 return cast<OMPCollapseClause>(*I)->getNumForLoops();
2896 return nullptr;
2897}
2898
Alexey Bataev4acb8592014-07-07 13:01:15 +00002899StmtResult Sema::ActOnOpenMPSimdDirective(
2900 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2901 SourceLocation EndLoc,
2902 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00002903 BuiltLoopExprs B;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002904 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002905 unsigned NestedLoopCount =
2906 CheckOpenMPLoop(OMPD_simd, GetCollapseNumberExpr(Clauses), AStmt, *this,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002907 *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00002908 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002909 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002910
Alexander Musmana5f070a2014-10-01 06:03:56 +00002911 assert((CurContext->isDependentContext() || B.builtAll()) &&
2912 "omp simd loop exprs were not built");
2913
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002914 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00002915 return OMPSimdDirective::Create(
2916 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt,
2917 B.IterationVarRef, B.LastIteration, B.CalcLastIteration, B.PreCond,
2918 B.Cond, B.SeparatedCond, B.Init, B.Inc, B.Counters, B.Updates, B.Finals);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002919}
2920
Alexey Bataev4acb8592014-07-07 13:01:15 +00002921StmtResult Sema::ActOnOpenMPForDirective(
2922 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2923 SourceLocation EndLoc,
2924 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00002925 BuiltLoopExprs B;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002926 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002927 unsigned NestedLoopCount =
2928 CheckOpenMPLoop(OMPD_for, GetCollapseNumberExpr(Clauses), AStmt, *this,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002929 *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00002930 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00002931 return StmtError();
2932
Alexander Musmana5f070a2014-10-01 06:03:56 +00002933 assert((CurContext->isDependentContext() || B.builtAll()) &&
2934 "omp for loop exprs were not built");
2935
Alexey Bataevf29276e2014-06-18 04:14:57 +00002936 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00002937 return OMPForDirective::Create(
2938 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt,
2939 B.IterationVarRef, B.LastIteration, B.CalcLastIteration, B.PreCond,
2940 B.Cond, B.SeparatedCond, B.Init, B.Inc, B.Counters, B.Updates, B.Finals);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002941}
2942
Alexander Musmanf82886e2014-09-18 05:12:34 +00002943StmtResult Sema::ActOnOpenMPForSimdDirective(
2944 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
2945 SourceLocation EndLoc,
2946 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00002947 BuiltLoopExprs B;
Alexander Musmanf82886e2014-09-18 05:12:34 +00002948 // In presence of clause 'collapse', it will define the nested loops number.
2949 unsigned NestedLoopCount =
2950 CheckOpenMPLoop(OMPD_for_simd, GetCollapseNumberExpr(Clauses), AStmt,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002951 *this, *DSAStack, VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00002952 if (NestedLoopCount == 0)
2953 return StmtError();
2954
2955 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00002956 return OMPForSimdDirective::Create(
2957 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt,
2958 B.IterationVarRef, B.LastIteration, B.CalcLastIteration, B.PreCond,
2959 B.Cond, B.SeparatedCond, B.Init, B.Inc, B.Counters, B.Updates, B.Finals);
Alexander Musmanf82886e2014-09-18 05:12:34 +00002960}
2961
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002962StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
2963 Stmt *AStmt,
2964 SourceLocation StartLoc,
2965 SourceLocation EndLoc) {
2966 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2967 auto BaseStmt = AStmt;
2968 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
2969 BaseStmt = CS->getCapturedStmt();
2970 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
2971 auto S = C->children();
2972 if (!S)
2973 return StmtError();
2974 // All associated statements must be '#pragma omp section' except for
2975 // the first one.
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002976 for (++S; S; ++S) {
2977 auto SectionStmt = *S;
2978 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
2979 if (SectionStmt)
2980 Diag(SectionStmt->getLocStart(),
2981 diag::err_omp_sections_substmt_not_section);
2982 return StmtError();
2983 }
2984 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002985 } else {
2986 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
2987 return StmtError();
2988 }
2989
2990 getCurFunction()->setHasBranchProtectedScope();
2991
2992 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses,
2993 AStmt);
2994}
2995
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002996StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
2997 SourceLocation StartLoc,
2998 SourceLocation EndLoc) {
2999 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3000
3001 getCurFunction()->setHasBranchProtectedScope();
3002
3003 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt);
3004}
3005
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003006StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
3007 Stmt *AStmt,
3008 SourceLocation StartLoc,
3009 SourceLocation EndLoc) {
Alexey Bataev74a05c92014-07-15 02:55:09 +00003010 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3011
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003012 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00003013
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003014 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3015}
3016
Alexander Musman80c22892014-07-17 08:54:58 +00003017StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
3018 SourceLocation StartLoc,
3019 SourceLocation EndLoc) {
3020 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3021
3022 getCurFunction()->setHasBranchProtectedScope();
3023
3024 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
3025}
3026
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003027StmtResult
3028Sema::ActOnOpenMPCriticalDirective(const DeclarationNameInfo &DirName,
3029 Stmt *AStmt, SourceLocation StartLoc,
3030 SourceLocation EndLoc) {
3031 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3032
3033 getCurFunction()->setHasBranchProtectedScope();
3034
3035 return OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
3036 AStmt);
3037}
3038
Alexey Bataev4acb8592014-07-07 13:01:15 +00003039StmtResult Sema::ActOnOpenMPParallelForDirective(
3040 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3041 SourceLocation EndLoc,
3042 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
3043 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3044 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3045 // 1.2.2 OpenMP Language Terminology
3046 // Structured block - An executable statement with a single entry at the
3047 // top and a single exit at the bottom.
3048 // The point of exit cannot be a branch out of the structured block.
3049 // longjmp() and throw() must not violate the entry/exit criteria.
3050 CS->getCapturedDecl()->setNothrow();
3051
Alexander Musmana5f070a2014-10-01 06:03:56 +00003052 BuiltLoopExprs B;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003053 // In presence of clause 'collapse', it will define the nested loops number.
3054 unsigned NestedLoopCount =
3055 CheckOpenMPLoop(OMPD_parallel_for, GetCollapseNumberExpr(Clauses), AStmt,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003056 *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003057 if (NestedLoopCount == 0)
3058 return StmtError();
3059
Alexander Musmana5f070a2014-10-01 06:03:56 +00003060 assert((CurContext->isDependentContext() || B.builtAll()) &&
3061 "omp parallel for loop exprs were not built");
3062
Alexey Bataev4acb8592014-07-07 13:01:15 +00003063 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003064 return OMPParallelForDirective::Create(
3065 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt,
3066 B.IterationVarRef, B.LastIteration, B.CalcLastIteration, B.PreCond,
3067 B.Cond, B.SeparatedCond, B.Init, B.Inc, B.Counters, B.Updates, B.Finals);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003068}
3069
Alexander Musmane4e893b2014-09-23 09:33:00 +00003070StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
3071 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3072 SourceLocation EndLoc,
3073 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
3074 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3075 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3076 // 1.2.2 OpenMP Language Terminology
3077 // Structured block - An executable statement with a single entry at the
3078 // top and a single exit at the bottom.
3079 // The point of exit cannot be a branch out of the structured block.
3080 // longjmp() and throw() must not violate the entry/exit criteria.
3081 CS->getCapturedDecl()->setNothrow();
3082
Alexander Musmana5f070a2014-10-01 06:03:56 +00003083 BuiltLoopExprs B;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003084 // In presence of clause 'collapse', it will define the nested loops number.
3085 unsigned NestedLoopCount =
3086 CheckOpenMPLoop(OMPD_parallel_for_simd, GetCollapseNumberExpr(Clauses),
Alexander Musmana5f070a2014-10-01 06:03:56 +00003087 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003088 if (NestedLoopCount == 0)
3089 return StmtError();
3090
3091 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003092 return OMPParallelForSimdDirective::Create(
3093 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt,
3094 B.IterationVarRef, B.LastIteration, B.CalcLastIteration, B.PreCond,
3095 B.Cond, B.SeparatedCond, B.Init, B.Inc, B.Counters, B.Updates, B.Finals);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003096}
3097
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003098StmtResult
3099Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
3100 Stmt *AStmt, SourceLocation StartLoc,
3101 SourceLocation EndLoc) {
3102 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3103 auto BaseStmt = AStmt;
3104 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
3105 BaseStmt = CS->getCapturedStmt();
3106 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
3107 auto S = C->children();
3108 if (!S)
3109 return StmtError();
3110 // All associated statements must be '#pragma omp section' except for
3111 // the first one.
3112 for (++S; S; ++S) {
3113 auto SectionStmt = *S;
3114 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
3115 if (SectionStmt)
3116 Diag(SectionStmt->getLocStart(),
3117 diag::err_omp_parallel_sections_substmt_not_section);
3118 return StmtError();
3119 }
3120 }
3121 } else {
3122 Diag(AStmt->getLocStart(),
3123 diag::err_omp_parallel_sections_not_compound_stmt);
3124 return StmtError();
3125 }
3126
3127 getCurFunction()->setHasBranchProtectedScope();
3128
3129 return OMPParallelSectionsDirective::Create(Context, StartLoc, EndLoc,
3130 Clauses, AStmt);
3131}
3132
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003133StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
3134 Stmt *AStmt, SourceLocation StartLoc,
3135 SourceLocation EndLoc) {
3136 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3137 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3138 // 1.2.2 OpenMP Language Terminology
3139 // Structured block - An executable statement with a single entry at the
3140 // top and a single exit at the bottom.
3141 // The point of exit cannot be a branch out of the structured block.
3142 // longjmp() and throw() must not violate the entry/exit criteria.
3143 CS->getCapturedDecl()->setNothrow();
3144
3145 getCurFunction()->setHasBranchProtectedScope();
3146
3147 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3148}
3149
Alexey Bataev68446b72014-07-18 07:47:19 +00003150StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
3151 SourceLocation EndLoc) {
3152 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
3153}
3154
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003155StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
3156 SourceLocation EndLoc) {
3157 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
3158}
3159
Alexey Bataev2df347a2014-07-18 10:17:07 +00003160StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
3161 SourceLocation EndLoc) {
3162 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
3163}
3164
Alexey Bataev6125da92014-07-21 11:26:11 +00003165StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
3166 SourceLocation StartLoc,
3167 SourceLocation EndLoc) {
3168 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
3169 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
3170}
3171
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003172StmtResult Sema::ActOnOpenMPOrderedDirective(Stmt *AStmt,
3173 SourceLocation StartLoc,
3174 SourceLocation EndLoc) {
3175 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3176
3177 getCurFunction()->setHasBranchProtectedScope();
3178
3179 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, AStmt);
3180}
3181
Alexey Bataev0162e452014-07-22 10:10:35 +00003182StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
3183 Stmt *AStmt,
3184 SourceLocation StartLoc,
3185 SourceLocation EndLoc) {
3186 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003187 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00003188 // 1.2.2 OpenMP Language Terminology
3189 // Structured block - An executable statement with a single entry at the
3190 // top and a single exit at the bottom.
3191 // The point of exit cannot be a branch out of the structured block.
3192 // longjmp() and throw() must not violate the entry/exit criteria.
3193 // TODO further analysis of associated statements and clauses.
Alexey Bataevdea47612014-07-23 07:46:59 +00003194 OpenMPClauseKind AtomicKind = OMPC_unknown;
3195 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003196 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00003197 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00003198 C->getClauseKind() == OMPC_update ||
3199 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00003200 if (AtomicKind != OMPC_unknown) {
3201 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
3202 << SourceRange(C->getLocStart(), C->getLocEnd());
3203 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
3204 << getOpenMPClauseName(AtomicKind);
3205 } else {
3206 AtomicKind = C->getClauseKind();
3207 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003208 }
3209 }
3210 }
Alexey Bataev459dec02014-07-24 06:46:57 +00003211 auto Body = CS->getCapturedStmt();
Alexey Bataevdea47612014-07-23 07:46:59 +00003212 if (AtomicKind == OMPC_read) {
Alexey Bataev459dec02014-07-24 06:46:57 +00003213 if (!isa<Expr>(Body)) {
3214 Diag(Body->getLocStart(),
Alexey Bataevdea47612014-07-23 07:46:59 +00003215 diag::err_omp_atomic_read_not_expression_statement);
3216 return StmtError();
3217 }
3218 } else if (AtomicKind == OMPC_write) {
Alexey Bataev459dec02014-07-24 06:46:57 +00003219 if (!isa<Expr>(Body)) {
3220 Diag(Body->getLocStart(),
Alexey Bataevdea47612014-07-23 07:46:59 +00003221 diag::err_omp_atomic_write_not_expression_statement);
3222 return StmtError();
3223 }
Alexey Bataev67a4f222014-07-23 10:25:33 +00003224 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev459dec02014-07-24 06:46:57 +00003225 if (!isa<Expr>(Body)) {
3226 Diag(Body->getLocStart(),
Alexey Bataev67a4f222014-07-23 10:25:33 +00003227 diag::err_omp_atomic_update_not_expression_statement)
3228 << (AtomicKind == OMPC_update);
3229 return StmtError();
3230 }
Alexey Bataev459dec02014-07-24 06:46:57 +00003231 } else if (AtomicKind == OMPC_capture) {
3232 if (isa<Expr>(Body) && !isa<BinaryOperator>(Body)) {
3233 Diag(Body->getLocStart(),
3234 diag::err_omp_atomic_capture_not_expression_statement);
3235 return StmtError();
3236 } else if (!isa<Expr>(Body) && !isa<CompoundStmt>(Body)) {
3237 Diag(Body->getLocStart(),
3238 diag::err_omp_atomic_capture_not_compound_statement);
3239 return StmtError();
3240 }
Alexey Bataevdea47612014-07-23 07:46:59 +00003241 }
Alexey Bataev0162e452014-07-22 10:10:35 +00003242
3243 getCurFunction()->setHasBranchProtectedScope();
3244
3245 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3246}
3247
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003248StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
3249 Stmt *AStmt,
3250 SourceLocation StartLoc,
3251 SourceLocation EndLoc) {
3252 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3253
Alexey Bataev13314bf2014-10-09 04:18:56 +00003254 // OpenMP [2.16, Nesting of Regions]
3255 // If specified, a teams construct must be contained within a target
3256 // construct. That target construct must contain no statements or directives
3257 // outside of the teams construct.
3258 if (DSAStack->hasInnerTeamsRegion()) {
3259 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
3260 bool OMPTeamsFound = true;
3261 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
3262 auto I = CS->body_begin();
3263 while (I != CS->body_end()) {
3264 auto OED = dyn_cast<OMPExecutableDirective>(*I);
3265 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
3266 OMPTeamsFound = false;
3267 break;
3268 }
3269 ++I;
3270 }
3271 assert(I != CS->body_end() && "Not found statement");
3272 S = *I;
3273 }
3274 if (!OMPTeamsFound) {
3275 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
3276 Diag(DSAStack->getInnerTeamsRegionLoc(),
3277 diag::note_omp_nested_teams_construct_here);
3278 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
3279 << isa<OMPExecutableDirective>(S);
3280 return StmtError();
3281 }
3282 }
3283
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003284 getCurFunction()->setHasBranchProtectedScope();
3285
3286 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3287}
3288
Alexey Bataev13314bf2014-10-09 04:18:56 +00003289StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
3290 Stmt *AStmt, SourceLocation StartLoc,
3291 SourceLocation EndLoc) {
3292 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3293 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3294 // 1.2.2 OpenMP Language Terminology
3295 // Structured block - An executable statement with a single entry at the
3296 // top and a single exit at the bottom.
3297 // The point of exit cannot be a branch out of the structured block.
3298 // longjmp() and throw() must not violate the entry/exit criteria.
3299 CS->getCapturedDecl()->setNothrow();
3300
3301 getCurFunction()->setHasBranchProtectedScope();
3302
3303 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3304}
3305
Alexey Bataeved09d242014-05-28 05:53:51 +00003306OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003307 SourceLocation StartLoc,
3308 SourceLocation LParenLoc,
3309 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003310 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003311 switch (Kind) {
3312 case OMPC_if:
3313 Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc);
3314 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00003315 case OMPC_final:
3316 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
3317 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00003318 case OMPC_num_threads:
3319 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
3320 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00003321 case OMPC_safelen:
3322 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
3323 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00003324 case OMPC_collapse:
3325 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
3326 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003327 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003328 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003329 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003330 case OMPC_private:
3331 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00003332 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003333 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00003334 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00003335 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00003336 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003337 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003338 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003339 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00003340 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003341 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003342 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003343 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00003344 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003345 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00003346 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00003347 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00003348 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003349 case OMPC_seq_cst:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003350 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003351 llvm_unreachable("Clause is not allowed.");
3352 }
3353 return Res;
3354}
3355
Alexey Bataeved09d242014-05-28 05:53:51 +00003356OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003357 SourceLocation LParenLoc,
3358 SourceLocation EndLoc) {
3359 Expr *ValExpr = Condition;
3360 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
3361 !Condition->isInstantiationDependent() &&
3362 !Condition->containsUnexpandedParameterPack()) {
3363 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00003364 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003365 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003366 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003367
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003368 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003369 }
3370
3371 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc);
3372}
3373
Alexey Bataev3778b602014-07-17 07:32:53 +00003374OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
3375 SourceLocation StartLoc,
3376 SourceLocation LParenLoc,
3377 SourceLocation EndLoc) {
3378 Expr *ValExpr = Condition;
3379 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
3380 !Condition->isInstantiationDependent() &&
3381 !Condition->containsUnexpandedParameterPack()) {
3382 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
3383 Condition->getExprLoc(), Condition);
3384 if (Val.isInvalid())
3385 return nullptr;
3386
3387 ValExpr = Val.get();
3388 }
3389
3390 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
3391}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003392ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
3393 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00003394 if (!Op)
3395 return ExprError();
3396
3397 class IntConvertDiagnoser : public ICEConvertDiagnoser {
3398 public:
3399 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00003400 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00003401 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
3402 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003403 return S.Diag(Loc, diag::err_omp_not_integral) << T;
3404 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003405 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
3406 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003407 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
3408 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003409 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
3410 QualType T,
3411 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003412 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
3413 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003414 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
3415 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003416 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00003417 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00003418 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003419 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
3420 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003421 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
3422 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003423 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
3424 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003425 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00003426 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00003427 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003428 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
3429 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00003430 llvm_unreachable("conversion functions are permitted");
3431 }
3432 } ConvertDiagnoser;
3433 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
3434}
3435
3436OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
3437 SourceLocation StartLoc,
3438 SourceLocation LParenLoc,
3439 SourceLocation EndLoc) {
3440 Expr *ValExpr = NumThreads;
3441 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
3442 !NumThreads->isInstantiationDependent() &&
3443 !NumThreads->containsUnexpandedParameterPack()) {
3444 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
3445 ExprResult Val =
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003446 PerformOpenMPImplicitIntegerConversion(NumThreadsLoc, NumThreads);
Alexey Bataev568a8332014-03-06 06:15:19 +00003447 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003448 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00003449
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003450 ValExpr = Val.get();
Alexey Bataev568a8332014-03-06 06:15:19 +00003451
3452 // OpenMP [2.5, Restrictions]
3453 // The num_threads expression must evaluate to a positive integer value.
3454 llvm::APSInt Result;
Alexey Bataeved09d242014-05-28 05:53:51 +00003455 if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() &&
3456 !Result.isStrictlyPositive()) {
Alexey Bataev568a8332014-03-06 06:15:19 +00003457 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
3458 << "num_threads" << NumThreads->getSourceRange();
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003459 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00003460 }
3461 }
3462
Alexey Bataeved09d242014-05-28 05:53:51 +00003463 return new (Context)
3464 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00003465}
3466
Alexey Bataev62c87d22014-03-21 04:51:18 +00003467ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
3468 OpenMPClauseKind CKind) {
3469 if (!E)
3470 return ExprError();
3471 if (E->isValueDependent() || E->isTypeDependent() ||
3472 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00003473 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00003474 llvm::APSInt Result;
3475 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
3476 if (ICE.isInvalid())
3477 return ExprError();
3478 if (!Result.isStrictlyPositive()) {
3479 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
3480 << getOpenMPClauseName(CKind) << E->getSourceRange();
3481 return ExprError();
3482 }
Alexander Musman09184fe2014-09-30 05:29:28 +00003483 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
3484 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
3485 << E->getSourceRange();
3486 return ExprError();
3487 }
Alexey Bataev62c87d22014-03-21 04:51:18 +00003488 return ICE;
3489}
3490
3491OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
3492 SourceLocation LParenLoc,
3493 SourceLocation EndLoc) {
3494 // OpenMP [2.8.1, simd construct, Description]
3495 // The parameter of the safelen clause must be a constant
3496 // positive integer expression.
3497 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
3498 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003499 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00003500 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003501 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00003502}
3503
Alexander Musman64d33f12014-06-04 07:53:32 +00003504OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
3505 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00003506 SourceLocation LParenLoc,
3507 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00003508 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00003509 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00003510 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00003511 // The parameter of the collapse clause must be a constant
3512 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00003513 ExprResult NumForLoopsResult =
3514 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
3515 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00003516 return nullptr;
3517 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00003518 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00003519}
3520
Alexey Bataeved09d242014-05-28 05:53:51 +00003521OMPClause *Sema::ActOnOpenMPSimpleClause(
3522 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
3523 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003524 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003525 switch (Kind) {
3526 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00003527 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00003528 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
3529 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003530 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003531 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00003532 Res = ActOnOpenMPProcBindClause(
3533 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
3534 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003535 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003536 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00003537 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00003538 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00003539 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00003540 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003541 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003542 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003543 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00003544 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00003545 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00003546 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00003547 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00003548 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003549 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003550 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003551 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00003552 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003553 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003554 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003555 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00003556 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003557 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00003558 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00003559 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00003560 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003561 case OMPC_seq_cst:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003562 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003563 llvm_unreachable("Clause is not allowed.");
3564 }
3565 return Res;
3566}
3567
3568OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
3569 SourceLocation KindKwLoc,
3570 SourceLocation StartLoc,
3571 SourceLocation LParenLoc,
3572 SourceLocation EndLoc) {
3573 if (Kind == OMPC_DEFAULT_unknown) {
3574 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00003575 static_assert(OMPC_DEFAULT_unknown > 0,
3576 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00003577 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00003578 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003579 Values += "'";
3580 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
3581 Values += "'";
3582 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00003583 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003584 Values += " or ";
3585 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00003586 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003587 break;
3588 default:
3589 Values += Sep;
3590 break;
3591 }
3592 }
3593 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00003594 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003595 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003596 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00003597 switch (Kind) {
3598 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003599 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003600 break;
3601 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003602 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00003603 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003604 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003605 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00003606 break;
3607 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003608 return new (Context)
3609 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003610}
3611
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003612OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
3613 SourceLocation KindKwLoc,
3614 SourceLocation StartLoc,
3615 SourceLocation LParenLoc,
3616 SourceLocation EndLoc) {
3617 if (Kind == OMPC_PROC_BIND_unknown) {
3618 std::string Values;
3619 std::string Sep(", ");
3620 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
3621 Values += "'";
3622 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
3623 Values += "'";
3624 switch (i) {
3625 case OMPC_PROC_BIND_unknown - 2:
3626 Values += " or ";
3627 break;
3628 case OMPC_PROC_BIND_unknown - 1:
3629 break;
3630 default:
3631 Values += Sep;
3632 break;
3633 }
3634 }
3635 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00003636 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003637 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003638 }
Alexey Bataeved09d242014-05-28 05:53:51 +00003639 return new (Context)
3640 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003641}
3642
Alexey Bataev56dafe82014-06-20 07:16:17 +00003643OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
3644 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
3645 SourceLocation StartLoc, SourceLocation LParenLoc,
3646 SourceLocation ArgumentLoc, SourceLocation CommaLoc,
3647 SourceLocation EndLoc) {
3648 OMPClause *Res = nullptr;
3649 switch (Kind) {
3650 case OMPC_schedule:
3651 Res = ActOnOpenMPScheduleClause(
3652 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
3653 LParenLoc, ArgumentLoc, CommaLoc, EndLoc);
3654 break;
3655 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00003656 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003657 case OMPC_num_threads:
3658 case OMPC_safelen:
3659 case OMPC_collapse:
3660 case OMPC_default:
3661 case OMPC_proc_bind:
3662 case OMPC_private:
3663 case OMPC_firstprivate:
3664 case OMPC_lastprivate:
3665 case OMPC_shared:
3666 case OMPC_reduction:
3667 case OMPC_linear:
3668 case OMPC_aligned:
3669 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003670 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003671 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00003672 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003673 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003674 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003675 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00003676 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003677 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00003678 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00003679 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00003680 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003681 case OMPC_seq_cst:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003682 case OMPC_unknown:
3683 llvm_unreachable("Clause is not allowed.");
3684 }
3685 return Res;
3686}
3687
3688OMPClause *Sema::ActOnOpenMPScheduleClause(
3689 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
3690 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
3691 SourceLocation EndLoc) {
3692 if (Kind == OMPC_SCHEDULE_unknown) {
3693 std::string Values;
3694 std::string Sep(", ");
3695 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) {
3696 Values += "'";
3697 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i);
3698 Values += "'";
3699 switch (i) {
3700 case OMPC_SCHEDULE_unknown - 2:
3701 Values += " or ";
3702 break;
3703 case OMPC_SCHEDULE_unknown - 1:
3704 break;
3705 default:
3706 Values += Sep;
3707 break;
3708 }
3709 }
3710 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
3711 << Values << getOpenMPClauseName(OMPC_schedule);
3712 return nullptr;
3713 }
3714 Expr *ValExpr = ChunkSize;
3715 if (ChunkSize) {
3716 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
3717 !ChunkSize->isInstantiationDependent() &&
3718 !ChunkSize->containsUnexpandedParameterPack()) {
3719 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
3720 ExprResult Val =
3721 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
3722 if (Val.isInvalid())
3723 return nullptr;
3724
3725 ValExpr = Val.get();
3726
3727 // OpenMP [2.7.1, Restrictions]
3728 // chunk_size must be a loop invariant integer expression with a positive
3729 // value.
3730 llvm::APSInt Result;
3731 if (ValExpr->isIntegerConstantExpr(Result, Context) &&
3732 Result.isSigned() && !Result.isStrictlyPositive()) {
3733 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
3734 << "schedule" << ChunkSize->getSourceRange();
3735 return nullptr;
3736 }
3737 }
3738 }
3739
3740 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc,
3741 EndLoc, Kind, ValExpr);
3742}
3743
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003744OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
3745 SourceLocation StartLoc,
3746 SourceLocation EndLoc) {
3747 OMPClause *Res = nullptr;
3748 switch (Kind) {
3749 case OMPC_ordered:
3750 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
3751 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00003752 case OMPC_nowait:
3753 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
3754 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003755 case OMPC_untied:
3756 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
3757 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003758 case OMPC_mergeable:
3759 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
3760 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003761 case OMPC_read:
3762 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
3763 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00003764 case OMPC_write:
3765 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
3766 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00003767 case OMPC_update:
3768 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
3769 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00003770 case OMPC_capture:
3771 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
3772 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003773 case OMPC_seq_cst:
3774 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
3775 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003776 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00003777 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003778 case OMPC_num_threads:
3779 case OMPC_safelen:
3780 case OMPC_collapse:
3781 case OMPC_schedule:
3782 case OMPC_private:
3783 case OMPC_firstprivate:
3784 case OMPC_lastprivate:
3785 case OMPC_shared:
3786 case OMPC_reduction:
3787 case OMPC_linear:
3788 case OMPC_aligned:
3789 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00003790 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003791 case OMPC_default:
3792 case OMPC_proc_bind:
3793 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00003794 case OMPC_flush:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003795 case OMPC_unknown:
3796 llvm_unreachable("Clause is not allowed.");
3797 }
3798 return Res;
3799}
3800
3801OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
3802 SourceLocation EndLoc) {
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003803 DSAStack->setOrderedRegion();
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003804 return new (Context) OMPOrderedClause(StartLoc, EndLoc);
3805}
3806
Alexey Bataev236070f2014-06-20 11:19:47 +00003807OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
3808 SourceLocation EndLoc) {
3809 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
3810}
3811
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003812OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
3813 SourceLocation EndLoc) {
3814 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
3815}
3816
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003817OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
3818 SourceLocation EndLoc) {
3819 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
3820}
3821
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003822OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
3823 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003824 return new (Context) OMPReadClause(StartLoc, EndLoc);
3825}
3826
Alexey Bataevdea47612014-07-23 07:46:59 +00003827OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
3828 SourceLocation EndLoc) {
3829 return new (Context) OMPWriteClause(StartLoc, EndLoc);
3830}
3831
Alexey Bataev67a4f222014-07-23 10:25:33 +00003832OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
3833 SourceLocation EndLoc) {
3834 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
3835}
3836
Alexey Bataev459dec02014-07-24 06:46:57 +00003837OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
3838 SourceLocation EndLoc) {
3839 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
3840}
3841
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003842OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
3843 SourceLocation EndLoc) {
3844 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
3845}
3846
Alexey Bataevc5e02582014-06-16 07:08:35 +00003847OMPClause *Sema::ActOnOpenMPVarListClause(
3848 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
3849 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
3850 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
3851 const DeclarationNameInfo &ReductionId) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003852 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003853 switch (Kind) {
3854 case OMPC_private:
3855 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
3856 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003857 case OMPC_firstprivate:
3858 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
3859 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00003860 case OMPC_lastprivate:
3861 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
3862 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00003863 case OMPC_shared:
3864 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
3865 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00003866 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00003867 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
3868 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00003869 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00003870 case OMPC_linear:
3871 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
3872 ColonLoc, EndLoc);
3873 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00003874 case OMPC_aligned:
3875 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
3876 ColonLoc, EndLoc);
3877 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00003878 case OMPC_copyin:
3879 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
3880 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00003881 case OMPC_copyprivate:
3882 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
3883 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003884 case OMPC_flush:
3885 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
3886 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00003887 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00003888 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00003889 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00003890 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00003891 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003892 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00003893 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00003894 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00003895 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00003896 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00003897 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00003898 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003899 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003900 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00003901 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00003902 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00003903 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00003904 case OMPC_seq_cst:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003905 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003906 llvm_unreachable("Clause is not allowed.");
3907 }
3908 return Res;
3909}
3910
3911OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
3912 SourceLocation StartLoc,
3913 SourceLocation LParenLoc,
3914 SourceLocation EndLoc) {
3915 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00003916 for (auto &RefExpr : VarList) {
3917 assert(RefExpr && "NULL expr in OpenMP private clause.");
3918 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003919 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00003920 Vars.push_back(RefExpr);
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);
3942 continue;
3943 }
3944
3945 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
3946 // A variable that appears in a private clause must not have an incomplete
3947 // type or a reference type.
3948 if (RequireCompleteType(ELoc, Type,
3949 diag::err_omp_private_incomplete_type)) {
3950 continue;
3951 }
3952 if (Type->isReferenceType()) {
3953 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00003954 << getOpenMPClauseName(OMPC_private) << Type;
3955 bool IsDecl =
3956 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
3957 Diag(VD->getLocation(),
3958 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3959 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003960 continue;
3961 }
3962
3963 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
3964 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00003965 // clause requires an accessible, unambiguous default constructor for the
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003966 // class type.
3967 while (Type.getNonReferenceType()->isArrayType()) {
Alexey Bataeved09d242014-05-28 05:53:51 +00003968 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
3969 ->getElementType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003970 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00003971 CXXRecordDecl *RD = getLangOpts().CPlusPlus
3972 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
3973 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00003974 // FIXME This code must be replaced by actual constructing/destructing of
3975 // the private variable.
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003976 if (RD) {
3977 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
3978 PartialDiagnostic PD =
Alexey Bataeved09d242014-05-28 05:53:51 +00003979 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataevf29276e2014-06-18 04:14:57 +00003980 if (!CD ||
3981 CheckConstructorAccess(ELoc, CD,
3982 InitializedEntity::InitializeTemporary(Type),
3983 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003984 CD->isDeleted()) {
3985 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00003986 << getOpenMPClauseName(OMPC_private) << 0;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003987 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
3988 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00003989 Diag(VD->getLocation(),
3990 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
3991 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003992 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
3993 continue;
3994 }
3995 MarkFunctionReferenced(ELoc, CD);
3996 DiagnoseUseOfDecl(CD, ELoc);
3997
3998 CXXDestructorDecl *DD = RD->getDestructor();
3999 if (DD) {
4000 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
4001 DD->isDeleted()) {
4002 Diag(ELoc, diag::err_omp_required_method)
Alexey Bataeved09d242014-05-28 05:53:51 +00004003 << getOpenMPClauseName(OMPC_private) << 4;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004004 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4005 VarDecl::DeclarationOnly;
Alexey Bataeved09d242014-05-28 05:53:51 +00004006 Diag(VD->getLocation(),
4007 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4008 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004009 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4010 continue;
4011 }
4012 MarkFunctionReferenced(ELoc, DD);
4013 DiagnoseUseOfDecl(DD, ELoc);
4014 }
4015 }
4016
Alexey Bataev758e55e2013-09-06 18:03:48 +00004017 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
4018 // in a Construct]
4019 // Variables with the predetermined data-sharing attributes may not be
4020 // listed in data-sharing attributes clauses, except for the cases
4021 // listed below. For these exceptions only, listing a predetermined
4022 // variable in a data-sharing attribute clause is allowed and overrides
4023 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004024 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004025 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00004026 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
4027 << getOpenMPClauseName(OMPC_private);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004028 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004029 continue;
4030 }
4031
4032 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004033 Vars.push_back(DE);
4034 }
4035
Alexey Bataeved09d242014-05-28 05:53:51 +00004036 if (Vars.empty())
4037 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004038
4039 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
4040}
4041
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004042namespace {
4043class DiagsUninitializedSeveretyRAII {
4044private:
4045 DiagnosticsEngine &Diags;
4046 SourceLocation SavedLoc;
4047 bool IsIgnored;
4048
4049public:
4050 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
4051 bool IsIgnored)
4052 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
4053 if (!IsIgnored) {
4054 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
4055 /*Map*/ diag::Severity::Ignored, Loc);
4056 }
4057 }
4058 ~DiagsUninitializedSeveretyRAII() {
4059 if (!IsIgnored)
4060 Diags.popMappings(SavedLoc);
4061 }
4062};
4063}
4064
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004065OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
4066 SourceLocation StartLoc,
4067 SourceLocation LParenLoc,
4068 SourceLocation EndLoc) {
4069 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004070 SmallVector<Expr *, 8> PrivateCopies;
4071 SmallVector<Expr *, 8> Inits;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004072 bool IsImplicitClause =
4073 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
4074 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
4075
Alexey Bataeved09d242014-05-28 05:53:51 +00004076 for (auto &RefExpr : VarList) {
4077 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
4078 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004079 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00004080 Vars.push_back(RefExpr);
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
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004086 SourceLocation ELoc =
4087 IsImplicitClause ? ImplicitClauseLoc : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004088 // OpenMP [2.1, C/C++]
4089 // A list item is a variable name.
4090 // OpenMP [2.9.3.3, Restrictions, p.1]
4091 // A variable that is part of another variable (as an array or
4092 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00004093 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004094 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00004095 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004096 continue;
4097 }
4098 Decl *D = DE->getDecl();
4099 VarDecl *VD = cast<VarDecl>(D);
4100
4101 QualType Type = VD->getType();
4102 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
4103 // It will be analyzed later.
4104 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004105 PrivateCopies.push_back(nullptr);
4106 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004107 continue;
4108 }
4109
4110 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
4111 // A variable that appears in a private clause must not have an incomplete
4112 // type or a reference type.
4113 if (RequireCompleteType(ELoc, Type,
4114 diag::err_omp_firstprivate_incomplete_type)) {
4115 continue;
4116 }
4117 if (Type->isReferenceType()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004118 if (IsImplicitClause) {
4119 Diag(ImplicitClauseLoc,
4120 diag::err_omp_task_predetermined_firstprivate_ref_type_arg)
4121 << Type;
4122 Diag(RefExpr->getExprLoc(), diag::note_used_here);
4123 } else {
4124 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
4125 << getOpenMPClauseName(OMPC_firstprivate) << Type;
4126 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004127 bool IsDecl =
4128 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4129 Diag(VD->getLocation(),
4130 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4131 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004132 continue;
4133 }
4134
4135 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
4136 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00004137 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004138 // class type.
4139 Type = Context.getBaseElementType(Type);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004140
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004141 // If an implicit firstprivate variable found it was checked already.
4142 if (!IsImplicitClause) {
4143 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004144 Type = Type.getNonReferenceType().getCanonicalType();
4145 bool IsConstant = Type.isConstant(Context);
4146 Type = Context.getBaseElementType(Type);
4147 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
4148 // A list item that specifies a given variable may not appear in more
4149 // than one clause on the same directive, except that a variable may be
4150 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004151 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00004152 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004153 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00004154 << getOpenMPClauseName(DVar.CKind)
4155 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004156 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004157 continue;
4158 }
4159
4160 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
4161 // in a Construct]
4162 // Variables with the predetermined data-sharing attributes may not be
4163 // listed in data-sharing attributes clauses, except for the cases
4164 // listed below. For these exceptions only, listing a predetermined
4165 // variable in a data-sharing attribute clause is allowed and overrides
4166 // the variable's predetermined data-sharing attributes.
4167 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
4168 // in a Construct, C/C++, p.2]
4169 // Variables with const-qualified type having no mutable member may be
4170 // listed in a firstprivate clause, even if they are static data members.
4171 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
4172 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
4173 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00004174 << getOpenMPClauseName(DVar.CKind)
4175 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004176 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004177 continue;
4178 }
4179
Alexey Bataevf29276e2014-06-18 04:14:57 +00004180 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004181 // OpenMP [2.9.3.4, Restrictions, p.2]
4182 // A list item that is private within a parallel region must not appear
4183 // in a firstprivate clause on a worksharing construct if any of the
4184 // worksharing regions arising from the worksharing construct ever bind
4185 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00004186 if (isOpenMPWorksharingDirective(CurrDir) &&
4187 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004188 DVar = DSAStack->getImplicitDSA(VD, true);
4189 if (DVar.CKind != OMPC_shared &&
4190 (isOpenMPParallelDirective(DVar.DKind) ||
4191 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00004192 Diag(ELoc, diag::err_omp_required_access)
4193 << getOpenMPClauseName(OMPC_firstprivate)
4194 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004195 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004196 continue;
4197 }
4198 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004199 // OpenMP [2.9.3.4, Restrictions, p.3]
4200 // A list item that appears in a reduction clause of a parallel construct
4201 // must not appear in a firstprivate clause on a worksharing or task
4202 // construct if any of the worksharing or task regions arising from the
4203 // worksharing or task construct ever bind to any of the parallel regions
4204 // arising from the parallel construct.
4205 // OpenMP [2.9.3.4, Restrictions, p.4]
4206 // A list item that appears in a reduction clause in worksharing
4207 // construct must not appear in a firstprivate clause in a task construct
4208 // encountered during execution of any of the worksharing regions arising
4209 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004210 if (CurrDir == OMPD_task) {
4211 DVar =
4212 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
4213 [](OpenMPDirectiveKind K) -> bool {
4214 return isOpenMPParallelDirective(K) ||
4215 isOpenMPWorksharingDirective(K);
4216 },
4217 false);
4218 if (DVar.CKind == OMPC_reduction &&
4219 (isOpenMPParallelDirective(DVar.DKind) ||
4220 isOpenMPWorksharingDirective(DVar.DKind))) {
4221 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
4222 << getOpenMPDirectiveName(DVar.DKind);
4223 ReportOriginalDSA(*this, DSAStack, VD, DVar);
4224 continue;
4225 }
4226 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004227 }
4228
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004229 Type = Type.getUnqualifiedType();
4230 auto VDPrivate = VarDecl::Create(Context, CurContext, DE->getLocStart(),
4231 ELoc, VD->getIdentifier(), VD->getType(),
4232 VD->getTypeSourceInfo(), /*S*/ SC_Auto);
4233 // Generate helper private variable and initialize it with the value of the
4234 // original variable. The address of the original variable is replaced by
4235 // the address of the new private variable in the CodeGen. This new variable
4236 // is not added to IdResolver, so the code in the OpenMP region uses
4237 // original variable for proper diagnostics and variable capturing.
4238 Expr *VDInitRefExpr = nullptr;
4239 // For arrays generate initializer for single element and replace it by the
4240 // original array element in CodeGen.
4241 if (DE->getType()->isArrayType()) {
4242 auto VDInit = VarDecl::Create(Context, CurContext, DE->getLocStart(),
4243 ELoc, VD->getIdentifier(), Type,
4244 VD->getTypeSourceInfo(), /*S*/ SC_Auto);
4245 CurContext->addHiddenDecl(VDInit);
4246 VDInitRefExpr = DeclRefExpr::Create(
4247 Context, /*QualifierLoc*/ NestedNameSpecifierLoc(),
4248 /*TemplateKWLoc*/ SourceLocation(), VDInit,
4249 /*isEnclosingLocal*/ false, ELoc, Type,
4250 /*VK*/ VK_LValue);
4251 VDInit->setIsUsed();
4252 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
4253 InitializedEntity Entity = InitializedEntity::InitializeVariable(VDInit);
4254 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
4255
4256 InitializationSequence InitSeq(*this, Entity, Kind, Init);
4257 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
4258 if (Result.isInvalid())
4259 VDPrivate->setInvalidDecl();
4260 else
4261 VDPrivate->setInit(Result.getAs<Expr>());
4262 } else {
4263 AddInitializerToDecl(VDPrivate, DefaultLvalueConversion(DE).get(),
4264 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
4265 }
4266 if (VDPrivate->isInvalidDecl()) {
4267 if (IsImplicitClause) {
4268 Diag(DE->getExprLoc(),
4269 diag::note_omp_task_predetermined_firstprivate_here);
4270 }
4271 continue;
4272 }
4273 CurContext->addDecl(VDPrivate);
4274 auto VDPrivateRefExpr = DeclRefExpr::Create(
4275 Context, /*QualifierLoc*/ NestedNameSpecifierLoc(),
4276 /*TemplateKWLoc*/ SourceLocation(), VDPrivate,
4277 /*isEnclosingLocal*/ false, DE->getLocStart(), DE->getType(),
4278 /*VK*/ VK_LValue);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004279 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
4280 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004281 PrivateCopies.push_back(VDPrivateRefExpr);
4282 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004283 }
4284
Alexey Bataeved09d242014-05-28 05:53:51 +00004285 if (Vars.empty())
4286 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004287
4288 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev4a5bb772014-10-08 14:01:46 +00004289 Vars, PrivateCopies, Inits);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004290}
4291
Alexander Musman1bb328c2014-06-04 13:06:39 +00004292OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
4293 SourceLocation StartLoc,
4294 SourceLocation LParenLoc,
4295 SourceLocation EndLoc) {
4296 SmallVector<Expr *, 8> Vars;
4297 for (auto &RefExpr : VarList) {
4298 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
4299 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
4300 // It will be analyzed later.
4301 Vars.push_back(RefExpr);
4302 continue;
4303 }
4304
4305 SourceLocation ELoc = RefExpr->getExprLoc();
4306 // OpenMP [2.1, C/C++]
4307 // A list item is a variable name.
4308 // OpenMP [2.14.3.5, Restrictions, p.1]
4309 // A variable that is part of another variable (as an array or structure
4310 // element) cannot appear in a lastprivate clause.
4311 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
4312 if (!DE || !isa<VarDecl>(DE->getDecl())) {
4313 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
4314 continue;
4315 }
4316 Decl *D = DE->getDecl();
4317 VarDecl *VD = cast<VarDecl>(D);
4318
4319 QualType Type = VD->getType();
4320 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
4321 // It will be analyzed later.
4322 Vars.push_back(DE);
4323 continue;
4324 }
4325
4326 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
4327 // A variable that appears in a lastprivate clause must not have an
4328 // incomplete type or a reference type.
4329 if (RequireCompleteType(ELoc, Type,
4330 diag::err_omp_lastprivate_incomplete_type)) {
4331 continue;
4332 }
4333 if (Type->isReferenceType()) {
4334 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
4335 << getOpenMPClauseName(OMPC_lastprivate) << Type;
4336 bool IsDecl =
4337 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4338 Diag(VD->getLocation(),
4339 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4340 << VD;
4341 continue;
4342 }
4343
4344 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4345 // in a Construct]
4346 // Variables with the predetermined data-sharing attributes may not be
4347 // listed in data-sharing attributes clauses, except for the cases
4348 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004349 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00004350 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
4351 DVar.CKind != OMPC_firstprivate &&
4352 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
4353 Diag(ELoc, diag::err_omp_wrong_dsa)
4354 << getOpenMPClauseName(DVar.CKind)
4355 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004356 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00004357 continue;
4358 }
4359
Alexey Bataevf29276e2014-06-18 04:14:57 +00004360 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
4361 // OpenMP [2.14.3.5, Restrictions, p.2]
4362 // A list item that is private within a parallel region, or that appears in
4363 // the reduction clause of a parallel construct, must not appear in a
4364 // lastprivate clause on a worksharing construct if any of the corresponding
4365 // worksharing regions ever binds to any of the corresponding parallel
4366 // regions.
Alexey Bataev549210e2014-06-24 04:39:47 +00004367 if (isOpenMPWorksharingDirective(CurrDir) &&
4368 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004369 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004370 if (DVar.CKind != OMPC_shared) {
4371 Diag(ELoc, diag::err_omp_required_access)
4372 << getOpenMPClauseName(OMPC_lastprivate)
4373 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004374 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004375 continue;
4376 }
4377 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00004378 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00004379 // A variable of class type (or array thereof) that appears in a
4380 // lastprivate clause requires an accessible, unambiguous default
4381 // constructor for the class type, unless the list item is also specified
4382 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00004383 // A variable of class type (or array thereof) that appears in a
4384 // lastprivate clause requires an accessible, unambiguous copy assignment
4385 // operator for the class type.
4386 while (Type.getNonReferenceType()->isArrayType())
4387 Type = cast<ArrayType>(Type.getNonReferenceType().getTypePtr())
4388 ->getElementType();
4389 CXXRecordDecl *RD = getLangOpts().CPlusPlus
4390 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
4391 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00004392 // FIXME This code must be replaced by actual copying and destructing of the
4393 // lastprivate variable.
Alexander Musman1bb328c2014-06-04 13:06:39 +00004394 if (RD) {
Alexander Musman1bb328c2014-06-04 13:06:39 +00004395 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
4396 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00004397 if (MD) {
4398 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
4399 MD->isDeleted()) {
4400 Diag(ELoc, diag::err_omp_required_method)
4401 << getOpenMPClauseName(OMPC_lastprivate) << 2;
4402 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4403 VarDecl::DeclarationOnly;
4404 Diag(VD->getLocation(),
4405 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4406 << VD;
4407 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4408 continue;
4409 }
4410 MarkFunctionReferenced(ELoc, MD);
4411 DiagnoseUseOfDecl(MD, ELoc);
Alexander Musman1bb328c2014-06-04 13:06:39 +00004412 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00004413
4414 CXXDestructorDecl *DD = RD->getDestructor();
4415 if (DD) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00004416 PartialDiagnostic PD =
4417 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexander Musman1bb328c2014-06-04 13:06:39 +00004418 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
4419 DD->isDeleted()) {
4420 Diag(ELoc, diag::err_omp_required_method)
4421 << getOpenMPClauseName(OMPC_lastprivate) << 4;
4422 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4423 VarDecl::DeclarationOnly;
4424 Diag(VD->getLocation(),
4425 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4426 << VD;
4427 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4428 continue;
4429 }
4430 MarkFunctionReferenced(ELoc, DD);
4431 DiagnoseUseOfDecl(DD, ELoc);
4432 }
4433 }
4434
Alexey Bataevf29276e2014-06-18 04:14:57 +00004435 if (DVar.CKind != OMPC_firstprivate)
4436 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00004437 Vars.push_back(DE);
4438 }
4439
4440 if (Vars.empty())
4441 return nullptr;
4442
4443 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
4444 Vars);
4445}
4446
Alexey Bataev758e55e2013-09-06 18:03:48 +00004447OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
4448 SourceLocation StartLoc,
4449 SourceLocation LParenLoc,
4450 SourceLocation EndLoc) {
4451 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00004452 for (auto &RefExpr : VarList) {
4453 assert(RefExpr && "NULL expr in OpenMP shared clause.");
4454 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00004455 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00004456 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004457 continue;
4458 }
4459
Alexey Bataeved09d242014-05-28 05:53:51 +00004460 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00004461 // OpenMP [2.1, C/C++]
4462 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00004463 // OpenMP [2.14.3.2, Restrictions, p.1]
4464 // A variable that is part of another variable (as an array or structure
4465 // element) cannot appear in a shared unless it is a static data member
4466 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00004467 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004468 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00004469 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00004470 continue;
4471 }
4472 Decl *D = DE->getDecl();
4473 VarDecl *VD = cast<VarDecl>(D);
4474
4475 QualType Type = VD->getType();
4476 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
4477 // It will be analyzed later.
4478 Vars.push_back(DE);
4479 continue;
4480 }
4481
4482 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
4483 // in a Construct]
4484 // Variables with the predetermined data-sharing attributes may not be
4485 // listed in data-sharing attributes clauses, except for the cases
4486 // listed below. For these exceptions only, listing a predetermined
4487 // variable in a data-sharing attribute clause is allowed and overrides
4488 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004489 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00004490 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
4491 DVar.RefExpr) {
4492 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
4493 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004494 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004495 continue;
4496 }
4497
4498 DSAStack->addDSA(VD, DE, OMPC_shared);
4499 Vars.push_back(DE);
4500 }
4501
Alexey Bataeved09d242014-05-28 05:53:51 +00004502 if (Vars.empty())
4503 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00004504
4505 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
4506}
4507
Alexey Bataevc5e02582014-06-16 07:08:35 +00004508namespace {
4509class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
4510 DSAStackTy *Stack;
4511
4512public:
4513 bool VisitDeclRefExpr(DeclRefExpr *E) {
4514 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004515 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00004516 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
4517 return false;
4518 if (DVar.CKind != OMPC_unknown)
4519 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00004520 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004521 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004522 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00004523 return true;
4524 return false;
4525 }
4526 return false;
4527 }
4528 bool VisitStmt(Stmt *S) {
4529 for (auto Child : S->children()) {
4530 if (Child && Visit(Child))
4531 return true;
4532 }
4533 return false;
4534 }
Alexey Bataev23b69422014-06-18 07:08:49 +00004535 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00004536};
Alexey Bataev23b69422014-06-18 07:08:49 +00004537} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00004538
4539OMPClause *Sema::ActOnOpenMPReductionClause(
4540 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
4541 SourceLocation ColonLoc, SourceLocation EndLoc,
4542 CXXScopeSpec &ReductionIdScopeSpec,
4543 const DeclarationNameInfo &ReductionId) {
4544 // TODO: Allow scope specification search when 'declare reduction' is
4545 // supported.
4546 assert(ReductionIdScopeSpec.isEmpty() &&
4547 "No support for scoped reduction identifiers yet.");
4548
4549 auto DN = ReductionId.getName();
4550 auto OOK = DN.getCXXOverloadedOperator();
4551 BinaryOperatorKind BOK = BO_Comma;
4552
4553 // OpenMP [2.14.3.6, reduction clause]
4554 // C
4555 // reduction-identifier is either an identifier or one of the following
4556 // operators: +, -, *, &, |, ^, && and ||
4557 // C++
4558 // reduction-identifier is either an id-expression or one of the following
4559 // operators: +, -, *, &, |, ^, && and ||
4560 // FIXME: Only 'min' and 'max' identifiers are supported for now.
4561 switch (OOK) {
4562 case OO_Plus:
4563 case OO_Minus:
4564 BOK = BO_AddAssign;
4565 break;
4566 case OO_Star:
4567 BOK = BO_MulAssign;
4568 break;
4569 case OO_Amp:
4570 BOK = BO_AndAssign;
4571 break;
4572 case OO_Pipe:
4573 BOK = BO_OrAssign;
4574 break;
4575 case OO_Caret:
4576 BOK = BO_XorAssign;
4577 break;
4578 case OO_AmpAmp:
4579 BOK = BO_LAnd;
4580 break;
4581 case OO_PipePipe:
4582 BOK = BO_LOr;
4583 break;
4584 default:
4585 if (auto II = DN.getAsIdentifierInfo()) {
4586 if (II->isStr("max"))
4587 BOK = BO_GT;
4588 else if (II->isStr("min"))
4589 BOK = BO_LT;
4590 }
4591 break;
4592 }
4593 SourceRange ReductionIdRange;
4594 if (ReductionIdScopeSpec.isValid()) {
4595 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
4596 }
4597 ReductionIdRange.setEnd(ReductionId.getEndLoc());
4598 if (BOK == BO_Comma) {
4599 // Not allowed reduction identifier is found.
4600 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
4601 << ReductionIdRange;
4602 return nullptr;
4603 }
4604
4605 SmallVector<Expr *, 8> Vars;
4606 for (auto RefExpr : VarList) {
4607 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
4608 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
4609 // It will be analyzed later.
4610 Vars.push_back(RefExpr);
4611 continue;
4612 }
4613
4614 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
4615 RefExpr->isInstantiationDependent() ||
4616 RefExpr->containsUnexpandedParameterPack()) {
4617 // It will be analyzed later.
4618 Vars.push_back(RefExpr);
4619 continue;
4620 }
4621
4622 auto ELoc = RefExpr->getExprLoc();
4623 auto ERange = RefExpr->getSourceRange();
4624 // OpenMP [2.1, C/C++]
4625 // A list item is a variable or array section, subject to the restrictions
4626 // specified in Section 2.4 on page 42 and in each of the sections
4627 // describing clauses and directives for which a list appears.
4628 // OpenMP [2.14.3.3, Restrictions, p.1]
4629 // A variable that is part of another variable (as an array or
4630 // structure element) cannot appear in a private clause.
4631 auto DE = dyn_cast<DeclRefExpr>(RefExpr);
4632 if (!DE || !isa<VarDecl>(DE->getDecl())) {
4633 Diag(ELoc, diag::err_omp_expected_var_name) << ERange;
4634 continue;
4635 }
4636 auto D = DE->getDecl();
4637 auto VD = cast<VarDecl>(D);
4638 auto Type = VD->getType();
4639 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
4640 // A variable that appears in a private clause must not have an incomplete
4641 // type or a reference type.
4642 if (RequireCompleteType(ELoc, Type,
4643 diag::err_omp_reduction_incomplete_type))
4644 continue;
4645 // OpenMP [2.14.3.6, reduction clause, Restrictions]
4646 // Arrays may not appear in a reduction clause.
4647 if (Type.getNonReferenceType()->isArrayType()) {
4648 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
4649 bool IsDecl =
4650 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4651 Diag(VD->getLocation(),
4652 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4653 << VD;
4654 continue;
4655 }
4656 // OpenMP [2.14.3.6, reduction clause, Restrictions]
4657 // A list item that appears in a reduction clause must not be
4658 // const-qualified.
4659 if (Type.getNonReferenceType().isConstant(Context)) {
4660 Diag(ELoc, diag::err_omp_const_variable)
4661 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
4662 bool IsDecl =
4663 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4664 Diag(VD->getLocation(),
4665 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4666 << VD;
4667 continue;
4668 }
4669 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
4670 // If a list-item is a reference type then it must bind to the same object
4671 // for all threads of the team.
4672 VarDecl *VDDef = VD->getDefinition();
4673 if (Type->isReferenceType() && VDDef) {
4674 DSARefChecker Check(DSAStack);
4675 if (Check.Visit(VDDef->getInit())) {
4676 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
4677 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
4678 continue;
4679 }
4680 }
4681 // OpenMP [2.14.3.6, reduction clause, Restrictions]
4682 // The type of a list item that appears in a reduction clause must be valid
4683 // for the reduction-identifier. For a max or min reduction in C, the type
4684 // of the list item must be an allowed arithmetic data type: char, int,
4685 // float, double, or _Bool, possibly modified with long, short, signed, or
4686 // unsigned. For a max or min reduction in C++, the type of the list item
4687 // must be an allowed arithmetic data type: char, wchar_t, int, float,
4688 // double, or bool, possibly modified with long, short, signed, or unsigned.
4689 if ((BOK == BO_GT || BOK == BO_LT) &&
4690 !(Type->isScalarType() ||
4691 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
4692 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
4693 << getLangOpts().CPlusPlus;
4694 bool IsDecl =
4695 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4696 Diag(VD->getLocation(),
4697 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4698 << VD;
4699 continue;
4700 }
4701 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
4702 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
4703 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
4704 bool IsDecl =
4705 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4706 Diag(VD->getLocation(),
4707 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4708 << VD;
4709 continue;
4710 }
4711 bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
4712 getDiagnostics().setSuppressAllDiagnostics(true);
4713 ExprResult ReductionOp =
4714 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
4715 RefExpr, RefExpr);
4716 getDiagnostics().setSuppressAllDiagnostics(Suppress);
4717 if (ReductionOp.isInvalid()) {
4718 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
Alexey Bataev23b69422014-06-18 07:08:49 +00004719 << ReductionIdRange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00004720 bool IsDecl =
4721 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4722 Diag(VD->getLocation(),
4723 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4724 << VD;
4725 continue;
4726 }
4727
4728 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4729 // in a Construct]
4730 // Variables with the predetermined data-sharing attributes may not be
4731 // listed in data-sharing attributes clauses, except for the cases
4732 // listed below. For these exceptions only, listing a predetermined
4733 // variable in a data-sharing attribute clause is allowed and overrides
4734 // the variable's predetermined data-sharing attributes.
4735 // OpenMP [2.14.3.6, Restrictions, p.3]
4736 // Any number of reduction clauses can be specified on the directive,
4737 // but a list item can appear only once in the reduction clauses for that
4738 // directive.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004739 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00004740 if (DVar.CKind == OMPC_reduction) {
4741 Diag(ELoc, diag::err_omp_once_referenced)
4742 << getOpenMPClauseName(OMPC_reduction);
4743 if (DVar.RefExpr) {
4744 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
4745 }
4746 } else if (DVar.CKind != OMPC_unknown) {
4747 Diag(ELoc, diag::err_omp_wrong_dsa)
4748 << getOpenMPClauseName(DVar.CKind)
4749 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004750 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00004751 continue;
4752 }
4753
4754 // OpenMP [2.14.3.6, Restrictions, p.1]
4755 // A list item that appears in a reduction clause of a worksharing
4756 // construct must be shared in the parallel regions to which any of the
4757 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf29276e2014-06-18 04:14:57 +00004758 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataev549210e2014-06-24 04:39:47 +00004759 if (isOpenMPWorksharingDirective(CurrDir) &&
4760 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004761 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004762 if (DVar.CKind != OMPC_shared) {
4763 Diag(ELoc, diag::err_omp_required_access)
4764 << getOpenMPClauseName(OMPC_reduction)
4765 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004766 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00004767 continue;
4768 }
4769 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00004770
4771 CXXRecordDecl *RD = getLangOpts().CPlusPlus
4772 ? Type.getNonReferenceType()->getAsCXXRecordDecl()
4773 : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00004774 // FIXME This code must be replaced by actual constructing/destructing of
4775 // the reduction variable.
Alexey Bataevc5e02582014-06-16 07:08:35 +00004776 if (RD) {
4777 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
4778 PartialDiagnostic PD =
4779 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
Alexey Bataev23b69422014-06-18 07:08:49 +00004780 if (!CD ||
4781 CheckConstructorAccess(ELoc, CD,
4782 InitializedEntity::InitializeTemporary(Type),
4783 CD->getAccess(), PD) == AR_inaccessible ||
Alexey Bataevc5e02582014-06-16 07:08:35 +00004784 CD->isDeleted()) {
4785 Diag(ELoc, diag::err_omp_required_method)
4786 << getOpenMPClauseName(OMPC_reduction) << 0;
4787 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4788 VarDecl::DeclarationOnly;
4789 Diag(VD->getLocation(),
4790 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4791 << VD;
4792 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4793 continue;
4794 }
4795 MarkFunctionReferenced(ELoc, CD);
4796 DiagnoseUseOfDecl(CD, ELoc);
4797
4798 CXXDestructorDecl *DD = RD->getDestructor();
4799 if (DD) {
4800 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
4801 DD->isDeleted()) {
4802 Diag(ELoc, diag::err_omp_required_method)
4803 << getOpenMPClauseName(OMPC_reduction) << 4;
4804 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
4805 VarDecl::DeclarationOnly;
4806 Diag(VD->getLocation(),
4807 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4808 << VD;
4809 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
4810 continue;
4811 }
4812 MarkFunctionReferenced(ELoc, DD);
4813 DiagnoseUseOfDecl(DD, ELoc);
4814 }
4815 }
4816
4817 DSAStack->addDSA(VD, DE, OMPC_reduction);
4818 Vars.push_back(DE);
4819 }
4820
4821 if (Vars.empty())
4822 return nullptr;
4823
4824 return OMPReductionClause::Create(
4825 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
4826 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId);
4827}
4828
Alexander Musman8dba6642014-04-22 13:09:42 +00004829OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step,
4830 SourceLocation StartLoc,
4831 SourceLocation LParenLoc,
4832 SourceLocation ColonLoc,
4833 SourceLocation EndLoc) {
4834 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00004835 for (auto &RefExpr : VarList) {
4836 assert(RefExpr && "NULL expr in OpenMP linear clause.");
4837 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00004838 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00004839 Vars.push_back(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00004840 continue;
4841 }
4842
4843 // OpenMP [2.14.3.7, linear clause]
4844 // A list item that appears in a linear clause is subject to the private
4845 // clause semantics described in Section 2.14.3.3 on page 159 except as
4846 // noted. In addition, the value of the new list item on each iteration
4847 // of the associated loop(s) corresponds to the value of the original
4848 // list item before entering the construct plus the logical number of
4849 // the iteration times linear-step.
4850
Alexey Bataeved09d242014-05-28 05:53:51 +00004851 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00004852 // OpenMP [2.1, C/C++]
4853 // A list item is a variable name.
4854 // OpenMP [2.14.3.3, Restrictions, p.1]
4855 // A variable that is part of another variable (as an array or
4856 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00004857 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00004858 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00004859 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00004860 continue;
4861 }
4862
4863 VarDecl *VD = cast<VarDecl>(DE->getDecl());
4864
4865 // OpenMP [2.14.3.7, linear clause]
4866 // A list-item cannot appear in more than one linear clause.
4867 // A list-item that appears in a linear clause cannot appear in any
4868 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004869 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00004870 if (DVar.RefExpr) {
4871 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
4872 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00004873 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00004874 continue;
4875 }
4876
4877 QualType QType = VD->getType();
4878 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
4879 // It will be analyzed later.
4880 Vars.push_back(DE);
4881 continue;
4882 }
4883
4884 // A variable must not have an incomplete type or a reference type.
4885 if (RequireCompleteType(ELoc, QType,
4886 diag::err_omp_linear_incomplete_type)) {
4887 continue;
4888 }
4889 if (QType->isReferenceType()) {
4890 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
4891 << getOpenMPClauseName(OMPC_linear) << QType;
4892 bool IsDecl =
4893 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4894 Diag(VD->getLocation(),
4895 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4896 << VD;
4897 continue;
4898 }
4899
4900 // A list item must not be const-qualified.
4901 if (QType.isConstant(Context)) {
4902 Diag(ELoc, diag::err_omp_const_variable)
4903 << getOpenMPClauseName(OMPC_linear);
4904 bool IsDecl =
4905 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4906 Diag(VD->getLocation(),
4907 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4908 << VD;
4909 continue;
4910 }
4911
4912 // A list item must be of integral or pointer type.
4913 QType = QType.getUnqualifiedType().getCanonicalType();
4914 const Type *Ty = QType.getTypePtrOrNull();
4915 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
4916 !Ty->isPointerType())) {
4917 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
4918 bool IsDecl =
4919 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4920 Diag(VD->getLocation(),
4921 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4922 << VD;
4923 continue;
4924 }
4925
4926 DSAStack->addDSA(VD, DE, OMPC_linear);
4927 Vars.push_back(DE);
4928 }
4929
4930 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004931 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00004932
4933 Expr *StepExpr = Step;
4934 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
4935 !Step->isInstantiationDependent() &&
4936 !Step->containsUnexpandedParameterPack()) {
4937 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004938 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00004939 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004940 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00004941 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00004942
4943 // Warn about zero linear step (it would be probably better specified as
4944 // making corresponding variables 'const').
4945 llvm::APSInt Result;
4946 if (StepExpr->isIntegerConstantExpr(Result, Context) &&
4947 !Result.isNegative() && !Result.isStrictlyPositive())
4948 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
4949 << (Vars.size() > 1);
4950 }
4951
4952 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc,
4953 Vars, StepExpr);
4954}
4955
Alexander Musmanf0d76e72014-05-29 14:36:25 +00004956OMPClause *Sema::ActOnOpenMPAlignedClause(
4957 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
4958 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
4959
4960 SmallVector<Expr *, 8> Vars;
4961 for (auto &RefExpr : VarList) {
4962 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
4963 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
4964 // It will be analyzed later.
4965 Vars.push_back(RefExpr);
4966 continue;
4967 }
4968
4969 SourceLocation ELoc = RefExpr->getExprLoc();
4970 // OpenMP [2.1, C/C++]
4971 // A list item is a variable name.
4972 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
4973 if (!DE || !isa<VarDecl>(DE->getDecl())) {
4974 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
4975 continue;
4976 }
4977
4978 VarDecl *VD = cast<VarDecl>(DE->getDecl());
4979
4980 // OpenMP [2.8.1, simd construct, Restrictions]
4981 // The type of list items appearing in the aligned clause must be
4982 // array, pointer, reference to array, or reference to pointer.
4983 QualType QType = DE->getType()
4984 .getNonReferenceType()
4985 .getUnqualifiedType()
4986 .getCanonicalType();
4987 const Type *Ty = QType.getTypePtrOrNull();
4988 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
4989 !Ty->isPointerType())) {
4990 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
4991 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
4992 bool IsDecl =
4993 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
4994 Diag(VD->getLocation(),
4995 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
4996 << VD;
4997 continue;
4998 }
4999
5000 // OpenMP [2.8.1, simd construct, Restrictions]
5001 // A list-item cannot appear in more than one aligned clause.
5002 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
5003 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
5004 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
5005 << getOpenMPClauseName(OMPC_aligned);
5006 continue;
5007 }
5008
5009 Vars.push_back(DE);
5010 }
5011
5012 // OpenMP [2.8.1, simd construct, Description]
5013 // The parameter of the aligned clause, alignment, must be a constant
5014 // positive integer expression.
5015 // If no optional parameter is specified, implementation-defined default
5016 // alignments for SIMD instructions on the target platforms are assumed.
5017 if (Alignment != nullptr) {
5018 ExprResult AlignResult =
5019 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
5020 if (AlignResult.isInvalid())
5021 return nullptr;
5022 Alignment = AlignResult.get();
5023 }
5024 if (Vars.empty())
5025 return nullptr;
5026
5027 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
5028 EndLoc, Vars, Alignment);
5029}
5030
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005031OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
5032 SourceLocation StartLoc,
5033 SourceLocation LParenLoc,
5034 SourceLocation EndLoc) {
5035 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00005036 for (auto &RefExpr : VarList) {
5037 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
5038 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005039 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00005040 Vars.push_back(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005041 continue;
5042 }
5043
Alexey Bataeved09d242014-05-28 05:53:51 +00005044 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005045 // OpenMP [2.1, C/C++]
5046 // A list item is a variable name.
5047 // OpenMP [2.14.4.1, Restrictions, p.1]
5048 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00005049 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005050 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005051 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005052 continue;
5053 }
5054
5055 Decl *D = DE->getDecl();
5056 VarDecl *VD = cast<VarDecl>(D);
5057
5058 QualType Type = VD->getType();
5059 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5060 // It will be analyzed later.
5061 Vars.push_back(DE);
5062 continue;
5063 }
5064
5065 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
5066 // A list item that appears in a copyin clause must be threadprivate.
5067 if (!DSAStack->isThreadPrivate(VD)) {
5068 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00005069 << getOpenMPClauseName(OMPC_copyin)
5070 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005071 continue;
5072 }
5073
5074 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
5075 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00005076 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005077 // operator for the class type.
5078 Type = Context.getBaseElementType(Type);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005079 CXXRecordDecl *RD =
5080 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev23b69422014-06-18 07:08:49 +00005081 // FIXME This code must be replaced by actual assignment of the
5082 // threadprivate variable.
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005083 if (RD) {
5084 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
5085 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
Alexey Bataevf29276e2014-06-18 04:14:57 +00005086 if (MD) {
5087 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
5088 MD->isDeleted()) {
5089 Diag(ELoc, diag::err_omp_required_method)
5090 << getOpenMPClauseName(OMPC_copyin) << 2;
5091 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
5092 VarDecl::DeclarationOnly;
5093 Diag(VD->getLocation(),
5094 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5095 << VD;
5096 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
5097 continue;
5098 }
5099 MarkFunctionReferenced(ELoc, MD);
5100 DiagnoseUseOfDecl(MD, ELoc);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005101 }
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005102 }
5103
5104 DSAStack->addDSA(VD, DE, OMPC_copyin);
5105 Vars.push_back(DE);
5106 }
5107
Alexey Bataeved09d242014-05-28 05:53:51 +00005108 if (Vars.empty())
5109 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005110
5111 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
5112}
5113
Alexey Bataevbae9a792014-06-27 10:37:06 +00005114OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
5115 SourceLocation StartLoc,
5116 SourceLocation LParenLoc,
5117 SourceLocation EndLoc) {
5118 SmallVector<Expr *, 8> Vars;
5119 for (auto &RefExpr : VarList) {
5120 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
5121 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
5122 // It will be analyzed later.
5123 Vars.push_back(RefExpr);
5124 continue;
5125 }
5126
5127 SourceLocation ELoc = RefExpr->getExprLoc();
5128 // OpenMP [2.1, C/C++]
5129 // A list item is a variable name.
5130 // OpenMP [2.14.4.1, Restrictions, p.1]
5131 // A list item that appears in a copyin clause must be threadprivate.
5132 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
5133 if (!DE || !isa<VarDecl>(DE->getDecl())) {
5134 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
5135 continue;
5136 }
5137
5138 Decl *D = DE->getDecl();
5139 VarDecl *VD = cast<VarDecl>(D);
5140
5141 QualType Type = VD->getType();
5142 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5143 // It will be analyzed later.
5144 Vars.push_back(DE);
5145 continue;
5146 }
5147
5148 // OpenMP [2.14.4.2, Restrictions, p.2]
5149 // A list item that appears in a copyprivate clause may not appear in a
5150 // private or firstprivate clause on the single construct.
5151 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005152 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00005153 if (DVar.CKind != OMPC_copyprivate && DVar.CKind != OMPC_unknown &&
5154 !(DVar.CKind == OMPC_private && !DVar.RefExpr)) {
5155 Diag(ELoc, diag::err_omp_wrong_dsa)
5156 << getOpenMPClauseName(DVar.CKind)
5157 << getOpenMPClauseName(OMPC_copyprivate);
5158 ReportOriginalDSA(*this, DSAStack, VD, DVar);
5159 continue;
5160 }
5161
5162 // OpenMP [2.11.4.2, Restrictions, p.1]
5163 // All list items that appear in a copyprivate clause must be either
5164 // threadprivate or private in the enclosing context.
5165 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005166 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00005167 if (DVar.CKind == OMPC_shared) {
5168 Diag(ELoc, diag::err_omp_required_access)
5169 << getOpenMPClauseName(OMPC_copyprivate)
5170 << "threadprivate or private in the enclosing context";
5171 ReportOriginalDSA(*this, DSAStack, VD, DVar);
5172 continue;
5173 }
5174 }
5175 }
5176
5177 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
5178 // A variable of class type (or array thereof) that appears in a
5179 // copyin clause requires an accessible, unambiguous copy assignment
5180 // operator for the class type.
5181 Type = Context.getBaseElementType(Type);
5182 CXXRecordDecl *RD =
5183 getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
5184 // FIXME This code must be replaced by actual assignment of the
5185 // threadprivate variable.
5186 if (RD) {
5187 CXXMethodDecl *MD = LookupCopyingAssignment(RD, 0, false, 0);
5188 DeclAccessPair FoundDecl = DeclAccessPair::make(MD, MD->getAccess());
5189 if (MD) {
5190 if (CheckMemberAccess(ELoc, RD, FoundDecl) == AR_inaccessible ||
5191 MD->isDeleted()) {
5192 Diag(ELoc, diag::err_omp_required_method)
5193 << getOpenMPClauseName(OMPC_copyprivate) << 2;
5194 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
5195 VarDecl::DeclarationOnly;
5196 Diag(VD->getLocation(),
5197 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5198 << VD;
5199 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
5200 continue;
5201 }
5202 MarkFunctionReferenced(ELoc, MD);
5203 DiagnoseUseOfDecl(MD, ELoc);
5204 }
5205 }
5206
5207 // No need to mark vars as copyprivate, they are already threadprivate or
5208 // implicitly private.
5209 Vars.push_back(DE);
5210 }
5211
5212 if (Vars.empty())
5213 return nullptr;
5214
5215 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
5216}
5217
Alexey Bataev6125da92014-07-21 11:26:11 +00005218OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
5219 SourceLocation StartLoc,
5220 SourceLocation LParenLoc,
5221 SourceLocation EndLoc) {
5222 if (VarList.empty())
5223 return nullptr;
5224
5225 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
5226}
Alexey Bataevdea47612014-07-23 07:46:59 +00005227