blob: 88a705056a2ccb96299d9d72ce1a661214f36486 [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 Bataevf841bd92014-12-16 07:00:22 +0000660 if (DSAStack->getCurrentDirective() != OMPD_unknown) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000661 if (DSAStack->isLoopControlVariable(VD) ||
662 (VD->hasLocalStorage() &&
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000663 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
664 DSAStack->isForceVarCapturing())
Alexey Bataev9c821032015-04-30 04:23:23 +0000665 return true;
Alexey Bataevaac108a2015-06-23 04:51:00 +0000666 auto DVarPrivate = DSAStack->getTopDSA(VD, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000667 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
668 return true;
669 DVarPrivate = DSAStack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(),
Alexey Bataevaac108a2015-06-23 04:51:00 +0000670 DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000671 return DVarPrivate.CKind != OMPC_unknown;
672 }
673 return false;
674}
675
Alexey Bataevaac108a2015-06-23 04:51:00 +0000676bool Sema::isOpenMPPrivateVar(VarDecl *VD, unsigned Level) {
677 assert(LangOpts.OpenMP && "OpenMP is not allowed");
678 return DSAStack->hasExplicitDSA(
679 VD, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level);
680}
681
Alexey Bataeved09d242014-05-28 05:53:51 +0000682void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000683
684void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
685 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000686 Scope *CurScope, SourceLocation Loc) {
687 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000688 PushExpressionEvaluationContext(PotentiallyEvaluated);
689}
690
Alexey Bataevaac108a2015-06-23 04:51:00 +0000691void Sema::StartOpenMPClause(OpenMPClauseKind K) {
692 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000693}
694
Alexey Bataevaac108a2015-06-23 04:51:00 +0000695void Sema::EndOpenMPClause() {
696 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000697}
698
Alexey Bataev758e55e2013-09-06 18:03:48 +0000699void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000700 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
701 // A variable of class type (or array thereof) that appears in a lastprivate
702 // clause requires an accessible, unambiguous default constructor for the
703 // class type, unless the list item is also specified in a firstprivate
704 // clause.
705 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000706 for (auto *C : D->clauses()) {
707 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
708 SmallVector<Expr *, 8> PrivateCopies;
709 for (auto *DE : Clause->varlists()) {
710 if (DE->isValueDependent() || DE->isTypeDependent()) {
711 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000712 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +0000713 }
714 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(DE)->getDecl());
Alexey Bataevf120c0d2015-05-19 07:46:42 +0000715 QualType Type = VD->getType();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000716 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000717 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000718 // Generate helper private variable and initialize it with the
719 // default value. The address of the original variable is replaced
720 // by the address of the new private variable in CodeGen. This new
721 // variable is not added to IdResolver, so the code in the OpenMP
722 // region uses original variable for proper diagnostics.
Alexey Bataev39f915b82015-05-08 10:41:21 +0000723 auto *VDPrivate =
Alexey Bataevf120c0d2015-05-19 07:46:42 +0000724 buildVarDecl(*this, DE->getExprLoc(), Type.getUnqualifiedType(),
725 VD->getName());
Alexey Bataev38e89532015-04-16 04:54:05 +0000726 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
727 if (VDPrivate->isInvalidDecl())
728 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +0000729 PrivateCopies.push_back(buildDeclRefExpr(
730 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +0000731 } else {
732 // The variable is also a firstprivate, so initialization sequence
733 // for private copy is generated already.
734 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000735 }
736 }
Alexey Bataev38e89532015-04-16 04:54:05 +0000737 // Set initializers to private copies if no errors were found.
738 if (PrivateCopies.size() == Clause->varlist_size()) {
739 Clause->setPrivateCopies(PrivateCopies);
740 }
Alexey Bataevf29276e2014-06-18 04:14:57 +0000741 }
742 }
743 }
744
Alexey Bataev758e55e2013-09-06 18:03:48 +0000745 DSAStack->pop();
746 DiscardCleanupsInEvaluationContext();
747 PopExpressionEvaluationContext();
748}
749
Alexander Musman3276a272015-03-21 10:12:56 +0000750static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
751 Expr *NumIterations, Sema &SemaRef,
752 Scope *S);
753
Alexey Bataeva769e072013-03-22 06:34:35 +0000754namespace {
755
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000756class VarDeclFilterCCC : public CorrectionCandidateCallback {
757private:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000758 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +0000759
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000760public:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000761 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +0000762 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000763 NamedDecl *ND = Candidate.getCorrectionDecl();
764 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
765 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000766 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
767 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +0000768 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000769 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000770 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000771};
Alexey Bataeved09d242014-05-28 05:53:51 +0000772} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000773
774ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
775 CXXScopeSpec &ScopeSpec,
776 const DeclarationNameInfo &Id) {
777 LookupResult Lookup(*this, Id, LookupOrdinaryName);
778 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
779
780 if (Lookup.isAmbiguous())
781 return ExprError();
782
783 VarDecl *VD;
784 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +0000785 if (TypoCorrection Corrected = CorrectTypo(
786 Id, LookupOrdinaryName, CurScope, nullptr,
787 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +0000788 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000789 PDiag(Lookup.empty()
790 ? diag::err_undeclared_var_use_suggest
791 : diag::err_omp_expected_var_arg_suggest)
792 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +0000793 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000794 } else {
Richard Smithf9b15102013-08-17 00:46:16 +0000795 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
796 : diag::err_omp_expected_var_arg)
797 << Id.getName();
798 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000799 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000800 } else {
801 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000802 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000803 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
804 return ExprError();
805 }
806 }
807 Lookup.suppressDiagnostics();
808
809 // OpenMP [2.9.2, Syntax, C/C++]
810 // Variables must be file-scope, namespace-scope, or static block-scope.
811 if (!VD->hasGlobalStorage()) {
812 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000813 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
814 bool IsDecl =
815 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000816 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +0000817 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
818 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000819 return ExprError();
820 }
821
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000822 VarDecl *CanonicalVD = VD->getCanonicalDecl();
823 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000824 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
825 // A threadprivate directive for file-scope variables must appear outside
826 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000827 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
828 !getCurLexicalContext()->isTranslationUnit()) {
829 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000830 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
831 bool IsDecl =
832 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
833 Diag(VD->getLocation(),
834 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
835 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000836 return ExprError();
837 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000838 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
839 // A threadprivate directive for static class member variables must appear
840 // in the class definition, in the same scope in which the member
841 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000842 if (CanonicalVD->isStaticDataMember() &&
843 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
844 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000845 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
846 bool IsDecl =
847 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
848 Diag(VD->getLocation(),
849 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
850 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000851 return ExprError();
852 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000853 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
854 // A threadprivate directive for namespace-scope variables must appear
855 // outside any definition or declaration other than the namespace
856 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000857 if (CanonicalVD->getDeclContext()->isNamespace() &&
858 (!getCurLexicalContext()->isFileContext() ||
859 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
860 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000861 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
862 bool IsDecl =
863 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
864 Diag(VD->getLocation(),
865 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
866 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000867 return ExprError();
868 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000869 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
870 // A threadprivate directive for static block-scope variables must appear
871 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000872 if (CanonicalVD->isStaticLocal() && CurScope &&
873 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000874 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000875 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
876 bool IsDecl =
877 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
878 Diag(VD->getLocation(),
879 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
880 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000881 return ExprError();
882 }
883
884 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
885 // A threadprivate directive must lexically precede all references to any
886 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000887 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000888 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +0000889 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000890 return ExprError();
891 }
892
893 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +0000894 ExprResult DE = buildDeclRefExpr(*this, VD, ExprType, Id.getLoc());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000895 return DE;
896}
897
Alexey Bataeved09d242014-05-28 05:53:51 +0000898Sema::DeclGroupPtrTy
899Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
900 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000901 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000902 CurContext->addDecl(D);
903 return DeclGroupPtrTy::make(DeclGroupRef(D));
904 }
905 return DeclGroupPtrTy();
906}
907
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000908namespace {
909class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
910 Sema &SemaRef;
911
912public:
913 bool VisitDeclRefExpr(const DeclRefExpr *E) {
914 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
915 if (VD->hasLocalStorage()) {
916 SemaRef.Diag(E->getLocStart(),
917 diag::err_omp_local_var_in_threadprivate_init)
918 << E->getSourceRange();
919 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
920 << VD << VD->getSourceRange();
921 return true;
922 }
923 }
924 return false;
925 }
926 bool VisitStmt(const Stmt *S) {
927 for (auto Child : S->children()) {
928 if (Child && Visit(Child))
929 return true;
930 }
931 return false;
932 }
Alexey Bataev23b69422014-06-18 07:08:49 +0000933 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000934};
935} // namespace
936
Alexey Bataeved09d242014-05-28 05:53:51 +0000937OMPThreadPrivateDecl *
938Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000939 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +0000940 for (auto &RefExpr : VarList) {
941 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000942 VarDecl *VD = cast<VarDecl>(DE->getDecl());
943 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +0000944
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000945 QualType QType = VD->getType();
946 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
947 // It will be analyzed later.
948 Vars.push_back(DE);
949 continue;
950 }
951
Alexey Bataeva769e072013-03-22 06:34:35 +0000952 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
953 // A threadprivate variable must not have an incomplete type.
954 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000955 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000956 continue;
957 }
958
959 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
960 // A threadprivate variable must not have a reference type.
961 if (VD->getType()->isReferenceType()) {
962 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000963 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
964 bool IsDecl =
965 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
966 Diag(VD->getLocation(),
967 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
968 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000969 continue;
970 }
971
Samuel Antaof8b50122015-07-13 22:54:53 +0000972 // Check if this is a TLS variable. If TLS is not being supported, produce
973 // the corresponding diagnostic.
974 if ((VD->getTLSKind() != VarDecl::TLS_None &&
975 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
976 getLangOpts().OpenMPUseTLS &&
977 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +0000978 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
979 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +0000980 Diag(ILoc, diag::err_omp_var_thread_local)
981 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +0000982 bool IsDecl =
983 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
984 Diag(VD->getLocation(),
985 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
986 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000987 continue;
988 }
989
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000990 // Check if initial value of threadprivate variable reference variable with
991 // local storage (it is not supported by runtime).
992 if (auto Init = VD->getAnyInitializer()) {
993 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000994 if (Checker.Visit(Init))
995 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000996 }
997
Alexey Bataeved09d242014-05-28 05:53:51 +0000998 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +0000999 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00001000 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1001 Context, SourceRange(Loc, Loc)));
1002 if (auto *ML = Context.getASTMutationListener())
1003 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00001004 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001005 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001006 if (!Vars.empty()) {
1007 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1008 Vars);
1009 D->setAccess(AS_public);
1010 }
1011 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00001012}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001013
Alexey Bataev7ff55242014-06-19 09:13:45 +00001014static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
1015 const VarDecl *VD, DSAStackTy::DSAVarData DVar,
1016 bool IsLoopIterVar = false) {
1017 if (DVar.RefExpr) {
1018 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1019 << getOpenMPClauseName(DVar.CKind);
1020 return;
1021 }
1022 enum {
1023 PDSA_StaticMemberShared,
1024 PDSA_StaticLocalVarShared,
1025 PDSA_LoopIterVarPrivate,
1026 PDSA_LoopIterVarLinear,
1027 PDSA_LoopIterVarLastprivate,
1028 PDSA_ConstVarShared,
1029 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001030 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001031 PDSA_LocalVarPrivate,
1032 PDSA_Implicit
1033 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001034 bool ReportHint = false;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001035 auto ReportLoc = VD->getLocation();
Alexey Bataev7ff55242014-06-19 09:13:45 +00001036 if (IsLoopIterVar) {
1037 if (DVar.CKind == OMPC_private)
1038 Reason = PDSA_LoopIterVarPrivate;
1039 else if (DVar.CKind == OMPC_lastprivate)
1040 Reason = PDSA_LoopIterVarLastprivate;
1041 else
1042 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001043 } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) {
1044 Reason = PDSA_TaskVarFirstprivate;
1045 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001046 } else if (VD->isStaticLocal())
1047 Reason = PDSA_StaticLocalVarShared;
1048 else if (VD->isStaticDataMember())
1049 Reason = PDSA_StaticMemberShared;
1050 else if (VD->isFileVarDecl())
1051 Reason = PDSA_GlobalVarShared;
1052 else if (VD->getType().isConstant(SemaRef.getASTContext()))
1053 Reason = PDSA_ConstVarShared;
Alexey Bataevbae9a792014-06-27 10:37:06 +00001054 else if (VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00001055 ReportHint = true;
1056 Reason = PDSA_LocalVarPrivate;
1057 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00001058 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001059 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00001060 << Reason << ReportHint
1061 << getOpenMPDirectiveName(Stack->getCurrentDirective());
1062 } else if (DVar.ImplicitDSALoc.isValid()) {
1063 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1064 << getOpenMPClauseName(DVar.CKind);
1065 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00001066}
1067
Alexey Bataev758e55e2013-09-06 18:03:48 +00001068namespace {
1069class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1070 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001071 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001072 bool ErrorFound;
1073 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001074 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev4acb8592014-07-07 13:01:15 +00001075 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +00001076
Alexey Bataev758e55e2013-09-06 18:03:48 +00001077public:
1078 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001079 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001080 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +00001081 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1082 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001083
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001084 auto DVar = Stack->getTopDSA(VD, false);
1085 // Check if the variable has explicit DSA set and stop analysis if it so.
1086 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001087
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001088 auto ELoc = E->getExprLoc();
1089 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001090 // The default(none) clause requires that each variable that is referenced
1091 // in the construct, and does not have a predetermined data-sharing
1092 // attribute, must have its data-sharing attribute explicitly determined
1093 // by being listed in a data-sharing attribute clause.
1094 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001095 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001096 VarsWithInheritedDSA.count(VD) == 0) {
1097 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001098 return;
1099 }
1100
1101 // OpenMP [2.9.3.6, Restrictions, p.2]
1102 // A list item that appears in a reduction clause of the innermost
1103 // enclosing worksharing or parallel construct may not be accessed in an
1104 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +00001105 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001106 [](OpenMPDirectiveKind K) -> bool {
1107 return isOpenMPParallelDirective(K) ||
Alexey Bataev13314bf2014-10-09 04:18:56 +00001108 isOpenMPWorksharingDirective(K) ||
1109 isOpenMPTeamsDirective(K);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001110 },
1111 false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001112 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
1113 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001114 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1115 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001116 return;
1117 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001118
1119 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001120 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001121 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001122 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001123 }
1124 }
1125 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001126 for (auto *C : S->clauses()) {
1127 // Skip analysis of arguments of implicitly defined firstprivate clause
1128 // for task directives.
1129 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1130 for (auto *CC : C->children()) {
1131 if (CC)
1132 Visit(CC);
1133 }
1134 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001135 }
1136 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001137 for (auto *C : S->children()) {
1138 if (C && !isa<OMPExecutableDirective>(C))
1139 Visit(C);
1140 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001141 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001142
1143 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001144 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001145 llvm::DenseMap<VarDecl *, Expr *> &getVarsWithInheritedDSA() {
1146 return VarsWithInheritedDSA;
1147 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001148
Alexey Bataev7ff55242014-06-19 09:13:45 +00001149 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1150 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00001151};
Alexey Bataeved09d242014-05-28 05:53:51 +00001152} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001153
Alexey Bataevbae9a792014-06-27 10:37:06 +00001154void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001155 switch (DKind) {
1156 case OMPD_parallel: {
1157 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1158 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001159 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001160 std::make_pair(".global_tid.", KmpInt32PtrTy),
1161 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1162 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001163 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001164 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1165 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001166 break;
1167 }
1168 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001169 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001170 std::make_pair(StringRef(), QualType()) // __context with shared vars
1171 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001172 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1173 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001174 break;
1175 }
1176 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001177 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001178 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001179 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001180 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1181 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001182 break;
1183 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001184 case OMPD_for_simd: {
1185 Sema::CapturedParamNameType Params[] = {
1186 std::make_pair(StringRef(), QualType()) // __context with shared vars
1187 };
1188 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1189 Params);
1190 break;
1191 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001192 case OMPD_sections: {
1193 Sema::CapturedParamNameType Params[] = {
1194 std::make_pair(StringRef(), QualType()) // __context with shared vars
1195 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001196 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1197 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001198 break;
1199 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001200 case OMPD_section: {
1201 Sema::CapturedParamNameType Params[] = {
1202 std::make_pair(StringRef(), QualType()) // __context with shared vars
1203 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001204 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1205 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001206 break;
1207 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001208 case OMPD_single: {
1209 Sema::CapturedParamNameType Params[] = {
1210 std::make_pair(StringRef(), QualType()) // __context with shared vars
1211 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001212 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1213 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001214 break;
1215 }
Alexander Musman80c22892014-07-17 08:54:58 +00001216 case OMPD_master: {
1217 Sema::CapturedParamNameType Params[] = {
1218 std::make_pair(StringRef(), QualType()) // __context with shared vars
1219 };
1220 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1221 Params);
1222 break;
1223 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001224 case OMPD_critical: {
1225 Sema::CapturedParamNameType Params[] = {
1226 std::make_pair(StringRef(), QualType()) // __context with shared vars
1227 };
1228 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1229 Params);
1230 break;
1231 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001232 case OMPD_parallel_for: {
1233 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1234 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1235 Sema::CapturedParamNameType Params[] = {
1236 std::make_pair(".global_tid.", KmpInt32PtrTy),
1237 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1238 std::make_pair(StringRef(), QualType()) // __context with shared vars
1239 };
1240 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1241 Params);
1242 break;
1243 }
Alexander Musmane4e893b2014-09-23 09:33:00 +00001244 case OMPD_parallel_for_simd: {
1245 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1246 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1247 Sema::CapturedParamNameType Params[] = {
1248 std::make_pair(".global_tid.", KmpInt32PtrTy),
1249 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1250 std::make_pair(StringRef(), QualType()) // __context with shared vars
1251 };
1252 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1253 Params);
1254 break;
1255 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001256 case OMPD_parallel_sections: {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001257 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1258 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001259 Sema::CapturedParamNameType Params[] = {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001260 std::make_pair(".global_tid.", KmpInt32PtrTy),
1261 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001262 std::make_pair(StringRef(), QualType()) // __context with shared vars
1263 };
1264 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1265 Params);
1266 break;
1267 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001268 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001269 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001270 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1271 FunctionProtoType::ExtProtoInfo EPI;
1272 EPI.Variadic = true;
1273 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001274 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001275 std::make_pair(".global_tid.", KmpInt32Ty),
1276 std::make_pair(".part_id.", KmpInt32Ty),
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001277 std::make_pair(".privates.",
1278 Context.VoidPtrTy.withConst().withRestrict()),
1279 std::make_pair(
1280 ".copy_fn.",
1281 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001282 std::make_pair(StringRef(), QualType()) // __context with shared vars
1283 };
1284 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1285 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001286 // Mark this captured region as inlined, because we don't use outlined
1287 // function directly.
1288 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1289 AlwaysInlineAttr::CreateImplicit(
1290 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001291 break;
1292 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001293 case OMPD_ordered: {
1294 Sema::CapturedParamNameType Params[] = {
1295 std::make_pair(StringRef(), QualType()) // __context with shared vars
1296 };
1297 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1298 Params);
1299 break;
1300 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001301 case OMPD_atomic: {
1302 Sema::CapturedParamNameType Params[] = {
1303 std::make_pair(StringRef(), QualType()) // __context with shared vars
1304 };
1305 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1306 Params);
1307 break;
1308 }
Michael Wong65f367f2015-07-21 13:44:28 +00001309 case OMPD_target_data:
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001310 case OMPD_target: {
1311 Sema::CapturedParamNameType Params[] = {
1312 std::make_pair(StringRef(), QualType()) // __context with shared vars
1313 };
1314 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1315 Params);
1316 break;
1317 }
Alexey Bataev13314bf2014-10-09 04:18:56 +00001318 case OMPD_teams: {
1319 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1320 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1321 Sema::CapturedParamNameType Params[] = {
1322 std::make_pair(".global_tid.", KmpInt32PtrTy),
1323 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1324 std::make_pair(StringRef(), QualType()) // __context with shared vars
1325 };
1326 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1327 Params);
1328 break;
1329 }
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001330 case OMPD_taskgroup: {
1331 Sema::CapturedParamNameType Params[] = {
1332 std::make_pair(StringRef(), QualType()) // __context with shared vars
1333 };
1334 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1335 Params);
1336 break;
1337 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001338 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001339 case OMPD_taskyield:
1340 case OMPD_barrier:
1341 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001342 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001343 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00001344 case OMPD_flush:
Alexey Bataev9959db52014-05-06 10:08:46 +00001345 llvm_unreachable("OpenMP Directive is not allowed");
1346 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001347 llvm_unreachable("Unknown OpenMP directive");
1348 }
1349}
1350
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001351StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1352 ArrayRef<OMPClause *> Clauses) {
1353 if (!S.isUsable()) {
1354 ActOnCapturedRegionError();
1355 return StmtError();
1356 }
Alexey Bataev040d5402015-05-12 08:35:28 +00001357 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001358 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001359 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001360 Clause->getClauseKind() == OMPC_copyprivate ||
1361 (getLangOpts().OpenMPUseTLS &&
1362 getASTContext().getTargetInfo().isTLSSupported() &&
1363 Clause->getClauseKind() == OMPC_copyin)) {
1364 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001365 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001366 for (auto *VarRef : Clause->children()) {
1367 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001368 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001369 }
1370 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001371 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev040d5402015-05-12 08:35:28 +00001372 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
1373 Clause->getClauseKind() == OMPC_schedule) {
1374 // Mark all variables in private list clauses as used in inner region.
1375 // Required for proper codegen of combined directives.
1376 // TODO: add processing for other clauses.
1377 if (auto *E = cast_or_null<Expr>(
1378 cast<OMPScheduleClause>(Clause)->getHelperChunkSize())) {
1379 MarkDeclarationsReferencedInExpr(E);
1380 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001381 }
1382 }
1383 return ActOnCapturedRegionEnd(S.get());
1384}
1385
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001386static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1387 OpenMPDirectiveKind CurrentRegion,
1388 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001389 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001390 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001391 // Allowed nesting of constructs
1392 // +------------------+-----------------+------------------------------------+
1393 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1394 // +------------------+-----------------+------------------------------------+
1395 // | parallel | parallel | * |
1396 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001397 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001398 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001399 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001400 // | parallel | simd | * |
1401 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001402 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001403 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001404 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001405 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001406 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001407 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001408 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001409 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001410 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001411 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001412 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001413 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001414 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001415 // | parallel | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001416 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001417 // | parallel | cancellation | |
1418 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001419 // | parallel | cancel | ! |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001420 // +------------------+-----------------+------------------------------------+
1421 // | for | parallel | * |
1422 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001423 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001424 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001425 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001426 // | for | simd | * |
1427 // | for | sections | + |
1428 // | for | section | + |
1429 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001430 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001431 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001432 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001433 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001434 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001435 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001436 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001437 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001438 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001439 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001440 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001441 // | for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001442 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001443 // | for | cancellation | |
1444 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001445 // | for | cancel | ! |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001446 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001447 // | master | parallel | * |
1448 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001449 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001450 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001451 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001452 // | master | simd | * |
1453 // | master | sections | + |
1454 // | master | section | + |
1455 // | master | single | + |
1456 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001457 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001458 // | master |parallel sections| * |
1459 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001460 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001461 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001462 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001463 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001464 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001465 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001466 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001467 // | master | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001468 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001469 // | master | cancellation | |
1470 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001471 // | master | cancel | |
Alexander Musman80c22892014-07-17 08:54:58 +00001472 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001473 // | critical | parallel | * |
1474 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001475 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001476 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001477 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001478 // | critical | simd | * |
1479 // | critical | sections | + |
1480 // | critical | section | + |
1481 // | critical | single | + |
1482 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001483 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001484 // | critical |parallel sections| * |
1485 // | critical | task | * |
1486 // | critical | taskyield | * |
1487 // | critical | barrier | + |
1488 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001489 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001490 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001491 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001492 // | critical | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001493 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001494 // | critical | cancellation | |
1495 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001496 // | critical | cancel | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001497 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001498 // | simd | parallel | |
1499 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001500 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00001501 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001502 // | simd | critical | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001503 // | simd | simd | |
1504 // | simd | sections | |
1505 // | simd | section | |
1506 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001507 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001508 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001509 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001510 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00001511 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001512 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001513 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001514 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00001515 // | simd | flush | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001516 // | simd | ordered | |
Alexey Bataev0162e452014-07-22 10:10:35 +00001517 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001518 // | simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001519 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001520 // | simd | cancellation | |
1521 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001522 // | simd | cancel | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001523 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00001524 // | for simd | parallel | |
1525 // | for simd | for | |
1526 // | for simd | for simd | |
1527 // | for simd | master | |
1528 // | for simd | critical | |
1529 // | for simd | simd | |
1530 // | for simd | sections | |
1531 // | for simd | section | |
1532 // | for simd | single | |
1533 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001534 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001535 // | for simd |parallel sections| |
1536 // | for simd | task | |
1537 // | for simd | taskyield | |
1538 // | for simd | barrier | |
1539 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001540 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001541 // | for simd | flush | |
1542 // | for simd | ordered | |
1543 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001544 // | for simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001545 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001546 // | for simd | cancellation | |
1547 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001548 // | for simd | cancel | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001549 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00001550 // | parallel for simd| parallel | |
1551 // | parallel for simd| for | |
1552 // | parallel for simd| for simd | |
1553 // | parallel for simd| master | |
1554 // | parallel for simd| critical | |
1555 // | parallel for simd| simd | |
1556 // | parallel for simd| sections | |
1557 // | parallel for simd| section | |
1558 // | parallel for simd| single | |
1559 // | parallel for simd| parallel for | |
1560 // | parallel for simd|parallel for simd| |
1561 // | parallel for simd|parallel sections| |
1562 // | parallel for simd| task | |
1563 // | parallel for simd| taskyield | |
1564 // | parallel for simd| barrier | |
1565 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001566 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001567 // | parallel for simd| flush | |
1568 // | parallel for simd| ordered | |
1569 // | parallel for simd| atomic | |
1570 // | parallel for simd| target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001571 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001572 // | parallel for simd| cancellation | |
1573 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001574 // | parallel for simd| cancel | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001575 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001576 // | sections | parallel | * |
1577 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001578 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001579 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001580 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001581 // | sections | simd | * |
1582 // | sections | sections | + |
1583 // | sections | section | * |
1584 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001585 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001586 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001587 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001588 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001589 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001590 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001591 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001592 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001593 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001594 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001595 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001596 // | sections | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001597 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001598 // | sections | cancellation | |
1599 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001600 // | sections | cancel | ! |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001601 // +------------------+-----------------+------------------------------------+
1602 // | section | parallel | * |
1603 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001604 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001605 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001606 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001607 // | section | simd | * |
1608 // | section | sections | + |
1609 // | section | section | + |
1610 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001611 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001612 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001613 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001614 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001615 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001616 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001617 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001618 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001619 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001620 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001621 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001622 // | section | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001623 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001624 // | section | cancellation | |
1625 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001626 // | section | cancel | ! |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001627 // +------------------+-----------------+------------------------------------+
1628 // | single | parallel | * |
1629 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001630 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001631 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001632 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001633 // | single | simd | * |
1634 // | single | sections | + |
1635 // | single | section | + |
1636 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001637 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001638 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001639 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001640 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001641 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001642 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001643 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001644 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001645 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001646 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001647 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001648 // | single | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001649 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001650 // | single | cancellation | |
1651 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001652 // | single | cancel | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001653 // +------------------+-----------------+------------------------------------+
1654 // | parallel for | parallel | * |
1655 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001656 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001657 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001658 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001659 // | parallel for | simd | * |
1660 // | parallel for | sections | + |
1661 // | parallel for | section | + |
1662 // | parallel for | single | + |
1663 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001664 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001665 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001666 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001667 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001668 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001669 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001670 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001671 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001672 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001673 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001674 // | parallel for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001675 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001676 // | parallel for | cancellation | |
1677 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001678 // | parallel for | cancel | ! |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001679 // +------------------+-----------------+------------------------------------+
1680 // | parallel sections| parallel | * |
1681 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001682 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001683 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001684 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001685 // | parallel sections| simd | * |
1686 // | parallel sections| sections | + |
1687 // | parallel sections| section | * |
1688 // | parallel sections| single | + |
1689 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001690 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001691 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001692 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001693 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001694 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001695 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001696 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001697 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001698 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001699 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001700 // | parallel sections| target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001701 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001702 // | parallel sections| cancellation | |
1703 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001704 // | parallel sections| cancel | ! |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001705 // +------------------+-----------------+------------------------------------+
1706 // | task | parallel | * |
1707 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001708 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001709 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001710 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001711 // | task | simd | * |
1712 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001713 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001714 // | task | single | + |
1715 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001716 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001717 // | task |parallel sections| * |
1718 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001719 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001720 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001721 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001722 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001723 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001724 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001725 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001726 // | task | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001727 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001728 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00001729 // | | point | ! |
1730 // | task | cancel | ! |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001731 // +------------------+-----------------+------------------------------------+
1732 // | ordered | parallel | * |
1733 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001734 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001735 // | ordered | master | * |
1736 // | ordered | critical | * |
1737 // | ordered | simd | * |
1738 // | ordered | sections | + |
1739 // | ordered | section | + |
1740 // | ordered | single | + |
1741 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001742 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001743 // | ordered |parallel sections| * |
1744 // | ordered | task | * |
1745 // | ordered | taskyield | * |
1746 // | ordered | barrier | + |
1747 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001748 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001749 // | ordered | flush | * |
1750 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001751 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001752 // | ordered | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001753 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001754 // | ordered | cancellation | |
1755 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001756 // | ordered | cancel | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001757 // +------------------+-----------------+------------------------------------+
1758 // | atomic | parallel | |
1759 // | atomic | for | |
1760 // | atomic | for simd | |
1761 // | atomic | master | |
1762 // | atomic | critical | |
1763 // | atomic | simd | |
1764 // | atomic | sections | |
1765 // | atomic | section | |
1766 // | atomic | single | |
1767 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001768 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001769 // | atomic |parallel sections| |
1770 // | atomic | task | |
1771 // | atomic | taskyield | |
1772 // | atomic | barrier | |
1773 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001774 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001775 // | atomic | flush | |
1776 // | atomic | ordered | |
1777 // | atomic | atomic | |
1778 // | atomic | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001779 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001780 // | atomic | cancellation | |
1781 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001782 // | atomic | cancel | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001783 // +------------------+-----------------+------------------------------------+
1784 // | target | parallel | * |
1785 // | target | for | * |
1786 // | target | for simd | * |
1787 // | target | master | * |
1788 // | target | critical | * |
1789 // | target | simd | * |
1790 // | target | sections | * |
1791 // | target | section | * |
1792 // | target | single | * |
1793 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001794 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001795 // | target |parallel sections| * |
1796 // | target | task | * |
1797 // | target | taskyield | * |
1798 // | target | barrier | * |
1799 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001800 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001801 // | target | flush | * |
1802 // | target | ordered | * |
1803 // | target | atomic | * |
1804 // | target | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001805 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001806 // | target | cancellation | |
1807 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001808 // | target | cancel | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001809 // +------------------+-----------------+------------------------------------+
1810 // | teams | parallel | * |
1811 // | teams | for | + |
1812 // | teams | for simd | + |
1813 // | teams | master | + |
1814 // | teams | critical | + |
1815 // | teams | simd | + |
1816 // | teams | sections | + |
1817 // | teams | section | + |
1818 // | teams | single | + |
1819 // | teams | parallel for | * |
1820 // | teams |parallel for simd| * |
1821 // | teams |parallel sections| * |
1822 // | teams | task | + |
1823 // | teams | taskyield | + |
1824 // | teams | barrier | + |
1825 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00001826 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001827 // | teams | flush | + |
1828 // | teams | ordered | + |
1829 // | teams | atomic | + |
1830 // | teams | target | + |
1831 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001832 // | teams | cancellation | |
1833 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001834 // | teams | cancel | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001835 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00001836 if (Stack->getCurScope()) {
1837 auto ParentRegion = Stack->getParentDirective();
1838 bool NestingProhibited = false;
1839 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001840 enum {
1841 NoRecommend,
1842 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00001843 ShouldBeInOrderedRegion,
1844 ShouldBeInTargetRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001845 } Recommend = NoRecommend;
Alexey Bataev549210e2014-06-24 04:39:47 +00001846 if (isOpenMPSimdDirective(ParentRegion)) {
1847 // OpenMP [2.16, Nesting of Regions]
1848 // OpenMP constructs may not be nested inside a simd region.
1849 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
1850 return true;
1851 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001852 if (ParentRegion == OMPD_atomic) {
1853 // OpenMP [2.16, Nesting of Regions]
1854 // OpenMP constructs may not be nested inside an atomic region.
1855 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
1856 return true;
1857 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001858 if (CurrentRegion == OMPD_section) {
1859 // OpenMP [2.7.2, sections Construct, Restrictions]
1860 // Orphaned section directives are prohibited. That is, the section
1861 // directives must appear within the sections construct and must not be
1862 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001863 if (ParentRegion != OMPD_sections &&
1864 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001865 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
1866 << (ParentRegion != OMPD_unknown)
1867 << getOpenMPDirectiveName(ParentRegion);
1868 return true;
1869 }
1870 return false;
1871 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001872 // Allow some constructs to be orphaned (they could be used in functions,
1873 // called from OpenMP regions with the required preconditions).
1874 if (ParentRegion == OMPD_unknown)
1875 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00001876 if (CurrentRegion == OMPD_cancellation_point ||
1877 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001878 // OpenMP [2.16, Nesting of Regions]
1879 // A cancellation point construct for which construct-type-clause is
1880 // taskgroup must be nested inside a task construct. A cancellation
1881 // point construct for which construct-type-clause is not taskgroup must
1882 // be closely nested inside an OpenMP construct that matches the type
1883 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00001884 // A cancel construct for which construct-type-clause is taskgroup must be
1885 // nested inside a task construct. A cancel construct for which
1886 // construct-type-clause is not taskgroup must be closely nested inside an
1887 // OpenMP construct that matches the type specified in
1888 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001889 NestingProhibited =
1890 !((CancelRegion == OMPD_parallel && ParentRegion == OMPD_parallel) ||
1891 (CancelRegion == OMPD_for && ParentRegion == OMPD_for) ||
1892 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
1893 (CancelRegion == OMPD_sections &&
1894 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections)));
1895 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00001896 // OpenMP [2.16, Nesting of Regions]
1897 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00001898 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00001899 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
1900 ParentRegion == OMPD_task;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001901 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
1902 // OpenMP [2.16, Nesting of Regions]
1903 // A critical region may not be nested (closely or otherwise) inside a
1904 // critical region with the same name. Note that this restriction is not
1905 // sufficient to prevent deadlock.
1906 SourceLocation PreviousCriticalLoc;
1907 bool DeadLock =
1908 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
1909 OpenMPDirectiveKind K,
1910 const DeclarationNameInfo &DNI,
1911 SourceLocation Loc)
1912 ->bool {
1913 if (K == OMPD_critical &&
1914 DNI.getName() == CurrentName.getName()) {
1915 PreviousCriticalLoc = Loc;
1916 return true;
1917 } else
1918 return false;
1919 },
1920 false /* skip top directive */);
1921 if (DeadLock) {
1922 SemaRef.Diag(StartLoc,
1923 diag::err_omp_prohibited_region_critical_same_name)
1924 << CurrentName.getName();
1925 if (PreviousCriticalLoc.isValid())
1926 SemaRef.Diag(PreviousCriticalLoc,
1927 diag::note_omp_previous_critical_region);
1928 return true;
1929 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001930 } else if (CurrentRegion == OMPD_barrier) {
1931 // OpenMP [2.16, Nesting of Regions]
1932 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00001933 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001934 NestingProhibited =
1935 isOpenMPWorksharingDirective(ParentRegion) ||
1936 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1937 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00001938 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00001939 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00001940 // OpenMP [2.16, Nesting of Regions]
1941 // A worksharing region may not be closely nested inside a worksharing,
1942 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001943 NestingProhibited =
Alexander Musmanf82886e2014-09-18 05:12:34 +00001944 isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001945 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1946 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
1947 Recommend = ShouldBeInParallelRegion;
1948 } else if (CurrentRegion == OMPD_ordered) {
1949 // OpenMP [2.16, Nesting of Regions]
1950 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00001951 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001952 // An ordered region must be closely nested inside a loop region (or
1953 // parallel loop region) with an ordered clause.
1954 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00001955 ParentRegion == OMPD_task ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001956 !Stack->isParentOrderedRegion();
1957 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00001958 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
1959 // OpenMP [2.16, Nesting of Regions]
1960 // If specified, a teams construct must be contained within a target
1961 // construct.
1962 NestingProhibited = ParentRegion != OMPD_target;
1963 Recommend = ShouldBeInTargetRegion;
1964 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
1965 }
1966 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
1967 // OpenMP [2.16, Nesting of Regions]
1968 // distribute, parallel, parallel sections, parallel workshare, and the
1969 // parallel loop and parallel loop SIMD constructs are the only OpenMP
1970 // constructs that can be closely nested in the teams region.
1971 // TODO: add distribute directive.
1972 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion);
1973 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00001974 }
1975 if (NestingProhibited) {
1976 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001977 << CloseNesting << getOpenMPDirectiveName(ParentRegion) << Recommend
1978 << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00001979 return true;
1980 }
1981 }
1982 return false;
1983}
1984
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001985StmtResult Sema::ActOnOpenMPExecutableDirective(
1986 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
1987 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
1988 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001989 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001990 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
1991 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00001992 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001993
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001994 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev68446b72014-07-18 07:47:19 +00001995 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001996 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00001997 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00001998 if (AStmt) {
1999 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
2000
2001 // Check default data sharing attributes for referenced variables.
2002 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
2003 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
2004 if (DSAChecker.isErrorFound())
2005 return StmtError();
2006 // Generate list of implicitly defined firstprivate variables.
2007 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00002008
2009 if (!DSAChecker.getImplicitFirstprivate().empty()) {
2010 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
2011 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
2012 SourceLocation(), SourceLocation())) {
2013 ClausesWithImplicit.push_back(Implicit);
2014 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
2015 DSAChecker.getImplicitFirstprivate().size();
2016 } else
2017 ErrorFound = true;
2018 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002019 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002020
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002021 switch (Kind) {
2022 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00002023 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
2024 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002025 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002026 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002027 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2028 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002029 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002030 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002031 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2032 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002033 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00002034 case OMPD_for_simd:
2035 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2036 EndLoc, VarsWithInheritedDSA);
2037 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002038 case OMPD_sections:
2039 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
2040 EndLoc);
2041 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002042 case OMPD_section:
2043 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00002044 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002045 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
2046 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002047 case OMPD_single:
2048 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
2049 EndLoc);
2050 break;
Alexander Musman80c22892014-07-17 08:54:58 +00002051 case OMPD_master:
2052 assert(ClausesWithImplicit.empty() &&
2053 "No clauses are allowed for 'omp master' directive");
2054 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
2055 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002056 case OMPD_critical:
2057 assert(ClausesWithImplicit.empty() &&
2058 "No clauses are allowed for 'omp critical' directive");
2059 Res = ActOnOpenMPCriticalDirective(DirName, AStmt, StartLoc, EndLoc);
2060 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002061 case OMPD_parallel_for:
2062 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
2063 EndLoc, VarsWithInheritedDSA);
2064 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00002065 case OMPD_parallel_for_simd:
2066 Res = ActOnOpenMPParallelForSimdDirective(
2067 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
2068 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002069 case OMPD_parallel_sections:
2070 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
2071 StartLoc, EndLoc);
2072 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002073 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002074 Res =
2075 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
2076 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00002077 case OMPD_taskyield:
2078 assert(ClausesWithImplicit.empty() &&
2079 "No clauses are allowed for 'omp taskyield' directive");
2080 assert(AStmt == nullptr &&
2081 "No associated statement allowed for 'omp taskyield' directive");
2082 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
2083 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002084 case OMPD_barrier:
2085 assert(ClausesWithImplicit.empty() &&
2086 "No clauses are allowed for 'omp barrier' directive");
2087 assert(AStmt == nullptr &&
2088 "No associated statement allowed for 'omp barrier' directive");
2089 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
2090 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00002091 case OMPD_taskwait:
2092 assert(ClausesWithImplicit.empty() &&
2093 "No clauses are allowed for 'omp taskwait' directive");
2094 assert(AStmt == nullptr &&
2095 "No associated statement allowed for 'omp taskwait' directive");
2096 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
2097 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002098 case OMPD_taskgroup:
2099 assert(ClausesWithImplicit.empty() &&
2100 "No clauses are allowed for 'omp taskgroup' directive");
2101 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
2102 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00002103 case OMPD_flush:
2104 assert(AStmt == nullptr &&
2105 "No associated statement allowed for 'omp flush' directive");
2106 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
2107 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002108 case OMPD_ordered:
2109 assert(ClausesWithImplicit.empty() &&
2110 "No clauses are allowed for 'omp ordered' directive");
2111 Res = ActOnOpenMPOrderedDirective(AStmt, StartLoc, EndLoc);
2112 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00002113 case OMPD_atomic:
2114 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
2115 EndLoc);
2116 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002117 case OMPD_teams:
2118 Res =
2119 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
2120 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002121 case OMPD_target:
2122 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
2123 EndLoc);
2124 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002125 case OMPD_cancellation_point:
2126 assert(ClausesWithImplicit.empty() &&
2127 "No clauses are allowed for 'omp cancellation point' directive");
2128 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
2129 "cancellation point' directive");
2130 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
2131 break;
Alexey Bataev80909872015-07-02 11:25:17 +00002132 case OMPD_cancel:
2133 assert(ClausesWithImplicit.empty() &&
2134 "No clauses are allowed for 'omp cancel' directive");
2135 assert(AStmt == nullptr &&
2136 "No associated statement allowed for 'omp cancel' directive");
2137 Res = ActOnOpenMPCancelDirective(StartLoc, EndLoc, CancelRegion);
2138 break;
Michael Wong65f367f2015-07-21 13:44:28 +00002139 case OMPD_target_data:
2140 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
2141 EndLoc);
2142 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002143 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002144 llvm_unreachable("OpenMP Directive is not allowed");
2145 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002146 llvm_unreachable("Unknown OpenMP directive");
2147 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002148
Alexey Bataev4acb8592014-07-07 13:01:15 +00002149 for (auto P : VarsWithInheritedDSA) {
2150 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
2151 << P.first << P.second->getSourceRange();
2152 }
2153 if (!VarsWithInheritedDSA.empty())
2154 return StmtError();
2155
Alexey Bataeved09d242014-05-28 05:53:51 +00002156 if (ErrorFound)
2157 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002158 return Res;
2159}
2160
2161StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
2162 Stmt *AStmt,
2163 SourceLocation StartLoc,
2164 SourceLocation EndLoc) {
Alexey Bataev9959db52014-05-06 10:08:46 +00002165 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2166 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
2167 // 1.2.2 OpenMP Language Terminology
2168 // Structured block - An executable statement with a single entry at the
2169 // top and a single exit at the bottom.
2170 // The point of exit cannot be a branch out of the structured block.
2171 // longjmp() and throw() must not violate the entry/exit criteria.
2172 CS->getCapturedDecl()->setNothrow();
2173
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002174 getCurFunction()->setHasBranchProtectedScope();
2175
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00002176 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
2177 AStmt);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002178}
2179
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002180namespace {
2181/// \brief Helper class for checking canonical form of the OpenMP loops and
2182/// extracting iteration space of each loop in the loop nest, that will be used
2183/// for IR generation.
2184class OpenMPIterationSpaceChecker {
2185 /// \brief Reference to Sema.
2186 Sema &SemaRef;
2187 /// \brief A location for diagnostics (when there is no some better location).
2188 SourceLocation DefaultLoc;
2189 /// \brief A location for diagnostics (when increment is not compatible).
2190 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002191 /// \brief A source location for referring to loop init later.
2192 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002193 /// \brief A source location for referring to condition later.
2194 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002195 /// \brief A source location for referring to increment later.
2196 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002197 /// \brief Loop variable.
2198 VarDecl *Var;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002199 /// \brief Reference to loop variable.
2200 DeclRefExpr *VarRef;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002201 /// \brief Lower bound (initializer for the var).
2202 Expr *LB;
2203 /// \brief Upper bound.
2204 Expr *UB;
2205 /// \brief Loop step (increment).
2206 Expr *Step;
2207 /// \brief This flag is true when condition is one of:
2208 /// Var < UB
2209 /// Var <= UB
2210 /// UB > Var
2211 /// UB >= Var
2212 bool TestIsLessOp;
2213 /// \brief This flag is true when condition is strict ( < or > ).
2214 bool TestIsStrictOp;
2215 /// \brief This flag is true when step is subtracted on each iteration.
2216 bool SubtractStep;
2217
2218public:
2219 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
2220 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
Alexander Musmana5f070a2014-10-01 06:03:56 +00002221 InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()),
2222 IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002223 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
2224 TestIsStrictOp(false), SubtractStep(false) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002225 /// \brief Check init-expr for canonical loop form and save loop counter
2226 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00002227 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002228 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
2229 /// for less/greater and for strict/non-strict comparison.
2230 bool CheckCond(Expr *S);
2231 /// \brief Check incr-expr for canonical loop form and return true if it
2232 /// does not conform, otherwise save loop step (#Step).
2233 bool CheckInc(Expr *S);
2234 /// \brief Return the loop counter variable.
2235 VarDecl *GetLoopVar() const { return Var; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002236 /// \brief Return the reference expression to loop counter variable.
2237 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002238 /// \brief Source range of the loop init.
2239 SourceRange GetInitSrcRange() const { return InitSrcRange; }
2240 /// \brief Source range of the loop condition.
2241 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
2242 /// \brief Source range of the loop increment.
2243 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
2244 /// \brief True if the step should be subtracted.
2245 bool ShouldSubtractStep() const { return SubtractStep; }
2246 /// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00002247 Expr *BuildNumIterations(Scope *S, const bool LimitedType) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00002248 /// \brief Build the precondition expression for the loops.
2249 Expr *BuildPreCond(Scope *S, Expr *Cond) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002250 /// \brief Build reference expression to the counter be used for codegen.
2251 Expr *BuildCounterVar() const;
2252 /// \brief Build initization of the counter be used for codegen.
2253 Expr *BuildCounterInit() const;
2254 /// \brief Build step of the counter be used for codegen.
2255 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002256 /// \brief Return true if any expression is dependent.
2257 bool Dependent() const;
2258
2259private:
2260 /// \brief Check the right-hand side of an assignment in the increment
2261 /// expression.
2262 bool CheckIncRHS(Expr *RHS);
2263 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002264 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002265 /// \brief Helper to set upper bound.
2266 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, const SourceRange &SR,
2267 const SourceLocation &SL);
2268 /// \brief Helper to set loop increment.
2269 bool SetStep(Expr *NewStep, bool Subtract);
2270};
2271
2272bool OpenMPIterationSpaceChecker::Dependent() const {
2273 if (!Var) {
2274 assert(!LB && !UB && !Step);
2275 return false;
2276 }
2277 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
2278 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
2279}
2280
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002281template <typename T>
2282static T *getExprAsWritten(T *E) {
2283 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
2284 E = ExprTemp->getSubExpr();
2285
2286 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
2287 E = MTE->GetTemporaryExpr();
2288
2289 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
2290 E = Binder->getSubExpr();
2291
2292 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
2293 E = ICE->getSubExprAsWritten();
2294 return E->IgnoreParens();
2295}
2296
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002297bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
2298 DeclRefExpr *NewVarRefExpr,
2299 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002300 // State consistency checking to ensure correct usage.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002301 assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
2302 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002303 if (!NewVar || !NewLB)
2304 return true;
2305 Var = NewVar;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002306 VarRef = NewVarRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002307 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
2308 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00002309 if ((Ctor->isCopyOrMoveConstructor() ||
2310 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
2311 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002312 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002313 LB = NewLB;
2314 return false;
2315}
2316
2317bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
2318 const SourceRange &SR,
2319 const SourceLocation &SL) {
2320 // State consistency checking to ensure correct usage.
2321 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
2322 !TestIsLessOp && !TestIsStrictOp);
2323 if (!NewUB)
2324 return true;
2325 UB = NewUB;
2326 TestIsLessOp = LessOp;
2327 TestIsStrictOp = StrictOp;
2328 ConditionSrcRange = SR;
2329 ConditionLoc = SL;
2330 return false;
2331}
2332
2333bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
2334 // State consistency checking to ensure correct usage.
2335 assert(Var != nullptr && LB != nullptr && Step == nullptr);
2336 if (!NewStep)
2337 return true;
2338 if (!NewStep->isValueDependent()) {
2339 // Check that the step is integer expression.
2340 SourceLocation StepLoc = NewStep->getLocStart();
2341 ExprResult Val =
2342 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
2343 if (Val.isInvalid())
2344 return true;
2345 NewStep = Val.get();
2346
2347 // OpenMP [2.6, Canonical Loop Form, Restrictions]
2348 // If test-expr is of form var relational-op b and relational-op is < or
2349 // <= then incr-expr must cause var to increase on each iteration of the
2350 // loop. If test-expr is of form var relational-op b and relational-op is
2351 // > or >= then incr-expr must cause var to decrease on each iteration of
2352 // the loop.
2353 // If test-expr is of form b relational-op var and relational-op is < or
2354 // <= then incr-expr must cause var to decrease on each iteration of the
2355 // loop. If test-expr is of form b relational-op var and relational-op is
2356 // > or >= then incr-expr must cause var to increase on each iteration of
2357 // the loop.
2358 llvm::APSInt Result;
2359 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
2360 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
2361 bool IsConstNeg =
2362 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00002363 bool IsConstPos =
2364 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002365 bool IsConstZero = IsConstant && !Result.getBoolValue();
2366 if (UB && (IsConstZero ||
2367 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00002368 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002369 SemaRef.Diag(NewStep->getExprLoc(),
2370 diag::err_omp_loop_incr_not_compatible)
2371 << Var << TestIsLessOp << NewStep->getSourceRange();
2372 SemaRef.Diag(ConditionLoc,
2373 diag::note_omp_loop_cond_requres_compatible_incr)
2374 << TestIsLessOp << ConditionSrcRange;
2375 return true;
2376 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002377 if (TestIsLessOp == Subtract) {
2378 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
2379 NewStep).get();
2380 Subtract = !Subtract;
2381 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002382 }
2383
2384 Step = NewStep;
2385 SubtractStep = Subtract;
2386 return false;
2387}
2388
Alexey Bataev9c821032015-04-30 04:23:23 +00002389bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002390 // Check init-expr for canonical loop form and save loop counter
2391 // variable - #Var and its initialization value - #LB.
2392 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
2393 // var = lb
2394 // integer-type var = lb
2395 // random-access-iterator-type var = lb
2396 // pointer-type var = lb
2397 //
2398 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00002399 if (EmitDiags) {
2400 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
2401 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002402 return true;
2403 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002404 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002405 if (Expr *E = dyn_cast<Expr>(S))
2406 S = E->IgnoreParens();
2407 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2408 if (BO->getOpcode() == BO_Assign)
2409 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002410 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002411 BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002412 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
2413 if (DS->isSingleDecl()) {
2414 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
2415 if (Var->hasInit()) {
2416 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00002417 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002418 SemaRef.Diag(S->getLocStart(),
2419 diag::ext_omp_loop_not_canonical_init)
2420 << S->getSourceRange();
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002421 return SetVarAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002422 }
2423 }
2424 }
2425 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
2426 if (CE->getOperator() == OO_Equal)
2427 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002428 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
2429 CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002430
Alexey Bataev9c821032015-04-30 04:23:23 +00002431 if (EmitDiags) {
2432 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
2433 << S->getSourceRange();
2434 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002435 return true;
2436}
2437
Alexey Bataev23b69422014-06-18 07:08:49 +00002438/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002439/// variable (which may be the loop variable) if possible.
2440static const VarDecl *GetInitVarDecl(const Expr *E) {
2441 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00002442 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002443 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002444 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
2445 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00002446 if ((Ctor->isCopyOrMoveConstructor() ||
2447 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
2448 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002449 E = CE->getArg(0)->IgnoreParenImpCasts();
2450 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
2451 if (!DRE)
2452 return nullptr;
2453 return dyn_cast<VarDecl>(DRE->getDecl());
2454}
2455
2456bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
2457 // Check test-expr for canonical form, save upper-bound UB, flags for
2458 // less/greater and for strict/non-strict comparison.
2459 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2460 // var relational-op b
2461 // b relational-op var
2462 //
2463 if (!S) {
2464 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
2465 return true;
2466 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002467 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002468 SourceLocation CondLoc = S->getLocStart();
2469 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2470 if (BO->isRelationalOp()) {
2471 if (GetInitVarDecl(BO->getLHS()) == Var)
2472 return SetUB(BO->getRHS(),
2473 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
2474 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2475 BO->getSourceRange(), BO->getOperatorLoc());
2476 if (GetInitVarDecl(BO->getRHS()) == Var)
2477 return SetUB(BO->getLHS(),
2478 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
2479 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2480 BO->getSourceRange(), BO->getOperatorLoc());
2481 }
2482 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2483 if (CE->getNumArgs() == 2) {
2484 auto Op = CE->getOperator();
2485 switch (Op) {
2486 case OO_Greater:
2487 case OO_GreaterEqual:
2488 case OO_Less:
2489 case OO_LessEqual:
2490 if (GetInitVarDecl(CE->getArg(0)) == Var)
2491 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
2492 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2493 CE->getOperatorLoc());
2494 if (GetInitVarDecl(CE->getArg(1)) == Var)
2495 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
2496 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2497 CE->getOperatorLoc());
2498 break;
2499 default:
2500 break;
2501 }
2502 }
2503 }
2504 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
2505 << S->getSourceRange() << Var;
2506 return true;
2507}
2508
2509bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
2510 // RHS of canonical loop form increment can be:
2511 // var + incr
2512 // incr + var
2513 // var - incr
2514 //
2515 RHS = RHS->IgnoreParenImpCasts();
2516 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
2517 if (BO->isAdditiveOp()) {
2518 bool IsAdd = BO->getOpcode() == BO_Add;
2519 if (GetInitVarDecl(BO->getLHS()) == Var)
2520 return SetStep(BO->getRHS(), !IsAdd);
2521 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
2522 return SetStep(BO->getLHS(), false);
2523 }
2524 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
2525 bool IsAdd = CE->getOperator() == OO_Plus;
2526 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
2527 if (GetInitVarDecl(CE->getArg(0)) == Var)
2528 return SetStep(CE->getArg(1), !IsAdd);
2529 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
2530 return SetStep(CE->getArg(0), false);
2531 }
2532 }
2533 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2534 << RHS->getSourceRange() << Var;
2535 return true;
2536}
2537
2538bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
2539 // Check incr-expr for canonical loop form and return true if it
2540 // does not conform.
2541 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2542 // ++var
2543 // var++
2544 // --var
2545 // var--
2546 // var += incr
2547 // var -= incr
2548 // var = var + incr
2549 // var = incr + var
2550 // var = var - incr
2551 //
2552 if (!S) {
2553 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
2554 return true;
2555 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002556 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002557 S = S->IgnoreParens();
2558 if (auto UO = dyn_cast<UnaryOperator>(S)) {
2559 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
2560 return SetStep(
2561 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
2562 (UO->isDecrementOp() ? -1 : 1)).get(),
2563 false);
2564 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
2565 switch (BO->getOpcode()) {
2566 case BO_AddAssign:
2567 case BO_SubAssign:
2568 if (GetInitVarDecl(BO->getLHS()) == Var)
2569 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
2570 break;
2571 case BO_Assign:
2572 if (GetInitVarDecl(BO->getLHS()) == Var)
2573 return CheckIncRHS(BO->getRHS());
2574 break;
2575 default:
2576 break;
2577 }
2578 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2579 switch (CE->getOperator()) {
2580 case OO_PlusPlus:
2581 case OO_MinusMinus:
2582 if (GetInitVarDecl(CE->getArg(0)) == Var)
2583 return SetStep(
2584 SemaRef.ActOnIntegerConstant(
2585 CE->getLocStart(),
2586 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
2587 false);
2588 break;
2589 case OO_PlusEqual:
2590 case OO_MinusEqual:
2591 if (GetInitVarDecl(CE->getArg(0)) == Var)
2592 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
2593 break;
2594 case OO_Equal:
2595 if (GetInitVarDecl(CE->getArg(0)) == Var)
2596 return CheckIncRHS(CE->getArg(1));
2597 break;
2598 default:
2599 break;
2600 }
2601 }
2602 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2603 << S->getSourceRange() << Var;
2604 return true;
2605}
Alexander Musmana5f070a2014-10-01 06:03:56 +00002606
2607/// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00002608Expr *
2609OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S,
2610 const bool LimitedType) const {
Alexander Musmana5f070a2014-10-01 06:03:56 +00002611 ExprResult Diff;
2612 if (Var->getType()->isIntegerType() || Var->getType()->isPointerType() ||
2613 SemaRef.getLangOpts().CPlusPlus) {
2614 // Upper - Lower
2615 Expr *Upper = TestIsLessOp ? UB : LB;
2616 Expr *Lower = TestIsLessOp ? LB : UB;
2617
2618 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
2619
2620 if (!Diff.isUsable() && Var->getType()->getAsCXXRecordDecl()) {
2621 // BuildBinOp already emitted error, this one is to point user to upper
2622 // and lower bound, and to tell what is passed to 'operator-'.
2623 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
2624 << Upper->getSourceRange() << Lower->getSourceRange();
2625 return nullptr;
2626 }
2627 }
2628
2629 if (!Diff.isUsable())
2630 return nullptr;
2631
2632 // Upper - Lower [- 1]
2633 if (TestIsStrictOp)
2634 Diff = SemaRef.BuildBinOp(
2635 S, DefaultLoc, BO_Sub, Diff.get(),
2636 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
2637 if (!Diff.isUsable())
2638 return nullptr;
2639
2640 // Upper - Lower [- 1] + Step
2641 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(),
2642 Step->IgnoreImplicit());
2643 if (!Diff.isUsable())
2644 return nullptr;
2645
2646 // Parentheses (for dumping/debugging purposes only).
2647 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
2648 if (!Diff.isUsable())
2649 return nullptr;
2650
2651 // (Upper - Lower [- 1] + Step) / Step
2652 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(),
2653 Step->IgnoreImplicit());
2654 if (!Diff.isUsable())
2655 return nullptr;
2656
Alexander Musman174b3ca2014-10-06 11:16:29 +00002657 // OpenMP runtime requires 32-bit or 64-bit loop variables.
2658 if (LimitedType) {
2659 auto &C = SemaRef.Context;
2660 QualType Type = Diff.get()->getType();
2661 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
2662 if (NewSize != C.getTypeSize(Type)) {
2663 if (NewSize < C.getTypeSize(Type)) {
2664 assert(NewSize == 64 && "incorrect loop var size");
2665 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
2666 << InitSrcRange << ConditionSrcRange;
2667 }
2668 QualType NewType = C.getIntTypeForBitwidth(
2669 NewSize, Type->hasSignedIntegerRepresentation());
2670 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
2671 Sema::AA_Converting, true);
2672 if (!Diff.isUsable())
2673 return nullptr;
2674 }
2675 }
2676
Alexander Musmana5f070a2014-10-01 06:03:56 +00002677 return Diff.get();
2678}
2679
Alexey Bataev62dbb972015-04-22 11:59:37 +00002680Expr *OpenMPIterationSpaceChecker::BuildPreCond(Scope *S, Expr *Cond) const {
2681 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
2682 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
2683 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
2684 auto CondExpr = SemaRef.BuildBinOp(
2685 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
2686 : (TestIsStrictOp ? BO_GT : BO_GE),
2687 LB, UB);
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002688 if (CondExpr.isUsable()) {
2689 CondExpr = SemaRef.PerformImplicitConversion(
2690 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
2691 /*AllowExplicit=*/true);
2692 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00002693 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
2694 // Otherwise use original loop conditon and evaluate it in runtime.
2695 return CondExpr.isUsable() ? CondExpr.get() : Cond;
2696}
2697
Alexander Musmana5f070a2014-10-01 06:03:56 +00002698/// \brief Build reference expression to the counter be used for codegen.
2699Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const {
Alexey Bataev39f915b82015-05-08 10:41:21 +00002700 return buildDeclRefExpr(SemaRef, Var, Var->getType(), DefaultLoc);
Alexander Musmana5f070a2014-10-01 06:03:56 +00002701}
2702
2703/// \brief Build initization of the counter be used for codegen.
2704Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
2705
2706/// \brief Build step of the counter be used for codegen.
2707Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
2708
2709/// \brief Iteration space of a single for loop.
2710struct LoopIterationSpace {
Alexey Bataev62dbb972015-04-22 11:59:37 +00002711 /// \brief Condition of the loop.
2712 Expr *PreCond;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002713 /// \brief This expression calculates the number of iterations in the loop.
2714 /// It is always possible to calculate it before starting the loop.
2715 Expr *NumIterations;
2716 /// \brief The loop counter variable.
2717 Expr *CounterVar;
2718 /// \brief This is initializer for the initial value of #CounterVar.
2719 Expr *CounterInit;
2720 /// \brief This is step for the #CounterVar used to generate its update:
2721 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
2722 Expr *CounterStep;
2723 /// \brief Should step be subtracted?
2724 bool Subtract;
2725 /// \brief Source range of the loop init.
2726 SourceRange InitSrcRange;
2727 /// \brief Source range of the loop condition.
2728 SourceRange CondSrcRange;
2729 /// \brief Source range of the loop increment.
2730 SourceRange IncSrcRange;
2731};
2732
Alexey Bataev23b69422014-06-18 07:08:49 +00002733} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002734
Alexey Bataev9c821032015-04-30 04:23:23 +00002735void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
2736 assert(getLangOpts().OpenMP && "OpenMP is not active.");
2737 assert(Init && "Expected loop in canonical form.");
2738 unsigned CollapseIteration = DSAStack->getCollapseNumber();
2739 if (CollapseIteration > 0 &&
2740 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
2741 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
2742 if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
2743 DSAStack->addLoopControlVariable(ISC.GetLoopVar());
2744 }
2745 DSAStack->setCollapseNumber(CollapseIteration - 1);
2746 }
2747}
2748
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002749/// \brief Called on a for stmt to check and extract its iteration space
2750/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00002751static bool CheckOpenMPIterationSpace(
2752 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
2753 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
2754 Expr *NestedLoopCountExpr,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002755 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
2756 LoopIterationSpace &ResultIterSpace) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002757 // OpenMP [2.6, Canonical Loop Form]
2758 // for (init-expr; test-expr; incr-expr) structured-block
2759 auto For = dyn_cast_or_null<ForStmt>(S);
2760 if (!For) {
2761 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002762 << (NestedLoopCountExpr != nullptr) << getOpenMPDirectiveName(DKind)
2763 << NestedLoopCount << (CurrentNestedLoopCount > 0)
2764 << CurrentNestedLoopCount;
2765 if (NestedLoopCount > 1)
2766 SemaRef.Diag(NestedLoopCountExpr->getExprLoc(),
2767 diag::note_omp_collapse_expr)
2768 << NestedLoopCountExpr->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002769 return true;
2770 }
2771 assert(For->getBody());
2772
2773 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
2774
2775 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002776 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002777 if (ISC.CheckInit(Init)) {
2778 return true;
2779 }
2780
2781 bool HasErrors = false;
2782
2783 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002784 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002785
2786 // OpenMP [2.6, Canonical Loop Form]
2787 // Var is one of the following:
2788 // A variable of signed or unsigned integer type.
2789 // For C++, a variable of a random access iterator type.
2790 // For C, a variable of a pointer type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002791 auto VarType = Var->getType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002792 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
2793 !VarType->isPointerType() &&
2794 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
2795 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
2796 << SemaRef.getLangOpts().CPlusPlus;
2797 HasErrors = true;
2798 }
2799
Alexey Bataev4acb8592014-07-07 13:01:15 +00002800 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
2801 // Construct
2802 // The loop iteration variable(s) in the associated for-loop(s) of a for or
2803 // parallel for construct is (are) private.
2804 // The loop iteration variable in the associated for-loop of a simd construct
2805 // with just one associated for-loop is linear with a constant-linear-step
2806 // that is the increment of the associated for-loop.
2807 // Exclude loop var from the list of variables with implicitly defined data
2808 // sharing attributes.
Benjamin Kramerad8e0792014-10-10 15:32:48 +00002809 VarsWithImplicitDSA.erase(Var);
Alexey Bataev4acb8592014-07-07 13:01:15 +00002810
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002811 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
2812 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00002813 // The loop iteration variable in the associated for-loop of a simd construct
2814 // with just one associated for-loop may be listed in a linear clause with a
2815 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002816 // The loop iteration variable(s) in the associated for-loop(s) of a for or
2817 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002818 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002819 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
2820 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
2821 // declared in the loop and it is predetermined as a private.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002822 auto PredeterminedCKind =
2823 isOpenMPSimdDirective(DKind)
2824 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
2825 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002826 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataev0c024df2015-05-12 09:02:07 +00002827 DVar.CKind != OMPC_threadprivate && DVar.CKind != PredeterminedCKind) ||
Alexander Musmanf82886e2014-09-18 05:12:34 +00002828 (isOpenMPWorksharingDirective(DKind) && !isOpenMPSimdDirective(DKind) &&
2829 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private &&
Alexey Bataev0c024df2015-05-12 09:02:07 +00002830 DVar.CKind != OMPC_lastprivate && DVar.CKind != OMPC_threadprivate)) &&
2831 ((DVar.CKind != OMPC_private && DVar.CKind != OMPC_threadprivate) ||
2832 DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002833 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00002834 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
2835 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataevf2453a02015-05-06 07:25:08 +00002836 if (DVar.RefExpr == nullptr)
2837 DVar.CKind = PredeterminedCKind;
2838 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, /*IsLoopIterVar=*/true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002839 HasErrors = true;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002840 } else if (LoopVarRefExpr != nullptr) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00002841 // Make the loop iteration variable private (for worksharing constructs),
2842 // linear (for simd directives with the only one associated loop) or
2843 // lastprivate (for simd directives with several collapsed loops).
Alexey Bataev9aba41c2014-11-14 04:08:45 +00002844 if (DVar.CKind == OMPC_unknown)
2845 DVar = DSA.hasDSA(Var, isOpenMPPrivate, MatchesAlways(),
2846 /*FromParent=*/false);
Alexey Bataev9c821032015-04-30 04:23:23 +00002847 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002848 }
2849
Alexey Bataev7ff55242014-06-19 09:13:45 +00002850 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00002851
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002852 // Check test-expr.
2853 HasErrors |= ISC.CheckCond(For->getCond());
2854
2855 // Check incr-expr.
2856 HasErrors |= ISC.CheckInc(For->getInc());
2857
Alexander Musmana5f070a2014-10-01 06:03:56 +00002858 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002859 return HasErrors;
2860
Alexander Musmana5f070a2014-10-01 06:03:56 +00002861 // Build the loop's iteration space representation.
Alexey Bataev62dbb972015-04-22 11:59:37 +00002862 ResultIterSpace.PreCond = ISC.BuildPreCond(DSA.getCurScope(), For->getCond());
Alexander Musman174b3ca2014-10-06 11:16:29 +00002863 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
2864 DSA.getCurScope(), /* LimitedType */ isOpenMPWorksharingDirective(DKind));
Alexander Musmana5f070a2014-10-01 06:03:56 +00002865 ResultIterSpace.CounterVar = ISC.BuildCounterVar();
2866 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
2867 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
2868 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
2869 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
2870 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
2871 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
2872
Alexey Bataev62dbb972015-04-22 11:59:37 +00002873 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
2874 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00002875 ResultIterSpace.CounterVar == nullptr ||
2876 ResultIterSpace.CounterInit == nullptr ||
2877 ResultIterSpace.CounterStep == nullptr);
2878
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002879 return HasErrors;
2880}
2881
Alexander Musmana5f070a2014-10-01 06:03:56 +00002882/// \brief Build 'VarRef = Start + Iter * Step'.
2883static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S,
2884 SourceLocation Loc, ExprResult VarRef,
2885 ExprResult Start, ExprResult Iter,
2886 ExprResult Step, bool Subtract) {
2887 // Add parentheses (for debugging purposes only).
2888 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
2889 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
2890 !Step.isUsable())
2891 return ExprError();
2892
2893 ExprResult Update = SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(),
2894 Step.get()->IgnoreImplicit());
2895 if (!Update.isUsable())
2896 return ExprError();
2897
2898 // Build 'VarRef = Start + Iter * Step'.
2899 Update = SemaRef.BuildBinOp(S, Loc, (Subtract ? BO_Sub : BO_Add),
2900 Start.get()->IgnoreImplicit(), Update.get());
2901 if (!Update.isUsable())
2902 return ExprError();
2903
2904 Update = SemaRef.PerformImplicitConversion(
2905 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
2906 if (!Update.isUsable())
2907 return ExprError();
2908
2909 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
2910 return Update;
2911}
2912
2913/// \brief Convert integer expression \a E to make it have at least \a Bits
2914/// bits.
2915static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
2916 Sema &SemaRef) {
2917 if (E == nullptr)
2918 return ExprError();
2919 auto &C = SemaRef.Context;
2920 QualType OldType = E->getType();
2921 unsigned HasBits = C.getTypeSize(OldType);
2922 if (HasBits >= Bits)
2923 return ExprResult(E);
2924 // OK to convert to signed, because new type has more bits than old.
2925 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
2926 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
2927 true);
2928}
2929
2930/// \brief Check if the given expression \a E is a constant integer that fits
2931/// into \a Bits bits.
2932static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
2933 if (E == nullptr)
2934 return false;
2935 llvm::APSInt Result;
2936 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
2937 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
2938 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002939}
2940
2941/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00002942/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
2943/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002944static unsigned
2945CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *NestedLoopCountExpr,
2946 Stmt *AStmt, Sema &SemaRef, DSAStackTy &DSA,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002947 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00002948 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002949 unsigned NestedLoopCount = 1;
2950 if (NestedLoopCountExpr) {
2951 // Found 'collapse' clause - calculate collapse number.
2952 llvm::APSInt Result;
2953 if (NestedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
2954 NestedLoopCount = Result.getLimitedValue();
2955 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002956 // This is helper routine for loop directives (e.g., 'for', 'simd',
2957 // 'for simd', etc.).
Alexander Musmana5f070a2014-10-01 06:03:56 +00002958 SmallVector<LoopIterationSpace, 4> IterSpaces;
2959 IterSpaces.resize(NestedLoopCount);
2960 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002961 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002962 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev4acb8592014-07-07 13:01:15 +00002963 NestedLoopCount, NestedLoopCountExpr,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002964 VarsWithImplicitDSA, IterSpaces[Cnt]))
Alexey Bataevabfc0692014-06-25 06:52:00 +00002965 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002966 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00002967 // OpenMP [2.8.1, simd construct, Restrictions]
2968 // All loops associated with the construct must be perfectly nested; that
2969 // is, there must be no intervening code nor any OpenMP directive between
2970 // any two loops.
2971 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002972 }
2973
Alexander Musmana5f070a2014-10-01 06:03:56 +00002974 Built.clear(/* size */ NestedLoopCount);
2975
2976 if (SemaRef.CurContext->isDependentContext())
2977 return NestedLoopCount;
2978
2979 // An example of what is generated for the following code:
2980 //
2981 // #pragma omp simd collapse(2)
2982 // for (i = 0; i < NI; ++i)
2983 // for (j = J0; j < NJ; j+=2) {
2984 // <loop body>
2985 // }
2986 //
2987 // We generate the code below.
2988 // Note: the loop body may be outlined in CodeGen.
2989 // Note: some counters may be C++ classes, operator- is used to find number of
2990 // iterations and operator+= to calculate counter value.
2991 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
2992 // or i64 is currently supported).
2993 //
2994 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
2995 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
2996 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
2997 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
2998 // // similar updates for vars in clauses (e.g. 'linear')
2999 // <loop body (using local i and j)>
3000 // }
3001 // i = NI; // assign final values of counters
3002 // j = NJ;
3003 //
3004
3005 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
3006 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00003007 // Precondition tests if there is at least one iteration (all conditions are
3008 // true).
3009 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003010 auto N0 = IterSpaces[0].NumIterations;
3011 ExprResult LastIteration32 = WidenIterationCount(32 /* Bits */, N0, SemaRef);
3012 ExprResult LastIteration64 = WidenIterationCount(64 /* Bits */, N0, SemaRef);
3013
3014 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
3015 return NestedLoopCount;
3016
3017 auto &C = SemaRef.Context;
3018 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
3019
3020 Scope *CurScope = DSA.getCurScope();
3021 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003022 if (PreCond.isUsable()) {
3023 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
3024 PreCond.get(), IterSpaces[Cnt].PreCond);
3025 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003026 auto N = IterSpaces[Cnt].NumIterations;
3027 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
3028 if (LastIteration32.isUsable())
3029 LastIteration32 = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_Mul,
3030 LastIteration32.get(), N);
3031 if (LastIteration64.isUsable())
3032 LastIteration64 = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_Mul,
3033 LastIteration64.get(), N);
3034 }
3035
3036 // Choose either the 32-bit or 64-bit version.
3037 ExprResult LastIteration = LastIteration64;
3038 if (LastIteration32.isUsable() &&
3039 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
3040 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
3041 FitsInto(
3042 32 /* Bits */,
3043 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
3044 LastIteration64.get(), SemaRef)))
3045 LastIteration = LastIteration32;
3046
3047 if (!LastIteration.isUsable())
3048 return 0;
3049
3050 // Save the number of iterations.
3051 ExprResult NumIterations = LastIteration;
3052 {
3053 LastIteration = SemaRef.BuildBinOp(
3054 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
3055 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3056 if (!LastIteration.isUsable())
3057 return 0;
3058 }
3059
3060 // Calculate the last iteration number beforehand instead of doing this on
3061 // each iteration. Do not do this if the number of iterations may be kfold-ed.
3062 llvm::APSInt Result;
3063 bool IsConstant =
3064 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
3065 ExprResult CalcLastIteration;
3066 if (!IsConstant) {
3067 SourceLocation SaveLoc;
3068 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00003069 buildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00003070 ".omp.last.iteration");
Alexey Bataev39f915b82015-05-08 10:41:21 +00003071 ExprResult SaveRef = buildDeclRefExpr(
3072 SemaRef, SaveVar, LastIteration.get()->getType(), SaveLoc);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003073 CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign,
3074 SaveRef.get(), LastIteration.get());
3075 LastIteration = SaveRef;
3076
3077 // Prepare SaveRef + 1.
3078 NumIterations = SemaRef.BuildBinOp(
3079 CurScope, SaveLoc, BO_Add, SaveRef.get(),
3080 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3081 if (!NumIterations.isUsable())
3082 return 0;
3083 }
3084
3085 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
3086
Alexander Musmanc6388682014-12-15 07:07:06 +00003087 QualType VType = LastIteration.get()->getType();
3088 // Build variables passed into runtime, nesessary for worksharing directives.
3089 ExprResult LB, UB, IL, ST, EUB;
3090 if (isOpenMPWorksharingDirective(DKind)) {
3091 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00003092 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
3093 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003094 SemaRef.AddInitializerToDecl(
3095 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
3096 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3097
3098 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00003099 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
3100 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003101 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
3102 /*DirectInit*/ false,
3103 /*TypeMayContainAuto*/ false);
3104
3105 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
3106 // This will be used to implement clause 'lastprivate'.
3107 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00003108 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
3109 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003110 SemaRef.AddInitializerToDecl(
3111 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
3112 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3113
3114 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev39f915b82015-05-08 10:41:21 +00003115 VarDecl *STDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.stride");
3116 ST = buildDeclRefExpr(SemaRef, STDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003117 SemaRef.AddInitializerToDecl(
3118 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
3119 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3120
3121 // Build expression: UB = min(UB, LastIteration)
3122 // It is nesessary for CodeGen of directives with static scheduling.
3123 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
3124 UB.get(), LastIteration.get());
3125 ExprResult CondOp = SemaRef.ActOnConditionalOp(
3126 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
3127 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
3128 CondOp.get());
3129 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
3130 }
3131
3132 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003133 ExprResult IV;
3134 ExprResult Init;
3135 {
Alexey Bataev39f915b82015-05-08 10:41:21 +00003136 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.iv");
3137 IV = buildDeclRefExpr(SemaRef, IVDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003138 Expr *RHS = isOpenMPWorksharingDirective(DKind)
3139 ? LB.get()
3140 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
3141 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
3142 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003143 }
3144
Alexander Musmanc6388682014-12-15 07:07:06 +00003145 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003146 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00003147 ExprResult Cond =
3148 isOpenMPWorksharingDirective(DKind)
3149 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
3150 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
3151 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003152
3153 // Loop increment (IV = IV + 1)
3154 SourceLocation IncLoc;
3155 ExprResult Inc =
3156 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
3157 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
3158 if (!Inc.isUsable())
3159 return 0;
3160 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00003161 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
3162 if (!Inc.isUsable())
3163 return 0;
3164
3165 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
3166 // Used for directives with static scheduling.
3167 ExprResult NextLB, NextUB;
3168 if (isOpenMPWorksharingDirective(DKind)) {
3169 // LB + ST
3170 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
3171 if (!NextLB.isUsable())
3172 return 0;
3173 // LB = LB + ST
3174 NextLB =
3175 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
3176 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
3177 if (!NextLB.isUsable())
3178 return 0;
3179 // UB + ST
3180 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
3181 if (!NextUB.isUsable())
3182 return 0;
3183 // UB = UB + ST
3184 NextUB =
3185 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
3186 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
3187 if (!NextUB.isUsable())
3188 return 0;
3189 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003190
3191 // Build updates and final values of the loop counters.
3192 bool HasErrors = false;
3193 Built.Counters.resize(NestedLoopCount);
3194 Built.Updates.resize(NestedLoopCount);
3195 Built.Finals.resize(NestedLoopCount);
3196 {
3197 ExprResult Div;
3198 // Go from inner nested loop to outer.
3199 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
3200 LoopIterationSpace &IS = IterSpaces[Cnt];
3201 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
3202 // Build: Iter = (IV / Div) % IS.NumIters
3203 // where Div is product of previous iterations' IS.NumIters.
3204 ExprResult Iter;
3205 if (Div.isUsable()) {
3206 Iter =
3207 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
3208 } else {
3209 Iter = IV;
3210 assert((Cnt == (int)NestedLoopCount - 1) &&
3211 "unusable div expected on first iteration only");
3212 }
3213
3214 if (Cnt != 0 && Iter.isUsable())
3215 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
3216 IS.NumIterations);
3217 if (!Iter.isUsable()) {
3218 HasErrors = true;
3219 break;
3220 }
3221
Alexey Bataev39f915b82015-05-08 10:41:21 +00003222 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
3223 auto *CounterVar = buildDeclRefExpr(
3224 SemaRef, cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()),
3225 IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
3226 /*RefersToCapture=*/true);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003227 ExprResult Update =
Alexey Bataev39f915b82015-05-08 10:41:21 +00003228 BuildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003229 IS.CounterInit, Iter, IS.CounterStep, IS.Subtract);
3230 if (!Update.isUsable()) {
3231 HasErrors = true;
3232 break;
3233 }
3234
3235 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
3236 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00003237 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003238 IS.NumIterations, IS.CounterStep, IS.Subtract);
3239 if (!Final.isUsable()) {
3240 HasErrors = true;
3241 break;
3242 }
3243
3244 // Build Div for the next iteration: Div <- Div * IS.NumIters
3245 if (Cnt != 0) {
3246 if (Div.isUnset())
3247 Div = IS.NumIterations;
3248 else
3249 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
3250 IS.NumIterations);
3251
3252 // Add parentheses (for debugging purposes only).
3253 if (Div.isUsable())
3254 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
3255 if (!Div.isUsable()) {
3256 HasErrors = true;
3257 break;
3258 }
3259 }
3260 if (!Update.isUsable() || !Final.isUsable()) {
3261 HasErrors = true;
3262 break;
3263 }
3264 // Save results
3265 Built.Counters[Cnt] = IS.CounterVar;
3266 Built.Updates[Cnt] = Update.get();
3267 Built.Finals[Cnt] = Final.get();
3268 }
3269 }
3270
3271 if (HasErrors)
3272 return 0;
3273
3274 // Save results
3275 Built.IterationVarRef = IV.get();
3276 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00003277 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003278 Built.CalcLastIteration =
3279 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003280 Built.PreCond = PreCond.get();
3281 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003282 Built.Init = Init.get();
3283 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00003284 Built.LB = LB.get();
3285 Built.UB = UB.get();
3286 Built.IL = IL.get();
3287 Built.ST = ST.get();
3288 Built.EUB = EUB.get();
3289 Built.NLB = NextLB.get();
3290 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003291
Alexey Bataevabfc0692014-06-25 06:52:00 +00003292 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003293}
3294
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003295static Expr *GetCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Alexey Bataevc925aa32015-04-27 08:00:32 +00003296 auto &&CollapseFilter = [](const OMPClause *C) -> bool {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003297 return C->getClauseKind() == OMPC_collapse;
3298 };
3299 OMPExecutableDirective::filtered_clause_iterator<decltype(CollapseFilter)> I(
Alexey Bataevc925aa32015-04-27 08:00:32 +00003300 Clauses, std::move(CollapseFilter));
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003301 if (I)
3302 return cast<OMPCollapseClause>(*I)->getNumForLoops();
3303 return nullptr;
3304}
3305
Alexey Bataev4acb8592014-07-07 13:01:15 +00003306StmtResult Sema::ActOnOpenMPSimdDirective(
3307 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3308 SourceLocation EndLoc,
3309 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmanc6388682014-12-15 07:07:06 +00003310 OMPLoopDirective::HelperExprs B;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003311 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003312 unsigned NestedLoopCount =
3313 CheckOpenMPLoop(OMPD_simd, GetCollapseNumberExpr(Clauses), AStmt, *this,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003314 *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00003315 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003316 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003317
Alexander Musmana5f070a2014-10-01 06:03:56 +00003318 assert((CurContext->isDependentContext() || B.builtAll()) &&
3319 "omp simd loop exprs were not built");
3320
Alexander Musman3276a272015-03-21 10:12:56 +00003321 if (!CurContext->isDependentContext()) {
3322 // Finalize the clauses that need pre-built expressions for CodeGen.
3323 for (auto C : Clauses) {
3324 if (auto LC = dyn_cast<OMPLinearClause>(C))
3325 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3326 B.NumIterations, *this, CurScope))
3327 return StmtError();
3328 }
3329 }
3330
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003331 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003332 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
3333 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003334}
3335
Alexey Bataev4acb8592014-07-07 13:01:15 +00003336StmtResult Sema::ActOnOpenMPForDirective(
3337 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3338 SourceLocation EndLoc,
3339 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmanc6388682014-12-15 07:07:06 +00003340 OMPLoopDirective::HelperExprs B;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003341 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003342 unsigned NestedLoopCount =
3343 CheckOpenMPLoop(OMPD_for, GetCollapseNumberExpr(Clauses), AStmt, *this,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003344 *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00003345 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00003346 return StmtError();
3347
Alexander Musmana5f070a2014-10-01 06:03:56 +00003348 assert((CurContext->isDependentContext() || B.builtAll()) &&
3349 "omp for loop exprs were not built");
3350
Alexey Bataevf29276e2014-06-18 04:14:57 +00003351 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003352 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
3353 Clauses, AStmt, B);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003354}
3355
Alexander Musmanf82886e2014-09-18 05:12:34 +00003356StmtResult Sema::ActOnOpenMPForSimdDirective(
3357 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3358 SourceLocation EndLoc,
3359 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmanc6388682014-12-15 07:07:06 +00003360 OMPLoopDirective::HelperExprs B;
Alexander Musmanf82886e2014-09-18 05:12:34 +00003361 // In presence of clause 'collapse', it will define the nested loops number.
3362 unsigned NestedLoopCount =
3363 CheckOpenMPLoop(OMPD_for_simd, GetCollapseNumberExpr(Clauses), AStmt,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003364 *this, *DSAStack, VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00003365 if (NestedLoopCount == 0)
3366 return StmtError();
3367
Alexander Musmanc6388682014-12-15 07:07:06 +00003368 assert((CurContext->isDependentContext() || B.builtAll()) &&
3369 "omp for simd loop exprs were not built");
3370
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00003371 if (!CurContext->isDependentContext()) {
3372 // Finalize the clauses that need pre-built expressions for CodeGen.
3373 for (auto C : Clauses) {
3374 if (auto LC = dyn_cast<OMPLinearClause>(C))
3375 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3376 B.NumIterations, *this, CurScope))
3377 return StmtError();
3378 }
3379 }
3380
Alexander Musmanf82886e2014-09-18 05:12:34 +00003381 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003382 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
3383 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00003384}
3385
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003386StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
3387 Stmt *AStmt,
3388 SourceLocation StartLoc,
3389 SourceLocation EndLoc) {
3390 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3391 auto BaseStmt = AStmt;
3392 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
3393 BaseStmt = CS->getCapturedStmt();
3394 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
3395 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00003396 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003397 return StmtError();
3398 // All associated statements must be '#pragma omp section' except for
3399 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00003400 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003401 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
3402 if (SectionStmt)
3403 Diag(SectionStmt->getLocStart(),
3404 diag::err_omp_sections_substmt_not_section);
3405 return StmtError();
3406 }
3407 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003408 } else {
3409 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
3410 return StmtError();
3411 }
3412
3413 getCurFunction()->setHasBranchProtectedScope();
3414
3415 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses,
3416 AStmt);
3417}
3418
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003419StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
3420 SourceLocation StartLoc,
3421 SourceLocation EndLoc) {
3422 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3423
3424 getCurFunction()->setHasBranchProtectedScope();
3425
3426 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt);
3427}
3428
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003429StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
3430 Stmt *AStmt,
3431 SourceLocation StartLoc,
3432 SourceLocation EndLoc) {
Alexey Bataev74a05c92014-07-15 02:55:09 +00003433 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3434
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003435 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00003436
Alexey Bataev3255bf32015-01-19 05:20:46 +00003437 // OpenMP [2.7.3, single Construct, Restrictions]
3438 // The copyprivate clause must not be used with the nowait clause.
3439 OMPClause *Nowait = nullptr;
3440 OMPClause *Copyprivate = nullptr;
3441 for (auto *Clause : Clauses) {
3442 if (Clause->getClauseKind() == OMPC_nowait)
3443 Nowait = Clause;
3444 else if (Clause->getClauseKind() == OMPC_copyprivate)
3445 Copyprivate = Clause;
3446 if (Copyprivate && Nowait) {
3447 Diag(Copyprivate->getLocStart(),
3448 diag::err_omp_single_copyprivate_with_nowait);
3449 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
3450 return StmtError();
3451 }
3452 }
3453
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003454 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3455}
3456
Alexander Musman80c22892014-07-17 08:54:58 +00003457StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
3458 SourceLocation StartLoc,
3459 SourceLocation EndLoc) {
3460 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3461
3462 getCurFunction()->setHasBranchProtectedScope();
3463
3464 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
3465}
3466
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003467StmtResult
3468Sema::ActOnOpenMPCriticalDirective(const DeclarationNameInfo &DirName,
3469 Stmt *AStmt, SourceLocation StartLoc,
3470 SourceLocation EndLoc) {
3471 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3472
3473 getCurFunction()->setHasBranchProtectedScope();
3474
3475 return OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
3476 AStmt);
3477}
3478
Alexey Bataev4acb8592014-07-07 13:01:15 +00003479StmtResult Sema::ActOnOpenMPParallelForDirective(
3480 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3481 SourceLocation EndLoc,
3482 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
3483 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3484 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3485 // 1.2.2 OpenMP Language Terminology
3486 // Structured block - An executable statement with a single entry at the
3487 // top and a single exit at the bottom.
3488 // The point of exit cannot be a branch out of the structured block.
3489 // longjmp() and throw() must not violate the entry/exit criteria.
3490 CS->getCapturedDecl()->setNothrow();
3491
Alexander Musmanc6388682014-12-15 07:07:06 +00003492 OMPLoopDirective::HelperExprs B;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003493 // In presence of clause 'collapse', it will define the nested loops number.
3494 unsigned NestedLoopCount =
3495 CheckOpenMPLoop(OMPD_parallel_for, GetCollapseNumberExpr(Clauses), AStmt,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003496 *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003497 if (NestedLoopCount == 0)
3498 return StmtError();
3499
Alexander Musmana5f070a2014-10-01 06:03:56 +00003500 assert((CurContext->isDependentContext() || B.builtAll()) &&
3501 "omp parallel for loop exprs were not built");
3502
Alexey Bataev4acb8592014-07-07 13:01:15 +00003503 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003504 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
3505 NestedLoopCount, Clauses, AStmt, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003506}
3507
Alexander Musmane4e893b2014-09-23 09:33:00 +00003508StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
3509 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3510 SourceLocation EndLoc,
3511 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
3512 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3513 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3514 // 1.2.2 OpenMP Language Terminology
3515 // Structured block - An executable statement with a single entry at the
3516 // top and a single exit at the bottom.
3517 // The point of exit cannot be a branch out of the structured block.
3518 // longjmp() and throw() must not violate the entry/exit criteria.
3519 CS->getCapturedDecl()->setNothrow();
3520
Alexander Musmanc6388682014-12-15 07:07:06 +00003521 OMPLoopDirective::HelperExprs B;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003522 // In presence of clause 'collapse', it will define the nested loops number.
3523 unsigned NestedLoopCount =
3524 CheckOpenMPLoop(OMPD_parallel_for_simd, GetCollapseNumberExpr(Clauses),
Alexander Musmana5f070a2014-10-01 06:03:56 +00003525 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003526 if (NestedLoopCount == 0)
3527 return StmtError();
3528
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00003529 if (!CurContext->isDependentContext()) {
3530 // Finalize the clauses that need pre-built expressions for CodeGen.
3531 for (auto C : Clauses) {
3532 if (auto LC = dyn_cast<OMPLinearClause>(C))
3533 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3534 B.NumIterations, *this, CurScope))
3535 return StmtError();
3536 }
3537 }
3538
Alexander Musmane4e893b2014-09-23 09:33:00 +00003539 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003540 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00003541 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003542}
3543
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003544StmtResult
3545Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
3546 Stmt *AStmt, SourceLocation StartLoc,
3547 SourceLocation EndLoc) {
3548 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3549 auto BaseStmt = AStmt;
3550 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
3551 BaseStmt = CS->getCapturedStmt();
3552 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
3553 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00003554 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003555 return StmtError();
3556 // All associated statements must be '#pragma omp section' except for
3557 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00003558 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003559 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
3560 if (SectionStmt)
3561 Diag(SectionStmt->getLocStart(),
3562 diag::err_omp_parallel_sections_substmt_not_section);
3563 return StmtError();
3564 }
3565 }
3566 } else {
3567 Diag(AStmt->getLocStart(),
3568 diag::err_omp_parallel_sections_not_compound_stmt);
3569 return StmtError();
3570 }
3571
3572 getCurFunction()->setHasBranchProtectedScope();
3573
3574 return OMPParallelSectionsDirective::Create(Context, StartLoc, EndLoc,
3575 Clauses, AStmt);
3576}
3577
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003578StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
3579 Stmt *AStmt, SourceLocation StartLoc,
3580 SourceLocation EndLoc) {
3581 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3582 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3583 // 1.2.2 OpenMP Language Terminology
3584 // Structured block - An executable statement with a single entry at the
3585 // top and a single exit at the bottom.
3586 // The point of exit cannot be a branch out of the structured block.
3587 // longjmp() and throw() must not violate the entry/exit criteria.
3588 CS->getCapturedDecl()->setNothrow();
3589
3590 getCurFunction()->setHasBranchProtectedScope();
3591
3592 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3593}
3594
Alexey Bataev68446b72014-07-18 07:47:19 +00003595StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
3596 SourceLocation EndLoc) {
3597 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
3598}
3599
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003600StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
3601 SourceLocation EndLoc) {
3602 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
3603}
3604
Alexey Bataev2df347a2014-07-18 10:17:07 +00003605StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
3606 SourceLocation EndLoc) {
3607 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
3608}
3609
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003610StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
3611 SourceLocation StartLoc,
3612 SourceLocation EndLoc) {
3613 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3614
3615 getCurFunction()->setHasBranchProtectedScope();
3616
3617 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
3618}
3619
Alexey Bataev6125da92014-07-21 11:26:11 +00003620StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
3621 SourceLocation StartLoc,
3622 SourceLocation EndLoc) {
3623 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
3624 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
3625}
3626
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003627StmtResult Sema::ActOnOpenMPOrderedDirective(Stmt *AStmt,
3628 SourceLocation StartLoc,
3629 SourceLocation EndLoc) {
3630 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3631
3632 getCurFunction()->setHasBranchProtectedScope();
3633
3634 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, AStmt);
3635}
3636
Alexey Bataev1d160b12015-03-13 12:27:31 +00003637namespace {
3638/// \brief Helper class for checking expression in 'omp atomic [update]'
3639/// construct.
3640class OpenMPAtomicUpdateChecker {
3641 /// \brief Error results for atomic update expressions.
3642 enum ExprAnalysisErrorCode {
3643 /// \brief A statement is not an expression statement.
3644 NotAnExpression,
3645 /// \brief Expression is not builtin binary or unary operation.
3646 NotABinaryOrUnaryExpression,
3647 /// \brief Unary operation is not post-/pre- increment/decrement operation.
3648 NotAnUnaryIncDecExpression,
3649 /// \brief An expression is not of scalar type.
3650 NotAScalarType,
3651 /// \brief A binary operation is not an assignment operation.
3652 NotAnAssignmentOp,
3653 /// \brief RHS part of the binary operation is not a binary expression.
3654 NotABinaryExpression,
3655 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
3656 /// expression.
3657 NotABinaryOperator,
3658 /// \brief RHS binary operation does not have reference to the updated LHS
3659 /// part.
3660 NotAnUpdateExpression,
3661 /// \brief No errors is found.
3662 NoError
3663 };
3664 /// \brief Reference to Sema.
3665 Sema &SemaRef;
3666 /// \brief A location for note diagnostics (when error is found).
3667 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003668 /// \brief 'x' lvalue part of the source atomic expression.
3669 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003670 /// \brief 'expr' rvalue part of the source atomic expression.
3671 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00003672 /// \brief Helper expression of the form
3673 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
3674 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
3675 Expr *UpdateExpr;
3676 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
3677 /// important for non-associative operations.
3678 bool IsXLHSInRHSPart;
3679 BinaryOperatorKind Op;
3680 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00003681 /// \brief true if the source expression is a postfix unary operation, false
3682 /// if it is a prefix unary operation.
3683 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003684
3685public:
3686 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00003687 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00003688 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00003689 /// \brief Check specified statement that it is suitable for 'atomic update'
3690 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00003691 /// expression. If DiagId and NoteId == 0, then only check is performed
3692 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00003693 /// \param DiagId Diagnostic which should be emitted if error is found.
3694 /// \param NoteId Diagnostic note for the main error message.
3695 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00003696 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00003697 /// \brief Return the 'x' lvalue part of the source atomic expression.
3698 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00003699 /// \brief Return the 'expr' rvalue part of the source atomic expression.
3700 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00003701 /// \brief Return the update expression used in calculation of the updated
3702 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
3703 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
3704 Expr *getUpdateExpr() const { return UpdateExpr; }
3705 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
3706 /// false otherwise.
3707 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
3708
Alexey Bataevb78ca832015-04-01 03:33:17 +00003709 /// \brief true if the source expression is a postfix unary operation, false
3710 /// if it is a prefix unary operation.
3711 bool isPostfixUpdate() const { return IsPostfixUpdate; }
3712
Alexey Bataev1d160b12015-03-13 12:27:31 +00003713private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00003714 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
3715 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00003716};
3717} // namespace
3718
3719bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
3720 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
3721 ExprAnalysisErrorCode ErrorFound = NoError;
3722 SourceLocation ErrorLoc, NoteLoc;
3723 SourceRange ErrorRange, NoteRange;
3724 // Allowed constructs are:
3725 // x = x binop expr;
3726 // x = expr binop x;
3727 if (AtomicBinOp->getOpcode() == BO_Assign) {
3728 X = AtomicBinOp->getLHS();
3729 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
3730 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
3731 if (AtomicInnerBinOp->isMultiplicativeOp() ||
3732 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
3733 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00003734 Op = AtomicInnerBinOp->getOpcode();
3735 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00003736 auto *LHS = AtomicInnerBinOp->getLHS();
3737 auto *RHS = AtomicInnerBinOp->getRHS();
3738 llvm::FoldingSetNodeID XId, LHSId, RHSId;
3739 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
3740 /*Canonical=*/true);
3741 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
3742 /*Canonical=*/true);
3743 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
3744 /*Canonical=*/true);
3745 if (XId == LHSId) {
3746 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00003747 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003748 } else if (XId == RHSId) {
3749 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00003750 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003751 } else {
3752 ErrorLoc = AtomicInnerBinOp->getExprLoc();
3753 ErrorRange = AtomicInnerBinOp->getSourceRange();
3754 NoteLoc = X->getExprLoc();
3755 NoteRange = X->getSourceRange();
3756 ErrorFound = NotAnUpdateExpression;
3757 }
3758 } else {
3759 ErrorLoc = AtomicInnerBinOp->getExprLoc();
3760 ErrorRange = AtomicInnerBinOp->getSourceRange();
3761 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
3762 NoteRange = SourceRange(NoteLoc, NoteLoc);
3763 ErrorFound = NotABinaryOperator;
3764 }
3765 } else {
3766 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
3767 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
3768 ErrorFound = NotABinaryExpression;
3769 }
3770 } else {
3771 ErrorLoc = AtomicBinOp->getExprLoc();
3772 ErrorRange = AtomicBinOp->getSourceRange();
3773 NoteLoc = AtomicBinOp->getOperatorLoc();
3774 NoteRange = SourceRange(NoteLoc, NoteLoc);
3775 ErrorFound = NotAnAssignmentOp;
3776 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00003777 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00003778 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
3779 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
3780 return true;
3781 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00003782 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00003783 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003784}
3785
3786bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
3787 unsigned NoteId) {
3788 ExprAnalysisErrorCode ErrorFound = NoError;
3789 SourceLocation ErrorLoc, NoteLoc;
3790 SourceRange ErrorRange, NoteRange;
3791 // Allowed constructs are:
3792 // x++;
3793 // x--;
3794 // ++x;
3795 // --x;
3796 // x binop= expr;
3797 // x = x binop expr;
3798 // x = expr binop x;
3799 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
3800 AtomicBody = AtomicBody->IgnoreParenImpCasts();
3801 if (AtomicBody->getType()->isScalarType() ||
3802 AtomicBody->isInstantiationDependent()) {
3803 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
3804 AtomicBody->IgnoreParenImpCasts())) {
3805 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00003806 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00003807 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00003808 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00003809 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00003810 X = AtomicCompAssignOp->getLHS();
3811 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003812 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
3813 AtomicBody->IgnoreParenImpCasts())) {
3814 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00003815 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
3816 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003817 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00003818 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
3819 // Check for Unary Operation
3820 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00003821 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00003822 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
3823 OpLoc = AtomicUnaryOp->getOperatorLoc();
3824 X = AtomicUnaryOp->getSubExpr();
3825 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
3826 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003827 } else {
3828 ErrorFound = NotAnUnaryIncDecExpression;
3829 ErrorLoc = AtomicUnaryOp->getExprLoc();
3830 ErrorRange = AtomicUnaryOp->getSourceRange();
3831 NoteLoc = AtomicUnaryOp->getOperatorLoc();
3832 NoteRange = SourceRange(NoteLoc, NoteLoc);
3833 }
3834 } else {
3835 ErrorFound = NotABinaryOrUnaryExpression;
3836 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
3837 NoteRange = ErrorRange = AtomicBody->getSourceRange();
3838 }
3839 } else {
3840 ErrorFound = NotAScalarType;
3841 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
3842 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
3843 }
3844 } else {
3845 ErrorFound = NotAnExpression;
3846 NoteLoc = ErrorLoc = S->getLocStart();
3847 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
3848 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00003849 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00003850 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
3851 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
3852 return true;
3853 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00003854 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00003855 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00003856 // Build an update expression of form 'OpaqueValueExpr(x) binop
3857 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
3858 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
3859 auto *OVEX = new (SemaRef.getASTContext())
3860 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
3861 auto *OVEExpr = new (SemaRef.getASTContext())
3862 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
3863 auto Update =
3864 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
3865 IsXLHSInRHSPart ? OVEExpr : OVEX);
3866 if (Update.isInvalid())
3867 return true;
3868 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
3869 Sema::AA_Casting);
3870 if (Update.isInvalid())
3871 return true;
3872 UpdateExpr = Update.get();
3873 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00003874 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003875}
3876
Alexey Bataev0162e452014-07-22 10:10:35 +00003877StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
3878 Stmt *AStmt,
3879 SourceLocation StartLoc,
3880 SourceLocation EndLoc) {
3881 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003882 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00003883 // 1.2.2 OpenMP Language Terminology
3884 // Structured block - An executable statement with a single entry at the
3885 // top and a single exit at the bottom.
3886 // The point of exit cannot be a branch out of the structured block.
3887 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00003888 OpenMPClauseKind AtomicKind = OMPC_unknown;
3889 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003890 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00003891 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00003892 C->getClauseKind() == OMPC_update ||
3893 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00003894 if (AtomicKind != OMPC_unknown) {
3895 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
3896 << SourceRange(C->getLocStart(), C->getLocEnd());
3897 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
3898 << getOpenMPClauseName(AtomicKind);
3899 } else {
3900 AtomicKind = C->getClauseKind();
3901 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003902 }
3903 }
3904 }
Alexey Bataev62cec442014-11-18 10:14:22 +00003905
Alexey Bataev459dec02014-07-24 06:46:57 +00003906 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00003907 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
3908 Body = EWC->getSubExpr();
3909
Alexey Bataev62cec442014-11-18 10:14:22 +00003910 Expr *X = nullptr;
3911 Expr *V = nullptr;
3912 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00003913 Expr *UE = nullptr;
3914 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00003915 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00003916 // OpenMP [2.12.6, atomic Construct]
3917 // In the next expressions:
3918 // * x and v (as applicable) are both l-value expressions with scalar type.
3919 // * During the execution of an atomic region, multiple syntactic
3920 // occurrences of x must designate the same storage location.
3921 // * Neither of v and expr (as applicable) may access the storage location
3922 // designated by x.
3923 // * Neither of x and expr (as applicable) may access the storage location
3924 // designated by v.
3925 // * expr is an expression with scalar type.
3926 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
3927 // * binop, binop=, ++, and -- are not overloaded operators.
3928 // * The expression x binop expr must be numerically equivalent to x binop
3929 // (expr). This requirement is satisfied if the operators in expr have
3930 // precedence greater than binop, or by using parentheses around expr or
3931 // subexpressions of expr.
3932 // * The expression expr binop x must be numerically equivalent to (expr)
3933 // binop x. This requirement is satisfied if the operators in expr have
3934 // precedence equal to or greater than binop, or by using parentheses around
3935 // expr or subexpressions of expr.
3936 // * For forms that allow multiple occurrences of x, the number of times
3937 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00003938 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00003939 enum {
3940 NotAnExpression,
3941 NotAnAssignmentOp,
3942 NotAScalarType,
3943 NotAnLValue,
3944 NoError
3945 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00003946 SourceLocation ErrorLoc, NoteLoc;
3947 SourceRange ErrorRange, NoteRange;
3948 // If clause is read:
3949 // v = x;
3950 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
3951 auto AtomicBinOp =
3952 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
3953 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
3954 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
3955 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
3956 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
3957 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
3958 if (!X->isLValue() || !V->isLValue()) {
3959 auto NotLValueExpr = X->isLValue() ? V : X;
3960 ErrorFound = NotAnLValue;
3961 ErrorLoc = AtomicBinOp->getExprLoc();
3962 ErrorRange = AtomicBinOp->getSourceRange();
3963 NoteLoc = NotLValueExpr->getExprLoc();
3964 NoteRange = NotLValueExpr->getSourceRange();
3965 }
3966 } else if (!X->isInstantiationDependent() ||
3967 !V->isInstantiationDependent()) {
3968 auto NotScalarExpr =
3969 (X->isInstantiationDependent() || X->getType()->isScalarType())
3970 ? V
3971 : X;
3972 ErrorFound = NotAScalarType;
3973 ErrorLoc = AtomicBinOp->getExprLoc();
3974 ErrorRange = AtomicBinOp->getSourceRange();
3975 NoteLoc = NotScalarExpr->getExprLoc();
3976 NoteRange = NotScalarExpr->getSourceRange();
3977 }
3978 } else {
3979 ErrorFound = NotAnAssignmentOp;
3980 ErrorLoc = AtomicBody->getExprLoc();
3981 ErrorRange = AtomicBody->getSourceRange();
3982 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
3983 : AtomicBody->getExprLoc();
3984 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
3985 : AtomicBody->getSourceRange();
3986 }
3987 } else {
3988 ErrorFound = NotAnExpression;
3989 NoteLoc = ErrorLoc = Body->getLocStart();
3990 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00003991 }
Alexey Bataev62cec442014-11-18 10:14:22 +00003992 if (ErrorFound != NoError) {
3993 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
3994 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00003995 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
3996 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00003997 return StmtError();
3998 } else if (CurContext->isDependentContext())
3999 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00004000 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004001 enum {
4002 NotAnExpression,
4003 NotAnAssignmentOp,
4004 NotAScalarType,
4005 NotAnLValue,
4006 NoError
4007 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00004008 SourceLocation ErrorLoc, NoteLoc;
4009 SourceRange ErrorRange, NoteRange;
4010 // If clause is write:
4011 // x = expr;
4012 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
4013 auto AtomicBinOp =
4014 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
4015 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00004016 X = AtomicBinOp->getLHS();
4017 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00004018 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
4019 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
4020 if (!X->isLValue()) {
4021 ErrorFound = NotAnLValue;
4022 ErrorLoc = AtomicBinOp->getExprLoc();
4023 ErrorRange = AtomicBinOp->getSourceRange();
4024 NoteLoc = X->getExprLoc();
4025 NoteRange = X->getSourceRange();
4026 }
4027 } else if (!X->isInstantiationDependent() ||
4028 !E->isInstantiationDependent()) {
4029 auto NotScalarExpr =
4030 (X->isInstantiationDependent() || X->getType()->isScalarType())
4031 ? E
4032 : X;
4033 ErrorFound = NotAScalarType;
4034 ErrorLoc = AtomicBinOp->getExprLoc();
4035 ErrorRange = AtomicBinOp->getSourceRange();
4036 NoteLoc = NotScalarExpr->getExprLoc();
4037 NoteRange = NotScalarExpr->getSourceRange();
4038 }
4039 } else {
4040 ErrorFound = NotAnAssignmentOp;
4041 ErrorLoc = AtomicBody->getExprLoc();
4042 ErrorRange = AtomicBody->getSourceRange();
4043 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
4044 : AtomicBody->getExprLoc();
4045 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
4046 : AtomicBody->getSourceRange();
4047 }
4048 } else {
4049 ErrorFound = NotAnExpression;
4050 NoteLoc = ErrorLoc = Body->getLocStart();
4051 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00004052 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00004053 if (ErrorFound != NoError) {
4054 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
4055 << ErrorRange;
4056 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
4057 << NoteRange;
4058 return StmtError();
4059 } else if (CurContext->isDependentContext())
4060 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00004061 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004062 // If clause is update:
4063 // x++;
4064 // x--;
4065 // ++x;
4066 // --x;
4067 // x binop= expr;
4068 // x = x binop expr;
4069 // x = expr binop x;
4070 OpenMPAtomicUpdateChecker Checker(*this);
4071 if (Checker.checkStatement(
4072 Body, (AtomicKind == OMPC_update)
4073 ? diag::err_omp_atomic_update_not_expression_statement
4074 : diag::err_omp_atomic_not_expression_statement,
4075 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00004076 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00004077 if (!CurContext->isDependentContext()) {
4078 E = Checker.getExpr();
4079 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00004080 UE = Checker.getUpdateExpr();
4081 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00004082 }
Alexey Bataev459dec02014-07-24 06:46:57 +00004083 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004084 enum {
4085 NotAnAssignmentOp,
4086 NotACompoundStatement,
4087 NotTwoSubstatements,
4088 NotASpecificExpression,
4089 NoError
4090 } ErrorFound = NoError;
4091 SourceLocation ErrorLoc, NoteLoc;
4092 SourceRange ErrorRange, NoteRange;
4093 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
4094 // If clause is a capture:
4095 // v = x++;
4096 // v = x--;
4097 // v = ++x;
4098 // v = --x;
4099 // v = x binop= expr;
4100 // v = x = x binop expr;
4101 // v = x = expr binop x;
4102 auto *AtomicBinOp =
4103 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
4104 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
4105 V = AtomicBinOp->getLHS();
4106 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
4107 OpenMPAtomicUpdateChecker Checker(*this);
4108 if (Checker.checkStatement(
4109 Body, diag::err_omp_atomic_capture_not_expression_statement,
4110 diag::note_omp_atomic_update))
4111 return StmtError();
4112 E = Checker.getExpr();
4113 X = Checker.getX();
4114 UE = Checker.getUpdateExpr();
4115 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
4116 IsPostfixUpdate = Checker.isPostfixUpdate();
4117 } else {
4118 ErrorLoc = AtomicBody->getExprLoc();
4119 ErrorRange = AtomicBody->getSourceRange();
4120 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
4121 : AtomicBody->getExprLoc();
4122 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
4123 : AtomicBody->getSourceRange();
4124 ErrorFound = NotAnAssignmentOp;
4125 }
4126 if (ErrorFound != NoError) {
4127 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
4128 << ErrorRange;
4129 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
4130 return StmtError();
4131 } else if (CurContext->isDependentContext()) {
4132 UE = V = E = X = nullptr;
4133 }
4134 } else {
4135 // If clause is a capture:
4136 // { v = x; x = expr; }
4137 // { v = x; x++; }
4138 // { v = x; x--; }
4139 // { v = x; ++x; }
4140 // { v = x; --x; }
4141 // { v = x; x binop= expr; }
4142 // { v = x; x = x binop expr; }
4143 // { v = x; x = expr binop x; }
4144 // { x++; v = x; }
4145 // { x--; v = x; }
4146 // { ++x; v = x; }
4147 // { --x; v = x; }
4148 // { x binop= expr; v = x; }
4149 // { x = x binop expr; v = x; }
4150 // { x = expr binop x; v = x; }
4151 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
4152 // Check that this is { expr1; expr2; }
4153 if (CS->size() == 2) {
4154 auto *First = CS->body_front();
4155 auto *Second = CS->body_back();
4156 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
4157 First = EWC->getSubExpr()->IgnoreParenImpCasts();
4158 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
4159 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
4160 // Need to find what subexpression is 'v' and what is 'x'.
4161 OpenMPAtomicUpdateChecker Checker(*this);
4162 bool IsUpdateExprFound = !Checker.checkStatement(Second);
4163 BinaryOperator *BinOp = nullptr;
4164 if (IsUpdateExprFound) {
4165 BinOp = dyn_cast<BinaryOperator>(First);
4166 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
4167 }
4168 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
4169 // { v = x; x++; }
4170 // { v = x; x--; }
4171 // { v = x; ++x; }
4172 // { v = x; --x; }
4173 // { v = x; x binop= expr; }
4174 // { v = x; x = x binop expr; }
4175 // { v = x; x = expr binop x; }
4176 // Check that the first expression has form v = x.
4177 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
4178 llvm::FoldingSetNodeID XId, PossibleXId;
4179 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
4180 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
4181 IsUpdateExprFound = XId == PossibleXId;
4182 if (IsUpdateExprFound) {
4183 V = BinOp->getLHS();
4184 X = Checker.getX();
4185 E = Checker.getExpr();
4186 UE = Checker.getUpdateExpr();
4187 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00004188 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004189 }
4190 }
4191 if (!IsUpdateExprFound) {
4192 IsUpdateExprFound = !Checker.checkStatement(First);
4193 BinOp = nullptr;
4194 if (IsUpdateExprFound) {
4195 BinOp = dyn_cast<BinaryOperator>(Second);
4196 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
4197 }
4198 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
4199 // { x++; v = x; }
4200 // { x--; v = x; }
4201 // { ++x; v = x; }
4202 // { --x; v = x; }
4203 // { x binop= expr; v = x; }
4204 // { x = x binop expr; v = x; }
4205 // { x = expr binop x; v = x; }
4206 // Check that the second expression has form v = x.
4207 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
4208 llvm::FoldingSetNodeID XId, PossibleXId;
4209 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
4210 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
4211 IsUpdateExprFound = XId == PossibleXId;
4212 if (IsUpdateExprFound) {
4213 V = BinOp->getLHS();
4214 X = Checker.getX();
4215 E = Checker.getExpr();
4216 UE = Checker.getUpdateExpr();
4217 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00004218 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004219 }
4220 }
4221 }
4222 if (!IsUpdateExprFound) {
4223 // { v = x; x = expr; }
4224 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
4225 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
4226 ErrorFound = NotAnAssignmentOp;
4227 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
4228 : First->getLocStart();
4229 NoteRange = ErrorRange = FirstBinOp
4230 ? FirstBinOp->getSourceRange()
4231 : SourceRange(ErrorLoc, ErrorLoc);
4232 } else {
4233 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
4234 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
4235 ErrorFound = NotAnAssignmentOp;
4236 NoteLoc = ErrorLoc = SecondBinOp ? SecondBinOp->getOperatorLoc()
4237 : Second->getLocStart();
4238 NoteRange = ErrorRange = SecondBinOp
4239 ? SecondBinOp->getSourceRange()
4240 : SourceRange(ErrorLoc, ErrorLoc);
4241 } else {
4242 auto *PossibleXRHSInFirst =
4243 FirstBinOp->getRHS()->IgnoreParenImpCasts();
4244 auto *PossibleXLHSInSecond =
4245 SecondBinOp->getLHS()->IgnoreParenImpCasts();
4246 llvm::FoldingSetNodeID X1Id, X2Id;
4247 PossibleXRHSInFirst->Profile(X1Id, Context, /*Canonical=*/true);
4248 PossibleXLHSInSecond->Profile(X2Id, Context,
4249 /*Canonical=*/true);
4250 IsUpdateExprFound = X1Id == X2Id;
4251 if (IsUpdateExprFound) {
4252 V = FirstBinOp->getLHS();
4253 X = SecondBinOp->getLHS();
4254 E = SecondBinOp->getRHS();
4255 UE = nullptr;
4256 IsXLHSInRHSPart = false;
4257 IsPostfixUpdate = true;
4258 } else {
4259 ErrorFound = NotASpecificExpression;
4260 ErrorLoc = FirstBinOp->getExprLoc();
4261 ErrorRange = FirstBinOp->getSourceRange();
4262 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
4263 NoteRange = SecondBinOp->getRHS()->getSourceRange();
4264 }
4265 }
4266 }
4267 }
4268 } else {
4269 NoteLoc = ErrorLoc = Body->getLocStart();
4270 NoteRange = ErrorRange =
4271 SourceRange(Body->getLocStart(), Body->getLocStart());
4272 ErrorFound = NotTwoSubstatements;
4273 }
4274 } else {
4275 NoteLoc = ErrorLoc = Body->getLocStart();
4276 NoteRange = ErrorRange =
4277 SourceRange(Body->getLocStart(), Body->getLocStart());
4278 ErrorFound = NotACompoundStatement;
4279 }
4280 if (ErrorFound != NoError) {
4281 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
4282 << ErrorRange;
4283 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
4284 return StmtError();
4285 } else if (CurContext->isDependentContext()) {
4286 UE = V = E = X = nullptr;
4287 }
Alexey Bataev459dec02014-07-24 06:46:57 +00004288 }
Alexey Bataevdea47612014-07-23 07:46:59 +00004289 }
Alexey Bataev0162e452014-07-22 10:10:35 +00004290
4291 getCurFunction()->setHasBranchProtectedScope();
4292
Alexey Bataev62cec442014-11-18 10:14:22 +00004293 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00004294 X, V, E, UE, IsXLHSInRHSPart,
4295 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00004296}
4297
Alexey Bataev0bd520b2014-09-19 08:19:49 +00004298StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
4299 Stmt *AStmt,
4300 SourceLocation StartLoc,
4301 SourceLocation EndLoc) {
4302 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
4303
Alexey Bataev13314bf2014-10-09 04:18:56 +00004304 // OpenMP [2.16, Nesting of Regions]
4305 // If specified, a teams construct must be contained within a target
4306 // construct. That target construct must contain no statements or directives
4307 // outside of the teams construct.
4308 if (DSAStack->hasInnerTeamsRegion()) {
4309 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
4310 bool OMPTeamsFound = true;
4311 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
4312 auto I = CS->body_begin();
4313 while (I != CS->body_end()) {
4314 auto OED = dyn_cast<OMPExecutableDirective>(*I);
4315 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
4316 OMPTeamsFound = false;
4317 break;
4318 }
4319 ++I;
4320 }
4321 assert(I != CS->body_end() && "Not found statement");
4322 S = *I;
4323 }
4324 if (!OMPTeamsFound) {
4325 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
4326 Diag(DSAStack->getInnerTeamsRegionLoc(),
4327 diag::note_omp_nested_teams_construct_here);
4328 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
4329 << isa<OMPExecutableDirective>(S);
4330 return StmtError();
4331 }
4332 }
4333
Alexey Bataev0bd520b2014-09-19 08:19:49 +00004334 getCurFunction()->setHasBranchProtectedScope();
4335
4336 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4337}
4338
Michael Wong65f367f2015-07-21 13:44:28 +00004339StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
4340 Stmt *AStmt,
4341 SourceLocation StartLoc,
4342 SourceLocation EndLoc) {
4343 getCurFunction()->setHasBranchProtectedScope();
4344
4345 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
4346 AStmt);
4347}
4348
Alexey Bataev13314bf2014-10-09 04:18:56 +00004349StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
4350 Stmt *AStmt, SourceLocation StartLoc,
4351 SourceLocation EndLoc) {
4352 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
4353 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4354 // 1.2.2 OpenMP Language Terminology
4355 // Structured block - An executable statement with a single entry at the
4356 // top and a single exit at the bottom.
4357 // The point of exit cannot be a branch out of the structured block.
4358 // longjmp() and throw() must not violate the entry/exit criteria.
4359 CS->getCapturedDecl()->setNothrow();
4360
4361 getCurFunction()->setHasBranchProtectedScope();
4362
4363 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4364}
4365
Alexey Bataev6d4ed052015-07-01 06:57:41 +00004366StmtResult
4367Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
4368 SourceLocation EndLoc,
4369 OpenMPDirectiveKind CancelRegion) {
4370 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
4371 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
4372 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
4373 << getOpenMPDirectiveName(CancelRegion);
4374 return StmtError();
4375 }
4376 if (DSAStack->isParentNowaitRegion()) {
4377 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
4378 return StmtError();
4379 }
4380 if (DSAStack->isParentOrderedRegion()) {
4381 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
4382 return StmtError();
4383 }
4384 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
4385 CancelRegion);
4386}
4387
Alexey Bataev80909872015-07-02 11:25:17 +00004388StmtResult Sema::ActOnOpenMPCancelDirective(SourceLocation StartLoc,
4389 SourceLocation EndLoc,
4390 OpenMPDirectiveKind CancelRegion) {
4391 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
4392 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
4393 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
4394 << getOpenMPDirectiveName(CancelRegion);
4395 return StmtError();
4396 }
4397 if (DSAStack->isParentNowaitRegion()) {
4398 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
4399 return StmtError();
4400 }
4401 if (DSAStack->isParentOrderedRegion()) {
4402 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
4403 return StmtError();
4404 }
4405 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, CancelRegion);
4406}
4407
Alexey Bataeved09d242014-05-28 05:53:51 +00004408OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004409 SourceLocation StartLoc,
4410 SourceLocation LParenLoc,
4411 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004412 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004413 switch (Kind) {
4414 case OMPC_if:
4415 Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc);
4416 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00004417 case OMPC_final:
4418 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
4419 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00004420 case OMPC_num_threads:
4421 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
4422 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00004423 case OMPC_safelen:
4424 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
4425 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00004426 case OMPC_collapse:
4427 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
4428 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004429 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004430 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00004431 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004432 case OMPC_private:
4433 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00004434 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004435 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00004436 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00004437 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00004438 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004439 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00004440 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004441 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00004442 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00004443 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00004444 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004445 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00004446 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004447 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00004448 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00004449 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00004450 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00004451 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00004452 case OMPC_depend:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004453 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004454 llvm_unreachable("Clause is not allowed.");
4455 }
4456 return Res;
4457}
4458
Alexey Bataeved09d242014-05-28 05:53:51 +00004459OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004460 SourceLocation LParenLoc,
4461 SourceLocation EndLoc) {
4462 Expr *ValExpr = Condition;
4463 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
4464 !Condition->isInstantiationDependent() &&
4465 !Condition->containsUnexpandedParameterPack()) {
4466 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00004467 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004468 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004469 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004470
Nikola Smiljanic01a75982014-05-29 10:55:11 +00004471 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004472 }
4473
4474 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc);
4475}
4476
Alexey Bataev3778b602014-07-17 07:32:53 +00004477OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
4478 SourceLocation StartLoc,
4479 SourceLocation LParenLoc,
4480 SourceLocation EndLoc) {
4481 Expr *ValExpr = Condition;
4482 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
4483 !Condition->isInstantiationDependent() &&
4484 !Condition->containsUnexpandedParameterPack()) {
4485 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
4486 Condition->getExprLoc(), Condition);
4487 if (Val.isInvalid())
4488 return nullptr;
4489
4490 ValExpr = Val.get();
4491 }
4492
4493 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
4494}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004495ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
4496 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00004497 if (!Op)
4498 return ExprError();
4499
4500 class IntConvertDiagnoser : public ICEConvertDiagnoser {
4501 public:
4502 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00004503 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00004504 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
4505 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004506 return S.Diag(Loc, diag::err_omp_not_integral) << T;
4507 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004508 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
4509 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004510 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
4511 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004512 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
4513 QualType T,
4514 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004515 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
4516 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004517 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
4518 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004519 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00004520 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00004521 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004522 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
4523 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004524 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
4525 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004526 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
4527 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004528 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00004529 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00004530 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004531 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
4532 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004533 llvm_unreachable("conversion functions are permitted");
4534 }
4535 } ConvertDiagnoser;
4536 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
4537}
4538
4539OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
4540 SourceLocation StartLoc,
4541 SourceLocation LParenLoc,
4542 SourceLocation EndLoc) {
4543 Expr *ValExpr = NumThreads;
4544 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
Alexey Bataev568a8332014-03-06 06:15:19 +00004545 !NumThreads->containsUnexpandedParameterPack()) {
4546 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
4547 ExprResult Val =
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004548 PerformOpenMPImplicitIntegerConversion(NumThreadsLoc, NumThreads);
Alexey Bataev568a8332014-03-06 06:15:19 +00004549 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004550 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00004551
Nikola Smiljanic01a75982014-05-29 10:55:11 +00004552 ValExpr = Val.get();
Alexey Bataev568a8332014-03-06 06:15:19 +00004553
4554 // OpenMP [2.5, Restrictions]
4555 // The num_threads expression must evaluate to a positive integer value.
4556 llvm::APSInt Result;
Alexey Bataeved09d242014-05-28 05:53:51 +00004557 if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() &&
4558 !Result.isStrictlyPositive()) {
Alexey Bataev568a8332014-03-06 06:15:19 +00004559 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
4560 << "num_threads" << NumThreads->getSourceRange();
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004561 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00004562 }
4563 }
4564
Alexey Bataeved09d242014-05-28 05:53:51 +00004565 return new (Context)
4566 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00004567}
4568
Alexey Bataev62c87d22014-03-21 04:51:18 +00004569ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
4570 OpenMPClauseKind CKind) {
4571 if (!E)
4572 return ExprError();
4573 if (E->isValueDependent() || E->isTypeDependent() ||
4574 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00004575 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00004576 llvm::APSInt Result;
4577 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
4578 if (ICE.isInvalid())
4579 return ExprError();
4580 if (!Result.isStrictlyPositive()) {
4581 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
4582 << getOpenMPClauseName(CKind) << E->getSourceRange();
4583 return ExprError();
4584 }
Alexander Musman09184fe2014-09-30 05:29:28 +00004585 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
4586 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
4587 << E->getSourceRange();
4588 return ExprError();
4589 }
Alexey Bataev9c821032015-04-30 04:23:23 +00004590 if (CKind == OMPC_collapse) {
4591 DSAStack->setCollapseNumber(Result.getExtValue());
4592 }
Alexey Bataev62c87d22014-03-21 04:51:18 +00004593 return ICE;
4594}
4595
4596OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
4597 SourceLocation LParenLoc,
4598 SourceLocation EndLoc) {
4599 // OpenMP [2.8.1, simd construct, Description]
4600 // The parameter of the safelen clause must be a constant
4601 // positive integer expression.
4602 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
4603 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004604 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00004605 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00004606 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00004607}
4608
Alexander Musman64d33f12014-06-04 07:53:32 +00004609OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
4610 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00004611 SourceLocation LParenLoc,
4612 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00004613 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00004614 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00004615 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00004616 // The parameter of the collapse clause must be a constant
4617 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00004618 ExprResult NumForLoopsResult =
4619 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
4620 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00004621 return nullptr;
4622 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00004623 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00004624}
4625
Alexey Bataeved09d242014-05-28 05:53:51 +00004626OMPClause *Sema::ActOnOpenMPSimpleClause(
4627 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
4628 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004629 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004630 switch (Kind) {
4631 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00004632 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00004633 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
4634 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004635 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004636 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00004637 Res = ActOnOpenMPProcBindClause(
4638 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
4639 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004640 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004641 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00004642 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00004643 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00004644 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00004645 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00004646 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004647 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004648 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00004649 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00004650 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00004651 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00004652 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00004653 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004654 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00004655 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004656 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00004657 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00004658 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00004659 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004660 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00004661 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004662 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00004663 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00004664 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00004665 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00004666 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00004667 case OMPC_depend:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004668 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004669 llvm_unreachable("Clause is not allowed.");
4670 }
4671 return Res;
4672}
4673
4674OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
4675 SourceLocation KindKwLoc,
4676 SourceLocation StartLoc,
4677 SourceLocation LParenLoc,
4678 SourceLocation EndLoc) {
4679 if (Kind == OMPC_DEFAULT_unknown) {
4680 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00004681 static_assert(OMPC_DEFAULT_unknown > 0,
4682 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00004683 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00004684 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004685 Values += "'";
4686 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
4687 Values += "'";
4688 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00004689 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004690 Values += " or ";
4691 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00004692 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004693 break;
4694 default:
4695 Values += Sep;
4696 break;
4697 }
4698 }
4699 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00004700 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004701 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004702 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00004703 switch (Kind) {
4704 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00004705 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004706 break;
4707 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00004708 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004709 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004710 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004711 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00004712 break;
4713 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004714 return new (Context)
4715 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004716}
4717
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004718OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
4719 SourceLocation KindKwLoc,
4720 SourceLocation StartLoc,
4721 SourceLocation LParenLoc,
4722 SourceLocation EndLoc) {
4723 if (Kind == OMPC_PROC_BIND_unknown) {
4724 std::string Values;
4725 std::string Sep(", ");
4726 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
4727 Values += "'";
4728 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
4729 Values += "'";
4730 switch (i) {
4731 case OMPC_PROC_BIND_unknown - 2:
4732 Values += " or ";
4733 break;
4734 case OMPC_PROC_BIND_unknown - 1:
4735 break;
4736 default:
4737 Values += Sep;
4738 break;
4739 }
4740 }
4741 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00004742 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004743 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004744 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004745 return new (Context)
4746 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004747}
4748
Alexey Bataev56dafe82014-06-20 07:16:17 +00004749OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
4750 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
4751 SourceLocation StartLoc, SourceLocation LParenLoc,
4752 SourceLocation ArgumentLoc, SourceLocation CommaLoc,
4753 SourceLocation EndLoc) {
4754 OMPClause *Res = nullptr;
4755 switch (Kind) {
4756 case OMPC_schedule:
4757 Res = ActOnOpenMPScheduleClause(
4758 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
4759 LParenLoc, ArgumentLoc, CommaLoc, EndLoc);
4760 break;
4761 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00004762 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00004763 case OMPC_num_threads:
4764 case OMPC_safelen:
4765 case OMPC_collapse:
4766 case OMPC_default:
4767 case OMPC_proc_bind:
4768 case OMPC_private:
4769 case OMPC_firstprivate:
4770 case OMPC_lastprivate:
4771 case OMPC_shared:
4772 case OMPC_reduction:
4773 case OMPC_linear:
4774 case OMPC_aligned:
4775 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00004776 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004777 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00004778 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00004779 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00004780 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00004781 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00004782 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004783 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00004784 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00004785 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00004786 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00004787 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00004788 case OMPC_depend:
Alexey Bataev56dafe82014-06-20 07:16:17 +00004789 case OMPC_unknown:
4790 llvm_unreachable("Clause is not allowed.");
4791 }
4792 return Res;
4793}
4794
4795OMPClause *Sema::ActOnOpenMPScheduleClause(
4796 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
4797 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
4798 SourceLocation EndLoc) {
4799 if (Kind == OMPC_SCHEDULE_unknown) {
4800 std::string Values;
4801 std::string Sep(", ");
4802 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) {
4803 Values += "'";
4804 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i);
4805 Values += "'";
4806 switch (i) {
4807 case OMPC_SCHEDULE_unknown - 2:
4808 Values += " or ";
4809 break;
4810 case OMPC_SCHEDULE_unknown - 1:
4811 break;
4812 default:
4813 Values += Sep;
4814 break;
4815 }
4816 }
4817 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
4818 << Values << getOpenMPClauseName(OMPC_schedule);
4819 return nullptr;
4820 }
4821 Expr *ValExpr = ChunkSize;
Alexey Bataev040d5402015-05-12 08:35:28 +00004822 Expr *HelperValExpr = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00004823 if (ChunkSize) {
4824 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
4825 !ChunkSize->isInstantiationDependent() &&
4826 !ChunkSize->containsUnexpandedParameterPack()) {
4827 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
4828 ExprResult Val =
4829 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
4830 if (Val.isInvalid())
4831 return nullptr;
4832
4833 ValExpr = Val.get();
4834
4835 // OpenMP [2.7.1, Restrictions]
4836 // chunk_size must be a loop invariant integer expression with a positive
4837 // value.
4838 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00004839 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
4840 if (Result.isSigned() && !Result.isStrictlyPositive()) {
4841 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
4842 << "schedule" << ChunkSize->getSourceRange();
4843 return nullptr;
4844 }
4845 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
4846 auto *ImpVar = buildVarDecl(*this, ChunkSize->getExprLoc(),
4847 ChunkSize->getType(), ".chunk.");
4848 auto *ImpVarRef = buildDeclRefExpr(*this, ImpVar, ChunkSize->getType(),
4849 ChunkSize->getExprLoc(),
4850 /*RefersToCapture=*/true);
4851 HelperValExpr = ImpVarRef;
Alexey Bataev56dafe82014-06-20 07:16:17 +00004852 }
4853 }
4854 }
4855
4856 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc,
Alexey Bataev040d5402015-05-12 08:35:28 +00004857 EndLoc, Kind, ValExpr, HelperValExpr);
Alexey Bataev56dafe82014-06-20 07:16:17 +00004858}
4859
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004860OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
4861 SourceLocation StartLoc,
4862 SourceLocation EndLoc) {
4863 OMPClause *Res = nullptr;
4864 switch (Kind) {
4865 case OMPC_ordered:
4866 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
4867 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00004868 case OMPC_nowait:
4869 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
4870 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00004871 case OMPC_untied:
4872 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
4873 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00004874 case OMPC_mergeable:
4875 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
4876 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004877 case OMPC_read:
4878 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
4879 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00004880 case OMPC_write:
4881 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
4882 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00004883 case OMPC_update:
4884 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
4885 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00004886 case OMPC_capture:
4887 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
4888 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00004889 case OMPC_seq_cst:
4890 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
4891 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004892 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00004893 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004894 case OMPC_num_threads:
4895 case OMPC_safelen:
4896 case OMPC_collapse:
4897 case OMPC_schedule:
4898 case OMPC_private:
4899 case OMPC_firstprivate:
4900 case OMPC_lastprivate:
4901 case OMPC_shared:
4902 case OMPC_reduction:
4903 case OMPC_linear:
4904 case OMPC_aligned:
4905 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00004906 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004907 case OMPC_default:
4908 case OMPC_proc_bind:
4909 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00004910 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00004911 case OMPC_depend:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004912 case OMPC_unknown:
4913 llvm_unreachable("Clause is not allowed.");
4914 }
4915 return Res;
4916}
4917
4918OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
4919 SourceLocation EndLoc) {
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004920 DSAStack->setOrderedRegion();
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004921 return new (Context) OMPOrderedClause(StartLoc, EndLoc);
4922}
4923
Alexey Bataev236070f2014-06-20 11:19:47 +00004924OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
4925 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00004926 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00004927 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
4928}
4929
Alexey Bataev7aea99a2014-07-17 12:19:31 +00004930OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
4931 SourceLocation EndLoc) {
4932 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
4933}
4934
Alexey Bataev74ba3a52014-07-17 12:47:03 +00004935OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
4936 SourceLocation EndLoc) {
4937 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
4938}
4939
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004940OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
4941 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004942 return new (Context) OMPReadClause(StartLoc, EndLoc);
4943}
4944
Alexey Bataevdea47612014-07-23 07:46:59 +00004945OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
4946 SourceLocation EndLoc) {
4947 return new (Context) OMPWriteClause(StartLoc, EndLoc);
4948}
4949
Alexey Bataev67a4f222014-07-23 10:25:33 +00004950OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
4951 SourceLocation EndLoc) {
4952 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
4953}
4954
Alexey Bataev459dec02014-07-24 06:46:57 +00004955OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
4956 SourceLocation EndLoc) {
4957 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
4958}
4959
Alexey Bataev82bad8b2014-07-24 08:55:34 +00004960OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
4961 SourceLocation EndLoc) {
4962 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
4963}
4964
Alexey Bataevc5e02582014-06-16 07:08:35 +00004965OMPClause *Sema::ActOnOpenMPVarListClause(
4966 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
4967 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
4968 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00004969 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
4970 SourceLocation DepLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004971 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004972 switch (Kind) {
4973 case OMPC_private:
4974 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
4975 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004976 case OMPC_firstprivate:
4977 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
4978 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00004979 case OMPC_lastprivate:
4980 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
4981 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00004982 case OMPC_shared:
4983 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
4984 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00004985 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00004986 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
4987 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00004988 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00004989 case OMPC_linear:
4990 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
4991 ColonLoc, EndLoc);
4992 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00004993 case OMPC_aligned:
4994 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
4995 ColonLoc, EndLoc);
4996 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004997 case OMPC_copyin:
4998 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
4999 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00005000 case OMPC_copyprivate:
5001 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
5002 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00005003 case OMPC_flush:
5004 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
5005 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005006 case OMPC_depend:
5007 Res = ActOnOpenMPDependClause(DepKind, DepLoc, ColonLoc, VarList, StartLoc,
5008 LParenLoc, EndLoc);
5009 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005010 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00005011 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00005012 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00005013 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00005014 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005015 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005016 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005017 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005018 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00005019 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005020 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005021 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005022 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005023 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00005024 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00005025 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00005026 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005027 case OMPC_seq_cst:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005028 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005029 llvm_unreachable("Clause is not allowed.");
5030 }
5031 return Res;
5032}
5033
5034OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
5035 SourceLocation StartLoc,
5036 SourceLocation LParenLoc,
5037 SourceLocation EndLoc) {
5038 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00005039 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00005040 for (auto &RefExpr : VarList) {
5041 assert(RefExpr && "NULL expr in OpenMP private clause.");
5042 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005043 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00005044 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00005045 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005046 continue;
5047 }
5048
Alexey Bataeved09d242014-05-28 05:53:51 +00005049 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005050 // OpenMP [2.1, C/C++]
5051 // A list item is a variable name.
5052 // OpenMP [2.9.3.3, Restrictions, p.1]
5053 // A variable that is part of another variable (as an array or
5054 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00005055 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005056 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005057 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005058 continue;
5059 }
5060 Decl *D = DE->getDecl();
5061 VarDecl *VD = cast<VarDecl>(D);
5062
5063 QualType Type = VD->getType();
5064 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5065 // It will be analyzed later.
5066 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00005067 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005068 continue;
5069 }
5070
5071 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
5072 // A variable that appears in a private clause must not have an incomplete
5073 // type or a reference type.
5074 if (RequireCompleteType(ELoc, Type,
5075 diag::err_omp_private_incomplete_type)) {
5076 continue;
5077 }
5078 if (Type->isReferenceType()) {
5079 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00005080 << getOpenMPClauseName(OMPC_private) << Type;
5081 bool IsDecl =
5082 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5083 Diag(VD->getLocation(),
5084 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5085 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005086 continue;
5087 }
5088
Alexey Bataev758e55e2013-09-06 18:03:48 +00005089 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
5090 // in a Construct]
5091 // Variables with the predetermined data-sharing attributes may not be
5092 // listed in data-sharing attributes clauses, except for the cases
5093 // listed below. For these exceptions only, listing a predetermined
5094 // variable in a data-sharing attribute clause is allowed and overrides
5095 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005096 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005097 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005098 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
5099 << getOpenMPClauseName(OMPC_private);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005100 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005101 continue;
5102 }
5103
Alexey Bataevccb59ec2015-05-19 08:44:56 +00005104 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00005105 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00005106 DSAStack->getCurrentDirective() == OMPD_task) {
5107 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
5108 << getOpenMPClauseName(OMPC_private) << Type
5109 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
5110 bool IsDecl =
5111 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5112 Diag(VD->getLocation(),
5113 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5114 << VD;
5115 continue;
5116 }
5117
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005118 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
5119 // A variable of class type (or array thereof) that appears in a private
5120 // clause requires an accessible, unambiguous default constructor for the
5121 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00005122 // Generate helper private variable and initialize it with the default
5123 // value. The address of the original variable is replaced by the address of
5124 // the new private variable in CodeGen. This new variable is not added to
5125 // IdResolver, so the code in the OpenMP region uses original variable for
5126 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005127 Type = Type.getUnqualifiedType();
5128 auto VDPrivate = buildVarDecl(*this, DE->getExprLoc(), Type, VD->getName());
Alexey Bataev39f915b82015-05-08 10:41:21 +00005129 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00005130 if (VDPrivate->isInvalidDecl())
5131 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005132 auto VDPrivateRefExpr = buildDeclRefExpr(
5133 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev03b340a2014-10-21 03:16:40 +00005134
Alexey Bataev758e55e2013-09-06 18:03:48 +00005135 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005136 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00005137 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005138 }
5139
Alexey Bataeved09d242014-05-28 05:53:51 +00005140 if (Vars.empty())
5141 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005142
Alexey Bataev03b340a2014-10-21 03:16:40 +00005143 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
5144 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005145}
5146
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005147namespace {
5148class DiagsUninitializedSeveretyRAII {
5149private:
5150 DiagnosticsEngine &Diags;
5151 SourceLocation SavedLoc;
5152 bool IsIgnored;
5153
5154public:
5155 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
5156 bool IsIgnored)
5157 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
5158 if (!IsIgnored) {
5159 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
5160 /*Map*/ diag::Severity::Ignored, Loc);
5161 }
5162 }
5163 ~DiagsUninitializedSeveretyRAII() {
5164 if (!IsIgnored)
5165 Diags.popMappings(SavedLoc);
5166 }
5167};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00005168}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005169
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005170OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
5171 SourceLocation StartLoc,
5172 SourceLocation LParenLoc,
5173 SourceLocation EndLoc) {
5174 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005175 SmallVector<Expr *, 8> PrivateCopies;
5176 SmallVector<Expr *, 8> Inits;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005177 bool IsImplicitClause =
5178 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
5179 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
5180
Alexey Bataeved09d242014-05-28 05:53:51 +00005181 for (auto &RefExpr : VarList) {
5182 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
5183 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005184 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00005185 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005186 PrivateCopies.push_back(nullptr);
5187 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005188 continue;
5189 }
5190
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005191 SourceLocation ELoc =
5192 IsImplicitClause ? ImplicitClauseLoc : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005193 // OpenMP [2.1, C/C++]
5194 // A list item is a variable name.
5195 // OpenMP [2.9.3.3, Restrictions, p.1]
5196 // A variable that is part of another variable (as an array or
5197 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00005198 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005199 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005200 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005201 continue;
5202 }
5203 Decl *D = DE->getDecl();
5204 VarDecl *VD = cast<VarDecl>(D);
5205
5206 QualType Type = VD->getType();
5207 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5208 // It will be analyzed later.
5209 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005210 PrivateCopies.push_back(nullptr);
5211 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005212 continue;
5213 }
5214
5215 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
5216 // A variable that appears in a private clause must not have an incomplete
5217 // type or a reference type.
5218 if (RequireCompleteType(ELoc, Type,
5219 diag::err_omp_firstprivate_incomplete_type)) {
5220 continue;
5221 }
5222 if (Type->isReferenceType()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005223 if (IsImplicitClause) {
5224 Diag(ImplicitClauseLoc,
5225 diag::err_omp_task_predetermined_firstprivate_ref_type_arg)
5226 << Type;
5227 Diag(RefExpr->getExprLoc(), diag::note_used_here);
5228 } else {
5229 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
5230 << getOpenMPClauseName(OMPC_firstprivate) << Type;
5231 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005232 bool IsDecl =
5233 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5234 Diag(VD->getLocation(),
5235 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5236 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005237 continue;
5238 }
5239
5240 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
5241 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00005242 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005243 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005244 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005245
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005246 // If an implicit firstprivate variable found it was checked already.
5247 if (!IsImplicitClause) {
5248 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005249 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005250 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
5251 // A list item that specifies a given variable may not appear in more
5252 // than one clause on the same directive, except that a variable may be
5253 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005254 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00005255 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005256 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00005257 << getOpenMPClauseName(DVar.CKind)
5258 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005259 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005260 continue;
5261 }
5262
5263 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
5264 // in a Construct]
5265 // Variables with the predetermined data-sharing attributes may not be
5266 // listed in data-sharing attributes clauses, except for the cases
5267 // listed below. For these exceptions only, listing a predetermined
5268 // variable in a data-sharing attribute clause is allowed and overrides
5269 // the variable's predetermined data-sharing attributes.
5270 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
5271 // in a Construct, C/C++, p.2]
5272 // Variables with const-qualified type having no mutable member may be
5273 // listed in a firstprivate clause, even if they are static data members.
5274 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
5275 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
5276 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00005277 << getOpenMPClauseName(DVar.CKind)
5278 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005279 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005280 continue;
5281 }
5282
Alexey Bataevf29276e2014-06-18 04:14:57 +00005283 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005284 // OpenMP [2.9.3.4, Restrictions, p.2]
5285 // A list item that is private within a parallel region must not appear
5286 // in a firstprivate clause on a worksharing construct if any of the
5287 // worksharing regions arising from the worksharing construct ever bind
5288 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00005289 if (isOpenMPWorksharingDirective(CurrDir) &&
5290 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005291 DVar = DSAStack->getImplicitDSA(VD, true);
5292 if (DVar.CKind != OMPC_shared &&
5293 (isOpenMPParallelDirective(DVar.DKind) ||
5294 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00005295 Diag(ELoc, diag::err_omp_required_access)
5296 << getOpenMPClauseName(OMPC_firstprivate)
5297 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005298 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005299 continue;
5300 }
5301 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005302 // OpenMP [2.9.3.4, Restrictions, p.3]
5303 // A list item that appears in a reduction clause of a parallel construct
5304 // must not appear in a firstprivate clause on a worksharing or task
5305 // construct if any of the worksharing or task regions arising from the
5306 // worksharing or task construct ever bind to any of the parallel regions
5307 // arising from the parallel construct.
5308 // OpenMP [2.9.3.4, Restrictions, p.4]
5309 // A list item that appears in a reduction clause in worksharing
5310 // construct must not appear in a firstprivate clause in a task construct
5311 // encountered during execution of any of the worksharing regions arising
5312 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005313 if (CurrDir == OMPD_task) {
5314 DVar =
5315 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
5316 [](OpenMPDirectiveKind K) -> bool {
5317 return isOpenMPParallelDirective(K) ||
5318 isOpenMPWorksharingDirective(K);
5319 },
5320 false);
5321 if (DVar.CKind == OMPC_reduction &&
5322 (isOpenMPParallelDirective(DVar.DKind) ||
5323 isOpenMPWorksharingDirective(DVar.DKind))) {
5324 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
5325 << getOpenMPDirectiveName(DVar.DKind);
5326 ReportOriginalDSA(*this, DSAStack, VD, DVar);
5327 continue;
5328 }
5329 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005330 }
5331
Alexey Bataevccb59ec2015-05-19 08:44:56 +00005332 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00005333 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00005334 DSAStack->getCurrentDirective() == OMPD_task) {
5335 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
5336 << getOpenMPClauseName(OMPC_firstprivate) << Type
5337 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
5338 bool IsDecl =
5339 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5340 Diag(VD->getLocation(),
5341 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5342 << VD;
5343 continue;
5344 }
5345
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005346 Type = Type.getUnqualifiedType();
5347 auto VDPrivate = buildVarDecl(*this, ELoc, Type, VD->getName());
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005348 // Generate helper private variable and initialize it with the value of the
5349 // original variable. The address of the original variable is replaced by
5350 // the address of the new private variable in the CodeGen. This new variable
5351 // is not added to IdResolver, so the code in the OpenMP region uses
5352 // original variable for proper diagnostics and variable capturing.
5353 Expr *VDInitRefExpr = nullptr;
5354 // For arrays generate initializer for single element and replace it by the
5355 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005356 if (Type->isArrayType()) {
5357 auto VDInit =
5358 buildVarDecl(*this, DE->getExprLoc(), ElemType, VD->getName());
5359 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005360 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005361 ElemType = ElemType.getUnqualifiedType();
5362 auto *VDInitTemp = buildVarDecl(*this, DE->getLocStart(), ElemType,
5363 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00005364 InitializedEntity Entity =
5365 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005366 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
5367
5368 InitializationSequence InitSeq(*this, Entity, Kind, Init);
5369 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
5370 if (Result.isInvalid())
5371 VDPrivate->setInvalidDecl();
5372 else
5373 VDPrivate->setInit(Result.getAs<Expr>());
5374 } else {
Alexey Bataev69c62a92015-04-15 04:52:20 +00005375 auto *VDInit =
Alexey Bataev39f915b82015-05-08 10:41:21 +00005376 buildVarDecl(*this, DE->getLocStart(), Type, ".firstprivate.temp");
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005377 VDInitRefExpr =
5378 buildDeclRefExpr(*this, VDInit, DE->getType(), DE->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00005379 AddInitializerToDecl(VDPrivate,
5380 DefaultLvalueConversion(VDInitRefExpr).get(),
5381 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005382 }
5383 if (VDPrivate->isInvalidDecl()) {
5384 if (IsImplicitClause) {
5385 Diag(DE->getExprLoc(),
5386 diag::note_omp_task_predetermined_firstprivate_here);
5387 }
5388 continue;
5389 }
5390 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00005391 auto VDPrivateRefExpr = buildDeclRefExpr(
5392 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005393 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
5394 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005395 PrivateCopies.push_back(VDPrivateRefExpr);
5396 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005397 }
5398
Alexey Bataeved09d242014-05-28 05:53:51 +00005399 if (Vars.empty())
5400 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005401
5402 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005403 Vars, PrivateCopies, Inits);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005404}
5405
Alexander Musman1bb328c2014-06-04 13:06:39 +00005406OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
5407 SourceLocation StartLoc,
5408 SourceLocation LParenLoc,
5409 SourceLocation EndLoc) {
5410 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00005411 SmallVector<Expr *, 8> SrcExprs;
5412 SmallVector<Expr *, 8> DstExprs;
5413 SmallVector<Expr *, 8> AssignmentOps;
Alexander Musman1bb328c2014-06-04 13:06:39 +00005414 for (auto &RefExpr : VarList) {
5415 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
5416 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
5417 // It will be analyzed later.
5418 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00005419 SrcExprs.push_back(nullptr);
5420 DstExprs.push_back(nullptr);
5421 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005422 continue;
5423 }
5424
5425 SourceLocation ELoc = RefExpr->getExprLoc();
5426 // OpenMP [2.1, C/C++]
5427 // A list item is a variable name.
5428 // OpenMP [2.14.3.5, Restrictions, p.1]
5429 // A variable that is part of another variable (as an array or structure
5430 // element) cannot appear in a lastprivate clause.
5431 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
5432 if (!DE || !isa<VarDecl>(DE->getDecl())) {
5433 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
5434 continue;
5435 }
5436 Decl *D = DE->getDecl();
5437 VarDecl *VD = cast<VarDecl>(D);
5438
5439 QualType Type = VD->getType();
5440 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5441 // It will be analyzed later.
5442 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00005443 SrcExprs.push_back(nullptr);
5444 DstExprs.push_back(nullptr);
5445 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005446 continue;
5447 }
5448
5449 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
5450 // A variable that appears in a lastprivate clause must not have an
5451 // incomplete type or a reference type.
5452 if (RequireCompleteType(ELoc, Type,
5453 diag::err_omp_lastprivate_incomplete_type)) {
5454 continue;
5455 }
5456 if (Type->isReferenceType()) {
5457 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
5458 << getOpenMPClauseName(OMPC_lastprivate) << Type;
5459 bool IsDecl =
5460 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5461 Diag(VD->getLocation(),
5462 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5463 << VD;
5464 continue;
5465 }
5466
5467 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
5468 // in a Construct]
5469 // Variables with the predetermined data-sharing attributes may not be
5470 // listed in data-sharing attributes clauses, except for the cases
5471 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005472 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005473 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
5474 DVar.CKind != OMPC_firstprivate &&
5475 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
5476 Diag(ELoc, diag::err_omp_wrong_dsa)
5477 << getOpenMPClauseName(DVar.CKind)
5478 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005479 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005480 continue;
5481 }
5482
Alexey Bataevf29276e2014-06-18 04:14:57 +00005483 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
5484 // OpenMP [2.14.3.5, Restrictions, p.2]
5485 // A list item that is private within a parallel region, or that appears in
5486 // the reduction clause of a parallel construct, must not appear in a
5487 // lastprivate clause on a worksharing construct if any of the corresponding
5488 // worksharing regions ever binds to any of the corresponding parallel
5489 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005490 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00005491 if (isOpenMPWorksharingDirective(CurrDir) &&
5492 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005493 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005494 if (DVar.CKind != OMPC_shared) {
5495 Diag(ELoc, diag::err_omp_required_access)
5496 << getOpenMPClauseName(OMPC_lastprivate)
5497 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005498 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005499 continue;
5500 }
5501 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00005502 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00005503 // A variable of class type (or array thereof) that appears in a
5504 // lastprivate clause requires an accessible, unambiguous default
5505 // constructor for the class type, unless the list item is also specified
5506 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00005507 // A variable of class type (or array thereof) that appears in a
5508 // lastprivate clause requires an accessible, unambiguous copy assignment
5509 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00005510 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00005511 auto *SrcVD = buildVarDecl(*this, DE->getLocStart(),
Alexey Bataev38e89532015-04-16 04:54:05 +00005512 Type.getUnqualifiedType(), ".lastprivate.src");
Alexey Bataev39f915b82015-05-08 10:41:21 +00005513 auto *PseudoSrcExpr = buildDeclRefExpr(
5514 *this, SrcVD, Type.getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00005515 auto *DstVD =
Alexey Bataev39f915b82015-05-08 10:41:21 +00005516 buildVarDecl(*this, DE->getLocStart(), Type, ".lastprivate.dst");
Alexey Bataev38e89532015-04-16 04:54:05 +00005517 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00005518 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00005519 // For arrays generate assignment operation for single element and replace
5520 // it by the original array element in CodeGen.
5521 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
5522 PseudoDstExpr, PseudoSrcExpr);
5523 if (AssignmentOp.isInvalid())
5524 continue;
5525 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
5526 /*DiscardedValue=*/true);
5527 if (AssignmentOp.isInvalid())
5528 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00005529
Alexey Bataev39f915b82015-05-08 10:41:21 +00005530 if (TopDVar.CKind != OMPC_firstprivate)
Alexey Bataevf29276e2014-06-18 04:14:57 +00005531 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005532 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00005533 SrcExprs.push_back(PseudoSrcExpr);
5534 DstExprs.push_back(PseudoDstExpr);
5535 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00005536 }
5537
5538 if (Vars.empty())
5539 return nullptr;
5540
5541 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00005542 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005543}
5544
Alexey Bataev758e55e2013-09-06 18:03:48 +00005545OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
5546 SourceLocation StartLoc,
5547 SourceLocation LParenLoc,
5548 SourceLocation EndLoc) {
5549 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00005550 for (auto &RefExpr : VarList) {
5551 assert(RefExpr && "NULL expr in OpenMP shared clause.");
5552 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00005553 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00005554 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005555 continue;
5556 }
5557
Alexey Bataeved09d242014-05-28 05:53:51 +00005558 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00005559 // OpenMP [2.1, C/C++]
5560 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00005561 // OpenMP [2.14.3.2, Restrictions, p.1]
5562 // A variable that is part of another variable (as an array or structure
5563 // element) cannot appear in a shared unless it is a static data member
5564 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00005565 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005566 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005567 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00005568 continue;
5569 }
5570 Decl *D = DE->getDecl();
5571 VarDecl *VD = cast<VarDecl>(D);
5572
5573 QualType Type = VD->getType();
5574 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5575 // It will be analyzed later.
5576 Vars.push_back(DE);
5577 continue;
5578 }
5579
5580 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
5581 // in a Construct]
5582 // Variables with the predetermined data-sharing attributes may not be
5583 // listed in data-sharing attributes clauses, except for the cases
5584 // listed below. For these exceptions only, listing a predetermined
5585 // variable in a data-sharing attribute clause is allowed and overrides
5586 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005587 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00005588 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
5589 DVar.RefExpr) {
5590 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
5591 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005592 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005593 continue;
5594 }
5595
5596 DSAStack->addDSA(VD, DE, OMPC_shared);
5597 Vars.push_back(DE);
5598 }
5599
Alexey Bataeved09d242014-05-28 05:53:51 +00005600 if (Vars.empty())
5601 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00005602
5603 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
5604}
5605
Alexey Bataevc5e02582014-06-16 07:08:35 +00005606namespace {
5607class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
5608 DSAStackTy *Stack;
5609
5610public:
5611 bool VisitDeclRefExpr(DeclRefExpr *E) {
5612 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005613 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00005614 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
5615 return false;
5616 if (DVar.CKind != OMPC_unknown)
5617 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00005618 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005619 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005620 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00005621 return true;
5622 return false;
5623 }
5624 return false;
5625 }
5626 bool VisitStmt(Stmt *S) {
5627 for (auto Child : S->children()) {
5628 if (Child && Visit(Child))
5629 return true;
5630 }
5631 return false;
5632 }
Alexey Bataev23b69422014-06-18 07:08:49 +00005633 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00005634};
Alexey Bataev23b69422014-06-18 07:08:49 +00005635} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00005636
5637OMPClause *Sema::ActOnOpenMPReductionClause(
5638 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
5639 SourceLocation ColonLoc, SourceLocation EndLoc,
5640 CXXScopeSpec &ReductionIdScopeSpec,
5641 const DeclarationNameInfo &ReductionId) {
5642 // TODO: Allow scope specification search when 'declare reduction' is
5643 // supported.
5644 assert(ReductionIdScopeSpec.isEmpty() &&
5645 "No support for scoped reduction identifiers yet.");
5646
5647 auto DN = ReductionId.getName();
5648 auto OOK = DN.getCXXOverloadedOperator();
5649 BinaryOperatorKind BOK = BO_Comma;
5650
5651 // OpenMP [2.14.3.6, reduction clause]
5652 // C
5653 // reduction-identifier is either an identifier or one of the following
5654 // operators: +, -, *, &, |, ^, && and ||
5655 // C++
5656 // reduction-identifier is either an id-expression or one of the following
5657 // operators: +, -, *, &, |, ^, && and ||
5658 // FIXME: Only 'min' and 'max' identifiers are supported for now.
5659 switch (OOK) {
5660 case OO_Plus:
5661 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005662 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005663 break;
5664 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005665 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005666 break;
5667 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005668 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005669 break;
5670 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005671 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005672 break;
5673 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005674 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005675 break;
5676 case OO_AmpAmp:
5677 BOK = BO_LAnd;
5678 break;
5679 case OO_PipePipe:
5680 BOK = BO_LOr;
5681 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005682 case OO_New:
5683 case OO_Delete:
5684 case OO_Array_New:
5685 case OO_Array_Delete:
5686 case OO_Slash:
5687 case OO_Percent:
5688 case OO_Tilde:
5689 case OO_Exclaim:
5690 case OO_Equal:
5691 case OO_Less:
5692 case OO_Greater:
5693 case OO_LessEqual:
5694 case OO_GreaterEqual:
5695 case OO_PlusEqual:
5696 case OO_MinusEqual:
5697 case OO_StarEqual:
5698 case OO_SlashEqual:
5699 case OO_PercentEqual:
5700 case OO_CaretEqual:
5701 case OO_AmpEqual:
5702 case OO_PipeEqual:
5703 case OO_LessLess:
5704 case OO_GreaterGreater:
5705 case OO_LessLessEqual:
5706 case OO_GreaterGreaterEqual:
5707 case OO_EqualEqual:
5708 case OO_ExclaimEqual:
5709 case OO_PlusPlus:
5710 case OO_MinusMinus:
5711 case OO_Comma:
5712 case OO_ArrowStar:
5713 case OO_Arrow:
5714 case OO_Call:
5715 case OO_Subscript:
5716 case OO_Conditional:
5717 case NUM_OVERLOADED_OPERATORS:
5718 llvm_unreachable("Unexpected reduction identifier");
5719 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00005720 if (auto II = DN.getAsIdentifierInfo()) {
5721 if (II->isStr("max"))
5722 BOK = BO_GT;
5723 else if (II->isStr("min"))
5724 BOK = BO_LT;
5725 }
5726 break;
5727 }
5728 SourceRange ReductionIdRange;
5729 if (ReductionIdScopeSpec.isValid()) {
5730 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
5731 }
5732 ReductionIdRange.setEnd(ReductionId.getEndLoc());
5733 if (BOK == BO_Comma) {
5734 // Not allowed reduction identifier is found.
5735 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
5736 << ReductionIdRange;
5737 return nullptr;
5738 }
5739
5740 SmallVector<Expr *, 8> Vars;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005741 SmallVector<Expr *, 8> LHSs;
5742 SmallVector<Expr *, 8> RHSs;
5743 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005744 for (auto RefExpr : VarList) {
5745 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
5746 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
5747 // It will be analyzed later.
5748 Vars.push_back(RefExpr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005749 LHSs.push_back(nullptr);
5750 RHSs.push_back(nullptr);
5751 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00005752 continue;
5753 }
5754
5755 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
5756 RefExpr->isInstantiationDependent() ||
5757 RefExpr->containsUnexpandedParameterPack()) {
5758 // It will be analyzed later.
5759 Vars.push_back(RefExpr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005760 LHSs.push_back(nullptr);
5761 RHSs.push_back(nullptr);
5762 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00005763 continue;
5764 }
5765
5766 auto ELoc = RefExpr->getExprLoc();
5767 auto ERange = RefExpr->getSourceRange();
5768 // OpenMP [2.1, C/C++]
5769 // A list item is a variable or array section, subject to the restrictions
5770 // specified in Section 2.4 on page 42 and in each of the sections
5771 // describing clauses and directives for which a list appears.
5772 // OpenMP [2.14.3.3, Restrictions, p.1]
5773 // A variable that is part of another variable (as an array or
5774 // structure element) cannot appear in a private clause.
5775 auto DE = dyn_cast<DeclRefExpr>(RefExpr);
5776 if (!DE || !isa<VarDecl>(DE->getDecl())) {
5777 Diag(ELoc, diag::err_omp_expected_var_name) << ERange;
5778 continue;
5779 }
5780 auto D = DE->getDecl();
5781 auto VD = cast<VarDecl>(D);
5782 auto Type = VD->getType();
5783 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
5784 // A variable that appears in a private clause must not have an incomplete
5785 // type or a reference type.
5786 if (RequireCompleteType(ELoc, Type,
5787 diag::err_omp_reduction_incomplete_type))
5788 continue;
5789 // OpenMP [2.14.3.6, reduction clause, Restrictions]
5790 // Arrays may not appear in a reduction clause.
5791 if (Type.getNonReferenceType()->isArrayType()) {
5792 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
5793 bool IsDecl =
5794 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5795 Diag(VD->getLocation(),
5796 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5797 << VD;
5798 continue;
5799 }
5800 // OpenMP [2.14.3.6, reduction clause, Restrictions]
5801 // A list item that appears in a reduction clause must not be
5802 // const-qualified.
5803 if (Type.getNonReferenceType().isConstant(Context)) {
5804 Diag(ELoc, diag::err_omp_const_variable)
5805 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
5806 bool IsDecl =
5807 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5808 Diag(VD->getLocation(),
5809 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5810 << VD;
5811 continue;
5812 }
5813 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
5814 // If a list-item is a reference type then it must bind to the same object
5815 // for all threads of the team.
5816 VarDecl *VDDef = VD->getDefinition();
5817 if (Type->isReferenceType() && VDDef) {
5818 DSARefChecker Check(DSAStack);
5819 if (Check.Visit(VDDef->getInit())) {
5820 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
5821 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
5822 continue;
5823 }
5824 }
5825 // OpenMP [2.14.3.6, reduction clause, Restrictions]
5826 // The type of a list item that appears in a reduction clause must be valid
5827 // for the reduction-identifier. For a max or min reduction in C, the type
5828 // of the list item must be an allowed arithmetic data type: char, int,
5829 // float, double, or _Bool, possibly modified with long, short, signed, or
5830 // unsigned. For a max or min reduction in C++, the type of the list item
5831 // must be an allowed arithmetic data type: char, wchar_t, int, float,
5832 // double, or bool, possibly modified with long, short, signed, or unsigned.
5833 if ((BOK == BO_GT || BOK == BO_LT) &&
5834 !(Type->isScalarType() ||
5835 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
5836 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
5837 << getLangOpts().CPlusPlus;
5838 bool IsDecl =
5839 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5840 Diag(VD->getLocation(),
5841 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5842 << VD;
5843 continue;
5844 }
5845 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
5846 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
5847 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
5848 bool IsDecl =
5849 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5850 Diag(VD->getLocation(),
5851 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5852 << VD;
5853 continue;
5854 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00005855 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
5856 // in a Construct]
5857 // Variables with the predetermined data-sharing attributes may not be
5858 // listed in data-sharing attributes clauses, except for the cases
5859 // listed below. For these exceptions only, listing a predetermined
5860 // variable in a data-sharing attribute clause is allowed and overrides
5861 // the variable's predetermined data-sharing attributes.
5862 // OpenMP [2.14.3.6, Restrictions, p.3]
5863 // Any number of reduction clauses can be specified on the directive,
5864 // but a list item can appear only once in the reduction clauses for that
5865 // directive.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005866 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00005867 if (DVar.CKind == OMPC_reduction) {
5868 Diag(ELoc, diag::err_omp_once_referenced)
5869 << getOpenMPClauseName(OMPC_reduction);
5870 if (DVar.RefExpr) {
5871 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
5872 }
5873 } else if (DVar.CKind != OMPC_unknown) {
5874 Diag(ELoc, diag::err_omp_wrong_dsa)
5875 << getOpenMPClauseName(DVar.CKind)
5876 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005877 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00005878 continue;
5879 }
5880
5881 // OpenMP [2.14.3.6, Restrictions, p.1]
5882 // A list item that appears in a reduction clause of a worksharing
5883 // construct must be shared in the parallel regions to which any of the
5884 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf29276e2014-06-18 04:14:57 +00005885 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataev549210e2014-06-24 04:39:47 +00005886 if (isOpenMPWorksharingDirective(CurrDir) &&
5887 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005888 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005889 if (DVar.CKind != OMPC_shared) {
5890 Diag(ELoc, diag::err_omp_required_access)
5891 << getOpenMPClauseName(OMPC_reduction)
5892 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005893 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005894 continue;
5895 }
5896 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005897 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00005898 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs");
5899 auto *RHSVD = buildVarDecl(*this, ELoc, Type, VD->getName());
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005900 // Add initializer for private variable.
5901 Expr *Init = nullptr;
5902 switch (BOK) {
5903 case BO_Add:
5904 case BO_Xor:
5905 case BO_Or:
5906 case BO_LOr:
5907 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
5908 if (Type->isScalarType() || Type->isAnyComplexType()) {
5909 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00005910 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005911 break;
5912 case BO_Mul:
5913 case BO_LAnd:
5914 if (Type->isScalarType() || Type->isAnyComplexType()) {
5915 // '*' and '&&' reduction ops - initializer is '1'.
5916 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
5917 }
5918 break;
5919 case BO_And: {
5920 // '&' reduction op - initializer is '~0'.
5921 QualType OrigType = Type;
5922 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) {
5923 Type = ComplexTy->getElementType();
5924 }
5925 if (Type->isRealFloatingType()) {
5926 llvm::APFloat InitValue =
5927 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
5928 /*isIEEE=*/true);
5929 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
5930 Type, ELoc);
5931 } else if (Type->isScalarType()) {
5932 auto Size = Context.getTypeSize(Type);
5933 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
5934 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
5935 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
5936 }
5937 if (Init && OrigType->isAnyComplexType()) {
5938 // Init = 0xFFFF + 0xFFFFi;
5939 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
5940 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
5941 }
5942 Type = OrigType;
5943 break;
5944 }
5945 case BO_LT:
5946 case BO_GT: {
5947 // 'min' reduction op - initializer is 'Largest representable number in
5948 // the reduction list item type'.
5949 // 'max' reduction op - initializer is 'Least representable number in
5950 // the reduction list item type'.
5951 if (Type->isIntegerType() || Type->isPointerType()) {
5952 bool IsSigned = Type->hasSignedIntegerRepresentation();
5953 auto Size = Context.getTypeSize(Type);
5954 QualType IntTy =
5955 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
5956 llvm::APInt InitValue =
5957 (BOK != BO_LT)
5958 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
5959 : llvm::APInt::getMinValue(Size)
5960 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
5961 : llvm::APInt::getMaxValue(Size);
5962 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
5963 if (Type->isPointerType()) {
5964 // Cast to pointer type.
5965 auto CastExpr = BuildCStyleCastExpr(
5966 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
5967 SourceLocation(), Init);
5968 if (CastExpr.isInvalid())
5969 continue;
5970 Init = CastExpr.get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00005971 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005972 } else if (Type->isRealFloatingType()) {
5973 llvm::APFloat InitValue = llvm::APFloat::getLargest(
5974 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
5975 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
5976 Type, ELoc);
5977 }
5978 break;
5979 }
5980 case BO_PtrMemD:
5981 case BO_PtrMemI:
5982 case BO_MulAssign:
5983 case BO_Div:
5984 case BO_Rem:
5985 case BO_Sub:
5986 case BO_Shl:
5987 case BO_Shr:
5988 case BO_LE:
5989 case BO_GE:
5990 case BO_EQ:
5991 case BO_NE:
5992 case BO_AndAssign:
5993 case BO_XorAssign:
5994 case BO_OrAssign:
5995 case BO_Assign:
5996 case BO_AddAssign:
5997 case BO_SubAssign:
5998 case BO_DivAssign:
5999 case BO_RemAssign:
6000 case BO_ShlAssign:
6001 case BO_ShrAssign:
6002 case BO_Comma:
6003 llvm_unreachable("Unexpected reduction operation");
6004 }
6005 if (Init) {
6006 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
6007 /*TypeMayContainAuto=*/false);
6008 } else {
6009 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
6010 }
6011 if (!RHSVD->hasInit()) {
6012 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
6013 << ReductionIdRange;
6014 bool IsDecl =
6015 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6016 Diag(VD->getLocation(),
6017 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6018 << VD;
6019 continue;
6020 }
Alexey Bataev39f915b82015-05-08 10:41:21 +00006021 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
6022 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006023 ExprResult ReductionOp =
6024 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
6025 LHSDRE, RHSDRE);
6026 if (ReductionOp.isUsable()) {
Alexey Bataev69a47792015-05-07 03:54:03 +00006027 if (BOK != BO_LT && BOK != BO_GT) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006028 ReductionOp =
6029 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
6030 BO_Assign, LHSDRE, ReductionOp.get());
6031 } else {
6032 auto *ConditionalOp = new (Context) ConditionalOperator(
6033 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
6034 RHSDRE, Type, VK_LValue, OK_Ordinary);
6035 ReductionOp =
6036 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
6037 BO_Assign, LHSDRE, ConditionalOp);
6038 }
6039 if (ReductionOp.isUsable()) {
6040 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00006041 }
6042 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006043 if (ReductionOp.isInvalid())
6044 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006045
6046 DSAStack->addDSA(VD, DE, OMPC_reduction);
6047 Vars.push_back(DE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006048 LHSs.push_back(LHSDRE);
6049 RHSs.push_back(RHSDRE);
6050 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00006051 }
6052
6053 if (Vars.empty())
6054 return nullptr;
6055
6056 return OMPReductionClause::Create(
6057 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006058 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, LHSs,
6059 RHSs, ReductionOps);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006060}
6061
Alexander Musman8dba6642014-04-22 13:09:42 +00006062OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step,
6063 SourceLocation StartLoc,
6064 SourceLocation LParenLoc,
6065 SourceLocation ColonLoc,
6066 SourceLocation EndLoc) {
6067 SmallVector<Expr *, 8> Vars;
Alexander Musman3276a272015-03-21 10:12:56 +00006068 SmallVector<Expr *, 8> Inits;
Alexey Bataeved09d242014-05-28 05:53:51 +00006069 for (auto &RefExpr : VarList) {
6070 assert(RefExpr && "NULL expr in OpenMP linear clause.");
6071 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00006072 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006073 Vars.push_back(RefExpr);
Alexander Musman3276a272015-03-21 10:12:56 +00006074 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00006075 continue;
6076 }
6077
6078 // OpenMP [2.14.3.7, linear clause]
6079 // A list item that appears in a linear clause is subject to the private
6080 // clause semantics described in Section 2.14.3.3 on page 159 except as
6081 // noted. In addition, the value of the new list item on each iteration
6082 // of the associated loop(s) corresponds to the value of the original
6083 // list item before entering the construct plus the logical number of
6084 // the iteration times linear-step.
6085
Alexey Bataeved09d242014-05-28 05:53:51 +00006086 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00006087 // OpenMP [2.1, C/C++]
6088 // A list item is a variable name.
6089 // OpenMP [2.14.3.3, Restrictions, p.1]
6090 // A variable that is part of another variable (as an array or
6091 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00006092 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00006093 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006094 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00006095 continue;
6096 }
6097
6098 VarDecl *VD = cast<VarDecl>(DE->getDecl());
6099
6100 // OpenMP [2.14.3.7, linear clause]
6101 // A list-item cannot appear in more than one linear clause.
6102 // A list-item that appears in a linear clause cannot appear in any
6103 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006104 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00006105 if (DVar.RefExpr) {
6106 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
6107 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006108 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00006109 continue;
6110 }
6111
6112 QualType QType = VD->getType();
6113 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
6114 // It will be analyzed later.
6115 Vars.push_back(DE);
Alexander Musman3276a272015-03-21 10:12:56 +00006116 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00006117 continue;
6118 }
6119
6120 // A variable must not have an incomplete type or a reference type.
6121 if (RequireCompleteType(ELoc, QType,
6122 diag::err_omp_linear_incomplete_type)) {
6123 continue;
6124 }
6125 if (QType->isReferenceType()) {
6126 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
6127 << getOpenMPClauseName(OMPC_linear) << QType;
6128 bool IsDecl =
6129 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6130 Diag(VD->getLocation(),
6131 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6132 << VD;
6133 continue;
6134 }
6135
6136 // A list item must not be const-qualified.
6137 if (QType.isConstant(Context)) {
6138 Diag(ELoc, diag::err_omp_const_variable)
6139 << getOpenMPClauseName(OMPC_linear);
6140 bool IsDecl =
6141 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6142 Diag(VD->getLocation(),
6143 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6144 << VD;
6145 continue;
6146 }
6147
6148 // A list item must be of integral or pointer type.
6149 QType = QType.getUnqualifiedType().getCanonicalType();
6150 const Type *Ty = QType.getTypePtrOrNull();
6151 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
6152 !Ty->isPointerType())) {
6153 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
6154 bool IsDecl =
6155 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6156 Diag(VD->getLocation(),
6157 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6158 << VD;
6159 continue;
6160 }
6161
Alexander Musman3276a272015-03-21 10:12:56 +00006162 // Build var to save initial value.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006163 VarDecl *Init = buildVarDecl(*this, ELoc, QType, ".linear.start");
Alexander Musman3276a272015-03-21 10:12:56 +00006164 AddInitializerToDecl(Init, DefaultLvalueConversion(DE).get(),
6165 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006166 auto InitRef = buildDeclRefExpr(
6167 *this, Init, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman8dba6642014-04-22 13:09:42 +00006168 DSAStack->addDSA(VD, DE, OMPC_linear);
6169 Vars.push_back(DE);
Alexander Musman3276a272015-03-21 10:12:56 +00006170 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00006171 }
6172
6173 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006174 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00006175
6176 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00006177 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00006178 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
6179 !Step->isInstantiationDependent() &&
6180 !Step->containsUnexpandedParameterPack()) {
6181 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006182 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00006183 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006184 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006185 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00006186
Alexander Musman3276a272015-03-21 10:12:56 +00006187 // Build var to save the step value.
6188 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006189 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00006190 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006191 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00006192 ExprResult CalcStep =
6193 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
6194
Alexander Musman8dba6642014-04-22 13:09:42 +00006195 // Warn about zero linear step (it would be probably better specified as
6196 // making corresponding variables 'const').
6197 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00006198 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
6199 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00006200 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
6201 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00006202 if (!IsConstant && CalcStep.isUsable()) {
6203 // Calculate the step beforehand instead of doing this on each iteration.
6204 // (This is not used if the number of iterations may be kfold-ed).
6205 CalcStepExpr = CalcStep.get();
6206 }
Alexander Musman8dba6642014-04-22 13:09:42 +00006207 }
6208
6209 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc,
Alexander Musman3276a272015-03-21 10:12:56 +00006210 Vars, Inits, StepExpr, CalcStepExpr);
6211}
6212
6213static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
6214 Expr *NumIterations, Sema &SemaRef,
6215 Scope *S) {
6216 // Walk the vars and build update/final expressions for the CodeGen.
6217 SmallVector<Expr *, 8> Updates;
6218 SmallVector<Expr *, 8> Finals;
6219 Expr *Step = Clause.getStep();
6220 Expr *CalcStep = Clause.getCalcStep();
6221 // OpenMP [2.14.3.7, linear clause]
6222 // If linear-step is not specified it is assumed to be 1.
6223 if (Step == nullptr)
6224 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
6225 else if (CalcStep)
6226 Step = cast<BinaryOperator>(CalcStep)->getLHS();
6227 bool HasErrors = false;
6228 auto CurInit = Clause.inits().begin();
6229 for (auto &RefExpr : Clause.varlists()) {
6230 Expr *InitExpr = *CurInit;
6231
6232 // Build privatized reference to the current linear var.
6233 auto DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00006234 auto PrivateRef =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006235 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
6236 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
6237 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00006238
6239 // Build update: Var = InitExpr + IV * Step
6240 ExprResult Update =
6241 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), PrivateRef,
6242 InitExpr, IV, Step, /* Subtract */ false);
6243 Update = SemaRef.ActOnFinishFullExpr(Update.get());
6244
6245 // Build final: Var = InitExpr + NumIterations * Step
6246 ExprResult Final =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006247 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), PrivateRef,
6248 InitExpr, NumIterations, Step, /* Subtract */ false);
Alexander Musman3276a272015-03-21 10:12:56 +00006249 Final = SemaRef.ActOnFinishFullExpr(Final.get());
6250 if (!Update.isUsable() || !Final.isUsable()) {
6251 Updates.push_back(nullptr);
6252 Finals.push_back(nullptr);
6253 HasErrors = true;
6254 } else {
6255 Updates.push_back(Update.get());
6256 Finals.push_back(Final.get());
6257 }
6258 ++CurInit;
6259 }
6260 Clause.setUpdates(Updates);
6261 Clause.setFinals(Finals);
6262 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00006263}
6264
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006265OMPClause *Sema::ActOnOpenMPAlignedClause(
6266 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
6267 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
6268
6269 SmallVector<Expr *, 8> Vars;
6270 for (auto &RefExpr : VarList) {
6271 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
6272 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6273 // It will be analyzed later.
6274 Vars.push_back(RefExpr);
6275 continue;
6276 }
6277
6278 SourceLocation ELoc = RefExpr->getExprLoc();
6279 // OpenMP [2.1, C/C++]
6280 // A list item is a variable name.
6281 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
6282 if (!DE || !isa<VarDecl>(DE->getDecl())) {
6283 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
6284 continue;
6285 }
6286
6287 VarDecl *VD = cast<VarDecl>(DE->getDecl());
6288
6289 // OpenMP [2.8.1, simd construct, Restrictions]
6290 // The type of list items appearing in the aligned clause must be
6291 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006292 QualType QType = VD->getType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006293 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006294 const Type *Ty = QType.getTypePtrOrNull();
6295 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
6296 !Ty->isPointerType())) {
6297 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
6298 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
6299 bool IsDecl =
6300 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6301 Diag(VD->getLocation(),
6302 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6303 << VD;
6304 continue;
6305 }
6306
6307 // OpenMP [2.8.1, simd construct, Restrictions]
6308 // A list-item cannot appear in more than one aligned clause.
6309 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
6310 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
6311 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
6312 << getOpenMPClauseName(OMPC_aligned);
6313 continue;
6314 }
6315
6316 Vars.push_back(DE);
6317 }
6318
6319 // OpenMP [2.8.1, simd construct, Description]
6320 // The parameter of the aligned clause, alignment, must be a constant
6321 // positive integer expression.
6322 // If no optional parameter is specified, implementation-defined default
6323 // alignments for SIMD instructions on the target platforms are assumed.
6324 if (Alignment != nullptr) {
6325 ExprResult AlignResult =
6326 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
6327 if (AlignResult.isInvalid())
6328 return nullptr;
6329 Alignment = AlignResult.get();
6330 }
6331 if (Vars.empty())
6332 return nullptr;
6333
6334 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
6335 EndLoc, Vars, Alignment);
6336}
6337
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006338OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
6339 SourceLocation StartLoc,
6340 SourceLocation LParenLoc,
6341 SourceLocation EndLoc) {
6342 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006343 SmallVector<Expr *, 8> SrcExprs;
6344 SmallVector<Expr *, 8> DstExprs;
6345 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00006346 for (auto &RefExpr : VarList) {
6347 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
6348 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006349 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006350 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006351 SrcExprs.push_back(nullptr);
6352 DstExprs.push_back(nullptr);
6353 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006354 continue;
6355 }
6356
Alexey Bataeved09d242014-05-28 05:53:51 +00006357 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006358 // OpenMP [2.1, C/C++]
6359 // A list item is a variable name.
6360 // OpenMP [2.14.4.1, Restrictions, p.1]
6361 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00006362 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006363 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006364 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006365 continue;
6366 }
6367
6368 Decl *D = DE->getDecl();
6369 VarDecl *VD = cast<VarDecl>(D);
6370
6371 QualType Type = VD->getType();
6372 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6373 // It will be analyzed later.
6374 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006375 SrcExprs.push_back(nullptr);
6376 DstExprs.push_back(nullptr);
6377 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006378 continue;
6379 }
6380
6381 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
6382 // A list item that appears in a copyin clause must be threadprivate.
6383 if (!DSAStack->isThreadPrivate(VD)) {
6384 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00006385 << getOpenMPClauseName(OMPC_copyin)
6386 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006387 continue;
6388 }
6389
6390 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
6391 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00006392 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006393 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006394 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00006395 auto *SrcVD = buildVarDecl(*this, DE->getLocStart(),
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006396 ElemType.getUnqualifiedType(), ".copyin.src");
Alexey Bataev39f915b82015-05-08 10:41:21 +00006397 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006398 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
6399 auto *DstVD =
6400 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst");
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006401 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006402 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006403 // For arrays generate assignment operation for single element and replace
6404 // it by the original array element in CodeGen.
6405 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
6406 PseudoDstExpr, PseudoSrcExpr);
6407 if (AssignmentOp.isInvalid())
6408 continue;
6409 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
6410 /*DiscardedValue=*/true);
6411 if (AssignmentOp.isInvalid())
6412 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006413
6414 DSAStack->addDSA(VD, DE, OMPC_copyin);
6415 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006416 SrcExprs.push_back(PseudoSrcExpr);
6417 DstExprs.push_back(PseudoDstExpr);
6418 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006419 }
6420
Alexey Bataeved09d242014-05-28 05:53:51 +00006421 if (Vars.empty())
6422 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006423
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006424 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
6425 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006426}
6427
Alexey Bataevbae9a792014-06-27 10:37:06 +00006428OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
6429 SourceLocation StartLoc,
6430 SourceLocation LParenLoc,
6431 SourceLocation EndLoc) {
6432 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00006433 SmallVector<Expr *, 8> SrcExprs;
6434 SmallVector<Expr *, 8> DstExprs;
6435 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00006436 for (auto &RefExpr : VarList) {
6437 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
6438 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6439 // It will be analyzed later.
6440 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00006441 SrcExprs.push_back(nullptr);
6442 DstExprs.push_back(nullptr);
6443 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00006444 continue;
6445 }
6446
6447 SourceLocation ELoc = RefExpr->getExprLoc();
6448 // OpenMP [2.1, C/C++]
6449 // A list item is a variable name.
6450 // OpenMP [2.14.4.1, Restrictions, p.1]
6451 // A list item that appears in a copyin clause must be threadprivate.
6452 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
6453 if (!DE || !isa<VarDecl>(DE->getDecl())) {
6454 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
6455 continue;
6456 }
6457
6458 Decl *D = DE->getDecl();
6459 VarDecl *VD = cast<VarDecl>(D);
6460
6461 QualType Type = VD->getType();
6462 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6463 // It will be analyzed later.
6464 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00006465 SrcExprs.push_back(nullptr);
6466 DstExprs.push_back(nullptr);
6467 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00006468 continue;
6469 }
6470
6471 // OpenMP [2.14.4.2, Restrictions, p.2]
6472 // A list item that appears in a copyprivate clause may not appear in a
6473 // private or firstprivate clause on the single construct.
6474 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006475 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00006476 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
6477 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00006478 Diag(ELoc, diag::err_omp_wrong_dsa)
6479 << getOpenMPClauseName(DVar.CKind)
6480 << getOpenMPClauseName(OMPC_copyprivate);
6481 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6482 continue;
6483 }
6484
6485 // OpenMP [2.11.4.2, Restrictions, p.1]
6486 // All list items that appear in a copyprivate clause must be either
6487 // threadprivate or private in the enclosing context.
6488 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006489 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00006490 if (DVar.CKind == OMPC_shared) {
6491 Diag(ELoc, diag::err_omp_required_access)
6492 << getOpenMPClauseName(OMPC_copyprivate)
6493 << "threadprivate or private in the enclosing context";
6494 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6495 continue;
6496 }
6497 }
6498 }
6499
Alexey Bataev7a3e5852015-05-19 08:19:24 +00006500 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00006501 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00006502 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006503 << getOpenMPClauseName(OMPC_copyprivate) << Type
6504 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00006505 bool IsDecl =
6506 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6507 Diag(VD->getLocation(),
6508 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6509 << VD;
6510 continue;
6511 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006512
Alexey Bataevbae9a792014-06-27 10:37:06 +00006513 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
6514 // A variable of class type (or array thereof) that appears in a
6515 // copyin clause requires an accessible, unambiguous copy assignment
6516 // operator for the class type.
Alexey Bataev420d45b2015-04-14 05:11:24 +00006517 Type = Context.getBaseElementType(Type).getUnqualifiedType();
6518 auto *SrcVD =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006519 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.src");
Alexey Bataev420d45b2015-04-14 05:11:24 +00006520 auto *PseudoSrcExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006521 buildDeclRefExpr(*this, SrcVD, Type, DE->getExprLoc());
Alexey Bataev420d45b2015-04-14 05:11:24 +00006522 auto *DstVD =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006523 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.dst");
Alexey Bataev420d45b2015-04-14 05:11:24 +00006524 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006525 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataeva63048e2015-03-23 06:18:07 +00006526 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
6527 PseudoDstExpr, PseudoSrcExpr);
6528 if (AssignmentOp.isInvalid())
6529 continue;
6530 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
6531 /*DiscardedValue=*/true);
6532 if (AssignmentOp.isInvalid())
6533 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00006534
6535 // No need to mark vars as copyprivate, they are already threadprivate or
6536 // implicitly private.
6537 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00006538 SrcExprs.push_back(PseudoSrcExpr);
6539 DstExprs.push_back(PseudoDstExpr);
6540 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +00006541 }
6542
6543 if (Vars.empty())
6544 return nullptr;
6545
Alexey Bataeva63048e2015-03-23 06:18:07 +00006546 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
6547 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00006548}
6549
Alexey Bataev6125da92014-07-21 11:26:11 +00006550OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
6551 SourceLocation StartLoc,
6552 SourceLocation LParenLoc,
6553 SourceLocation EndLoc) {
6554 if (VarList.empty())
6555 return nullptr;
6556
6557 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
6558}
Alexey Bataevdea47612014-07-23 07:46:59 +00006559
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006560OMPClause *
6561Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
6562 SourceLocation DepLoc, SourceLocation ColonLoc,
6563 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
6564 SourceLocation LParenLoc, SourceLocation EndLoc) {
6565 if (DepKind == OMPC_DEPEND_unknown) {
6566 std::string Values;
6567 std::string Sep(", ");
6568 for (unsigned i = 0; i < OMPC_DEPEND_unknown; ++i) {
6569 Values += "'";
6570 Values += getOpenMPSimpleClauseTypeName(OMPC_depend, i);
6571 Values += "'";
6572 switch (i) {
6573 case OMPC_DEPEND_unknown - 2:
6574 Values += " or ";
6575 break;
6576 case OMPC_DEPEND_unknown - 1:
6577 break;
6578 default:
6579 Values += Sep;
6580 break;
6581 }
6582 }
6583 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
6584 << Values << getOpenMPClauseName(OMPC_depend);
6585 return nullptr;
6586 }
6587 SmallVector<Expr *, 8> Vars;
6588 for (auto &RefExpr : VarList) {
6589 assert(RefExpr && "NULL expr in OpenMP shared clause.");
6590 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6591 // It will be analyzed later.
6592 Vars.push_back(RefExpr);
6593 continue;
6594 }
6595
6596 SourceLocation ELoc = RefExpr->getExprLoc();
6597 // OpenMP [2.11.1.1, Restrictions, p.3]
6598 // A variable that is part of another variable (such as a field of a
6599 // structure) but is not an array element or an array section cannot appear
6600 // in a depend clause.
6601 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
6602 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
6603 ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
6604 if (!RefExpr->IgnoreParenImpCasts()->isLValue() || (!ASE && !DE) ||
6605 (DE && !isa<VarDecl>(DE->getDecl())) ||
6606 (ASE && !ASE->getBase()->getType()->isAnyPointerType() &&
6607 !ASE->getBase()->getType()->isArrayType())) {
6608 Diag(ELoc, diag::err_omp_expected_var_name_or_array_item)
6609 << RefExpr->getSourceRange();
6610 continue;
6611 }
6612
6613 Vars.push_back(RefExpr->IgnoreParenImpCasts());
6614 }
6615
6616 if (Vars.empty())
6617 return nullptr;
6618
6619 return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
6620 DepLoc, ColonLoc, Vars);
6621}