blob: 57ebb38a70cd15ea91351d83d488f65d11dd91bd [file] [log] [blame]
Alexey Bataev9959db52014-05-06 10:08:46 +00001//===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===//
Alexey Bataeva769e072013-03-22 06:34:35 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10/// \brief This file implements semantic analysis for OpenMP directives and
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000011/// clauses.
Alexey Bataeva769e072013-03-22 06:34:35 +000012///
13//===----------------------------------------------------------------------===//
14
Alexey Bataev9959db52014-05-06 10:08:46 +000015#include "clang/AST/ASTContext.h"
Alexey Bataev97720002014-11-11 04:05:39 +000016#include "clang/AST/ASTMutationListener.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000017#include "clang/AST/Decl.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000018#include "clang/AST/DeclCXX.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000019#include "clang/AST/DeclOpenMP.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000020#include "clang/AST/StmtCXX.h"
21#include "clang/AST/StmtOpenMP.h"
22#include "clang/AST/StmtVisitor.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000023#include "clang/Basic/OpenMPKinds.h"
Samuel Antaof8b50122015-07-13 22:54:53 +000024#include "clang/Basic/TargetInfo.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000025#include "clang/Lex/Preprocessor.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000026#include "clang/Sema/Initialization.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000027#include "clang/Sema/Lookup.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000028#include "clang/Sema/Scope.h"
29#include "clang/Sema/ScopeInfo.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000030#include "clang/Sema/SemaInternal.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000031using namespace clang;
32
Alexey Bataev758e55e2013-09-06 18:03:48 +000033//===----------------------------------------------------------------------===//
34// Stack of data-sharing attributes for variables
35//===----------------------------------------------------------------------===//
36
37namespace {
38/// \brief Default data sharing attributes, which can be applied to directive.
39enum DefaultDataSharingAttributes {
Alexey Bataeved09d242014-05-28 05:53:51 +000040 DSA_unspecified = 0, /// \brief Data sharing attribute not specified.
41 DSA_none = 1 << 0, /// \brief Default data sharing attribute 'none'.
42 DSA_shared = 1 << 1 /// \brief Default data sharing attribute 'shared'.
Alexey Bataev758e55e2013-09-06 18:03:48 +000043};
Alexey Bataev7ff55242014-06-19 09:13:45 +000044
Alexey Bataevf29276e2014-06-18 04:14:57 +000045template <class T> struct MatchesAny {
Alexey Bataev23b69422014-06-18 07:08:49 +000046 explicit MatchesAny(ArrayRef<T> Arr) : Arr(std::move(Arr)) {}
Alexey Bataevf29276e2014-06-18 04:14:57 +000047 bool operator()(T Kind) {
48 for (auto KindEl : Arr)
49 if (KindEl == Kind)
50 return true;
51 return false;
52 }
53
54private:
55 ArrayRef<T> Arr;
56};
Alexey Bataev23b69422014-06-18 07:08:49 +000057struct MatchesAlways {
Alexey Bataevf29276e2014-06-18 04:14:57 +000058 MatchesAlways() {}
Alexey Bataev7ff55242014-06-19 09:13:45 +000059 template <class T> bool operator()(T) { return true; }
Alexey Bataevf29276e2014-06-18 04:14:57 +000060};
61
62typedef MatchesAny<OpenMPClauseKind> MatchesAnyClause;
63typedef MatchesAny<OpenMPDirectiveKind> MatchesAnyDirective;
Alexey Bataev758e55e2013-09-06 18:03:48 +000064
65/// \brief Stack for tracking declarations used in OpenMP directives and
66/// clauses and their data-sharing attributes.
67class DSAStackTy {
68public:
69 struct DSAVarData {
70 OpenMPDirectiveKind DKind;
71 OpenMPClauseKind CKind;
72 DeclRefExpr *RefExpr;
Alexey Bataevbae9a792014-06-27 10:37:06 +000073 SourceLocation ImplicitDSALoc;
74 DSAVarData()
75 : DKind(OMPD_unknown), CKind(OMPC_unknown), RefExpr(nullptr),
76 ImplicitDSALoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000077 };
Alexey Bataeved09d242014-05-28 05:53:51 +000078
Alexey Bataev758e55e2013-09-06 18:03:48 +000079private:
80 struct DSAInfo {
81 OpenMPClauseKind Attributes;
82 DeclRefExpr *RefExpr;
83 };
84 typedef llvm::SmallDenseMap<VarDecl *, DSAInfo, 64> DeclSAMapTy;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000085 typedef llvm::SmallDenseMap<VarDecl *, DeclRefExpr *, 64> AlignedMapTy;
Alexey Bataev9c821032015-04-30 04:23:23 +000086 typedef llvm::DenseSet<VarDecl *> LoopControlVariablesSetTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +000087
88 struct SharingMapTy {
89 DeclSAMapTy SharingMap;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000090 AlignedMapTy AlignedMap;
Alexey Bataev9c821032015-04-30 04:23:23 +000091 LoopControlVariablesSetTy LCVSet;
Alexey Bataev758e55e2013-09-06 18:03:48 +000092 DefaultDataSharingAttributes DefaultAttr;
Alexey Bataevbae9a792014-06-27 10:37:06 +000093 SourceLocation DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +000094 OpenMPDirectiveKind Directive;
95 DeclarationNameInfo DirectiveName;
96 Scope *CurScope;
Alexey Bataevbae9a792014-06-27 10:37:06 +000097 SourceLocation ConstructLoc;
Alexey Bataev9fb6e642014-07-22 06:45:04 +000098 bool OrderedRegion;
Alexey Bataev6d4ed052015-07-01 06:57:41 +000099 bool NowaitRegion;
Alexey Bataev9c821032015-04-30 04:23:23 +0000100 unsigned CollapseNumber;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000101 SourceLocation InnerTeamsRegionLoc;
Alexey Bataeved09d242014-05-28 05:53:51 +0000102 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000103 Scope *CurScope, SourceLocation Loc)
Alexey Bataev9c821032015-04-30 04:23:23 +0000104 : SharingMap(), AlignedMap(), LCVSet(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000105 Directive(DKind), DirectiveName(std::move(Name)), CurScope(CurScope),
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000106 ConstructLoc(Loc), OrderedRegion(false), NowaitRegion(false),
107 CollapseNumber(1), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000108 SharingMapTy()
Alexey Bataev9c821032015-04-30 04:23:23 +0000109 : SharingMap(), AlignedMap(), LCVSet(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000110 Directive(OMPD_unknown), DirectiveName(), CurScope(nullptr),
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000111 ConstructLoc(), OrderedRegion(false), NowaitRegion(false),
112 CollapseNumber(1), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000113 };
114
115 typedef SmallVector<SharingMapTy, 64> StackTy;
116
117 /// \brief Stack of used declaration and their data-sharing attributes.
118 StackTy Stack;
Alexey Bataev39f915b82015-05-08 10:41:21 +0000119 /// \brief true, if check for DSA must be from parent directive, false, if
120 /// from current directive.
Alexey Bataevaac108a2015-06-23 04:51:00 +0000121 OpenMPClauseKind ClauseKindMode;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000122 Sema &SemaRef;
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000123 bool ForceCapturing;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000124
125 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
126
127 DSAVarData getDSA(StackTy::reverse_iterator Iter, VarDecl *D);
Alexey Bataevec3da872014-01-31 05:15:34 +0000128
129 /// \brief Checks if the variable is a local for OpenMP region.
130 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
Alexey Bataeved09d242014-05-28 05:53:51 +0000131
Alexey Bataev758e55e2013-09-06 18:03:48 +0000132public:
Alexey Bataevaac108a2015-06-23 04:51:00 +0000133 explicit DSAStackTy(Sema &S)
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000134 : Stack(1), ClauseKindMode(OMPC_unknown), SemaRef(S),
135 ForceCapturing(false) {}
Alexey Bataev39f915b82015-05-08 10:41:21 +0000136
Alexey Bataevaac108a2015-06-23 04:51:00 +0000137 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
138 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000139
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000140 bool isForceVarCapturing() const { return ForceCapturing; }
141 void setForceVarCapturing(bool V) { ForceCapturing = V; }
142
Alexey Bataev758e55e2013-09-06 18:03:48 +0000143 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000144 Scope *CurScope, SourceLocation Loc) {
145 Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc));
146 Stack.back().DefaultAttrLoc = Loc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000147 }
148
149 void pop() {
150 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
151 Stack.pop_back();
152 }
153
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000154 /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000155 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000156 /// for diagnostics.
157 DeclRefExpr *addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE);
158
Alexey Bataev9c821032015-04-30 04:23:23 +0000159 /// \brief Register specified variable as loop control variable.
160 void addLoopControlVariable(VarDecl *D);
161 /// \brief Check if the specified variable is a loop control variable for
162 /// current region.
163 bool isLoopControlVariable(VarDecl *D);
164
Alexey Bataev758e55e2013-09-06 18:03:48 +0000165 /// \brief Adds explicit data sharing attribute to the specified declaration.
166 void addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A);
167
Alexey Bataev758e55e2013-09-06 18:03:48 +0000168 /// \brief Returns data sharing attributes from top of the stack for the
169 /// specified declaration.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000170 DSAVarData getTopDSA(VarDecl *D, bool FromParent);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000171 /// \brief Returns data-sharing attributes for the specified declaration.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000172 DSAVarData getImplicitDSA(VarDecl *D, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000173 /// \brief Checks if the specified variables has data-sharing attributes which
174 /// match specified \a CPred predicate in any directive which matches \a DPred
175 /// predicate.
176 template <class ClausesPredicate, class DirectivesPredicate>
177 DSAVarData hasDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000178 DirectivesPredicate DPred, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000179 /// \brief Checks if the specified variables has data-sharing attributes which
180 /// match specified \a CPred predicate in any innermost directive which
181 /// matches \a DPred predicate.
182 template <class ClausesPredicate, class DirectivesPredicate>
183 DSAVarData hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000184 DirectivesPredicate DPred,
185 bool FromParent);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000186 /// \brief Checks if the specified variables has explicit data-sharing
187 /// attributes which match specified \a CPred predicate at the specified
188 /// OpenMP region.
189 bool hasExplicitDSA(VarDecl *D,
190 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
191 unsigned Level);
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000192 /// \brief Finds a directive which matches specified \a DPred predicate.
193 template <class NamedDirectivesPredicate>
194 bool hasDirective(NamedDirectivesPredicate DPred, bool FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000195
Alexey Bataev758e55e2013-09-06 18:03:48 +0000196 /// \brief Returns currently analyzed directive.
197 OpenMPDirectiveKind getCurrentDirective() const {
198 return Stack.back().Directive;
199 }
Alexey Bataev549210e2014-06-24 04:39:47 +0000200 /// \brief Returns parent directive.
201 OpenMPDirectiveKind getParentDirective() const {
202 if (Stack.size() > 2)
203 return Stack[Stack.size() - 2].Directive;
204 return OMPD_unknown;
205 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000206
207 /// \brief Set default data sharing attribute to none.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000208 void setDefaultDSANone(SourceLocation Loc) {
209 Stack.back().DefaultAttr = DSA_none;
210 Stack.back().DefaultAttrLoc = Loc;
211 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000212 /// \brief Set default data sharing attribute to shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000213 void setDefaultDSAShared(SourceLocation Loc) {
214 Stack.back().DefaultAttr = DSA_shared;
215 Stack.back().DefaultAttrLoc = Loc;
216 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000217
218 DefaultDataSharingAttributes getDefaultDSA() const {
219 return Stack.back().DefaultAttr;
220 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000221 SourceLocation getDefaultDSALocation() const {
222 return Stack.back().DefaultAttrLoc;
223 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000224
Alexey Bataevf29276e2014-06-18 04:14:57 +0000225 /// \brief Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000226 bool isThreadPrivate(VarDecl *D) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000227 DSAVarData DVar = getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000228 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000229 }
230
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000231 /// \brief Marks current region as ordered (it has an 'ordered' clause).
232 void setOrderedRegion(bool IsOrdered = true) {
233 Stack.back().OrderedRegion = IsOrdered;
234 }
235 /// \brief Returns true, if parent region is ordered (has associated
236 /// 'ordered' clause), false - otherwise.
237 bool isParentOrderedRegion() const {
238 if (Stack.size() > 2)
239 return Stack[Stack.size() - 2].OrderedRegion;
240 return false;
241 }
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000242 /// \brief Marks current region as nowait (it has a 'nowait' clause).
243 void setNowaitRegion(bool IsNowait = true) {
244 Stack.back().NowaitRegion = IsNowait;
245 }
246 /// \brief Returns true, if parent region is nowait (has associated
247 /// 'nowait' clause), false - otherwise.
248 bool isParentNowaitRegion() const {
249 if (Stack.size() > 2)
250 return Stack[Stack.size() - 2].NowaitRegion;
251 return false;
252 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000253
Alexey Bataev9c821032015-04-30 04:23:23 +0000254 /// \brief Set collapse value for the region.
255 void setCollapseNumber(unsigned Val) { Stack.back().CollapseNumber = Val; }
256 /// \brief Return collapse value for region.
257 unsigned getCollapseNumber() const {
258 return Stack.back().CollapseNumber;
259 }
260
Alexey Bataev13314bf2014-10-09 04:18:56 +0000261 /// \brief Marks current target region as one with closely nested teams
262 /// region.
263 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
264 if (Stack.size() > 2)
265 Stack[Stack.size() - 2].InnerTeamsRegionLoc = TeamsRegionLoc;
266 }
267 /// \brief Returns true, if current region has closely nested teams region.
268 bool hasInnerTeamsRegion() const {
269 return getInnerTeamsRegionLoc().isValid();
270 }
271 /// \brief Returns location of the nested teams region (if any).
272 SourceLocation getInnerTeamsRegionLoc() const {
273 if (Stack.size() > 1)
274 return Stack.back().InnerTeamsRegionLoc;
275 return SourceLocation();
276 }
277
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000278 Scope *getCurScope() const { return Stack.back().CurScope; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000279 Scope *getCurScope() { return Stack.back().CurScope; }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000280 SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000281};
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000282bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
283 return isOpenMPParallelDirective(DKind) || DKind == OMPD_task ||
Alexey Bataev13314bf2014-10-09 04:18:56 +0000284 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000285}
Alexey Bataeved09d242014-05-28 05:53:51 +0000286} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000287
288DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter,
289 VarDecl *D) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000290 D = D->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000291 DSAVarData DVar;
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000292 if (Iter == std::prev(Stack.rend())) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000293 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
294 // in a region but not in construct]
295 // File-scope or namespace-scope variables referenced in called routines
296 // in the region are shared unless they appear in a threadprivate
297 // directive.
Alexey Bataev8b9cb982014-07-24 02:33:58 +0000298 if (!D->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D))
Alexey Bataev750a58b2014-03-18 12:19:12 +0000299 DVar.CKind = OMPC_shared;
300
301 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
302 // in a region but not in construct]
303 // Variables with static storage duration that are declared in called
304 // routines in the region are shared.
305 if (D->hasGlobalStorage())
306 DVar.CKind = OMPC_shared;
307
Alexey Bataev758e55e2013-09-06 18:03:48 +0000308 return DVar;
309 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000310
Alexey Bataev758e55e2013-09-06 18:03:48 +0000311 DVar.DKind = Iter->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +0000312 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
313 // in a Construct, C/C++, predetermined, p.1]
314 // Variables with automatic storage duration that are declared in a scope
315 // inside the construct are private.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000316 if (isOpenMPLocal(D, Iter) && D->isLocalVarDecl() &&
317 (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) {
318 DVar.CKind = OMPC_private;
319 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000320 }
321
Alexey Bataev758e55e2013-09-06 18:03:48 +0000322 // Explicitly specified attributes and local variables with predetermined
323 // attributes.
324 if (Iter->SharingMap.count(D)) {
325 DVar.RefExpr = Iter->SharingMap[D].RefExpr;
326 DVar.CKind = Iter->SharingMap[D].Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000327 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000328 return DVar;
329 }
330
331 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
332 // in a Construct, C/C++, implicitly determined, p.1]
333 // In a parallel or task construct, the data-sharing attributes of these
334 // variables are determined by the default clause, if present.
335 switch (Iter->DefaultAttr) {
336 case DSA_shared:
337 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000338 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000339 return DVar;
340 case DSA_none:
341 return DVar;
342 case DSA_unspecified:
343 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
344 // in a Construct, implicitly determined, p.2]
345 // In a parallel construct, if no default clause is present, these
346 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000347 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000348 if (isOpenMPParallelDirective(DVar.DKind) ||
349 isOpenMPTeamsDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000350 DVar.CKind = OMPC_shared;
351 return DVar;
352 }
353
354 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
355 // in a Construct, implicitly determined, p.4]
356 // In a task construct, if no default clause is present, a variable that in
357 // the enclosing context is determined to be shared by all implicit tasks
358 // bound to the current team is shared.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000359 if (DVar.DKind == OMPD_task) {
360 DSAVarData DVarTemp;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000361 for (StackTy::reverse_iterator I = std::next(Iter), EE = Stack.rend();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000362 I != EE; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000363 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
364 // Referenced
Alexey Bataev758e55e2013-09-06 18:03:48 +0000365 // in a Construct, implicitly determined, p.6]
366 // In a task construct, if no default clause is present, a variable
367 // whose data-sharing attribute is not determined by the rules above is
368 // firstprivate.
369 DVarTemp = getDSA(I, D);
370 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000371 DVar.RefExpr = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000372 DVar.DKind = OMPD_task;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000373 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000374 return DVar;
375 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000376 if (isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000377 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000378 }
379 DVar.DKind = OMPD_task;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000380 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000381 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000382 return DVar;
383 }
384 }
385 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
386 // in a Construct, implicitly determined, p.3]
387 // For constructs other than task, if no default clause is present, these
388 // variables inherit their data-sharing attributes from the enclosing
389 // context.
Benjamin Kramer167e9992014-03-02 12:20:24 +0000390 return getDSA(std::next(Iter), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000391}
392
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000393DeclRefExpr *DSAStackTy::addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE) {
394 assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000395 D = D->getCanonicalDecl();
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000396 auto It = Stack.back().AlignedMap.find(D);
397 if (It == Stack.back().AlignedMap.end()) {
398 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
399 Stack.back().AlignedMap[D] = NewDE;
400 return nullptr;
401 } else {
402 assert(It->second && "Unexpected nullptr expr in the aligned map");
403 return It->second;
404 }
405 return nullptr;
406}
407
Alexey Bataev9c821032015-04-30 04:23:23 +0000408void DSAStackTy::addLoopControlVariable(VarDecl *D) {
409 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
410 D = D->getCanonicalDecl();
411 Stack.back().LCVSet.insert(D);
412}
413
414bool DSAStackTy::isLoopControlVariable(VarDecl *D) {
415 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
416 D = D->getCanonicalDecl();
417 return Stack.back().LCVSet.count(D) > 0;
418}
419
Alexey Bataev758e55e2013-09-06 18:03:48 +0000420void DSAStackTy::addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000421 D = D->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000422 if (A == OMPC_threadprivate) {
423 Stack[0].SharingMap[D].Attributes = A;
424 Stack[0].SharingMap[D].RefExpr = E;
425 } else {
426 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
427 Stack.back().SharingMap[D].Attributes = A;
428 Stack.back().SharingMap[D].RefExpr = E;
429 }
430}
431
Alexey Bataeved09d242014-05-28 05:53:51 +0000432bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000433 D = D->getCanonicalDecl();
Alexey Bataevec3da872014-01-31 05:15:34 +0000434 if (Stack.size() > 2) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000435 reverse_iterator I = Iter, E = std::prev(Stack.rend());
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000436 Scope *TopScope = nullptr;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000437 while (I != E && !isParallelOrTaskRegion(I->Directive)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000438 ++I;
439 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000440 if (I == E)
441 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000442 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000443 Scope *CurScope = getCurScope();
444 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000445 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000446 }
447 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000448 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000449 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000450}
451
Alexey Bataev39f915b82015-05-08 10:41:21 +0000452/// \brief Build a variable declaration for OpenMP loop iteration variable.
453static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
454 StringRef Name) {
455 DeclContext *DC = SemaRef.CurContext;
456 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
457 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
458 VarDecl *Decl =
459 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
460 Decl->setImplicit();
461 return Decl;
462}
463
464static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
465 SourceLocation Loc,
466 bool RefersToCapture = false) {
467 D->setReferenced();
468 D->markUsed(S.Context);
469 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
470 SourceLocation(), D, RefersToCapture, Loc, Ty,
471 VK_LValue);
472}
473
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000474DSAStackTy::DSAVarData DSAStackTy::getTopDSA(VarDecl *D, bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000475 D = D->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000476 DSAVarData DVar;
477
478 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
479 // in a Construct, C/C++, predetermined, p.1]
480 // Variables appearing in threadprivate directives are threadprivate.
Samuel Antaof8b50122015-07-13 22:54:53 +0000481 if ((D->getTLSKind() != VarDecl::TLS_None &&
482 !(D->hasAttr<OMPThreadPrivateDeclAttr>() &&
483 SemaRef.getLangOpts().OpenMPUseTLS &&
484 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +0000485 (D->getStorageClass() == SC_Register && D->hasAttr<AsmLabelAttr>() &&
486 !D->isLocalVarDecl())) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000487 addDSA(D, buildDeclRefExpr(SemaRef, D, D->getType().getNonReferenceType(),
488 D->getLocation()),
Alexey Bataevf2453a02015-05-06 07:25:08 +0000489 OMPC_threadprivate);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000490 }
491 if (Stack[0].SharingMap.count(D)) {
492 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
493 DVar.CKind = OMPC_threadprivate;
494 return DVar;
495 }
496
497 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
498 // in a Construct, C/C++, predetermined, p.1]
499 // Variables with automatic storage duration that are declared in a scope
500 // inside the construct are private.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000501 OpenMPDirectiveKind Kind =
502 FromParent ? getParentDirective() : getCurrentDirective();
503 auto StartI = std::next(Stack.rbegin());
504 auto EndI = std::prev(Stack.rend());
505 if (FromParent && StartI != EndI) {
506 StartI = std::next(StartI);
507 }
508 if (!isParallelOrTaskRegion(Kind)) {
Alexey Bataev8b9cb982014-07-24 02:33:58 +0000509 if (isOpenMPLocal(D, StartI) &&
510 ((D->isLocalVarDecl() && (D->getStorageClass() == SC_Auto ||
511 D->getStorageClass() == SC_None)) ||
512 isa<ParmVarDecl>(D))) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000513 DVar.CKind = OMPC_private;
514 return DVar;
Alexander Musman8dba6642014-04-22 13:09:42 +0000515 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000516
Alexey Bataev24b04aa2015-01-16 07:11:33 +0000517 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
518 // in a Construct, C/C++, predetermined, p.4]
519 // Static data members are shared.
Alexey Bataev24b04aa2015-01-16 07:11:33 +0000520 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
521 // in a Construct, C/C++, predetermined, p.7]
522 // Variables with static storage duration that are declared in a scope
523 // inside the construct are shared.
Alexey Bataev42971a32015-01-20 07:03:46 +0000524 if (D->isStaticDataMember() || D->isStaticLocal()) {
525 DSAVarData DVarTemp =
526 hasDSA(D, isOpenMPPrivate, MatchesAlways(), FromParent);
527 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
528 return DVar;
529
Alexey Bataev24b04aa2015-01-16 07:11:33 +0000530 DVar.CKind = OMPC_shared;
531 return DVar;
532 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000533 }
534
535 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +0000536 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
537 Type = SemaRef.getASTContext().getBaseElementType(Type);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000538 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
539 // in a Construct, C/C++, predetermined, p.6]
540 // Variables with const qualified type having no mutable member are
541 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000542 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000543 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000544 if (IsConstant &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000545 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000546 // Variables with const-qualified type having no mutable member may be
547 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000548 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
549 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000550 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
551 return DVar;
552
Alexey Bataev758e55e2013-09-06 18:03:48 +0000553 DVar.CKind = OMPC_shared;
554 return DVar;
555 }
556
Alexey Bataev758e55e2013-09-06 18:03:48 +0000557 // Explicitly specified attributes and local variables with predetermined
558 // attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000559 auto I = std::prev(StartI);
560 if (I->SharingMap.count(D)) {
561 DVar.RefExpr = I->SharingMap[D].RefExpr;
562 DVar.CKind = I->SharingMap[D].Attributes;
563 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000564 }
565
566 return DVar;
567}
568
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000569DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D, bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000570 D = D->getCanonicalDecl();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000571 auto StartI = Stack.rbegin();
572 auto EndI = std::prev(Stack.rend());
573 if (FromParent && StartI != EndI) {
574 StartI = std::next(StartI);
575 }
576 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000577}
578
Alexey Bataevf29276e2014-06-18 04:14:57 +0000579template <class ClausesPredicate, class DirectivesPredicate>
580DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000581 DirectivesPredicate DPred,
582 bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000583 D = D->getCanonicalDecl();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000584 auto StartI = std::next(Stack.rbegin());
585 auto EndI = std::prev(Stack.rend());
586 if (FromParent && StartI != EndI) {
587 StartI = std::next(StartI);
588 }
589 for (auto I = StartI, EE = EndI; I != EE; ++I) {
590 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000591 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000592 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000593 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000594 return DVar;
595 }
596 return DSAVarData();
597}
598
Alexey Bataevf29276e2014-06-18 04:14:57 +0000599template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000600DSAStackTy::DSAVarData
601DSAStackTy::hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
602 DirectivesPredicate DPred, bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000603 D = D->getCanonicalDecl();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000604 auto StartI = std::next(Stack.rbegin());
605 auto EndI = std::prev(Stack.rend());
606 if (FromParent && StartI != EndI) {
607 StartI = std::next(StartI);
608 }
609 for (auto I = StartI, EE = EndI; I != EE; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000610 if (!DPred(I->Directive))
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000611 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000612 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000613 if (CPred(DVar.CKind))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000614 return DVar;
615 return DSAVarData();
616 }
617 return DSAVarData();
618}
619
Alexey Bataevaac108a2015-06-23 04:51:00 +0000620bool DSAStackTy::hasExplicitDSA(
621 VarDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
622 unsigned Level) {
623 if (CPred(ClauseKindMode))
624 return true;
625 if (isClauseParsingMode())
626 ++Level;
627 D = D->getCanonicalDecl();
628 auto StartI = Stack.rbegin();
629 auto EndI = std::prev(Stack.rend());
NAKAMURA Takumi0332eda2015-06-23 10:01:20 +0000630 if (std::distance(StartI, EndI) <= (int)Level)
Alexey Bataevaac108a2015-06-23 04:51:00 +0000631 return false;
632 std::advance(StartI, Level);
633 return (StartI->SharingMap.count(D) > 0) && StartI->SharingMap[D].RefExpr &&
634 CPred(StartI->SharingMap[D].Attributes);
635}
636
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000637template <class NamedDirectivesPredicate>
638bool DSAStackTy::hasDirective(NamedDirectivesPredicate DPred, bool FromParent) {
639 auto StartI = std::next(Stack.rbegin());
640 auto EndI = std::prev(Stack.rend());
641 if (FromParent && StartI != EndI) {
642 StartI = std::next(StartI);
643 }
644 for (auto I = StartI, EE = EndI; I != EE; ++I) {
645 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
646 return true;
647 }
648 return false;
649}
650
Alexey Bataev758e55e2013-09-06 18:03:48 +0000651void Sema::InitDataSharingAttributesStack() {
652 VarDataSharingAttributesStack = new DSAStackTy(*this);
653}
654
655#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
656
Alexey Bataevf841bd92014-12-16 07:00:22 +0000657bool Sema::IsOpenMPCapturedVar(VarDecl *VD) {
658 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000659 VD = VD->getCanonicalDecl();
Alexey Bataev48977c32015-08-04 08:10:48 +0000660 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
661 (!DSAStack->isClauseParsingMode() ||
662 DSAStack->getParentDirective() != OMPD_unknown)) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000663 if (DSAStack->isLoopControlVariable(VD) ||
664 (VD->hasLocalStorage() &&
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000665 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
666 DSAStack->isForceVarCapturing())
Alexey Bataev9c821032015-04-30 04:23:23 +0000667 return true;
Alexey Bataevaac108a2015-06-23 04:51:00 +0000668 auto DVarPrivate = DSAStack->getTopDSA(VD, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000669 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
670 return true;
671 DVarPrivate = DSAStack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(),
Alexey Bataevaac108a2015-06-23 04:51:00 +0000672 DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000673 return DVarPrivate.CKind != OMPC_unknown;
674 }
675 return false;
676}
677
Alexey Bataevaac108a2015-06-23 04:51:00 +0000678bool Sema::isOpenMPPrivateVar(VarDecl *VD, unsigned Level) {
679 assert(LangOpts.OpenMP && "OpenMP is not allowed");
680 return DSAStack->hasExplicitDSA(
681 VD, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level);
682}
683
Alexey Bataeved09d242014-05-28 05:53:51 +0000684void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000685
686void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
687 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000688 Scope *CurScope, SourceLocation Loc) {
689 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000690 PushExpressionEvaluationContext(PotentiallyEvaluated);
691}
692
Alexey Bataevaac108a2015-06-23 04:51:00 +0000693void Sema::StartOpenMPClause(OpenMPClauseKind K) {
694 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000695}
696
Alexey Bataevaac108a2015-06-23 04:51:00 +0000697void Sema::EndOpenMPClause() {
698 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000699}
700
Alexey Bataev758e55e2013-09-06 18:03:48 +0000701void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000702 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
703 // A variable of class type (or array thereof) that appears in a lastprivate
704 // clause requires an accessible, unambiguous default constructor for the
705 // class type, unless the list item is also specified in a firstprivate
706 // clause.
707 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000708 for (auto *C : D->clauses()) {
709 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
710 SmallVector<Expr *, 8> PrivateCopies;
711 for (auto *DE : Clause->varlists()) {
712 if (DE->isValueDependent() || DE->isTypeDependent()) {
713 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000714 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +0000715 }
716 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(DE)->getDecl());
Alexey Bataevf120c0d2015-05-19 07:46:42 +0000717 QualType Type = VD->getType();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000718 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000719 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000720 // Generate helper private variable and initialize it with the
721 // default value. The address of the original variable is replaced
722 // by the address of the new private variable in CodeGen. This new
723 // variable is not added to IdResolver, so the code in the OpenMP
724 // region uses original variable for proper diagnostics.
Alexey Bataev39f915b82015-05-08 10:41:21 +0000725 auto *VDPrivate =
Alexey Bataevf120c0d2015-05-19 07:46:42 +0000726 buildVarDecl(*this, DE->getExprLoc(), Type.getUnqualifiedType(),
727 VD->getName());
Alexey Bataev38e89532015-04-16 04:54:05 +0000728 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
729 if (VDPrivate->isInvalidDecl())
730 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +0000731 PrivateCopies.push_back(buildDeclRefExpr(
732 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +0000733 } else {
734 // The variable is also a firstprivate, so initialization sequence
735 // for private copy is generated already.
736 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000737 }
738 }
Alexey Bataev38e89532015-04-16 04:54:05 +0000739 // Set initializers to private copies if no errors were found.
740 if (PrivateCopies.size() == Clause->varlist_size()) {
741 Clause->setPrivateCopies(PrivateCopies);
742 }
Alexey Bataevf29276e2014-06-18 04:14:57 +0000743 }
744 }
745 }
746
Alexey Bataev758e55e2013-09-06 18:03:48 +0000747 DSAStack->pop();
748 DiscardCleanupsInEvaluationContext();
749 PopExpressionEvaluationContext();
750}
751
Alexander Musman3276a272015-03-21 10:12:56 +0000752static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
753 Expr *NumIterations, Sema &SemaRef,
754 Scope *S);
755
Alexey Bataeva769e072013-03-22 06:34:35 +0000756namespace {
757
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000758class VarDeclFilterCCC : public CorrectionCandidateCallback {
759private:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000760 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +0000761
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000762public:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000763 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +0000764 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000765 NamedDecl *ND = Candidate.getCorrectionDecl();
766 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
767 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000768 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
769 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +0000770 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000771 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000772 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000773};
Alexey Bataeved09d242014-05-28 05:53:51 +0000774} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000775
776ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
777 CXXScopeSpec &ScopeSpec,
778 const DeclarationNameInfo &Id) {
779 LookupResult Lookup(*this, Id, LookupOrdinaryName);
780 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
781
782 if (Lookup.isAmbiguous())
783 return ExprError();
784
785 VarDecl *VD;
786 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +0000787 if (TypoCorrection Corrected = CorrectTypo(
788 Id, LookupOrdinaryName, CurScope, nullptr,
789 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +0000790 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000791 PDiag(Lookup.empty()
792 ? diag::err_undeclared_var_use_suggest
793 : diag::err_omp_expected_var_arg_suggest)
794 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +0000795 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000796 } else {
Richard Smithf9b15102013-08-17 00:46:16 +0000797 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
798 : diag::err_omp_expected_var_arg)
799 << Id.getName();
800 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000801 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000802 } else {
803 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000804 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000805 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
806 return ExprError();
807 }
808 }
809 Lookup.suppressDiagnostics();
810
811 // OpenMP [2.9.2, Syntax, C/C++]
812 // Variables must be file-scope, namespace-scope, or static block-scope.
813 if (!VD->hasGlobalStorage()) {
814 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000815 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
816 bool IsDecl =
817 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000818 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +0000819 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
820 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000821 return ExprError();
822 }
823
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000824 VarDecl *CanonicalVD = VD->getCanonicalDecl();
825 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000826 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
827 // A threadprivate directive for file-scope variables must appear outside
828 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000829 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
830 !getCurLexicalContext()->isTranslationUnit()) {
831 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000832 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
833 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 Bataev7d2960b2013-09-26 03:24:06 +0000838 return ExprError();
839 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000840 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
841 // A threadprivate directive for static class member variables must appear
842 // in the class definition, in the same scope in which the member
843 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000844 if (CanonicalVD->isStaticDataMember() &&
845 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
846 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000847 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
848 bool IsDecl =
849 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
850 Diag(VD->getLocation(),
851 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
852 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000853 return ExprError();
854 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000855 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
856 // A threadprivate directive for namespace-scope variables must appear
857 // outside any definition or declaration other than the namespace
858 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000859 if (CanonicalVD->getDeclContext()->isNamespace() &&
860 (!getCurLexicalContext()->isFileContext() ||
861 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
862 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000863 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
864 bool IsDecl =
865 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
866 Diag(VD->getLocation(),
867 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
868 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000869 return ExprError();
870 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000871 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
872 // A threadprivate directive for static block-scope variables must appear
873 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000874 if (CanonicalVD->isStaticLocal() && CurScope &&
875 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000876 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000877 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
878 bool IsDecl =
879 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
880 Diag(VD->getLocation(),
881 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
882 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000883 return ExprError();
884 }
885
886 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
887 // A threadprivate directive must lexically precede all references to any
888 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000889 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000890 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +0000891 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000892 return ExprError();
893 }
894
895 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +0000896 ExprResult DE = buildDeclRefExpr(*this, VD, ExprType, Id.getLoc());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000897 return DE;
898}
899
Alexey Bataeved09d242014-05-28 05:53:51 +0000900Sema::DeclGroupPtrTy
901Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
902 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000903 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000904 CurContext->addDecl(D);
905 return DeclGroupPtrTy::make(DeclGroupRef(D));
906 }
907 return DeclGroupPtrTy();
908}
909
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000910namespace {
911class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
912 Sema &SemaRef;
913
914public:
915 bool VisitDeclRefExpr(const DeclRefExpr *E) {
916 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
917 if (VD->hasLocalStorage()) {
918 SemaRef.Diag(E->getLocStart(),
919 diag::err_omp_local_var_in_threadprivate_init)
920 << E->getSourceRange();
921 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
922 << VD << VD->getSourceRange();
923 return true;
924 }
925 }
926 return false;
927 }
928 bool VisitStmt(const Stmt *S) {
929 for (auto Child : S->children()) {
930 if (Child && Visit(Child))
931 return true;
932 }
933 return false;
934 }
Alexey Bataev23b69422014-06-18 07:08:49 +0000935 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000936};
937} // namespace
938
Alexey Bataeved09d242014-05-28 05:53:51 +0000939OMPThreadPrivateDecl *
940Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000941 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +0000942 for (auto &RefExpr : VarList) {
943 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000944 VarDecl *VD = cast<VarDecl>(DE->getDecl());
945 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +0000946
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000947 QualType QType = VD->getType();
948 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
949 // It will be analyzed later.
950 Vars.push_back(DE);
951 continue;
952 }
953
Alexey Bataeva769e072013-03-22 06:34:35 +0000954 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
955 // A threadprivate variable must not have an incomplete type.
956 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000957 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000958 continue;
959 }
960
961 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
962 // A threadprivate variable must not have a reference type.
963 if (VD->getType()->isReferenceType()) {
964 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000965 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
966 bool IsDecl =
967 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
968 Diag(VD->getLocation(),
969 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
970 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000971 continue;
972 }
973
Samuel Antaof8b50122015-07-13 22:54:53 +0000974 // Check if this is a TLS variable. If TLS is not being supported, produce
975 // the corresponding diagnostic.
976 if ((VD->getTLSKind() != VarDecl::TLS_None &&
977 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
978 getLangOpts().OpenMPUseTLS &&
979 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +0000980 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
981 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +0000982 Diag(ILoc, diag::err_omp_var_thread_local)
983 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +0000984 bool IsDecl =
985 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
986 Diag(VD->getLocation(),
987 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
988 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000989 continue;
990 }
991
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000992 // Check if initial value of threadprivate variable reference variable with
993 // local storage (it is not supported by runtime).
994 if (auto Init = VD->getAnyInitializer()) {
995 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000996 if (Checker.Visit(Init))
997 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000998 }
999
Alexey Bataeved09d242014-05-28 05:53:51 +00001000 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00001001 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00001002 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1003 Context, SourceRange(Loc, Loc)));
1004 if (auto *ML = Context.getASTMutationListener())
1005 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00001006 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001007 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001008 if (!Vars.empty()) {
1009 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1010 Vars);
1011 D->setAccess(AS_public);
1012 }
1013 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00001014}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001015
Alexey Bataev7ff55242014-06-19 09:13:45 +00001016static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
1017 const VarDecl *VD, DSAStackTy::DSAVarData DVar,
1018 bool IsLoopIterVar = false) {
1019 if (DVar.RefExpr) {
1020 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1021 << getOpenMPClauseName(DVar.CKind);
1022 return;
1023 }
1024 enum {
1025 PDSA_StaticMemberShared,
1026 PDSA_StaticLocalVarShared,
1027 PDSA_LoopIterVarPrivate,
1028 PDSA_LoopIterVarLinear,
1029 PDSA_LoopIterVarLastprivate,
1030 PDSA_ConstVarShared,
1031 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001032 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001033 PDSA_LocalVarPrivate,
1034 PDSA_Implicit
1035 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001036 bool ReportHint = false;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001037 auto ReportLoc = VD->getLocation();
Alexey Bataev7ff55242014-06-19 09:13:45 +00001038 if (IsLoopIterVar) {
1039 if (DVar.CKind == OMPC_private)
1040 Reason = PDSA_LoopIterVarPrivate;
1041 else if (DVar.CKind == OMPC_lastprivate)
1042 Reason = PDSA_LoopIterVarLastprivate;
1043 else
1044 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001045 } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) {
1046 Reason = PDSA_TaskVarFirstprivate;
1047 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001048 } else if (VD->isStaticLocal())
1049 Reason = PDSA_StaticLocalVarShared;
1050 else if (VD->isStaticDataMember())
1051 Reason = PDSA_StaticMemberShared;
1052 else if (VD->isFileVarDecl())
1053 Reason = PDSA_GlobalVarShared;
1054 else if (VD->getType().isConstant(SemaRef.getASTContext()))
1055 Reason = PDSA_ConstVarShared;
Alexey Bataevbae9a792014-06-27 10:37:06 +00001056 else if (VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00001057 ReportHint = true;
1058 Reason = PDSA_LocalVarPrivate;
1059 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00001060 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001061 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00001062 << Reason << ReportHint
1063 << getOpenMPDirectiveName(Stack->getCurrentDirective());
1064 } else if (DVar.ImplicitDSALoc.isValid()) {
1065 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1066 << getOpenMPClauseName(DVar.CKind);
1067 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00001068}
1069
Alexey Bataev758e55e2013-09-06 18:03:48 +00001070namespace {
1071class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1072 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001073 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001074 bool ErrorFound;
1075 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001076 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev4acb8592014-07-07 13:01:15 +00001077 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +00001078
Alexey Bataev758e55e2013-09-06 18:03:48 +00001079public:
1080 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001081 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001082 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +00001083 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1084 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001085
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001086 auto DVar = Stack->getTopDSA(VD, false);
1087 // Check if the variable has explicit DSA set and stop analysis if it so.
1088 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001089
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001090 auto ELoc = E->getExprLoc();
1091 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001092 // The default(none) clause requires that each variable that is referenced
1093 // in the construct, and does not have a predetermined data-sharing
1094 // attribute, must have its data-sharing attribute explicitly determined
1095 // by being listed in a data-sharing attribute clause.
1096 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001097 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001098 VarsWithInheritedDSA.count(VD) == 0) {
1099 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001100 return;
1101 }
1102
1103 // OpenMP [2.9.3.6, Restrictions, p.2]
1104 // A list item that appears in a reduction clause of the innermost
1105 // enclosing worksharing or parallel construct may not be accessed in an
1106 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +00001107 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001108 [](OpenMPDirectiveKind K) -> bool {
1109 return isOpenMPParallelDirective(K) ||
Alexey Bataev13314bf2014-10-09 04:18:56 +00001110 isOpenMPWorksharingDirective(K) ||
1111 isOpenMPTeamsDirective(K);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001112 },
1113 false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001114 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
1115 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001116 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1117 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001118 return;
1119 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001120
1121 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001122 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001123 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001124 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001125 }
1126 }
1127 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001128 for (auto *C : S->clauses()) {
1129 // Skip analysis of arguments of implicitly defined firstprivate clause
1130 // for task directives.
1131 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1132 for (auto *CC : C->children()) {
1133 if (CC)
1134 Visit(CC);
1135 }
1136 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001137 }
1138 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001139 for (auto *C : S->children()) {
1140 if (C && !isa<OMPExecutableDirective>(C))
1141 Visit(C);
1142 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001143 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001144
1145 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001146 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001147 llvm::DenseMap<VarDecl *, Expr *> &getVarsWithInheritedDSA() {
1148 return VarsWithInheritedDSA;
1149 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001150
Alexey Bataev7ff55242014-06-19 09:13:45 +00001151 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1152 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00001153};
Alexey Bataeved09d242014-05-28 05:53:51 +00001154} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001155
Alexey Bataevbae9a792014-06-27 10:37:06 +00001156void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001157 switch (DKind) {
1158 case OMPD_parallel: {
1159 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1160 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001161 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001162 std::make_pair(".global_tid.", KmpInt32PtrTy),
1163 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1164 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001165 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001166 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1167 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001168 break;
1169 }
1170 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001171 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001172 std::make_pair(StringRef(), QualType()) // __context with shared vars
1173 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001174 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1175 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001176 break;
1177 }
1178 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001179 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001180 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001181 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001182 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1183 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001184 break;
1185 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001186 case OMPD_for_simd: {
1187 Sema::CapturedParamNameType Params[] = {
1188 std::make_pair(StringRef(), QualType()) // __context with shared vars
1189 };
1190 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1191 Params);
1192 break;
1193 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001194 case OMPD_sections: {
1195 Sema::CapturedParamNameType Params[] = {
1196 std::make_pair(StringRef(), QualType()) // __context with shared vars
1197 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001198 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1199 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001200 break;
1201 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001202 case OMPD_section: {
1203 Sema::CapturedParamNameType Params[] = {
1204 std::make_pair(StringRef(), QualType()) // __context with shared vars
1205 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001206 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1207 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001208 break;
1209 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001210 case OMPD_single: {
1211 Sema::CapturedParamNameType Params[] = {
1212 std::make_pair(StringRef(), QualType()) // __context with shared vars
1213 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001214 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1215 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001216 break;
1217 }
Alexander Musman80c22892014-07-17 08:54:58 +00001218 case OMPD_master: {
1219 Sema::CapturedParamNameType Params[] = {
1220 std::make_pair(StringRef(), QualType()) // __context with shared vars
1221 };
1222 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1223 Params);
1224 break;
1225 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001226 case OMPD_critical: {
1227 Sema::CapturedParamNameType Params[] = {
1228 std::make_pair(StringRef(), QualType()) // __context with shared vars
1229 };
1230 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1231 Params);
1232 break;
1233 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001234 case OMPD_parallel_for: {
1235 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1236 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1237 Sema::CapturedParamNameType Params[] = {
1238 std::make_pair(".global_tid.", KmpInt32PtrTy),
1239 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1240 std::make_pair(StringRef(), QualType()) // __context with shared vars
1241 };
1242 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1243 Params);
1244 break;
1245 }
Alexander Musmane4e893b2014-09-23 09:33:00 +00001246 case OMPD_parallel_for_simd: {
1247 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1248 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1249 Sema::CapturedParamNameType Params[] = {
1250 std::make_pair(".global_tid.", KmpInt32PtrTy),
1251 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1252 std::make_pair(StringRef(), QualType()) // __context with shared vars
1253 };
1254 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1255 Params);
1256 break;
1257 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001258 case OMPD_parallel_sections: {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001259 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1260 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001261 Sema::CapturedParamNameType Params[] = {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001262 std::make_pair(".global_tid.", KmpInt32PtrTy),
1263 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001264 std::make_pair(StringRef(), QualType()) // __context with shared vars
1265 };
1266 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1267 Params);
1268 break;
1269 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001270 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001271 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001272 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1273 FunctionProtoType::ExtProtoInfo EPI;
1274 EPI.Variadic = true;
1275 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001276 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001277 std::make_pair(".global_tid.", KmpInt32Ty),
1278 std::make_pair(".part_id.", KmpInt32Ty),
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001279 std::make_pair(".privates.",
1280 Context.VoidPtrTy.withConst().withRestrict()),
1281 std::make_pair(
1282 ".copy_fn.",
1283 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001284 std::make_pair(StringRef(), QualType()) // __context with shared vars
1285 };
1286 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1287 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001288 // Mark this captured region as inlined, because we don't use outlined
1289 // function directly.
1290 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1291 AlwaysInlineAttr::CreateImplicit(
1292 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001293 break;
1294 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001295 case OMPD_ordered: {
1296 Sema::CapturedParamNameType Params[] = {
1297 std::make_pair(StringRef(), QualType()) // __context with shared vars
1298 };
1299 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1300 Params);
1301 break;
1302 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001303 case OMPD_atomic: {
1304 Sema::CapturedParamNameType Params[] = {
1305 std::make_pair(StringRef(), QualType()) // __context with shared vars
1306 };
1307 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1308 Params);
1309 break;
1310 }
Michael Wong65f367f2015-07-21 13:44:28 +00001311 case OMPD_target_data:
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001312 case OMPD_target: {
1313 Sema::CapturedParamNameType Params[] = {
1314 std::make_pair(StringRef(), QualType()) // __context with shared vars
1315 };
1316 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1317 Params);
1318 break;
1319 }
Alexey Bataev13314bf2014-10-09 04:18:56 +00001320 case OMPD_teams: {
1321 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1322 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1323 Sema::CapturedParamNameType Params[] = {
1324 std::make_pair(".global_tid.", KmpInt32PtrTy),
1325 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1326 std::make_pair(StringRef(), QualType()) // __context with shared vars
1327 };
1328 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1329 Params);
1330 break;
1331 }
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001332 case OMPD_taskgroup: {
1333 Sema::CapturedParamNameType Params[] = {
1334 std::make_pair(StringRef(), QualType()) // __context with shared vars
1335 };
1336 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1337 Params);
1338 break;
1339 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001340 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001341 case OMPD_taskyield:
1342 case OMPD_barrier:
1343 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001344 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001345 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00001346 case OMPD_flush:
Alexey Bataev9959db52014-05-06 10:08:46 +00001347 llvm_unreachable("OpenMP Directive is not allowed");
1348 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001349 llvm_unreachable("Unknown OpenMP directive");
1350 }
1351}
1352
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001353StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1354 ArrayRef<OMPClause *> Clauses) {
1355 if (!S.isUsable()) {
1356 ActOnCapturedRegionError();
1357 return StmtError();
1358 }
Alexey Bataev040d5402015-05-12 08:35:28 +00001359 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001360 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001361 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001362 Clause->getClauseKind() == OMPC_copyprivate ||
1363 (getLangOpts().OpenMPUseTLS &&
1364 getASTContext().getTargetInfo().isTLSSupported() &&
1365 Clause->getClauseKind() == OMPC_copyin)) {
1366 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001367 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001368 for (auto *VarRef : Clause->children()) {
1369 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001370 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001371 }
1372 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001373 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev040d5402015-05-12 08:35:28 +00001374 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
1375 Clause->getClauseKind() == OMPC_schedule) {
1376 // Mark all variables in private list clauses as used in inner region.
1377 // Required for proper codegen of combined directives.
1378 // TODO: add processing for other clauses.
1379 if (auto *E = cast_or_null<Expr>(
1380 cast<OMPScheduleClause>(Clause)->getHelperChunkSize())) {
1381 MarkDeclarationsReferencedInExpr(E);
1382 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001383 }
1384 }
1385 return ActOnCapturedRegionEnd(S.get());
1386}
1387
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001388static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1389 OpenMPDirectiveKind CurrentRegion,
1390 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001391 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001392 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001393 // Allowed nesting of constructs
1394 // +------------------+-----------------+------------------------------------+
1395 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1396 // +------------------+-----------------+------------------------------------+
1397 // | parallel | parallel | * |
1398 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001399 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001400 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001401 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001402 // | parallel | simd | * |
1403 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001404 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001405 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001406 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001407 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001408 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001409 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001410 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001411 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001412 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001413 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001414 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001415 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001416 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001417 // | parallel | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001418 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001419 // | parallel | cancellation | |
1420 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001421 // | parallel | cancel | ! |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001422 // +------------------+-----------------+------------------------------------+
1423 // | for | parallel | * |
1424 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001425 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001426 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001427 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001428 // | for | simd | * |
1429 // | for | sections | + |
1430 // | for | section | + |
1431 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001432 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001433 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001434 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001435 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001436 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001437 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001438 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001439 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001440 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001441 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001442 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001443 // | for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001444 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001445 // | for | cancellation | |
1446 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001447 // | for | cancel | ! |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001448 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001449 // | master | parallel | * |
1450 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001451 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001452 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001453 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001454 // | master | simd | * |
1455 // | master | sections | + |
1456 // | master | section | + |
1457 // | master | single | + |
1458 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001459 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001460 // | master |parallel sections| * |
1461 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001462 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001463 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001464 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001465 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001466 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001467 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001468 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001469 // | master | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001470 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001471 // | master | cancellation | |
1472 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001473 // | master | cancel | |
Alexander Musman80c22892014-07-17 08:54:58 +00001474 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001475 // | critical | parallel | * |
1476 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001477 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001478 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001479 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001480 // | critical | simd | * |
1481 // | critical | sections | + |
1482 // | critical | section | + |
1483 // | critical | single | + |
1484 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001485 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001486 // | critical |parallel sections| * |
1487 // | critical | task | * |
1488 // | critical | taskyield | * |
1489 // | critical | barrier | + |
1490 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001491 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001492 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001493 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001494 // | critical | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001495 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001496 // | critical | cancellation | |
1497 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001498 // | critical | cancel | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001499 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001500 // | simd | parallel | |
1501 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001502 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00001503 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001504 // | simd | critical | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001505 // | simd | simd | |
1506 // | simd | sections | |
1507 // | simd | section | |
1508 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001509 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001510 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001511 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001512 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00001513 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001514 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001515 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001516 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00001517 // | simd | flush | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001518 // | simd | ordered | |
Alexey Bataev0162e452014-07-22 10:10:35 +00001519 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001520 // | simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001521 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001522 // | simd | cancellation | |
1523 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001524 // | simd | cancel | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001525 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00001526 // | for simd | parallel | |
1527 // | for simd | for | |
1528 // | for simd | for simd | |
1529 // | for simd | master | |
1530 // | for simd | critical | |
1531 // | for simd | simd | |
1532 // | for simd | sections | |
1533 // | for simd | section | |
1534 // | for simd | single | |
1535 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001536 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001537 // | for simd |parallel sections| |
1538 // | for simd | task | |
1539 // | for simd | taskyield | |
1540 // | for simd | barrier | |
1541 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001542 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001543 // | for simd | flush | |
1544 // | for simd | ordered | |
1545 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001546 // | for simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001547 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001548 // | for simd | cancellation | |
1549 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001550 // | for simd | cancel | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001551 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00001552 // | parallel for simd| parallel | |
1553 // | parallel for simd| for | |
1554 // | parallel for simd| for simd | |
1555 // | parallel for simd| master | |
1556 // | parallel for simd| critical | |
1557 // | parallel for simd| simd | |
1558 // | parallel for simd| sections | |
1559 // | parallel for simd| section | |
1560 // | parallel for simd| single | |
1561 // | parallel for simd| parallel for | |
1562 // | parallel for simd|parallel for simd| |
1563 // | parallel for simd|parallel sections| |
1564 // | parallel for simd| task | |
1565 // | parallel for simd| taskyield | |
1566 // | parallel for simd| barrier | |
1567 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001568 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001569 // | parallel for simd| flush | |
1570 // | parallel for simd| ordered | |
1571 // | parallel for simd| atomic | |
1572 // | parallel for simd| target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001573 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001574 // | parallel for simd| cancellation | |
1575 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001576 // | parallel for simd| cancel | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001577 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001578 // | sections | parallel | * |
1579 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001580 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001581 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001582 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001583 // | sections | simd | * |
1584 // | sections | sections | + |
1585 // | sections | section | * |
1586 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001587 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001588 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001589 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001590 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001591 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001592 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001593 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001594 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001595 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001596 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001597 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001598 // | sections | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001599 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001600 // | sections | cancellation | |
1601 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001602 // | sections | cancel | ! |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001603 // +------------------+-----------------+------------------------------------+
1604 // | section | parallel | * |
1605 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001606 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001607 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001608 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001609 // | section | simd | * |
1610 // | section | sections | + |
1611 // | section | section | + |
1612 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001613 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001614 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001615 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001616 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001617 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001618 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001619 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001620 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001621 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001622 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001623 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001624 // | section | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001625 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001626 // | section | cancellation | |
1627 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001628 // | section | cancel | ! |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001629 // +------------------+-----------------+------------------------------------+
1630 // | single | parallel | * |
1631 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001632 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001633 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001634 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001635 // | single | simd | * |
1636 // | single | sections | + |
1637 // | single | section | + |
1638 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001639 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001640 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001641 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001642 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001643 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001644 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001645 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001646 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001647 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001648 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001649 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001650 // | single | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001651 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001652 // | single | cancellation | |
1653 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001654 // | single | cancel | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001655 // +------------------+-----------------+------------------------------------+
1656 // | parallel for | parallel | * |
1657 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001658 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001659 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001660 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001661 // | parallel for | simd | * |
1662 // | parallel for | sections | + |
1663 // | parallel for | section | + |
1664 // | parallel for | single | + |
1665 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001666 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001667 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001668 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001669 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001670 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001671 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001672 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001673 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001674 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001675 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001676 // | parallel for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001677 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001678 // | parallel for | cancellation | |
1679 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001680 // | parallel for | cancel | ! |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001681 // +------------------+-----------------+------------------------------------+
1682 // | parallel sections| parallel | * |
1683 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001684 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001685 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001686 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001687 // | parallel sections| simd | * |
1688 // | parallel sections| sections | + |
1689 // | parallel sections| section | * |
1690 // | parallel sections| single | + |
1691 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001692 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001693 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001694 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001695 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001696 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001697 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001698 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001699 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001700 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001701 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001702 // | parallel sections| target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001703 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001704 // | parallel sections| cancellation | |
1705 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001706 // | parallel sections| cancel | ! |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001707 // +------------------+-----------------+------------------------------------+
1708 // | task | parallel | * |
1709 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001710 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001711 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001712 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001713 // | task | simd | * |
1714 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001715 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001716 // | task | single | + |
1717 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001718 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001719 // | task |parallel sections| * |
1720 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001721 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001722 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001723 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001724 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001725 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001726 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001727 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001728 // | task | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001729 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001730 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00001731 // | | point | ! |
1732 // | task | cancel | ! |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001733 // +------------------+-----------------+------------------------------------+
1734 // | ordered | parallel | * |
1735 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001736 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001737 // | ordered | master | * |
1738 // | ordered | critical | * |
1739 // | ordered | simd | * |
1740 // | ordered | sections | + |
1741 // | ordered | section | + |
1742 // | ordered | single | + |
1743 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001744 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001745 // | ordered |parallel sections| * |
1746 // | ordered | task | * |
1747 // | ordered | taskyield | * |
1748 // | ordered | barrier | + |
1749 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001750 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001751 // | ordered | flush | * |
1752 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001753 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001754 // | ordered | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001755 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001756 // | ordered | cancellation | |
1757 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001758 // | ordered | cancel | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001759 // +------------------+-----------------+------------------------------------+
1760 // | atomic | parallel | |
1761 // | atomic | for | |
1762 // | atomic | for simd | |
1763 // | atomic | master | |
1764 // | atomic | critical | |
1765 // | atomic | simd | |
1766 // | atomic | sections | |
1767 // | atomic | section | |
1768 // | atomic | single | |
1769 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001770 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001771 // | atomic |parallel sections| |
1772 // | atomic | task | |
1773 // | atomic | taskyield | |
1774 // | atomic | barrier | |
1775 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001776 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001777 // | atomic | flush | |
1778 // | atomic | ordered | |
1779 // | atomic | atomic | |
1780 // | atomic | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001781 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001782 // | atomic | cancellation | |
1783 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001784 // | atomic | cancel | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001785 // +------------------+-----------------+------------------------------------+
1786 // | target | parallel | * |
1787 // | target | for | * |
1788 // | target | for simd | * |
1789 // | target | master | * |
1790 // | target | critical | * |
1791 // | target | simd | * |
1792 // | target | sections | * |
1793 // | target | section | * |
1794 // | target | single | * |
1795 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001796 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001797 // | target |parallel sections| * |
1798 // | target | task | * |
1799 // | target | taskyield | * |
1800 // | target | barrier | * |
1801 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001802 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001803 // | target | flush | * |
1804 // | target | ordered | * |
1805 // | target | atomic | * |
1806 // | target | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001807 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001808 // | target | cancellation | |
1809 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001810 // | target | cancel | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001811 // +------------------+-----------------+------------------------------------+
1812 // | teams | parallel | * |
1813 // | teams | for | + |
1814 // | teams | for simd | + |
1815 // | teams | master | + |
1816 // | teams | critical | + |
1817 // | teams | simd | + |
1818 // | teams | sections | + |
1819 // | teams | section | + |
1820 // | teams | single | + |
1821 // | teams | parallel for | * |
1822 // | teams |parallel for simd| * |
1823 // | teams |parallel sections| * |
1824 // | teams | task | + |
1825 // | teams | taskyield | + |
1826 // | teams | barrier | + |
1827 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00001828 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001829 // | teams | flush | + |
1830 // | teams | ordered | + |
1831 // | teams | atomic | + |
1832 // | teams | target | + |
1833 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001834 // | teams | cancellation | |
1835 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001836 // | teams | cancel | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001837 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00001838 if (Stack->getCurScope()) {
1839 auto ParentRegion = Stack->getParentDirective();
1840 bool NestingProhibited = false;
1841 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001842 enum {
1843 NoRecommend,
1844 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00001845 ShouldBeInOrderedRegion,
1846 ShouldBeInTargetRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001847 } Recommend = NoRecommend;
Alexey Bataev549210e2014-06-24 04:39:47 +00001848 if (isOpenMPSimdDirective(ParentRegion)) {
1849 // OpenMP [2.16, Nesting of Regions]
1850 // OpenMP constructs may not be nested inside a simd region.
1851 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
1852 return true;
1853 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001854 if (ParentRegion == OMPD_atomic) {
1855 // OpenMP [2.16, Nesting of Regions]
1856 // OpenMP constructs may not be nested inside an atomic region.
1857 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
1858 return true;
1859 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001860 if (CurrentRegion == OMPD_section) {
1861 // OpenMP [2.7.2, sections Construct, Restrictions]
1862 // Orphaned section directives are prohibited. That is, the section
1863 // directives must appear within the sections construct and must not be
1864 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001865 if (ParentRegion != OMPD_sections &&
1866 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001867 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
1868 << (ParentRegion != OMPD_unknown)
1869 << getOpenMPDirectiveName(ParentRegion);
1870 return true;
1871 }
1872 return false;
1873 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001874 // Allow some constructs to be orphaned (they could be used in functions,
1875 // called from OpenMP regions with the required preconditions).
1876 if (ParentRegion == OMPD_unknown)
1877 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00001878 if (CurrentRegion == OMPD_cancellation_point ||
1879 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001880 // OpenMP [2.16, Nesting of Regions]
1881 // A cancellation point construct for which construct-type-clause is
1882 // taskgroup must be nested inside a task construct. A cancellation
1883 // point construct for which construct-type-clause is not taskgroup must
1884 // be closely nested inside an OpenMP construct that matches the type
1885 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00001886 // A cancel construct for which construct-type-clause is taskgroup must be
1887 // nested inside a task construct. A cancel construct for which
1888 // construct-type-clause is not taskgroup must be closely nested inside an
1889 // OpenMP construct that matches the type specified in
1890 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001891 NestingProhibited =
1892 !((CancelRegion == OMPD_parallel && ParentRegion == OMPD_parallel) ||
1893 (CancelRegion == OMPD_for && ParentRegion == OMPD_for) ||
1894 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
1895 (CancelRegion == OMPD_sections &&
1896 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections)));
1897 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00001898 // OpenMP [2.16, Nesting of Regions]
1899 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00001900 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00001901 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
1902 ParentRegion == OMPD_task;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001903 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
1904 // OpenMP [2.16, Nesting of Regions]
1905 // A critical region may not be nested (closely or otherwise) inside a
1906 // critical region with the same name. Note that this restriction is not
1907 // sufficient to prevent deadlock.
1908 SourceLocation PreviousCriticalLoc;
1909 bool DeadLock =
1910 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
1911 OpenMPDirectiveKind K,
1912 const DeclarationNameInfo &DNI,
1913 SourceLocation Loc)
1914 ->bool {
1915 if (K == OMPD_critical &&
1916 DNI.getName() == CurrentName.getName()) {
1917 PreviousCriticalLoc = Loc;
1918 return true;
1919 } else
1920 return false;
1921 },
1922 false /* skip top directive */);
1923 if (DeadLock) {
1924 SemaRef.Diag(StartLoc,
1925 diag::err_omp_prohibited_region_critical_same_name)
1926 << CurrentName.getName();
1927 if (PreviousCriticalLoc.isValid())
1928 SemaRef.Diag(PreviousCriticalLoc,
1929 diag::note_omp_previous_critical_region);
1930 return true;
1931 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001932 } else if (CurrentRegion == OMPD_barrier) {
1933 // OpenMP [2.16, Nesting of Regions]
1934 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00001935 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001936 NestingProhibited =
1937 isOpenMPWorksharingDirective(ParentRegion) ||
1938 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1939 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00001940 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00001941 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00001942 // OpenMP [2.16, Nesting of Regions]
1943 // A worksharing region may not be closely nested inside a worksharing,
1944 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001945 NestingProhibited =
Alexander Musmanf82886e2014-09-18 05:12:34 +00001946 isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001947 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1948 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
1949 Recommend = ShouldBeInParallelRegion;
1950 } else if (CurrentRegion == OMPD_ordered) {
1951 // OpenMP [2.16, Nesting of Regions]
1952 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00001953 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001954 // An ordered region must be closely nested inside a loop region (or
1955 // parallel loop region) with an ordered clause.
1956 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00001957 ParentRegion == OMPD_task ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001958 !Stack->isParentOrderedRegion();
1959 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00001960 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
1961 // OpenMP [2.16, Nesting of Regions]
1962 // If specified, a teams construct must be contained within a target
1963 // construct.
1964 NestingProhibited = ParentRegion != OMPD_target;
1965 Recommend = ShouldBeInTargetRegion;
1966 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
1967 }
1968 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
1969 // OpenMP [2.16, Nesting of Regions]
1970 // distribute, parallel, parallel sections, parallel workshare, and the
1971 // parallel loop and parallel loop SIMD constructs are the only OpenMP
1972 // constructs that can be closely nested in the teams region.
1973 // TODO: add distribute directive.
1974 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion);
1975 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00001976 }
1977 if (NestingProhibited) {
1978 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001979 << CloseNesting << getOpenMPDirectiveName(ParentRegion) << Recommend
1980 << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00001981 return true;
1982 }
1983 }
1984 return false;
1985}
1986
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001987StmtResult Sema::ActOnOpenMPExecutableDirective(
1988 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
1989 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
1990 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001991 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001992 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
1993 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00001994 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001995
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001996 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev68446b72014-07-18 07:47:19 +00001997 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001998 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00001999 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00002000 if (AStmt) {
2001 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
2002
2003 // Check default data sharing attributes for referenced variables.
2004 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
2005 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
2006 if (DSAChecker.isErrorFound())
2007 return StmtError();
2008 // Generate list of implicitly defined firstprivate variables.
2009 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00002010
2011 if (!DSAChecker.getImplicitFirstprivate().empty()) {
2012 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
2013 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
2014 SourceLocation(), SourceLocation())) {
2015 ClausesWithImplicit.push_back(Implicit);
2016 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
2017 DSAChecker.getImplicitFirstprivate().size();
2018 } else
2019 ErrorFound = true;
2020 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002021 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002022
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002023 switch (Kind) {
2024 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00002025 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
2026 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002027 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002028 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002029 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2030 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002031 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002032 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002033 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2034 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002035 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00002036 case OMPD_for_simd:
2037 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2038 EndLoc, VarsWithInheritedDSA);
2039 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002040 case OMPD_sections:
2041 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
2042 EndLoc);
2043 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002044 case OMPD_section:
2045 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00002046 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002047 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
2048 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002049 case OMPD_single:
2050 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
2051 EndLoc);
2052 break;
Alexander Musman80c22892014-07-17 08:54:58 +00002053 case OMPD_master:
2054 assert(ClausesWithImplicit.empty() &&
2055 "No clauses are allowed for 'omp master' directive");
2056 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
2057 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002058 case OMPD_critical:
2059 assert(ClausesWithImplicit.empty() &&
2060 "No clauses are allowed for 'omp critical' directive");
2061 Res = ActOnOpenMPCriticalDirective(DirName, AStmt, StartLoc, EndLoc);
2062 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002063 case OMPD_parallel_for:
2064 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
2065 EndLoc, VarsWithInheritedDSA);
2066 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00002067 case OMPD_parallel_for_simd:
2068 Res = ActOnOpenMPParallelForSimdDirective(
2069 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
2070 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002071 case OMPD_parallel_sections:
2072 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
2073 StartLoc, EndLoc);
2074 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002075 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002076 Res =
2077 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
2078 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00002079 case OMPD_taskyield:
2080 assert(ClausesWithImplicit.empty() &&
2081 "No clauses are allowed for 'omp taskyield' directive");
2082 assert(AStmt == nullptr &&
2083 "No associated statement allowed for 'omp taskyield' directive");
2084 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
2085 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002086 case OMPD_barrier:
2087 assert(ClausesWithImplicit.empty() &&
2088 "No clauses are allowed for 'omp barrier' directive");
2089 assert(AStmt == nullptr &&
2090 "No associated statement allowed for 'omp barrier' directive");
2091 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
2092 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00002093 case OMPD_taskwait:
2094 assert(ClausesWithImplicit.empty() &&
2095 "No clauses are allowed for 'omp taskwait' directive");
2096 assert(AStmt == nullptr &&
2097 "No associated statement allowed for 'omp taskwait' directive");
2098 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
2099 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002100 case OMPD_taskgroup:
2101 assert(ClausesWithImplicit.empty() &&
2102 "No clauses are allowed for 'omp taskgroup' directive");
2103 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
2104 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00002105 case OMPD_flush:
2106 assert(AStmt == nullptr &&
2107 "No associated statement allowed for 'omp flush' directive");
2108 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
2109 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002110 case OMPD_ordered:
2111 assert(ClausesWithImplicit.empty() &&
2112 "No clauses are allowed for 'omp ordered' directive");
2113 Res = ActOnOpenMPOrderedDirective(AStmt, StartLoc, EndLoc);
2114 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00002115 case OMPD_atomic:
2116 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
2117 EndLoc);
2118 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002119 case OMPD_teams:
2120 Res =
2121 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
2122 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002123 case OMPD_target:
2124 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
2125 EndLoc);
2126 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002127 case OMPD_cancellation_point:
2128 assert(ClausesWithImplicit.empty() &&
2129 "No clauses are allowed for 'omp cancellation point' directive");
2130 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
2131 "cancellation point' directive");
2132 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
2133 break;
Alexey Bataev80909872015-07-02 11:25:17 +00002134 case OMPD_cancel:
2135 assert(ClausesWithImplicit.empty() &&
2136 "No clauses are allowed for 'omp cancel' directive");
2137 assert(AStmt == nullptr &&
2138 "No associated statement allowed for 'omp cancel' directive");
2139 Res = ActOnOpenMPCancelDirective(StartLoc, EndLoc, CancelRegion);
2140 break;
Michael Wong65f367f2015-07-21 13:44:28 +00002141 case OMPD_target_data:
2142 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
2143 EndLoc);
2144 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002145 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002146 llvm_unreachable("OpenMP Directive is not allowed");
2147 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002148 llvm_unreachable("Unknown OpenMP directive");
2149 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002150
Alexey Bataev4acb8592014-07-07 13:01:15 +00002151 for (auto P : VarsWithInheritedDSA) {
2152 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
2153 << P.first << P.second->getSourceRange();
2154 }
2155 if (!VarsWithInheritedDSA.empty())
2156 return StmtError();
2157
Alexey Bataeved09d242014-05-28 05:53:51 +00002158 if (ErrorFound)
2159 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002160 return Res;
2161}
2162
2163StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
2164 Stmt *AStmt,
2165 SourceLocation StartLoc,
2166 SourceLocation EndLoc) {
Alexey Bataev9959db52014-05-06 10:08:46 +00002167 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2168 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
2169 // 1.2.2 OpenMP Language Terminology
2170 // Structured block - An executable statement with a single entry at the
2171 // top and a single exit at the bottom.
2172 // The point of exit cannot be a branch out of the structured block.
2173 // longjmp() and throw() must not violate the entry/exit criteria.
2174 CS->getCapturedDecl()->setNothrow();
2175
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002176 getCurFunction()->setHasBranchProtectedScope();
2177
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00002178 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
2179 AStmt);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002180}
2181
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002182namespace {
2183/// \brief Helper class for checking canonical form of the OpenMP loops and
2184/// extracting iteration space of each loop in the loop nest, that will be used
2185/// for IR generation.
2186class OpenMPIterationSpaceChecker {
2187 /// \brief Reference to Sema.
2188 Sema &SemaRef;
2189 /// \brief A location for diagnostics (when there is no some better location).
2190 SourceLocation DefaultLoc;
2191 /// \brief A location for diagnostics (when increment is not compatible).
2192 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002193 /// \brief A source location for referring to loop init later.
2194 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002195 /// \brief A source location for referring to condition later.
2196 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002197 /// \brief A source location for referring to increment later.
2198 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002199 /// \brief Loop variable.
2200 VarDecl *Var;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002201 /// \brief Reference to loop variable.
2202 DeclRefExpr *VarRef;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002203 /// \brief Lower bound (initializer for the var).
2204 Expr *LB;
2205 /// \brief Upper bound.
2206 Expr *UB;
2207 /// \brief Loop step (increment).
2208 Expr *Step;
2209 /// \brief This flag is true when condition is one of:
2210 /// Var < UB
2211 /// Var <= UB
2212 /// UB > Var
2213 /// UB >= Var
2214 bool TestIsLessOp;
2215 /// \brief This flag is true when condition is strict ( < or > ).
2216 bool TestIsStrictOp;
2217 /// \brief This flag is true when step is subtracted on each iteration.
2218 bool SubtractStep;
2219
2220public:
2221 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
2222 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
Alexander Musmana5f070a2014-10-01 06:03:56 +00002223 InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()),
2224 IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002225 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
2226 TestIsStrictOp(false), SubtractStep(false) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002227 /// \brief Check init-expr for canonical loop form and save loop counter
2228 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00002229 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002230 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
2231 /// for less/greater and for strict/non-strict comparison.
2232 bool CheckCond(Expr *S);
2233 /// \brief Check incr-expr for canonical loop form and return true if it
2234 /// does not conform, otherwise save loop step (#Step).
2235 bool CheckInc(Expr *S);
2236 /// \brief Return the loop counter variable.
2237 VarDecl *GetLoopVar() const { return Var; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002238 /// \brief Return the reference expression to loop counter variable.
2239 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002240 /// \brief Source range of the loop init.
2241 SourceRange GetInitSrcRange() const { return InitSrcRange; }
2242 /// \brief Source range of the loop condition.
2243 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
2244 /// \brief Source range of the loop increment.
2245 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
2246 /// \brief True if the step should be subtracted.
2247 bool ShouldSubtractStep() const { return SubtractStep; }
2248 /// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00002249 Expr *BuildNumIterations(Scope *S, const bool LimitedType) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00002250 /// \brief Build the precondition expression for the loops.
2251 Expr *BuildPreCond(Scope *S, Expr *Cond) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002252 /// \brief Build reference expression to the counter be used for codegen.
2253 Expr *BuildCounterVar() const;
Alexey Bataeva8899172015-08-06 12:30:57 +00002254 /// \brief Build reference expression to the private counter be used for
2255 /// codegen.
2256 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002257 /// \brief Build initization of the counter be used for codegen.
2258 Expr *BuildCounterInit() const;
2259 /// \brief Build step of the counter be used for codegen.
2260 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002261 /// \brief Return true if any expression is dependent.
2262 bool Dependent() const;
2263
2264private:
2265 /// \brief Check the right-hand side of an assignment in the increment
2266 /// expression.
2267 bool CheckIncRHS(Expr *RHS);
2268 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002269 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002270 /// \brief Helper to set upper bound.
2271 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, const SourceRange &SR,
2272 const SourceLocation &SL);
2273 /// \brief Helper to set loop increment.
2274 bool SetStep(Expr *NewStep, bool Subtract);
2275};
2276
2277bool OpenMPIterationSpaceChecker::Dependent() const {
2278 if (!Var) {
2279 assert(!LB && !UB && !Step);
2280 return false;
2281 }
2282 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
2283 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
2284}
2285
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002286template <typename T>
2287static T *getExprAsWritten(T *E) {
2288 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
2289 E = ExprTemp->getSubExpr();
2290
2291 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
2292 E = MTE->GetTemporaryExpr();
2293
2294 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
2295 E = Binder->getSubExpr();
2296
2297 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
2298 E = ICE->getSubExprAsWritten();
2299 return E->IgnoreParens();
2300}
2301
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002302bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
2303 DeclRefExpr *NewVarRefExpr,
2304 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002305 // State consistency checking to ensure correct usage.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002306 assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
2307 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002308 if (!NewVar || !NewLB)
2309 return true;
2310 Var = NewVar;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002311 VarRef = NewVarRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002312 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
2313 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00002314 if ((Ctor->isCopyOrMoveConstructor() ||
2315 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
2316 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002317 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002318 LB = NewLB;
2319 return false;
2320}
2321
2322bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
2323 const SourceRange &SR,
2324 const SourceLocation &SL) {
2325 // State consistency checking to ensure correct usage.
2326 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
2327 !TestIsLessOp && !TestIsStrictOp);
2328 if (!NewUB)
2329 return true;
2330 UB = NewUB;
2331 TestIsLessOp = LessOp;
2332 TestIsStrictOp = StrictOp;
2333 ConditionSrcRange = SR;
2334 ConditionLoc = SL;
2335 return false;
2336}
2337
2338bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
2339 // State consistency checking to ensure correct usage.
2340 assert(Var != nullptr && LB != nullptr && Step == nullptr);
2341 if (!NewStep)
2342 return true;
2343 if (!NewStep->isValueDependent()) {
2344 // Check that the step is integer expression.
2345 SourceLocation StepLoc = NewStep->getLocStart();
2346 ExprResult Val =
2347 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
2348 if (Val.isInvalid())
2349 return true;
2350 NewStep = Val.get();
2351
2352 // OpenMP [2.6, Canonical Loop Form, Restrictions]
2353 // If test-expr is of form var relational-op b and relational-op is < or
2354 // <= then incr-expr must cause var to increase on each iteration of the
2355 // loop. If test-expr is of form var relational-op b and relational-op is
2356 // > or >= then incr-expr must cause var to decrease on each iteration of
2357 // the loop.
2358 // If test-expr is of form b relational-op var and relational-op is < or
2359 // <= then incr-expr must cause var to decrease on each iteration of the
2360 // loop. If test-expr is of form b relational-op var and relational-op is
2361 // > or >= then incr-expr must cause var to increase on each iteration of
2362 // the loop.
2363 llvm::APSInt Result;
2364 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
2365 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
2366 bool IsConstNeg =
2367 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00002368 bool IsConstPos =
2369 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002370 bool IsConstZero = IsConstant && !Result.getBoolValue();
2371 if (UB && (IsConstZero ||
2372 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00002373 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002374 SemaRef.Diag(NewStep->getExprLoc(),
2375 diag::err_omp_loop_incr_not_compatible)
2376 << Var << TestIsLessOp << NewStep->getSourceRange();
2377 SemaRef.Diag(ConditionLoc,
2378 diag::note_omp_loop_cond_requres_compatible_incr)
2379 << TestIsLessOp << ConditionSrcRange;
2380 return true;
2381 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002382 if (TestIsLessOp == Subtract) {
2383 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
2384 NewStep).get();
2385 Subtract = !Subtract;
2386 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002387 }
2388
2389 Step = NewStep;
2390 SubtractStep = Subtract;
2391 return false;
2392}
2393
Alexey Bataev9c821032015-04-30 04:23:23 +00002394bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002395 // Check init-expr for canonical loop form and save loop counter
2396 // variable - #Var and its initialization value - #LB.
2397 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
2398 // var = lb
2399 // integer-type var = lb
2400 // random-access-iterator-type var = lb
2401 // pointer-type var = lb
2402 //
2403 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00002404 if (EmitDiags) {
2405 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
2406 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002407 return true;
2408 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002409 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002410 if (Expr *E = dyn_cast<Expr>(S))
2411 S = E->IgnoreParens();
2412 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2413 if (BO->getOpcode() == BO_Assign)
2414 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002415 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002416 BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002417 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
2418 if (DS->isSingleDecl()) {
2419 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00002420 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002421 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00002422 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002423 SemaRef.Diag(S->getLocStart(),
2424 diag::ext_omp_loop_not_canonical_init)
2425 << S->getSourceRange();
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002426 return SetVarAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002427 }
2428 }
2429 }
2430 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
2431 if (CE->getOperator() == OO_Equal)
2432 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002433 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
2434 CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002435
Alexey Bataev9c821032015-04-30 04:23:23 +00002436 if (EmitDiags) {
2437 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
2438 << S->getSourceRange();
2439 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002440 return true;
2441}
2442
Alexey Bataev23b69422014-06-18 07:08:49 +00002443/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002444/// variable (which may be the loop variable) if possible.
2445static const VarDecl *GetInitVarDecl(const Expr *E) {
2446 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00002447 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002448 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002449 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
2450 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00002451 if ((Ctor->isCopyOrMoveConstructor() ||
2452 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
2453 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002454 E = CE->getArg(0)->IgnoreParenImpCasts();
2455 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
2456 if (!DRE)
2457 return nullptr;
2458 return dyn_cast<VarDecl>(DRE->getDecl());
2459}
2460
2461bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
2462 // Check test-expr for canonical form, save upper-bound UB, flags for
2463 // less/greater and for strict/non-strict comparison.
2464 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2465 // var relational-op b
2466 // b relational-op var
2467 //
2468 if (!S) {
2469 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
2470 return true;
2471 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002472 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002473 SourceLocation CondLoc = S->getLocStart();
2474 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2475 if (BO->isRelationalOp()) {
2476 if (GetInitVarDecl(BO->getLHS()) == Var)
2477 return SetUB(BO->getRHS(),
2478 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
2479 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2480 BO->getSourceRange(), BO->getOperatorLoc());
2481 if (GetInitVarDecl(BO->getRHS()) == Var)
2482 return SetUB(BO->getLHS(),
2483 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
2484 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2485 BO->getSourceRange(), BO->getOperatorLoc());
2486 }
2487 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2488 if (CE->getNumArgs() == 2) {
2489 auto Op = CE->getOperator();
2490 switch (Op) {
2491 case OO_Greater:
2492 case OO_GreaterEqual:
2493 case OO_Less:
2494 case OO_LessEqual:
2495 if (GetInitVarDecl(CE->getArg(0)) == Var)
2496 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
2497 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2498 CE->getOperatorLoc());
2499 if (GetInitVarDecl(CE->getArg(1)) == Var)
2500 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
2501 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2502 CE->getOperatorLoc());
2503 break;
2504 default:
2505 break;
2506 }
2507 }
2508 }
2509 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
2510 << S->getSourceRange() << Var;
2511 return true;
2512}
2513
2514bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
2515 // RHS of canonical loop form increment can be:
2516 // var + incr
2517 // incr + var
2518 // var - incr
2519 //
2520 RHS = RHS->IgnoreParenImpCasts();
2521 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
2522 if (BO->isAdditiveOp()) {
2523 bool IsAdd = BO->getOpcode() == BO_Add;
2524 if (GetInitVarDecl(BO->getLHS()) == Var)
2525 return SetStep(BO->getRHS(), !IsAdd);
2526 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
2527 return SetStep(BO->getLHS(), false);
2528 }
2529 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
2530 bool IsAdd = CE->getOperator() == OO_Plus;
2531 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
2532 if (GetInitVarDecl(CE->getArg(0)) == Var)
2533 return SetStep(CE->getArg(1), !IsAdd);
2534 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
2535 return SetStep(CE->getArg(0), false);
2536 }
2537 }
2538 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2539 << RHS->getSourceRange() << Var;
2540 return true;
2541}
2542
2543bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
2544 // Check incr-expr for canonical loop form and return true if it
2545 // does not conform.
2546 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2547 // ++var
2548 // var++
2549 // --var
2550 // var--
2551 // var += incr
2552 // var -= incr
2553 // var = var + incr
2554 // var = incr + var
2555 // var = var - incr
2556 //
2557 if (!S) {
2558 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
2559 return true;
2560 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002561 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002562 S = S->IgnoreParens();
2563 if (auto UO = dyn_cast<UnaryOperator>(S)) {
2564 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
2565 return SetStep(
2566 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
2567 (UO->isDecrementOp() ? -1 : 1)).get(),
2568 false);
2569 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
2570 switch (BO->getOpcode()) {
2571 case BO_AddAssign:
2572 case BO_SubAssign:
2573 if (GetInitVarDecl(BO->getLHS()) == Var)
2574 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
2575 break;
2576 case BO_Assign:
2577 if (GetInitVarDecl(BO->getLHS()) == Var)
2578 return CheckIncRHS(BO->getRHS());
2579 break;
2580 default:
2581 break;
2582 }
2583 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2584 switch (CE->getOperator()) {
2585 case OO_PlusPlus:
2586 case OO_MinusMinus:
2587 if (GetInitVarDecl(CE->getArg(0)) == Var)
2588 return SetStep(
2589 SemaRef.ActOnIntegerConstant(
2590 CE->getLocStart(),
2591 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
2592 false);
2593 break;
2594 case OO_PlusEqual:
2595 case OO_MinusEqual:
2596 if (GetInitVarDecl(CE->getArg(0)) == Var)
2597 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
2598 break;
2599 case OO_Equal:
2600 if (GetInitVarDecl(CE->getArg(0)) == Var)
2601 return CheckIncRHS(CE->getArg(1));
2602 break;
2603 default:
2604 break;
2605 }
2606 }
2607 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2608 << S->getSourceRange() << Var;
2609 return true;
2610}
Alexander Musmana5f070a2014-10-01 06:03:56 +00002611
2612/// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00002613Expr *
2614OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S,
2615 const bool LimitedType) const {
Alexander Musmana5f070a2014-10-01 06:03:56 +00002616 ExprResult Diff;
2617 if (Var->getType()->isIntegerType() || Var->getType()->isPointerType() ||
2618 SemaRef.getLangOpts().CPlusPlus) {
2619 // Upper - Lower
2620 Expr *Upper = TestIsLessOp ? UB : LB;
2621 Expr *Lower = TestIsLessOp ? LB : UB;
2622
2623 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
2624
2625 if (!Diff.isUsable() && Var->getType()->getAsCXXRecordDecl()) {
2626 // BuildBinOp already emitted error, this one is to point user to upper
2627 // and lower bound, and to tell what is passed to 'operator-'.
2628 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
2629 << Upper->getSourceRange() << Lower->getSourceRange();
2630 return nullptr;
2631 }
2632 }
2633
2634 if (!Diff.isUsable())
2635 return nullptr;
2636
2637 // Upper - Lower [- 1]
2638 if (TestIsStrictOp)
2639 Diff = SemaRef.BuildBinOp(
2640 S, DefaultLoc, BO_Sub, Diff.get(),
2641 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
2642 if (!Diff.isUsable())
2643 return nullptr;
2644
2645 // Upper - Lower [- 1] + Step
2646 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(),
2647 Step->IgnoreImplicit());
2648 if (!Diff.isUsable())
2649 return nullptr;
2650
2651 // Parentheses (for dumping/debugging purposes only).
2652 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
2653 if (!Diff.isUsable())
2654 return nullptr;
2655
2656 // (Upper - Lower [- 1] + Step) / Step
2657 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(),
2658 Step->IgnoreImplicit());
2659 if (!Diff.isUsable())
2660 return nullptr;
2661
Alexander Musman174b3ca2014-10-06 11:16:29 +00002662 // OpenMP runtime requires 32-bit or 64-bit loop variables.
2663 if (LimitedType) {
2664 auto &C = SemaRef.Context;
2665 QualType Type = Diff.get()->getType();
2666 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
2667 if (NewSize != C.getTypeSize(Type)) {
2668 if (NewSize < C.getTypeSize(Type)) {
2669 assert(NewSize == 64 && "incorrect loop var size");
2670 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
2671 << InitSrcRange << ConditionSrcRange;
2672 }
2673 QualType NewType = C.getIntTypeForBitwidth(
2674 NewSize, Type->hasSignedIntegerRepresentation());
2675 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
2676 Sema::AA_Converting, true);
2677 if (!Diff.isUsable())
2678 return nullptr;
2679 }
2680 }
2681
Alexander Musmana5f070a2014-10-01 06:03:56 +00002682 return Diff.get();
2683}
2684
Alexey Bataev62dbb972015-04-22 11:59:37 +00002685Expr *OpenMPIterationSpaceChecker::BuildPreCond(Scope *S, Expr *Cond) const {
2686 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
2687 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
2688 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
2689 auto CondExpr = SemaRef.BuildBinOp(
2690 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
2691 : (TestIsStrictOp ? BO_GT : BO_GE),
2692 LB, UB);
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002693 if (CondExpr.isUsable()) {
2694 CondExpr = SemaRef.PerformImplicitConversion(
2695 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
2696 /*AllowExplicit=*/true);
2697 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00002698 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
2699 // Otherwise use original loop conditon and evaluate it in runtime.
2700 return CondExpr.isUsable() ? CondExpr.get() : Cond;
2701}
2702
Alexander Musmana5f070a2014-10-01 06:03:56 +00002703/// \brief Build reference expression to the counter be used for codegen.
2704Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const {
Alexey Bataeva8899172015-08-06 12:30:57 +00002705 return buildDeclRefExpr(SemaRef, Var, Var->getType().getNonReferenceType(),
2706 DefaultLoc);
2707}
2708
2709Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
2710 if (Var && !Var->isInvalidDecl()) {
2711 auto Type = Var->getType().getNonReferenceType();
2712 auto *PrivateVar = buildVarDecl(SemaRef, DefaultLoc, Type, Var->getName());
2713 if (PrivateVar->isInvalidDecl())
2714 return nullptr;
2715 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
2716 }
2717 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002718}
2719
2720/// \brief Build initization of the counter be used for codegen.
2721Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
2722
2723/// \brief Build step of the counter be used for codegen.
2724Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
2725
2726/// \brief Iteration space of a single for loop.
2727struct LoopIterationSpace {
Alexey Bataev62dbb972015-04-22 11:59:37 +00002728 /// \brief Condition of the loop.
2729 Expr *PreCond;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002730 /// \brief This expression calculates the number of iterations in the loop.
2731 /// It is always possible to calculate it before starting the loop.
2732 Expr *NumIterations;
2733 /// \brief The loop counter variable.
2734 Expr *CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00002735 /// \brief Private loop counter variable.
2736 Expr *PrivateCounterVar;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002737 /// \brief This is initializer for the initial value of #CounterVar.
2738 Expr *CounterInit;
2739 /// \brief This is step for the #CounterVar used to generate its update:
2740 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
2741 Expr *CounterStep;
2742 /// \brief Should step be subtracted?
2743 bool Subtract;
2744 /// \brief Source range of the loop init.
2745 SourceRange InitSrcRange;
2746 /// \brief Source range of the loop condition.
2747 SourceRange CondSrcRange;
2748 /// \brief Source range of the loop increment.
2749 SourceRange IncSrcRange;
2750};
2751
Alexey Bataev23b69422014-06-18 07:08:49 +00002752} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002753
Alexey Bataev9c821032015-04-30 04:23:23 +00002754void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
2755 assert(getLangOpts().OpenMP && "OpenMP is not active.");
2756 assert(Init && "Expected loop in canonical form.");
2757 unsigned CollapseIteration = DSAStack->getCollapseNumber();
2758 if (CollapseIteration > 0 &&
2759 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
2760 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
2761 if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
2762 DSAStack->addLoopControlVariable(ISC.GetLoopVar());
2763 }
2764 DSAStack->setCollapseNumber(CollapseIteration - 1);
2765 }
2766}
2767
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002768/// \brief Called on a for stmt to check and extract its iteration space
2769/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00002770static bool CheckOpenMPIterationSpace(
2771 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
2772 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00002773 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002774 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
2775 LoopIterationSpace &ResultIterSpace) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002776 // OpenMP [2.6, Canonical Loop Form]
2777 // for (init-expr; test-expr; incr-expr) structured-block
2778 auto For = dyn_cast_or_null<ForStmt>(S);
2779 if (!For) {
2780 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00002781 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
2782 << getOpenMPDirectiveName(DKind) << NestedLoopCount
2783 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
2784 if (NestedLoopCount > 1) {
2785 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
2786 SemaRef.Diag(DSA.getConstructLoc(),
2787 diag::note_omp_collapse_ordered_expr)
2788 << 2 << CollapseLoopCountExpr->getSourceRange()
2789 << OrderedLoopCountExpr->getSourceRange();
2790 else if (CollapseLoopCountExpr)
2791 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
2792 diag::note_omp_collapse_ordered_expr)
2793 << 0 << CollapseLoopCountExpr->getSourceRange();
2794 else
2795 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
2796 diag::note_omp_collapse_ordered_expr)
2797 << 1 << OrderedLoopCountExpr->getSourceRange();
2798 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002799 return true;
2800 }
2801 assert(For->getBody());
2802
2803 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
2804
2805 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002806 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002807 if (ISC.CheckInit(Init)) {
2808 return true;
2809 }
2810
2811 bool HasErrors = false;
2812
2813 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002814 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002815
2816 // OpenMP [2.6, Canonical Loop Form]
2817 // Var is one of the following:
2818 // A variable of signed or unsigned integer type.
2819 // For C++, a variable of a random access iterator type.
2820 // For C, a variable of a pointer type.
Alexey Bataeva8899172015-08-06 12:30:57 +00002821 auto VarType = Var->getType().getNonReferenceType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002822 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
2823 !VarType->isPointerType() &&
2824 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
2825 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
2826 << SemaRef.getLangOpts().CPlusPlus;
2827 HasErrors = true;
2828 }
2829
Alexey Bataev4acb8592014-07-07 13:01:15 +00002830 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
2831 // Construct
2832 // The loop iteration variable(s) in the associated for-loop(s) of a for or
2833 // parallel for construct is (are) private.
2834 // The loop iteration variable in the associated for-loop of a simd construct
2835 // with just one associated for-loop is linear with a constant-linear-step
2836 // that is the increment of the associated for-loop.
2837 // Exclude loop var from the list of variables with implicitly defined data
2838 // sharing attributes.
Benjamin Kramerad8e0792014-10-10 15:32:48 +00002839 VarsWithImplicitDSA.erase(Var);
Alexey Bataev4acb8592014-07-07 13:01:15 +00002840
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002841 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
2842 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00002843 // The loop iteration variable in the associated for-loop of a simd construct
2844 // with just one associated for-loop may be listed in a linear clause with a
2845 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002846 // The loop iteration variable(s) in the associated for-loop(s) of a for or
2847 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002848 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002849 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
2850 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
2851 // declared in the loop and it is predetermined as a private.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002852 auto PredeterminedCKind =
2853 isOpenMPSimdDirective(DKind)
2854 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
2855 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002856 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataev0c024df2015-05-12 09:02:07 +00002857 DVar.CKind != OMPC_threadprivate && DVar.CKind != PredeterminedCKind) ||
Alexander Musmanf82886e2014-09-18 05:12:34 +00002858 (isOpenMPWorksharingDirective(DKind) && !isOpenMPSimdDirective(DKind) &&
2859 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private &&
Alexey Bataev0c024df2015-05-12 09:02:07 +00002860 DVar.CKind != OMPC_lastprivate && DVar.CKind != OMPC_threadprivate)) &&
2861 ((DVar.CKind != OMPC_private && DVar.CKind != OMPC_threadprivate) ||
2862 DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002863 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00002864 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
2865 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataevf2453a02015-05-06 07:25:08 +00002866 if (DVar.RefExpr == nullptr)
2867 DVar.CKind = PredeterminedCKind;
2868 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, /*IsLoopIterVar=*/true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002869 HasErrors = true;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002870 } else if (LoopVarRefExpr != nullptr) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00002871 // Make the loop iteration variable private (for worksharing constructs),
2872 // linear (for simd directives with the only one associated loop) or
Alexey Bataev10e775f2015-07-30 11:36:16 +00002873 // lastprivate (for simd directives with several collapsed or ordered
2874 // loops).
Alexey Bataev9aba41c2014-11-14 04:08:45 +00002875 if (DVar.CKind == OMPC_unknown)
2876 DVar = DSA.hasDSA(Var, isOpenMPPrivate, MatchesAlways(),
2877 /*FromParent=*/false);
Alexey Bataev9c821032015-04-30 04:23:23 +00002878 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002879 }
2880
Alexey Bataev7ff55242014-06-19 09:13:45 +00002881 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00002882
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002883 // Check test-expr.
2884 HasErrors |= ISC.CheckCond(For->getCond());
2885
2886 // Check incr-expr.
2887 HasErrors |= ISC.CheckInc(For->getInc());
2888
Alexander Musmana5f070a2014-10-01 06:03:56 +00002889 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002890 return HasErrors;
2891
Alexander Musmana5f070a2014-10-01 06:03:56 +00002892 // Build the loop's iteration space representation.
Alexey Bataev62dbb972015-04-22 11:59:37 +00002893 ResultIterSpace.PreCond = ISC.BuildPreCond(DSA.getCurScope(), For->getCond());
Alexander Musman174b3ca2014-10-06 11:16:29 +00002894 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
2895 DSA.getCurScope(), /* LimitedType */ isOpenMPWorksharingDirective(DKind));
Alexander Musmana5f070a2014-10-01 06:03:56 +00002896 ResultIterSpace.CounterVar = ISC.BuildCounterVar();
Alexey Bataeva8899172015-08-06 12:30:57 +00002897 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00002898 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
2899 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
2900 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
2901 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
2902 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
2903 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
2904
Alexey Bataev62dbb972015-04-22 11:59:37 +00002905 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
2906 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00002907 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00002908 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00002909 ResultIterSpace.CounterInit == nullptr ||
2910 ResultIterSpace.CounterStep == nullptr);
2911
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002912 return HasErrors;
2913}
2914
Alexander Musmana5f070a2014-10-01 06:03:56 +00002915/// \brief Build 'VarRef = Start + Iter * Step'.
2916static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S,
2917 SourceLocation Loc, ExprResult VarRef,
2918 ExprResult Start, ExprResult Iter,
2919 ExprResult Step, bool Subtract) {
2920 // Add parentheses (for debugging purposes only).
2921 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
2922 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
2923 !Step.isUsable())
2924 return ExprError();
2925
2926 ExprResult Update = SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(),
2927 Step.get()->IgnoreImplicit());
2928 if (!Update.isUsable())
2929 return ExprError();
2930
2931 // Build 'VarRef = Start + Iter * Step'.
2932 Update = SemaRef.BuildBinOp(S, Loc, (Subtract ? BO_Sub : BO_Add),
2933 Start.get()->IgnoreImplicit(), Update.get());
2934 if (!Update.isUsable())
2935 return ExprError();
2936
2937 Update = SemaRef.PerformImplicitConversion(
2938 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
2939 if (!Update.isUsable())
2940 return ExprError();
2941
2942 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
2943 return Update;
2944}
2945
2946/// \brief Convert integer expression \a E to make it have at least \a Bits
2947/// bits.
2948static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
2949 Sema &SemaRef) {
2950 if (E == nullptr)
2951 return ExprError();
2952 auto &C = SemaRef.Context;
2953 QualType OldType = E->getType();
2954 unsigned HasBits = C.getTypeSize(OldType);
2955 if (HasBits >= Bits)
2956 return ExprResult(E);
2957 // OK to convert to signed, because new type has more bits than old.
2958 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
2959 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
2960 true);
2961}
2962
2963/// \brief Check if the given expression \a E is a constant integer that fits
2964/// into \a Bits bits.
2965static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
2966 if (E == nullptr)
2967 return false;
2968 llvm::APSInt Result;
2969 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
2970 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
2971 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002972}
2973
2974/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00002975/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
2976/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002977static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00002978CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
2979 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
2980 DSAStackTy &DSA,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002981 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00002982 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002983 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00002984 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002985 // Found 'collapse' clause - calculate collapse number.
2986 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00002987 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
2988 NestedLoopCount += Result.getLimitedValue() - 1;
2989 }
2990 if (OrderedLoopCountExpr) {
2991 // Found 'ordered' clause - calculate collapse number.
2992 llvm::APSInt Result;
2993 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
2994 NestedLoopCount += Result.getLimitedValue() - 1;
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002995 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002996 // This is helper routine for loop directives (e.g., 'for', 'simd',
2997 // 'for simd', etc.).
Alexander Musmana5f070a2014-10-01 06:03:56 +00002998 SmallVector<LoopIterationSpace, 4> IterSpaces;
2999 IterSpaces.resize(NestedLoopCount);
3000 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003001 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003002 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003003 NestedLoopCount, CollapseLoopCountExpr,
3004 OrderedLoopCountExpr, VarsWithImplicitDSA,
3005 IterSpaces[Cnt]))
Alexey Bataevabfc0692014-06-25 06:52:00 +00003006 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003007 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003008 // OpenMP [2.8.1, simd construct, Restrictions]
3009 // All loops associated with the construct must be perfectly nested; that
3010 // is, there must be no intervening code nor any OpenMP directive between
3011 // any two loops.
3012 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003013 }
3014
Alexander Musmana5f070a2014-10-01 06:03:56 +00003015 Built.clear(/* size */ NestedLoopCount);
3016
3017 if (SemaRef.CurContext->isDependentContext())
3018 return NestedLoopCount;
3019
3020 // An example of what is generated for the following code:
3021 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00003022 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00003023 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003024 // for (k = 0; k < NK; ++k)
3025 // for (j = J0; j < NJ; j+=2) {
3026 // <loop body>
3027 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003028 //
3029 // We generate the code below.
3030 // Note: the loop body may be outlined in CodeGen.
3031 // Note: some counters may be C++ classes, operator- is used to find number of
3032 // iterations and operator+= to calculate counter value.
3033 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
3034 // or i64 is currently supported).
3035 //
3036 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
3037 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
3038 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
3039 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
3040 // // similar updates for vars in clauses (e.g. 'linear')
3041 // <loop body (using local i and j)>
3042 // }
3043 // i = NI; // assign final values of counters
3044 // j = NJ;
3045 //
3046
3047 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
3048 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00003049 // Precondition tests if there is at least one iteration (all conditions are
3050 // true).
3051 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003052 auto N0 = IterSpaces[0].NumIterations;
3053 ExprResult LastIteration32 = WidenIterationCount(32 /* Bits */, N0, SemaRef);
3054 ExprResult LastIteration64 = WidenIterationCount(64 /* Bits */, N0, SemaRef);
3055
3056 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
3057 return NestedLoopCount;
3058
3059 auto &C = SemaRef.Context;
3060 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
3061
3062 Scope *CurScope = DSA.getCurScope();
3063 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003064 if (PreCond.isUsable()) {
3065 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
3066 PreCond.get(), IterSpaces[Cnt].PreCond);
3067 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003068 auto N = IterSpaces[Cnt].NumIterations;
3069 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
3070 if (LastIteration32.isUsable())
3071 LastIteration32 = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_Mul,
3072 LastIteration32.get(), N);
3073 if (LastIteration64.isUsable())
3074 LastIteration64 = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_Mul,
3075 LastIteration64.get(), N);
3076 }
3077
3078 // Choose either the 32-bit or 64-bit version.
3079 ExprResult LastIteration = LastIteration64;
3080 if (LastIteration32.isUsable() &&
3081 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
3082 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
3083 FitsInto(
3084 32 /* Bits */,
3085 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
3086 LastIteration64.get(), SemaRef)))
3087 LastIteration = LastIteration32;
3088
3089 if (!LastIteration.isUsable())
3090 return 0;
3091
3092 // Save the number of iterations.
3093 ExprResult NumIterations = LastIteration;
3094 {
3095 LastIteration = SemaRef.BuildBinOp(
3096 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
3097 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3098 if (!LastIteration.isUsable())
3099 return 0;
3100 }
3101
3102 // Calculate the last iteration number beforehand instead of doing this on
3103 // each iteration. Do not do this if the number of iterations may be kfold-ed.
3104 llvm::APSInt Result;
3105 bool IsConstant =
3106 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
3107 ExprResult CalcLastIteration;
3108 if (!IsConstant) {
3109 SourceLocation SaveLoc;
3110 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00003111 buildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00003112 ".omp.last.iteration");
Alexey Bataev39f915b82015-05-08 10:41:21 +00003113 ExprResult SaveRef = buildDeclRefExpr(
3114 SemaRef, SaveVar, LastIteration.get()->getType(), SaveLoc);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003115 CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign,
3116 SaveRef.get(), LastIteration.get());
3117 LastIteration = SaveRef;
3118
3119 // Prepare SaveRef + 1.
3120 NumIterations = SemaRef.BuildBinOp(
3121 CurScope, SaveLoc, BO_Add, SaveRef.get(),
3122 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3123 if (!NumIterations.isUsable())
3124 return 0;
3125 }
3126
3127 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
3128
Alexander Musmanc6388682014-12-15 07:07:06 +00003129 QualType VType = LastIteration.get()->getType();
3130 // Build variables passed into runtime, nesessary for worksharing directives.
3131 ExprResult LB, UB, IL, ST, EUB;
3132 if (isOpenMPWorksharingDirective(DKind)) {
3133 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00003134 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
3135 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003136 SemaRef.AddInitializerToDecl(
3137 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
3138 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3139
3140 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00003141 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
3142 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003143 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
3144 /*DirectInit*/ false,
3145 /*TypeMayContainAuto*/ false);
3146
3147 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
3148 // This will be used to implement clause 'lastprivate'.
3149 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00003150 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
3151 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003152 SemaRef.AddInitializerToDecl(
3153 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
3154 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3155
3156 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev39f915b82015-05-08 10:41:21 +00003157 VarDecl *STDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.stride");
3158 ST = buildDeclRefExpr(SemaRef, STDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003159 SemaRef.AddInitializerToDecl(
3160 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
3161 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3162
3163 // Build expression: UB = min(UB, LastIteration)
3164 // It is nesessary for CodeGen of directives with static scheduling.
3165 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
3166 UB.get(), LastIteration.get());
3167 ExprResult CondOp = SemaRef.ActOnConditionalOp(
3168 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
3169 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
3170 CondOp.get());
3171 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
3172 }
3173
3174 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003175 ExprResult IV;
3176 ExprResult Init;
3177 {
Alexey Bataev39f915b82015-05-08 10:41:21 +00003178 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.iv");
3179 IV = buildDeclRefExpr(SemaRef, IVDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003180 Expr *RHS = isOpenMPWorksharingDirective(DKind)
3181 ? LB.get()
3182 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
3183 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
3184 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003185 }
3186
Alexander Musmanc6388682014-12-15 07:07:06 +00003187 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003188 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00003189 ExprResult Cond =
3190 isOpenMPWorksharingDirective(DKind)
3191 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
3192 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
3193 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003194
3195 // Loop increment (IV = IV + 1)
3196 SourceLocation IncLoc;
3197 ExprResult Inc =
3198 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
3199 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
3200 if (!Inc.isUsable())
3201 return 0;
3202 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00003203 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
3204 if (!Inc.isUsable())
3205 return 0;
3206
3207 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
3208 // Used for directives with static scheduling.
3209 ExprResult NextLB, NextUB;
3210 if (isOpenMPWorksharingDirective(DKind)) {
3211 // LB + ST
3212 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
3213 if (!NextLB.isUsable())
3214 return 0;
3215 // LB = LB + ST
3216 NextLB =
3217 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
3218 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
3219 if (!NextLB.isUsable())
3220 return 0;
3221 // UB + ST
3222 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
3223 if (!NextUB.isUsable())
3224 return 0;
3225 // UB = UB + ST
3226 NextUB =
3227 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
3228 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
3229 if (!NextUB.isUsable())
3230 return 0;
3231 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003232
3233 // Build updates and final values of the loop counters.
3234 bool HasErrors = false;
3235 Built.Counters.resize(NestedLoopCount);
3236 Built.Updates.resize(NestedLoopCount);
3237 Built.Finals.resize(NestedLoopCount);
3238 {
3239 ExprResult Div;
3240 // Go from inner nested loop to outer.
3241 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
3242 LoopIterationSpace &IS = IterSpaces[Cnt];
3243 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
3244 // Build: Iter = (IV / Div) % IS.NumIters
3245 // where Div is product of previous iterations' IS.NumIters.
3246 ExprResult Iter;
3247 if (Div.isUsable()) {
3248 Iter =
3249 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
3250 } else {
3251 Iter = IV;
3252 assert((Cnt == (int)NestedLoopCount - 1) &&
3253 "unusable div expected on first iteration only");
3254 }
3255
3256 if (Cnt != 0 && Iter.isUsable())
3257 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
3258 IS.NumIterations);
3259 if (!Iter.isUsable()) {
3260 HasErrors = true;
3261 break;
3262 }
3263
Alexey Bataev39f915b82015-05-08 10:41:21 +00003264 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
3265 auto *CounterVar = buildDeclRefExpr(
3266 SemaRef, cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()),
3267 IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
3268 /*RefersToCapture=*/true);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003269 ExprResult Update =
Alexey Bataev39f915b82015-05-08 10:41:21 +00003270 BuildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003271 IS.CounterInit, Iter, IS.CounterStep, IS.Subtract);
3272 if (!Update.isUsable()) {
3273 HasErrors = true;
3274 break;
3275 }
3276
3277 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
3278 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00003279 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003280 IS.NumIterations, IS.CounterStep, IS.Subtract);
3281 if (!Final.isUsable()) {
3282 HasErrors = true;
3283 break;
3284 }
3285
3286 // Build Div for the next iteration: Div <- Div * IS.NumIters
3287 if (Cnt != 0) {
3288 if (Div.isUnset())
3289 Div = IS.NumIterations;
3290 else
3291 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
3292 IS.NumIterations);
3293
3294 // Add parentheses (for debugging purposes only).
3295 if (Div.isUsable())
3296 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
3297 if (!Div.isUsable()) {
3298 HasErrors = true;
3299 break;
3300 }
3301 }
3302 if (!Update.isUsable() || !Final.isUsable()) {
3303 HasErrors = true;
3304 break;
3305 }
3306 // Save results
3307 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00003308 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003309 Built.Updates[Cnt] = Update.get();
3310 Built.Finals[Cnt] = Final.get();
3311 }
3312 }
3313
3314 if (HasErrors)
3315 return 0;
3316
3317 // Save results
3318 Built.IterationVarRef = IV.get();
3319 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00003320 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003321 Built.CalcLastIteration =
3322 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003323 Built.PreCond = PreCond.get();
3324 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003325 Built.Init = Init.get();
3326 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00003327 Built.LB = LB.get();
3328 Built.UB = UB.get();
3329 Built.IL = IL.get();
3330 Built.ST = ST.get();
3331 Built.EUB = EUB.get();
3332 Built.NLB = NextLB.get();
3333 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003334
Alexey Bataevabfc0692014-06-25 06:52:00 +00003335 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003336}
3337
Alexey Bataev10e775f2015-07-30 11:36:16 +00003338static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Alexey Bataevc925aa32015-04-27 08:00:32 +00003339 auto &&CollapseFilter = [](const OMPClause *C) -> bool {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003340 return C->getClauseKind() == OMPC_collapse;
3341 };
3342 OMPExecutableDirective::filtered_clause_iterator<decltype(CollapseFilter)> I(
Alexey Bataevc925aa32015-04-27 08:00:32 +00003343 Clauses, std::move(CollapseFilter));
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003344 if (I)
3345 return cast<OMPCollapseClause>(*I)->getNumForLoops();
3346 return nullptr;
3347}
3348
Alexey Bataev10e775f2015-07-30 11:36:16 +00003349static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
3350 auto &&OrderedFilter = [](const OMPClause *C) -> bool {
3351 return C->getClauseKind() == OMPC_ordered;
3352 };
3353 OMPExecutableDirective::filtered_clause_iterator<decltype(OrderedFilter)> I(
3354 Clauses, std::move(OrderedFilter));
3355 if (I)
3356 return cast<OMPOrderedClause>(*I)->getNumForLoops();
3357 return nullptr;
3358}
3359
Alexey Bataev4acb8592014-07-07 13:01:15 +00003360StmtResult Sema::ActOnOpenMPSimdDirective(
3361 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3362 SourceLocation EndLoc,
3363 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmanc6388682014-12-15 07:07:06 +00003364 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003365 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
3366 // define the nested loops number.
3367 unsigned NestedLoopCount = CheckOpenMPLoop(
3368 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
3369 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00003370 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003371 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003372
Alexander Musmana5f070a2014-10-01 06:03:56 +00003373 assert((CurContext->isDependentContext() || B.builtAll()) &&
3374 "omp simd loop exprs were not built");
3375
Alexander Musman3276a272015-03-21 10:12:56 +00003376 if (!CurContext->isDependentContext()) {
3377 // Finalize the clauses that need pre-built expressions for CodeGen.
3378 for (auto C : Clauses) {
3379 if (auto LC = dyn_cast<OMPLinearClause>(C))
3380 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3381 B.NumIterations, *this, CurScope))
3382 return StmtError();
3383 }
3384 }
3385
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003386 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003387 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
3388 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003389}
3390
Alexey Bataev4acb8592014-07-07 13:01:15 +00003391StmtResult Sema::ActOnOpenMPForDirective(
3392 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3393 SourceLocation EndLoc,
3394 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmanc6388682014-12-15 07:07:06 +00003395 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003396 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
3397 // define the nested loops number.
3398 unsigned NestedLoopCount = CheckOpenMPLoop(
3399 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
3400 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00003401 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00003402 return StmtError();
3403
Alexander Musmana5f070a2014-10-01 06:03:56 +00003404 assert((CurContext->isDependentContext() || B.builtAll()) &&
3405 "omp for loop exprs were not built");
3406
Alexey Bataev54acd402015-08-04 11:18:19 +00003407 if (!CurContext->isDependentContext()) {
3408 // Finalize the clauses that need pre-built expressions for CodeGen.
3409 for (auto C : Clauses) {
3410 if (auto LC = dyn_cast<OMPLinearClause>(C))
3411 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3412 B.NumIterations, *this, CurScope))
3413 return StmtError();
3414 }
3415 }
3416
Alexey Bataevf29276e2014-06-18 04:14:57 +00003417 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003418 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
3419 Clauses, AStmt, B);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003420}
3421
Alexander Musmanf82886e2014-09-18 05:12:34 +00003422StmtResult Sema::ActOnOpenMPForSimdDirective(
3423 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3424 SourceLocation EndLoc,
3425 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmanc6388682014-12-15 07:07:06 +00003426 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003427 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
3428 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00003429 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00003430 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
3431 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
3432 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00003433 if (NestedLoopCount == 0)
3434 return StmtError();
3435
Alexander Musmanc6388682014-12-15 07:07:06 +00003436 assert((CurContext->isDependentContext() || B.builtAll()) &&
3437 "omp for simd loop exprs were not built");
3438
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00003439 if (!CurContext->isDependentContext()) {
3440 // Finalize the clauses that need pre-built expressions for CodeGen.
3441 for (auto C : Clauses) {
3442 if (auto LC = dyn_cast<OMPLinearClause>(C))
3443 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3444 B.NumIterations, *this, CurScope))
3445 return StmtError();
3446 }
3447 }
3448
Alexander Musmanf82886e2014-09-18 05:12:34 +00003449 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003450 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
3451 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00003452}
3453
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003454StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
3455 Stmt *AStmt,
3456 SourceLocation StartLoc,
3457 SourceLocation EndLoc) {
3458 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3459 auto BaseStmt = AStmt;
3460 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
3461 BaseStmt = CS->getCapturedStmt();
3462 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
3463 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00003464 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003465 return StmtError();
3466 // All associated statements must be '#pragma omp section' except for
3467 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00003468 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003469 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
3470 if (SectionStmt)
3471 Diag(SectionStmt->getLocStart(),
3472 diag::err_omp_sections_substmt_not_section);
3473 return StmtError();
3474 }
3475 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003476 } else {
3477 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
3478 return StmtError();
3479 }
3480
3481 getCurFunction()->setHasBranchProtectedScope();
3482
3483 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses,
3484 AStmt);
3485}
3486
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003487StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
3488 SourceLocation StartLoc,
3489 SourceLocation EndLoc) {
3490 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3491
3492 getCurFunction()->setHasBranchProtectedScope();
3493
3494 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt);
3495}
3496
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003497StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
3498 Stmt *AStmt,
3499 SourceLocation StartLoc,
3500 SourceLocation EndLoc) {
Alexey Bataev74a05c92014-07-15 02:55:09 +00003501 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3502
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003503 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00003504
Alexey Bataev3255bf32015-01-19 05:20:46 +00003505 // OpenMP [2.7.3, single Construct, Restrictions]
3506 // The copyprivate clause must not be used with the nowait clause.
3507 OMPClause *Nowait = nullptr;
3508 OMPClause *Copyprivate = nullptr;
3509 for (auto *Clause : Clauses) {
3510 if (Clause->getClauseKind() == OMPC_nowait)
3511 Nowait = Clause;
3512 else if (Clause->getClauseKind() == OMPC_copyprivate)
3513 Copyprivate = Clause;
3514 if (Copyprivate && Nowait) {
3515 Diag(Copyprivate->getLocStart(),
3516 diag::err_omp_single_copyprivate_with_nowait);
3517 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
3518 return StmtError();
3519 }
3520 }
3521
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003522 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3523}
3524
Alexander Musman80c22892014-07-17 08:54:58 +00003525StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
3526 SourceLocation StartLoc,
3527 SourceLocation EndLoc) {
3528 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3529
3530 getCurFunction()->setHasBranchProtectedScope();
3531
3532 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
3533}
3534
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003535StmtResult
3536Sema::ActOnOpenMPCriticalDirective(const DeclarationNameInfo &DirName,
3537 Stmt *AStmt, SourceLocation StartLoc,
3538 SourceLocation EndLoc) {
3539 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3540
3541 getCurFunction()->setHasBranchProtectedScope();
3542
3543 return OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
3544 AStmt);
3545}
3546
Alexey Bataev4acb8592014-07-07 13:01:15 +00003547StmtResult Sema::ActOnOpenMPParallelForDirective(
3548 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3549 SourceLocation EndLoc,
3550 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
3551 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3552 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3553 // 1.2.2 OpenMP Language Terminology
3554 // Structured block - An executable statement with a single entry at the
3555 // top and a single exit at the bottom.
3556 // The point of exit cannot be a branch out of the structured block.
3557 // longjmp() and throw() must not violate the entry/exit criteria.
3558 CS->getCapturedDecl()->setNothrow();
3559
Alexander Musmanc6388682014-12-15 07:07:06 +00003560 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003561 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
3562 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003563 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00003564 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
3565 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
3566 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003567 if (NestedLoopCount == 0)
3568 return StmtError();
3569
Alexander Musmana5f070a2014-10-01 06:03:56 +00003570 assert((CurContext->isDependentContext() || B.builtAll()) &&
3571 "omp parallel for loop exprs were not built");
3572
Alexey Bataev54acd402015-08-04 11:18:19 +00003573 if (!CurContext->isDependentContext()) {
3574 // Finalize the clauses that need pre-built expressions for CodeGen.
3575 for (auto C : Clauses) {
3576 if (auto LC = dyn_cast<OMPLinearClause>(C))
3577 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3578 B.NumIterations, *this, CurScope))
3579 return StmtError();
3580 }
3581 }
3582
Alexey Bataev4acb8592014-07-07 13:01:15 +00003583 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003584 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
3585 NestedLoopCount, Clauses, AStmt, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003586}
3587
Alexander Musmane4e893b2014-09-23 09:33:00 +00003588StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
3589 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3590 SourceLocation EndLoc,
3591 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
3592 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3593 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3594 // 1.2.2 OpenMP Language Terminology
3595 // Structured block - An executable statement with a single entry at the
3596 // top and a single exit at the bottom.
3597 // The point of exit cannot be a branch out of the structured block.
3598 // longjmp() and throw() must not violate the entry/exit criteria.
3599 CS->getCapturedDecl()->setNothrow();
3600
Alexander Musmanc6388682014-12-15 07:07:06 +00003601 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003602 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
3603 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00003604 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00003605 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
3606 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
3607 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003608 if (NestedLoopCount == 0)
3609 return StmtError();
3610
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00003611 if (!CurContext->isDependentContext()) {
3612 // Finalize the clauses that need pre-built expressions for CodeGen.
3613 for (auto C : Clauses) {
3614 if (auto LC = dyn_cast<OMPLinearClause>(C))
3615 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3616 B.NumIterations, *this, CurScope))
3617 return StmtError();
3618 }
3619 }
3620
Alexander Musmane4e893b2014-09-23 09:33:00 +00003621 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003622 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00003623 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003624}
3625
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003626StmtResult
3627Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
3628 Stmt *AStmt, SourceLocation StartLoc,
3629 SourceLocation EndLoc) {
3630 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3631 auto BaseStmt = AStmt;
3632 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
3633 BaseStmt = CS->getCapturedStmt();
3634 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
3635 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00003636 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003637 return StmtError();
3638 // All associated statements must be '#pragma omp section' except for
3639 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00003640 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003641 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
3642 if (SectionStmt)
3643 Diag(SectionStmt->getLocStart(),
3644 diag::err_omp_parallel_sections_substmt_not_section);
3645 return StmtError();
3646 }
3647 }
3648 } else {
3649 Diag(AStmt->getLocStart(),
3650 diag::err_omp_parallel_sections_not_compound_stmt);
3651 return StmtError();
3652 }
3653
3654 getCurFunction()->setHasBranchProtectedScope();
3655
3656 return OMPParallelSectionsDirective::Create(Context, StartLoc, EndLoc,
3657 Clauses, AStmt);
3658}
3659
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003660StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
3661 Stmt *AStmt, SourceLocation StartLoc,
3662 SourceLocation EndLoc) {
3663 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3664 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3665 // 1.2.2 OpenMP Language Terminology
3666 // Structured block - An executable statement with a single entry at the
3667 // top and a single exit at the bottom.
3668 // The point of exit cannot be a branch out of the structured block.
3669 // longjmp() and throw() must not violate the entry/exit criteria.
3670 CS->getCapturedDecl()->setNothrow();
3671
3672 getCurFunction()->setHasBranchProtectedScope();
3673
3674 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3675}
3676
Alexey Bataev68446b72014-07-18 07:47:19 +00003677StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
3678 SourceLocation EndLoc) {
3679 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
3680}
3681
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003682StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
3683 SourceLocation EndLoc) {
3684 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
3685}
3686
Alexey Bataev2df347a2014-07-18 10:17:07 +00003687StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
3688 SourceLocation EndLoc) {
3689 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
3690}
3691
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003692StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
3693 SourceLocation StartLoc,
3694 SourceLocation EndLoc) {
3695 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3696
3697 getCurFunction()->setHasBranchProtectedScope();
3698
3699 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
3700}
3701
Alexey Bataev6125da92014-07-21 11:26:11 +00003702StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
3703 SourceLocation StartLoc,
3704 SourceLocation EndLoc) {
3705 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
3706 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
3707}
3708
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003709StmtResult Sema::ActOnOpenMPOrderedDirective(Stmt *AStmt,
3710 SourceLocation StartLoc,
3711 SourceLocation EndLoc) {
3712 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3713
3714 getCurFunction()->setHasBranchProtectedScope();
3715
3716 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, AStmt);
3717}
3718
Alexey Bataev1d160b12015-03-13 12:27:31 +00003719namespace {
3720/// \brief Helper class for checking expression in 'omp atomic [update]'
3721/// construct.
3722class OpenMPAtomicUpdateChecker {
3723 /// \brief Error results for atomic update expressions.
3724 enum ExprAnalysisErrorCode {
3725 /// \brief A statement is not an expression statement.
3726 NotAnExpression,
3727 /// \brief Expression is not builtin binary or unary operation.
3728 NotABinaryOrUnaryExpression,
3729 /// \brief Unary operation is not post-/pre- increment/decrement operation.
3730 NotAnUnaryIncDecExpression,
3731 /// \brief An expression is not of scalar type.
3732 NotAScalarType,
3733 /// \brief A binary operation is not an assignment operation.
3734 NotAnAssignmentOp,
3735 /// \brief RHS part of the binary operation is not a binary expression.
3736 NotABinaryExpression,
3737 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
3738 /// expression.
3739 NotABinaryOperator,
3740 /// \brief RHS binary operation does not have reference to the updated LHS
3741 /// part.
3742 NotAnUpdateExpression,
3743 /// \brief No errors is found.
3744 NoError
3745 };
3746 /// \brief Reference to Sema.
3747 Sema &SemaRef;
3748 /// \brief A location for note diagnostics (when error is found).
3749 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003750 /// \brief 'x' lvalue part of the source atomic expression.
3751 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003752 /// \brief 'expr' rvalue part of the source atomic expression.
3753 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00003754 /// \brief Helper expression of the form
3755 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
3756 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
3757 Expr *UpdateExpr;
3758 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
3759 /// important for non-associative operations.
3760 bool IsXLHSInRHSPart;
3761 BinaryOperatorKind Op;
3762 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00003763 /// \brief true if the source expression is a postfix unary operation, false
3764 /// if it is a prefix unary operation.
3765 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003766
3767public:
3768 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00003769 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00003770 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00003771 /// \brief Check specified statement that it is suitable for 'atomic update'
3772 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00003773 /// expression. If DiagId and NoteId == 0, then only check is performed
3774 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00003775 /// \param DiagId Diagnostic which should be emitted if error is found.
3776 /// \param NoteId Diagnostic note for the main error message.
3777 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00003778 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00003779 /// \brief Return the 'x' lvalue part of the source atomic expression.
3780 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00003781 /// \brief Return the 'expr' rvalue part of the source atomic expression.
3782 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00003783 /// \brief Return the update expression used in calculation of the updated
3784 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
3785 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
3786 Expr *getUpdateExpr() const { return UpdateExpr; }
3787 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
3788 /// false otherwise.
3789 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
3790
Alexey Bataevb78ca832015-04-01 03:33:17 +00003791 /// \brief true if the source expression is a postfix unary operation, false
3792 /// if it is a prefix unary operation.
3793 bool isPostfixUpdate() const { return IsPostfixUpdate; }
3794
Alexey Bataev1d160b12015-03-13 12:27:31 +00003795private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00003796 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
3797 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00003798};
3799} // namespace
3800
3801bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
3802 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
3803 ExprAnalysisErrorCode ErrorFound = NoError;
3804 SourceLocation ErrorLoc, NoteLoc;
3805 SourceRange ErrorRange, NoteRange;
3806 // Allowed constructs are:
3807 // x = x binop expr;
3808 // x = expr binop x;
3809 if (AtomicBinOp->getOpcode() == BO_Assign) {
3810 X = AtomicBinOp->getLHS();
3811 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
3812 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
3813 if (AtomicInnerBinOp->isMultiplicativeOp() ||
3814 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
3815 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00003816 Op = AtomicInnerBinOp->getOpcode();
3817 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00003818 auto *LHS = AtomicInnerBinOp->getLHS();
3819 auto *RHS = AtomicInnerBinOp->getRHS();
3820 llvm::FoldingSetNodeID XId, LHSId, RHSId;
3821 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
3822 /*Canonical=*/true);
3823 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
3824 /*Canonical=*/true);
3825 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
3826 /*Canonical=*/true);
3827 if (XId == LHSId) {
3828 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00003829 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003830 } else if (XId == RHSId) {
3831 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00003832 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003833 } else {
3834 ErrorLoc = AtomicInnerBinOp->getExprLoc();
3835 ErrorRange = AtomicInnerBinOp->getSourceRange();
3836 NoteLoc = X->getExprLoc();
3837 NoteRange = X->getSourceRange();
3838 ErrorFound = NotAnUpdateExpression;
3839 }
3840 } else {
3841 ErrorLoc = AtomicInnerBinOp->getExprLoc();
3842 ErrorRange = AtomicInnerBinOp->getSourceRange();
3843 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
3844 NoteRange = SourceRange(NoteLoc, NoteLoc);
3845 ErrorFound = NotABinaryOperator;
3846 }
3847 } else {
3848 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
3849 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
3850 ErrorFound = NotABinaryExpression;
3851 }
3852 } else {
3853 ErrorLoc = AtomicBinOp->getExprLoc();
3854 ErrorRange = AtomicBinOp->getSourceRange();
3855 NoteLoc = AtomicBinOp->getOperatorLoc();
3856 NoteRange = SourceRange(NoteLoc, NoteLoc);
3857 ErrorFound = NotAnAssignmentOp;
3858 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00003859 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00003860 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
3861 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
3862 return true;
3863 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00003864 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00003865 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003866}
3867
3868bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
3869 unsigned NoteId) {
3870 ExprAnalysisErrorCode ErrorFound = NoError;
3871 SourceLocation ErrorLoc, NoteLoc;
3872 SourceRange ErrorRange, NoteRange;
3873 // Allowed constructs are:
3874 // x++;
3875 // x--;
3876 // ++x;
3877 // --x;
3878 // x binop= expr;
3879 // x = x binop expr;
3880 // x = expr binop x;
3881 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
3882 AtomicBody = AtomicBody->IgnoreParenImpCasts();
3883 if (AtomicBody->getType()->isScalarType() ||
3884 AtomicBody->isInstantiationDependent()) {
3885 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
3886 AtomicBody->IgnoreParenImpCasts())) {
3887 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00003888 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00003889 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00003890 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00003891 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00003892 X = AtomicCompAssignOp->getLHS();
3893 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003894 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
3895 AtomicBody->IgnoreParenImpCasts())) {
3896 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00003897 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
3898 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003899 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00003900 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
3901 // Check for Unary Operation
3902 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00003903 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00003904 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
3905 OpLoc = AtomicUnaryOp->getOperatorLoc();
3906 X = AtomicUnaryOp->getSubExpr();
3907 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
3908 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003909 } else {
3910 ErrorFound = NotAnUnaryIncDecExpression;
3911 ErrorLoc = AtomicUnaryOp->getExprLoc();
3912 ErrorRange = AtomicUnaryOp->getSourceRange();
3913 NoteLoc = AtomicUnaryOp->getOperatorLoc();
3914 NoteRange = SourceRange(NoteLoc, NoteLoc);
3915 }
3916 } else {
3917 ErrorFound = NotABinaryOrUnaryExpression;
3918 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
3919 NoteRange = ErrorRange = AtomicBody->getSourceRange();
3920 }
3921 } else {
3922 ErrorFound = NotAScalarType;
3923 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
3924 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
3925 }
3926 } else {
3927 ErrorFound = NotAnExpression;
3928 NoteLoc = ErrorLoc = S->getLocStart();
3929 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
3930 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00003931 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00003932 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
3933 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
3934 return true;
3935 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00003936 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00003937 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00003938 // Build an update expression of form 'OpaqueValueExpr(x) binop
3939 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
3940 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
3941 auto *OVEX = new (SemaRef.getASTContext())
3942 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
3943 auto *OVEExpr = new (SemaRef.getASTContext())
3944 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
3945 auto Update =
3946 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
3947 IsXLHSInRHSPart ? OVEExpr : OVEX);
3948 if (Update.isInvalid())
3949 return true;
3950 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
3951 Sema::AA_Casting);
3952 if (Update.isInvalid())
3953 return true;
3954 UpdateExpr = Update.get();
3955 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00003956 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003957}
3958
Alexey Bataev0162e452014-07-22 10:10:35 +00003959StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
3960 Stmt *AStmt,
3961 SourceLocation StartLoc,
3962 SourceLocation EndLoc) {
3963 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003964 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00003965 // 1.2.2 OpenMP Language Terminology
3966 // Structured block - An executable statement with a single entry at the
3967 // top and a single exit at the bottom.
3968 // The point of exit cannot be a branch out of the structured block.
3969 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00003970 OpenMPClauseKind AtomicKind = OMPC_unknown;
3971 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003972 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00003973 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00003974 C->getClauseKind() == OMPC_update ||
3975 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00003976 if (AtomicKind != OMPC_unknown) {
3977 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
3978 << SourceRange(C->getLocStart(), C->getLocEnd());
3979 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
3980 << getOpenMPClauseName(AtomicKind);
3981 } else {
3982 AtomicKind = C->getClauseKind();
3983 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003984 }
3985 }
3986 }
Alexey Bataev62cec442014-11-18 10:14:22 +00003987
Alexey Bataev459dec02014-07-24 06:46:57 +00003988 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00003989 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
3990 Body = EWC->getSubExpr();
3991
Alexey Bataev62cec442014-11-18 10:14:22 +00003992 Expr *X = nullptr;
3993 Expr *V = nullptr;
3994 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00003995 Expr *UE = nullptr;
3996 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00003997 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00003998 // OpenMP [2.12.6, atomic Construct]
3999 // In the next expressions:
4000 // * x and v (as applicable) are both l-value expressions with scalar type.
4001 // * During the execution of an atomic region, multiple syntactic
4002 // occurrences of x must designate the same storage location.
4003 // * Neither of v and expr (as applicable) may access the storage location
4004 // designated by x.
4005 // * Neither of x and expr (as applicable) may access the storage location
4006 // designated by v.
4007 // * expr is an expression with scalar type.
4008 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
4009 // * binop, binop=, ++, and -- are not overloaded operators.
4010 // * The expression x binop expr must be numerically equivalent to x binop
4011 // (expr). This requirement is satisfied if the operators in expr have
4012 // precedence greater than binop, or by using parentheses around expr or
4013 // subexpressions of expr.
4014 // * The expression expr binop x must be numerically equivalent to (expr)
4015 // binop x. This requirement is satisfied if the operators in expr have
4016 // precedence equal to or greater than binop, or by using parentheses around
4017 // expr or subexpressions of expr.
4018 // * For forms that allow multiple occurrences of x, the number of times
4019 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00004020 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004021 enum {
4022 NotAnExpression,
4023 NotAnAssignmentOp,
4024 NotAScalarType,
4025 NotAnLValue,
4026 NoError
4027 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00004028 SourceLocation ErrorLoc, NoteLoc;
4029 SourceRange ErrorRange, NoteRange;
4030 // If clause is read:
4031 // v = x;
4032 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
4033 auto AtomicBinOp =
4034 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
4035 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
4036 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
4037 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
4038 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
4039 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
4040 if (!X->isLValue() || !V->isLValue()) {
4041 auto NotLValueExpr = X->isLValue() ? V : X;
4042 ErrorFound = NotAnLValue;
4043 ErrorLoc = AtomicBinOp->getExprLoc();
4044 ErrorRange = AtomicBinOp->getSourceRange();
4045 NoteLoc = NotLValueExpr->getExprLoc();
4046 NoteRange = NotLValueExpr->getSourceRange();
4047 }
4048 } else if (!X->isInstantiationDependent() ||
4049 !V->isInstantiationDependent()) {
4050 auto NotScalarExpr =
4051 (X->isInstantiationDependent() || X->getType()->isScalarType())
4052 ? V
4053 : X;
4054 ErrorFound = NotAScalarType;
4055 ErrorLoc = AtomicBinOp->getExprLoc();
4056 ErrorRange = AtomicBinOp->getSourceRange();
4057 NoteLoc = NotScalarExpr->getExprLoc();
4058 NoteRange = NotScalarExpr->getSourceRange();
4059 }
4060 } else {
4061 ErrorFound = NotAnAssignmentOp;
4062 ErrorLoc = AtomicBody->getExprLoc();
4063 ErrorRange = AtomicBody->getSourceRange();
4064 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
4065 : AtomicBody->getExprLoc();
4066 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
4067 : AtomicBody->getSourceRange();
4068 }
4069 } else {
4070 ErrorFound = NotAnExpression;
4071 NoteLoc = ErrorLoc = Body->getLocStart();
4072 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00004073 }
Alexey Bataev62cec442014-11-18 10:14:22 +00004074 if (ErrorFound != NoError) {
4075 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
4076 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00004077 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
4078 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00004079 return StmtError();
4080 } else if (CurContext->isDependentContext())
4081 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00004082 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004083 enum {
4084 NotAnExpression,
4085 NotAnAssignmentOp,
4086 NotAScalarType,
4087 NotAnLValue,
4088 NoError
4089 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00004090 SourceLocation ErrorLoc, NoteLoc;
4091 SourceRange ErrorRange, NoteRange;
4092 // If clause is write:
4093 // x = expr;
4094 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
4095 auto AtomicBinOp =
4096 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
4097 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00004098 X = AtomicBinOp->getLHS();
4099 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00004100 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
4101 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
4102 if (!X->isLValue()) {
4103 ErrorFound = NotAnLValue;
4104 ErrorLoc = AtomicBinOp->getExprLoc();
4105 ErrorRange = AtomicBinOp->getSourceRange();
4106 NoteLoc = X->getExprLoc();
4107 NoteRange = X->getSourceRange();
4108 }
4109 } else if (!X->isInstantiationDependent() ||
4110 !E->isInstantiationDependent()) {
4111 auto NotScalarExpr =
4112 (X->isInstantiationDependent() || X->getType()->isScalarType())
4113 ? E
4114 : X;
4115 ErrorFound = NotAScalarType;
4116 ErrorLoc = AtomicBinOp->getExprLoc();
4117 ErrorRange = AtomicBinOp->getSourceRange();
4118 NoteLoc = NotScalarExpr->getExprLoc();
4119 NoteRange = NotScalarExpr->getSourceRange();
4120 }
4121 } else {
4122 ErrorFound = NotAnAssignmentOp;
4123 ErrorLoc = AtomicBody->getExprLoc();
4124 ErrorRange = AtomicBody->getSourceRange();
4125 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
4126 : AtomicBody->getExprLoc();
4127 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
4128 : AtomicBody->getSourceRange();
4129 }
4130 } else {
4131 ErrorFound = NotAnExpression;
4132 NoteLoc = ErrorLoc = Body->getLocStart();
4133 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00004134 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00004135 if (ErrorFound != NoError) {
4136 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
4137 << ErrorRange;
4138 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
4139 << NoteRange;
4140 return StmtError();
4141 } else if (CurContext->isDependentContext())
4142 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00004143 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004144 // If clause is update:
4145 // x++;
4146 // x--;
4147 // ++x;
4148 // --x;
4149 // x binop= expr;
4150 // x = x binop expr;
4151 // x = expr binop x;
4152 OpenMPAtomicUpdateChecker Checker(*this);
4153 if (Checker.checkStatement(
4154 Body, (AtomicKind == OMPC_update)
4155 ? diag::err_omp_atomic_update_not_expression_statement
4156 : diag::err_omp_atomic_not_expression_statement,
4157 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00004158 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00004159 if (!CurContext->isDependentContext()) {
4160 E = Checker.getExpr();
4161 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00004162 UE = Checker.getUpdateExpr();
4163 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00004164 }
Alexey Bataev459dec02014-07-24 06:46:57 +00004165 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004166 enum {
4167 NotAnAssignmentOp,
4168 NotACompoundStatement,
4169 NotTwoSubstatements,
4170 NotASpecificExpression,
4171 NoError
4172 } ErrorFound = NoError;
4173 SourceLocation ErrorLoc, NoteLoc;
4174 SourceRange ErrorRange, NoteRange;
4175 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
4176 // If clause is a capture:
4177 // v = x++;
4178 // v = x--;
4179 // v = ++x;
4180 // v = --x;
4181 // v = x binop= expr;
4182 // v = x = x binop expr;
4183 // v = x = expr binop x;
4184 auto *AtomicBinOp =
4185 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
4186 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
4187 V = AtomicBinOp->getLHS();
4188 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
4189 OpenMPAtomicUpdateChecker Checker(*this);
4190 if (Checker.checkStatement(
4191 Body, diag::err_omp_atomic_capture_not_expression_statement,
4192 diag::note_omp_atomic_update))
4193 return StmtError();
4194 E = Checker.getExpr();
4195 X = Checker.getX();
4196 UE = Checker.getUpdateExpr();
4197 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
4198 IsPostfixUpdate = Checker.isPostfixUpdate();
4199 } else {
4200 ErrorLoc = AtomicBody->getExprLoc();
4201 ErrorRange = AtomicBody->getSourceRange();
4202 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
4203 : AtomicBody->getExprLoc();
4204 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
4205 : AtomicBody->getSourceRange();
4206 ErrorFound = NotAnAssignmentOp;
4207 }
4208 if (ErrorFound != NoError) {
4209 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
4210 << ErrorRange;
4211 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
4212 return StmtError();
4213 } else if (CurContext->isDependentContext()) {
4214 UE = V = E = X = nullptr;
4215 }
4216 } else {
4217 // If clause is a capture:
4218 // { v = x; x = expr; }
4219 // { v = x; x++; }
4220 // { v = x; x--; }
4221 // { v = x; ++x; }
4222 // { v = x; --x; }
4223 // { v = x; x binop= expr; }
4224 // { v = x; x = x binop expr; }
4225 // { v = x; x = expr binop x; }
4226 // { x++; v = x; }
4227 // { x--; v = x; }
4228 // { ++x; v = x; }
4229 // { --x; v = x; }
4230 // { x binop= expr; v = x; }
4231 // { x = x binop expr; v = x; }
4232 // { x = expr binop x; v = x; }
4233 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
4234 // Check that this is { expr1; expr2; }
4235 if (CS->size() == 2) {
4236 auto *First = CS->body_front();
4237 auto *Second = CS->body_back();
4238 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
4239 First = EWC->getSubExpr()->IgnoreParenImpCasts();
4240 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
4241 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
4242 // Need to find what subexpression is 'v' and what is 'x'.
4243 OpenMPAtomicUpdateChecker Checker(*this);
4244 bool IsUpdateExprFound = !Checker.checkStatement(Second);
4245 BinaryOperator *BinOp = nullptr;
4246 if (IsUpdateExprFound) {
4247 BinOp = dyn_cast<BinaryOperator>(First);
4248 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
4249 }
4250 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
4251 // { v = x; x++; }
4252 // { v = x; x--; }
4253 // { v = x; ++x; }
4254 // { v = x; --x; }
4255 // { v = x; x binop= expr; }
4256 // { v = x; x = x binop expr; }
4257 // { v = x; x = expr binop x; }
4258 // Check that the first expression has form v = x.
4259 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
4260 llvm::FoldingSetNodeID XId, PossibleXId;
4261 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
4262 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
4263 IsUpdateExprFound = XId == PossibleXId;
4264 if (IsUpdateExprFound) {
4265 V = BinOp->getLHS();
4266 X = Checker.getX();
4267 E = Checker.getExpr();
4268 UE = Checker.getUpdateExpr();
4269 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00004270 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004271 }
4272 }
4273 if (!IsUpdateExprFound) {
4274 IsUpdateExprFound = !Checker.checkStatement(First);
4275 BinOp = nullptr;
4276 if (IsUpdateExprFound) {
4277 BinOp = dyn_cast<BinaryOperator>(Second);
4278 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
4279 }
4280 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
4281 // { x++; v = x; }
4282 // { x--; v = x; }
4283 // { ++x; v = x; }
4284 // { --x; v = x; }
4285 // { x binop= expr; v = x; }
4286 // { x = x binop expr; v = x; }
4287 // { x = expr binop x; v = x; }
4288 // Check that the second expression has form v = x.
4289 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
4290 llvm::FoldingSetNodeID XId, PossibleXId;
4291 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
4292 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
4293 IsUpdateExprFound = XId == PossibleXId;
4294 if (IsUpdateExprFound) {
4295 V = BinOp->getLHS();
4296 X = Checker.getX();
4297 E = Checker.getExpr();
4298 UE = Checker.getUpdateExpr();
4299 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00004300 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004301 }
4302 }
4303 }
4304 if (!IsUpdateExprFound) {
4305 // { v = x; x = expr; }
4306 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
4307 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
4308 ErrorFound = NotAnAssignmentOp;
4309 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
4310 : First->getLocStart();
4311 NoteRange = ErrorRange = FirstBinOp
4312 ? FirstBinOp->getSourceRange()
4313 : SourceRange(ErrorLoc, ErrorLoc);
4314 } else {
4315 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
4316 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
4317 ErrorFound = NotAnAssignmentOp;
4318 NoteLoc = ErrorLoc = SecondBinOp ? SecondBinOp->getOperatorLoc()
4319 : Second->getLocStart();
4320 NoteRange = ErrorRange = SecondBinOp
4321 ? SecondBinOp->getSourceRange()
4322 : SourceRange(ErrorLoc, ErrorLoc);
4323 } else {
4324 auto *PossibleXRHSInFirst =
4325 FirstBinOp->getRHS()->IgnoreParenImpCasts();
4326 auto *PossibleXLHSInSecond =
4327 SecondBinOp->getLHS()->IgnoreParenImpCasts();
4328 llvm::FoldingSetNodeID X1Id, X2Id;
4329 PossibleXRHSInFirst->Profile(X1Id, Context, /*Canonical=*/true);
4330 PossibleXLHSInSecond->Profile(X2Id, Context,
4331 /*Canonical=*/true);
4332 IsUpdateExprFound = X1Id == X2Id;
4333 if (IsUpdateExprFound) {
4334 V = FirstBinOp->getLHS();
4335 X = SecondBinOp->getLHS();
4336 E = SecondBinOp->getRHS();
4337 UE = nullptr;
4338 IsXLHSInRHSPart = false;
4339 IsPostfixUpdate = true;
4340 } else {
4341 ErrorFound = NotASpecificExpression;
4342 ErrorLoc = FirstBinOp->getExprLoc();
4343 ErrorRange = FirstBinOp->getSourceRange();
4344 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
4345 NoteRange = SecondBinOp->getRHS()->getSourceRange();
4346 }
4347 }
4348 }
4349 }
4350 } else {
4351 NoteLoc = ErrorLoc = Body->getLocStart();
4352 NoteRange = ErrorRange =
4353 SourceRange(Body->getLocStart(), Body->getLocStart());
4354 ErrorFound = NotTwoSubstatements;
4355 }
4356 } else {
4357 NoteLoc = ErrorLoc = Body->getLocStart();
4358 NoteRange = ErrorRange =
4359 SourceRange(Body->getLocStart(), Body->getLocStart());
4360 ErrorFound = NotACompoundStatement;
4361 }
4362 if (ErrorFound != NoError) {
4363 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
4364 << ErrorRange;
4365 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
4366 return StmtError();
4367 } else if (CurContext->isDependentContext()) {
4368 UE = V = E = X = nullptr;
4369 }
Alexey Bataev459dec02014-07-24 06:46:57 +00004370 }
Alexey Bataevdea47612014-07-23 07:46:59 +00004371 }
Alexey Bataev0162e452014-07-22 10:10:35 +00004372
4373 getCurFunction()->setHasBranchProtectedScope();
4374
Alexey Bataev62cec442014-11-18 10:14:22 +00004375 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00004376 X, V, E, UE, IsXLHSInRHSPart,
4377 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00004378}
4379
Alexey Bataev0bd520b2014-09-19 08:19:49 +00004380StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
4381 Stmt *AStmt,
4382 SourceLocation StartLoc,
4383 SourceLocation EndLoc) {
4384 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
4385
Alexey Bataev13314bf2014-10-09 04:18:56 +00004386 // OpenMP [2.16, Nesting of Regions]
4387 // If specified, a teams construct must be contained within a target
4388 // construct. That target construct must contain no statements or directives
4389 // outside of the teams construct.
4390 if (DSAStack->hasInnerTeamsRegion()) {
4391 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
4392 bool OMPTeamsFound = true;
4393 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
4394 auto I = CS->body_begin();
4395 while (I != CS->body_end()) {
4396 auto OED = dyn_cast<OMPExecutableDirective>(*I);
4397 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
4398 OMPTeamsFound = false;
4399 break;
4400 }
4401 ++I;
4402 }
4403 assert(I != CS->body_end() && "Not found statement");
4404 S = *I;
4405 }
4406 if (!OMPTeamsFound) {
4407 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
4408 Diag(DSAStack->getInnerTeamsRegionLoc(),
4409 diag::note_omp_nested_teams_construct_here);
4410 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
4411 << isa<OMPExecutableDirective>(S);
4412 return StmtError();
4413 }
4414 }
4415
Alexey Bataev0bd520b2014-09-19 08:19:49 +00004416 getCurFunction()->setHasBranchProtectedScope();
4417
4418 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4419}
4420
Michael Wong65f367f2015-07-21 13:44:28 +00004421StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
4422 Stmt *AStmt,
4423 SourceLocation StartLoc,
4424 SourceLocation EndLoc) {
4425 getCurFunction()->setHasBranchProtectedScope();
4426
4427 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
4428 AStmt);
4429}
4430
Alexey Bataev13314bf2014-10-09 04:18:56 +00004431StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
4432 Stmt *AStmt, SourceLocation StartLoc,
4433 SourceLocation EndLoc) {
4434 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
4435 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4436 // 1.2.2 OpenMP Language Terminology
4437 // Structured block - An executable statement with a single entry at the
4438 // top and a single exit at the bottom.
4439 // The point of exit cannot be a branch out of the structured block.
4440 // longjmp() and throw() must not violate the entry/exit criteria.
4441 CS->getCapturedDecl()->setNothrow();
4442
4443 getCurFunction()->setHasBranchProtectedScope();
4444
4445 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4446}
4447
Alexey Bataev6d4ed052015-07-01 06:57:41 +00004448StmtResult
4449Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
4450 SourceLocation EndLoc,
4451 OpenMPDirectiveKind CancelRegion) {
4452 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
4453 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
4454 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
4455 << getOpenMPDirectiveName(CancelRegion);
4456 return StmtError();
4457 }
4458 if (DSAStack->isParentNowaitRegion()) {
4459 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
4460 return StmtError();
4461 }
4462 if (DSAStack->isParentOrderedRegion()) {
4463 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
4464 return StmtError();
4465 }
4466 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
4467 CancelRegion);
4468}
4469
Alexey Bataev80909872015-07-02 11:25:17 +00004470StmtResult Sema::ActOnOpenMPCancelDirective(SourceLocation StartLoc,
4471 SourceLocation EndLoc,
4472 OpenMPDirectiveKind CancelRegion) {
4473 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
4474 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
4475 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
4476 << getOpenMPDirectiveName(CancelRegion);
4477 return StmtError();
4478 }
4479 if (DSAStack->isParentNowaitRegion()) {
4480 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
4481 return StmtError();
4482 }
4483 if (DSAStack->isParentOrderedRegion()) {
4484 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
4485 return StmtError();
4486 }
4487 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, CancelRegion);
4488}
4489
Alexey Bataeved09d242014-05-28 05:53:51 +00004490OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004491 SourceLocation StartLoc,
4492 SourceLocation LParenLoc,
4493 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004494 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004495 switch (Kind) {
4496 case OMPC_if:
4497 Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc);
4498 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00004499 case OMPC_final:
4500 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
4501 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00004502 case OMPC_num_threads:
4503 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
4504 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00004505 case OMPC_safelen:
4506 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
4507 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00004508 case OMPC_collapse:
4509 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
4510 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004511 case OMPC_ordered:
4512 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
4513 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004514 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004515 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00004516 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004517 case OMPC_private:
4518 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00004519 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004520 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00004521 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00004522 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00004523 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004524 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00004525 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00004526 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00004527 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00004528 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004529 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00004530 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004531 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00004532 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00004533 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00004534 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00004535 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00004536 case OMPC_depend:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004537 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004538 llvm_unreachable("Clause is not allowed.");
4539 }
4540 return Res;
4541}
4542
Alexey Bataeved09d242014-05-28 05:53:51 +00004543OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004544 SourceLocation LParenLoc,
4545 SourceLocation EndLoc) {
4546 Expr *ValExpr = Condition;
4547 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
4548 !Condition->isInstantiationDependent() &&
4549 !Condition->containsUnexpandedParameterPack()) {
4550 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00004551 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004552 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004553 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004554
Nikola Smiljanic01a75982014-05-29 10:55:11 +00004555 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004556 }
4557
4558 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc);
4559}
4560
Alexey Bataev3778b602014-07-17 07:32:53 +00004561OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
4562 SourceLocation StartLoc,
4563 SourceLocation LParenLoc,
4564 SourceLocation EndLoc) {
4565 Expr *ValExpr = Condition;
4566 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
4567 !Condition->isInstantiationDependent() &&
4568 !Condition->containsUnexpandedParameterPack()) {
4569 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
4570 Condition->getExprLoc(), Condition);
4571 if (Val.isInvalid())
4572 return nullptr;
4573
4574 ValExpr = Val.get();
4575 }
4576
4577 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
4578}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004579ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
4580 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00004581 if (!Op)
4582 return ExprError();
4583
4584 class IntConvertDiagnoser : public ICEConvertDiagnoser {
4585 public:
4586 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00004587 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00004588 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
4589 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004590 return S.Diag(Loc, diag::err_omp_not_integral) << T;
4591 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004592 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
4593 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004594 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
4595 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004596 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
4597 QualType T,
4598 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004599 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
4600 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004601 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
4602 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004603 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00004604 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00004605 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004606 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
4607 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004608 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
4609 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004610 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
4611 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004612 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00004613 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00004614 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004615 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
4616 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004617 llvm_unreachable("conversion functions are permitted");
4618 }
4619 } ConvertDiagnoser;
4620 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
4621}
4622
4623OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
4624 SourceLocation StartLoc,
4625 SourceLocation LParenLoc,
4626 SourceLocation EndLoc) {
4627 Expr *ValExpr = NumThreads;
4628 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
Alexey Bataev568a8332014-03-06 06:15:19 +00004629 !NumThreads->containsUnexpandedParameterPack()) {
4630 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
4631 ExprResult Val =
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004632 PerformOpenMPImplicitIntegerConversion(NumThreadsLoc, NumThreads);
Alexey Bataev568a8332014-03-06 06:15:19 +00004633 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004634 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00004635
Nikola Smiljanic01a75982014-05-29 10:55:11 +00004636 ValExpr = Val.get();
Alexey Bataev568a8332014-03-06 06:15:19 +00004637
4638 // OpenMP [2.5, Restrictions]
4639 // The num_threads expression must evaluate to a positive integer value.
4640 llvm::APSInt Result;
Alexey Bataeved09d242014-05-28 05:53:51 +00004641 if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() &&
4642 !Result.isStrictlyPositive()) {
Alexey Bataev568a8332014-03-06 06:15:19 +00004643 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
4644 << "num_threads" << NumThreads->getSourceRange();
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004645 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00004646 }
4647 }
4648
Alexey Bataeved09d242014-05-28 05:53:51 +00004649 return new (Context)
4650 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00004651}
4652
Alexey Bataev62c87d22014-03-21 04:51:18 +00004653ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
4654 OpenMPClauseKind CKind) {
4655 if (!E)
4656 return ExprError();
4657 if (E->isValueDependent() || E->isTypeDependent() ||
4658 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00004659 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00004660 llvm::APSInt Result;
4661 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
4662 if (ICE.isInvalid())
4663 return ExprError();
4664 if (!Result.isStrictlyPositive()) {
4665 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
4666 << getOpenMPClauseName(CKind) << E->getSourceRange();
4667 return ExprError();
4668 }
Alexander Musman09184fe2014-09-30 05:29:28 +00004669 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
4670 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
4671 << E->getSourceRange();
4672 return ExprError();
4673 }
Alexey Bataev9c821032015-04-30 04:23:23 +00004674 if (CKind == OMPC_collapse) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00004675 DSAStack->setCollapseNumber(DSAStack->getCollapseNumber() - 1 +
4676 Result.getExtValue());
4677 } else if (CKind == OMPC_ordered) {
4678 DSAStack->setCollapseNumber(DSAStack->getCollapseNumber() - 1 +
4679 Result.getExtValue());
Alexey Bataev9c821032015-04-30 04:23:23 +00004680 }
Alexey Bataev62c87d22014-03-21 04:51:18 +00004681 return ICE;
4682}
4683
4684OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
4685 SourceLocation LParenLoc,
4686 SourceLocation EndLoc) {
4687 // OpenMP [2.8.1, simd construct, Description]
4688 // The parameter of the safelen clause must be a constant
4689 // positive integer expression.
4690 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
4691 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004692 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00004693 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00004694 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00004695}
4696
Alexander Musman64d33f12014-06-04 07:53:32 +00004697OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
4698 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00004699 SourceLocation LParenLoc,
4700 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00004701 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00004702 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00004703 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00004704 // The parameter of the collapse clause must be a constant
4705 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00004706 ExprResult NumForLoopsResult =
4707 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
4708 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00004709 return nullptr;
4710 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00004711 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00004712}
4713
Alexey Bataev10e775f2015-07-30 11:36:16 +00004714OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
4715 SourceLocation EndLoc,
4716 SourceLocation LParenLoc,
4717 Expr *NumForLoops) {
4718 DSAStack->setOrderedRegion();
4719 // OpenMP [2.7.1, loop construct, Description]
4720 // OpenMP [2.8.1, simd construct, Description]
4721 // OpenMP [2.9.6, distribute construct, Description]
4722 // The parameter of the ordered clause must be a constant
4723 // positive integer expression if any.
4724 if (NumForLoops && LParenLoc.isValid()) {
4725 ExprResult NumForLoopsResult =
4726 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
4727 if (NumForLoopsResult.isInvalid())
4728 return nullptr;
4729 NumForLoops = NumForLoopsResult.get();
4730 }
4731 return new (Context)
4732 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
4733}
4734
Alexey Bataeved09d242014-05-28 05:53:51 +00004735OMPClause *Sema::ActOnOpenMPSimpleClause(
4736 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
4737 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004738 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004739 switch (Kind) {
4740 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00004741 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00004742 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
4743 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004744 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004745 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00004746 Res = ActOnOpenMPProcBindClause(
4747 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
4748 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004749 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004750 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00004751 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00004752 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00004753 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00004754 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00004755 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004756 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004757 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00004758 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00004759 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00004760 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00004761 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00004762 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004763 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00004764 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004765 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00004766 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00004767 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00004768 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004769 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00004770 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004771 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00004772 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00004773 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00004774 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00004775 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00004776 case OMPC_depend:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004777 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004778 llvm_unreachable("Clause is not allowed.");
4779 }
4780 return Res;
4781}
4782
4783OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
4784 SourceLocation KindKwLoc,
4785 SourceLocation StartLoc,
4786 SourceLocation LParenLoc,
4787 SourceLocation EndLoc) {
4788 if (Kind == OMPC_DEFAULT_unknown) {
4789 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00004790 static_assert(OMPC_DEFAULT_unknown > 0,
4791 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00004792 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00004793 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004794 Values += "'";
4795 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
4796 Values += "'";
4797 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00004798 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004799 Values += " or ";
4800 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00004801 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004802 break;
4803 default:
4804 Values += Sep;
4805 break;
4806 }
4807 }
4808 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00004809 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004810 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004811 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00004812 switch (Kind) {
4813 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00004814 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004815 break;
4816 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00004817 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004818 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004819 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004820 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00004821 break;
4822 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004823 return new (Context)
4824 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004825}
4826
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004827OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
4828 SourceLocation KindKwLoc,
4829 SourceLocation StartLoc,
4830 SourceLocation LParenLoc,
4831 SourceLocation EndLoc) {
4832 if (Kind == OMPC_PROC_BIND_unknown) {
4833 std::string Values;
4834 std::string Sep(", ");
4835 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
4836 Values += "'";
4837 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
4838 Values += "'";
4839 switch (i) {
4840 case OMPC_PROC_BIND_unknown - 2:
4841 Values += " or ";
4842 break;
4843 case OMPC_PROC_BIND_unknown - 1:
4844 break;
4845 default:
4846 Values += Sep;
4847 break;
4848 }
4849 }
4850 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00004851 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004852 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004853 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004854 return new (Context)
4855 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004856}
4857
Alexey Bataev56dafe82014-06-20 07:16:17 +00004858OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
4859 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
4860 SourceLocation StartLoc, SourceLocation LParenLoc,
4861 SourceLocation ArgumentLoc, SourceLocation CommaLoc,
4862 SourceLocation EndLoc) {
4863 OMPClause *Res = nullptr;
4864 switch (Kind) {
4865 case OMPC_schedule:
4866 Res = ActOnOpenMPScheduleClause(
4867 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
4868 LParenLoc, ArgumentLoc, CommaLoc, EndLoc);
4869 break;
4870 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00004871 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00004872 case OMPC_num_threads:
4873 case OMPC_safelen:
4874 case OMPC_collapse:
4875 case OMPC_default:
4876 case OMPC_proc_bind:
4877 case OMPC_private:
4878 case OMPC_firstprivate:
4879 case OMPC_lastprivate:
4880 case OMPC_shared:
4881 case OMPC_reduction:
4882 case OMPC_linear:
4883 case OMPC_aligned:
4884 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00004885 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004886 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00004887 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00004888 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00004889 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00004890 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00004891 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004892 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00004893 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00004894 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00004895 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00004896 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00004897 case OMPC_depend:
Alexey Bataev56dafe82014-06-20 07:16:17 +00004898 case OMPC_unknown:
4899 llvm_unreachable("Clause is not allowed.");
4900 }
4901 return Res;
4902}
4903
4904OMPClause *Sema::ActOnOpenMPScheduleClause(
4905 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
4906 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
4907 SourceLocation EndLoc) {
4908 if (Kind == OMPC_SCHEDULE_unknown) {
4909 std::string Values;
4910 std::string Sep(", ");
4911 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) {
4912 Values += "'";
4913 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i);
4914 Values += "'";
4915 switch (i) {
4916 case OMPC_SCHEDULE_unknown - 2:
4917 Values += " or ";
4918 break;
4919 case OMPC_SCHEDULE_unknown - 1:
4920 break;
4921 default:
4922 Values += Sep;
4923 break;
4924 }
4925 }
4926 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
4927 << Values << getOpenMPClauseName(OMPC_schedule);
4928 return nullptr;
4929 }
4930 Expr *ValExpr = ChunkSize;
Alexey Bataev040d5402015-05-12 08:35:28 +00004931 Expr *HelperValExpr = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00004932 if (ChunkSize) {
4933 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
4934 !ChunkSize->isInstantiationDependent() &&
4935 !ChunkSize->containsUnexpandedParameterPack()) {
4936 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
4937 ExprResult Val =
4938 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
4939 if (Val.isInvalid())
4940 return nullptr;
4941
4942 ValExpr = Val.get();
4943
4944 // OpenMP [2.7.1, Restrictions]
4945 // chunk_size must be a loop invariant integer expression with a positive
4946 // value.
4947 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00004948 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
4949 if (Result.isSigned() && !Result.isStrictlyPositive()) {
4950 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
4951 << "schedule" << ChunkSize->getSourceRange();
4952 return nullptr;
4953 }
4954 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
4955 auto *ImpVar = buildVarDecl(*this, ChunkSize->getExprLoc(),
4956 ChunkSize->getType(), ".chunk.");
4957 auto *ImpVarRef = buildDeclRefExpr(*this, ImpVar, ChunkSize->getType(),
4958 ChunkSize->getExprLoc(),
4959 /*RefersToCapture=*/true);
4960 HelperValExpr = ImpVarRef;
Alexey Bataev56dafe82014-06-20 07:16:17 +00004961 }
4962 }
4963 }
4964
4965 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc,
Alexey Bataev040d5402015-05-12 08:35:28 +00004966 EndLoc, Kind, ValExpr, HelperValExpr);
Alexey Bataev56dafe82014-06-20 07:16:17 +00004967}
4968
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004969OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
4970 SourceLocation StartLoc,
4971 SourceLocation EndLoc) {
4972 OMPClause *Res = nullptr;
4973 switch (Kind) {
4974 case OMPC_ordered:
4975 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
4976 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00004977 case OMPC_nowait:
4978 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
4979 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00004980 case OMPC_untied:
4981 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
4982 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00004983 case OMPC_mergeable:
4984 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
4985 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004986 case OMPC_read:
4987 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
4988 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00004989 case OMPC_write:
4990 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
4991 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00004992 case OMPC_update:
4993 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
4994 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00004995 case OMPC_capture:
4996 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
4997 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00004998 case OMPC_seq_cst:
4999 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
5000 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005001 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00005002 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005003 case OMPC_num_threads:
5004 case OMPC_safelen:
5005 case OMPC_collapse:
5006 case OMPC_schedule:
5007 case OMPC_private:
5008 case OMPC_firstprivate:
5009 case OMPC_lastprivate:
5010 case OMPC_shared:
5011 case OMPC_reduction:
5012 case OMPC_linear:
5013 case OMPC_aligned:
5014 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005015 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005016 case OMPC_default:
5017 case OMPC_proc_bind:
5018 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00005019 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005020 case OMPC_depend:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005021 case OMPC_unknown:
5022 llvm_unreachable("Clause is not allowed.");
5023 }
5024 return Res;
5025}
5026
Alexey Bataev236070f2014-06-20 11:19:47 +00005027OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
5028 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00005029 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00005030 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
5031}
5032
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005033OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
5034 SourceLocation EndLoc) {
5035 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
5036}
5037
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005038OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
5039 SourceLocation EndLoc) {
5040 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
5041}
5042
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005043OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
5044 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005045 return new (Context) OMPReadClause(StartLoc, EndLoc);
5046}
5047
Alexey Bataevdea47612014-07-23 07:46:59 +00005048OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
5049 SourceLocation EndLoc) {
5050 return new (Context) OMPWriteClause(StartLoc, EndLoc);
5051}
5052
Alexey Bataev67a4f222014-07-23 10:25:33 +00005053OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
5054 SourceLocation EndLoc) {
5055 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
5056}
5057
Alexey Bataev459dec02014-07-24 06:46:57 +00005058OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
5059 SourceLocation EndLoc) {
5060 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
5061}
5062
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005063OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
5064 SourceLocation EndLoc) {
5065 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
5066}
5067
Alexey Bataevc5e02582014-06-16 07:08:35 +00005068OMPClause *Sema::ActOnOpenMPVarListClause(
5069 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
5070 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
5071 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005072 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
5073 SourceLocation DepLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005074 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005075 switch (Kind) {
5076 case OMPC_private:
5077 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
5078 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005079 case OMPC_firstprivate:
5080 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
5081 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00005082 case OMPC_lastprivate:
5083 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
5084 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00005085 case OMPC_shared:
5086 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
5087 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005088 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00005089 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
5090 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00005091 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00005092 case OMPC_linear:
5093 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
5094 ColonLoc, EndLoc);
5095 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00005096 case OMPC_aligned:
5097 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
5098 ColonLoc, EndLoc);
5099 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005100 case OMPC_copyin:
5101 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
5102 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00005103 case OMPC_copyprivate:
5104 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
5105 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00005106 case OMPC_flush:
5107 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
5108 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005109 case OMPC_depend:
5110 Res = ActOnOpenMPDependClause(DepKind, DepLoc, ColonLoc, VarList, StartLoc,
5111 LParenLoc, EndLoc);
5112 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005113 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00005114 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00005115 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00005116 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00005117 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005118 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005119 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005120 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005121 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00005122 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005123 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005124 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005125 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005126 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00005127 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00005128 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00005129 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005130 case OMPC_seq_cst:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005131 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005132 llvm_unreachable("Clause is not allowed.");
5133 }
5134 return Res;
5135}
5136
5137OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
5138 SourceLocation StartLoc,
5139 SourceLocation LParenLoc,
5140 SourceLocation EndLoc) {
5141 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00005142 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00005143 for (auto &RefExpr : VarList) {
5144 assert(RefExpr && "NULL expr in OpenMP private clause.");
5145 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005146 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00005147 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00005148 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005149 continue;
5150 }
5151
Alexey Bataeved09d242014-05-28 05:53:51 +00005152 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005153 // OpenMP [2.1, C/C++]
5154 // A list item is a variable name.
5155 // OpenMP [2.9.3.3, Restrictions, p.1]
5156 // A variable that is part of another variable (as an array or
5157 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00005158 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005159 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005160 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005161 continue;
5162 }
5163 Decl *D = DE->getDecl();
5164 VarDecl *VD = cast<VarDecl>(D);
5165
5166 QualType Type = VD->getType();
5167 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5168 // It will be analyzed later.
5169 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00005170 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005171 continue;
5172 }
5173
5174 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
5175 // A variable that appears in a private clause must not have an incomplete
5176 // type or a reference type.
5177 if (RequireCompleteType(ELoc, Type,
5178 diag::err_omp_private_incomplete_type)) {
5179 continue;
5180 }
5181 if (Type->isReferenceType()) {
5182 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00005183 << getOpenMPClauseName(OMPC_private) << Type;
5184 bool IsDecl =
5185 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5186 Diag(VD->getLocation(),
5187 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5188 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005189 continue;
5190 }
5191
Alexey Bataev758e55e2013-09-06 18:03:48 +00005192 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
5193 // in a Construct]
5194 // Variables with the predetermined data-sharing attributes may not be
5195 // listed in data-sharing attributes clauses, except for the cases
5196 // listed below. For these exceptions only, listing a predetermined
5197 // variable in a data-sharing attribute clause is allowed and overrides
5198 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005199 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005200 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005201 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
5202 << getOpenMPClauseName(OMPC_private);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005203 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005204 continue;
5205 }
5206
Alexey Bataevccb59ec2015-05-19 08:44:56 +00005207 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00005208 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00005209 DSAStack->getCurrentDirective() == OMPD_task) {
5210 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
5211 << getOpenMPClauseName(OMPC_private) << Type
5212 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
5213 bool IsDecl =
5214 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5215 Diag(VD->getLocation(),
5216 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5217 << VD;
5218 continue;
5219 }
5220
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005221 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
5222 // A variable of class type (or array thereof) that appears in a private
5223 // clause requires an accessible, unambiguous default constructor for the
5224 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00005225 // Generate helper private variable and initialize it with the default
5226 // value. The address of the original variable is replaced by the address of
5227 // the new private variable in CodeGen. This new variable is not added to
5228 // IdResolver, so the code in the OpenMP region uses original variable for
5229 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005230 Type = Type.getUnqualifiedType();
5231 auto VDPrivate = buildVarDecl(*this, DE->getExprLoc(), Type, VD->getName());
Alexey Bataev39f915b82015-05-08 10:41:21 +00005232 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00005233 if (VDPrivate->isInvalidDecl())
5234 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005235 auto VDPrivateRefExpr = buildDeclRefExpr(
5236 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev03b340a2014-10-21 03:16:40 +00005237
Alexey Bataev758e55e2013-09-06 18:03:48 +00005238 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005239 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00005240 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005241 }
5242
Alexey Bataeved09d242014-05-28 05:53:51 +00005243 if (Vars.empty())
5244 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005245
Alexey Bataev03b340a2014-10-21 03:16:40 +00005246 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
5247 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005248}
5249
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005250namespace {
5251class DiagsUninitializedSeveretyRAII {
5252private:
5253 DiagnosticsEngine &Diags;
5254 SourceLocation SavedLoc;
5255 bool IsIgnored;
5256
5257public:
5258 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
5259 bool IsIgnored)
5260 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
5261 if (!IsIgnored) {
5262 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
5263 /*Map*/ diag::Severity::Ignored, Loc);
5264 }
5265 }
5266 ~DiagsUninitializedSeveretyRAII() {
5267 if (!IsIgnored)
5268 Diags.popMappings(SavedLoc);
5269 }
5270};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00005271}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005272
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005273OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
5274 SourceLocation StartLoc,
5275 SourceLocation LParenLoc,
5276 SourceLocation EndLoc) {
5277 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005278 SmallVector<Expr *, 8> PrivateCopies;
5279 SmallVector<Expr *, 8> Inits;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005280 bool IsImplicitClause =
5281 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
5282 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
5283
Alexey Bataeved09d242014-05-28 05:53:51 +00005284 for (auto &RefExpr : VarList) {
5285 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
5286 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005287 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00005288 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005289 PrivateCopies.push_back(nullptr);
5290 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005291 continue;
5292 }
5293
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005294 SourceLocation ELoc =
5295 IsImplicitClause ? ImplicitClauseLoc : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005296 // OpenMP [2.1, C/C++]
5297 // A list item is a variable name.
5298 // OpenMP [2.9.3.3, Restrictions, p.1]
5299 // A variable that is part of another variable (as an array or
5300 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00005301 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005302 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005303 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005304 continue;
5305 }
5306 Decl *D = DE->getDecl();
5307 VarDecl *VD = cast<VarDecl>(D);
5308
5309 QualType Type = VD->getType();
5310 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5311 // It will be analyzed later.
5312 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005313 PrivateCopies.push_back(nullptr);
5314 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005315 continue;
5316 }
5317
5318 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
5319 // A variable that appears in a private clause must not have an incomplete
5320 // type or a reference type.
5321 if (RequireCompleteType(ELoc, Type,
5322 diag::err_omp_firstprivate_incomplete_type)) {
5323 continue;
5324 }
5325 if (Type->isReferenceType()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005326 if (IsImplicitClause) {
5327 Diag(ImplicitClauseLoc,
5328 diag::err_omp_task_predetermined_firstprivate_ref_type_arg)
5329 << Type;
5330 Diag(RefExpr->getExprLoc(), diag::note_used_here);
5331 } else {
5332 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
5333 << getOpenMPClauseName(OMPC_firstprivate) << Type;
5334 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005335 bool IsDecl =
5336 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5337 Diag(VD->getLocation(),
5338 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5339 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005340 continue;
5341 }
5342
5343 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
5344 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00005345 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005346 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005347 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005348
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005349 // If an implicit firstprivate variable found it was checked already.
5350 if (!IsImplicitClause) {
5351 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005352 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005353 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
5354 // A list item that specifies a given variable may not appear in more
5355 // than one clause on the same directive, except that a variable may be
5356 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005357 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00005358 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005359 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00005360 << getOpenMPClauseName(DVar.CKind)
5361 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005362 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005363 continue;
5364 }
5365
5366 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
5367 // in a Construct]
5368 // Variables with the predetermined data-sharing attributes may not be
5369 // listed in data-sharing attributes clauses, except for the cases
5370 // listed below. For these exceptions only, listing a predetermined
5371 // variable in a data-sharing attribute clause is allowed and overrides
5372 // the variable's predetermined data-sharing attributes.
5373 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
5374 // in a Construct, C/C++, p.2]
5375 // Variables with const-qualified type having no mutable member may be
5376 // listed in a firstprivate clause, even if they are static data members.
5377 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
5378 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
5379 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00005380 << getOpenMPClauseName(DVar.CKind)
5381 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005382 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005383 continue;
5384 }
5385
Alexey Bataevf29276e2014-06-18 04:14:57 +00005386 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005387 // OpenMP [2.9.3.4, Restrictions, p.2]
5388 // A list item that is private within a parallel region must not appear
5389 // in a firstprivate clause on a worksharing construct if any of the
5390 // worksharing regions arising from the worksharing construct ever bind
5391 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00005392 if (isOpenMPWorksharingDirective(CurrDir) &&
5393 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005394 DVar = DSAStack->getImplicitDSA(VD, true);
5395 if (DVar.CKind != OMPC_shared &&
5396 (isOpenMPParallelDirective(DVar.DKind) ||
5397 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00005398 Diag(ELoc, diag::err_omp_required_access)
5399 << getOpenMPClauseName(OMPC_firstprivate)
5400 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005401 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005402 continue;
5403 }
5404 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005405 // OpenMP [2.9.3.4, Restrictions, p.3]
5406 // A list item that appears in a reduction clause of a parallel construct
5407 // must not appear in a firstprivate clause on a worksharing or task
5408 // construct if any of the worksharing or task regions arising from the
5409 // worksharing or task construct ever bind to any of the parallel regions
5410 // arising from the parallel construct.
5411 // OpenMP [2.9.3.4, Restrictions, p.4]
5412 // A list item that appears in a reduction clause in worksharing
5413 // construct must not appear in a firstprivate clause in a task construct
5414 // encountered during execution of any of the worksharing regions arising
5415 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005416 if (CurrDir == OMPD_task) {
5417 DVar =
5418 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
5419 [](OpenMPDirectiveKind K) -> bool {
5420 return isOpenMPParallelDirective(K) ||
5421 isOpenMPWorksharingDirective(K);
5422 },
5423 false);
5424 if (DVar.CKind == OMPC_reduction &&
5425 (isOpenMPParallelDirective(DVar.DKind) ||
5426 isOpenMPWorksharingDirective(DVar.DKind))) {
5427 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
5428 << getOpenMPDirectiveName(DVar.DKind);
5429 ReportOriginalDSA(*this, DSAStack, VD, DVar);
5430 continue;
5431 }
5432 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005433 }
5434
Alexey Bataevccb59ec2015-05-19 08:44:56 +00005435 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00005436 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00005437 DSAStack->getCurrentDirective() == OMPD_task) {
5438 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
5439 << getOpenMPClauseName(OMPC_firstprivate) << Type
5440 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
5441 bool IsDecl =
5442 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5443 Diag(VD->getLocation(),
5444 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5445 << VD;
5446 continue;
5447 }
5448
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005449 Type = Type.getUnqualifiedType();
5450 auto VDPrivate = buildVarDecl(*this, ELoc, Type, VD->getName());
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005451 // Generate helper private variable and initialize it with the value of the
5452 // original variable. The address of the original variable is replaced by
5453 // the address of the new private variable in the CodeGen. This new variable
5454 // is not added to IdResolver, so the code in the OpenMP region uses
5455 // original variable for proper diagnostics and variable capturing.
5456 Expr *VDInitRefExpr = nullptr;
5457 // For arrays generate initializer for single element and replace it by the
5458 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005459 if (Type->isArrayType()) {
5460 auto VDInit =
5461 buildVarDecl(*this, DE->getExprLoc(), ElemType, VD->getName());
5462 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005463 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005464 ElemType = ElemType.getUnqualifiedType();
5465 auto *VDInitTemp = buildVarDecl(*this, DE->getLocStart(), ElemType,
5466 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00005467 InitializedEntity Entity =
5468 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005469 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
5470
5471 InitializationSequence InitSeq(*this, Entity, Kind, Init);
5472 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
5473 if (Result.isInvalid())
5474 VDPrivate->setInvalidDecl();
5475 else
5476 VDPrivate->setInit(Result.getAs<Expr>());
5477 } else {
Alexey Bataev69c62a92015-04-15 04:52:20 +00005478 auto *VDInit =
Alexey Bataev39f915b82015-05-08 10:41:21 +00005479 buildVarDecl(*this, DE->getLocStart(), Type, ".firstprivate.temp");
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005480 VDInitRefExpr =
5481 buildDeclRefExpr(*this, VDInit, DE->getType(), DE->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00005482 AddInitializerToDecl(VDPrivate,
5483 DefaultLvalueConversion(VDInitRefExpr).get(),
5484 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005485 }
5486 if (VDPrivate->isInvalidDecl()) {
5487 if (IsImplicitClause) {
5488 Diag(DE->getExprLoc(),
5489 diag::note_omp_task_predetermined_firstprivate_here);
5490 }
5491 continue;
5492 }
5493 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00005494 auto VDPrivateRefExpr = buildDeclRefExpr(
5495 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005496 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
5497 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005498 PrivateCopies.push_back(VDPrivateRefExpr);
5499 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005500 }
5501
Alexey Bataeved09d242014-05-28 05:53:51 +00005502 if (Vars.empty())
5503 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005504
5505 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005506 Vars, PrivateCopies, Inits);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005507}
5508
Alexander Musman1bb328c2014-06-04 13:06:39 +00005509OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
5510 SourceLocation StartLoc,
5511 SourceLocation LParenLoc,
5512 SourceLocation EndLoc) {
5513 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00005514 SmallVector<Expr *, 8> SrcExprs;
5515 SmallVector<Expr *, 8> DstExprs;
5516 SmallVector<Expr *, 8> AssignmentOps;
Alexander Musman1bb328c2014-06-04 13:06:39 +00005517 for (auto &RefExpr : VarList) {
5518 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
5519 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
5520 // It will be analyzed later.
5521 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00005522 SrcExprs.push_back(nullptr);
5523 DstExprs.push_back(nullptr);
5524 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005525 continue;
5526 }
5527
5528 SourceLocation ELoc = RefExpr->getExprLoc();
5529 // OpenMP [2.1, C/C++]
5530 // A list item is a variable name.
5531 // OpenMP [2.14.3.5, Restrictions, p.1]
5532 // A variable that is part of another variable (as an array or structure
5533 // element) cannot appear in a lastprivate clause.
5534 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
5535 if (!DE || !isa<VarDecl>(DE->getDecl())) {
5536 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
5537 continue;
5538 }
5539 Decl *D = DE->getDecl();
5540 VarDecl *VD = cast<VarDecl>(D);
5541
5542 QualType Type = VD->getType();
5543 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5544 // It will be analyzed later.
5545 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00005546 SrcExprs.push_back(nullptr);
5547 DstExprs.push_back(nullptr);
5548 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005549 continue;
5550 }
5551
5552 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
5553 // A variable that appears in a lastprivate clause must not have an
5554 // incomplete type or a reference type.
5555 if (RequireCompleteType(ELoc, Type,
5556 diag::err_omp_lastprivate_incomplete_type)) {
5557 continue;
5558 }
5559 if (Type->isReferenceType()) {
5560 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
5561 << getOpenMPClauseName(OMPC_lastprivate) << Type;
5562 bool IsDecl =
5563 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5564 Diag(VD->getLocation(),
5565 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5566 << VD;
5567 continue;
5568 }
5569
5570 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
5571 // in a Construct]
5572 // Variables with the predetermined data-sharing attributes may not be
5573 // listed in data-sharing attributes clauses, except for the cases
5574 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005575 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005576 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
5577 DVar.CKind != OMPC_firstprivate &&
5578 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
5579 Diag(ELoc, diag::err_omp_wrong_dsa)
5580 << getOpenMPClauseName(DVar.CKind)
5581 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005582 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005583 continue;
5584 }
5585
Alexey Bataevf29276e2014-06-18 04:14:57 +00005586 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
5587 // OpenMP [2.14.3.5, Restrictions, p.2]
5588 // A list item that is private within a parallel region, or that appears in
5589 // the reduction clause of a parallel construct, must not appear in a
5590 // lastprivate clause on a worksharing construct if any of the corresponding
5591 // worksharing regions ever binds to any of the corresponding parallel
5592 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005593 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00005594 if (isOpenMPWorksharingDirective(CurrDir) &&
5595 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005596 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005597 if (DVar.CKind != OMPC_shared) {
5598 Diag(ELoc, diag::err_omp_required_access)
5599 << getOpenMPClauseName(OMPC_lastprivate)
5600 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005601 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005602 continue;
5603 }
5604 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00005605 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00005606 // A variable of class type (or array thereof) that appears in a
5607 // lastprivate clause requires an accessible, unambiguous default
5608 // constructor for the class type, unless the list item is also specified
5609 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00005610 // A variable of class type (or array thereof) that appears in a
5611 // lastprivate clause requires an accessible, unambiguous copy assignment
5612 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00005613 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00005614 auto *SrcVD = buildVarDecl(*this, DE->getLocStart(),
Alexey Bataev38e89532015-04-16 04:54:05 +00005615 Type.getUnqualifiedType(), ".lastprivate.src");
Alexey Bataev39f915b82015-05-08 10:41:21 +00005616 auto *PseudoSrcExpr = buildDeclRefExpr(
5617 *this, SrcVD, Type.getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00005618 auto *DstVD =
Alexey Bataev39f915b82015-05-08 10:41:21 +00005619 buildVarDecl(*this, DE->getLocStart(), Type, ".lastprivate.dst");
Alexey Bataev38e89532015-04-16 04:54:05 +00005620 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00005621 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00005622 // For arrays generate assignment operation for single element and replace
5623 // it by the original array element in CodeGen.
5624 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
5625 PseudoDstExpr, PseudoSrcExpr);
5626 if (AssignmentOp.isInvalid())
5627 continue;
5628 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
5629 /*DiscardedValue=*/true);
5630 if (AssignmentOp.isInvalid())
5631 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00005632
Alexey Bataev39f915b82015-05-08 10:41:21 +00005633 if (TopDVar.CKind != OMPC_firstprivate)
Alexey Bataevf29276e2014-06-18 04:14:57 +00005634 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005635 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00005636 SrcExprs.push_back(PseudoSrcExpr);
5637 DstExprs.push_back(PseudoDstExpr);
5638 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00005639 }
5640
5641 if (Vars.empty())
5642 return nullptr;
5643
5644 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00005645 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005646}
5647
Alexey Bataev758e55e2013-09-06 18:03:48 +00005648OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
5649 SourceLocation StartLoc,
5650 SourceLocation LParenLoc,
5651 SourceLocation EndLoc) {
5652 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00005653 for (auto &RefExpr : VarList) {
5654 assert(RefExpr && "NULL expr in OpenMP shared clause.");
5655 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00005656 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00005657 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005658 continue;
5659 }
5660
Alexey Bataeved09d242014-05-28 05:53:51 +00005661 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00005662 // OpenMP [2.1, C/C++]
5663 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00005664 // OpenMP [2.14.3.2, Restrictions, p.1]
5665 // A variable that is part of another variable (as an array or structure
5666 // element) cannot appear in a shared unless it is a static data member
5667 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00005668 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005669 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005670 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00005671 continue;
5672 }
5673 Decl *D = DE->getDecl();
5674 VarDecl *VD = cast<VarDecl>(D);
5675
5676 QualType Type = VD->getType();
5677 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5678 // It will be analyzed later.
5679 Vars.push_back(DE);
5680 continue;
5681 }
5682
5683 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
5684 // in a Construct]
5685 // Variables with the predetermined data-sharing attributes may not be
5686 // listed in data-sharing attributes clauses, except for the cases
5687 // listed below. For these exceptions only, listing a predetermined
5688 // variable in a data-sharing attribute clause is allowed and overrides
5689 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005690 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00005691 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
5692 DVar.RefExpr) {
5693 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
5694 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005695 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005696 continue;
5697 }
5698
5699 DSAStack->addDSA(VD, DE, OMPC_shared);
5700 Vars.push_back(DE);
5701 }
5702
Alexey Bataeved09d242014-05-28 05:53:51 +00005703 if (Vars.empty())
5704 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00005705
5706 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
5707}
5708
Alexey Bataevc5e02582014-06-16 07:08:35 +00005709namespace {
5710class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
5711 DSAStackTy *Stack;
5712
5713public:
5714 bool VisitDeclRefExpr(DeclRefExpr *E) {
5715 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005716 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00005717 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
5718 return false;
5719 if (DVar.CKind != OMPC_unknown)
5720 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00005721 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005722 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005723 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00005724 return true;
5725 return false;
5726 }
5727 return false;
5728 }
5729 bool VisitStmt(Stmt *S) {
5730 for (auto Child : S->children()) {
5731 if (Child && Visit(Child))
5732 return true;
5733 }
5734 return false;
5735 }
Alexey Bataev23b69422014-06-18 07:08:49 +00005736 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00005737};
Alexey Bataev23b69422014-06-18 07:08:49 +00005738} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00005739
5740OMPClause *Sema::ActOnOpenMPReductionClause(
5741 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
5742 SourceLocation ColonLoc, SourceLocation EndLoc,
5743 CXXScopeSpec &ReductionIdScopeSpec,
5744 const DeclarationNameInfo &ReductionId) {
5745 // TODO: Allow scope specification search when 'declare reduction' is
5746 // supported.
5747 assert(ReductionIdScopeSpec.isEmpty() &&
5748 "No support for scoped reduction identifiers yet.");
5749
5750 auto DN = ReductionId.getName();
5751 auto OOK = DN.getCXXOverloadedOperator();
5752 BinaryOperatorKind BOK = BO_Comma;
5753
5754 // OpenMP [2.14.3.6, reduction clause]
5755 // C
5756 // reduction-identifier is either an identifier or one of the following
5757 // operators: +, -, *, &, |, ^, && and ||
5758 // C++
5759 // reduction-identifier is either an id-expression or one of the following
5760 // operators: +, -, *, &, |, ^, && and ||
5761 // FIXME: Only 'min' and 'max' identifiers are supported for now.
5762 switch (OOK) {
5763 case OO_Plus:
5764 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005765 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005766 break;
5767 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005768 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005769 break;
5770 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005771 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005772 break;
5773 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005774 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005775 break;
5776 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005777 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005778 break;
5779 case OO_AmpAmp:
5780 BOK = BO_LAnd;
5781 break;
5782 case OO_PipePipe:
5783 BOK = BO_LOr;
5784 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005785 case OO_New:
5786 case OO_Delete:
5787 case OO_Array_New:
5788 case OO_Array_Delete:
5789 case OO_Slash:
5790 case OO_Percent:
5791 case OO_Tilde:
5792 case OO_Exclaim:
5793 case OO_Equal:
5794 case OO_Less:
5795 case OO_Greater:
5796 case OO_LessEqual:
5797 case OO_GreaterEqual:
5798 case OO_PlusEqual:
5799 case OO_MinusEqual:
5800 case OO_StarEqual:
5801 case OO_SlashEqual:
5802 case OO_PercentEqual:
5803 case OO_CaretEqual:
5804 case OO_AmpEqual:
5805 case OO_PipeEqual:
5806 case OO_LessLess:
5807 case OO_GreaterGreater:
5808 case OO_LessLessEqual:
5809 case OO_GreaterGreaterEqual:
5810 case OO_EqualEqual:
5811 case OO_ExclaimEqual:
5812 case OO_PlusPlus:
5813 case OO_MinusMinus:
5814 case OO_Comma:
5815 case OO_ArrowStar:
5816 case OO_Arrow:
5817 case OO_Call:
5818 case OO_Subscript:
5819 case OO_Conditional:
5820 case NUM_OVERLOADED_OPERATORS:
5821 llvm_unreachable("Unexpected reduction identifier");
5822 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00005823 if (auto II = DN.getAsIdentifierInfo()) {
5824 if (II->isStr("max"))
5825 BOK = BO_GT;
5826 else if (II->isStr("min"))
5827 BOK = BO_LT;
5828 }
5829 break;
5830 }
5831 SourceRange ReductionIdRange;
5832 if (ReductionIdScopeSpec.isValid()) {
5833 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
5834 }
5835 ReductionIdRange.setEnd(ReductionId.getEndLoc());
5836 if (BOK == BO_Comma) {
5837 // Not allowed reduction identifier is found.
5838 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
5839 << ReductionIdRange;
5840 return nullptr;
5841 }
5842
5843 SmallVector<Expr *, 8> Vars;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005844 SmallVector<Expr *, 8> LHSs;
5845 SmallVector<Expr *, 8> RHSs;
5846 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005847 for (auto RefExpr : VarList) {
5848 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
5849 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
5850 // It will be analyzed later.
5851 Vars.push_back(RefExpr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005852 LHSs.push_back(nullptr);
5853 RHSs.push_back(nullptr);
5854 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00005855 continue;
5856 }
5857
5858 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
5859 RefExpr->isInstantiationDependent() ||
5860 RefExpr->containsUnexpandedParameterPack()) {
5861 // It will be analyzed later.
5862 Vars.push_back(RefExpr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005863 LHSs.push_back(nullptr);
5864 RHSs.push_back(nullptr);
5865 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00005866 continue;
5867 }
5868
5869 auto ELoc = RefExpr->getExprLoc();
5870 auto ERange = RefExpr->getSourceRange();
5871 // OpenMP [2.1, C/C++]
5872 // A list item is a variable or array section, subject to the restrictions
5873 // specified in Section 2.4 on page 42 and in each of the sections
5874 // describing clauses and directives for which a list appears.
5875 // OpenMP [2.14.3.3, Restrictions, p.1]
5876 // A variable that is part of another variable (as an array or
5877 // structure element) cannot appear in a private clause.
5878 auto DE = dyn_cast<DeclRefExpr>(RefExpr);
5879 if (!DE || !isa<VarDecl>(DE->getDecl())) {
5880 Diag(ELoc, diag::err_omp_expected_var_name) << ERange;
5881 continue;
5882 }
5883 auto D = DE->getDecl();
5884 auto VD = cast<VarDecl>(D);
5885 auto Type = VD->getType();
5886 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
5887 // A variable that appears in a private clause must not have an incomplete
5888 // type or a reference type.
5889 if (RequireCompleteType(ELoc, Type,
5890 diag::err_omp_reduction_incomplete_type))
5891 continue;
5892 // OpenMP [2.14.3.6, reduction clause, Restrictions]
5893 // Arrays may not appear in a reduction clause.
5894 if (Type.getNonReferenceType()->isArrayType()) {
5895 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
5896 bool IsDecl =
5897 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5898 Diag(VD->getLocation(),
5899 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5900 << VD;
5901 continue;
5902 }
5903 // OpenMP [2.14.3.6, reduction clause, Restrictions]
5904 // A list item that appears in a reduction clause must not be
5905 // const-qualified.
5906 if (Type.getNonReferenceType().isConstant(Context)) {
5907 Diag(ELoc, diag::err_omp_const_variable)
5908 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
5909 bool IsDecl =
5910 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5911 Diag(VD->getLocation(),
5912 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5913 << VD;
5914 continue;
5915 }
5916 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
5917 // If a list-item is a reference type then it must bind to the same object
5918 // for all threads of the team.
5919 VarDecl *VDDef = VD->getDefinition();
5920 if (Type->isReferenceType() && VDDef) {
5921 DSARefChecker Check(DSAStack);
5922 if (Check.Visit(VDDef->getInit())) {
5923 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
5924 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
5925 continue;
5926 }
5927 }
5928 // OpenMP [2.14.3.6, reduction clause, Restrictions]
5929 // The type of a list item that appears in a reduction clause must be valid
5930 // for the reduction-identifier. For a max or min reduction in C, the type
5931 // of the list item must be an allowed arithmetic data type: char, int,
5932 // float, double, or _Bool, possibly modified with long, short, signed, or
5933 // unsigned. For a max or min reduction in C++, the type of the list item
5934 // must be an allowed arithmetic data type: char, wchar_t, int, float,
5935 // double, or bool, possibly modified with long, short, signed, or unsigned.
5936 if ((BOK == BO_GT || BOK == BO_LT) &&
5937 !(Type->isScalarType() ||
5938 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
5939 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
5940 << getLangOpts().CPlusPlus;
5941 bool IsDecl =
5942 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5943 Diag(VD->getLocation(),
5944 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5945 << VD;
5946 continue;
5947 }
5948 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
5949 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
5950 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
5951 bool IsDecl =
5952 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5953 Diag(VD->getLocation(),
5954 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5955 << VD;
5956 continue;
5957 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00005958 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
5959 // in a Construct]
5960 // Variables with the predetermined data-sharing attributes may not be
5961 // listed in data-sharing attributes clauses, except for the cases
5962 // listed below. For these exceptions only, listing a predetermined
5963 // variable in a data-sharing attribute clause is allowed and overrides
5964 // the variable's predetermined data-sharing attributes.
5965 // OpenMP [2.14.3.6, Restrictions, p.3]
5966 // Any number of reduction clauses can be specified on the directive,
5967 // but a list item can appear only once in the reduction clauses for that
5968 // directive.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005969 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00005970 if (DVar.CKind == OMPC_reduction) {
5971 Diag(ELoc, diag::err_omp_once_referenced)
5972 << getOpenMPClauseName(OMPC_reduction);
5973 if (DVar.RefExpr) {
5974 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
5975 }
5976 } else if (DVar.CKind != OMPC_unknown) {
5977 Diag(ELoc, diag::err_omp_wrong_dsa)
5978 << getOpenMPClauseName(DVar.CKind)
5979 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005980 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00005981 continue;
5982 }
5983
5984 // OpenMP [2.14.3.6, Restrictions, p.1]
5985 // A list item that appears in a reduction clause of a worksharing
5986 // construct must be shared in the parallel regions to which any of the
5987 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf29276e2014-06-18 04:14:57 +00005988 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataev549210e2014-06-24 04:39:47 +00005989 if (isOpenMPWorksharingDirective(CurrDir) &&
5990 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005991 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005992 if (DVar.CKind != OMPC_shared) {
5993 Diag(ELoc, diag::err_omp_required_access)
5994 << getOpenMPClauseName(OMPC_reduction)
5995 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005996 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005997 continue;
5998 }
5999 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006000 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00006001 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs");
6002 auto *RHSVD = buildVarDecl(*this, ELoc, Type, VD->getName());
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006003 // Add initializer for private variable.
6004 Expr *Init = nullptr;
6005 switch (BOK) {
6006 case BO_Add:
6007 case BO_Xor:
6008 case BO_Or:
6009 case BO_LOr:
6010 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
6011 if (Type->isScalarType() || Type->isAnyComplexType()) {
6012 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00006013 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006014 break;
6015 case BO_Mul:
6016 case BO_LAnd:
6017 if (Type->isScalarType() || Type->isAnyComplexType()) {
6018 // '*' and '&&' reduction ops - initializer is '1'.
6019 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
6020 }
6021 break;
6022 case BO_And: {
6023 // '&' reduction op - initializer is '~0'.
6024 QualType OrigType = Type;
6025 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) {
6026 Type = ComplexTy->getElementType();
6027 }
6028 if (Type->isRealFloatingType()) {
6029 llvm::APFloat InitValue =
6030 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
6031 /*isIEEE=*/true);
6032 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
6033 Type, ELoc);
6034 } else if (Type->isScalarType()) {
6035 auto Size = Context.getTypeSize(Type);
6036 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
6037 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
6038 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
6039 }
6040 if (Init && OrigType->isAnyComplexType()) {
6041 // Init = 0xFFFF + 0xFFFFi;
6042 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
6043 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
6044 }
6045 Type = OrigType;
6046 break;
6047 }
6048 case BO_LT:
6049 case BO_GT: {
6050 // 'min' reduction op - initializer is 'Largest representable number in
6051 // the reduction list item type'.
6052 // 'max' reduction op - initializer is 'Least representable number in
6053 // the reduction list item type'.
6054 if (Type->isIntegerType() || Type->isPointerType()) {
6055 bool IsSigned = Type->hasSignedIntegerRepresentation();
6056 auto Size = Context.getTypeSize(Type);
6057 QualType IntTy =
6058 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
6059 llvm::APInt InitValue =
6060 (BOK != BO_LT)
6061 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
6062 : llvm::APInt::getMinValue(Size)
6063 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
6064 : llvm::APInt::getMaxValue(Size);
6065 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
6066 if (Type->isPointerType()) {
6067 // Cast to pointer type.
6068 auto CastExpr = BuildCStyleCastExpr(
6069 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
6070 SourceLocation(), Init);
6071 if (CastExpr.isInvalid())
6072 continue;
6073 Init = CastExpr.get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00006074 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006075 } else if (Type->isRealFloatingType()) {
6076 llvm::APFloat InitValue = llvm::APFloat::getLargest(
6077 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
6078 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
6079 Type, ELoc);
6080 }
6081 break;
6082 }
6083 case BO_PtrMemD:
6084 case BO_PtrMemI:
6085 case BO_MulAssign:
6086 case BO_Div:
6087 case BO_Rem:
6088 case BO_Sub:
6089 case BO_Shl:
6090 case BO_Shr:
6091 case BO_LE:
6092 case BO_GE:
6093 case BO_EQ:
6094 case BO_NE:
6095 case BO_AndAssign:
6096 case BO_XorAssign:
6097 case BO_OrAssign:
6098 case BO_Assign:
6099 case BO_AddAssign:
6100 case BO_SubAssign:
6101 case BO_DivAssign:
6102 case BO_RemAssign:
6103 case BO_ShlAssign:
6104 case BO_ShrAssign:
6105 case BO_Comma:
6106 llvm_unreachable("Unexpected reduction operation");
6107 }
6108 if (Init) {
6109 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
6110 /*TypeMayContainAuto=*/false);
6111 } else {
6112 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
6113 }
6114 if (!RHSVD->hasInit()) {
6115 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
6116 << ReductionIdRange;
6117 bool IsDecl =
6118 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6119 Diag(VD->getLocation(),
6120 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6121 << VD;
6122 continue;
6123 }
Alexey Bataev39f915b82015-05-08 10:41:21 +00006124 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
6125 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006126 ExprResult ReductionOp =
6127 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
6128 LHSDRE, RHSDRE);
6129 if (ReductionOp.isUsable()) {
Alexey Bataev69a47792015-05-07 03:54:03 +00006130 if (BOK != BO_LT && BOK != BO_GT) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006131 ReductionOp =
6132 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
6133 BO_Assign, LHSDRE, ReductionOp.get());
6134 } else {
6135 auto *ConditionalOp = new (Context) ConditionalOperator(
6136 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
6137 RHSDRE, Type, VK_LValue, OK_Ordinary);
6138 ReductionOp =
6139 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
6140 BO_Assign, LHSDRE, ConditionalOp);
6141 }
6142 if (ReductionOp.isUsable()) {
6143 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00006144 }
6145 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006146 if (ReductionOp.isInvalid())
6147 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006148
6149 DSAStack->addDSA(VD, DE, OMPC_reduction);
6150 Vars.push_back(DE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006151 LHSs.push_back(LHSDRE);
6152 RHSs.push_back(RHSDRE);
6153 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00006154 }
6155
6156 if (Vars.empty())
6157 return nullptr;
6158
6159 return OMPReductionClause::Create(
6160 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006161 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, LHSs,
6162 RHSs, ReductionOps);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006163}
6164
Alexander Musman8dba6642014-04-22 13:09:42 +00006165OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step,
6166 SourceLocation StartLoc,
6167 SourceLocation LParenLoc,
6168 SourceLocation ColonLoc,
6169 SourceLocation EndLoc) {
6170 SmallVector<Expr *, 8> Vars;
Alexander Musman3276a272015-03-21 10:12:56 +00006171 SmallVector<Expr *, 8> Inits;
Alexey Bataeved09d242014-05-28 05:53:51 +00006172 for (auto &RefExpr : VarList) {
6173 assert(RefExpr && "NULL expr in OpenMP linear clause.");
6174 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00006175 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006176 Vars.push_back(RefExpr);
Alexander Musman3276a272015-03-21 10:12:56 +00006177 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00006178 continue;
6179 }
6180
6181 // OpenMP [2.14.3.7, linear clause]
6182 // A list item that appears in a linear clause is subject to the private
6183 // clause semantics described in Section 2.14.3.3 on page 159 except as
6184 // noted. In addition, the value of the new list item on each iteration
6185 // of the associated loop(s) corresponds to the value of the original
6186 // list item before entering the construct plus the logical number of
6187 // the iteration times linear-step.
6188
Alexey Bataeved09d242014-05-28 05:53:51 +00006189 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00006190 // OpenMP [2.1, C/C++]
6191 // A list item is a variable name.
6192 // OpenMP [2.14.3.3, Restrictions, p.1]
6193 // A variable that is part of another variable (as an array or
6194 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00006195 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00006196 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006197 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00006198 continue;
6199 }
6200
6201 VarDecl *VD = cast<VarDecl>(DE->getDecl());
6202
6203 // OpenMP [2.14.3.7, linear clause]
6204 // A list-item cannot appear in more than one linear clause.
6205 // A list-item that appears in a linear clause cannot appear in any
6206 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006207 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00006208 if (DVar.RefExpr) {
6209 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
6210 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006211 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00006212 continue;
6213 }
6214
6215 QualType QType = VD->getType();
6216 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
6217 // It will be analyzed later.
6218 Vars.push_back(DE);
Alexander Musman3276a272015-03-21 10:12:56 +00006219 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00006220 continue;
6221 }
6222
6223 // A variable must not have an incomplete type or a reference type.
6224 if (RequireCompleteType(ELoc, QType,
6225 diag::err_omp_linear_incomplete_type)) {
6226 continue;
6227 }
6228 if (QType->isReferenceType()) {
6229 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
6230 << getOpenMPClauseName(OMPC_linear) << QType;
6231 bool IsDecl =
6232 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6233 Diag(VD->getLocation(),
6234 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6235 << VD;
6236 continue;
6237 }
6238
6239 // A list item must not be const-qualified.
6240 if (QType.isConstant(Context)) {
6241 Diag(ELoc, diag::err_omp_const_variable)
6242 << getOpenMPClauseName(OMPC_linear);
6243 bool IsDecl =
6244 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6245 Diag(VD->getLocation(),
6246 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6247 << VD;
6248 continue;
6249 }
6250
6251 // A list item must be of integral or pointer type.
6252 QType = QType.getUnqualifiedType().getCanonicalType();
6253 const Type *Ty = QType.getTypePtrOrNull();
6254 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
6255 !Ty->isPointerType())) {
6256 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
6257 bool IsDecl =
6258 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6259 Diag(VD->getLocation(),
6260 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6261 << VD;
6262 continue;
6263 }
6264
Alexander Musman3276a272015-03-21 10:12:56 +00006265 // Build var to save initial value.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006266 VarDecl *Init = buildVarDecl(*this, ELoc, QType, ".linear.start");
Alexander Musman3276a272015-03-21 10:12:56 +00006267 AddInitializerToDecl(Init, DefaultLvalueConversion(DE).get(),
6268 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006269 auto InitRef = buildDeclRefExpr(
6270 *this, Init, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman8dba6642014-04-22 13:09:42 +00006271 DSAStack->addDSA(VD, DE, OMPC_linear);
6272 Vars.push_back(DE);
Alexander Musman3276a272015-03-21 10:12:56 +00006273 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00006274 }
6275
6276 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006277 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00006278
6279 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00006280 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00006281 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
6282 !Step->isInstantiationDependent() &&
6283 !Step->containsUnexpandedParameterPack()) {
6284 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006285 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00006286 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006287 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006288 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00006289
Alexander Musman3276a272015-03-21 10:12:56 +00006290 // Build var to save the step value.
6291 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006292 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00006293 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006294 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00006295 ExprResult CalcStep =
6296 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
6297
Alexander Musman8dba6642014-04-22 13:09:42 +00006298 // Warn about zero linear step (it would be probably better specified as
6299 // making corresponding variables 'const').
6300 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00006301 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
6302 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00006303 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
6304 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00006305 if (!IsConstant && CalcStep.isUsable()) {
6306 // Calculate the step beforehand instead of doing this on each iteration.
6307 // (This is not used if the number of iterations may be kfold-ed).
6308 CalcStepExpr = CalcStep.get();
6309 }
Alexander Musman8dba6642014-04-22 13:09:42 +00006310 }
6311
6312 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc,
Alexander Musman3276a272015-03-21 10:12:56 +00006313 Vars, Inits, StepExpr, CalcStepExpr);
6314}
6315
6316static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
6317 Expr *NumIterations, Sema &SemaRef,
6318 Scope *S) {
6319 // Walk the vars and build update/final expressions for the CodeGen.
6320 SmallVector<Expr *, 8> Updates;
6321 SmallVector<Expr *, 8> Finals;
6322 Expr *Step = Clause.getStep();
6323 Expr *CalcStep = Clause.getCalcStep();
6324 // OpenMP [2.14.3.7, linear clause]
6325 // If linear-step is not specified it is assumed to be 1.
6326 if (Step == nullptr)
6327 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
6328 else if (CalcStep)
6329 Step = cast<BinaryOperator>(CalcStep)->getLHS();
6330 bool HasErrors = false;
6331 auto CurInit = Clause.inits().begin();
6332 for (auto &RefExpr : Clause.varlists()) {
6333 Expr *InitExpr = *CurInit;
6334
6335 // Build privatized reference to the current linear var.
6336 auto DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00006337 auto PrivateRef =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006338 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
6339 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
6340 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00006341
6342 // Build update: Var = InitExpr + IV * Step
6343 ExprResult Update =
6344 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), PrivateRef,
6345 InitExpr, IV, Step, /* Subtract */ false);
6346 Update = SemaRef.ActOnFinishFullExpr(Update.get());
6347
6348 // Build final: Var = InitExpr + NumIterations * Step
6349 ExprResult Final =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006350 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), PrivateRef,
6351 InitExpr, NumIterations, Step, /* Subtract */ false);
Alexander Musman3276a272015-03-21 10:12:56 +00006352 Final = SemaRef.ActOnFinishFullExpr(Final.get());
6353 if (!Update.isUsable() || !Final.isUsable()) {
6354 Updates.push_back(nullptr);
6355 Finals.push_back(nullptr);
6356 HasErrors = true;
6357 } else {
6358 Updates.push_back(Update.get());
6359 Finals.push_back(Final.get());
6360 }
6361 ++CurInit;
6362 }
6363 Clause.setUpdates(Updates);
6364 Clause.setFinals(Finals);
6365 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00006366}
6367
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006368OMPClause *Sema::ActOnOpenMPAlignedClause(
6369 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
6370 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
6371
6372 SmallVector<Expr *, 8> Vars;
6373 for (auto &RefExpr : VarList) {
6374 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
6375 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6376 // It will be analyzed later.
6377 Vars.push_back(RefExpr);
6378 continue;
6379 }
6380
6381 SourceLocation ELoc = RefExpr->getExprLoc();
6382 // OpenMP [2.1, C/C++]
6383 // A list item is a variable name.
6384 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
6385 if (!DE || !isa<VarDecl>(DE->getDecl())) {
6386 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
6387 continue;
6388 }
6389
6390 VarDecl *VD = cast<VarDecl>(DE->getDecl());
6391
6392 // OpenMP [2.8.1, simd construct, Restrictions]
6393 // The type of list items appearing in the aligned clause must be
6394 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006395 QualType QType = VD->getType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006396 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006397 const Type *Ty = QType.getTypePtrOrNull();
6398 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
6399 !Ty->isPointerType())) {
6400 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
6401 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
6402 bool IsDecl =
6403 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6404 Diag(VD->getLocation(),
6405 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6406 << VD;
6407 continue;
6408 }
6409
6410 // OpenMP [2.8.1, simd construct, Restrictions]
6411 // A list-item cannot appear in more than one aligned clause.
6412 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
6413 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
6414 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
6415 << getOpenMPClauseName(OMPC_aligned);
6416 continue;
6417 }
6418
6419 Vars.push_back(DE);
6420 }
6421
6422 // OpenMP [2.8.1, simd construct, Description]
6423 // The parameter of the aligned clause, alignment, must be a constant
6424 // positive integer expression.
6425 // If no optional parameter is specified, implementation-defined default
6426 // alignments for SIMD instructions on the target platforms are assumed.
6427 if (Alignment != nullptr) {
6428 ExprResult AlignResult =
6429 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
6430 if (AlignResult.isInvalid())
6431 return nullptr;
6432 Alignment = AlignResult.get();
6433 }
6434 if (Vars.empty())
6435 return nullptr;
6436
6437 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
6438 EndLoc, Vars, Alignment);
6439}
6440
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006441OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
6442 SourceLocation StartLoc,
6443 SourceLocation LParenLoc,
6444 SourceLocation EndLoc) {
6445 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006446 SmallVector<Expr *, 8> SrcExprs;
6447 SmallVector<Expr *, 8> DstExprs;
6448 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00006449 for (auto &RefExpr : VarList) {
6450 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
6451 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006452 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006453 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006454 SrcExprs.push_back(nullptr);
6455 DstExprs.push_back(nullptr);
6456 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006457 continue;
6458 }
6459
Alexey Bataeved09d242014-05-28 05:53:51 +00006460 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006461 // OpenMP [2.1, C/C++]
6462 // A list item is a variable name.
6463 // OpenMP [2.14.4.1, Restrictions, p.1]
6464 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00006465 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006466 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006467 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006468 continue;
6469 }
6470
6471 Decl *D = DE->getDecl();
6472 VarDecl *VD = cast<VarDecl>(D);
6473
6474 QualType Type = VD->getType();
6475 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6476 // It will be analyzed later.
6477 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006478 SrcExprs.push_back(nullptr);
6479 DstExprs.push_back(nullptr);
6480 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006481 continue;
6482 }
6483
6484 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
6485 // A list item that appears in a copyin clause must be threadprivate.
6486 if (!DSAStack->isThreadPrivate(VD)) {
6487 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00006488 << getOpenMPClauseName(OMPC_copyin)
6489 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006490 continue;
6491 }
6492
6493 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
6494 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00006495 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006496 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006497 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00006498 auto *SrcVD = buildVarDecl(*this, DE->getLocStart(),
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006499 ElemType.getUnqualifiedType(), ".copyin.src");
Alexey Bataev39f915b82015-05-08 10:41:21 +00006500 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006501 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
6502 auto *DstVD =
6503 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst");
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006504 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006505 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006506 // For arrays generate assignment operation for single element and replace
6507 // it by the original array element in CodeGen.
6508 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
6509 PseudoDstExpr, PseudoSrcExpr);
6510 if (AssignmentOp.isInvalid())
6511 continue;
6512 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
6513 /*DiscardedValue=*/true);
6514 if (AssignmentOp.isInvalid())
6515 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006516
6517 DSAStack->addDSA(VD, DE, OMPC_copyin);
6518 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006519 SrcExprs.push_back(PseudoSrcExpr);
6520 DstExprs.push_back(PseudoDstExpr);
6521 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006522 }
6523
Alexey Bataeved09d242014-05-28 05:53:51 +00006524 if (Vars.empty())
6525 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006526
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006527 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
6528 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006529}
6530
Alexey Bataevbae9a792014-06-27 10:37:06 +00006531OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
6532 SourceLocation StartLoc,
6533 SourceLocation LParenLoc,
6534 SourceLocation EndLoc) {
6535 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00006536 SmallVector<Expr *, 8> SrcExprs;
6537 SmallVector<Expr *, 8> DstExprs;
6538 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00006539 for (auto &RefExpr : VarList) {
6540 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
6541 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6542 // It will be analyzed later.
6543 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00006544 SrcExprs.push_back(nullptr);
6545 DstExprs.push_back(nullptr);
6546 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00006547 continue;
6548 }
6549
6550 SourceLocation ELoc = RefExpr->getExprLoc();
6551 // OpenMP [2.1, C/C++]
6552 // A list item is a variable name.
6553 // OpenMP [2.14.4.1, Restrictions, p.1]
6554 // A list item that appears in a copyin clause must be threadprivate.
6555 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
6556 if (!DE || !isa<VarDecl>(DE->getDecl())) {
6557 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
6558 continue;
6559 }
6560
6561 Decl *D = DE->getDecl();
6562 VarDecl *VD = cast<VarDecl>(D);
6563
6564 QualType Type = VD->getType();
6565 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6566 // It will be analyzed later.
6567 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00006568 SrcExprs.push_back(nullptr);
6569 DstExprs.push_back(nullptr);
6570 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00006571 continue;
6572 }
6573
6574 // OpenMP [2.14.4.2, Restrictions, p.2]
6575 // A list item that appears in a copyprivate clause may not appear in a
6576 // private or firstprivate clause on the single construct.
6577 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006578 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00006579 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
6580 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00006581 Diag(ELoc, diag::err_omp_wrong_dsa)
6582 << getOpenMPClauseName(DVar.CKind)
6583 << getOpenMPClauseName(OMPC_copyprivate);
6584 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6585 continue;
6586 }
6587
6588 // OpenMP [2.11.4.2, Restrictions, p.1]
6589 // All list items that appear in a copyprivate clause must be either
6590 // threadprivate or private in the enclosing context.
6591 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006592 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00006593 if (DVar.CKind == OMPC_shared) {
6594 Diag(ELoc, diag::err_omp_required_access)
6595 << getOpenMPClauseName(OMPC_copyprivate)
6596 << "threadprivate or private in the enclosing context";
6597 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6598 continue;
6599 }
6600 }
6601 }
6602
Alexey Bataev7a3e5852015-05-19 08:19:24 +00006603 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00006604 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00006605 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006606 << getOpenMPClauseName(OMPC_copyprivate) << Type
6607 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00006608 bool IsDecl =
6609 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6610 Diag(VD->getLocation(),
6611 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6612 << VD;
6613 continue;
6614 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006615
Alexey Bataevbae9a792014-06-27 10:37:06 +00006616 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
6617 // A variable of class type (or array thereof) that appears in a
6618 // copyin clause requires an accessible, unambiguous copy assignment
6619 // operator for the class type.
Alexey Bataev420d45b2015-04-14 05:11:24 +00006620 Type = Context.getBaseElementType(Type).getUnqualifiedType();
6621 auto *SrcVD =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006622 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.src");
Alexey Bataev420d45b2015-04-14 05:11:24 +00006623 auto *PseudoSrcExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006624 buildDeclRefExpr(*this, SrcVD, Type, DE->getExprLoc());
Alexey Bataev420d45b2015-04-14 05:11:24 +00006625 auto *DstVD =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006626 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.dst");
Alexey Bataev420d45b2015-04-14 05:11:24 +00006627 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006628 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataeva63048e2015-03-23 06:18:07 +00006629 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
6630 PseudoDstExpr, PseudoSrcExpr);
6631 if (AssignmentOp.isInvalid())
6632 continue;
6633 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
6634 /*DiscardedValue=*/true);
6635 if (AssignmentOp.isInvalid())
6636 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00006637
6638 // No need to mark vars as copyprivate, they are already threadprivate or
6639 // implicitly private.
6640 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00006641 SrcExprs.push_back(PseudoSrcExpr);
6642 DstExprs.push_back(PseudoDstExpr);
6643 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +00006644 }
6645
6646 if (Vars.empty())
6647 return nullptr;
6648
Alexey Bataeva63048e2015-03-23 06:18:07 +00006649 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
6650 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00006651}
6652
Alexey Bataev6125da92014-07-21 11:26:11 +00006653OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
6654 SourceLocation StartLoc,
6655 SourceLocation LParenLoc,
6656 SourceLocation EndLoc) {
6657 if (VarList.empty())
6658 return nullptr;
6659
6660 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
6661}
Alexey Bataevdea47612014-07-23 07:46:59 +00006662
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006663OMPClause *
6664Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
6665 SourceLocation DepLoc, SourceLocation ColonLoc,
6666 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
6667 SourceLocation LParenLoc, SourceLocation EndLoc) {
6668 if (DepKind == OMPC_DEPEND_unknown) {
6669 std::string Values;
6670 std::string Sep(", ");
6671 for (unsigned i = 0; i < OMPC_DEPEND_unknown; ++i) {
6672 Values += "'";
6673 Values += getOpenMPSimpleClauseTypeName(OMPC_depend, i);
6674 Values += "'";
6675 switch (i) {
6676 case OMPC_DEPEND_unknown - 2:
6677 Values += " or ";
6678 break;
6679 case OMPC_DEPEND_unknown - 1:
6680 break;
6681 default:
6682 Values += Sep;
6683 break;
6684 }
6685 }
6686 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
6687 << Values << getOpenMPClauseName(OMPC_depend);
6688 return nullptr;
6689 }
6690 SmallVector<Expr *, 8> Vars;
6691 for (auto &RefExpr : VarList) {
6692 assert(RefExpr && "NULL expr in OpenMP shared clause.");
6693 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6694 // It will be analyzed later.
6695 Vars.push_back(RefExpr);
6696 continue;
6697 }
6698
6699 SourceLocation ELoc = RefExpr->getExprLoc();
6700 // OpenMP [2.11.1.1, Restrictions, p.3]
6701 // A variable that is part of another variable (such as a field of a
6702 // structure) but is not an array element or an array section cannot appear
6703 // in a depend clause.
6704 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
6705 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
6706 ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
6707 if (!RefExpr->IgnoreParenImpCasts()->isLValue() || (!ASE && !DE) ||
6708 (DE && !isa<VarDecl>(DE->getDecl())) ||
6709 (ASE && !ASE->getBase()->getType()->isAnyPointerType() &&
6710 !ASE->getBase()->getType()->isArrayType())) {
6711 Diag(ELoc, diag::err_omp_expected_var_name_or_array_item)
6712 << RefExpr->getSourceRange();
6713 continue;
6714 }
6715
6716 Vars.push_back(RefExpr->IgnoreParenImpCasts());
6717 }
6718
6719 if (Vars.empty())
6720 return nullptr;
6721
6722 return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
6723 DepLoc, ColonLoc, Vars);
6724}