blob: 422e0e4ca337f31c56dbf0a76cd59a7b29a28753 [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,
Alexey Bataev10e775f2015-07-30 11:36:16 +00002754 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
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 Bataev10e775f2015-07-30 11:36:16 +00002762 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
2763 << getOpenMPDirectiveName(DKind) << NestedLoopCount
2764 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
2765 if (NestedLoopCount > 1) {
2766 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
2767 SemaRef.Diag(DSA.getConstructLoc(),
2768 diag::note_omp_collapse_ordered_expr)
2769 << 2 << CollapseLoopCountExpr->getSourceRange()
2770 << OrderedLoopCountExpr->getSourceRange();
2771 else if (CollapseLoopCountExpr)
2772 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
2773 diag::note_omp_collapse_ordered_expr)
2774 << 0 << CollapseLoopCountExpr->getSourceRange();
2775 else
2776 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
2777 diag::note_omp_collapse_ordered_expr)
2778 << 1 << OrderedLoopCountExpr->getSourceRange();
2779 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002780 return true;
2781 }
2782 assert(For->getBody());
2783
2784 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
2785
2786 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002787 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002788 if (ISC.CheckInit(Init)) {
2789 return true;
2790 }
2791
2792 bool HasErrors = false;
2793
2794 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002795 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002796
2797 // OpenMP [2.6, Canonical Loop Form]
2798 // Var is one of the following:
2799 // A variable of signed or unsigned integer type.
2800 // For C++, a variable of a random access iterator type.
2801 // For C, a variable of a pointer type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002802 auto VarType = Var->getType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002803 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
2804 !VarType->isPointerType() &&
2805 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
2806 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
2807 << SemaRef.getLangOpts().CPlusPlus;
2808 HasErrors = true;
2809 }
2810
Alexey Bataev4acb8592014-07-07 13:01:15 +00002811 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
2812 // Construct
2813 // The loop iteration variable(s) in the associated for-loop(s) of a for or
2814 // parallel for construct is (are) private.
2815 // The loop iteration variable in the associated for-loop of a simd construct
2816 // with just one associated for-loop is linear with a constant-linear-step
2817 // that is the increment of the associated for-loop.
2818 // Exclude loop var from the list of variables with implicitly defined data
2819 // sharing attributes.
Benjamin Kramerad8e0792014-10-10 15:32:48 +00002820 VarsWithImplicitDSA.erase(Var);
Alexey Bataev4acb8592014-07-07 13:01:15 +00002821
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002822 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
2823 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00002824 // The loop iteration variable in the associated for-loop of a simd construct
2825 // with just one associated for-loop may be listed in a linear clause with a
2826 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002827 // The loop iteration variable(s) in the associated for-loop(s) of a for or
2828 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002829 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002830 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
2831 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
2832 // declared in the loop and it is predetermined as a private.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002833 auto PredeterminedCKind =
2834 isOpenMPSimdDirective(DKind)
2835 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
2836 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002837 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataev0c024df2015-05-12 09:02:07 +00002838 DVar.CKind != OMPC_threadprivate && DVar.CKind != PredeterminedCKind) ||
Alexander Musmanf82886e2014-09-18 05:12:34 +00002839 (isOpenMPWorksharingDirective(DKind) && !isOpenMPSimdDirective(DKind) &&
2840 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private &&
Alexey Bataev0c024df2015-05-12 09:02:07 +00002841 DVar.CKind != OMPC_lastprivate && DVar.CKind != OMPC_threadprivate)) &&
2842 ((DVar.CKind != OMPC_private && DVar.CKind != OMPC_threadprivate) ||
2843 DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002844 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00002845 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
2846 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataevf2453a02015-05-06 07:25:08 +00002847 if (DVar.RefExpr == nullptr)
2848 DVar.CKind = PredeterminedCKind;
2849 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, /*IsLoopIterVar=*/true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002850 HasErrors = true;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002851 } else if (LoopVarRefExpr != nullptr) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00002852 // Make the loop iteration variable private (for worksharing constructs),
2853 // linear (for simd directives with the only one associated loop) or
Alexey Bataev10e775f2015-07-30 11:36:16 +00002854 // lastprivate (for simd directives with several collapsed or ordered
2855 // loops).
Alexey Bataev9aba41c2014-11-14 04:08:45 +00002856 if (DVar.CKind == OMPC_unknown)
2857 DVar = DSA.hasDSA(Var, isOpenMPPrivate, MatchesAlways(),
2858 /*FromParent=*/false);
Alexey Bataev9c821032015-04-30 04:23:23 +00002859 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002860 }
2861
Alexey Bataev7ff55242014-06-19 09:13:45 +00002862 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00002863
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002864 // Check test-expr.
2865 HasErrors |= ISC.CheckCond(For->getCond());
2866
2867 // Check incr-expr.
2868 HasErrors |= ISC.CheckInc(For->getInc());
2869
Alexander Musmana5f070a2014-10-01 06:03:56 +00002870 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002871 return HasErrors;
2872
Alexander Musmana5f070a2014-10-01 06:03:56 +00002873 // Build the loop's iteration space representation.
Alexey Bataev62dbb972015-04-22 11:59:37 +00002874 ResultIterSpace.PreCond = ISC.BuildPreCond(DSA.getCurScope(), For->getCond());
Alexander Musman174b3ca2014-10-06 11:16:29 +00002875 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
2876 DSA.getCurScope(), /* LimitedType */ isOpenMPWorksharingDirective(DKind));
Alexander Musmana5f070a2014-10-01 06:03:56 +00002877 ResultIterSpace.CounterVar = ISC.BuildCounterVar();
2878 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
2879 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
2880 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
2881 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
2882 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
2883 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
2884
Alexey Bataev62dbb972015-04-22 11:59:37 +00002885 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
2886 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00002887 ResultIterSpace.CounterVar == nullptr ||
2888 ResultIterSpace.CounterInit == nullptr ||
2889 ResultIterSpace.CounterStep == nullptr);
2890
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002891 return HasErrors;
2892}
2893
Alexander Musmana5f070a2014-10-01 06:03:56 +00002894/// \brief Build 'VarRef = Start + Iter * Step'.
2895static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S,
2896 SourceLocation Loc, ExprResult VarRef,
2897 ExprResult Start, ExprResult Iter,
2898 ExprResult Step, bool Subtract) {
2899 // Add parentheses (for debugging purposes only).
2900 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
2901 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
2902 !Step.isUsable())
2903 return ExprError();
2904
2905 ExprResult Update = SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(),
2906 Step.get()->IgnoreImplicit());
2907 if (!Update.isUsable())
2908 return ExprError();
2909
2910 // Build 'VarRef = Start + Iter * Step'.
2911 Update = SemaRef.BuildBinOp(S, Loc, (Subtract ? BO_Sub : BO_Add),
2912 Start.get()->IgnoreImplicit(), Update.get());
2913 if (!Update.isUsable())
2914 return ExprError();
2915
2916 Update = SemaRef.PerformImplicitConversion(
2917 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
2918 if (!Update.isUsable())
2919 return ExprError();
2920
2921 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
2922 return Update;
2923}
2924
2925/// \brief Convert integer expression \a E to make it have at least \a Bits
2926/// bits.
2927static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
2928 Sema &SemaRef) {
2929 if (E == nullptr)
2930 return ExprError();
2931 auto &C = SemaRef.Context;
2932 QualType OldType = E->getType();
2933 unsigned HasBits = C.getTypeSize(OldType);
2934 if (HasBits >= Bits)
2935 return ExprResult(E);
2936 // OK to convert to signed, because new type has more bits than old.
2937 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
2938 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
2939 true);
2940}
2941
2942/// \brief Check if the given expression \a E is a constant integer that fits
2943/// into \a Bits bits.
2944static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
2945 if (E == nullptr)
2946 return false;
2947 llvm::APSInt Result;
2948 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
2949 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
2950 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002951}
2952
2953/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00002954/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
2955/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002956static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00002957CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
2958 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
2959 DSAStackTy &DSA,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002960 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00002961 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002962 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00002963 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002964 // Found 'collapse' clause - calculate collapse number.
2965 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00002966 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
2967 NestedLoopCount += Result.getLimitedValue() - 1;
2968 }
2969 if (OrderedLoopCountExpr) {
2970 // Found 'ordered' clause - calculate collapse number.
2971 llvm::APSInt Result;
2972 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
2973 NestedLoopCount += Result.getLimitedValue() - 1;
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002974 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002975 // This is helper routine for loop directives (e.g., 'for', 'simd',
2976 // 'for simd', etc.).
Alexander Musmana5f070a2014-10-01 06:03:56 +00002977 SmallVector<LoopIterationSpace, 4> IterSpaces;
2978 IterSpaces.resize(NestedLoopCount);
2979 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002980 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002981 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00002982 NestedLoopCount, CollapseLoopCountExpr,
2983 OrderedLoopCountExpr, VarsWithImplicitDSA,
2984 IterSpaces[Cnt]))
Alexey Bataevabfc0692014-06-25 06:52:00 +00002985 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002986 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00002987 // OpenMP [2.8.1, simd construct, Restrictions]
2988 // All loops associated with the construct must be perfectly nested; that
2989 // is, there must be no intervening code nor any OpenMP directive between
2990 // any two loops.
2991 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002992 }
2993
Alexander Musmana5f070a2014-10-01 06:03:56 +00002994 Built.clear(/* size */ NestedLoopCount);
2995
2996 if (SemaRef.CurContext->isDependentContext())
2997 return NestedLoopCount;
2998
2999 // An example of what is generated for the following code:
3000 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00003001 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00003002 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003003 // for (k = 0; k < NK; ++k)
3004 // for (j = J0; j < NJ; j+=2) {
3005 // <loop body>
3006 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003007 //
3008 // We generate the code below.
3009 // Note: the loop body may be outlined in CodeGen.
3010 // Note: some counters may be C++ classes, operator- is used to find number of
3011 // iterations and operator+= to calculate counter value.
3012 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
3013 // or i64 is currently supported).
3014 //
3015 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
3016 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
3017 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
3018 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
3019 // // similar updates for vars in clauses (e.g. 'linear')
3020 // <loop body (using local i and j)>
3021 // }
3022 // i = NI; // assign final values of counters
3023 // j = NJ;
3024 //
3025
3026 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
3027 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00003028 // Precondition tests if there is at least one iteration (all conditions are
3029 // true).
3030 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003031 auto N0 = IterSpaces[0].NumIterations;
3032 ExprResult LastIteration32 = WidenIterationCount(32 /* Bits */, N0, SemaRef);
3033 ExprResult LastIteration64 = WidenIterationCount(64 /* Bits */, N0, SemaRef);
3034
3035 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
3036 return NestedLoopCount;
3037
3038 auto &C = SemaRef.Context;
3039 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
3040
3041 Scope *CurScope = DSA.getCurScope();
3042 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003043 if (PreCond.isUsable()) {
3044 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
3045 PreCond.get(), IterSpaces[Cnt].PreCond);
3046 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003047 auto N = IterSpaces[Cnt].NumIterations;
3048 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
3049 if (LastIteration32.isUsable())
3050 LastIteration32 = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_Mul,
3051 LastIteration32.get(), N);
3052 if (LastIteration64.isUsable())
3053 LastIteration64 = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_Mul,
3054 LastIteration64.get(), N);
3055 }
3056
3057 // Choose either the 32-bit or 64-bit version.
3058 ExprResult LastIteration = LastIteration64;
3059 if (LastIteration32.isUsable() &&
3060 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
3061 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
3062 FitsInto(
3063 32 /* Bits */,
3064 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
3065 LastIteration64.get(), SemaRef)))
3066 LastIteration = LastIteration32;
3067
3068 if (!LastIteration.isUsable())
3069 return 0;
3070
3071 // Save the number of iterations.
3072 ExprResult NumIterations = LastIteration;
3073 {
3074 LastIteration = SemaRef.BuildBinOp(
3075 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
3076 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3077 if (!LastIteration.isUsable())
3078 return 0;
3079 }
3080
3081 // Calculate the last iteration number beforehand instead of doing this on
3082 // each iteration. Do not do this if the number of iterations may be kfold-ed.
3083 llvm::APSInt Result;
3084 bool IsConstant =
3085 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
3086 ExprResult CalcLastIteration;
3087 if (!IsConstant) {
3088 SourceLocation SaveLoc;
3089 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00003090 buildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00003091 ".omp.last.iteration");
Alexey Bataev39f915b82015-05-08 10:41:21 +00003092 ExprResult SaveRef = buildDeclRefExpr(
3093 SemaRef, SaveVar, LastIteration.get()->getType(), SaveLoc);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003094 CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign,
3095 SaveRef.get(), LastIteration.get());
3096 LastIteration = SaveRef;
3097
3098 // Prepare SaveRef + 1.
3099 NumIterations = SemaRef.BuildBinOp(
3100 CurScope, SaveLoc, BO_Add, SaveRef.get(),
3101 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3102 if (!NumIterations.isUsable())
3103 return 0;
3104 }
3105
3106 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
3107
Alexander Musmanc6388682014-12-15 07:07:06 +00003108 QualType VType = LastIteration.get()->getType();
3109 // Build variables passed into runtime, nesessary for worksharing directives.
3110 ExprResult LB, UB, IL, ST, EUB;
3111 if (isOpenMPWorksharingDirective(DKind)) {
3112 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00003113 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
3114 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003115 SemaRef.AddInitializerToDecl(
3116 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
3117 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3118
3119 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00003120 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
3121 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003122 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
3123 /*DirectInit*/ false,
3124 /*TypeMayContainAuto*/ false);
3125
3126 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
3127 // This will be used to implement clause 'lastprivate'.
3128 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00003129 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
3130 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003131 SemaRef.AddInitializerToDecl(
3132 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
3133 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3134
3135 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev39f915b82015-05-08 10:41:21 +00003136 VarDecl *STDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.stride");
3137 ST = buildDeclRefExpr(SemaRef, STDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003138 SemaRef.AddInitializerToDecl(
3139 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
3140 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3141
3142 // Build expression: UB = min(UB, LastIteration)
3143 // It is nesessary for CodeGen of directives with static scheduling.
3144 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
3145 UB.get(), LastIteration.get());
3146 ExprResult CondOp = SemaRef.ActOnConditionalOp(
3147 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
3148 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
3149 CondOp.get());
3150 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
3151 }
3152
3153 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003154 ExprResult IV;
3155 ExprResult Init;
3156 {
Alexey Bataev39f915b82015-05-08 10:41:21 +00003157 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.iv");
3158 IV = buildDeclRefExpr(SemaRef, IVDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003159 Expr *RHS = isOpenMPWorksharingDirective(DKind)
3160 ? LB.get()
3161 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
3162 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
3163 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003164 }
3165
Alexander Musmanc6388682014-12-15 07:07:06 +00003166 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003167 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00003168 ExprResult Cond =
3169 isOpenMPWorksharingDirective(DKind)
3170 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
3171 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
3172 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003173
3174 // Loop increment (IV = IV + 1)
3175 SourceLocation IncLoc;
3176 ExprResult Inc =
3177 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
3178 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
3179 if (!Inc.isUsable())
3180 return 0;
3181 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00003182 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
3183 if (!Inc.isUsable())
3184 return 0;
3185
3186 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
3187 // Used for directives with static scheduling.
3188 ExprResult NextLB, NextUB;
3189 if (isOpenMPWorksharingDirective(DKind)) {
3190 // LB + ST
3191 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
3192 if (!NextLB.isUsable())
3193 return 0;
3194 // LB = LB + ST
3195 NextLB =
3196 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
3197 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
3198 if (!NextLB.isUsable())
3199 return 0;
3200 // UB + ST
3201 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
3202 if (!NextUB.isUsable())
3203 return 0;
3204 // UB = UB + ST
3205 NextUB =
3206 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
3207 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
3208 if (!NextUB.isUsable())
3209 return 0;
3210 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003211
3212 // Build updates and final values of the loop counters.
3213 bool HasErrors = false;
3214 Built.Counters.resize(NestedLoopCount);
3215 Built.Updates.resize(NestedLoopCount);
3216 Built.Finals.resize(NestedLoopCount);
3217 {
3218 ExprResult Div;
3219 // Go from inner nested loop to outer.
3220 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
3221 LoopIterationSpace &IS = IterSpaces[Cnt];
3222 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
3223 // Build: Iter = (IV / Div) % IS.NumIters
3224 // where Div is product of previous iterations' IS.NumIters.
3225 ExprResult Iter;
3226 if (Div.isUsable()) {
3227 Iter =
3228 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
3229 } else {
3230 Iter = IV;
3231 assert((Cnt == (int)NestedLoopCount - 1) &&
3232 "unusable div expected on first iteration only");
3233 }
3234
3235 if (Cnt != 0 && Iter.isUsable())
3236 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
3237 IS.NumIterations);
3238 if (!Iter.isUsable()) {
3239 HasErrors = true;
3240 break;
3241 }
3242
Alexey Bataev39f915b82015-05-08 10:41:21 +00003243 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
3244 auto *CounterVar = buildDeclRefExpr(
3245 SemaRef, cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()),
3246 IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
3247 /*RefersToCapture=*/true);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003248 ExprResult Update =
Alexey Bataev39f915b82015-05-08 10:41:21 +00003249 BuildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003250 IS.CounterInit, Iter, IS.CounterStep, IS.Subtract);
3251 if (!Update.isUsable()) {
3252 HasErrors = true;
3253 break;
3254 }
3255
3256 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
3257 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00003258 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003259 IS.NumIterations, IS.CounterStep, IS.Subtract);
3260 if (!Final.isUsable()) {
3261 HasErrors = true;
3262 break;
3263 }
3264
3265 // Build Div for the next iteration: Div <- Div * IS.NumIters
3266 if (Cnt != 0) {
3267 if (Div.isUnset())
3268 Div = IS.NumIterations;
3269 else
3270 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
3271 IS.NumIterations);
3272
3273 // Add parentheses (for debugging purposes only).
3274 if (Div.isUsable())
3275 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
3276 if (!Div.isUsable()) {
3277 HasErrors = true;
3278 break;
3279 }
3280 }
3281 if (!Update.isUsable() || !Final.isUsable()) {
3282 HasErrors = true;
3283 break;
3284 }
3285 // Save results
3286 Built.Counters[Cnt] = IS.CounterVar;
3287 Built.Updates[Cnt] = Update.get();
3288 Built.Finals[Cnt] = Final.get();
3289 }
3290 }
3291
3292 if (HasErrors)
3293 return 0;
3294
3295 // Save results
3296 Built.IterationVarRef = IV.get();
3297 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00003298 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003299 Built.CalcLastIteration =
3300 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003301 Built.PreCond = PreCond.get();
3302 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003303 Built.Init = Init.get();
3304 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00003305 Built.LB = LB.get();
3306 Built.UB = UB.get();
3307 Built.IL = IL.get();
3308 Built.ST = ST.get();
3309 Built.EUB = EUB.get();
3310 Built.NLB = NextLB.get();
3311 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003312
Alexey Bataevabfc0692014-06-25 06:52:00 +00003313 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003314}
3315
Alexey Bataev10e775f2015-07-30 11:36:16 +00003316static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Alexey Bataevc925aa32015-04-27 08:00:32 +00003317 auto &&CollapseFilter = [](const OMPClause *C) -> bool {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003318 return C->getClauseKind() == OMPC_collapse;
3319 };
3320 OMPExecutableDirective::filtered_clause_iterator<decltype(CollapseFilter)> I(
Alexey Bataevc925aa32015-04-27 08:00:32 +00003321 Clauses, std::move(CollapseFilter));
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003322 if (I)
3323 return cast<OMPCollapseClause>(*I)->getNumForLoops();
3324 return nullptr;
3325}
3326
Alexey Bataev10e775f2015-07-30 11:36:16 +00003327static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
3328 auto &&OrderedFilter = [](const OMPClause *C) -> bool {
3329 return C->getClauseKind() == OMPC_ordered;
3330 };
3331 OMPExecutableDirective::filtered_clause_iterator<decltype(OrderedFilter)> I(
3332 Clauses, std::move(OrderedFilter));
3333 if (I)
3334 return cast<OMPOrderedClause>(*I)->getNumForLoops();
3335 return nullptr;
3336}
3337
Alexey Bataev4acb8592014-07-07 13:01:15 +00003338StmtResult Sema::ActOnOpenMPSimdDirective(
3339 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3340 SourceLocation EndLoc,
3341 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmanc6388682014-12-15 07:07:06 +00003342 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003343 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
3344 // define the nested loops number.
3345 unsigned NestedLoopCount = CheckOpenMPLoop(
3346 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
3347 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00003348 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003349 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003350
Alexander Musmana5f070a2014-10-01 06:03:56 +00003351 assert((CurContext->isDependentContext() || B.builtAll()) &&
3352 "omp simd loop exprs were not built");
3353
Alexander Musman3276a272015-03-21 10:12:56 +00003354 if (!CurContext->isDependentContext()) {
3355 // Finalize the clauses that need pre-built expressions for CodeGen.
3356 for (auto C : Clauses) {
3357 if (auto LC = dyn_cast<OMPLinearClause>(C))
3358 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3359 B.NumIterations, *this, CurScope))
3360 return StmtError();
3361 }
3362 }
3363
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003364 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003365 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
3366 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003367}
3368
Alexey Bataev4acb8592014-07-07 13:01:15 +00003369StmtResult Sema::ActOnOpenMPForDirective(
3370 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3371 SourceLocation EndLoc,
3372 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmanc6388682014-12-15 07:07:06 +00003373 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003374 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
3375 // define the nested loops number.
3376 unsigned NestedLoopCount = CheckOpenMPLoop(
3377 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
3378 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00003379 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00003380 return StmtError();
3381
Alexander Musmana5f070a2014-10-01 06:03:56 +00003382 assert((CurContext->isDependentContext() || B.builtAll()) &&
3383 "omp for loop exprs were not built");
3384
Alexey Bataevf29276e2014-06-18 04:14:57 +00003385 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003386 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
3387 Clauses, AStmt, B);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003388}
3389
Alexander Musmanf82886e2014-09-18 05:12:34 +00003390StmtResult Sema::ActOnOpenMPForSimdDirective(
3391 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3392 SourceLocation EndLoc,
3393 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmanc6388682014-12-15 07:07:06 +00003394 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003395 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
3396 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00003397 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00003398 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
3399 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
3400 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00003401 if (NestedLoopCount == 0)
3402 return StmtError();
3403
Alexander Musmanc6388682014-12-15 07:07:06 +00003404 assert((CurContext->isDependentContext() || B.builtAll()) &&
3405 "omp for simd loop exprs were not built");
3406
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00003407 if (!CurContext->isDependentContext()) {
3408 // Finalize the clauses that need pre-built expressions for CodeGen.
3409 for (auto C : Clauses) {
3410 if (auto LC = dyn_cast<OMPLinearClause>(C))
3411 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3412 B.NumIterations, *this, CurScope))
3413 return StmtError();
3414 }
3415 }
3416
Alexander Musmanf82886e2014-09-18 05:12:34 +00003417 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003418 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
3419 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00003420}
3421
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003422StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
3423 Stmt *AStmt,
3424 SourceLocation StartLoc,
3425 SourceLocation EndLoc) {
3426 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3427 auto BaseStmt = AStmt;
3428 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
3429 BaseStmt = CS->getCapturedStmt();
3430 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
3431 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00003432 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003433 return StmtError();
3434 // All associated statements must be '#pragma omp section' except for
3435 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00003436 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003437 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
3438 if (SectionStmt)
3439 Diag(SectionStmt->getLocStart(),
3440 diag::err_omp_sections_substmt_not_section);
3441 return StmtError();
3442 }
3443 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003444 } else {
3445 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
3446 return StmtError();
3447 }
3448
3449 getCurFunction()->setHasBranchProtectedScope();
3450
3451 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses,
3452 AStmt);
3453}
3454
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003455StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
3456 SourceLocation StartLoc,
3457 SourceLocation EndLoc) {
3458 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3459
3460 getCurFunction()->setHasBranchProtectedScope();
3461
3462 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt);
3463}
3464
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003465StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
3466 Stmt *AStmt,
3467 SourceLocation StartLoc,
3468 SourceLocation EndLoc) {
Alexey Bataev74a05c92014-07-15 02:55:09 +00003469 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3470
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003471 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00003472
Alexey Bataev3255bf32015-01-19 05:20:46 +00003473 // OpenMP [2.7.3, single Construct, Restrictions]
3474 // The copyprivate clause must not be used with the nowait clause.
3475 OMPClause *Nowait = nullptr;
3476 OMPClause *Copyprivate = nullptr;
3477 for (auto *Clause : Clauses) {
3478 if (Clause->getClauseKind() == OMPC_nowait)
3479 Nowait = Clause;
3480 else if (Clause->getClauseKind() == OMPC_copyprivate)
3481 Copyprivate = Clause;
3482 if (Copyprivate && Nowait) {
3483 Diag(Copyprivate->getLocStart(),
3484 diag::err_omp_single_copyprivate_with_nowait);
3485 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
3486 return StmtError();
3487 }
3488 }
3489
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003490 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3491}
3492
Alexander Musman80c22892014-07-17 08:54:58 +00003493StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
3494 SourceLocation StartLoc,
3495 SourceLocation EndLoc) {
3496 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3497
3498 getCurFunction()->setHasBranchProtectedScope();
3499
3500 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
3501}
3502
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003503StmtResult
3504Sema::ActOnOpenMPCriticalDirective(const DeclarationNameInfo &DirName,
3505 Stmt *AStmt, SourceLocation StartLoc,
3506 SourceLocation EndLoc) {
3507 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3508
3509 getCurFunction()->setHasBranchProtectedScope();
3510
3511 return OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
3512 AStmt);
3513}
3514
Alexey Bataev4acb8592014-07-07 13:01:15 +00003515StmtResult Sema::ActOnOpenMPParallelForDirective(
3516 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3517 SourceLocation EndLoc,
3518 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
3519 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3520 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3521 // 1.2.2 OpenMP Language Terminology
3522 // Structured block - An executable statement with a single entry at the
3523 // top and a single exit at the bottom.
3524 // The point of exit cannot be a branch out of the structured block.
3525 // longjmp() and throw() must not violate the entry/exit criteria.
3526 CS->getCapturedDecl()->setNothrow();
3527
Alexander Musmanc6388682014-12-15 07:07:06 +00003528 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003529 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
3530 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003531 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00003532 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
3533 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
3534 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003535 if (NestedLoopCount == 0)
3536 return StmtError();
3537
Alexander Musmana5f070a2014-10-01 06:03:56 +00003538 assert((CurContext->isDependentContext() || B.builtAll()) &&
3539 "omp parallel for loop exprs were not built");
3540
Alexey Bataev4acb8592014-07-07 13:01:15 +00003541 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003542 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
3543 NestedLoopCount, Clauses, AStmt, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003544}
3545
Alexander Musmane4e893b2014-09-23 09:33:00 +00003546StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
3547 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3548 SourceLocation EndLoc,
3549 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
3550 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3551 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3552 // 1.2.2 OpenMP Language Terminology
3553 // Structured block - An executable statement with a single entry at the
3554 // top and a single exit at the bottom.
3555 // The point of exit cannot be a branch out of the structured block.
3556 // longjmp() and throw() must not violate the entry/exit criteria.
3557 CS->getCapturedDecl()->setNothrow();
3558
Alexander Musmanc6388682014-12-15 07:07:06 +00003559 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003560 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
3561 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00003562 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00003563 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
3564 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
3565 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003566 if (NestedLoopCount == 0)
3567 return StmtError();
3568
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00003569 if (!CurContext->isDependentContext()) {
3570 // Finalize the clauses that need pre-built expressions for CodeGen.
3571 for (auto C : Clauses) {
3572 if (auto LC = dyn_cast<OMPLinearClause>(C))
3573 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3574 B.NumIterations, *this, CurScope))
3575 return StmtError();
3576 }
3577 }
3578
Alexander Musmane4e893b2014-09-23 09:33:00 +00003579 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003580 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00003581 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003582}
3583
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003584StmtResult
3585Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
3586 Stmt *AStmt, SourceLocation StartLoc,
3587 SourceLocation EndLoc) {
3588 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3589 auto BaseStmt = AStmt;
3590 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
3591 BaseStmt = CS->getCapturedStmt();
3592 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
3593 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00003594 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003595 return StmtError();
3596 // All associated statements must be '#pragma omp section' except for
3597 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00003598 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003599 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
3600 if (SectionStmt)
3601 Diag(SectionStmt->getLocStart(),
3602 diag::err_omp_parallel_sections_substmt_not_section);
3603 return StmtError();
3604 }
3605 }
3606 } else {
3607 Diag(AStmt->getLocStart(),
3608 diag::err_omp_parallel_sections_not_compound_stmt);
3609 return StmtError();
3610 }
3611
3612 getCurFunction()->setHasBranchProtectedScope();
3613
3614 return OMPParallelSectionsDirective::Create(Context, StartLoc, EndLoc,
3615 Clauses, AStmt);
3616}
3617
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003618StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
3619 Stmt *AStmt, SourceLocation StartLoc,
3620 SourceLocation EndLoc) {
3621 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3622 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3623 // 1.2.2 OpenMP Language Terminology
3624 // Structured block - An executable statement with a single entry at the
3625 // top and a single exit at the bottom.
3626 // The point of exit cannot be a branch out of the structured block.
3627 // longjmp() and throw() must not violate the entry/exit criteria.
3628 CS->getCapturedDecl()->setNothrow();
3629
3630 getCurFunction()->setHasBranchProtectedScope();
3631
3632 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3633}
3634
Alexey Bataev68446b72014-07-18 07:47:19 +00003635StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
3636 SourceLocation EndLoc) {
3637 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
3638}
3639
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003640StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
3641 SourceLocation EndLoc) {
3642 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
3643}
3644
Alexey Bataev2df347a2014-07-18 10:17:07 +00003645StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
3646 SourceLocation EndLoc) {
3647 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
3648}
3649
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003650StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
3651 SourceLocation StartLoc,
3652 SourceLocation EndLoc) {
3653 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3654
3655 getCurFunction()->setHasBranchProtectedScope();
3656
3657 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
3658}
3659
Alexey Bataev6125da92014-07-21 11:26:11 +00003660StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
3661 SourceLocation StartLoc,
3662 SourceLocation EndLoc) {
3663 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
3664 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
3665}
3666
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003667StmtResult Sema::ActOnOpenMPOrderedDirective(Stmt *AStmt,
3668 SourceLocation StartLoc,
3669 SourceLocation EndLoc) {
3670 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3671
3672 getCurFunction()->setHasBranchProtectedScope();
3673
3674 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, AStmt);
3675}
3676
Alexey Bataev1d160b12015-03-13 12:27:31 +00003677namespace {
3678/// \brief Helper class for checking expression in 'omp atomic [update]'
3679/// construct.
3680class OpenMPAtomicUpdateChecker {
3681 /// \brief Error results for atomic update expressions.
3682 enum ExprAnalysisErrorCode {
3683 /// \brief A statement is not an expression statement.
3684 NotAnExpression,
3685 /// \brief Expression is not builtin binary or unary operation.
3686 NotABinaryOrUnaryExpression,
3687 /// \brief Unary operation is not post-/pre- increment/decrement operation.
3688 NotAnUnaryIncDecExpression,
3689 /// \brief An expression is not of scalar type.
3690 NotAScalarType,
3691 /// \brief A binary operation is not an assignment operation.
3692 NotAnAssignmentOp,
3693 /// \brief RHS part of the binary operation is not a binary expression.
3694 NotABinaryExpression,
3695 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
3696 /// expression.
3697 NotABinaryOperator,
3698 /// \brief RHS binary operation does not have reference to the updated LHS
3699 /// part.
3700 NotAnUpdateExpression,
3701 /// \brief No errors is found.
3702 NoError
3703 };
3704 /// \brief Reference to Sema.
3705 Sema &SemaRef;
3706 /// \brief A location for note diagnostics (when error is found).
3707 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003708 /// \brief 'x' lvalue part of the source atomic expression.
3709 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003710 /// \brief 'expr' rvalue part of the source atomic expression.
3711 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00003712 /// \brief Helper expression of the form
3713 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
3714 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
3715 Expr *UpdateExpr;
3716 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
3717 /// important for non-associative operations.
3718 bool IsXLHSInRHSPart;
3719 BinaryOperatorKind Op;
3720 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00003721 /// \brief true if the source expression is a postfix unary operation, false
3722 /// if it is a prefix unary operation.
3723 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003724
3725public:
3726 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00003727 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00003728 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00003729 /// \brief Check specified statement that it is suitable for 'atomic update'
3730 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00003731 /// expression. If DiagId and NoteId == 0, then only check is performed
3732 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00003733 /// \param DiagId Diagnostic which should be emitted if error is found.
3734 /// \param NoteId Diagnostic note for the main error message.
3735 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00003736 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00003737 /// \brief Return the 'x' lvalue part of the source atomic expression.
3738 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00003739 /// \brief Return the 'expr' rvalue part of the source atomic expression.
3740 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00003741 /// \brief Return the update expression used in calculation of the updated
3742 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
3743 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
3744 Expr *getUpdateExpr() const { return UpdateExpr; }
3745 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
3746 /// false otherwise.
3747 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
3748
Alexey Bataevb78ca832015-04-01 03:33:17 +00003749 /// \brief true if the source expression is a postfix unary operation, false
3750 /// if it is a prefix unary operation.
3751 bool isPostfixUpdate() const { return IsPostfixUpdate; }
3752
Alexey Bataev1d160b12015-03-13 12:27:31 +00003753private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00003754 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
3755 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00003756};
3757} // namespace
3758
3759bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
3760 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
3761 ExprAnalysisErrorCode ErrorFound = NoError;
3762 SourceLocation ErrorLoc, NoteLoc;
3763 SourceRange ErrorRange, NoteRange;
3764 // Allowed constructs are:
3765 // x = x binop expr;
3766 // x = expr binop x;
3767 if (AtomicBinOp->getOpcode() == BO_Assign) {
3768 X = AtomicBinOp->getLHS();
3769 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
3770 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
3771 if (AtomicInnerBinOp->isMultiplicativeOp() ||
3772 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
3773 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00003774 Op = AtomicInnerBinOp->getOpcode();
3775 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00003776 auto *LHS = AtomicInnerBinOp->getLHS();
3777 auto *RHS = AtomicInnerBinOp->getRHS();
3778 llvm::FoldingSetNodeID XId, LHSId, RHSId;
3779 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
3780 /*Canonical=*/true);
3781 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
3782 /*Canonical=*/true);
3783 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
3784 /*Canonical=*/true);
3785 if (XId == LHSId) {
3786 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00003787 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003788 } else if (XId == RHSId) {
3789 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00003790 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003791 } else {
3792 ErrorLoc = AtomicInnerBinOp->getExprLoc();
3793 ErrorRange = AtomicInnerBinOp->getSourceRange();
3794 NoteLoc = X->getExprLoc();
3795 NoteRange = X->getSourceRange();
3796 ErrorFound = NotAnUpdateExpression;
3797 }
3798 } else {
3799 ErrorLoc = AtomicInnerBinOp->getExprLoc();
3800 ErrorRange = AtomicInnerBinOp->getSourceRange();
3801 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
3802 NoteRange = SourceRange(NoteLoc, NoteLoc);
3803 ErrorFound = NotABinaryOperator;
3804 }
3805 } else {
3806 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
3807 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
3808 ErrorFound = NotABinaryExpression;
3809 }
3810 } else {
3811 ErrorLoc = AtomicBinOp->getExprLoc();
3812 ErrorRange = AtomicBinOp->getSourceRange();
3813 NoteLoc = AtomicBinOp->getOperatorLoc();
3814 NoteRange = SourceRange(NoteLoc, NoteLoc);
3815 ErrorFound = NotAnAssignmentOp;
3816 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00003817 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00003818 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
3819 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
3820 return true;
3821 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00003822 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00003823 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003824}
3825
3826bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
3827 unsigned NoteId) {
3828 ExprAnalysisErrorCode ErrorFound = NoError;
3829 SourceLocation ErrorLoc, NoteLoc;
3830 SourceRange ErrorRange, NoteRange;
3831 // Allowed constructs are:
3832 // x++;
3833 // x--;
3834 // ++x;
3835 // --x;
3836 // x binop= expr;
3837 // x = x binop expr;
3838 // x = expr binop x;
3839 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
3840 AtomicBody = AtomicBody->IgnoreParenImpCasts();
3841 if (AtomicBody->getType()->isScalarType() ||
3842 AtomicBody->isInstantiationDependent()) {
3843 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
3844 AtomicBody->IgnoreParenImpCasts())) {
3845 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00003846 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00003847 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00003848 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00003849 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00003850 X = AtomicCompAssignOp->getLHS();
3851 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003852 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
3853 AtomicBody->IgnoreParenImpCasts())) {
3854 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00003855 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
3856 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003857 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00003858 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
3859 // Check for Unary Operation
3860 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00003861 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00003862 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
3863 OpLoc = AtomicUnaryOp->getOperatorLoc();
3864 X = AtomicUnaryOp->getSubExpr();
3865 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
3866 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003867 } else {
3868 ErrorFound = NotAnUnaryIncDecExpression;
3869 ErrorLoc = AtomicUnaryOp->getExprLoc();
3870 ErrorRange = AtomicUnaryOp->getSourceRange();
3871 NoteLoc = AtomicUnaryOp->getOperatorLoc();
3872 NoteRange = SourceRange(NoteLoc, NoteLoc);
3873 }
3874 } else {
3875 ErrorFound = NotABinaryOrUnaryExpression;
3876 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
3877 NoteRange = ErrorRange = AtomicBody->getSourceRange();
3878 }
3879 } else {
3880 ErrorFound = NotAScalarType;
3881 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
3882 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
3883 }
3884 } else {
3885 ErrorFound = NotAnExpression;
3886 NoteLoc = ErrorLoc = S->getLocStart();
3887 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
3888 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00003889 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00003890 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
3891 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
3892 return true;
3893 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00003894 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00003895 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00003896 // Build an update expression of form 'OpaqueValueExpr(x) binop
3897 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
3898 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
3899 auto *OVEX = new (SemaRef.getASTContext())
3900 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
3901 auto *OVEExpr = new (SemaRef.getASTContext())
3902 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
3903 auto Update =
3904 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
3905 IsXLHSInRHSPart ? OVEExpr : OVEX);
3906 if (Update.isInvalid())
3907 return true;
3908 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
3909 Sema::AA_Casting);
3910 if (Update.isInvalid())
3911 return true;
3912 UpdateExpr = Update.get();
3913 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00003914 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003915}
3916
Alexey Bataev0162e452014-07-22 10:10:35 +00003917StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
3918 Stmt *AStmt,
3919 SourceLocation StartLoc,
3920 SourceLocation EndLoc) {
3921 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003922 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00003923 // 1.2.2 OpenMP Language Terminology
3924 // Structured block - An executable statement with a single entry at the
3925 // top and a single exit at the bottom.
3926 // The point of exit cannot be a branch out of the structured block.
3927 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00003928 OpenMPClauseKind AtomicKind = OMPC_unknown;
3929 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003930 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00003931 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00003932 C->getClauseKind() == OMPC_update ||
3933 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00003934 if (AtomicKind != OMPC_unknown) {
3935 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
3936 << SourceRange(C->getLocStart(), C->getLocEnd());
3937 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
3938 << getOpenMPClauseName(AtomicKind);
3939 } else {
3940 AtomicKind = C->getClauseKind();
3941 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003942 }
3943 }
3944 }
Alexey Bataev62cec442014-11-18 10:14:22 +00003945
Alexey Bataev459dec02014-07-24 06:46:57 +00003946 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00003947 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
3948 Body = EWC->getSubExpr();
3949
Alexey Bataev62cec442014-11-18 10:14:22 +00003950 Expr *X = nullptr;
3951 Expr *V = nullptr;
3952 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00003953 Expr *UE = nullptr;
3954 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00003955 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00003956 // OpenMP [2.12.6, atomic Construct]
3957 // In the next expressions:
3958 // * x and v (as applicable) are both l-value expressions with scalar type.
3959 // * During the execution of an atomic region, multiple syntactic
3960 // occurrences of x must designate the same storage location.
3961 // * Neither of v and expr (as applicable) may access the storage location
3962 // designated by x.
3963 // * Neither of x and expr (as applicable) may access the storage location
3964 // designated by v.
3965 // * expr is an expression with scalar type.
3966 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
3967 // * binop, binop=, ++, and -- are not overloaded operators.
3968 // * The expression x binop expr must be numerically equivalent to x binop
3969 // (expr). This requirement is satisfied if the operators in expr have
3970 // precedence greater than binop, or by using parentheses around expr or
3971 // subexpressions of expr.
3972 // * The expression expr binop x must be numerically equivalent to (expr)
3973 // binop x. This requirement is satisfied if the operators in expr have
3974 // precedence equal to or greater than binop, or by using parentheses around
3975 // expr or subexpressions of expr.
3976 // * For forms that allow multiple occurrences of x, the number of times
3977 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00003978 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00003979 enum {
3980 NotAnExpression,
3981 NotAnAssignmentOp,
3982 NotAScalarType,
3983 NotAnLValue,
3984 NoError
3985 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00003986 SourceLocation ErrorLoc, NoteLoc;
3987 SourceRange ErrorRange, NoteRange;
3988 // If clause is read:
3989 // v = x;
3990 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
3991 auto AtomicBinOp =
3992 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
3993 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
3994 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
3995 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
3996 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
3997 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
3998 if (!X->isLValue() || !V->isLValue()) {
3999 auto NotLValueExpr = X->isLValue() ? V : X;
4000 ErrorFound = NotAnLValue;
4001 ErrorLoc = AtomicBinOp->getExprLoc();
4002 ErrorRange = AtomicBinOp->getSourceRange();
4003 NoteLoc = NotLValueExpr->getExprLoc();
4004 NoteRange = NotLValueExpr->getSourceRange();
4005 }
4006 } else if (!X->isInstantiationDependent() ||
4007 !V->isInstantiationDependent()) {
4008 auto NotScalarExpr =
4009 (X->isInstantiationDependent() || X->getType()->isScalarType())
4010 ? V
4011 : X;
4012 ErrorFound = NotAScalarType;
4013 ErrorLoc = AtomicBinOp->getExprLoc();
4014 ErrorRange = AtomicBinOp->getSourceRange();
4015 NoteLoc = NotScalarExpr->getExprLoc();
4016 NoteRange = NotScalarExpr->getSourceRange();
4017 }
4018 } else {
4019 ErrorFound = NotAnAssignmentOp;
4020 ErrorLoc = AtomicBody->getExprLoc();
4021 ErrorRange = AtomicBody->getSourceRange();
4022 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
4023 : AtomicBody->getExprLoc();
4024 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
4025 : AtomicBody->getSourceRange();
4026 }
4027 } else {
4028 ErrorFound = NotAnExpression;
4029 NoteLoc = ErrorLoc = Body->getLocStart();
4030 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00004031 }
Alexey Bataev62cec442014-11-18 10:14:22 +00004032 if (ErrorFound != NoError) {
4033 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
4034 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00004035 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
4036 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00004037 return StmtError();
4038 } else if (CurContext->isDependentContext())
4039 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00004040 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004041 enum {
4042 NotAnExpression,
4043 NotAnAssignmentOp,
4044 NotAScalarType,
4045 NotAnLValue,
4046 NoError
4047 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00004048 SourceLocation ErrorLoc, NoteLoc;
4049 SourceRange ErrorRange, NoteRange;
4050 // If clause is write:
4051 // x = expr;
4052 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
4053 auto AtomicBinOp =
4054 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
4055 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00004056 X = AtomicBinOp->getLHS();
4057 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00004058 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
4059 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
4060 if (!X->isLValue()) {
4061 ErrorFound = NotAnLValue;
4062 ErrorLoc = AtomicBinOp->getExprLoc();
4063 ErrorRange = AtomicBinOp->getSourceRange();
4064 NoteLoc = X->getExprLoc();
4065 NoteRange = X->getSourceRange();
4066 }
4067 } else if (!X->isInstantiationDependent() ||
4068 !E->isInstantiationDependent()) {
4069 auto NotScalarExpr =
4070 (X->isInstantiationDependent() || X->getType()->isScalarType())
4071 ? E
4072 : X;
4073 ErrorFound = NotAScalarType;
4074 ErrorLoc = AtomicBinOp->getExprLoc();
4075 ErrorRange = AtomicBinOp->getSourceRange();
4076 NoteLoc = NotScalarExpr->getExprLoc();
4077 NoteRange = NotScalarExpr->getSourceRange();
4078 }
4079 } else {
4080 ErrorFound = NotAnAssignmentOp;
4081 ErrorLoc = AtomicBody->getExprLoc();
4082 ErrorRange = AtomicBody->getSourceRange();
4083 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
4084 : AtomicBody->getExprLoc();
4085 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
4086 : AtomicBody->getSourceRange();
4087 }
4088 } else {
4089 ErrorFound = NotAnExpression;
4090 NoteLoc = ErrorLoc = Body->getLocStart();
4091 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00004092 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00004093 if (ErrorFound != NoError) {
4094 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
4095 << ErrorRange;
4096 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
4097 << NoteRange;
4098 return StmtError();
4099 } else if (CurContext->isDependentContext())
4100 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00004101 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004102 // If clause is update:
4103 // x++;
4104 // x--;
4105 // ++x;
4106 // --x;
4107 // x binop= expr;
4108 // x = x binop expr;
4109 // x = expr binop x;
4110 OpenMPAtomicUpdateChecker Checker(*this);
4111 if (Checker.checkStatement(
4112 Body, (AtomicKind == OMPC_update)
4113 ? diag::err_omp_atomic_update_not_expression_statement
4114 : diag::err_omp_atomic_not_expression_statement,
4115 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00004116 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00004117 if (!CurContext->isDependentContext()) {
4118 E = Checker.getExpr();
4119 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00004120 UE = Checker.getUpdateExpr();
4121 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00004122 }
Alexey Bataev459dec02014-07-24 06:46:57 +00004123 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004124 enum {
4125 NotAnAssignmentOp,
4126 NotACompoundStatement,
4127 NotTwoSubstatements,
4128 NotASpecificExpression,
4129 NoError
4130 } ErrorFound = NoError;
4131 SourceLocation ErrorLoc, NoteLoc;
4132 SourceRange ErrorRange, NoteRange;
4133 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
4134 // If clause is a capture:
4135 // v = x++;
4136 // v = x--;
4137 // v = ++x;
4138 // v = --x;
4139 // v = x binop= expr;
4140 // v = x = x binop expr;
4141 // v = x = expr binop x;
4142 auto *AtomicBinOp =
4143 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
4144 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
4145 V = AtomicBinOp->getLHS();
4146 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
4147 OpenMPAtomicUpdateChecker Checker(*this);
4148 if (Checker.checkStatement(
4149 Body, diag::err_omp_atomic_capture_not_expression_statement,
4150 diag::note_omp_atomic_update))
4151 return StmtError();
4152 E = Checker.getExpr();
4153 X = Checker.getX();
4154 UE = Checker.getUpdateExpr();
4155 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
4156 IsPostfixUpdate = Checker.isPostfixUpdate();
4157 } else {
4158 ErrorLoc = AtomicBody->getExprLoc();
4159 ErrorRange = AtomicBody->getSourceRange();
4160 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
4161 : AtomicBody->getExprLoc();
4162 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
4163 : AtomicBody->getSourceRange();
4164 ErrorFound = NotAnAssignmentOp;
4165 }
4166 if (ErrorFound != NoError) {
4167 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
4168 << ErrorRange;
4169 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
4170 return StmtError();
4171 } else if (CurContext->isDependentContext()) {
4172 UE = V = E = X = nullptr;
4173 }
4174 } else {
4175 // If clause is a capture:
4176 // { v = x; x = expr; }
4177 // { v = x; x++; }
4178 // { v = x; x--; }
4179 // { v = x; ++x; }
4180 // { v = x; --x; }
4181 // { v = x; x binop= expr; }
4182 // { v = x; x = x binop expr; }
4183 // { v = x; x = expr binop x; }
4184 // { x++; v = x; }
4185 // { x--; v = x; }
4186 // { ++x; v = x; }
4187 // { --x; v = x; }
4188 // { x binop= expr; v = x; }
4189 // { x = x binop expr; v = x; }
4190 // { x = expr binop x; v = x; }
4191 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
4192 // Check that this is { expr1; expr2; }
4193 if (CS->size() == 2) {
4194 auto *First = CS->body_front();
4195 auto *Second = CS->body_back();
4196 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
4197 First = EWC->getSubExpr()->IgnoreParenImpCasts();
4198 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
4199 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
4200 // Need to find what subexpression is 'v' and what is 'x'.
4201 OpenMPAtomicUpdateChecker Checker(*this);
4202 bool IsUpdateExprFound = !Checker.checkStatement(Second);
4203 BinaryOperator *BinOp = nullptr;
4204 if (IsUpdateExprFound) {
4205 BinOp = dyn_cast<BinaryOperator>(First);
4206 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
4207 }
4208 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
4209 // { v = x; x++; }
4210 // { v = x; x--; }
4211 // { v = x; ++x; }
4212 // { v = x; --x; }
4213 // { v = x; x binop= expr; }
4214 // { v = x; x = x binop expr; }
4215 // { v = x; x = expr binop x; }
4216 // Check that the first expression has form v = x.
4217 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
4218 llvm::FoldingSetNodeID XId, PossibleXId;
4219 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
4220 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
4221 IsUpdateExprFound = XId == PossibleXId;
4222 if (IsUpdateExprFound) {
4223 V = BinOp->getLHS();
4224 X = Checker.getX();
4225 E = Checker.getExpr();
4226 UE = Checker.getUpdateExpr();
4227 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00004228 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004229 }
4230 }
4231 if (!IsUpdateExprFound) {
4232 IsUpdateExprFound = !Checker.checkStatement(First);
4233 BinOp = nullptr;
4234 if (IsUpdateExprFound) {
4235 BinOp = dyn_cast<BinaryOperator>(Second);
4236 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
4237 }
4238 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
4239 // { x++; v = x; }
4240 // { x--; v = x; }
4241 // { ++x; v = x; }
4242 // { --x; v = x; }
4243 // { x binop= expr; v = x; }
4244 // { x = x binop expr; v = x; }
4245 // { x = expr binop x; v = x; }
4246 // Check that the second expression has form v = x.
4247 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
4248 llvm::FoldingSetNodeID XId, PossibleXId;
4249 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
4250 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
4251 IsUpdateExprFound = XId == PossibleXId;
4252 if (IsUpdateExprFound) {
4253 V = BinOp->getLHS();
4254 X = Checker.getX();
4255 E = Checker.getExpr();
4256 UE = Checker.getUpdateExpr();
4257 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00004258 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004259 }
4260 }
4261 }
4262 if (!IsUpdateExprFound) {
4263 // { v = x; x = expr; }
4264 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
4265 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
4266 ErrorFound = NotAnAssignmentOp;
4267 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
4268 : First->getLocStart();
4269 NoteRange = ErrorRange = FirstBinOp
4270 ? FirstBinOp->getSourceRange()
4271 : SourceRange(ErrorLoc, ErrorLoc);
4272 } else {
4273 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
4274 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
4275 ErrorFound = NotAnAssignmentOp;
4276 NoteLoc = ErrorLoc = SecondBinOp ? SecondBinOp->getOperatorLoc()
4277 : Second->getLocStart();
4278 NoteRange = ErrorRange = SecondBinOp
4279 ? SecondBinOp->getSourceRange()
4280 : SourceRange(ErrorLoc, ErrorLoc);
4281 } else {
4282 auto *PossibleXRHSInFirst =
4283 FirstBinOp->getRHS()->IgnoreParenImpCasts();
4284 auto *PossibleXLHSInSecond =
4285 SecondBinOp->getLHS()->IgnoreParenImpCasts();
4286 llvm::FoldingSetNodeID X1Id, X2Id;
4287 PossibleXRHSInFirst->Profile(X1Id, Context, /*Canonical=*/true);
4288 PossibleXLHSInSecond->Profile(X2Id, Context,
4289 /*Canonical=*/true);
4290 IsUpdateExprFound = X1Id == X2Id;
4291 if (IsUpdateExprFound) {
4292 V = FirstBinOp->getLHS();
4293 X = SecondBinOp->getLHS();
4294 E = SecondBinOp->getRHS();
4295 UE = nullptr;
4296 IsXLHSInRHSPart = false;
4297 IsPostfixUpdate = true;
4298 } else {
4299 ErrorFound = NotASpecificExpression;
4300 ErrorLoc = FirstBinOp->getExprLoc();
4301 ErrorRange = FirstBinOp->getSourceRange();
4302 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
4303 NoteRange = SecondBinOp->getRHS()->getSourceRange();
4304 }
4305 }
4306 }
4307 }
4308 } else {
4309 NoteLoc = ErrorLoc = Body->getLocStart();
4310 NoteRange = ErrorRange =
4311 SourceRange(Body->getLocStart(), Body->getLocStart());
4312 ErrorFound = NotTwoSubstatements;
4313 }
4314 } else {
4315 NoteLoc = ErrorLoc = Body->getLocStart();
4316 NoteRange = ErrorRange =
4317 SourceRange(Body->getLocStart(), Body->getLocStart());
4318 ErrorFound = NotACompoundStatement;
4319 }
4320 if (ErrorFound != NoError) {
4321 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
4322 << ErrorRange;
4323 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
4324 return StmtError();
4325 } else if (CurContext->isDependentContext()) {
4326 UE = V = E = X = nullptr;
4327 }
Alexey Bataev459dec02014-07-24 06:46:57 +00004328 }
Alexey Bataevdea47612014-07-23 07:46:59 +00004329 }
Alexey Bataev0162e452014-07-22 10:10:35 +00004330
4331 getCurFunction()->setHasBranchProtectedScope();
4332
Alexey Bataev62cec442014-11-18 10:14:22 +00004333 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00004334 X, V, E, UE, IsXLHSInRHSPart,
4335 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00004336}
4337
Alexey Bataev0bd520b2014-09-19 08:19:49 +00004338StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
4339 Stmt *AStmt,
4340 SourceLocation StartLoc,
4341 SourceLocation EndLoc) {
4342 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
4343
Alexey Bataev13314bf2014-10-09 04:18:56 +00004344 // OpenMP [2.16, Nesting of Regions]
4345 // If specified, a teams construct must be contained within a target
4346 // construct. That target construct must contain no statements or directives
4347 // outside of the teams construct.
4348 if (DSAStack->hasInnerTeamsRegion()) {
4349 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
4350 bool OMPTeamsFound = true;
4351 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
4352 auto I = CS->body_begin();
4353 while (I != CS->body_end()) {
4354 auto OED = dyn_cast<OMPExecutableDirective>(*I);
4355 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
4356 OMPTeamsFound = false;
4357 break;
4358 }
4359 ++I;
4360 }
4361 assert(I != CS->body_end() && "Not found statement");
4362 S = *I;
4363 }
4364 if (!OMPTeamsFound) {
4365 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
4366 Diag(DSAStack->getInnerTeamsRegionLoc(),
4367 diag::note_omp_nested_teams_construct_here);
4368 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
4369 << isa<OMPExecutableDirective>(S);
4370 return StmtError();
4371 }
4372 }
4373
Alexey Bataev0bd520b2014-09-19 08:19:49 +00004374 getCurFunction()->setHasBranchProtectedScope();
4375
4376 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4377}
4378
Michael Wong65f367f2015-07-21 13:44:28 +00004379StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
4380 Stmt *AStmt,
4381 SourceLocation StartLoc,
4382 SourceLocation EndLoc) {
4383 getCurFunction()->setHasBranchProtectedScope();
4384
4385 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
4386 AStmt);
4387}
4388
Alexey Bataev13314bf2014-10-09 04:18:56 +00004389StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
4390 Stmt *AStmt, SourceLocation StartLoc,
4391 SourceLocation EndLoc) {
4392 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
4393 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4394 // 1.2.2 OpenMP Language Terminology
4395 // Structured block - An executable statement with a single entry at the
4396 // top and a single exit at the bottom.
4397 // The point of exit cannot be a branch out of the structured block.
4398 // longjmp() and throw() must not violate the entry/exit criteria.
4399 CS->getCapturedDecl()->setNothrow();
4400
4401 getCurFunction()->setHasBranchProtectedScope();
4402
4403 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4404}
4405
Alexey Bataev6d4ed052015-07-01 06:57:41 +00004406StmtResult
4407Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
4408 SourceLocation EndLoc,
4409 OpenMPDirectiveKind CancelRegion) {
4410 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
4411 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
4412 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
4413 << getOpenMPDirectiveName(CancelRegion);
4414 return StmtError();
4415 }
4416 if (DSAStack->isParentNowaitRegion()) {
4417 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
4418 return StmtError();
4419 }
4420 if (DSAStack->isParentOrderedRegion()) {
4421 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
4422 return StmtError();
4423 }
4424 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
4425 CancelRegion);
4426}
4427
Alexey Bataev80909872015-07-02 11:25:17 +00004428StmtResult Sema::ActOnOpenMPCancelDirective(SourceLocation StartLoc,
4429 SourceLocation EndLoc,
4430 OpenMPDirectiveKind CancelRegion) {
4431 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
4432 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
4433 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
4434 << getOpenMPDirectiveName(CancelRegion);
4435 return StmtError();
4436 }
4437 if (DSAStack->isParentNowaitRegion()) {
4438 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
4439 return StmtError();
4440 }
4441 if (DSAStack->isParentOrderedRegion()) {
4442 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
4443 return StmtError();
4444 }
4445 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, CancelRegion);
4446}
4447
Alexey Bataeved09d242014-05-28 05:53:51 +00004448OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004449 SourceLocation StartLoc,
4450 SourceLocation LParenLoc,
4451 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004452 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004453 switch (Kind) {
4454 case OMPC_if:
4455 Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc);
4456 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00004457 case OMPC_final:
4458 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
4459 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00004460 case OMPC_num_threads:
4461 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
4462 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00004463 case OMPC_safelen:
4464 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
4465 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00004466 case OMPC_collapse:
4467 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
4468 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004469 case OMPC_ordered:
4470 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
4471 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004472 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004473 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00004474 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004475 case OMPC_private:
4476 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00004477 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004478 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00004479 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00004480 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00004481 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004482 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00004483 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00004484 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00004485 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00004486 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004487 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00004488 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004489 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00004490 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00004491 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00004492 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00004493 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00004494 case OMPC_depend:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004495 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004496 llvm_unreachable("Clause is not allowed.");
4497 }
4498 return Res;
4499}
4500
Alexey Bataeved09d242014-05-28 05:53:51 +00004501OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004502 SourceLocation LParenLoc,
4503 SourceLocation EndLoc) {
4504 Expr *ValExpr = Condition;
4505 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
4506 !Condition->isInstantiationDependent() &&
4507 !Condition->containsUnexpandedParameterPack()) {
4508 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00004509 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004510 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004511 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004512
Nikola Smiljanic01a75982014-05-29 10:55:11 +00004513 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004514 }
4515
4516 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc);
4517}
4518
Alexey Bataev3778b602014-07-17 07:32:53 +00004519OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
4520 SourceLocation StartLoc,
4521 SourceLocation LParenLoc,
4522 SourceLocation EndLoc) {
4523 Expr *ValExpr = Condition;
4524 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
4525 !Condition->isInstantiationDependent() &&
4526 !Condition->containsUnexpandedParameterPack()) {
4527 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
4528 Condition->getExprLoc(), Condition);
4529 if (Val.isInvalid())
4530 return nullptr;
4531
4532 ValExpr = Val.get();
4533 }
4534
4535 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
4536}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004537ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
4538 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00004539 if (!Op)
4540 return ExprError();
4541
4542 class IntConvertDiagnoser : public ICEConvertDiagnoser {
4543 public:
4544 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00004545 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00004546 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
4547 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004548 return S.Diag(Loc, diag::err_omp_not_integral) << T;
4549 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004550 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
4551 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004552 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
4553 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004554 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
4555 QualType T,
4556 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004557 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
4558 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004559 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
4560 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004561 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00004562 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00004563 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004564 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
4565 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004566 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
4567 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004568 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
4569 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004570 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00004571 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00004572 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004573 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
4574 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004575 llvm_unreachable("conversion functions are permitted");
4576 }
4577 } ConvertDiagnoser;
4578 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
4579}
4580
4581OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
4582 SourceLocation StartLoc,
4583 SourceLocation LParenLoc,
4584 SourceLocation EndLoc) {
4585 Expr *ValExpr = NumThreads;
4586 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
Alexey Bataev568a8332014-03-06 06:15:19 +00004587 !NumThreads->containsUnexpandedParameterPack()) {
4588 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
4589 ExprResult Val =
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004590 PerformOpenMPImplicitIntegerConversion(NumThreadsLoc, NumThreads);
Alexey Bataev568a8332014-03-06 06:15:19 +00004591 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004592 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00004593
Nikola Smiljanic01a75982014-05-29 10:55:11 +00004594 ValExpr = Val.get();
Alexey Bataev568a8332014-03-06 06:15:19 +00004595
4596 // OpenMP [2.5, Restrictions]
4597 // The num_threads expression must evaluate to a positive integer value.
4598 llvm::APSInt Result;
Alexey Bataeved09d242014-05-28 05:53:51 +00004599 if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() &&
4600 !Result.isStrictlyPositive()) {
Alexey Bataev568a8332014-03-06 06:15:19 +00004601 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
4602 << "num_threads" << NumThreads->getSourceRange();
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004603 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00004604 }
4605 }
4606
Alexey Bataeved09d242014-05-28 05:53:51 +00004607 return new (Context)
4608 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00004609}
4610
Alexey Bataev62c87d22014-03-21 04:51:18 +00004611ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
4612 OpenMPClauseKind CKind) {
4613 if (!E)
4614 return ExprError();
4615 if (E->isValueDependent() || E->isTypeDependent() ||
4616 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00004617 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00004618 llvm::APSInt Result;
4619 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
4620 if (ICE.isInvalid())
4621 return ExprError();
4622 if (!Result.isStrictlyPositive()) {
4623 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
4624 << getOpenMPClauseName(CKind) << E->getSourceRange();
4625 return ExprError();
4626 }
Alexander Musman09184fe2014-09-30 05:29:28 +00004627 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
4628 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
4629 << E->getSourceRange();
4630 return ExprError();
4631 }
Alexey Bataev9c821032015-04-30 04:23:23 +00004632 if (CKind == OMPC_collapse) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00004633 DSAStack->setCollapseNumber(DSAStack->getCollapseNumber() - 1 +
4634 Result.getExtValue());
4635 } else if (CKind == OMPC_ordered) {
4636 DSAStack->setCollapseNumber(DSAStack->getCollapseNumber() - 1 +
4637 Result.getExtValue());
Alexey Bataev9c821032015-04-30 04:23:23 +00004638 }
Alexey Bataev62c87d22014-03-21 04:51:18 +00004639 return ICE;
4640}
4641
4642OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
4643 SourceLocation LParenLoc,
4644 SourceLocation EndLoc) {
4645 // OpenMP [2.8.1, simd construct, Description]
4646 // The parameter of the safelen clause must be a constant
4647 // positive integer expression.
4648 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
4649 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004650 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00004651 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00004652 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00004653}
4654
Alexander Musman64d33f12014-06-04 07:53:32 +00004655OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
4656 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00004657 SourceLocation LParenLoc,
4658 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00004659 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00004660 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00004661 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00004662 // The parameter of the collapse clause must be a constant
4663 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00004664 ExprResult NumForLoopsResult =
4665 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
4666 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00004667 return nullptr;
4668 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00004669 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00004670}
4671
Alexey Bataev10e775f2015-07-30 11:36:16 +00004672OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
4673 SourceLocation EndLoc,
4674 SourceLocation LParenLoc,
4675 Expr *NumForLoops) {
4676 DSAStack->setOrderedRegion();
4677 // OpenMP [2.7.1, loop construct, Description]
4678 // OpenMP [2.8.1, simd construct, Description]
4679 // OpenMP [2.9.6, distribute construct, Description]
4680 // The parameter of the ordered clause must be a constant
4681 // positive integer expression if any.
4682 if (NumForLoops && LParenLoc.isValid()) {
4683 ExprResult NumForLoopsResult =
4684 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
4685 if (NumForLoopsResult.isInvalid())
4686 return nullptr;
4687 NumForLoops = NumForLoopsResult.get();
4688 }
4689 return new (Context)
4690 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
4691}
4692
Alexey Bataeved09d242014-05-28 05:53:51 +00004693OMPClause *Sema::ActOnOpenMPSimpleClause(
4694 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
4695 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004696 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004697 switch (Kind) {
4698 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00004699 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00004700 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
4701 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004702 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004703 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00004704 Res = ActOnOpenMPProcBindClause(
4705 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
4706 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004707 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004708 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00004709 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00004710 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00004711 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00004712 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00004713 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004714 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004715 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00004716 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00004717 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00004718 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00004719 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00004720 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004721 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00004722 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004723 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00004724 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00004725 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00004726 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004727 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00004728 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004729 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00004730 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00004731 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00004732 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00004733 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00004734 case OMPC_depend:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004735 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004736 llvm_unreachable("Clause is not allowed.");
4737 }
4738 return Res;
4739}
4740
4741OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
4742 SourceLocation KindKwLoc,
4743 SourceLocation StartLoc,
4744 SourceLocation LParenLoc,
4745 SourceLocation EndLoc) {
4746 if (Kind == OMPC_DEFAULT_unknown) {
4747 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00004748 static_assert(OMPC_DEFAULT_unknown > 0,
4749 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00004750 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00004751 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004752 Values += "'";
4753 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
4754 Values += "'";
4755 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00004756 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004757 Values += " or ";
4758 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00004759 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004760 break;
4761 default:
4762 Values += Sep;
4763 break;
4764 }
4765 }
4766 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00004767 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004768 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004769 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00004770 switch (Kind) {
4771 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00004772 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004773 break;
4774 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00004775 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004776 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004777 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004778 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00004779 break;
4780 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004781 return new (Context)
4782 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004783}
4784
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004785OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
4786 SourceLocation KindKwLoc,
4787 SourceLocation StartLoc,
4788 SourceLocation LParenLoc,
4789 SourceLocation EndLoc) {
4790 if (Kind == OMPC_PROC_BIND_unknown) {
4791 std::string Values;
4792 std::string Sep(", ");
4793 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
4794 Values += "'";
4795 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
4796 Values += "'";
4797 switch (i) {
4798 case OMPC_PROC_BIND_unknown - 2:
4799 Values += " or ";
4800 break;
4801 case OMPC_PROC_BIND_unknown - 1:
4802 break;
4803 default:
4804 Values += Sep;
4805 break;
4806 }
4807 }
4808 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00004809 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004810 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004811 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004812 return new (Context)
4813 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004814}
4815
Alexey Bataev56dafe82014-06-20 07:16:17 +00004816OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
4817 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
4818 SourceLocation StartLoc, SourceLocation LParenLoc,
4819 SourceLocation ArgumentLoc, SourceLocation CommaLoc,
4820 SourceLocation EndLoc) {
4821 OMPClause *Res = nullptr;
4822 switch (Kind) {
4823 case OMPC_schedule:
4824 Res = ActOnOpenMPScheduleClause(
4825 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
4826 LParenLoc, ArgumentLoc, CommaLoc, EndLoc);
4827 break;
4828 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00004829 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00004830 case OMPC_num_threads:
4831 case OMPC_safelen:
4832 case OMPC_collapse:
4833 case OMPC_default:
4834 case OMPC_proc_bind:
4835 case OMPC_private:
4836 case OMPC_firstprivate:
4837 case OMPC_lastprivate:
4838 case OMPC_shared:
4839 case OMPC_reduction:
4840 case OMPC_linear:
4841 case OMPC_aligned:
4842 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00004843 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004844 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00004845 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00004846 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00004847 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00004848 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00004849 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004850 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00004851 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00004852 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00004853 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00004854 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00004855 case OMPC_depend:
Alexey Bataev56dafe82014-06-20 07:16:17 +00004856 case OMPC_unknown:
4857 llvm_unreachable("Clause is not allowed.");
4858 }
4859 return Res;
4860}
4861
4862OMPClause *Sema::ActOnOpenMPScheduleClause(
4863 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
4864 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
4865 SourceLocation EndLoc) {
4866 if (Kind == OMPC_SCHEDULE_unknown) {
4867 std::string Values;
4868 std::string Sep(", ");
4869 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) {
4870 Values += "'";
4871 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i);
4872 Values += "'";
4873 switch (i) {
4874 case OMPC_SCHEDULE_unknown - 2:
4875 Values += " or ";
4876 break;
4877 case OMPC_SCHEDULE_unknown - 1:
4878 break;
4879 default:
4880 Values += Sep;
4881 break;
4882 }
4883 }
4884 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
4885 << Values << getOpenMPClauseName(OMPC_schedule);
4886 return nullptr;
4887 }
4888 Expr *ValExpr = ChunkSize;
Alexey Bataev040d5402015-05-12 08:35:28 +00004889 Expr *HelperValExpr = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00004890 if (ChunkSize) {
4891 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
4892 !ChunkSize->isInstantiationDependent() &&
4893 !ChunkSize->containsUnexpandedParameterPack()) {
4894 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
4895 ExprResult Val =
4896 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
4897 if (Val.isInvalid())
4898 return nullptr;
4899
4900 ValExpr = Val.get();
4901
4902 // OpenMP [2.7.1, Restrictions]
4903 // chunk_size must be a loop invariant integer expression with a positive
4904 // value.
4905 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00004906 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
4907 if (Result.isSigned() && !Result.isStrictlyPositive()) {
4908 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
4909 << "schedule" << ChunkSize->getSourceRange();
4910 return nullptr;
4911 }
4912 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
4913 auto *ImpVar = buildVarDecl(*this, ChunkSize->getExprLoc(),
4914 ChunkSize->getType(), ".chunk.");
4915 auto *ImpVarRef = buildDeclRefExpr(*this, ImpVar, ChunkSize->getType(),
4916 ChunkSize->getExprLoc(),
4917 /*RefersToCapture=*/true);
4918 HelperValExpr = ImpVarRef;
Alexey Bataev56dafe82014-06-20 07:16:17 +00004919 }
4920 }
4921 }
4922
4923 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc,
Alexey Bataev040d5402015-05-12 08:35:28 +00004924 EndLoc, Kind, ValExpr, HelperValExpr);
Alexey Bataev56dafe82014-06-20 07:16:17 +00004925}
4926
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004927OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
4928 SourceLocation StartLoc,
4929 SourceLocation EndLoc) {
4930 OMPClause *Res = nullptr;
4931 switch (Kind) {
4932 case OMPC_ordered:
4933 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
4934 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00004935 case OMPC_nowait:
4936 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
4937 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00004938 case OMPC_untied:
4939 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
4940 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00004941 case OMPC_mergeable:
4942 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
4943 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004944 case OMPC_read:
4945 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
4946 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00004947 case OMPC_write:
4948 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
4949 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00004950 case OMPC_update:
4951 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
4952 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00004953 case OMPC_capture:
4954 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
4955 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00004956 case OMPC_seq_cst:
4957 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
4958 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004959 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00004960 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004961 case OMPC_num_threads:
4962 case OMPC_safelen:
4963 case OMPC_collapse:
4964 case OMPC_schedule:
4965 case OMPC_private:
4966 case OMPC_firstprivate:
4967 case OMPC_lastprivate:
4968 case OMPC_shared:
4969 case OMPC_reduction:
4970 case OMPC_linear:
4971 case OMPC_aligned:
4972 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00004973 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004974 case OMPC_default:
4975 case OMPC_proc_bind:
4976 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00004977 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00004978 case OMPC_depend:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004979 case OMPC_unknown:
4980 llvm_unreachable("Clause is not allowed.");
4981 }
4982 return Res;
4983}
4984
Alexey Bataev236070f2014-06-20 11:19:47 +00004985OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
4986 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00004987 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00004988 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
4989}
4990
Alexey Bataev7aea99a2014-07-17 12:19:31 +00004991OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
4992 SourceLocation EndLoc) {
4993 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
4994}
4995
Alexey Bataev74ba3a52014-07-17 12:47:03 +00004996OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
4997 SourceLocation EndLoc) {
4998 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
4999}
5000
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005001OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
5002 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005003 return new (Context) OMPReadClause(StartLoc, EndLoc);
5004}
5005
Alexey Bataevdea47612014-07-23 07:46:59 +00005006OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
5007 SourceLocation EndLoc) {
5008 return new (Context) OMPWriteClause(StartLoc, EndLoc);
5009}
5010
Alexey Bataev67a4f222014-07-23 10:25:33 +00005011OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
5012 SourceLocation EndLoc) {
5013 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
5014}
5015
Alexey Bataev459dec02014-07-24 06:46:57 +00005016OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
5017 SourceLocation EndLoc) {
5018 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
5019}
5020
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005021OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
5022 SourceLocation EndLoc) {
5023 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
5024}
5025
Alexey Bataevc5e02582014-06-16 07:08:35 +00005026OMPClause *Sema::ActOnOpenMPVarListClause(
5027 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
5028 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
5029 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005030 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
5031 SourceLocation DepLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005032 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005033 switch (Kind) {
5034 case OMPC_private:
5035 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
5036 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005037 case OMPC_firstprivate:
5038 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
5039 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00005040 case OMPC_lastprivate:
5041 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
5042 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00005043 case OMPC_shared:
5044 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
5045 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005046 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00005047 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
5048 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00005049 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00005050 case OMPC_linear:
5051 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
5052 ColonLoc, EndLoc);
5053 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00005054 case OMPC_aligned:
5055 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
5056 ColonLoc, EndLoc);
5057 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005058 case OMPC_copyin:
5059 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
5060 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00005061 case OMPC_copyprivate:
5062 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
5063 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00005064 case OMPC_flush:
5065 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
5066 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005067 case OMPC_depend:
5068 Res = ActOnOpenMPDependClause(DepKind, DepLoc, ColonLoc, VarList, StartLoc,
5069 LParenLoc, EndLoc);
5070 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005071 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00005072 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00005073 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00005074 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00005075 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005076 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005077 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005078 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005079 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00005080 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005081 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005082 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005083 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005084 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00005085 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00005086 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00005087 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005088 case OMPC_seq_cst:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005089 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005090 llvm_unreachable("Clause is not allowed.");
5091 }
5092 return Res;
5093}
5094
5095OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
5096 SourceLocation StartLoc,
5097 SourceLocation LParenLoc,
5098 SourceLocation EndLoc) {
5099 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00005100 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00005101 for (auto &RefExpr : VarList) {
5102 assert(RefExpr && "NULL expr in OpenMP private clause.");
5103 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005104 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00005105 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00005106 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005107 continue;
5108 }
5109
Alexey Bataeved09d242014-05-28 05:53:51 +00005110 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005111 // OpenMP [2.1, C/C++]
5112 // A list item is a variable name.
5113 // OpenMP [2.9.3.3, Restrictions, p.1]
5114 // A variable that is part of another variable (as an array or
5115 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00005116 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005117 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005118 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005119 continue;
5120 }
5121 Decl *D = DE->getDecl();
5122 VarDecl *VD = cast<VarDecl>(D);
5123
5124 QualType Type = VD->getType();
5125 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5126 // It will be analyzed later.
5127 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00005128 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005129 continue;
5130 }
5131
5132 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
5133 // A variable that appears in a private clause must not have an incomplete
5134 // type or a reference type.
5135 if (RequireCompleteType(ELoc, Type,
5136 diag::err_omp_private_incomplete_type)) {
5137 continue;
5138 }
5139 if (Type->isReferenceType()) {
5140 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00005141 << getOpenMPClauseName(OMPC_private) << Type;
5142 bool IsDecl =
5143 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5144 Diag(VD->getLocation(),
5145 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5146 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005147 continue;
5148 }
5149
Alexey Bataev758e55e2013-09-06 18:03:48 +00005150 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
5151 // in a Construct]
5152 // Variables with the predetermined data-sharing attributes may not be
5153 // listed in data-sharing attributes clauses, except for the cases
5154 // listed below. For these exceptions only, listing a predetermined
5155 // variable in a data-sharing attribute clause is allowed and overrides
5156 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005157 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005158 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005159 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
5160 << getOpenMPClauseName(OMPC_private);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005161 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005162 continue;
5163 }
5164
Alexey Bataevccb59ec2015-05-19 08:44:56 +00005165 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00005166 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00005167 DSAStack->getCurrentDirective() == OMPD_task) {
5168 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
5169 << getOpenMPClauseName(OMPC_private) << Type
5170 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
5171 bool IsDecl =
5172 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5173 Diag(VD->getLocation(),
5174 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5175 << VD;
5176 continue;
5177 }
5178
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005179 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
5180 // A variable of class type (or array thereof) that appears in a private
5181 // clause requires an accessible, unambiguous default constructor for the
5182 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00005183 // Generate helper private variable and initialize it with the default
5184 // value. The address of the original variable is replaced by the address of
5185 // the new private variable in CodeGen. This new variable is not added to
5186 // IdResolver, so the code in the OpenMP region uses original variable for
5187 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005188 Type = Type.getUnqualifiedType();
5189 auto VDPrivate = buildVarDecl(*this, DE->getExprLoc(), Type, VD->getName());
Alexey Bataev39f915b82015-05-08 10:41:21 +00005190 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00005191 if (VDPrivate->isInvalidDecl())
5192 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005193 auto VDPrivateRefExpr = buildDeclRefExpr(
5194 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev03b340a2014-10-21 03:16:40 +00005195
Alexey Bataev758e55e2013-09-06 18:03:48 +00005196 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005197 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00005198 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005199 }
5200
Alexey Bataeved09d242014-05-28 05:53:51 +00005201 if (Vars.empty())
5202 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005203
Alexey Bataev03b340a2014-10-21 03:16:40 +00005204 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
5205 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005206}
5207
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005208namespace {
5209class DiagsUninitializedSeveretyRAII {
5210private:
5211 DiagnosticsEngine &Diags;
5212 SourceLocation SavedLoc;
5213 bool IsIgnored;
5214
5215public:
5216 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
5217 bool IsIgnored)
5218 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
5219 if (!IsIgnored) {
5220 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
5221 /*Map*/ diag::Severity::Ignored, Loc);
5222 }
5223 }
5224 ~DiagsUninitializedSeveretyRAII() {
5225 if (!IsIgnored)
5226 Diags.popMappings(SavedLoc);
5227 }
5228};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00005229}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005230
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005231OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
5232 SourceLocation StartLoc,
5233 SourceLocation LParenLoc,
5234 SourceLocation EndLoc) {
5235 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005236 SmallVector<Expr *, 8> PrivateCopies;
5237 SmallVector<Expr *, 8> Inits;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005238 bool IsImplicitClause =
5239 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
5240 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
5241
Alexey Bataeved09d242014-05-28 05:53:51 +00005242 for (auto &RefExpr : VarList) {
5243 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
5244 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005245 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00005246 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005247 PrivateCopies.push_back(nullptr);
5248 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005249 continue;
5250 }
5251
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005252 SourceLocation ELoc =
5253 IsImplicitClause ? ImplicitClauseLoc : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005254 // OpenMP [2.1, C/C++]
5255 // A list item is a variable name.
5256 // OpenMP [2.9.3.3, Restrictions, p.1]
5257 // A variable that is part of another variable (as an array or
5258 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00005259 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005260 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005261 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005262 continue;
5263 }
5264 Decl *D = DE->getDecl();
5265 VarDecl *VD = cast<VarDecl>(D);
5266
5267 QualType Type = VD->getType();
5268 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5269 // It will be analyzed later.
5270 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005271 PrivateCopies.push_back(nullptr);
5272 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005273 continue;
5274 }
5275
5276 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
5277 // A variable that appears in a private clause must not have an incomplete
5278 // type or a reference type.
5279 if (RequireCompleteType(ELoc, Type,
5280 diag::err_omp_firstprivate_incomplete_type)) {
5281 continue;
5282 }
5283 if (Type->isReferenceType()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005284 if (IsImplicitClause) {
5285 Diag(ImplicitClauseLoc,
5286 diag::err_omp_task_predetermined_firstprivate_ref_type_arg)
5287 << Type;
5288 Diag(RefExpr->getExprLoc(), diag::note_used_here);
5289 } else {
5290 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
5291 << getOpenMPClauseName(OMPC_firstprivate) << Type;
5292 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005293 bool IsDecl =
5294 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5295 Diag(VD->getLocation(),
5296 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5297 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005298 continue;
5299 }
5300
5301 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
5302 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00005303 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005304 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005305 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005306
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005307 // If an implicit firstprivate variable found it was checked already.
5308 if (!IsImplicitClause) {
5309 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005310 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005311 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
5312 // A list item that specifies a given variable may not appear in more
5313 // than one clause on the same directive, except that a variable may be
5314 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005315 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00005316 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005317 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00005318 << getOpenMPClauseName(DVar.CKind)
5319 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005320 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005321 continue;
5322 }
5323
5324 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
5325 // in a Construct]
5326 // Variables with the predetermined data-sharing attributes may not be
5327 // listed in data-sharing attributes clauses, except for the cases
5328 // listed below. For these exceptions only, listing a predetermined
5329 // variable in a data-sharing attribute clause is allowed and overrides
5330 // the variable's predetermined data-sharing attributes.
5331 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
5332 // in a Construct, C/C++, p.2]
5333 // Variables with const-qualified type having no mutable member may be
5334 // listed in a firstprivate clause, even if they are static data members.
5335 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
5336 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
5337 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00005338 << getOpenMPClauseName(DVar.CKind)
5339 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005340 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005341 continue;
5342 }
5343
Alexey Bataevf29276e2014-06-18 04:14:57 +00005344 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005345 // OpenMP [2.9.3.4, Restrictions, p.2]
5346 // A list item that is private within a parallel region must not appear
5347 // in a firstprivate clause on a worksharing construct if any of the
5348 // worksharing regions arising from the worksharing construct ever bind
5349 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00005350 if (isOpenMPWorksharingDirective(CurrDir) &&
5351 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005352 DVar = DSAStack->getImplicitDSA(VD, true);
5353 if (DVar.CKind != OMPC_shared &&
5354 (isOpenMPParallelDirective(DVar.DKind) ||
5355 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00005356 Diag(ELoc, diag::err_omp_required_access)
5357 << getOpenMPClauseName(OMPC_firstprivate)
5358 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005359 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005360 continue;
5361 }
5362 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005363 // OpenMP [2.9.3.4, Restrictions, p.3]
5364 // A list item that appears in a reduction clause of a parallel construct
5365 // must not appear in a firstprivate clause on a worksharing or task
5366 // construct if any of the worksharing or task regions arising from the
5367 // worksharing or task construct ever bind to any of the parallel regions
5368 // arising from the parallel construct.
5369 // OpenMP [2.9.3.4, Restrictions, p.4]
5370 // A list item that appears in a reduction clause in worksharing
5371 // construct must not appear in a firstprivate clause in a task construct
5372 // encountered during execution of any of the worksharing regions arising
5373 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005374 if (CurrDir == OMPD_task) {
5375 DVar =
5376 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
5377 [](OpenMPDirectiveKind K) -> bool {
5378 return isOpenMPParallelDirective(K) ||
5379 isOpenMPWorksharingDirective(K);
5380 },
5381 false);
5382 if (DVar.CKind == OMPC_reduction &&
5383 (isOpenMPParallelDirective(DVar.DKind) ||
5384 isOpenMPWorksharingDirective(DVar.DKind))) {
5385 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
5386 << getOpenMPDirectiveName(DVar.DKind);
5387 ReportOriginalDSA(*this, DSAStack, VD, DVar);
5388 continue;
5389 }
5390 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005391 }
5392
Alexey Bataevccb59ec2015-05-19 08:44:56 +00005393 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00005394 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00005395 DSAStack->getCurrentDirective() == OMPD_task) {
5396 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
5397 << getOpenMPClauseName(OMPC_firstprivate) << Type
5398 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
5399 bool IsDecl =
5400 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5401 Diag(VD->getLocation(),
5402 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5403 << VD;
5404 continue;
5405 }
5406
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005407 Type = Type.getUnqualifiedType();
5408 auto VDPrivate = buildVarDecl(*this, ELoc, Type, VD->getName());
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005409 // Generate helper private variable and initialize it with the value of the
5410 // original variable. The address of the original variable is replaced by
5411 // the address of the new private variable in the CodeGen. This new variable
5412 // is not added to IdResolver, so the code in the OpenMP region uses
5413 // original variable for proper diagnostics and variable capturing.
5414 Expr *VDInitRefExpr = nullptr;
5415 // For arrays generate initializer for single element and replace it by the
5416 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005417 if (Type->isArrayType()) {
5418 auto VDInit =
5419 buildVarDecl(*this, DE->getExprLoc(), ElemType, VD->getName());
5420 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005421 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005422 ElemType = ElemType.getUnqualifiedType();
5423 auto *VDInitTemp = buildVarDecl(*this, DE->getLocStart(), ElemType,
5424 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00005425 InitializedEntity Entity =
5426 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005427 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
5428
5429 InitializationSequence InitSeq(*this, Entity, Kind, Init);
5430 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
5431 if (Result.isInvalid())
5432 VDPrivate->setInvalidDecl();
5433 else
5434 VDPrivate->setInit(Result.getAs<Expr>());
5435 } else {
Alexey Bataev69c62a92015-04-15 04:52:20 +00005436 auto *VDInit =
Alexey Bataev39f915b82015-05-08 10:41:21 +00005437 buildVarDecl(*this, DE->getLocStart(), Type, ".firstprivate.temp");
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005438 VDInitRefExpr =
5439 buildDeclRefExpr(*this, VDInit, DE->getType(), DE->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00005440 AddInitializerToDecl(VDPrivate,
5441 DefaultLvalueConversion(VDInitRefExpr).get(),
5442 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005443 }
5444 if (VDPrivate->isInvalidDecl()) {
5445 if (IsImplicitClause) {
5446 Diag(DE->getExprLoc(),
5447 diag::note_omp_task_predetermined_firstprivate_here);
5448 }
5449 continue;
5450 }
5451 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00005452 auto VDPrivateRefExpr = buildDeclRefExpr(
5453 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005454 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
5455 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005456 PrivateCopies.push_back(VDPrivateRefExpr);
5457 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005458 }
5459
Alexey Bataeved09d242014-05-28 05:53:51 +00005460 if (Vars.empty())
5461 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005462
5463 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005464 Vars, PrivateCopies, Inits);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005465}
5466
Alexander Musman1bb328c2014-06-04 13:06:39 +00005467OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
5468 SourceLocation StartLoc,
5469 SourceLocation LParenLoc,
5470 SourceLocation EndLoc) {
5471 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00005472 SmallVector<Expr *, 8> SrcExprs;
5473 SmallVector<Expr *, 8> DstExprs;
5474 SmallVector<Expr *, 8> AssignmentOps;
Alexander Musman1bb328c2014-06-04 13:06:39 +00005475 for (auto &RefExpr : VarList) {
5476 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
5477 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
5478 // It will be analyzed later.
5479 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00005480 SrcExprs.push_back(nullptr);
5481 DstExprs.push_back(nullptr);
5482 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005483 continue;
5484 }
5485
5486 SourceLocation ELoc = RefExpr->getExprLoc();
5487 // OpenMP [2.1, C/C++]
5488 // A list item is a variable name.
5489 // OpenMP [2.14.3.5, Restrictions, p.1]
5490 // A variable that is part of another variable (as an array or structure
5491 // element) cannot appear in a lastprivate clause.
5492 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
5493 if (!DE || !isa<VarDecl>(DE->getDecl())) {
5494 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
5495 continue;
5496 }
5497 Decl *D = DE->getDecl();
5498 VarDecl *VD = cast<VarDecl>(D);
5499
5500 QualType Type = VD->getType();
5501 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5502 // It will be analyzed later.
5503 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00005504 SrcExprs.push_back(nullptr);
5505 DstExprs.push_back(nullptr);
5506 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005507 continue;
5508 }
5509
5510 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
5511 // A variable that appears in a lastprivate clause must not have an
5512 // incomplete type or a reference type.
5513 if (RequireCompleteType(ELoc, Type,
5514 diag::err_omp_lastprivate_incomplete_type)) {
5515 continue;
5516 }
5517 if (Type->isReferenceType()) {
5518 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
5519 << getOpenMPClauseName(OMPC_lastprivate) << Type;
5520 bool IsDecl =
5521 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5522 Diag(VD->getLocation(),
5523 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5524 << VD;
5525 continue;
5526 }
5527
5528 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
5529 // in a Construct]
5530 // Variables with the predetermined data-sharing attributes may not be
5531 // listed in data-sharing attributes clauses, except for the cases
5532 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005533 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005534 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
5535 DVar.CKind != OMPC_firstprivate &&
5536 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
5537 Diag(ELoc, diag::err_omp_wrong_dsa)
5538 << getOpenMPClauseName(DVar.CKind)
5539 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005540 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005541 continue;
5542 }
5543
Alexey Bataevf29276e2014-06-18 04:14:57 +00005544 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
5545 // OpenMP [2.14.3.5, Restrictions, p.2]
5546 // A list item that is private within a parallel region, or that appears in
5547 // the reduction clause of a parallel construct, must not appear in a
5548 // lastprivate clause on a worksharing construct if any of the corresponding
5549 // worksharing regions ever binds to any of the corresponding parallel
5550 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005551 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00005552 if (isOpenMPWorksharingDirective(CurrDir) &&
5553 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005554 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005555 if (DVar.CKind != OMPC_shared) {
5556 Diag(ELoc, diag::err_omp_required_access)
5557 << getOpenMPClauseName(OMPC_lastprivate)
5558 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005559 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005560 continue;
5561 }
5562 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00005563 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00005564 // A variable of class type (or array thereof) that appears in a
5565 // lastprivate clause requires an accessible, unambiguous default
5566 // constructor for the class type, unless the list item is also specified
5567 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00005568 // A variable of class type (or array thereof) that appears in a
5569 // lastprivate clause requires an accessible, unambiguous copy assignment
5570 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00005571 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00005572 auto *SrcVD = buildVarDecl(*this, DE->getLocStart(),
Alexey Bataev38e89532015-04-16 04:54:05 +00005573 Type.getUnqualifiedType(), ".lastprivate.src");
Alexey Bataev39f915b82015-05-08 10:41:21 +00005574 auto *PseudoSrcExpr = buildDeclRefExpr(
5575 *this, SrcVD, Type.getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00005576 auto *DstVD =
Alexey Bataev39f915b82015-05-08 10:41:21 +00005577 buildVarDecl(*this, DE->getLocStart(), Type, ".lastprivate.dst");
Alexey Bataev38e89532015-04-16 04:54:05 +00005578 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00005579 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00005580 // For arrays generate assignment operation for single element and replace
5581 // it by the original array element in CodeGen.
5582 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
5583 PseudoDstExpr, PseudoSrcExpr);
5584 if (AssignmentOp.isInvalid())
5585 continue;
5586 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
5587 /*DiscardedValue=*/true);
5588 if (AssignmentOp.isInvalid())
5589 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00005590
Alexey Bataev39f915b82015-05-08 10:41:21 +00005591 if (TopDVar.CKind != OMPC_firstprivate)
Alexey Bataevf29276e2014-06-18 04:14:57 +00005592 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005593 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00005594 SrcExprs.push_back(PseudoSrcExpr);
5595 DstExprs.push_back(PseudoDstExpr);
5596 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00005597 }
5598
5599 if (Vars.empty())
5600 return nullptr;
5601
5602 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00005603 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005604}
5605
Alexey Bataev758e55e2013-09-06 18:03:48 +00005606OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
5607 SourceLocation StartLoc,
5608 SourceLocation LParenLoc,
5609 SourceLocation EndLoc) {
5610 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00005611 for (auto &RefExpr : VarList) {
5612 assert(RefExpr && "NULL expr in OpenMP shared clause.");
5613 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00005614 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00005615 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005616 continue;
5617 }
5618
Alexey Bataeved09d242014-05-28 05:53:51 +00005619 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00005620 // OpenMP [2.1, C/C++]
5621 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00005622 // OpenMP [2.14.3.2, Restrictions, p.1]
5623 // A variable that is part of another variable (as an array or structure
5624 // element) cannot appear in a shared unless it is a static data member
5625 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00005626 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005627 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005628 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00005629 continue;
5630 }
5631 Decl *D = DE->getDecl();
5632 VarDecl *VD = cast<VarDecl>(D);
5633
5634 QualType Type = VD->getType();
5635 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5636 // It will be analyzed later.
5637 Vars.push_back(DE);
5638 continue;
5639 }
5640
5641 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
5642 // in a Construct]
5643 // Variables with the predetermined data-sharing attributes may not be
5644 // listed in data-sharing attributes clauses, except for the cases
5645 // listed below. For these exceptions only, listing a predetermined
5646 // variable in a data-sharing attribute clause is allowed and overrides
5647 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005648 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00005649 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
5650 DVar.RefExpr) {
5651 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
5652 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005653 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005654 continue;
5655 }
5656
5657 DSAStack->addDSA(VD, DE, OMPC_shared);
5658 Vars.push_back(DE);
5659 }
5660
Alexey Bataeved09d242014-05-28 05:53:51 +00005661 if (Vars.empty())
5662 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00005663
5664 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
5665}
5666
Alexey Bataevc5e02582014-06-16 07:08:35 +00005667namespace {
5668class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
5669 DSAStackTy *Stack;
5670
5671public:
5672 bool VisitDeclRefExpr(DeclRefExpr *E) {
5673 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005674 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00005675 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
5676 return false;
5677 if (DVar.CKind != OMPC_unknown)
5678 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00005679 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005680 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005681 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00005682 return true;
5683 return false;
5684 }
5685 return false;
5686 }
5687 bool VisitStmt(Stmt *S) {
5688 for (auto Child : S->children()) {
5689 if (Child && Visit(Child))
5690 return true;
5691 }
5692 return false;
5693 }
Alexey Bataev23b69422014-06-18 07:08:49 +00005694 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00005695};
Alexey Bataev23b69422014-06-18 07:08:49 +00005696} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00005697
5698OMPClause *Sema::ActOnOpenMPReductionClause(
5699 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
5700 SourceLocation ColonLoc, SourceLocation EndLoc,
5701 CXXScopeSpec &ReductionIdScopeSpec,
5702 const DeclarationNameInfo &ReductionId) {
5703 // TODO: Allow scope specification search when 'declare reduction' is
5704 // supported.
5705 assert(ReductionIdScopeSpec.isEmpty() &&
5706 "No support for scoped reduction identifiers yet.");
5707
5708 auto DN = ReductionId.getName();
5709 auto OOK = DN.getCXXOverloadedOperator();
5710 BinaryOperatorKind BOK = BO_Comma;
5711
5712 // OpenMP [2.14.3.6, reduction clause]
5713 // C
5714 // reduction-identifier is either an identifier or one of the following
5715 // operators: +, -, *, &, |, ^, && and ||
5716 // C++
5717 // reduction-identifier is either an id-expression or one of the following
5718 // operators: +, -, *, &, |, ^, && and ||
5719 // FIXME: Only 'min' and 'max' identifiers are supported for now.
5720 switch (OOK) {
5721 case OO_Plus:
5722 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005723 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005724 break;
5725 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005726 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005727 break;
5728 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005729 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005730 break;
5731 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005732 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005733 break;
5734 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005735 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005736 break;
5737 case OO_AmpAmp:
5738 BOK = BO_LAnd;
5739 break;
5740 case OO_PipePipe:
5741 BOK = BO_LOr;
5742 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005743 case OO_New:
5744 case OO_Delete:
5745 case OO_Array_New:
5746 case OO_Array_Delete:
5747 case OO_Slash:
5748 case OO_Percent:
5749 case OO_Tilde:
5750 case OO_Exclaim:
5751 case OO_Equal:
5752 case OO_Less:
5753 case OO_Greater:
5754 case OO_LessEqual:
5755 case OO_GreaterEqual:
5756 case OO_PlusEqual:
5757 case OO_MinusEqual:
5758 case OO_StarEqual:
5759 case OO_SlashEqual:
5760 case OO_PercentEqual:
5761 case OO_CaretEqual:
5762 case OO_AmpEqual:
5763 case OO_PipeEqual:
5764 case OO_LessLess:
5765 case OO_GreaterGreater:
5766 case OO_LessLessEqual:
5767 case OO_GreaterGreaterEqual:
5768 case OO_EqualEqual:
5769 case OO_ExclaimEqual:
5770 case OO_PlusPlus:
5771 case OO_MinusMinus:
5772 case OO_Comma:
5773 case OO_ArrowStar:
5774 case OO_Arrow:
5775 case OO_Call:
5776 case OO_Subscript:
5777 case OO_Conditional:
5778 case NUM_OVERLOADED_OPERATORS:
5779 llvm_unreachable("Unexpected reduction identifier");
5780 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00005781 if (auto II = DN.getAsIdentifierInfo()) {
5782 if (II->isStr("max"))
5783 BOK = BO_GT;
5784 else if (II->isStr("min"))
5785 BOK = BO_LT;
5786 }
5787 break;
5788 }
5789 SourceRange ReductionIdRange;
5790 if (ReductionIdScopeSpec.isValid()) {
5791 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
5792 }
5793 ReductionIdRange.setEnd(ReductionId.getEndLoc());
5794 if (BOK == BO_Comma) {
5795 // Not allowed reduction identifier is found.
5796 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
5797 << ReductionIdRange;
5798 return nullptr;
5799 }
5800
5801 SmallVector<Expr *, 8> Vars;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005802 SmallVector<Expr *, 8> LHSs;
5803 SmallVector<Expr *, 8> RHSs;
5804 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005805 for (auto RefExpr : VarList) {
5806 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
5807 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
5808 // It will be analyzed later.
5809 Vars.push_back(RefExpr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005810 LHSs.push_back(nullptr);
5811 RHSs.push_back(nullptr);
5812 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00005813 continue;
5814 }
5815
5816 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
5817 RefExpr->isInstantiationDependent() ||
5818 RefExpr->containsUnexpandedParameterPack()) {
5819 // It will be analyzed later.
5820 Vars.push_back(RefExpr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005821 LHSs.push_back(nullptr);
5822 RHSs.push_back(nullptr);
5823 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00005824 continue;
5825 }
5826
5827 auto ELoc = RefExpr->getExprLoc();
5828 auto ERange = RefExpr->getSourceRange();
5829 // OpenMP [2.1, C/C++]
5830 // A list item is a variable or array section, subject to the restrictions
5831 // specified in Section 2.4 on page 42 and in each of the sections
5832 // describing clauses and directives for which a list appears.
5833 // OpenMP [2.14.3.3, Restrictions, p.1]
5834 // A variable that is part of another variable (as an array or
5835 // structure element) cannot appear in a private clause.
5836 auto DE = dyn_cast<DeclRefExpr>(RefExpr);
5837 if (!DE || !isa<VarDecl>(DE->getDecl())) {
5838 Diag(ELoc, diag::err_omp_expected_var_name) << ERange;
5839 continue;
5840 }
5841 auto D = DE->getDecl();
5842 auto VD = cast<VarDecl>(D);
5843 auto Type = VD->getType();
5844 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
5845 // A variable that appears in a private clause must not have an incomplete
5846 // type or a reference type.
5847 if (RequireCompleteType(ELoc, Type,
5848 diag::err_omp_reduction_incomplete_type))
5849 continue;
5850 // OpenMP [2.14.3.6, reduction clause, Restrictions]
5851 // Arrays may not appear in a reduction clause.
5852 if (Type.getNonReferenceType()->isArrayType()) {
5853 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
5854 bool IsDecl =
5855 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5856 Diag(VD->getLocation(),
5857 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5858 << VD;
5859 continue;
5860 }
5861 // OpenMP [2.14.3.6, reduction clause, Restrictions]
5862 // A list item that appears in a reduction clause must not be
5863 // const-qualified.
5864 if (Type.getNonReferenceType().isConstant(Context)) {
5865 Diag(ELoc, diag::err_omp_const_variable)
5866 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
5867 bool IsDecl =
5868 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5869 Diag(VD->getLocation(),
5870 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5871 << VD;
5872 continue;
5873 }
5874 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
5875 // If a list-item is a reference type then it must bind to the same object
5876 // for all threads of the team.
5877 VarDecl *VDDef = VD->getDefinition();
5878 if (Type->isReferenceType() && VDDef) {
5879 DSARefChecker Check(DSAStack);
5880 if (Check.Visit(VDDef->getInit())) {
5881 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
5882 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
5883 continue;
5884 }
5885 }
5886 // OpenMP [2.14.3.6, reduction clause, Restrictions]
5887 // The type of a list item that appears in a reduction clause must be valid
5888 // for the reduction-identifier. For a max or min reduction in C, the type
5889 // of the list item must be an allowed arithmetic data type: char, int,
5890 // float, double, or _Bool, possibly modified with long, short, signed, or
5891 // unsigned. For a max or min reduction in C++, the type of the list item
5892 // must be an allowed arithmetic data type: char, wchar_t, int, float,
5893 // double, or bool, possibly modified with long, short, signed, or unsigned.
5894 if ((BOK == BO_GT || BOK == BO_LT) &&
5895 !(Type->isScalarType() ||
5896 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
5897 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
5898 << getLangOpts().CPlusPlus;
5899 bool IsDecl =
5900 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5901 Diag(VD->getLocation(),
5902 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5903 << VD;
5904 continue;
5905 }
5906 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
5907 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
5908 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
5909 bool IsDecl =
5910 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5911 Diag(VD->getLocation(),
5912 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5913 << VD;
5914 continue;
5915 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00005916 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
5917 // in a Construct]
5918 // Variables with the predetermined data-sharing attributes may not be
5919 // listed in data-sharing attributes clauses, except for the cases
5920 // listed below. For these exceptions only, listing a predetermined
5921 // variable in a data-sharing attribute clause is allowed and overrides
5922 // the variable's predetermined data-sharing attributes.
5923 // OpenMP [2.14.3.6, Restrictions, p.3]
5924 // Any number of reduction clauses can be specified on the directive,
5925 // but a list item can appear only once in the reduction clauses for that
5926 // directive.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005927 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00005928 if (DVar.CKind == OMPC_reduction) {
5929 Diag(ELoc, diag::err_omp_once_referenced)
5930 << getOpenMPClauseName(OMPC_reduction);
5931 if (DVar.RefExpr) {
5932 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
5933 }
5934 } else if (DVar.CKind != OMPC_unknown) {
5935 Diag(ELoc, diag::err_omp_wrong_dsa)
5936 << getOpenMPClauseName(DVar.CKind)
5937 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005938 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00005939 continue;
5940 }
5941
5942 // OpenMP [2.14.3.6, Restrictions, p.1]
5943 // A list item that appears in a reduction clause of a worksharing
5944 // construct must be shared in the parallel regions to which any of the
5945 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf29276e2014-06-18 04:14:57 +00005946 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataev549210e2014-06-24 04:39:47 +00005947 if (isOpenMPWorksharingDirective(CurrDir) &&
5948 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005949 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005950 if (DVar.CKind != OMPC_shared) {
5951 Diag(ELoc, diag::err_omp_required_access)
5952 << getOpenMPClauseName(OMPC_reduction)
5953 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005954 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005955 continue;
5956 }
5957 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005958 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00005959 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs");
5960 auto *RHSVD = buildVarDecl(*this, ELoc, Type, VD->getName());
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005961 // Add initializer for private variable.
5962 Expr *Init = nullptr;
5963 switch (BOK) {
5964 case BO_Add:
5965 case BO_Xor:
5966 case BO_Or:
5967 case BO_LOr:
5968 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
5969 if (Type->isScalarType() || Type->isAnyComplexType()) {
5970 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00005971 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005972 break;
5973 case BO_Mul:
5974 case BO_LAnd:
5975 if (Type->isScalarType() || Type->isAnyComplexType()) {
5976 // '*' and '&&' reduction ops - initializer is '1'.
5977 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
5978 }
5979 break;
5980 case BO_And: {
5981 // '&' reduction op - initializer is '~0'.
5982 QualType OrigType = Type;
5983 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) {
5984 Type = ComplexTy->getElementType();
5985 }
5986 if (Type->isRealFloatingType()) {
5987 llvm::APFloat InitValue =
5988 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
5989 /*isIEEE=*/true);
5990 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
5991 Type, ELoc);
5992 } else if (Type->isScalarType()) {
5993 auto Size = Context.getTypeSize(Type);
5994 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
5995 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
5996 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
5997 }
5998 if (Init && OrigType->isAnyComplexType()) {
5999 // Init = 0xFFFF + 0xFFFFi;
6000 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
6001 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
6002 }
6003 Type = OrigType;
6004 break;
6005 }
6006 case BO_LT:
6007 case BO_GT: {
6008 // 'min' reduction op - initializer is 'Largest representable number in
6009 // the reduction list item type'.
6010 // 'max' reduction op - initializer is 'Least representable number in
6011 // the reduction list item type'.
6012 if (Type->isIntegerType() || Type->isPointerType()) {
6013 bool IsSigned = Type->hasSignedIntegerRepresentation();
6014 auto Size = Context.getTypeSize(Type);
6015 QualType IntTy =
6016 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
6017 llvm::APInt InitValue =
6018 (BOK != BO_LT)
6019 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
6020 : llvm::APInt::getMinValue(Size)
6021 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
6022 : llvm::APInt::getMaxValue(Size);
6023 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
6024 if (Type->isPointerType()) {
6025 // Cast to pointer type.
6026 auto CastExpr = BuildCStyleCastExpr(
6027 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
6028 SourceLocation(), Init);
6029 if (CastExpr.isInvalid())
6030 continue;
6031 Init = CastExpr.get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00006032 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006033 } else if (Type->isRealFloatingType()) {
6034 llvm::APFloat InitValue = llvm::APFloat::getLargest(
6035 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
6036 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
6037 Type, ELoc);
6038 }
6039 break;
6040 }
6041 case BO_PtrMemD:
6042 case BO_PtrMemI:
6043 case BO_MulAssign:
6044 case BO_Div:
6045 case BO_Rem:
6046 case BO_Sub:
6047 case BO_Shl:
6048 case BO_Shr:
6049 case BO_LE:
6050 case BO_GE:
6051 case BO_EQ:
6052 case BO_NE:
6053 case BO_AndAssign:
6054 case BO_XorAssign:
6055 case BO_OrAssign:
6056 case BO_Assign:
6057 case BO_AddAssign:
6058 case BO_SubAssign:
6059 case BO_DivAssign:
6060 case BO_RemAssign:
6061 case BO_ShlAssign:
6062 case BO_ShrAssign:
6063 case BO_Comma:
6064 llvm_unreachable("Unexpected reduction operation");
6065 }
6066 if (Init) {
6067 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
6068 /*TypeMayContainAuto=*/false);
6069 } else {
6070 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
6071 }
6072 if (!RHSVD->hasInit()) {
6073 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
6074 << ReductionIdRange;
6075 bool IsDecl =
6076 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6077 Diag(VD->getLocation(),
6078 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6079 << VD;
6080 continue;
6081 }
Alexey Bataev39f915b82015-05-08 10:41:21 +00006082 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
6083 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006084 ExprResult ReductionOp =
6085 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
6086 LHSDRE, RHSDRE);
6087 if (ReductionOp.isUsable()) {
Alexey Bataev69a47792015-05-07 03:54:03 +00006088 if (BOK != BO_LT && BOK != BO_GT) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006089 ReductionOp =
6090 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
6091 BO_Assign, LHSDRE, ReductionOp.get());
6092 } else {
6093 auto *ConditionalOp = new (Context) ConditionalOperator(
6094 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
6095 RHSDRE, Type, VK_LValue, OK_Ordinary);
6096 ReductionOp =
6097 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
6098 BO_Assign, LHSDRE, ConditionalOp);
6099 }
6100 if (ReductionOp.isUsable()) {
6101 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00006102 }
6103 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006104 if (ReductionOp.isInvalid())
6105 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006106
6107 DSAStack->addDSA(VD, DE, OMPC_reduction);
6108 Vars.push_back(DE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006109 LHSs.push_back(LHSDRE);
6110 RHSs.push_back(RHSDRE);
6111 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00006112 }
6113
6114 if (Vars.empty())
6115 return nullptr;
6116
6117 return OMPReductionClause::Create(
6118 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006119 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, LHSs,
6120 RHSs, ReductionOps);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006121}
6122
Alexander Musman8dba6642014-04-22 13:09:42 +00006123OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step,
6124 SourceLocation StartLoc,
6125 SourceLocation LParenLoc,
6126 SourceLocation ColonLoc,
6127 SourceLocation EndLoc) {
6128 SmallVector<Expr *, 8> Vars;
Alexander Musman3276a272015-03-21 10:12:56 +00006129 SmallVector<Expr *, 8> Inits;
Alexey Bataeved09d242014-05-28 05:53:51 +00006130 for (auto &RefExpr : VarList) {
6131 assert(RefExpr && "NULL expr in OpenMP linear clause.");
6132 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00006133 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006134 Vars.push_back(RefExpr);
Alexander Musman3276a272015-03-21 10:12:56 +00006135 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00006136 continue;
6137 }
6138
6139 // OpenMP [2.14.3.7, linear clause]
6140 // A list item that appears in a linear clause is subject to the private
6141 // clause semantics described in Section 2.14.3.3 on page 159 except as
6142 // noted. In addition, the value of the new list item on each iteration
6143 // of the associated loop(s) corresponds to the value of the original
6144 // list item before entering the construct plus the logical number of
6145 // the iteration times linear-step.
6146
Alexey Bataeved09d242014-05-28 05:53:51 +00006147 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00006148 // OpenMP [2.1, C/C++]
6149 // A list item is a variable name.
6150 // OpenMP [2.14.3.3, Restrictions, p.1]
6151 // A variable that is part of another variable (as an array or
6152 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00006153 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00006154 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006155 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00006156 continue;
6157 }
6158
6159 VarDecl *VD = cast<VarDecl>(DE->getDecl());
6160
6161 // OpenMP [2.14.3.7, linear clause]
6162 // A list-item cannot appear in more than one linear clause.
6163 // A list-item that appears in a linear clause cannot appear in any
6164 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006165 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00006166 if (DVar.RefExpr) {
6167 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
6168 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006169 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00006170 continue;
6171 }
6172
6173 QualType QType = VD->getType();
6174 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
6175 // It will be analyzed later.
6176 Vars.push_back(DE);
Alexander Musman3276a272015-03-21 10:12:56 +00006177 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00006178 continue;
6179 }
6180
6181 // A variable must not have an incomplete type or a reference type.
6182 if (RequireCompleteType(ELoc, QType,
6183 diag::err_omp_linear_incomplete_type)) {
6184 continue;
6185 }
6186 if (QType->isReferenceType()) {
6187 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
6188 << getOpenMPClauseName(OMPC_linear) << QType;
6189 bool IsDecl =
6190 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6191 Diag(VD->getLocation(),
6192 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6193 << VD;
6194 continue;
6195 }
6196
6197 // A list item must not be const-qualified.
6198 if (QType.isConstant(Context)) {
6199 Diag(ELoc, diag::err_omp_const_variable)
6200 << getOpenMPClauseName(OMPC_linear);
6201 bool IsDecl =
6202 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6203 Diag(VD->getLocation(),
6204 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6205 << VD;
6206 continue;
6207 }
6208
6209 // A list item must be of integral or pointer type.
6210 QType = QType.getUnqualifiedType().getCanonicalType();
6211 const Type *Ty = QType.getTypePtrOrNull();
6212 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
6213 !Ty->isPointerType())) {
6214 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
6215 bool IsDecl =
6216 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6217 Diag(VD->getLocation(),
6218 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6219 << VD;
6220 continue;
6221 }
6222
Alexander Musman3276a272015-03-21 10:12:56 +00006223 // Build var to save initial value.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006224 VarDecl *Init = buildVarDecl(*this, ELoc, QType, ".linear.start");
Alexander Musman3276a272015-03-21 10:12:56 +00006225 AddInitializerToDecl(Init, DefaultLvalueConversion(DE).get(),
6226 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006227 auto InitRef = buildDeclRefExpr(
6228 *this, Init, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman8dba6642014-04-22 13:09:42 +00006229 DSAStack->addDSA(VD, DE, OMPC_linear);
6230 Vars.push_back(DE);
Alexander Musman3276a272015-03-21 10:12:56 +00006231 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00006232 }
6233
6234 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006235 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00006236
6237 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00006238 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00006239 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
6240 !Step->isInstantiationDependent() &&
6241 !Step->containsUnexpandedParameterPack()) {
6242 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006243 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00006244 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006245 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006246 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00006247
Alexander Musman3276a272015-03-21 10:12:56 +00006248 // Build var to save the step value.
6249 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006250 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00006251 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006252 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00006253 ExprResult CalcStep =
6254 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
6255
Alexander Musman8dba6642014-04-22 13:09:42 +00006256 // Warn about zero linear step (it would be probably better specified as
6257 // making corresponding variables 'const').
6258 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00006259 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
6260 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00006261 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
6262 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00006263 if (!IsConstant && CalcStep.isUsable()) {
6264 // Calculate the step beforehand instead of doing this on each iteration.
6265 // (This is not used if the number of iterations may be kfold-ed).
6266 CalcStepExpr = CalcStep.get();
6267 }
Alexander Musman8dba6642014-04-22 13:09:42 +00006268 }
6269
6270 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc,
Alexander Musman3276a272015-03-21 10:12:56 +00006271 Vars, Inits, StepExpr, CalcStepExpr);
6272}
6273
6274static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
6275 Expr *NumIterations, Sema &SemaRef,
6276 Scope *S) {
6277 // Walk the vars and build update/final expressions for the CodeGen.
6278 SmallVector<Expr *, 8> Updates;
6279 SmallVector<Expr *, 8> Finals;
6280 Expr *Step = Clause.getStep();
6281 Expr *CalcStep = Clause.getCalcStep();
6282 // OpenMP [2.14.3.7, linear clause]
6283 // If linear-step is not specified it is assumed to be 1.
6284 if (Step == nullptr)
6285 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
6286 else if (CalcStep)
6287 Step = cast<BinaryOperator>(CalcStep)->getLHS();
6288 bool HasErrors = false;
6289 auto CurInit = Clause.inits().begin();
6290 for (auto &RefExpr : Clause.varlists()) {
6291 Expr *InitExpr = *CurInit;
6292
6293 // Build privatized reference to the current linear var.
6294 auto DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00006295 auto PrivateRef =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006296 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
6297 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
6298 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00006299
6300 // Build update: Var = InitExpr + IV * Step
6301 ExprResult Update =
6302 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), PrivateRef,
6303 InitExpr, IV, Step, /* Subtract */ false);
6304 Update = SemaRef.ActOnFinishFullExpr(Update.get());
6305
6306 // Build final: Var = InitExpr + NumIterations * Step
6307 ExprResult Final =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006308 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), PrivateRef,
6309 InitExpr, NumIterations, Step, /* Subtract */ false);
Alexander Musman3276a272015-03-21 10:12:56 +00006310 Final = SemaRef.ActOnFinishFullExpr(Final.get());
6311 if (!Update.isUsable() || !Final.isUsable()) {
6312 Updates.push_back(nullptr);
6313 Finals.push_back(nullptr);
6314 HasErrors = true;
6315 } else {
6316 Updates.push_back(Update.get());
6317 Finals.push_back(Final.get());
6318 }
6319 ++CurInit;
6320 }
6321 Clause.setUpdates(Updates);
6322 Clause.setFinals(Finals);
6323 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00006324}
6325
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006326OMPClause *Sema::ActOnOpenMPAlignedClause(
6327 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
6328 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
6329
6330 SmallVector<Expr *, 8> Vars;
6331 for (auto &RefExpr : VarList) {
6332 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
6333 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6334 // It will be analyzed later.
6335 Vars.push_back(RefExpr);
6336 continue;
6337 }
6338
6339 SourceLocation ELoc = RefExpr->getExprLoc();
6340 // OpenMP [2.1, C/C++]
6341 // A list item is a variable name.
6342 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
6343 if (!DE || !isa<VarDecl>(DE->getDecl())) {
6344 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
6345 continue;
6346 }
6347
6348 VarDecl *VD = cast<VarDecl>(DE->getDecl());
6349
6350 // OpenMP [2.8.1, simd construct, Restrictions]
6351 // The type of list items appearing in the aligned clause must be
6352 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006353 QualType QType = VD->getType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006354 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006355 const Type *Ty = QType.getTypePtrOrNull();
6356 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
6357 !Ty->isPointerType())) {
6358 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
6359 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
6360 bool IsDecl =
6361 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6362 Diag(VD->getLocation(),
6363 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6364 << VD;
6365 continue;
6366 }
6367
6368 // OpenMP [2.8.1, simd construct, Restrictions]
6369 // A list-item cannot appear in more than one aligned clause.
6370 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
6371 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
6372 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
6373 << getOpenMPClauseName(OMPC_aligned);
6374 continue;
6375 }
6376
6377 Vars.push_back(DE);
6378 }
6379
6380 // OpenMP [2.8.1, simd construct, Description]
6381 // The parameter of the aligned clause, alignment, must be a constant
6382 // positive integer expression.
6383 // If no optional parameter is specified, implementation-defined default
6384 // alignments for SIMD instructions on the target platforms are assumed.
6385 if (Alignment != nullptr) {
6386 ExprResult AlignResult =
6387 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
6388 if (AlignResult.isInvalid())
6389 return nullptr;
6390 Alignment = AlignResult.get();
6391 }
6392 if (Vars.empty())
6393 return nullptr;
6394
6395 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
6396 EndLoc, Vars, Alignment);
6397}
6398
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006399OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
6400 SourceLocation StartLoc,
6401 SourceLocation LParenLoc,
6402 SourceLocation EndLoc) {
6403 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006404 SmallVector<Expr *, 8> SrcExprs;
6405 SmallVector<Expr *, 8> DstExprs;
6406 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00006407 for (auto &RefExpr : VarList) {
6408 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
6409 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006410 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006411 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006412 SrcExprs.push_back(nullptr);
6413 DstExprs.push_back(nullptr);
6414 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006415 continue;
6416 }
6417
Alexey Bataeved09d242014-05-28 05:53:51 +00006418 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006419 // OpenMP [2.1, C/C++]
6420 // A list item is a variable name.
6421 // OpenMP [2.14.4.1, Restrictions, p.1]
6422 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00006423 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006424 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006425 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006426 continue;
6427 }
6428
6429 Decl *D = DE->getDecl();
6430 VarDecl *VD = cast<VarDecl>(D);
6431
6432 QualType Type = VD->getType();
6433 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6434 // It will be analyzed later.
6435 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006436 SrcExprs.push_back(nullptr);
6437 DstExprs.push_back(nullptr);
6438 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006439 continue;
6440 }
6441
6442 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
6443 // A list item that appears in a copyin clause must be threadprivate.
6444 if (!DSAStack->isThreadPrivate(VD)) {
6445 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00006446 << getOpenMPClauseName(OMPC_copyin)
6447 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006448 continue;
6449 }
6450
6451 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
6452 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00006453 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006454 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006455 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00006456 auto *SrcVD = buildVarDecl(*this, DE->getLocStart(),
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006457 ElemType.getUnqualifiedType(), ".copyin.src");
Alexey Bataev39f915b82015-05-08 10:41:21 +00006458 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006459 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
6460 auto *DstVD =
6461 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst");
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006462 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006463 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006464 // For arrays generate assignment operation for single element and replace
6465 // it by the original array element in CodeGen.
6466 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
6467 PseudoDstExpr, PseudoSrcExpr);
6468 if (AssignmentOp.isInvalid())
6469 continue;
6470 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
6471 /*DiscardedValue=*/true);
6472 if (AssignmentOp.isInvalid())
6473 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006474
6475 DSAStack->addDSA(VD, DE, OMPC_copyin);
6476 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006477 SrcExprs.push_back(PseudoSrcExpr);
6478 DstExprs.push_back(PseudoDstExpr);
6479 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006480 }
6481
Alexey Bataeved09d242014-05-28 05:53:51 +00006482 if (Vars.empty())
6483 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006484
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006485 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
6486 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006487}
6488
Alexey Bataevbae9a792014-06-27 10:37:06 +00006489OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
6490 SourceLocation StartLoc,
6491 SourceLocation LParenLoc,
6492 SourceLocation EndLoc) {
6493 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00006494 SmallVector<Expr *, 8> SrcExprs;
6495 SmallVector<Expr *, 8> DstExprs;
6496 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00006497 for (auto &RefExpr : VarList) {
6498 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
6499 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6500 // It will be analyzed later.
6501 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00006502 SrcExprs.push_back(nullptr);
6503 DstExprs.push_back(nullptr);
6504 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00006505 continue;
6506 }
6507
6508 SourceLocation ELoc = RefExpr->getExprLoc();
6509 // OpenMP [2.1, C/C++]
6510 // A list item is a variable name.
6511 // OpenMP [2.14.4.1, Restrictions, p.1]
6512 // A list item that appears in a copyin clause must be threadprivate.
6513 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
6514 if (!DE || !isa<VarDecl>(DE->getDecl())) {
6515 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
6516 continue;
6517 }
6518
6519 Decl *D = DE->getDecl();
6520 VarDecl *VD = cast<VarDecl>(D);
6521
6522 QualType Type = VD->getType();
6523 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6524 // It will be analyzed later.
6525 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00006526 SrcExprs.push_back(nullptr);
6527 DstExprs.push_back(nullptr);
6528 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00006529 continue;
6530 }
6531
6532 // OpenMP [2.14.4.2, Restrictions, p.2]
6533 // A list item that appears in a copyprivate clause may not appear in a
6534 // private or firstprivate clause on the single construct.
6535 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006536 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00006537 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
6538 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00006539 Diag(ELoc, diag::err_omp_wrong_dsa)
6540 << getOpenMPClauseName(DVar.CKind)
6541 << getOpenMPClauseName(OMPC_copyprivate);
6542 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6543 continue;
6544 }
6545
6546 // OpenMP [2.11.4.2, Restrictions, p.1]
6547 // All list items that appear in a copyprivate clause must be either
6548 // threadprivate or private in the enclosing context.
6549 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006550 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00006551 if (DVar.CKind == OMPC_shared) {
6552 Diag(ELoc, diag::err_omp_required_access)
6553 << getOpenMPClauseName(OMPC_copyprivate)
6554 << "threadprivate or private in the enclosing context";
6555 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6556 continue;
6557 }
6558 }
6559 }
6560
Alexey Bataev7a3e5852015-05-19 08:19:24 +00006561 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00006562 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00006563 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006564 << getOpenMPClauseName(OMPC_copyprivate) << Type
6565 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00006566 bool IsDecl =
6567 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6568 Diag(VD->getLocation(),
6569 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6570 << VD;
6571 continue;
6572 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006573
Alexey Bataevbae9a792014-06-27 10:37:06 +00006574 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
6575 // A variable of class type (or array thereof) that appears in a
6576 // copyin clause requires an accessible, unambiguous copy assignment
6577 // operator for the class type.
Alexey Bataev420d45b2015-04-14 05:11:24 +00006578 Type = Context.getBaseElementType(Type).getUnqualifiedType();
6579 auto *SrcVD =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006580 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.src");
Alexey Bataev420d45b2015-04-14 05:11:24 +00006581 auto *PseudoSrcExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006582 buildDeclRefExpr(*this, SrcVD, Type, DE->getExprLoc());
Alexey Bataev420d45b2015-04-14 05:11:24 +00006583 auto *DstVD =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006584 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.dst");
Alexey Bataev420d45b2015-04-14 05:11:24 +00006585 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006586 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataeva63048e2015-03-23 06:18:07 +00006587 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
6588 PseudoDstExpr, PseudoSrcExpr);
6589 if (AssignmentOp.isInvalid())
6590 continue;
6591 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
6592 /*DiscardedValue=*/true);
6593 if (AssignmentOp.isInvalid())
6594 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00006595
6596 // No need to mark vars as copyprivate, they are already threadprivate or
6597 // implicitly private.
6598 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00006599 SrcExprs.push_back(PseudoSrcExpr);
6600 DstExprs.push_back(PseudoDstExpr);
6601 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +00006602 }
6603
6604 if (Vars.empty())
6605 return nullptr;
6606
Alexey Bataeva63048e2015-03-23 06:18:07 +00006607 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
6608 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00006609}
6610
Alexey Bataev6125da92014-07-21 11:26:11 +00006611OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
6612 SourceLocation StartLoc,
6613 SourceLocation LParenLoc,
6614 SourceLocation EndLoc) {
6615 if (VarList.empty())
6616 return nullptr;
6617
6618 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
6619}
Alexey Bataevdea47612014-07-23 07:46:59 +00006620
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006621OMPClause *
6622Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
6623 SourceLocation DepLoc, SourceLocation ColonLoc,
6624 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
6625 SourceLocation LParenLoc, SourceLocation EndLoc) {
6626 if (DepKind == OMPC_DEPEND_unknown) {
6627 std::string Values;
6628 std::string Sep(", ");
6629 for (unsigned i = 0; i < OMPC_DEPEND_unknown; ++i) {
6630 Values += "'";
6631 Values += getOpenMPSimpleClauseTypeName(OMPC_depend, i);
6632 Values += "'";
6633 switch (i) {
6634 case OMPC_DEPEND_unknown - 2:
6635 Values += " or ";
6636 break;
6637 case OMPC_DEPEND_unknown - 1:
6638 break;
6639 default:
6640 Values += Sep;
6641 break;
6642 }
6643 }
6644 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
6645 << Values << getOpenMPClauseName(OMPC_depend);
6646 return nullptr;
6647 }
6648 SmallVector<Expr *, 8> Vars;
6649 for (auto &RefExpr : VarList) {
6650 assert(RefExpr && "NULL expr in OpenMP shared clause.");
6651 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6652 // It will be analyzed later.
6653 Vars.push_back(RefExpr);
6654 continue;
6655 }
6656
6657 SourceLocation ELoc = RefExpr->getExprLoc();
6658 // OpenMP [2.11.1.1, Restrictions, p.3]
6659 // A variable that is part of another variable (such as a field of a
6660 // structure) but is not an array element or an array section cannot appear
6661 // in a depend clause.
6662 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
6663 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
6664 ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
6665 if (!RefExpr->IgnoreParenImpCasts()->isLValue() || (!ASE && !DE) ||
6666 (DE && !isa<VarDecl>(DE->getDecl())) ||
6667 (ASE && !ASE->getBase()->getType()->isAnyPointerType() &&
6668 !ASE->getBase()->getType()->isArrayType())) {
6669 Diag(ELoc, diag::err_omp_expected_var_name_or_array_item)
6670 << RefExpr->getSourceRange();
6671 continue;
6672 }
6673
6674 Vars.push_back(RefExpr->IgnoreParenImpCasts());
6675 }
6676
6677 if (Vars.empty())
6678 return nullptr;
6679
6680 return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
6681 DepLoc, ColonLoc, Vars);
6682}