blob: 6abdd9d8cdf16dbfa9d5f6af357939b31b8168b9 [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 Bataevb08f89f2015-08-14 12:25:37 +000015#include "TreeTransform.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000016#include "clang/AST/ASTContext.h"
Alexey Bataev97720002014-11-11 04:05:39 +000017#include "clang/AST/ASTMutationListener.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000018#include "clang/AST/Decl.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000019#include "clang/AST/DeclCXX.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000020#include "clang/AST/DeclOpenMP.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000021#include "clang/AST/StmtCXX.h"
22#include "clang/AST/StmtOpenMP.h"
23#include "clang/AST/StmtVisitor.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000024#include "clang/Basic/OpenMPKinds.h"
Samuel Antaof8b50122015-07-13 22:54:53 +000025#include "clang/Basic/TargetInfo.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000026#include "clang/Lex/Preprocessor.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000027#include "clang/Sema/Initialization.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000028#include "clang/Sema/Lookup.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000029#include "clang/Sema/Scope.h"
30#include "clang/Sema/ScopeInfo.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000031#include "clang/Sema/SemaInternal.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000032using namespace clang;
33
Alexey Bataev758e55e2013-09-06 18:03:48 +000034//===----------------------------------------------------------------------===//
35// Stack of data-sharing attributes for variables
36//===----------------------------------------------------------------------===//
37
38namespace {
39/// \brief Default data sharing attributes, which can be applied to directive.
40enum DefaultDataSharingAttributes {
Alexey Bataeved09d242014-05-28 05:53:51 +000041 DSA_unspecified = 0, /// \brief Data sharing attribute not specified.
42 DSA_none = 1 << 0, /// \brief Default data sharing attribute 'none'.
43 DSA_shared = 1 << 1 /// \brief Default data sharing attribute 'shared'.
Alexey Bataev758e55e2013-09-06 18:03:48 +000044};
Alexey Bataev7ff55242014-06-19 09:13:45 +000045
Alexey Bataevf29276e2014-06-18 04:14:57 +000046template <class T> struct MatchesAny {
Alexey Bataev23b69422014-06-18 07:08:49 +000047 explicit MatchesAny(ArrayRef<T> Arr) : Arr(std::move(Arr)) {}
Alexey Bataevf29276e2014-06-18 04:14:57 +000048 bool operator()(T Kind) {
49 for (auto KindEl : Arr)
50 if (KindEl == Kind)
51 return true;
52 return false;
53 }
54
55private:
56 ArrayRef<T> Arr;
57};
Alexey Bataev23b69422014-06-18 07:08:49 +000058struct MatchesAlways {
Alexey Bataevf29276e2014-06-18 04:14:57 +000059 MatchesAlways() {}
Alexey Bataev7ff55242014-06-19 09:13:45 +000060 template <class T> bool operator()(T) { return true; }
Alexey Bataevf29276e2014-06-18 04:14:57 +000061};
62
63typedef MatchesAny<OpenMPClauseKind> MatchesAnyClause;
64typedef MatchesAny<OpenMPDirectiveKind> MatchesAnyDirective;
Alexey Bataev758e55e2013-09-06 18:03:48 +000065
66/// \brief Stack for tracking declarations used in OpenMP directives and
67/// clauses and their data-sharing attributes.
68class DSAStackTy {
69public:
70 struct DSAVarData {
71 OpenMPDirectiveKind DKind;
72 OpenMPClauseKind CKind;
73 DeclRefExpr *RefExpr;
Alexey Bataevbae9a792014-06-27 10:37:06 +000074 SourceLocation ImplicitDSALoc;
75 DSAVarData()
76 : DKind(OMPD_unknown), CKind(OMPC_unknown), RefExpr(nullptr),
77 ImplicitDSALoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000078 };
Alexey Bataeved09d242014-05-28 05:53:51 +000079
Alexey Bataev758e55e2013-09-06 18:03:48 +000080private:
81 struct DSAInfo {
82 OpenMPClauseKind Attributes;
83 DeclRefExpr *RefExpr;
84 };
85 typedef llvm::SmallDenseMap<VarDecl *, DSAInfo, 64> DeclSAMapTy;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000086 typedef llvm::SmallDenseMap<VarDecl *, DeclRefExpr *, 64> AlignedMapTy;
Alexey Bataev9c821032015-04-30 04:23:23 +000087 typedef llvm::DenseSet<VarDecl *> LoopControlVariablesSetTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +000088
89 struct SharingMapTy {
90 DeclSAMapTy SharingMap;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000091 AlignedMapTy AlignedMap;
Alexey Bataev9c821032015-04-30 04:23:23 +000092 LoopControlVariablesSetTy LCVSet;
Alexey Bataev758e55e2013-09-06 18:03:48 +000093 DefaultDataSharingAttributes DefaultAttr;
Alexey Bataevbae9a792014-06-27 10:37:06 +000094 SourceLocation DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +000095 OpenMPDirectiveKind Directive;
96 DeclarationNameInfo DirectiveName;
97 Scope *CurScope;
Alexey Bataevbae9a792014-06-27 10:37:06 +000098 SourceLocation ConstructLoc;
Alexey Bataev9fb6e642014-07-22 06:45:04 +000099 bool OrderedRegion;
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000100 bool NowaitRegion;
Alexey Bataev9c821032015-04-30 04:23:23 +0000101 unsigned CollapseNumber;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000102 SourceLocation InnerTeamsRegionLoc;
Alexey Bataeved09d242014-05-28 05:53:51 +0000103 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000104 Scope *CurScope, SourceLocation Loc)
Alexey Bataev9c821032015-04-30 04:23:23 +0000105 : SharingMap(), AlignedMap(), LCVSet(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000106 Directive(DKind), DirectiveName(std::move(Name)), CurScope(CurScope),
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000107 ConstructLoc(Loc), OrderedRegion(false), NowaitRegion(false),
108 CollapseNumber(1), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000109 SharingMapTy()
Alexey Bataev9c821032015-04-30 04:23:23 +0000110 : SharingMap(), AlignedMap(), LCVSet(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000111 Directive(OMPD_unknown), DirectiveName(), CurScope(nullptr),
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000112 ConstructLoc(), OrderedRegion(false), NowaitRegion(false),
113 CollapseNumber(1), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000114 };
115
116 typedef SmallVector<SharingMapTy, 64> StackTy;
117
118 /// \brief Stack of used declaration and their data-sharing attributes.
119 StackTy Stack;
Alexey Bataev39f915b82015-05-08 10:41:21 +0000120 /// \brief true, if check for DSA must be from parent directive, false, if
121 /// from current directive.
Alexey Bataevaac108a2015-06-23 04:51:00 +0000122 OpenMPClauseKind ClauseKindMode;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000123 Sema &SemaRef;
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000124 bool ForceCapturing;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000125
126 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
127
128 DSAVarData getDSA(StackTy::reverse_iterator Iter, VarDecl *D);
Alexey Bataevec3da872014-01-31 05:15:34 +0000129
130 /// \brief Checks if the variable is a local for OpenMP region.
131 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
Alexey Bataeved09d242014-05-28 05:53:51 +0000132
Alexey Bataev758e55e2013-09-06 18:03:48 +0000133public:
Alexey Bataevaac108a2015-06-23 04:51:00 +0000134 explicit DSAStackTy(Sema &S)
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000135 : Stack(1), ClauseKindMode(OMPC_unknown), SemaRef(S),
136 ForceCapturing(false) {}
Alexey Bataev39f915b82015-05-08 10:41:21 +0000137
Alexey Bataevaac108a2015-06-23 04:51:00 +0000138 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
139 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000140
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000141 bool isForceVarCapturing() const { return ForceCapturing; }
142 void setForceVarCapturing(bool V) { ForceCapturing = V; }
143
Alexey Bataev758e55e2013-09-06 18:03:48 +0000144 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000145 Scope *CurScope, SourceLocation Loc) {
146 Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc));
147 Stack.back().DefaultAttrLoc = Loc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000148 }
149
150 void pop() {
151 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
152 Stack.pop_back();
153 }
154
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000155 /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000156 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000157 /// for diagnostics.
158 DeclRefExpr *addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE);
159
Alexey Bataev9c821032015-04-30 04:23:23 +0000160 /// \brief Register specified variable as loop control variable.
161 void addLoopControlVariable(VarDecl *D);
162 /// \brief Check if the specified variable is a loop control variable for
163 /// current region.
164 bool isLoopControlVariable(VarDecl *D);
165
Alexey Bataev758e55e2013-09-06 18:03:48 +0000166 /// \brief Adds explicit data sharing attribute to the specified declaration.
167 void addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A);
168
Alexey Bataev758e55e2013-09-06 18:03:48 +0000169 /// \brief Returns data sharing attributes from top of the stack for the
170 /// specified declaration.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000171 DSAVarData getTopDSA(VarDecl *D, bool FromParent);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000172 /// \brief Returns data-sharing attributes for the specified declaration.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000173 DSAVarData getImplicitDSA(VarDecl *D, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000174 /// \brief Checks if the specified variables has data-sharing attributes which
175 /// match specified \a CPred predicate in any directive which matches \a DPred
176 /// predicate.
177 template <class ClausesPredicate, class DirectivesPredicate>
178 DSAVarData hasDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000179 DirectivesPredicate DPred, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000180 /// \brief Checks if the specified variables has data-sharing attributes which
181 /// match specified \a CPred predicate in any innermost directive which
182 /// matches \a DPred predicate.
183 template <class ClausesPredicate, class DirectivesPredicate>
184 DSAVarData hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000185 DirectivesPredicate DPred,
186 bool FromParent);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000187 /// \brief Checks if the specified variables has explicit data-sharing
188 /// attributes which match specified \a CPred predicate at the specified
189 /// OpenMP region.
190 bool hasExplicitDSA(VarDecl *D,
191 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
192 unsigned Level);
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000193 /// \brief Finds a directive which matches specified \a DPred predicate.
194 template <class NamedDirectivesPredicate>
195 bool hasDirective(NamedDirectivesPredicate DPred, bool FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000196
Alexey Bataev758e55e2013-09-06 18:03:48 +0000197 /// \brief Returns currently analyzed directive.
198 OpenMPDirectiveKind getCurrentDirective() const {
199 return Stack.back().Directive;
200 }
Alexey Bataev549210e2014-06-24 04:39:47 +0000201 /// \brief Returns parent directive.
202 OpenMPDirectiveKind getParentDirective() const {
203 if (Stack.size() > 2)
204 return Stack[Stack.size() - 2].Directive;
205 return OMPD_unknown;
206 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000207
208 /// \brief Set default data sharing attribute to none.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000209 void setDefaultDSANone(SourceLocation Loc) {
210 Stack.back().DefaultAttr = DSA_none;
211 Stack.back().DefaultAttrLoc = Loc;
212 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000213 /// \brief Set default data sharing attribute to shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000214 void setDefaultDSAShared(SourceLocation Loc) {
215 Stack.back().DefaultAttr = DSA_shared;
216 Stack.back().DefaultAttrLoc = Loc;
217 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000218
219 DefaultDataSharingAttributes getDefaultDSA() const {
220 return Stack.back().DefaultAttr;
221 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000222 SourceLocation getDefaultDSALocation() const {
223 return Stack.back().DefaultAttrLoc;
224 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000225
Alexey Bataevf29276e2014-06-18 04:14:57 +0000226 /// \brief Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000227 bool isThreadPrivate(VarDecl *D) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000228 DSAVarData DVar = getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000229 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000230 }
231
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000232 /// \brief Marks current region as ordered (it has an 'ordered' clause).
233 void setOrderedRegion(bool IsOrdered = true) {
234 Stack.back().OrderedRegion = IsOrdered;
235 }
236 /// \brief Returns true, if parent region is ordered (has associated
237 /// 'ordered' clause), false - otherwise.
238 bool isParentOrderedRegion() const {
239 if (Stack.size() > 2)
240 return Stack[Stack.size() - 2].OrderedRegion;
241 return false;
242 }
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000243 /// \brief Marks current region as nowait (it has a 'nowait' clause).
244 void setNowaitRegion(bool IsNowait = true) {
245 Stack.back().NowaitRegion = IsNowait;
246 }
247 /// \brief Returns true, if parent region is nowait (has associated
248 /// 'nowait' clause), false - otherwise.
249 bool isParentNowaitRegion() const {
250 if (Stack.size() > 2)
251 return Stack[Stack.size() - 2].NowaitRegion;
252 return false;
253 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000254
Alexey Bataev9c821032015-04-30 04:23:23 +0000255 /// \brief Set collapse value for the region.
256 void setCollapseNumber(unsigned Val) { Stack.back().CollapseNumber = Val; }
257 /// \brief Return collapse value for region.
258 unsigned getCollapseNumber() const {
259 return Stack.back().CollapseNumber;
260 }
261
Alexey Bataev13314bf2014-10-09 04:18:56 +0000262 /// \brief Marks current target region as one with closely nested teams
263 /// region.
264 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
265 if (Stack.size() > 2)
266 Stack[Stack.size() - 2].InnerTeamsRegionLoc = TeamsRegionLoc;
267 }
268 /// \brief Returns true, if current region has closely nested teams region.
269 bool hasInnerTeamsRegion() const {
270 return getInnerTeamsRegionLoc().isValid();
271 }
272 /// \brief Returns location of the nested teams region (if any).
273 SourceLocation getInnerTeamsRegionLoc() const {
274 if (Stack.size() > 1)
275 return Stack.back().InnerTeamsRegionLoc;
276 return SourceLocation();
277 }
278
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000279 Scope *getCurScope() const { return Stack.back().CurScope; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000280 Scope *getCurScope() { return Stack.back().CurScope; }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000281 SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000282};
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000283bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
284 return isOpenMPParallelDirective(DKind) || DKind == OMPD_task ||
Alexey Bataev13314bf2014-10-09 04:18:56 +0000285 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000286}
Alexey Bataeved09d242014-05-28 05:53:51 +0000287} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000288
289DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter,
290 VarDecl *D) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000291 D = D->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000292 DSAVarData DVar;
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000293 if (Iter == std::prev(Stack.rend())) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000294 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
295 // in a region but not in construct]
296 // File-scope or namespace-scope variables referenced in called routines
297 // in the region are shared unless they appear in a threadprivate
298 // directive.
Alexey Bataev8b9cb982014-07-24 02:33:58 +0000299 if (!D->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D))
Alexey Bataev750a58b2014-03-18 12:19:12 +0000300 DVar.CKind = OMPC_shared;
301
302 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
303 // in a region but not in construct]
304 // Variables with static storage duration that are declared in called
305 // routines in the region are shared.
306 if (D->hasGlobalStorage())
307 DVar.CKind = OMPC_shared;
308
Alexey Bataev758e55e2013-09-06 18:03:48 +0000309 return DVar;
310 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000311
Alexey Bataev758e55e2013-09-06 18:03:48 +0000312 DVar.DKind = Iter->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +0000313 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
314 // in a Construct, C/C++, predetermined, p.1]
315 // Variables with automatic storage duration that are declared in a scope
316 // inside the construct are private.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000317 if (isOpenMPLocal(D, Iter) && D->isLocalVarDecl() &&
318 (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) {
319 DVar.CKind = OMPC_private;
320 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000321 }
322
Alexey Bataev758e55e2013-09-06 18:03:48 +0000323 // Explicitly specified attributes and local variables with predetermined
324 // attributes.
325 if (Iter->SharingMap.count(D)) {
326 DVar.RefExpr = Iter->SharingMap[D].RefExpr;
327 DVar.CKind = Iter->SharingMap[D].Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000328 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000329 return DVar;
330 }
331
332 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
333 // in a Construct, C/C++, implicitly determined, p.1]
334 // In a parallel or task construct, the data-sharing attributes of these
335 // variables are determined by the default clause, if present.
336 switch (Iter->DefaultAttr) {
337 case DSA_shared:
338 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000339 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000340 return DVar;
341 case DSA_none:
342 return DVar;
343 case DSA_unspecified:
344 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
345 // in a Construct, implicitly determined, p.2]
346 // In a parallel construct, if no default clause is present, these
347 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000348 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000349 if (isOpenMPParallelDirective(DVar.DKind) ||
350 isOpenMPTeamsDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000351 DVar.CKind = OMPC_shared;
352 return DVar;
353 }
354
355 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
356 // in a Construct, implicitly determined, p.4]
357 // In a task construct, if no default clause is present, a variable that in
358 // the enclosing context is determined to be shared by all implicit tasks
359 // bound to the current team is shared.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000360 if (DVar.DKind == OMPD_task) {
361 DSAVarData DVarTemp;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000362 for (StackTy::reverse_iterator I = std::next(Iter), EE = Stack.rend();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000363 I != EE; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000364 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
365 // Referenced
Alexey Bataev758e55e2013-09-06 18:03:48 +0000366 // in a Construct, implicitly determined, p.6]
367 // In a task construct, if no default clause is present, a variable
368 // whose data-sharing attribute is not determined by the rules above is
369 // firstprivate.
370 DVarTemp = getDSA(I, D);
371 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000372 DVar.RefExpr = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000373 DVar.DKind = OMPD_task;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000374 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000375 return DVar;
376 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000377 if (isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000378 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000379 }
380 DVar.DKind = OMPD_task;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000381 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000382 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000383 return DVar;
384 }
385 }
386 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
387 // in a Construct, implicitly determined, p.3]
388 // For constructs other than task, if no default clause is present, these
389 // variables inherit their data-sharing attributes from the enclosing
390 // context.
Benjamin Kramer167e9992014-03-02 12:20:24 +0000391 return getDSA(std::next(Iter), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000392}
393
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000394DeclRefExpr *DSAStackTy::addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE) {
395 assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000396 D = D->getCanonicalDecl();
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000397 auto It = Stack.back().AlignedMap.find(D);
398 if (It == Stack.back().AlignedMap.end()) {
399 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
400 Stack.back().AlignedMap[D] = NewDE;
401 return nullptr;
402 } else {
403 assert(It->second && "Unexpected nullptr expr in the aligned map");
404 return It->second;
405 }
406 return nullptr;
407}
408
Alexey Bataev9c821032015-04-30 04:23:23 +0000409void DSAStackTy::addLoopControlVariable(VarDecl *D) {
410 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
411 D = D->getCanonicalDecl();
412 Stack.back().LCVSet.insert(D);
413}
414
415bool DSAStackTy::isLoopControlVariable(VarDecl *D) {
416 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
417 D = D->getCanonicalDecl();
418 return Stack.back().LCVSet.count(D) > 0;
419}
420
Alexey Bataev758e55e2013-09-06 18:03:48 +0000421void DSAStackTy::addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000422 D = D->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000423 if (A == OMPC_threadprivate) {
424 Stack[0].SharingMap[D].Attributes = A;
425 Stack[0].SharingMap[D].RefExpr = E;
426 } else {
427 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
428 Stack.back().SharingMap[D].Attributes = A;
429 Stack.back().SharingMap[D].RefExpr = E;
430 }
431}
432
Alexey Bataeved09d242014-05-28 05:53:51 +0000433bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000434 D = D->getCanonicalDecl();
Alexey Bataevec3da872014-01-31 05:15:34 +0000435 if (Stack.size() > 2) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000436 reverse_iterator I = Iter, E = std::prev(Stack.rend());
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000437 Scope *TopScope = nullptr;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000438 while (I != E && !isParallelOrTaskRegion(I->Directive)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000439 ++I;
440 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000441 if (I == E)
442 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000443 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000444 Scope *CurScope = getCurScope();
445 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000446 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000447 }
448 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000449 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000450 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000451}
452
Alexey Bataev39f915b82015-05-08 10:41:21 +0000453/// \brief Build a variable declaration for OpenMP loop iteration variable.
454static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
455 StringRef Name) {
456 DeclContext *DC = SemaRef.CurContext;
457 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
458 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
459 VarDecl *Decl =
460 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
461 Decl->setImplicit();
462 return Decl;
463}
464
465static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
466 SourceLocation Loc,
467 bool RefersToCapture = false) {
468 D->setReferenced();
469 D->markUsed(S.Context);
470 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
471 SourceLocation(), D, RefersToCapture, Loc, Ty,
472 VK_LValue);
473}
474
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000475DSAStackTy::DSAVarData DSAStackTy::getTopDSA(VarDecl *D, bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000476 D = D->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000477 DSAVarData DVar;
478
479 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
480 // in a Construct, C/C++, predetermined, p.1]
481 // Variables appearing in threadprivate directives are threadprivate.
Samuel Antaof8b50122015-07-13 22:54:53 +0000482 if ((D->getTLSKind() != VarDecl::TLS_None &&
483 !(D->hasAttr<OMPThreadPrivateDeclAttr>() &&
484 SemaRef.getLangOpts().OpenMPUseTLS &&
485 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +0000486 (D->getStorageClass() == SC_Register && D->hasAttr<AsmLabelAttr>() &&
487 !D->isLocalVarDecl())) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000488 addDSA(D, buildDeclRefExpr(SemaRef, D, D->getType().getNonReferenceType(),
489 D->getLocation()),
Alexey Bataevf2453a02015-05-06 07:25:08 +0000490 OMPC_threadprivate);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000491 }
492 if (Stack[0].SharingMap.count(D)) {
493 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
494 DVar.CKind = OMPC_threadprivate;
495 return DVar;
496 }
497
498 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
499 // in a Construct, C/C++, predetermined, p.1]
500 // Variables with automatic storage duration that are declared in a scope
501 // inside the construct are private.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000502 OpenMPDirectiveKind Kind =
503 FromParent ? getParentDirective() : getCurrentDirective();
504 auto StartI = std::next(Stack.rbegin());
505 auto EndI = std::prev(Stack.rend());
506 if (FromParent && StartI != EndI) {
507 StartI = std::next(StartI);
508 }
509 if (!isParallelOrTaskRegion(Kind)) {
Alexey Bataev8b9cb982014-07-24 02:33:58 +0000510 if (isOpenMPLocal(D, StartI) &&
511 ((D->isLocalVarDecl() && (D->getStorageClass() == SC_Auto ||
512 D->getStorageClass() == SC_None)) ||
513 isa<ParmVarDecl>(D))) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000514 DVar.CKind = OMPC_private;
515 return DVar;
Alexander Musman8dba6642014-04-22 13:09:42 +0000516 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000517
Alexey Bataev24b04aa2015-01-16 07:11:33 +0000518 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
519 // in a Construct, C/C++, predetermined, p.4]
520 // Static data members are shared.
Alexey Bataev24b04aa2015-01-16 07:11:33 +0000521 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
522 // in a Construct, C/C++, predetermined, p.7]
523 // Variables with static storage duration that are declared in a scope
524 // inside the construct are shared.
Alexey Bataev42971a32015-01-20 07:03:46 +0000525 if (D->isStaticDataMember() || D->isStaticLocal()) {
526 DSAVarData DVarTemp =
527 hasDSA(D, isOpenMPPrivate, MatchesAlways(), FromParent);
528 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
529 return DVar;
530
Alexey Bataev24b04aa2015-01-16 07:11:33 +0000531 DVar.CKind = OMPC_shared;
532 return DVar;
533 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000534 }
535
536 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +0000537 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
538 Type = SemaRef.getASTContext().getBaseElementType(Type);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000539 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
540 // in a Construct, C/C++, predetermined, p.6]
541 // Variables with const qualified type having no mutable member are
542 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000543 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000544 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000545 if (IsConstant &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000546 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000547 // Variables with const-qualified type having no mutable member may be
548 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000549 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
550 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000551 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
552 return DVar;
553
Alexey Bataev758e55e2013-09-06 18:03:48 +0000554 DVar.CKind = OMPC_shared;
555 return DVar;
556 }
557
Alexey Bataev758e55e2013-09-06 18:03:48 +0000558 // Explicitly specified attributes and local variables with predetermined
559 // attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000560 auto I = std::prev(StartI);
561 if (I->SharingMap.count(D)) {
562 DVar.RefExpr = I->SharingMap[D].RefExpr;
563 DVar.CKind = I->SharingMap[D].Attributes;
564 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000565 }
566
567 return DVar;
568}
569
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000570DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D, bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000571 D = D->getCanonicalDecl();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000572 auto StartI = Stack.rbegin();
573 auto EndI = std::prev(Stack.rend());
574 if (FromParent && StartI != EndI) {
575 StartI = std::next(StartI);
576 }
577 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000578}
579
Alexey Bataevf29276e2014-06-18 04:14:57 +0000580template <class ClausesPredicate, class DirectivesPredicate>
581DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000582 DirectivesPredicate DPred,
583 bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000584 D = D->getCanonicalDecl();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000585 auto StartI = std::next(Stack.rbegin());
586 auto EndI = std::prev(Stack.rend());
587 if (FromParent && StartI != EndI) {
588 StartI = std::next(StartI);
589 }
590 for (auto I = StartI, EE = EndI; I != EE; ++I) {
591 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000592 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000593 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000594 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000595 return DVar;
596 }
597 return DSAVarData();
598}
599
Alexey Bataevf29276e2014-06-18 04:14:57 +0000600template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000601DSAStackTy::DSAVarData
602DSAStackTy::hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
603 DirectivesPredicate DPred, bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000604 D = D->getCanonicalDecl();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000605 auto StartI = std::next(Stack.rbegin());
606 auto EndI = std::prev(Stack.rend());
607 if (FromParent && StartI != EndI) {
608 StartI = std::next(StartI);
609 }
610 for (auto I = StartI, EE = EndI; I != EE; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000611 if (!DPred(I->Directive))
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000612 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000613 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000614 if (CPred(DVar.CKind))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000615 return DVar;
616 return DSAVarData();
617 }
618 return DSAVarData();
619}
620
Alexey Bataevaac108a2015-06-23 04:51:00 +0000621bool DSAStackTy::hasExplicitDSA(
622 VarDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
623 unsigned Level) {
624 if (CPred(ClauseKindMode))
625 return true;
626 if (isClauseParsingMode())
627 ++Level;
628 D = D->getCanonicalDecl();
629 auto StartI = Stack.rbegin();
630 auto EndI = std::prev(Stack.rend());
NAKAMURA Takumi0332eda2015-06-23 10:01:20 +0000631 if (std::distance(StartI, EndI) <= (int)Level)
Alexey Bataevaac108a2015-06-23 04:51:00 +0000632 return false;
633 std::advance(StartI, Level);
634 return (StartI->SharingMap.count(D) > 0) && StartI->SharingMap[D].RefExpr &&
635 CPred(StartI->SharingMap[D].Attributes);
636}
637
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000638template <class NamedDirectivesPredicate>
639bool DSAStackTy::hasDirective(NamedDirectivesPredicate DPred, bool FromParent) {
640 auto StartI = std::next(Stack.rbegin());
641 auto EndI = std::prev(Stack.rend());
642 if (FromParent && StartI != EndI) {
643 StartI = std::next(StartI);
644 }
645 for (auto I = StartI, EE = EndI; I != EE; ++I) {
646 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
647 return true;
648 }
649 return false;
650}
651
Alexey Bataev758e55e2013-09-06 18:03:48 +0000652void Sema::InitDataSharingAttributesStack() {
653 VarDataSharingAttributesStack = new DSAStackTy(*this);
654}
655
656#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
657
Alexey Bataevf841bd92014-12-16 07:00:22 +0000658bool Sema::IsOpenMPCapturedVar(VarDecl *VD) {
659 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000660 VD = VD->getCanonicalDecl();
Alexey Bataev48977c32015-08-04 08:10:48 +0000661 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
662 (!DSAStack->isClauseParsingMode() ||
663 DSAStack->getParentDirective() != OMPD_unknown)) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000664 if (DSAStack->isLoopControlVariable(VD) ||
665 (VD->hasLocalStorage() &&
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000666 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
667 DSAStack->isForceVarCapturing())
Alexey Bataev9c821032015-04-30 04:23:23 +0000668 return true;
Alexey Bataevaac108a2015-06-23 04:51:00 +0000669 auto DVarPrivate = DSAStack->getTopDSA(VD, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000670 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
671 return true;
672 DVarPrivate = DSAStack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(),
Alexey Bataevaac108a2015-06-23 04:51:00 +0000673 DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000674 return DVarPrivate.CKind != OMPC_unknown;
675 }
676 return false;
677}
678
Alexey Bataevaac108a2015-06-23 04:51:00 +0000679bool Sema::isOpenMPPrivateVar(VarDecl *VD, unsigned Level) {
680 assert(LangOpts.OpenMP && "OpenMP is not allowed");
681 return DSAStack->hasExplicitDSA(
682 VD, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level);
683}
684
Alexey Bataeved09d242014-05-28 05:53:51 +0000685void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000686
687void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
688 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000689 Scope *CurScope, SourceLocation Loc) {
690 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000691 PushExpressionEvaluationContext(PotentiallyEvaluated);
692}
693
Alexey Bataevaac108a2015-06-23 04:51:00 +0000694void Sema::StartOpenMPClause(OpenMPClauseKind K) {
695 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000696}
697
Alexey Bataevaac108a2015-06-23 04:51:00 +0000698void Sema::EndOpenMPClause() {
699 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000700}
701
Alexey Bataev758e55e2013-09-06 18:03:48 +0000702void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000703 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
704 // A variable of class type (or array thereof) that appears in a lastprivate
705 // clause requires an accessible, unambiguous default constructor for the
706 // class type, unless the list item is also specified in a firstprivate
707 // clause.
708 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000709 for (auto *C : D->clauses()) {
710 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
711 SmallVector<Expr *, 8> PrivateCopies;
712 for (auto *DE : Clause->varlists()) {
713 if (DE->isValueDependent() || DE->isTypeDependent()) {
714 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000715 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +0000716 }
717 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(DE)->getDecl());
Alexey Bataevbd9fec12015-08-18 06:47:21 +0000718 QualType Type = VD->getType().getNonReferenceType();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000719 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000720 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000721 // Generate helper private variable and initialize it with the
722 // default value. The address of the original variable is replaced
723 // by the address of the new private variable in CodeGen. This new
724 // variable is not added to IdResolver, so the code in the OpenMP
725 // region uses original variable for proper diagnostics.
Alexey Bataev39f915b82015-05-08 10:41:21 +0000726 auto *VDPrivate =
Alexey Bataevf120c0d2015-05-19 07:46:42 +0000727 buildVarDecl(*this, DE->getExprLoc(), Type.getUnqualifiedType(),
728 VD->getName());
Alexey Bataev38e89532015-04-16 04:54:05 +0000729 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
730 if (VDPrivate->isInvalidDecl())
731 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +0000732 PrivateCopies.push_back(buildDeclRefExpr(
733 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +0000734 } else {
735 // The variable is also a firstprivate, so initialization sequence
736 // for private copy is generated already.
737 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000738 }
739 }
Alexey Bataev38e89532015-04-16 04:54:05 +0000740 // Set initializers to private copies if no errors were found.
741 if (PrivateCopies.size() == Clause->varlist_size()) {
742 Clause->setPrivateCopies(PrivateCopies);
743 }
Alexey Bataevf29276e2014-06-18 04:14:57 +0000744 }
745 }
746 }
747
Alexey Bataev758e55e2013-09-06 18:03:48 +0000748 DSAStack->pop();
749 DiscardCleanupsInEvaluationContext();
750 PopExpressionEvaluationContext();
751}
752
Alexander Musman3276a272015-03-21 10:12:56 +0000753static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
754 Expr *NumIterations, Sema &SemaRef,
755 Scope *S);
756
Alexey Bataeva769e072013-03-22 06:34:35 +0000757namespace {
758
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000759class VarDeclFilterCCC : public CorrectionCandidateCallback {
760private:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000761 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +0000762
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000763public:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000764 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +0000765 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000766 NamedDecl *ND = Candidate.getCorrectionDecl();
767 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
768 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000769 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
770 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +0000771 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000772 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000773 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000774};
Alexey Bataeved09d242014-05-28 05:53:51 +0000775} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000776
777ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
778 CXXScopeSpec &ScopeSpec,
779 const DeclarationNameInfo &Id) {
780 LookupResult Lookup(*this, Id, LookupOrdinaryName);
781 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
782
783 if (Lookup.isAmbiguous())
784 return ExprError();
785
786 VarDecl *VD;
787 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +0000788 if (TypoCorrection Corrected = CorrectTypo(
789 Id, LookupOrdinaryName, CurScope, nullptr,
790 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +0000791 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000792 PDiag(Lookup.empty()
793 ? diag::err_undeclared_var_use_suggest
794 : diag::err_omp_expected_var_arg_suggest)
795 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +0000796 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000797 } else {
Richard Smithf9b15102013-08-17 00:46:16 +0000798 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
799 : diag::err_omp_expected_var_arg)
800 << Id.getName();
801 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000802 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000803 } else {
804 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000805 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000806 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
807 return ExprError();
808 }
809 }
810 Lookup.suppressDiagnostics();
811
812 // OpenMP [2.9.2, Syntax, C/C++]
813 // Variables must be file-scope, namespace-scope, or static block-scope.
814 if (!VD->hasGlobalStorage()) {
815 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000816 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
817 bool IsDecl =
818 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000819 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +0000820 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
821 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000822 return ExprError();
823 }
824
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000825 VarDecl *CanonicalVD = VD->getCanonicalDecl();
826 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000827 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
828 // A threadprivate directive for file-scope variables must appear outside
829 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000830 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
831 !getCurLexicalContext()->isTranslationUnit()) {
832 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000833 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
834 bool IsDecl =
835 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
836 Diag(VD->getLocation(),
837 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
838 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000839 return ExprError();
840 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000841 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
842 // A threadprivate directive for static class member variables must appear
843 // in the class definition, in the same scope in which the member
844 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000845 if (CanonicalVD->isStaticDataMember() &&
846 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
847 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000848 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
849 bool IsDecl =
850 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
851 Diag(VD->getLocation(),
852 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
853 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000854 return ExprError();
855 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000856 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
857 // A threadprivate directive for namespace-scope variables must appear
858 // outside any definition or declaration other than the namespace
859 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000860 if (CanonicalVD->getDeclContext()->isNamespace() &&
861 (!getCurLexicalContext()->isFileContext() ||
862 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
863 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000864 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
865 bool IsDecl =
866 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
867 Diag(VD->getLocation(),
868 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
869 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000870 return ExprError();
871 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000872 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
873 // A threadprivate directive for static block-scope variables must appear
874 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000875 if (CanonicalVD->isStaticLocal() && CurScope &&
876 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000877 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000878 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
879 bool IsDecl =
880 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
881 Diag(VD->getLocation(),
882 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
883 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000884 return ExprError();
885 }
886
887 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
888 // A threadprivate directive must lexically precede all references to any
889 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000890 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000891 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +0000892 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000893 return ExprError();
894 }
895
896 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +0000897 ExprResult DE = buildDeclRefExpr(*this, VD, ExprType, Id.getLoc());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000898 return DE;
899}
900
Alexey Bataeved09d242014-05-28 05:53:51 +0000901Sema::DeclGroupPtrTy
902Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
903 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000904 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000905 CurContext->addDecl(D);
906 return DeclGroupPtrTy::make(DeclGroupRef(D));
907 }
908 return DeclGroupPtrTy();
909}
910
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000911namespace {
912class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
913 Sema &SemaRef;
914
915public:
916 bool VisitDeclRefExpr(const DeclRefExpr *E) {
917 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
918 if (VD->hasLocalStorage()) {
919 SemaRef.Diag(E->getLocStart(),
920 diag::err_omp_local_var_in_threadprivate_init)
921 << E->getSourceRange();
922 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
923 << VD << VD->getSourceRange();
924 return true;
925 }
926 }
927 return false;
928 }
929 bool VisitStmt(const Stmt *S) {
930 for (auto Child : S->children()) {
931 if (Child && Visit(Child))
932 return true;
933 }
934 return false;
935 }
Alexey Bataev23b69422014-06-18 07:08:49 +0000936 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000937};
938} // namespace
939
Alexey Bataeved09d242014-05-28 05:53:51 +0000940OMPThreadPrivateDecl *
941Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000942 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +0000943 for (auto &RefExpr : VarList) {
944 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000945 VarDecl *VD = cast<VarDecl>(DE->getDecl());
946 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +0000947
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000948 QualType QType = VD->getType();
949 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
950 // It will be analyzed later.
951 Vars.push_back(DE);
952 continue;
953 }
954
Alexey Bataeva769e072013-03-22 06:34:35 +0000955 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
956 // A threadprivate variable must not have an incomplete type.
957 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000958 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000959 continue;
960 }
961
962 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
963 // A threadprivate variable must not have a reference type.
964 if (VD->getType()->isReferenceType()) {
965 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000966 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
967 bool IsDecl =
968 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
969 Diag(VD->getLocation(),
970 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
971 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000972 continue;
973 }
974
Samuel Antaof8b50122015-07-13 22:54:53 +0000975 // Check if this is a TLS variable. If TLS is not being supported, produce
976 // the corresponding diagnostic.
977 if ((VD->getTLSKind() != VarDecl::TLS_None &&
978 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
979 getLangOpts().OpenMPUseTLS &&
980 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +0000981 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
982 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +0000983 Diag(ILoc, diag::err_omp_var_thread_local)
984 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +0000985 bool IsDecl =
986 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
987 Diag(VD->getLocation(),
988 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
989 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000990 continue;
991 }
992
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000993 // Check if initial value of threadprivate variable reference variable with
994 // local storage (it is not supported by runtime).
995 if (auto Init = VD->getAnyInitializer()) {
996 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000997 if (Checker.Visit(Init))
998 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000999 }
1000
Alexey Bataeved09d242014-05-28 05:53:51 +00001001 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00001002 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00001003 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1004 Context, SourceRange(Loc, Loc)));
1005 if (auto *ML = Context.getASTMutationListener())
1006 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00001007 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001008 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001009 if (!Vars.empty()) {
1010 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1011 Vars);
1012 D->setAccess(AS_public);
1013 }
1014 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00001015}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001016
Alexey Bataev7ff55242014-06-19 09:13:45 +00001017static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
1018 const VarDecl *VD, DSAStackTy::DSAVarData DVar,
1019 bool IsLoopIterVar = false) {
1020 if (DVar.RefExpr) {
1021 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1022 << getOpenMPClauseName(DVar.CKind);
1023 return;
1024 }
1025 enum {
1026 PDSA_StaticMemberShared,
1027 PDSA_StaticLocalVarShared,
1028 PDSA_LoopIterVarPrivate,
1029 PDSA_LoopIterVarLinear,
1030 PDSA_LoopIterVarLastprivate,
1031 PDSA_ConstVarShared,
1032 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001033 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001034 PDSA_LocalVarPrivate,
1035 PDSA_Implicit
1036 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001037 bool ReportHint = false;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001038 auto ReportLoc = VD->getLocation();
Alexey Bataev7ff55242014-06-19 09:13:45 +00001039 if (IsLoopIterVar) {
1040 if (DVar.CKind == OMPC_private)
1041 Reason = PDSA_LoopIterVarPrivate;
1042 else if (DVar.CKind == OMPC_lastprivate)
1043 Reason = PDSA_LoopIterVarLastprivate;
1044 else
1045 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001046 } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) {
1047 Reason = PDSA_TaskVarFirstprivate;
1048 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001049 } else if (VD->isStaticLocal())
1050 Reason = PDSA_StaticLocalVarShared;
1051 else if (VD->isStaticDataMember())
1052 Reason = PDSA_StaticMemberShared;
1053 else if (VD->isFileVarDecl())
1054 Reason = PDSA_GlobalVarShared;
1055 else if (VD->getType().isConstant(SemaRef.getASTContext()))
1056 Reason = PDSA_ConstVarShared;
Alexey Bataevbae9a792014-06-27 10:37:06 +00001057 else if (VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00001058 ReportHint = true;
1059 Reason = PDSA_LocalVarPrivate;
1060 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00001061 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001062 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00001063 << Reason << ReportHint
1064 << getOpenMPDirectiveName(Stack->getCurrentDirective());
1065 } else if (DVar.ImplicitDSALoc.isValid()) {
1066 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1067 << getOpenMPClauseName(DVar.CKind);
1068 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00001069}
1070
Alexey Bataev758e55e2013-09-06 18:03:48 +00001071namespace {
1072class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1073 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001074 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001075 bool ErrorFound;
1076 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001077 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev4acb8592014-07-07 13:01:15 +00001078 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +00001079
Alexey Bataev758e55e2013-09-06 18:03:48 +00001080public:
1081 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001082 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001083 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +00001084 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1085 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001086
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001087 auto DVar = Stack->getTopDSA(VD, false);
1088 // Check if the variable has explicit DSA set and stop analysis if it so.
1089 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001090
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001091 auto ELoc = E->getExprLoc();
1092 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001093 // The default(none) clause requires that each variable that is referenced
1094 // in the construct, and does not have a predetermined data-sharing
1095 // attribute, must have its data-sharing attribute explicitly determined
1096 // by being listed in a data-sharing attribute clause.
1097 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001098 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001099 VarsWithInheritedDSA.count(VD) == 0) {
1100 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001101 return;
1102 }
1103
1104 // OpenMP [2.9.3.6, Restrictions, p.2]
1105 // A list item that appears in a reduction clause of the innermost
1106 // enclosing worksharing or parallel construct may not be accessed in an
1107 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +00001108 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001109 [](OpenMPDirectiveKind K) -> bool {
1110 return isOpenMPParallelDirective(K) ||
Alexey Bataev13314bf2014-10-09 04:18:56 +00001111 isOpenMPWorksharingDirective(K) ||
1112 isOpenMPTeamsDirective(K);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001113 },
1114 false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001115 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
1116 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001117 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1118 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001119 return;
1120 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001121
1122 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001123 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001124 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001125 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001126 }
1127 }
1128 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001129 for (auto *C : S->clauses()) {
1130 // Skip analysis of arguments of implicitly defined firstprivate clause
1131 // for task directives.
1132 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1133 for (auto *CC : C->children()) {
1134 if (CC)
1135 Visit(CC);
1136 }
1137 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001138 }
1139 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001140 for (auto *C : S->children()) {
1141 if (C && !isa<OMPExecutableDirective>(C))
1142 Visit(C);
1143 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001144 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001145
1146 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001147 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001148 llvm::DenseMap<VarDecl *, Expr *> &getVarsWithInheritedDSA() {
1149 return VarsWithInheritedDSA;
1150 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001151
Alexey Bataev7ff55242014-06-19 09:13:45 +00001152 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1153 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00001154};
Alexey Bataeved09d242014-05-28 05:53:51 +00001155} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001156
Alexey Bataevbae9a792014-06-27 10:37:06 +00001157void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001158 switch (DKind) {
1159 case OMPD_parallel: {
1160 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1161 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001162 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001163 std::make_pair(".global_tid.", KmpInt32PtrTy),
1164 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1165 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001166 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001167 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1168 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001169 break;
1170 }
1171 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001172 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001173 std::make_pair(StringRef(), QualType()) // __context with shared vars
1174 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001175 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1176 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001177 break;
1178 }
1179 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001180 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001181 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001182 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001183 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1184 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001185 break;
1186 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001187 case OMPD_for_simd: {
1188 Sema::CapturedParamNameType Params[] = {
1189 std::make_pair(StringRef(), QualType()) // __context with shared vars
1190 };
1191 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1192 Params);
1193 break;
1194 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001195 case OMPD_sections: {
1196 Sema::CapturedParamNameType Params[] = {
1197 std::make_pair(StringRef(), QualType()) // __context with shared vars
1198 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001199 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1200 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001201 break;
1202 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001203 case OMPD_section: {
1204 Sema::CapturedParamNameType Params[] = {
1205 std::make_pair(StringRef(), QualType()) // __context with shared vars
1206 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001207 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1208 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001209 break;
1210 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001211 case OMPD_single: {
1212 Sema::CapturedParamNameType Params[] = {
1213 std::make_pair(StringRef(), QualType()) // __context with shared vars
1214 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001215 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1216 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001217 break;
1218 }
Alexander Musman80c22892014-07-17 08:54:58 +00001219 case OMPD_master: {
1220 Sema::CapturedParamNameType Params[] = {
1221 std::make_pair(StringRef(), QualType()) // __context with shared vars
1222 };
1223 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1224 Params);
1225 break;
1226 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001227 case OMPD_critical: {
1228 Sema::CapturedParamNameType Params[] = {
1229 std::make_pair(StringRef(), QualType()) // __context with shared vars
1230 };
1231 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1232 Params);
1233 break;
1234 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001235 case OMPD_parallel_for: {
1236 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1237 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1238 Sema::CapturedParamNameType Params[] = {
1239 std::make_pair(".global_tid.", KmpInt32PtrTy),
1240 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1241 std::make_pair(StringRef(), QualType()) // __context with shared vars
1242 };
1243 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1244 Params);
1245 break;
1246 }
Alexander Musmane4e893b2014-09-23 09:33:00 +00001247 case OMPD_parallel_for_simd: {
1248 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1249 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1250 Sema::CapturedParamNameType Params[] = {
1251 std::make_pair(".global_tid.", KmpInt32PtrTy),
1252 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1253 std::make_pair(StringRef(), QualType()) // __context with shared vars
1254 };
1255 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1256 Params);
1257 break;
1258 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001259 case OMPD_parallel_sections: {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001260 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1261 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001262 Sema::CapturedParamNameType Params[] = {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001263 std::make_pair(".global_tid.", KmpInt32PtrTy),
1264 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001265 std::make_pair(StringRef(), QualType()) // __context with shared vars
1266 };
1267 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1268 Params);
1269 break;
1270 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001271 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001272 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001273 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1274 FunctionProtoType::ExtProtoInfo EPI;
1275 EPI.Variadic = true;
1276 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001277 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001278 std::make_pair(".global_tid.", KmpInt32Ty),
1279 std::make_pair(".part_id.", KmpInt32Ty),
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001280 std::make_pair(".privates.",
1281 Context.VoidPtrTy.withConst().withRestrict()),
1282 std::make_pair(
1283 ".copy_fn.",
1284 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001285 std::make_pair(StringRef(), QualType()) // __context with shared vars
1286 };
1287 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1288 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001289 // Mark this captured region as inlined, because we don't use outlined
1290 // function directly.
1291 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1292 AlwaysInlineAttr::CreateImplicit(
1293 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001294 break;
1295 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001296 case OMPD_ordered: {
1297 Sema::CapturedParamNameType Params[] = {
1298 std::make_pair(StringRef(), QualType()) // __context with shared vars
1299 };
1300 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1301 Params);
1302 break;
1303 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001304 case OMPD_atomic: {
1305 Sema::CapturedParamNameType Params[] = {
1306 std::make_pair(StringRef(), QualType()) // __context with shared vars
1307 };
1308 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1309 Params);
1310 break;
1311 }
Michael Wong65f367f2015-07-21 13:44:28 +00001312 case OMPD_target_data:
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001313 case OMPD_target: {
1314 Sema::CapturedParamNameType Params[] = {
1315 std::make_pair(StringRef(), QualType()) // __context with shared vars
1316 };
1317 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1318 Params);
1319 break;
1320 }
Alexey Bataev13314bf2014-10-09 04:18:56 +00001321 case OMPD_teams: {
1322 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1323 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1324 Sema::CapturedParamNameType Params[] = {
1325 std::make_pair(".global_tid.", KmpInt32PtrTy),
1326 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1327 std::make_pair(StringRef(), QualType()) // __context with shared vars
1328 };
1329 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1330 Params);
1331 break;
1332 }
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001333 case OMPD_taskgroup: {
1334 Sema::CapturedParamNameType Params[] = {
1335 std::make_pair(StringRef(), QualType()) // __context with shared vars
1336 };
1337 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1338 Params);
1339 break;
1340 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001341 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001342 case OMPD_taskyield:
1343 case OMPD_barrier:
1344 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001345 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001346 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00001347 case OMPD_flush:
Alexey Bataev9959db52014-05-06 10:08:46 +00001348 llvm_unreachable("OpenMP Directive is not allowed");
1349 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001350 llvm_unreachable("Unknown OpenMP directive");
1351 }
1352}
1353
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001354StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1355 ArrayRef<OMPClause *> Clauses) {
1356 if (!S.isUsable()) {
1357 ActOnCapturedRegionError();
1358 return StmtError();
1359 }
Alexey Bataev040d5402015-05-12 08:35:28 +00001360 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001361 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001362 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001363 Clause->getClauseKind() == OMPC_copyprivate ||
1364 (getLangOpts().OpenMPUseTLS &&
1365 getASTContext().getTargetInfo().isTLSSupported() &&
1366 Clause->getClauseKind() == OMPC_copyin)) {
1367 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001368 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001369 for (auto *VarRef : Clause->children()) {
1370 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001371 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001372 }
1373 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001374 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev040d5402015-05-12 08:35:28 +00001375 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
1376 Clause->getClauseKind() == OMPC_schedule) {
1377 // Mark all variables in private list clauses as used in inner region.
1378 // Required for proper codegen of combined directives.
1379 // TODO: add processing for other clauses.
1380 if (auto *E = cast_or_null<Expr>(
1381 cast<OMPScheduleClause>(Clause)->getHelperChunkSize())) {
1382 MarkDeclarationsReferencedInExpr(E);
1383 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001384 }
1385 }
1386 return ActOnCapturedRegionEnd(S.get());
1387}
1388
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001389static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1390 OpenMPDirectiveKind CurrentRegion,
1391 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001392 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001393 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001394 // Allowed nesting of constructs
1395 // +------------------+-----------------+------------------------------------+
1396 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1397 // +------------------+-----------------+------------------------------------+
1398 // | parallel | parallel | * |
1399 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001400 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001401 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001402 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001403 // | parallel | simd | * |
1404 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001405 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001406 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001407 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001408 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001409 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001410 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001411 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001412 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001413 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001414 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001415 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001416 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001417 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001418 // | parallel | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001419 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001420 // | parallel | cancellation | |
1421 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001422 // | parallel | cancel | ! |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001423 // +------------------+-----------------+------------------------------------+
1424 // | for | parallel | * |
1425 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001426 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001427 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001428 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001429 // | for | simd | * |
1430 // | for | sections | + |
1431 // | for | section | + |
1432 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001433 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001434 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001435 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001436 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001437 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001438 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001439 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001440 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001441 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001442 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001443 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001444 // | for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001445 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001446 // | for | cancellation | |
1447 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001448 // | for | cancel | ! |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001449 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001450 // | master | parallel | * |
1451 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001452 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001453 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001454 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001455 // | master | simd | * |
1456 // | master | sections | + |
1457 // | master | section | + |
1458 // | master | single | + |
1459 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001460 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001461 // | master |parallel sections| * |
1462 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001463 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001464 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001465 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001466 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001467 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001468 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001469 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001470 // | master | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001471 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001472 // | master | cancellation | |
1473 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001474 // | master | cancel | |
Alexander Musman80c22892014-07-17 08:54:58 +00001475 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001476 // | critical | parallel | * |
1477 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001478 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001479 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001480 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001481 // | critical | simd | * |
1482 // | critical | sections | + |
1483 // | critical | section | + |
1484 // | critical | single | + |
1485 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001486 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001487 // | critical |parallel sections| * |
1488 // | critical | task | * |
1489 // | critical | taskyield | * |
1490 // | critical | barrier | + |
1491 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001492 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001493 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001494 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001495 // | critical | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001496 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001497 // | critical | cancellation | |
1498 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001499 // | critical | cancel | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001500 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001501 // | simd | parallel | |
1502 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001503 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00001504 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001505 // | simd | critical | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001506 // | simd | simd | |
1507 // | simd | sections | |
1508 // | simd | section | |
1509 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001510 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001511 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001512 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001513 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00001514 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001515 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001516 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001517 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00001518 // | simd | flush | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001519 // | simd | ordered | |
Alexey Bataev0162e452014-07-22 10:10:35 +00001520 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001521 // | simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001522 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001523 // | simd | cancellation | |
1524 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001525 // | simd | cancel | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001526 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00001527 // | for simd | parallel | |
1528 // | for simd | for | |
1529 // | for simd | for simd | |
1530 // | for simd | master | |
1531 // | for simd | critical | |
1532 // | for simd | simd | |
1533 // | for simd | sections | |
1534 // | for simd | section | |
1535 // | for simd | single | |
1536 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001537 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001538 // | for simd |parallel sections| |
1539 // | for simd | task | |
1540 // | for simd | taskyield | |
1541 // | for simd | barrier | |
1542 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001543 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001544 // | for simd | flush | |
1545 // | for simd | ordered | |
1546 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001547 // | for simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001548 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001549 // | for simd | cancellation | |
1550 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001551 // | for simd | cancel | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001552 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00001553 // | parallel for simd| parallel | |
1554 // | parallel for simd| for | |
1555 // | parallel for simd| for simd | |
1556 // | parallel for simd| master | |
1557 // | parallel for simd| critical | |
1558 // | parallel for simd| simd | |
1559 // | parallel for simd| sections | |
1560 // | parallel for simd| section | |
1561 // | parallel for simd| single | |
1562 // | parallel for simd| parallel for | |
1563 // | parallel for simd|parallel for simd| |
1564 // | parallel for simd|parallel sections| |
1565 // | parallel for simd| task | |
1566 // | parallel for simd| taskyield | |
1567 // | parallel for simd| barrier | |
1568 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001569 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001570 // | parallel for simd| flush | |
1571 // | parallel for simd| ordered | |
1572 // | parallel for simd| atomic | |
1573 // | parallel for simd| target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001574 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001575 // | parallel for simd| cancellation | |
1576 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001577 // | parallel for simd| cancel | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001578 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001579 // | sections | parallel | * |
1580 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001581 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001582 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001583 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001584 // | sections | simd | * |
1585 // | sections | sections | + |
1586 // | sections | section | * |
1587 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001588 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001589 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001590 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001591 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001592 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001593 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001594 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001595 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001596 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001597 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001598 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001599 // | sections | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001600 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001601 // | sections | cancellation | |
1602 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001603 // | sections | cancel | ! |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001604 // +------------------+-----------------+------------------------------------+
1605 // | section | parallel | * |
1606 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001607 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001608 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001609 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001610 // | section | simd | * |
1611 // | section | sections | + |
1612 // | section | section | + |
1613 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001614 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001615 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001616 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001617 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001618 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001619 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001620 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001621 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001622 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001623 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001624 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001625 // | section | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001626 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001627 // | section | cancellation | |
1628 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001629 // | section | cancel | ! |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001630 // +------------------+-----------------+------------------------------------+
1631 // | single | parallel | * |
1632 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001633 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001634 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001635 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001636 // | single | simd | * |
1637 // | single | sections | + |
1638 // | single | section | + |
1639 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001640 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001641 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001642 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001643 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001644 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001645 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001646 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001647 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001648 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001649 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001650 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001651 // | single | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001652 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001653 // | single | cancellation | |
1654 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001655 // | single | cancel | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001656 // +------------------+-----------------+------------------------------------+
1657 // | parallel for | parallel | * |
1658 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001659 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001660 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001661 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001662 // | parallel for | simd | * |
1663 // | parallel for | sections | + |
1664 // | parallel for | section | + |
1665 // | parallel for | single | + |
1666 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001667 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001668 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001669 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001670 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001671 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001672 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001673 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001674 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001675 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001676 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001677 // | parallel for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001678 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001679 // | parallel for | cancellation | |
1680 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001681 // | parallel for | cancel | ! |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001682 // +------------------+-----------------+------------------------------------+
1683 // | parallel sections| parallel | * |
1684 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001685 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001686 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001687 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001688 // | parallel sections| simd | * |
1689 // | parallel sections| sections | + |
1690 // | parallel sections| section | * |
1691 // | parallel sections| single | + |
1692 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001693 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001694 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001695 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001696 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001697 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001698 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001699 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001700 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001701 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001702 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001703 // | parallel sections| target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001704 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001705 // | parallel sections| cancellation | |
1706 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001707 // | parallel sections| cancel | ! |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001708 // +------------------+-----------------+------------------------------------+
1709 // | task | parallel | * |
1710 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001711 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001712 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001713 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001714 // | task | simd | * |
1715 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001716 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001717 // | task | single | + |
1718 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001719 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001720 // | task |parallel sections| * |
1721 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001722 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001723 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001724 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001725 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001726 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001727 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001728 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001729 // | task | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001730 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001731 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00001732 // | | point | ! |
1733 // | task | cancel | ! |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001734 // +------------------+-----------------+------------------------------------+
1735 // | ordered | parallel | * |
1736 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001737 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001738 // | ordered | master | * |
1739 // | ordered | critical | * |
1740 // | ordered | simd | * |
1741 // | ordered | sections | + |
1742 // | ordered | section | + |
1743 // | ordered | single | + |
1744 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001745 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001746 // | ordered |parallel sections| * |
1747 // | ordered | task | * |
1748 // | ordered | taskyield | * |
1749 // | ordered | barrier | + |
1750 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001751 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001752 // | ordered | flush | * |
1753 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001754 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001755 // | ordered | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001756 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001757 // | ordered | cancellation | |
1758 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001759 // | ordered | cancel | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001760 // +------------------+-----------------+------------------------------------+
1761 // | atomic | parallel | |
1762 // | atomic | for | |
1763 // | atomic | for simd | |
1764 // | atomic | master | |
1765 // | atomic | critical | |
1766 // | atomic | simd | |
1767 // | atomic | sections | |
1768 // | atomic | section | |
1769 // | atomic | single | |
1770 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001771 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001772 // | atomic |parallel sections| |
1773 // | atomic | task | |
1774 // | atomic | taskyield | |
1775 // | atomic | barrier | |
1776 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001777 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001778 // | atomic | flush | |
1779 // | atomic | ordered | |
1780 // | atomic | atomic | |
1781 // | atomic | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001782 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001783 // | atomic | cancellation | |
1784 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001785 // | atomic | cancel | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001786 // +------------------+-----------------+------------------------------------+
1787 // | target | parallel | * |
1788 // | target | for | * |
1789 // | target | for simd | * |
1790 // | target | master | * |
1791 // | target | critical | * |
1792 // | target | simd | * |
1793 // | target | sections | * |
1794 // | target | section | * |
1795 // | target | single | * |
1796 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001797 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001798 // | target |parallel sections| * |
1799 // | target | task | * |
1800 // | target | taskyield | * |
1801 // | target | barrier | * |
1802 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001803 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001804 // | target | flush | * |
1805 // | target | ordered | * |
1806 // | target | atomic | * |
1807 // | target | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001808 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001809 // | target | cancellation | |
1810 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001811 // | target | cancel | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001812 // +------------------+-----------------+------------------------------------+
1813 // | teams | parallel | * |
1814 // | teams | for | + |
1815 // | teams | for simd | + |
1816 // | teams | master | + |
1817 // | teams | critical | + |
1818 // | teams | simd | + |
1819 // | teams | sections | + |
1820 // | teams | section | + |
1821 // | teams | single | + |
1822 // | teams | parallel for | * |
1823 // | teams |parallel for simd| * |
1824 // | teams |parallel sections| * |
1825 // | teams | task | + |
1826 // | teams | taskyield | + |
1827 // | teams | barrier | + |
1828 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00001829 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001830 // | teams | flush | + |
1831 // | teams | ordered | + |
1832 // | teams | atomic | + |
1833 // | teams | target | + |
1834 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001835 // | teams | cancellation | |
1836 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001837 // | teams | cancel | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001838 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00001839 if (Stack->getCurScope()) {
1840 auto ParentRegion = Stack->getParentDirective();
1841 bool NestingProhibited = false;
1842 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001843 enum {
1844 NoRecommend,
1845 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00001846 ShouldBeInOrderedRegion,
1847 ShouldBeInTargetRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001848 } Recommend = NoRecommend;
Alexey Bataev549210e2014-06-24 04:39:47 +00001849 if (isOpenMPSimdDirective(ParentRegion)) {
1850 // OpenMP [2.16, Nesting of Regions]
1851 // OpenMP constructs may not be nested inside a simd region.
1852 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
1853 return true;
1854 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001855 if (ParentRegion == OMPD_atomic) {
1856 // OpenMP [2.16, Nesting of Regions]
1857 // OpenMP constructs may not be nested inside an atomic region.
1858 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
1859 return true;
1860 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001861 if (CurrentRegion == OMPD_section) {
1862 // OpenMP [2.7.2, sections Construct, Restrictions]
1863 // Orphaned section directives are prohibited. That is, the section
1864 // directives must appear within the sections construct and must not be
1865 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001866 if (ParentRegion != OMPD_sections &&
1867 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001868 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
1869 << (ParentRegion != OMPD_unknown)
1870 << getOpenMPDirectiveName(ParentRegion);
1871 return true;
1872 }
1873 return false;
1874 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001875 // Allow some constructs to be orphaned (they could be used in functions,
1876 // called from OpenMP regions with the required preconditions).
1877 if (ParentRegion == OMPD_unknown)
1878 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00001879 if (CurrentRegion == OMPD_cancellation_point ||
1880 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001881 // OpenMP [2.16, Nesting of Regions]
1882 // A cancellation point construct for which construct-type-clause is
1883 // taskgroup must be nested inside a task construct. A cancellation
1884 // point construct for which construct-type-clause is not taskgroup must
1885 // be closely nested inside an OpenMP construct that matches the type
1886 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00001887 // A cancel construct for which construct-type-clause is taskgroup must be
1888 // nested inside a task construct. A cancel construct for which
1889 // construct-type-clause is not taskgroup must be closely nested inside an
1890 // OpenMP construct that matches the type specified in
1891 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001892 NestingProhibited =
1893 !((CancelRegion == OMPD_parallel && ParentRegion == OMPD_parallel) ||
1894 (CancelRegion == OMPD_for && ParentRegion == OMPD_for) ||
1895 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
1896 (CancelRegion == OMPD_sections &&
1897 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections)));
1898 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00001899 // OpenMP [2.16, Nesting of Regions]
1900 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00001901 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00001902 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
1903 ParentRegion == OMPD_task;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001904 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
1905 // OpenMP [2.16, Nesting of Regions]
1906 // A critical region may not be nested (closely or otherwise) inside a
1907 // critical region with the same name. Note that this restriction is not
1908 // sufficient to prevent deadlock.
1909 SourceLocation PreviousCriticalLoc;
1910 bool DeadLock =
1911 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
1912 OpenMPDirectiveKind K,
1913 const DeclarationNameInfo &DNI,
1914 SourceLocation Loc)
1915 ->bool {
1916 if (K == OMPD_critical &&
1917 DNI.getName() == CurrentName.getName()) {
1918 PreviousCriticalLoc = Loc;
1919 return true;
1920 } else
1921 return false;
1922 },
1923 false /* skip top directive */);
1924 if (DeadLock) {
1925 SemaRef.Diag(StartLoc,
1926 diag::err_omp_prohibited_region_critical_same_name)
1927 << CurrentName.getName();
1928 if (PreviousCriticalLoc.isValid())
1929 SemaRef.Diag(PreviousCriticalLoc,
1930 diag::note_omp_previous_critical_region);
1931 return true;
1932 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001933 } else if (CurrentRegion == OMPD_barrier) {
1934 // OpenMP [2.16, Nesting of Regions]
1935 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00001936 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001937 NestingProhibited =
1938 isOpenMPWorksharingDirective(ParentRegion) ||
1939 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1940 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00001941 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00001942 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00001943 // OpenMP [2.16, Nesting of Regions]
1944 // A worksharing region may not be closely nested inside a worksharing,
1945 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001946 NestingProhibited =
Alexander Musmanf82886e2014-09-18 05:12:34 +00001947 isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001948 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1949 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
1950 Recommend = ShouldBeInParallelRegion;
1951 } else if (CurrentRegion == OMPD_ordered) {
1952 // OpenMP [2.16, Nesting of Regions]
1953 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00001954 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001955 // An ordered region must be closely nested inside a loop region (or
1956 // parallel loop region) with an ordered clause.
1957 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00001958 ParentRegion == OMPD_task ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001959 !Stack->isParentOrderedRegion();
1960 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00001961 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
1962 // OpenMP [2.16, Nesting of Regions]
1963 // If specified, a teams construct must be contained within a target
1964 // construct.
1965 NestingProhibited = ParentRegion != OMPD_target;
1966 Recommend = ShouldBeInTargetRegion;
1967 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
1968 }
1969 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
1970 // OpenMP [2.16, Nesting of Regions]
1971 // distribute, parallel, parallel sections, parallel workshare, and the
1972 // parallel loop and parallel loop SIMD constructs are the only OpenMP
1973 // constructs that can be closely nested in the teams region.
1974 // TODO: add distribute directive.
1975 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion);
1976 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00001977 }
1978 if (NestingProhibited) {
1979 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001980 << CloseNesting << getOpenMPDirectiveName(ParentRegion) << Recommend
1981 << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00001982 return true;
1983 }
1984 }
1985 return false;
1986}
1987
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001988static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
1989 ArrayRef<OMPClause *> Clauses,
1990 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
1991 bool ErrorFound = false;
1992 unsigned NamedModifiersNumber = 0;
1993 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
1994 OMPD_unknown + 1);
1995 for (const auto *C : Clauses) {
1996 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
1997 // At most one if clause without a directive-name-modifier can appear on
1998 // the directive.
1999 OpenMPDirectiveKind CurNM = IC->getNameModifier();
2000 if (FoundNameModifiers[CurNM]) {
2001 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
2002 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
2003 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
2004 ErrorFound = true;
2005 } else if (CurNM != OMPD_unknown)
2006 ++NamedModifiersNumber;
2007 FoundNameModifiers[CurNM] = IC;
2008 if (CurNM == OMPD_unknown)
2009 continue;
2010 // Check if the specified name modifier is allowed for the current
2011 // directive.
2012 // At most one if clause with the particular directive-name-modifier can
2013 // appear on the directive.
2014 bool MatchFound = false;
2015 for (auto NM : AllowedNameModifiers) {
2016 if (CurNM == NM) {
2017 MatchFound = true;
2018 break;
2019 }
2020 }
2021 if (!MatchFound) {
2022 S.Diag(IC->getNameModifierLoc(),
2023 diag::err_omp_wrong_if_directive_name_modifier)
2024 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
2025 ErrorFound = true;
2026 }
2027 }
2028 }
2029 // If any if clause on the directive includes a directive-name-modifier then
2030 // all if clauses on the directive must include a directive-name-modifier.
2031 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
2032 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
2033 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
2034 diag::err_omp_no_more_if_clause);
2035 } else {
2036 std::string Values;
2037 std::string Sep(", ");
2038 unsigned AllowedCnt = 0;
2039 unsigned TotalAllowedNum =
2040 AllowedNameModifiers.size() - NamedModifiersNumber;
2041 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
2042 ++Cnt) {
2043 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
2044 if (!FoundNameModifiers[NM]) {
2045 Values += "'";
2046 Values += getOpenMPDirectiveName(NM);
2047 Values += "'";
2048 if (AllowedCnt + 2 == TotalAllowedNum)
2049 Values += " or ";
2050 else if (AllowedCnt + 1 != TotalAllowedNum)
2051 Values += Sep;
2052 ++AllowedCnt;
2053 }
2054 }
2055 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
2056 diag::err_omp_unnamed_if_clause)
2057 << (TotalAllowedNum > 1) << Values;
2058 }
2059 ErrorFound = true;
2060 }
2061 return ErrorFound;
2062}
2063
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002064StmtResult Sema::ActOnOpenMPExecutableDirective(
2065 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
2066 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
2067 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002068 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002069 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
2070 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00002071 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002072
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002073 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev68446b72014-07-18 07:47:19 +00002074 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002075 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00002076 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00002077 if (AStmt) {
2078 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
2079
2080 // Check default data sharing attributes for referenced variables.
2081 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
2082 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
2083 if (DSAChecker.isErrorFound())
2084 return StmtError();
2085 // Generate list of implicitly defined firstprivate variables.
2086 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00002087
2088 if (!DSAChecker.getImplicitFirstprivate().empty()) {
2089 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
2090 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
2091 SourceLocation(), SourceLocation())) {
2092 ClausesWithImplicit.push_back(Implicit);
2093 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
2094 DSAChecker.getImplicitFirstprivate().size();
2095 } else
2096 ErrorFound = true;
2097 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002098 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002099
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002100 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002101 switch (Kind) {
2102 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00002103 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
2104 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002105 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002106 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002107 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002108 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2109 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002110 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002111 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002112 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2113 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002114 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00002115 case OMPD_for_simd:
2116 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2117 EndLoc, VarsWithInheritedDSA);
2118 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002119 case OMPD_sections:
2120 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
2121 EndLoc);
2122 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002123 case OMPD_section:
2124 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00002125 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002126 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
2127 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002128 case OMPD_single:
2129 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
2130 EndLoc);
2131 break;
Alexander Musman80c22892014-07-17 08:54:58 +00002132 case OMPD_master:
2133 assert(ClausesWithImplicit.empty() &&
2134 "No clauses are allowed for 'omp master' directive");
2135 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
2136 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002137 case OMPD_critical:
2138 assert(ClausesWithImplicit.empty() &&
2139 "No clauses are allowed for 'omp critical' directive");
2140 Res = ActOnOpenMPCriticalDirective(DirName, AStmt, StartLoc, EndLoc);
2141 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002142 case OMPD_parallel_for:
2143 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
2144 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002145 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00002146 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00002147 case OMPD_parallel_for_simd:
2148 Res = ActOnOpenMPParallelForSimdDirective(
2149 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002150 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00002151 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002152 case OMPD_parallel_sections:
2153 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
2154 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002155 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002156 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002157 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002158 Res =
2159 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002160 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002161 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00002162 case OMPD_taskyield:
2163 assert(ClausesWithImplicit.empty() &&
2164 "No clauses are allowed for 'omp taskyield' directive");
2165 assert(AStmt == nullptr &&
2166 "No associated statement allowed for 'omp taskyield' directive");
2167 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
2168 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002169 case OMPD_barrier:
2170 assert(ClausesWithImplicit.empty() &&
2171 "No clauses are allowed for 'omp barrier' directive");
2172 assert(AStmt == nullptr &&
2173 "No associated statement allowed for 'omp barrier' directive");
2174 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
2175 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00002176 case OMPD_taskwait:
2177 assert(ClausesWithImplicit.empty() &&
2178 "No clauses are allowed for 'omp taskwait' directive");
2179 assert(AStmt == nullptr &&
2180 "No associated statement allowed for 'omp taskwait' directive");
2181 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
2182 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002183 case OMPD_taskgroup:
2184 assert(ClausesWithImplicit.empty() &&
2185 "No clauses are allowed for 'omp taskgroup' directive");
2186 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
2187 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00002188 case OMPD_flush:
2189 assert(AStmt == nullptr &&
2190 "No associated statement allowed for 'omp flush' directive");
2191 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
2192 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002193 case OMPD_ordered:
2194 assert(ClausesWithImplicit.empty() &&
2195 "No clauses are allowed for 'omp ordered' directive");
2196 Res = ActOnOpenMPOrderedDirective(AStmt, StartLoc, EndLoc);
2197 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00002198 case OMPD_atomic:
2199 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
2200 EndLoc);
2201 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002202 case OMPD_teams:
2203 Res =
2204 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
2205 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002206 case OMPD_target:
2207 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
2208 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002209 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002210 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002211 case OMPD_cancellation_point:
2212 assert(ClausesWithImplicit.empty() &&
2213 "No clauses are allowed for 'omp cancellation point' directive");
2214 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
2215 "cancellation point' directive");
2216 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
2217 break;
Alexey Bataev80909872015-07-02 11:25:17 +00002218 case OMPD_cancel:
2219 assert(ClausesWithImplicit.empty() &&
2220 "No clauses are allowed for 'omp cancel' directive");
2221 assert(AStmt == nullptr &&
2222 "No associated statement allowed for 'omp cancel' directive");
2223 Res = ActOnOpenMPCancelDirective(StartLoc, EndLoc, CancelRegion);
2224 break;
Michael Wong65f367f2015-07-21 13:44:28 +00002225 case OMPD_target_data:
2226 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
2227 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002228 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00002229 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002230 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002231 llvm_unreachable("OpenMP Directive is not allowed");
2232 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002233 llvm_unreachable("Unknown OpenMP directive");
2234 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002235
Alexey Bataev4acb8592014-07-07 13:01:15 +00002236 for (auto P : VarsWithInheritedDSA) {
2237 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
2238 << P.first << P.second->getSourceRange();
2239 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002240 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
2241
2242 if (!AllowedNameModifiers.empty())
2243 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
2244 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002245
Alexey Bataeved09d242014-05-28 05:53:51 +00002246 if (ErrorFound)
2247 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002248 return Res;
2249}
2250
2251StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
2252 Stmt *AStmt,
2253 SourceLocation StartLoc,
2254 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002255 if (!AStmt)
2256 return StmtError();
2257
Alexey Bataev9959db52014-05-06 10:08:46 +00002258 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
2259 // 1.2.2 OpenMP Language Terminology
2260 // Structured block - An executable statement with a single entry at the
2261 // top and a single exit at the bottom.
2262 // The point of exit cannot be a branch out of the structured block.
2263 // longjmp() and throw() must not violate the entry/exit criteria.
2264 CS->getCapturedDecl()->setNothrow();
2265
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002266 getCurFunction()->setHasBranchProtectedScope();
2267
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00002268 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
2269 AStmt);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002270}
2271
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002272namespace {
2273/// \brief Helper class for checking canonical form of the OpenMP loops and
2274/// extracting iteration space of each loop in the loop nest, that will be used
2275/// for IR generation.
2276class OpenMPIterationSpaceChecker {
2277 /// \brief Reference to Sema.
2278 Sema &SemaRef;
2279 /// \brief A location for diagnostics (when there is no some better location).
2280 SourceLocation DefaultLoc;
2281 /// \brief A location for diagnostics (when increment is not compatible).
2282 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002283 /// \brief A source location for referring to loop init later.
2284 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002285 /// \brief A source location for referring to condition later.
2286 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002287 /// \brief A source location for referring to increment later.
2288 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002289 /// \brief Loop variable.
2290 VarDecl *Var;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002291 /// \brief Reference to loop variable.
2292 DeclRefExpr *VarRef;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002293 /// \brief Lower bound (initializer for the var).
2294 Expr *LB;
2295 /// \brief Upper bound.
2296 Expr *UB;
2297 /// \brief Loop step (increment).
2298 Expr *Step;
2299 /// \brief This flag is true when condition is one of:
2300 /// Var < UB
2301 /// Var <= UB
2302 /// UB > Var
2303 /// UB >= Var
2304 bool TestIsLessOp;
2305 /// \brief This flag is true when condition is strict ( < or > ).
2306 bool TestIsStrictOp;
2307 /// \brief This flag is true when step is subtracted on each iteration.
2308 bool SubtractStep;
2309
2310public:
2311 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
2312 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
Alexander Musmana5f070a2014-10-01 06:03:56 +00002313 InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()),
2314 IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002315 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
2316 TestIsStrictOp(false), SubtractStep(false) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002317 /// \brief Check init-expr for canonical loop form and save loop counter
2318 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00002319 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002320 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
2321 /// for less/greater and for strict/non-strict comparison.
2322 bool CheckCond(Expr *S);
2323 /// \brief Check incr-expr for canonical loop form and return true if it
2324 /// does not conform, otherwise save loop step (#Step).
2325 bool CheckInc(Expr *S);
2326 /// \brief Return the loop counter variable.
2327 VarDecl *GetLoopVar() const { return Var; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002328 /// \brief Return the reference expression to loop counter variable.
2329 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002330 /// \brief Source range of the loop init.
2331 SourceRange GetInitSrcRange() const { return InitSrcRange; }
2332 /// \brief Source range of the loop condition.
2333 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
2334 /// \brief Source range of the loop increment.
2335 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
2336 /// \brief True if the step should be subtracted.
2337 bool ShouldSubtractStep() const { return SubtractStep; }
2338 /// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00002339 Expr *BuildNumIterations(Scope *S, const bool LimitedType) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00002340 /// \brief Build the precondition expression for the loops.
2341 Expr *BuildPreCond(Scope *S, Expr *Cond) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002342 /// \brief Build reference expression to the counter be used for codegen.
2343 Expr *BuildCounterVar() const;
Alexey Bataeva8899172015-08-06 12:30:57 +00002344 /// \brief Build reference expression to the private counter be used for
2345 /// codegen.
2346 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002347 /// \brief Build initization of the counter be used for codegen.
2348 Expr *BuildCounterInit() const;
2349 /// \brief Build step of the counter be used for codegen.
2350 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002351 /// \brief Return true if any expression is dependent.
2352 bool Dependent() const;
2353
2354private:
2355 /// \brief Check the right-hand side of an assignment in the increment
2356 /// expression.
2357 bool CheckIncRHS(Expr *RHS);
2358 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002359 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002360 /// \brief Helper to set upper bound.
2361 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, const SourceRange &SR,
2362 const SourceLocation &SL);
2363 /// \brief Helper to set loop increment.
2364 bool SetStep(Expr *NewStep, bool Subtract);
2365};
2366
2367bool OpenMPIterationSpaceChecker::Dependent() const {
2368 if (!Var) {
2369 assert(!LB && !UB && !Step);
2370 return false;
2371 }
2372 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
2373 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
2374}
2375
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002376template <typename T>
2377static T *getExprAsWritten(T *E) {
2378 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
2379 E = ExprTemp->getSubExpr();
2380
2381 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
2382 E = MTE->GetTemporaryExpr();
2383
2384 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
2385 E = Binder->getSubExpr();
2386
2387 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
2388 E = ICE->getSubExprAsWritten();
2389 return E->IgnoreParens();
2390}
2391
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002392bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
2393 DeclRefExpr *NewVarRefExpr,
2394 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002395 // State consistency checking to ensure correct usage.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002396 assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
2397 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002398 if (!NewVar || !NewLB)
2399 return true;
2400 Var = NewVar;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002401 VarRef = NewVarRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002402 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
2403 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00002404 if ((Ctor->isCopyOrMoveConstructor() ||
2405 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
2406 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002407 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002408 LB = NewLB;
2409 return false;
2410}
2411
2412bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
2413 const SourceRange &SR,
2414 const SourceLocation &SL) {
2415 // State consistency checking to ensure correct usage.
2416 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
2417 !TestIsLessOp && !TestIsStrictOp);
2418 if (!NewUB)
2419 return true;
2420 UB = NewUB;
2421 TestIsLessOp = LessOp;
2422 TestIsStrictOp = StrictOp;
2423 ConditionSrcRange = SR;
2424 ConditionLoc = SL;
2425 return false;
2426}
2427
2428bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
2429 // State consistency checking to ensure correct usage.
2430 assert(Var != nullptr && LB != nullptr && Step == nullptr);
2431 if (!NewStep)
2432 return true;
2433 if (!NewStep->isValueDependent()) {
2434 // Check that the step is integer expression.
2435 SourceLocation StepLoc = NewStep->getLocStart();
2436 ExprResult Val =
2437 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
2438 if (Val.isInvalid())
2439 return true;
2440 NewStep = Val.get();
2441
2442 // OpenMP [2.6, Canonical Loop Form, Restrictions]
2443 // If test-expr is of form var relational-op b and relational-op is < or
2444 // <= then incr-expr must cause var to increase on each iteration of the
2445 // loop. If test-expr is of form var relational-op b and relational-op is
2446 // > or >= then incr-expr must cause var to decrease on each iteration of
2447 // the loop.
2448 // If test-expr is of form b relational-op var and relational-op is < or
2449 // <= then incr-expr must cause var to decrease on each iteration of the
2450 // loop. If test-expr is of form b relational-op var and relational-op is
2451 // > or >= then incr-expr must cause var to increase on each iteration of
2452 // the loop.
2453 llvm::APSInt Result;
2454 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
2455 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
2456 bool IsConstNeg =
2457 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00002458 bool IsConstPos =
2459 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002460 bool IsConstZero = IsConstant && !Result.getBoolValue();
2461 if (UB && (IsConstZero ||
2462 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00002463 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002464 SemaRef.Diag(NewStep->getExprLoc(),
2465 diag::err_omp_loop_incr_not_compatible)
2466 << Var << TestIsLessOp << NewStep->getSourceRange();
2467 SemaRef.Diag(ConditionLoc,
2468 diag::note_omp_loop_cond_requres_compatible_incr)
2469 << TestIsLessOp << ConditionSrcRange;
2470 return true;
2471 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002472 if (TestIsLessOp == Subtract) {
2473 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
2474 NewStep).get();
2475 Subtract = !Subtract;
2476 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002477 }
2478
2479 Step = NewStep;
2480 SubtractStep = Subtract;
2481 return false;
2482}
2483
Alexey Bataev9c821032015-04-30 04:23:23 +00002484bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002485 // Check init-expr for canonical loop form and save loop counter
2486 // variable - #Var and its initialization value - #LB.
2487 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
2488 // var = lb
2489 // integer-type var = lb
2490 // random-access-iterator-type var = lb
2491 // pointer-type var = lb
2492 //
2493 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00002494 if (EmitDiags) {
2495 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
2496 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002497 return true;
2498 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002499 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002500 if (Expr *E = dyn_cast<Expr>(S))
2501 S = E->IgnoreParens();
2502 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2503 if (BO->getOpcode() == BO_Assign)
2504 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002505 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002506 BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002507 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
2508 if (DS->isSingleDecl()) {
2509 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00002510 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002511 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00002512 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002513 SemaRef.Diag(S->getLocStart(),
2514 diag::ext_omp_loop_not_canonical_init)
2515 << S->getSourceRange();
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002516 return SetVarAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002517 }
2518 }
2519 }
2520 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
2521 if (CE->getOperator() == OO_Equal)
2522 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002523 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
2524 CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002525
Alexey Bataev9c821032015-04-30 04:23:23 +00002526 if (EmitDiags) {
2527 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
2528 << S->getSourceRange();
2529 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002530 return true;
2531}
2532
Alexey Bataev23b69422014-06-18 07:08:49 +00002533/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002534/// variable (which may be the loop variable) if possible.
2535static const VarDecl *GetInitVarDecl(const Expr *E) {
2536 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00002537 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002538 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002539 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
2540 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00002541 if ((Ctor->isCopyOrMoveConstructor() ||
2542 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
2543 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002544 E = CE->getArg(0)->IgnoreParenImpCasts();
2545 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
2546 if (!DRE)
2547 return nullptr;
2548 return dyn_cast<VarDecl>(DRE->getDecl());
2549}
2550
2551bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
2552 // Check test-expr for canonical form, save upper-bound UB, flags for
2553 // less/greater and for strict/non-strict comparison.
2554 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2555 // var relational-op b
2556 // b relational-op var
2557 //
2558 if (!S) {
2559 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
2560 return true;
2561 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002562 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002563 SourceLocation CondLoc = S->getLocStart();
2564 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2565 if (BO->isRelationalOp()) {
2566 if (GetInitVarDecl(BO->getLHS()) == Var)
2567 return SetUB(BO->getRHS(),
2568 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
2569 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2570 BO->getSourceRange(), BO->getOperatorLoc());
2571 if (GetInitVarDecl(BO->getRHS()) == Var)
2572 return SetUB(BO->getLHS(),
2573 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
2574 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2575 BO->getSourceRange(), BO->getOperatorLoc());
2576 }
2577 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2578 if (CE->getNumArgs() == 2) {
2579 auto Op = CE->getOperator();
2580 switch (Op) {
2581 case OO_Greater:
2582 case OO_GreaterEqual:
2583 case OO_Less:
2584 case OO_LessEqual:
2585 if (GetInitVarDecl(CE->getArg(0)) == Var)
2586 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
2587 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2588 CE->getOperatorLoc());
2589 if (GetInitVarDecl(CE->getArg(1)) == Var)
2590 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
2591 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2592 CE->getOperatorLoc());
2593 break;
2594 default:
2595 break;
2596 }
2597 }
2598 }
2599 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
2600 << S->getSourceRange() << Var;
2601 return true;
2602}
2603
2604bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
2605 // RHS of canonical loop form increment can be:
2606 // var + incr
2607 // incr + var
2608 // var - incr
2609 //
2610 RHS = RHS->IgnoreParenImpCasts();
2611 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
2612 if (BO->isAdditiveOp()) {
2613 bool IsAdd = BO->getOpcode() == BO_Add;
2614 if (GetInitVarDecl(BO->getLHS()) == Var)
2615 return SetStep(BO->getRHS(), !IsAdd);
2616 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
2617 return SetStep(BO->getLHS(), false);
2618 }
2619 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
2620 bool IsAdd = CE->getOperator() == OO_Plus;
2621 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
2622 if (GetInitVarDecl(CE->getArg(0)) == Var)
2623 return SetStep(CE->getArg(1), !IsAdd);
2624 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
2625 return SetStep(CE->getArg(0), false);
2626 }
2627 }
2628 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2629 << RHS->getSourceRange() << Var;
2630 return true;
2631}
2632
2633bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
2634 // Check incr-expr for canonical loop form and return true if it
2635 // does not conform.
2636 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2637 // ++var
2638 // var++
2639 // --var
2640 // var--
2641 // var += incr
2642 // var -= incr
2643 // var = var + incr
2644 // var = incr + var
2645 // var = var - incr
2646 //
2647 if (!S) {
2648 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
2649 return true;
2650 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002651 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002652 S = S->IgnoreParens();
2653 if (auto UO = dyn_cast<UnaryOperator>(S)) {
2654 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
2655 return SetStep(
2656 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
2657 (UO->isDecrementOp() ? -1 : 1)).get(),
2658 false);
2659 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
2660 switch (BO->getOpcode()) {
2661 case BO_AddAssign:
2662 case BO_SubAssign:
2663 if (GetInitVarDecl(BO->getLHS()) == Var)
2664 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
2665 break;
2666 case BO_Assign:
2667 if (GetInitVarDecl(BO->getLHS()) == Var)
2668 return CheckIncRHS(BO->getRHS());
2669 break;
2670 default:
2671 break;
2672 }
2673 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2674 switch (CE->getOperator()) {
2675 case OO_PlusPlus:
2676 case OO_MinusMinus:
2677 if (GetInitVarDecl(CE->getArg(0)) == Var)
2678 return SetStep(
2679 SemaRef.ActOnIntegerConstant(
2680 CE->getLocStart(),
2681 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
2682 false);
2683 break;
2684 case OO_PlusEqual:
2685 case OO_MinusEqual:
2686 if (GetInitVarDecl(CE->getArg(0)) == Var)
2687 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
2688 break;
2689 case OO_Equal:
2690 if (GetInitVarDecl(CE->getArg(0)) == Var)
2691 return CheckIncRHS(CE->getArg(1));
2692 break;
2693 default:
2694 break;
2695 }
2696 }
2697 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2698 << S->getSourceRange() << Var;
2699 return true;
2700}
Alexander Musmana5f070a2014-10-01 06:03:56 +00002701
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002702namespace {
2703// Transform variables declared in GNU statement expressions to new ones to
2704// avoid crash on codegen.
2705class TransformToNewDefs : public TreeTransform<TransformToNewDefs> {
2706 typedef TreeTransform<TransformToNewDefs> BaseTransform;
2707
2708public:
2709 TransformToNewDefs(Sema &SemaRef) : BaseTransform(SemaRef) {}
2710
2711 Decl *TransformDefinition(SourceLocation Loc, Decl *D) {
2712 if (auto *VD = cast<VarDecl>(D))
2713 if (!isa<ParmVarDecl>(D) && !isa<VarTemplateSpecializationDecl>(D) &&
2714 !isa<ImplicitParamDecl>(D)) {
2715 auto *NewVD = VarDecl::Create(
2716 SemaRef.Context, VD->getDeclContext(), VD->getLocStart(),
2717 VD->getLocation(), VD->getIdentifier(), VD->getType(),
2718 VD->getTypeSourceInfo(), VD->getStorageClass());
2719 NewVD->setTSCSpec(VD->getTSCSpec());
2720 NewVD->setInit(VD->getInit());
2721 NewVD->setInitStyle(VD->getInitStyle());
2722 NewVD->setExceptionVariable(VD->isExceptionVariable());
2723 NewVD->setNRVOVariable(VD->isNRVOVariable());
2724 NewVD->setCXXForRangeDecl(VD->isInExternCXXContext());
2725 NewVD->setConstexpr(VD->isConstexpr());
2726 NewVD->setInitCapture(VD->isInitCapture());
2727 NewVD->setPreviousDeclInSameBlockScope(
2728 VD->isPreviousDeclInSameBlockScope());
2729 VD->getDeclContext()->addHiddenDecl(NewVD);
2730 transformedLocalDecl(VD, NewVD);
2731 return NewVD;
2732 }
2733 return BaseTransform::TransformDefinition(Loc, D);
2734 }
2735
2736 ExprResult TransformDeclRefExpr(DeclRefExpr *E) {
2737 if (auto *NewD = TransformDecl(E->getExprLoc(), E->getDecl()))
2738 if (E->getDecl() != NewD) {
2739 NewD->setReferenced();
2740 NewD->markUsed(SemaRef.Context);
2741 return DeclRefExpr::Create(
2742 SemaRef.Context, E->getQualifierLoc(), E->getTemplateKeywordLoc(),
2743 cast<ValueDecl>(NewD), E->refersToEnclosingVariableOrCapture(),
2744 E->getNameInfo(), E->getType(), E->getValueKind());
2745 }
2746 return BaseTransform::TransformDeclRefExpr(E);
2747 }
2748};
2749}
2750
Alexander Musmana5f070a2014-10-01 06:03:56 +00002751/// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00002752Expr *
2753OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S,
2754 const bool LimitedType) const {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002755 TransformToNewDefs Transform(SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00002756 ExprResult Diff;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002757 auto VarType = Var->getType().getNonReferenceType();
2758 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00002759 SemaRef.getLangOpts().CPlusPlus) {
2760 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002761 auto *UBExpr = TestIsLessOp ? UB : LB;
2762 auto *LBExpr = TestIsLessOp ? LB : UB;
2763 Expr *Upper = Transform.TransformExpr(UBExpr).get();
2764 Expr *Lower = Transform.TransformExpr(LBExpr).get();
2765 if (!Upper || !Lower)
2766 return nullptr;
2767 Upper = SemaRef.PerformImplicitConversion(Upper, UBExpr->getType(),
2768 Sema::AA_Converting,
2769 /*AllowExplicit=*/true)
2770 .get();
2771 Lower = SemaRef.PerformImplicitConversion(Lower, LBExpr->getType(),
2772 Sema::AA_Converting,
2773 /*AllowExplicit=*/true)
2774 .get();
2775 if (!Upper || !Lower)
2776 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002777
2778 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
2779
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002780 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00002781 // BuildBinOp already emitted error, this one is to point user to upper
2782 // and lower bound, and to tell what is passed to 'operator-'.
2783 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
2784 << Upper->getSourceRange() << Lower->getSourceRange();
2785 return nullptr;
2786 }
2787 }
2788
2789 if (!Diff.isUsable())
2790 return nullptr;
2791
2792 // Upper - Lower [- 1]
2793 if (TestIsStrictOp)
2794 Diff = SemaRef.BuildBinOp(
2795 S, DefaultLoc, BO_Sub, Diff.get(),
2796 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
2797 if (!Diff.isUsable())
2798 return nullptr;
2799
2800 // Upper - Lower [- 1] + Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002801 auto NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
2802 if (NewStep.isInvalid())
2803 return nullptr;
2804 NewStep = SemaRef.PerformImplicitConversion(
2805 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
2806 /*AllowExplicit=*/true);
2807 if (NewStep.isInvalid())
2808 return nullptr;
2809 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00002810 if (!Diff.isUsable())
2811 return nullptr;
2812
2813 // Parentheses (for dumping/debugging purposes only).
2814 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
2815 if (!Diff.isUsable())
2816 return nullptr;
2817
2818 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002819 NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
2820 if (NewStep.isInvalid())
2821 return nullptr;
2822 NewStep = SemaRef.PerformImplicitConversion(
2823 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
2824 /*AllowExplicit=*/true);
2825 if (NewStep.isInvalid())
2826 return nullptr;
2827 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00002828 if (!Diff.isUsable())
2829 return nullptr;
2830
Alexander Musman174b3ca2014-10-06 11:16:29 +00002831 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002832 QualType Type = Diff.get()->getType();
2833 auto &C = SemaRef.Context;
2834 bool UseVarType = VarType->hasIntegerRepresentation() &&
2835 C.getTypeSize(Type) > C.getTypeSize(VarType);
2836 if (!Type->isIntegerType() || UseVarType) {
2837 unsigned NewSize =
2838 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
2839 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
2840 : Type->hasSignedIntegerRepresentation();
2841 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
2842 Diff = SemaRef.PerformImplicitConversion(
2843 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
2844 if (!Diff.isUsable())
2845 return nullptr;
2846 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00002847 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00002848 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
2849 if (NewSize != C.getTypeSize(Type)) {
2850 if (NewSize < C.getTypeSize(Type)) {
2851 assert(NewSize == 64 && "incorrect loop var size");
2852 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
2853 << InitSrcRange << ConditionSrcRange;
2854 }
2855 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002856 NewSize, Type->hasSignedIntegerRepresentation() ||
2857 C.getTypeSize(Type) < NewSize);
Alexander Musman174b3ca2014-10-06 11:16:29 +00002858 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
2859 Sema::AA_Converting, true);
2860 if (!Diff.isUsable())
2861 return nullptr;
2862 }
2863 }
2864
Alexander Musmana5f070a2014-10-01 06:03:56 +00002865 return Diff.get();
2866}
2867
Alexey Bataev62dbb972015-04-22 11:59:37 +00002868Expr *OpenMPIterationSpaceChecker::BuildPreCond(Scope *S, Expr *Cond) const {
2869 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
2870 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
2871 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002872 TransformToNewDefs Transform(SemaRef);
2873
2874 auto NewLB = Transform.TransformExpr(LB);
2875 auto NewUB = Transform.TransformExpr(UB);
2876 if (NewLB.isInvalid() || NewUB.isInvalid())
2877 return Cond;
2878 NewLB = SemaRef.PerformImplicitConversion(NewLB.get(), LB->getType(),
2879 Sema::AA_Converting,
2880 /*AllowExplicit=*/true);
2881 NewUB = SemaRef.PerformImplicitConversion(NewUB.get(), UB->getType(),
2882 Sema::AA_Converting,
2883 /*AllowExplicit=*/true);
2884 if (NewLB.isInvalid() || NewUB.isInvalid())
2885 return Cond;
Alexey Bataev62dbb972015-04-22 11:59:37 +00002886 auto CondExpr = SemaRef.BuildBinOp(
2887 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
2888 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002889 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002890 if (CondExpr.isUsable()) {
2891 CondExpr = SemaRef.PerformImplicitConversion(
2892 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
2893 /*AllowExplicit=*/true);
2894 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00002895 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
2896 // Otherwise use original loop conditon and evaluate it in runtime.
2897 return CondExpr.isUsable() ? CondExpr.get() : Cond;
2898}
2899
Alexander Musmana5f070a2014-10-01 06:03:56 +00002900/// \brief Build reference expression to the counter be used for codegen.
2901Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const {
Alexey Bataeva8899172015-08-06 12:30:57 +00002902 return buildDeclRefExpr(SemaRef, Var, Var->getType().getNonReferenceType(),
2903 DefaultLoc);
2904}
2905
2906Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
2907 if (Var && !Var->isInvalidDecl()) {
2908 auto Type = Var->getType().getNonReferenceType();
2909 auto *PrivateVar = buildVarDecl(SemaRef, DefaultLoc, Type, Var->getName());
2910 if (PrivateVar->isInvalidDecl())
2911 return nullptr;
2912 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
2913 }
2914 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002915}
2916
2917/// \brief Build initization of the counter be used for codegen.
2918Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
2919
2920/// \brief Build step of the counter be used for codegen.
2921Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
2922
2923/// \brief Iteration space of a single for loop.
2924struct LoopIterationSpace {
Alexey Bataev62dbb972015-04-22 11:59:37 +00002925 /// \brief Condition of the loop.
2926 Expr *PreCond;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002927 /// \brief This expression calculates the number of iterations in the loop.
2928 /// It is always possible to calculate it before starting the loop.
2929 Expr *NumIterations;
2930 /// \brief The loop counter variable.
2931 Expr *CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00002932 /// \brief Private loop counter variable.
2933 Expr *PrivateCounterVar;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002934 /// \brief This is initializer for the initial value of #CounterVar.
2935 Expr *CounterInit;
2936 /// \brief This is step for the #CounterVar used to generate its update:
2937 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
2938 Expr *CounterStep;
2939 /// \brief Should step be subtracted?
2940 bool Subtract;
2941 /// \brief Source range of the loop init.
2942 SourceRange InitSrcRange;
2943 /// \brief Source range of the loop condition.
2944 SourceRange CondSrcRange;
2945 /// \brief Source range of the loop increment.
2946 SourceRange IncSrcRange;
2947};
2948
Alexey Bataev23b69422014-06-18 07:08:49 +00002949} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002950
Alexey Bataev9c821032015-04-30 04:23:23 +00002951void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
2952 assert(getLangOpts().OpenMP && "OpenMP is not active.");
2953 assert(Init && "Expected loop in canonical form.");
2954 unsigned CollapseIteration = DSAStack->getCollapseNumber();
2955 if (CollapseIteration > 0 &&
2956 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
2957 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
2958 if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
2959 DSAStack->addLoopControlVariable(ISC.GetLoopVar());
2960 }
2961 DSAStack->setCollapseNumber(CollapseIteration - 1);
2962 }
2963}
2964
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002965/// \brief Called on a for stmt to check and extract its iteration space
2966/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00002967static bool CheckOpenMPIterationSpace(
2968 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
2969 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00002970 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002971 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
2972 LoopIterationSpace &ResultIterSpace) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002973 // OpenMP [2.6, Canonical Loop Form]
2974 // for (init-expr; test-expr; incr-expr) structured-block
2975 auto For = dyn_cast_or_null<ForStmt>(S);
2976 if (!For) {
2977 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00002978 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
2979 << getOpenMPDirectiveName(DKind) << NestedLoopCount
2980 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
2981 if (NestedLoopCount > 1) {
2982 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
2983 SemaRef.Diag(DSA.getConstructLoc(),
2984 diag::note_omp_collapse_ordered_expr)
2985 << 2 << CollapseLoopCountExpr->getSourceRange()
2986 << OrderedLoopCountExpr->getSourceRange();
2987 else if (CollapseLoopCountExpr)
2988 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
2989 diag::note_omp_collapse_ordered_expr)
2990 << 0 << CollapseLoopCountExpr->getSourceRange();
2991 else
2992 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
2993 diag::note_omp_collapse_ordered_expr)
2994 << 1 << OrderedLoopCountExpr->getSourceRange();
2995 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002996 return true;
2997 }
2998 assert(For->getBody());
2999
3000 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
3001
3002 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003003 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003004 if (ISC.CheckInit(Init)) {
3005 return true;
3006 }
3007
3008 bool HasErrors = false;
3009
3010 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003011 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003012
3013 // OpenMP [2.6, Canonical Loop Form]
3014 // Var is one of the following:
3015 // A variable of signed or unsigned integer type.
3016 // For C++, a variable of a random access iterator type.
3017 // For C, a variable of a pointer type.
Alexey Bataeva8899172015-08-06 12:30:57 +00003018 auto VarType = Var->getType().getNonReferenceType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003019 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
3020 !VarType->isPointerType() &&
3021 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
3022 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
3023 << SemaRef.getLangOpts().CPlusPlus;
3024 HasErrors = true;
3025 }
3026
Alexey Bataev4acb8592014-07-07 13:01:15 +00003027 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
3028 // Construct
3029 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3030 // parallel for construct is (are) private.
3031 // The loop iteration variable in the associated for-loop of a simd construct
3032 // with just one associated for-loop is linear with a constant-linear-step
3033 // that is the increment of the associated for-loop.
3034 // Exclude loop var from the list of variables with implicitly defined data
3035 // sharing attributes.
Benjamin Kramerad8e0792014-10-10 15:32:48 +00003036 VarsWithImplicitDSA.erase(Var);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003037
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003038 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
3039 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00003040 // The loop iteration variable in the associated for-loop of a simd construct
3041 // with just one associated for-loop may be listed in a linear clause with a
3042 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00003043 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3044 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003045 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003046 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
3047 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
3048 // declared in the loop and it is predetermined as a private.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003049 auto PredeterminedCKind =
3050 isOpenMPSimdDirective(DKind)
3051 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
3052 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003053 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataev0c024df2015-05-12 09:02:07 +00003054 DVar.CKind != OMPC_threadprivate && DVar.CKind != PredeterminedCKind) ||
Alexander Musmanf82886e2014-09-18 05:12:34 +00003055 (isOpenMPWorksharingDirective(DKind) && !isOpenMPSimdDirective(DKind) &&
3056 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private &&
Alexey Bataev0c024df2015-05-12 09:02:07 +00003057 DVar.CKind != OMPC_lastprivate && DVar.CKind != OMPC_threadprivate)) &&
3058 ((DVar.CKind != OMPC_private && DVar.CKind != OMPC_threadprivate) ||
3059 DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003060 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00003061 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
3062 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataevf2453a02015-05-06 07:25:08 +00003063 if (DVar.RefExpr == nullptr)
3064 DVar.CKind = PredeterminedCKind;
3065 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, /*IsLoopIterVar=*/true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003066 HasErrors = true;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003067 } else if (LoopVarRefExpr != nullptr) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00003068 // Make the loop iteration variable private (for worksharing constructs),
3069 // linear (for simd directives with the only one associated loop) or
Alexey Bataev10e775f2015-07-30 11:36:16 +00003070 // lastprivate (for simd directives with several collapsed or ordered
3071 // loops).
Alexey Bataev9aba41c2014-11-14 04:08:45 +00003072 if (DVar.CKind == OMPC_unknown)
3073 DVar = DSA.hasDSA(Var, isOpenMPPrivate, MatchesAlways(),
3074 /*FromParent=*/false);
Alexey Bataev9c821032015-04-30 04:23:23 +00003075 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003076 }
3077
Alexey Bataev7ff55242014-06-19 09:13:45 +00003078 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00003079
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003080 // Check test-expr.
3081 HasErrors |= ISC.CheckCond(For->getCond());
3082
3083 // Check incr-expr.
3084 HasErrors |= ISC.CheckInc(For->getInc());
3085
Alexander Musmana5f070a2014-10-01 06:03:56 +00003086 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003087 return HasErrors;
3088
Alexander Musmana5f070a2014-10-01 06:03:56 +00003089 // Build the loop's iteration space representation.
Alexey Bataev62dbb972015-04-22 11:59:37 +00003090 ResultIterSpace.PreCond = ISC.BuildPreCond(DSA.getCurScope(), For->getCond());
Alexander Musman174b3ca2014-10-06 11:16:29 +00003091 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
3092 DSA.getCurScope(), /* LimitedType */ isOpenMPWorksharingDirective(DKind));
Alexander Musmana5f070a2014-10-01 06:03:56 +00003093 ResultIterSpace.CounterVar = ISC.BuildCounterVar();
Alexey Bataeva8899172015-08-06 12:30:57 +00003094 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003095 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
3096 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
3097 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
3098 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
3099 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
3100 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
3101
Alexey Bataev62dbb972015-04-22 11:59:37 +00003102 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
3103 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003104 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00003105 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003106 ResultIterSpace.CounterInit == nullptr ||
3107 ResultIterSpace.CounterStep == nullptr);
3108
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003109 return HasErrors;
3110}
3111
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003112/// \brief Build 'VarRef = Start.
3113static ExprResult BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc,
3114 ExprResult VarRef, ExprResult Start) {
3115 TransformToNewDefs Transform(SemaRef);
3116 // Build 'VarRef = Start.
3117 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
3118 if (NewStart.isInvalid())
3119 return ExprError();
3120 NewStart = SemaRef.PerformImplicitConversion(
3121 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
3122 Sema::AA_Converting,
3123 /*AllowExplicit=*/true);
3124 if (NewStart.isInvalid())
3125 return ExprError();
3126 NewStart = SemaRef.PerformImplicitConversion(
3127 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
3128 /*AllowExplicit=*/true);
3129 if (!NewStart.isUsable())
3130 return ExprError();
3131
3132 auto Init =
3133 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
3134 return Init;
3135}
3136
Alexander Musmana5f070a2014-10-01 06:03:56 +00003137/// \brief Build 'VarRef = Start + Iter * Step'.
3138static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S,
3139 SourceLocation Loc, ExprResult VarRef,
3140 ExprResult Start, ExprResult Iter,
3141 ExprResult Step, bool Subtract) {
3142 // Add parentheses (for debugging purposes only).
3143 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
3144 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
3145 !Step.isUsable())
3146 return ExprError();
3147
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003148 TransformToNewDefs Transform(SemaRef);
3149 auto NewStep = Transform.TransformExpr(Step.get()->IgnoreImplicit());
3150 if (NewStep.isInvalid())
3151 return ExprError();
3152 NewStep = SemaRef.PerformImplicitConversion(
3153 NewStep.get(), Step.get()->IgnoreImplicit()->getType(),
3154 Sema::AA_Converting,
3155 /*AllowExplicit=*/true);
3156 if (NewStep.isInvalid())
3157 return ExprError();
3158 ExprResult Update =
3159 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003160 if (!Update.isUsable())
3161 return ExprError();
3162
3163 // Build 'VarRef = Start + Iter * Step'.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003164 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
3165 if (NewStart.isInvalid())
3166 return ExprError();
3167 NewStart = SemaRef.PerformImplicitConversion(
3168 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
3169 Sema::AA_Converting,
3170 /*AllowExplicit=*/true);
3171 if (NewStart.isInvalid())
3172 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003173 Update = SemaRef.BuildBinOp(S, Loc, (Subtract ? BO_Sub : BO_Add),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003174 NewStart.get(), Update.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003175 if (!Update.isUsable())
3176 return ExprError();
3177
3178 Update = SemaRef.PerformImplicitConversion(
3179 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
3180 if (!Update.isUsable())
3181 return ExprError();
3182
3183 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
3184 return Update;
3185}
3186
3187/// \brief Convert integer expression \a E to make it have at least \a Bits
3188/// bits.
3189static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
3190 Sema &SemaRef) {
3191 if (E == nullptr)
3192 return ExprError();
3193 auto &C = SemaRef.Context;
3194 QualType OldType = E->getType();
3195 unsigned HasBits = C.getTypeSize(OldType);
3196 if (HasBits >= Bits)
3197 return ExprResult(E);
3198 // OK to convert to signed, because new type has more bits than old.
3199 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
3200 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
3201 true);
3202}
3203
3204/// \brief Check if the given expression \a E is a constant integer that fits
3205/// into \a Bits bits.
3206static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
3207 if (E == nullptr)
3208 return false;
3209 llvm::APSInt Result;
3210 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
3211 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
3212 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003213}
3214
3215/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00003216/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
3217/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003218static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00003219CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
3220 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
3221 DSAStackTy &DSA,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003222 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00003223 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003224 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003225 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003226 // Found 'collapse' clause - calculate collapse number.
3227 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003228 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
3229 NestedLoopCount += Result.getLimitedValue() - 1;
3230 }
3231 if (OrderedLoopCountExpr) {
3232 // Found 'ordered' clause - calculate collapse number.
3233 llvm::APSInt Result;
3234 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
3235 NestedLoopCount += Result.getLimitedValue() - 1;
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003236 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003237 // This is helper routine for loop directives (e.g., 'for', 'simd',
3238 // 'for simd', etc.).
Alexander Musmana5f070a2014-10-01 06:03:56 +00003239 SmallVector<LoopIterationSpace, 4> IterSpaces;
3240 IterSpaces.resize(NestedLoopCount);
3241 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003242 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003243 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003244 NestedLoopCount, CollapseLoopCountExpr,
3245 OrderedLoopCountExpr, VarsWithImplicitDSA,
3246 IterSpaces[Cnt]))
Alexey Bataevabfc0692014-06-25 06:52:00 +00003247 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003248 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003249 // OpenMP [2.8.1, simd construct, Restrictions]
3250 // All loops associated with the construct must be perfectly nested; that
3251 // is, there must be no intervening code nor any OpenMP directive between
3252 // any two loops.
3253 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003254 }
3255
Alexander Musmana5f070a2014-10-01 06:03:56 +00003256 Built.clear(/* size */ NestedLoopCount);
3257
3258 if (SemaRef.CurContext->isDependentContext())
3259 return NestedLoopCount;
3260
3261 // An example of what is generated for the following code:
3262 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00003263 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00003264 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003265 // for (k = 0; k < NK; ++k)
3266 // for (j = J0; j < NJ; j+=2) {
3267 // <loop body>
3268 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003269 //
3270 // We generate the code below.
3271 // Note: the loop body may be outlined in CodeGen.
3272 // Note: some counters may be C++ classes, operator- is used to find number of
3273 // iterations and operator+= to calculate counter value.
3274 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
3275 // or i64 is currently supported).
3276 //
3277 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
3278 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
3279 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
3280 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
3281 // // similar updates for vars in clauses (e.g. 'linear')
3282 // <loop body (using local i and j)>
3283 // }
3284 // i = NI; // assign final values of counters
3285 // j = NJ;
3286 //
3287
3288 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
3289 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00003290 // Precondition tests if there is at least one iteration (all conditions are
3291 // true).
3292 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003293 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003294 ExprResult LastIteration32 = WidenIterationCount(
3295 32 /* Bits */, SemaRef.PerformImplicitConversion(
3296 N0->IgnoreImpCasts(), N0->getType(),
3297 Sema::AA_Converting, /*AllowExplicit=*/true)
3298 .get(),
3299 SemaRef);
3300 ExprResult LastIteration64 = WidenIterationCount(
3301 64 /* Bits */, SemaRef.PerformImplicitConversion(
3302 N0->IgnoreImpCasts(), N0->getType(),
3303 Sema::AA_Converting, /*AllowExplicit=*/true)
3304 .get(),
3305 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003306
3307 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
3308 return NestedLoopCount;
3309
3310 auto &C = SemaRef.Context;
3311 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
3312
3313 Scope *CurScope = DSA.getCurScope();
3314 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003315 if (PreCond.isUsable()) {
3316 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
3317 PreCond.get(), IterSpaces[Cnt].PreCond);
3318 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003319 auto N = IterSpaces[Cnt].NumIterations;
3320 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
3321 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003322 LastIteration32 = SemaRef.BuildBinOp(
3323 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
3324 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
3325 Sema::AA_Converting,
3326 /*AllowExplicit=*/true)
3327 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003328 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003329 LastIteration64 = SemaRef.BuildBinOp(
3330 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
3331 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
3332 Sema::AA_Converting,
3333 /*AllowExplicit=*/true)
3334 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003335 }
3336
3337 // Choose either the 32-bit or 64-bit version.
3338 ExprResult LastIteration = LastIteration64;
3339 if (LastIteration32.isUsable() &&
3340 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
3341 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
3342 FitsInto(
3343 32 /* Bits */,
3344 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
3345 LastIteration64.get(), SemaRef)))
3346 LastIteration = LastIteration32;
3347
3348 if (!LastIteration.isUsable())
3349 return 0;
3350
3351 // Save the number of iterations.
3352 ExprResult NumIterations = LastIteration;
3353 {
3354 LastIteration = SemaRef.BuildBinOp(
3355 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
3356 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3357 if (!LastIteration.isUsable())
3358 return 0;
3359 }
3360
3361 // Calculate the last iteration number beforehand instead of doing this on
3362 // each iteration. Do not do this if the number of iterations may be kfold-ed.
3363 llvm::APSInt Result;
3364 bool IsConstant =
3365 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
3366 ExprResult CalcLastIteration;
3367 if (!IsConstant) {
3368 SourceLocation SaveLoc;
3369 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00003370 buildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00003371 ".omp.last.iteration");
Alexey Bataev39f915b82015-05-08 10:41:21 +00003372 ExprResult SaveRef = buildDeclRefExpr(
3373 SemaRef, SaveVar, LastIteration.get()->getType(), SaveLoc);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003374 CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign,
3375 SaveRef.get(), LastIteration.get());
3376 LastIteration = SaveRef;
3377
3378 // Prepare SaveRef + 1.
3379 NumIterations = SemaRef.BuildBinOp(
3380 CurScope, SaveLoc, BO_Add, SaveRef.get(),
3381 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3382 if (!NumIterations.isUsable())
3383 return 0;
3384 }
3385
3386 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
3387
Alexander Musmanc6388682014-12-15 07:07:06 +00003388 QualType VType = LastIteration.get()->getType();
3389 // Build variables passed into runtime, nesessary for worksharing directives.
3390 ExprResult LB, UB, IL, ST, EUB;
3391 if (isOpenMPWorksharingDirective(DKind)) {
3392 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00003393 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
3394 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003395 SemaRef.AddInitializerToDecl(
3396 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
3397 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3398
3399 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00003400 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
3401 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003402 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
3403 /*DirectInit*/ false,
3404 /*TypeMayContainAuto*/ false);
3405
3406 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
3407 // This will be used to implement clause 'lastprivate'.
3408 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00003409 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
3410 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003411 SemaRef.AddInitializerToDecl(
3412 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
3413 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3414
3415 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev39f915b82015-05-08 10:41:21 +00003416 VarDecl *STDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.stride");
3417 ST = buildDeclRefExpr(SemaRef, STDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003418 SemaRef.AddInitializerToDecl(
3419 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
3420 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3421
3422 // Build expression: UB = min(UB, LastIteration)
3423 // It is nesessary for CodeGen of directives with static scheduling.
3424 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
3425 UB.get(), LastIteration.get());
3426 ExprResult CondOp = SemaRef.ActOnConditionalOp(
3427 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
3428 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
3429 CondOp.get());
3430 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
3431 }
3432
3433 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003434 ExprResult IV;
3435 ExprResult Init;
3436 {
Alexey Bataev39f915b82015-05-08 10:41:21 +00003437 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.iv");
3438 IV = buildDeclRefExpr(SemaRef, IVDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003439 Expr *RHS = isOpenMPWorksharingDirective(DKind)
3440 ? LB.get()
3441 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
3442 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
3443 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003444 }
3445
Alexander Musmanc6388682014-12-15 07:07:06 +00003446 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003447 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00003448 ExprResult Cond =
3449 isOpenMPWorksharingDirective(DKind)
3450 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
3451 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
3452 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003453
3454 // Loop increment (IV = IV + 1)
3455 SourceLocation IncLoc;
3456 ExprResult Inc =
3457 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
3458 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
3459 if (!Inc.isUsable())
3460 return 0;
3461 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00003462 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
3463 if (!Inc.isUsable())
3464 return 0;
3465
3466 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
3467 // Used for directives with static scheduling.
3468 ExprResult NextLB, NextUB;
3469 if (isOpenMPWorksharingDirective(DKind)) {
3470 // LB + ST
3471 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
3472 if (!NextLB.isUsable())
3473 return 0;
3474 // LB = LB + ST
3475 NextLB =
3476 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
3477 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
3478 if (!NextLB.isUsable())
3479 return 0;
3480 // UB + ST
3481 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
3482 if (!NextUB.isUsable())
3483 return 0;
3484 // UB = UB + ST
3485 NextUB =
3486 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
3487 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
3488 if (!NextUB.isUsable())
3489 return 0;
3490 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003491
3492 // Build updates and final values of the loop counters.
3493 bool HasErrors = false;
3494 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003495 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003496 Built.Updates.resize(NestedLoopCount);
3497 Built.Finals.resize(NestedLoopCount);
3498 {
3499 ExprResult Div;
3500 // Go from inner nested loop to outer.
3501 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
3502 LoopIterationSpace &IS = IterSpaces[Cnt];
3503 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
3504 // Build: Iter = (IV / Div) % IS.NumIters
3505 // where Div is product of previous iterations' IS.NumIters.
3506 ExprResult Iter;
3507 if (Div.isUsable()) {
3508 Iter =
3509 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
3510 } else {
3511 Iter = IV;
3512 assert((Cnt == (int)NestedLoopCount - 1) &&
3513 "unusable div expected on first iteration only");
3514 }
3515
3516 if (Cnt != 0 && Iter.isUsable())
3517 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
3518 IS.NumIterations);
3519 if (!Iter.isUsable()) {
3520 HasErrors = true;
3521 break;
3522 }
3523
Alexey Bataev39f915b82015-05-08 10:41:21 +00003524 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
3525 auto *CounterVar = buildDeclRefExpr(
3526 SemaRef, cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()),
3527 IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
3528 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003529 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
3530 IS.CounterInit);
3531 if (!Init.isUsable()) {
3532 HasErrors = true;
3533 break;
3534 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003535 ExprResult Update =
Alexey Bataev39f915b82015-05-08 10:41:21 +00003536 BuildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003537 IS.CounterInit, Iter, IS.CounterStep, IS.Subtract);
3538 if (!Update.isUsable()) {
3539 HasErrors = true;
3540 break;
3541 }
3542
3543 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
3544 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00003545 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003546 IS.NumIterations, IS.CounterStep, IS.Subtract);
3547 if (!Final.isUsable()) {
3548 HasErrors = true;
3549 break;
3550 }
3551
3552 // Build Div for the next iteration: Div <- Div * IS.NumIters
3553 if (Cnt != 0) {
3554 if (Div.isUnset())
3555 Div = IS.NumIterations;
3556 else
3557 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
3558 IS.NumIterations);
3559
3560 // Add parentheses (for debugging purposes only).
3561 if (Div.isUsable())
3562 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
3563 if (!Div.isUsable()) {
3564 HasErrors = true;
3565 break;
3566 }
3567 }
3568 if (!Update.isUsable() || !Final.isUsable()) {
3569 HasErrors = true;
3570 break;
3571 }
3572 // Save results
3573 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00003574 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003575 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003576 Built.Updates[Cnt] = Update.get();
3577 Built.Finals[Cnt] = Final.get();
3578 }
3579 }
3580
3581 if (HasErrors)
3582 return 0;
3583
3584 // Save results
3585 Built.IterationVarRef = IV.get();
3586 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00003587 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003588 Built.CalcLastIteration =
3589 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003590 Built.PreCond = PreCond.get();
3591 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003592 Built.Init = Init.get();
3593 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00003594 Built.LB = LB.get();
3595 Built.UB = UB.get();
3596 Built.IL = IL.get();
3597 Built.ST = ST.get();
3598 Built.EUB = EUB.get();
3599 Built.NLB = NextLB.get();
3600 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003601
Alexey Bataevabfc0692014-06-25 06:52:00 +00003602 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003603}
3604
Alexey Bataev10e775f2015-07-30 11:36:16 +00003605static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00003606 auto CollapseClauses =
3607 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
3608 if (CollapseClauses.begin() != CollapseClauses.end())
3609 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003610 return nullptr;
3611}
3612
Alexey Bataev10e775f2015-07-30 11:36:16 +00003613static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00003614 auto OrderedClauses =
3615 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
3616 if (OrderedClauses.begin() != OrderedClauses.end())
3617 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00003618 return nullptr;
3619}
3620
Alexey Bataev66b15b52015-08-21 11:14:16 +00003621static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen,
3622 const Expr *Safelen) {
3623 llvm::APSInt SimdlenRes, SafelenRes;
3624 if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() ||
3625 Simdlen->isInstantiationDependent() ||
3626 Simdlen->containsUnexpandedParameterPack())
3627 return false;
3628 if (Safelen->isValueDependent() || Safelen->isTypeDependent() ||
3629 Safelen->isInstantiationDependent() ||
3630 Safelen->containsUnexpandedParameterPack())
3631 return false;
3632 Simdlen->EvaluateAsInt(SimdlenRes, S.Context);
3633 Safelen->EvaluateAsInt(SafelenRes, S.Context);
3634 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
3635 // If both simdlen and safelen clauses are specified, the value of the simdlen
3636 // parameter must be less than or equal to the value of the safelen parameter.
3637 if (SimdlenRes > SafelenRes) {
3638 S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values)
3639 << Simdlen->getSourceRange() << Safelen->getSourceRange();
3640 return true;
3641 }
3642 return false;
3643}
3644
Alexey Bataev4acb8592014-07-07 13:01:15 +00003645StmtResult Sema::ActOnOpenMPSimdDirective(
3646 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3647 SourceLocation EndLoc,
3648 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003649 if (!AStmt)
3650 return StmtError();
3651
3652 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00003653 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003654 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
3655 // define the nested loops number.
3656 unsigned NestedLoopCount = CheckOpenMPLoop(
3657 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
3658 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00003659 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003660 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003661
Alexander Musmana5f070a2014-10-01 06:03:56 +00003662 assert((CurContext->isDependentContext() || B.builtAll()) &&
3663 "omp simd loop exprs were not built");
3664
Alexander Musman3276a272015-03-21 10:12:56 +00003665 if (!CurContext->isDependentContext()) {
3666 // Finalize the clauses that need pre-built expressions for CodeGen.
3667 for (auto C : Clauses) {
3668 if (auto LC = dyn_cast<OMPLinearClause>(C))
3669 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3670 B.NumIterations, *this, CurScope))
3671 return StmtError();
3672 }
3673 }
3674
Alexey Bataev66b15b52015-08-21 11:14:16 +00003675 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
3676 // If both simdlen and safelen clauses are specified, the value of the simdlen
3677 // parameter must be less than or equal to the value of the safelen parameter.
3678 OMPSafelenClause *Safelen = nullptr;
3679 OMPSimdlenClause *Simdlen = nullptr;
3680 for (auto *Clause : Clauses) {
3681 if (Clause->getClauseKind() == OMPC_safelen)
3682 Safelen = cast<OMPSafelenClause>(Clause);
3683 else if (Clause->getClauseKind() == OMPC_simdlen)
3684 Simdlen = cast<OMPSimdlenClause>(Clause);
3685 if (Safelen && Simdlen)
3686 break;
3687 }
3688 if (Simdlen && Safelen &&
3689 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
3690 Safelen->getSafelen()))
3691 return StmtError();
3692
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003693 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003694 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
3695 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003696}
3697
Alexey Bataev4acb8592014-07-07 13:01:15 +00003698StmtResult Sema::ActOnOpenMPForDirective(
3699 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3700 SourceLocation EndLoc,
3701 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003702 if (!AStmt)
3703 return StmtError();
3704
3705 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00003706 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003707 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
3708 // define the nested loops number.
3709 unsigned NestedLoopCount = CheckOpenMPLoop(
3710 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
3711 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00003712 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00003713 return StmtError();
3714
Alexander Musmana5f070a2014-10-01 06:03:56 +00003715 assert((CurContext->isDependentContext() || B.builtAll()) &&
3716 "omp for loop exprs were not built");
3717
Alexey Bataev54acd402015-08-04 11:18:19 +00003718 if (!CurContext->isDependentContext()) {
3719 // Finalize the clauses that need pre-built expressions for CodeGen.
3720 for (auto C : Clauses) {
3721 if (auto LC = dyn_cast<OMPLinearClause>(C))
3722 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3723 B.NumIterations, *this, CurScope))
3724 return StmtError();
3725 }
3726 }
3727
Alexey Bataevf29276e2014-06-18 04:14:57 +00003728 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003729 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
3730 Clauses, AStmt, B);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003731}
3732
Alexander Musmanf82886e2014-09-18 05:12:34 +00003733StmtResult Sema::ActOnOpenMPForSimdDirective(
3734 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3735 SourceLocation EndLoc,
3736 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003737 if (!AStmt)
3738 return StmtError();
3739
3740 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00003741 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003742 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
3743 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00003744 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00003745 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
3746 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
3747 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00003748 if (NestedLoopCount == 0)
3749 return StmtError();
3750
Alexander Musmanc6388682014-12-15 07:07:06 +00003751 assert((CurContext->isDependentContext() || B.builtAll()) &&
3752 "omp for simd loop exprs were not built");
3753
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00003754 if (!CurContext->isDependentContext()) {
3755 // Finalize the clauses that need pre-built expressions for CodeGen.
3756 for (auto C : Clauses) {
3757 if (auto LC = dyn_cast<OMPLinearClause>(C))
3758 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3759 B.NumIterations, *this, CurScope))
3760 return StmtError();
3761 }
3762 }
3763
Alexey Bataev66b15b52015-08-21 11:14:16 +00003764 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
3765 // If both simdlen and safelen clauses are specified, the value of the simdlen
3766 // parameter must be less than or equal to the value of the safelen parameter.
3767 OMPSafelenClause *Safelen = nullptr;
3768 OMPSimdlenClause *Simdlen = nullptr;
3769 for (auto *Clause : Clauses) {
3770 if (Clause->getClauseKind() == OMPC_safelen)
3771 Safelen = cast<OMPSafelenClause>(Clause);
3772 else if (Clause->getClauseKind() == OMPC_simdlen)
3773 Simdlen = cast<OMPSimdlenClause>(Clause);
3774 if (Safelen && Simdlen)
3775 break;
3776 }
3777 if (Simdlen && Safelen &&
3778 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
3779 Safelen->getSafelen()))
3780 return StmtError();
3781
Alexander Musmanf82886e2014-09-18 05:12:34 +00003782 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003783 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
3784 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00003785}
3786
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003787StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
3788 Stmt *AStmt,
3789 SourceLocation StartLoc,
3790 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003791 if (!AStmt)
3792 return StmtError();
3793
3794 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003795 auto BaseStmt = AStmt;
3796 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
3797 BaseStmt = CS->getCapturedStmt();
3798 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
3799 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00003800 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003801 return StmtError();
3802 // All associated statements must be '#pragma omp section' except for
3803 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00003804 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003805 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
3806 if (SectionStmt)
3807 Diag(SectionStmt->getLocStart(),
3808 diag::err_omp_sections_substmt_not_section);
3809 return StmtError();
3810 }
3811 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003812 } else {
3813 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
3814 return StmtError();
3815 }
3816
3817 getCurFunction()->setHasBranchProtectedScope();
3818
3819 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses,
3820 AStmt);
3821}
3822
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003823StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
3824 SourceLocation StartLoc,
3825 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003826 if (!AStmt)
3827 return StmtError();
3828
3829 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003830
3831 getCurFunction()->setHasBranchProtectedScope();
3832
3833 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt);
3834}
3835
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003836StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
3837 Stmt *AStmt,
3838 SourceLocation StartLoc,
3839 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003840 if (!AStmt)
3841 return StmtError();
3842
3843 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00003844
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003845 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00003846
Alexey Bataev3255bf32015-01-19 05:20:46 +00003847 // OpenMP [2.7.3, single Construct, Restrictions]
3848 // The copyprivate clause must not be used with the nowait clause.
3849 OMPClause *Nowait = nullptr;
3850 OMPClause *Copyprivate = nullptr;
3851 for (auto *Clause : Clauses) {
3852 if (Clause->getClauseKind() == OMPC_nowait)
3853 Nowait = Clause;
3854 else if (Clause->getClauseKind() == OMPC_copyprivate)
3855 Copyprivate = Clause;
3856 if (Copyprivate && Nowait) {
3857 Diag(Copyprivate->getLocStart(),
3858 diag::err_omp_single_copyprivate_with_nowait);
3859 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
3860 return StmtError();
3861 }
3862 }
3863
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003864 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3865}
3866
Alexander Musman80c22892014-07-17 08:54:58 +00003867StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
3868 SourceLocation StartLoc,
3869 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003870 if (!AStmt)
3871 return StmtError();
3872
3873 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00003874
3875 getCurFunction()->setHasBranchProtectedScope();
3876
3877 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
3878}
3879
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003880StmtResult
3881Sema::ActOnOpenMPCriticalDirective(const DeclarationNameInfo &DirName,
3882 Stmt *AStmt, SourceLocation StartLoc,
3883 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003884 if (!AStmt)
3885 return StmtError();
3886
3887 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003888
3889 getCurFunction()->setHasBranchProtectedScope();
3890
3891 return OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
3892 AStmt);
3893}
3894
Alexey Bataev4acb8592014-07-07 13:01:15 +00003895StmtResult Sema::ActOnOpenMPParallelForDirective(
3896 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3897 SourceLocation EndLoc,
3898 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003899 if (!AStmt)
3900 return StmtError();
3901
Alexey Bataev4acb8592014-07-07 13:01:15 +00003902 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3903 // 1.2.2 OpenMP Language Terminology
3904 // Structured block - An executable statement with a single entry at the
3905 // top and a single exit at the bottom.
3906 // The point of exit cannot be a branch out of the structured block.
3907 // longjmp() and throw() must not violate the entry/exit criteria.
3908 CS->getCapturedDecl()->setNothrow();
3909
Alexander Musmanc6388682014-12-15 07:07:06 +00003910 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003911 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
3912 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003913 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00003914 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
3915 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
3916 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003917 if (NestedLoopCount == 0)
3918 return StmtError();
3919
Alexander Musmana5f070a2014-10-01 06:03:56 +00003920 assert((CurContext->isDependentContext() || B.builtAll()) &&
3921 "omp parallel for loop exprs were not built");
3922
Alexey Bataev54acd402015-08-04 11:18:19 +00003923 if (!CurContext->isDependentContext()) {
3924 // Finalize the clauses that need pre-built expressions for CodeGen.
3925 for (auto C : Clauses) {
3926 if (auto LC = dyn_cast<OMPLinearClause>(C))
3927 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3928 B.NumIterations, *this, CurScope))
3929 return StmtError();
3930 }
3931 }
3932
Alexey Bataev4acb8592014-07-07 13:01:15 +00003933 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003934 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
3935 NestedLoopCount, Clauses, AStmt, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003936}
3937
Alexander Musmane4e893b2014-09-23 09:33:00 +00003938StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
3939 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3940 SourceLocation EndLoc,
3941 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003942 if (!AStmt)
3943 return StmtError();
3944
Alexander Musmane4e893b2014-09-23 09:33:00 +00003945 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3946 // 1.2.2 OpenMP Language Terminology
3947 // Structured block - An executable statement with a single entry at the
3948 // top and a single exit at the bottom.
3949 // The point of exit cannot be a branch out of the structured block.
3950 // longjmp() and throw() must not violate the entry/exit criteria.
3951 CS->getCapturedDecl()->setNothrow();
3952
Alexander Musmanc6388682014-12-15 07:07:06 +00003953 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003954 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
3955 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00003956 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00003957 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
3958 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
3959 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003960 if (NestedLoopCount == 0)
3961 return StmtError();
3962
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00003963 if (!CurContext->isDependentContext()) {
3964 // Finalize the clauses that need pre-built expressions for CodeGen.
3965 for (auto C : Clauses) {
3966 if (auto LC = dyn_cast<OMPLinearClause>(C))
3967 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3968 B.NumIterations, *this, CurScope))
3969 return StmtError();
3970 }
3971 }
3972
Alexey Bataev66b15b52015-08-21 11:14:16 +00003973 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
3974 // If both simdlen and safelen clauses are specified, the value of the simdlen
3975 // parameter must be less than or equal to the value of the safelen parameter.
3976 OMPSafelenClause *Safelen = nullptr;
3977 OMPSimdlenClause *Simdlen = nullptr;
3978 for (auto *Clause : Clauses) {
3979 if (Clause->getClauseKind() == OMPC_safelen)
3980 Safelen = cast<OMPSafelenClause>(Clause);
3981 else if (Clause->getClauseKind() == OMPC_simdlen)
3982 Simdlen = cast<OMPSimdlenClause>(Clause);
3983 if (Safelen && Simdlen)
3984 break;
3985 }
3986 if (Simdlen && Safelen &&
3987 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
3988 Safelen->getSafelen()))
3989 return StmtError();
3990
Alexander Musmane4e893b2014-09-23 09:33:00 +00003991 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003992 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00003993 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003994}
3995
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003996StmtResult
3997Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
3998 Stmt *AStmt, SourceLocation StartLoc,
3999 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004000 if (!AStmt)
4001 return StmtError();
4002
4003 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004004 auto BaseStmt = AStmt;
4005 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4006 BaseStmt = CS->getCapturedStmt();
4007 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4008 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004009 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004010 return StmtError();
4011 // All associated statements must be '#pragma omp section' except for
4012 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004013 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004014 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4015 if (SectionStmt)
4016 Diag(SectionStmt->getLocStart(),
4017 diag::err_omp_parallel_sections_substmt_not_section);
4018 return StmtError();
4019 }
4020 }
4021 } else {
4022 Diag(AStmt->getLocStart(),
4023 diag::err_omp_parallel_sections_not_compound_stmt);
4024 return StmtError();
4025 }
4026
4027 getCurFunction()->setHasBranchProtectedScope();
4028
4029 return OMPParallelSectionsDirective::Create(Context, StartLoc, EndLoc,
4030 Clauses, AStmt);
4031}
4032
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004033StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
4034 Stmt *AStmt, SourceLocation StartLoc,
4035 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004036 if (!AStmt)
4037 return StmtError();
4038
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004039 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4040 // 1.2.2 OpenMP Language Terminology
4041 // Structured block - An executable statement with a single entry at the
4042 // top and a single exit at the bottom.
4043 // The point of exit cannot be a branch out of the structured block.
4044 // longjmp() and throw() must not violate the entry/exit criteria.
4045 CS->getCapturedDecl()->setNothrow();
4046
4047 getCurFunction()->setHasBranchProtectedScope();
4048
4049 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4050}
4051
Alexey Bataev68446b72014-07-18 07:47:19 +00004052StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
4053 SourceLocation EndLoc) {
4054 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
4055}
4056
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00004057StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
4058 SourceLocation EndLoc) {
4059 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
4060}
4061
Alexey Bataev2df347a2014-07-18 10:17:07 +00004062StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
4063 SourceLocation EndLoc) {
4064 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
4065}
4066
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00004067StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
4068 SourceLocation StartLoc,
4069 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004070 if (!AStmt)
4071 return StmtError();
4072
4073 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00004074
4075 getCurFunction()->setHasBranchProtectedScope();
4076
4077 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
4078}
4079
Alexey Bataev6125da92014-07-21 11:26:11 +00004080StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
4081 SourceLocation StartLoc,
4082 SourceLocation EndLoc) {
4083 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
4084 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
4085}
4086
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004087StmtResult Sema::ActOnOpenMPOrderedDirective(Stmt *AStmt,
4088 SourceLocation StartLoc,
4089 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004090 if (!AStmt)
4091 return StmtError();
4092
4093 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004094
4095 getCurFunction()->setHasBranchProtectedScope();
4096
4097 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, AStmt);
4098}
4099
Alexey Bataev1d160b12015-03-13 12:27:31 +00004100namespace {
4101/// \brief Helper class for checking expression in 'omp atomic [update]'
4102/// construct.
4103class OpenMPAtomicUpdateChecker {
4104 /// \brief Error results for atomic update expressions.
4105 enum ExprAnalysisErrorCode {
4106 /// \brief A statement is not an expression statement.
4107 NotAnExpression,
4108 /// \brief Expression is not builtin binary or unary operation.
4109 NotABinaryOrUnaryExpression,
4110 /// \brief Unary operation is not post-/pre- increment/decrement operation.
4111 NotAnUnaryIncDecExpression,
4112 /// \brief An expression is not of scalar type.
4113 NotAScalarType,
4114 /// \brief A binary operation is not an assignment operation.
4115 NotAnAssignmentOp,
4116 /// \brief RHS part of the binary operation is not a binary expression.
4117 NotABinaryExpression,
4118 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
4119 /// expression.
4120 NotABinaryOperator,
4121 /// \brief RHS binary operation does not have reference to the updated LHS
4122 /// part.
4123 NotAnUpdateExpression,
4124 /// \brief No errors is found.
4125 NoError
4126 };
4127 /// \brief Reference to Sema.
4128 Sema &SemaRef;
4129 /// \brief A location for note diagnostics (when error is found).
4130 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004131 /// \brief 'x' lvalue part of the source atomic expression.
4132 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004133 /// \brief 'expr' rvalue part of the source atomic expression.
4134 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004135 /// \brief Helper expression of the form
4136 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
4137 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
4138 Expr *UpdateExpr;
4139 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
4140 /// important for non-associative operations.
4141 bool IsXLHSInRHSPart;
4142 BinaryOperatorKind Op;
4143 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004144 /// \brief true if the source expression is a postfix unary operation, false
4145 /// if it is a prefix unary operation.
4146 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004147
4148public:
4149 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00004150 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00004151 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00004152 /// \brief Check specified statement that it is suitable for 'atomic update'
4153 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00004154 /// expression. If DiagId and NoteId == 0, then only check is performed
4155 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00004156 /// \param DiagId Diagnostic which should be emitted if error is found.
4157 /// \param NoteId Diagnostic note for the main error message.
4158 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00004159 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00004160 /// \brief Return the 'x' lvalue part of the source atomic expression.
4161 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00004162 /// \brief Return the 'expr' rvalue part of the source atomic expression.
4163 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00004164 /// \brief Return the update expression used in calculation of the updated
4165 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
4166 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
4167 Expr *getUpdateExpr() const { return UpdateExpr; }
4168 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
4169 /// false otherwise.
4170 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
4171
Alexey Bataevb78ca832015-04-01 03:33:17 +00004172 /// \brief true if the source expression is a postfix unary operation, false
4173 /// if it is a prefix unary operation.
4174 bool isPostfixUpdate() const { return IsPostfixUpdate; }
4175
Alexey Bataev1d160b12015-03-13 12:27:31 +00004176private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00004177 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
4178 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00004179};
4180} // namespace
4181
4182bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
4183 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
4184 ExprAnalysisErrorCode ErrorFound = NoError;
4185 SourceLocation ErrorLoc, NoteLoc;
4186 SourceRange ErrorRange, NoteRange;
4187 // Allowed constructs are:
4188 // x = x binop expr;
4189 // x = expr binop x;
4190 if (AtomicBinOp->getOpcode() == BO_Assign) {
4191 X = AtomicBinOp->getLHS();
4192 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
4193 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
4194 if (AtomicInnerBinOp->isMultiplicativeOp() ||
4195 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
4196 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00004197 Op = AtomicInnerBinOp->getOpcode();
4198 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00004199 auto *LHS = AtomicInnerBinOp->getLHS();
4200 auto *RHS = AtomicInnerBinOp->getRHS();
4201 llvm::FoldingSetNodeID XId, LHSId, RHSId;
4202 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
4203 /*Canonical=*/true);
4204 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
4205 /*Canonical=*/true);
4206 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
4207 /*Canonical=*/true);
4208 if (XId == LHSId) {
4209 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004210 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004211 } else if (XId == RHSId) {
4212 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004213 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004214 } else {
4215 ErrorLoc = AtomicInnerBinOp->getExprLoc();
4216 ErrorRange = AtomicInnerBinOp->getSourceRange();
4217 NoteLoc = X->getExprLoc();
4218 NoteRange = X->getSourceRange();
4219 ErrorFound = NotAnUpdateExpression;
4220 }
4221 } else {
4222 ErrorLoc = AtomicInnerBinOp->getExprLoc();
4223 ErrorRange = AtomicInnerBinOp->getSourceRange();
4224 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
4225 NoteRange = SourceRange(NoteLoc, NoteLoc);
4226 ErrorFound = NotABinaryOperator;
4227 }
4228 } else {
4229 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
4230 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
4231 ErrorFound = NotABinaryExpression;
4232 }
4233 } else {
4234 ErrorLoc = AtomicBinOp->getExprLoc();
4235 ErrorRange = AtomicBinOp->getSourceRange();
4236 NoteLoc = AtomicBinOp->getOperatorLoc();
4237 NoteRange = SourceRange(NoteLoc, NoteLoc);
4238 ErrorFound = NotAnAssignmentOp;
4239 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00004240 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004241 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
4242 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
4243 return true;
4244 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00004245 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00004246 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004247}
4248
4249bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
4250 unsigned NoteId) {
4251 ExprAnalysisErrorCode ErrorFound = NoError;
4252 SourceLocation ErrorLoc, NoteLoc;
4253 SourceRange ErrorRange, NoteRange;
4254 // Allowed constructs are:
4255 // x++;
4256 // x--;
4257 // ++x;
4258 // --x;
4259 // x binop= expr;
4260 // x = x binop expr;
4261 // x = expr binop x;
4262 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
4263 AtomicBody = AtomicBody->IgnoreParenImpCasts();
4264 if (AtomicBody->getType()->isScalarType() ||
4265 AtomicBody->isInstantiationDependent()) {
4266 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
4267 AtomicBody->IgnoreParenImpCasts())) {
4268 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00004269 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00004270 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00004271 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00004272 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00004273 X = AtomicCompAssignOp->getLHS();
4274 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004275 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
4276 AtomicBody->IgnoreParenImpCasts())) {
4277 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00004278 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
4279 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004280 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00004281 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
4282 // Check for Unary Operation
4283 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004284 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00004285 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
4286 OpLoc = AtomicUnaryOp->getOperatorLoc();
4287 X = AtomicUnaryOp->getSubExpr();
4288 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
4289 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004290 } else {
4291 ErrorFound = NotAnUnaryIncDecExpression;
4292 ErrorLoc = AtomicUnaryOp->getExprLoc();
4293 ErrorRange = AtomicUnaryOp->getSourceRange();
4294 NoteLoc = AtomicUnaryOp->getOperatorLoc();
4295 NoteRange = SourceRange(NoteLoc, NoteLoc);
4296 }
Alexey Bataev5a195472015-09-04 12:55:50 +00004297 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004298 ErrorFound = NotABinaryOrUnaryExpression;
4299 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
4300 NoteRange = ErrorRange = AtomicBody->getSourceRange();
4301 }
4302 } else {
4303 ErrorFound = NotAScalarType;
4304 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
4305 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
4306 }
4307 } else {
4308 ErrorFound = NotAnExpression;
4309 NoteLoc = ErrorLoc = S->getLocStart();
4310 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
4311 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00004312 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004313 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
4314 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
4315 return true;
4316 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00004317 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00004318 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00004319 // Build an update expression of form 'OpaqueValueExpr(x) binop
4320 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
4321 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
4322 auto *OVEX = new (SemaRef.getASTContext())
4323 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
4324 auto *OVEExpr = new (SemaRef.getASTContext())
4325 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
4326 auto Update =
4327 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
4328 IsXLHSInRHSPart ? OVEExpr : OVEX);
4329 if (Update.isInvalid())
4330 return true;
4331 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
4332 Sema::AA_Casting);
4333 if (Update.isInvalid())
4334 return true;
4335 UpdateExpr = Update.get();
4336 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00004337 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004338}
4339
Alexey Bataev0162e452014-07-22 10:10:35 +00004340StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
4341 Stmt *AStmt,
4342 SourceLocation StartLoc,
4343 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004344 if (!AStmt)
4345 return StmtError();
4346
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004347 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00004348 // 1.2.2 OpenMP Language Terminology
4349 // Structured block - An executable statement with a single entry at the
4350 // top and a single exit at the bottom.
4351 // The point of exit cannot be a branch out of the structured block.
4352 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00004353 OpenMPClauseKind AtomicKind = OMPC_unknown;
4354 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004355 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00004356 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00004357 C->getClauseKind() == OMPC_update ||
4358 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00004359 if (AtomicKind != OMPC_unknown) {
4360 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
4361 << SourceRange(C->getLocStart(), C->getLocEnd());
4362 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
4363 << getOpenMPClauseName(AtomicKind);
4364 } else {
4365 AtomicKind = C->getClauseKind();
4366 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004367 }
4368 }
4369 }
Alexey Bataev62cec442014-11-18 10:14:22 +00004370
Alexey Bataev459dec02014-07-24 06:46:57 +00004371 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00004372 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
4373 Body = EWC->getSubExpr();
4374
Alexey Bataev62cec442014-11-18 10:14:22 +00004375 Expr *X = nullptr;
4376 Expr *V = nullptr;
4377 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004378 Expr *UE = nullptr;
4379 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004380 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00004381 // OpenMP [2.12.6, atomic Construct]
4382 // In the next expressions:
4383 // * x and v (as applicable) are both l-value expressions with scalar type.
4384 // * During the execution of an atomic region, multiple syntactic
4385 // occurrences of x must designate the same storage location.
4386 // * Neither of v and expr (as applicable) may access the storage location
4387 // designated by x.
4388 // * Neither of x and expr (as applicable) may access the storage location
4389 // designated by v.
4390 // * expr is an expression with scalar type.
4391 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
4392 // * binop, binop=, ++, and -- are not overloaded operators.
4393 // * The expression x binop expr must be numerically equivalent to x binop
4394 // (expr). This requirement is satisfied if the operators in expr have
4395 // precedence greater than binop, or by using parentheses around expr or
4396 // subexpressions of expr.
4397 // * The expression expr binop x must be numerically equivalent to (expr)
4398 // binop x. This requirement is satisfied if the operators in expr have
4399 // precedence equal to or greater than binop, or by using parentheses around
4400 // expr or subexpressions of expr.
4401 // * For forms that allow multiple occurrences of x, the number of times
4402 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00004403 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004404 enum {
4405 NotAnExpression,
4406 NotAnAssignmentOp,
4407 NotAScalarType,
4408 NotAnLValue,
4409 NoError
4410 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00004411 SourceLocation ErrorLoc, NoteLoc;
4412 SourceRange ErrorRange, NoteRange;
4413 // If clause is read:
4414 // v = x;
4415 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
4416 auto AtomicBinOp =
4417 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
4418 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
4419 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
4420 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
4421 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
4422 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
4423 if (!X->isLValue() || !V->isLValue()) {
4424 auto NotLValueExpr = X->isLValue() ? V : X;
4425 ErrorFound = NotAnLValue;
4426 ErrorLoc = AtomicBinOp->getExprLoc();
4427 ErrorRange = AtomicBinOp->getSourceRange();
4428 NoteLoc = NotLValueExpr->getExprLoc();
4429 NoteRange = NotLValueExpr->getSourceRange();
4430 }
4431 } else if (!X->isInstantiationDependent() ||
4432 !V->isInstantiationDependent()) {
4433 auto NotScalarExpr =
4434 (X->isInstantiationDependent() || X->getType()->isScalarType())
4435 ? V
4436 : X;
4437 ErrorFound = NotAScalarType;
4438 ErrorLoc = AtomicBinOp->getExprLoc();
4439 ErrorRange = AtomicBinOp->getSourceRange();
4440 NoteLoc = NotScalarExpr->getExprLoc();
4441 NoteRange = NotScalarExpr->getSourceRange();
4442 }
Alexey Bataev5a195472015-09-04 12:55:50 +00004443 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00004444 ErrorFound = NotAnAssignmentOp;
4445 ErrorLoc = AtomicBody->getExprLoc();
4446 ErrorRange = AtomicBody->getSourceRange();
4447 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
4448 : AtomicBody->getExprLoc();
4449 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
4450 : AtomicBody->getSourceRange();
4451 }
4452 } else {
4453 ErrorFound = NotAnExpression;
4454 NoteLoc = ErrorLoc = Body->getLocStart();
4455 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00004456 }
Alexey Bataev62cec442014-11-18 10:14:22 +00004457 if (ErrorFound != NoError) {
4458 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
4459 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00004460 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
4461 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00004462 return StmtError();
4463 } else if (CurContext->isDependentContext())
4464 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00004465 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004466 enum {
4467 NotAnExpression,
4468 NotAnAssignmentOp,
4469 NotAScalarType,
4470 NotAnLValue,
4471 NoError
4472 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00004473 SourceLocation ErrorLoc, NoteLoc;
4474 SourceRange ErrorRange, NoteRange;
4475 // If clause is write:
4476 // x = expr;
4477 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
4478 auto AtomicBinOp =
4479 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
4480 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00004481 X = AtomicBinOp->getLHS();
4482 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00004483 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
4484 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
4485 if (!X->isLValue()) {
4486 ErrorFound = NotAnLValue;
4487 ErrorLoc = AtomicBinOp->getExprLoc();
4488 ErrorRange = AtomicBinOp->getSourceRange();
4489 NoteLoc = X->getExprLoc();
4490 NoteRange = X->getSourceRange();
4491 }
4492 } else if (!X->isInstantiationDependent() ||
4493 !E->isInstantiationDependent()) {
4494 auto NotScalarExpr =
4495 (X->isInstantiationDependent() || X->getType()->isScalarType())
4496 ? E
4497 : X;
4498 ErrorFound = NotAScalarType;
4499 ErrorLoc = AtomicBinOp->getExprLoc();
4500 ErrorRange = AtomicBinOp->getSourceRange();
4501 NoteLoc = NotScalarExpr->getExprLoc();
4502 NoteRange = NotScalarExpr->getSourceRange();
4503 }
Alexey Bataev5a195472015-09-04 12:55:50 +00004504 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00004505 ErrorFound = NotAnAssignmentOp;
4506 ErrorLoc = AtomicBody->getExprLoc();
4507 ErrorRange = AtomicBody->getSourceRange();
4508 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
4509 : AtomicBody->getExprLoc();
4510 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
4511 : AtomicBody->getSourceRange();
4512 }
4513 } else {
4514 ErrorFound = NotAnExpression;
4515 NoteLoc = ErrorLoc = Body->getLocStart();
4516 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00004517 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00004518 if (ErrorFound != NoError) {
4519 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
4520 << ErrorRange;
4521 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
4522 << NoteRange;
4523 return StmtError();
4524 } else if (CurContext->isDependentContext())
4525 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00004526 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004527 // If clause is update:
4528 // x++;
4529 // x--;
4530 // ++x;
4531 // --x;
4532 // x binop= expr;
4533 // x = x binop expr;
4534 // x = expr binop x;
4535 OpenMPAtomicUpdateChecker Checker(*this);
4536 if (Checker.checkStatement(
4537 Body, (AtomicKind == OMPC_update)
4538 ? diag::err_omp_atomic_update_not_expression_statement
4539 : diag::err_omp_atomic_not_expression_statement,
4540 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00004541 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00004542 if (!CurContext->isDependentContext()) {
4543 E = Checker.getExpr();
4544 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00004545 UE = Checker.getUpdateExpr();
4546 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00004547 }
Alexey Bataev459dec02014-07-24 06:46:57 +00004548 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004549 enum {
4550 NotAnAssignmentOp,
4551 NotACompoundStatement,
4552 NotTwoSubstatements,
4553 NotASpecificExpression,
4554 NoError
4555 } ErrorFound = NoError;
4556 SourceLocation ErrorLoc, NoteLoc;
4557 SourceRange ErrorRange, NoteRange;
4558 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
4559 // If clause is a capture:
4560 // v = x++;
4561 // v = x--;
4562 // v = ++x;
4563 // v = --x;
4564 // v = x binop= expr;
4565 // v = x = x binop expr;
4566 // v = x = expr binop x;
4567 auto *AtomicBinOp =
4568 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
4569 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
4570 V = AtomicBinOp->getLHS();
4571 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
4572 OpenMPAtomicUpdateChecker Checker(*this);
4573 if (Checker.checkStatement(
4574 Body, diag::err_omp_atomic_capture_not_expression_statement,
4575 diag::note_omp_atomic_update))
4576 return StmtError();
4577 E = Checker.getExpr();
4578 X = Checker.getX();
4579 UE = Checker.getUpdateExpr();
4580 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
4581 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00004582 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004583 ErrorLoc = AtomicBody->getExprLoc();
4584 ErrorRange = AtomicBody->getSourceRange();
4585 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
4586 : AtomicBody->getExprLoc();
4587 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
4588 : AtomicBody->getSourceRange();
4589 ErrorFound = NotAnAssignmentOp;
4590 }
4591 if (ErrorFound != NoError) {
4592 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
4593 << ErrorRange;
4594 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
4595 return StmtError();
4596 } else if (CurContext->isDependentContext()) {
4597 UE = V = E = X = nullptr;
4598 }
4599 } else {
4600 // If clause is a capture:
4601 // { v = x; x = expr; }
4602 // { v = x; x++; }
4603 // { v = x; x--; }
4604 // { v = x; ++x; }
4605 // { v = x; --x; }
4606 // { v = x; x binop= expr; }
4607 // { v = x; x = x binop expr; }
4608 // { v = x; x = expr binop x; }
4609 // { x++; v = x; }
4610 // { x--; v = x; }
4611 // { ++x; v = x; }
4612 // { --x; v = x; }
4613 // { x binop= expr; v = x; }
4614 // { x = x binop expr; v = x; }
4615 // { x = expr binop x; v = x; }
4616 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
4617 // Check that this is { expr1; expr2; }
4618 if (CS->size() == 2) {
4619 auto *First = CS->body_front();
4620 auto *Second = CS->body_back();
4621 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
4622 First = EWC->getSubExpr()->IgnoreParenImpCasts();
4623 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
4624 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
4625 // Need to find what subexpression is 'v' and what is 'x'.
4626 OpenMPAtomicUpdateChecker Checker(*this);
4627 bool IsUpdateExprFound = !Checker.checkStatement(Second);
4628 BinaryOperator *BinOp = nullptr;
4629 if (IsUpdateExprFound) {
4630 BinOp = dyn_cast<BinaryOperator>(First);
4631 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
4632 }
4633 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
4634 // { v = x; x++; }
4635 // { v = x; x--; }
4636 // { v = x; ++x; }
4637 // { v = x; --x; }
4638 // { v = x; x binop= expr; }
4639 // { v = x; x = x binop expr; }
4640 // { v = x; x = expr binop x; }
4641 // Check that the first expression has form v = x.
4642 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
4643 llvm::FoldingSetNodeID XId, PossibleXId;
4644 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
4645 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
4646 IsUpdateExprFound = XId == PossibleXId;
4647 if (IsUpdateExprFound) {
4648 V = BinOp->getLHS();
4649 X = Checker.getX();
4650 E = Checker.getExpr();
4651 UE = Checker.getUpdateExpr();
4652 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00004653 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004654 }
4655 }
4656 if (!IsUpdateExprFound) {
4657 IsUpdateExprFound = !Checker.checkStatement(First);
4658 BinOp = nullptr;
4659 if (IsUpdateExprFound) {
4660 BinOp = dyn_cast<BinaryOperator>(Second);
4661 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
4662 }
4663 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
4664 // { x++; v = x; }
4665 // { x--; v = x; }
4666 // { ++x; v = x; }
4667 // { --x; v = x; }
4668 // { x binop= expr; v = x; }
4669 // { x = x binop expr; v = x; }
4670 // { x = expr binop x; v = x; }
4671 // Check that the second expression has form v = x.
4672 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
4673 llvm::FoldingSetNodeID XId, PossibleXId;
4674 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
4675 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
4676 IsUpdateExprFound = XId == PossibleXId;
4677 if (IsUpdateExprFound) {
4678 V = BinOp->getLHS();
4679 X = Checker.getX();
4680 E = Checker.getExpr();
4681 UE = Checker.getUpdateExpr();
4682 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00004683 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004684 }
4685 }
4686 }
4687 if (!IsUpdateExprFound) {
4688 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00004689 auto *FirstExpr = dyn_cast<Expr>(First);
4690 auto *SecondExpr = dyn_cast<Expr>(Second);
4691 if (!FirstExpr || !SecondExpr ||
4692 !(FirstExpr->isInstantiationDependent() ||
4693 SecondExpr->isInstantiationDependent())) {
4694 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
4695 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004696 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00004697 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
4698 : First->getLocStart();
4699 NoteRange = ErrorRange = FirstBinOp
4700 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00004701 : SourceRange(ErrorLoc, ErrorLoc);
4702 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00004703 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
4704 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
4705 ErrorFound = NotAnAssignmentOp;
4706 NoteLoc = ErrorLoc = SecondBinOp
4707 ? SecondBinOp->getOperatorLoc()
4708 : Second->getLocStart();
4709 NoteRange = ErrorRange =
4710 SecondBinOp ? SecondBinOp->getSourceRange()
4711 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00004712 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00004713 auto *PossibleXRHSInFirst =
4714 FirstBinOp->getRHS()->IgnoreParenImpCasts();
4715 auto *PossibleXLHSInSecond =
4716 SecondBinOp->getLHS()->IgnoreParenImpCasts();
4717 llvm::FoldingSetNodeID X1Id, X2Id;
4718 PossibleXRHSInFirst->Profile(X1Id, Context,
4719 /*Canonical=*/true);
4720 PossibleXLHSInSecond->Profile(X2Id, Context,
4721 /*Canonical=*/true);
4722 IsUpdateExprFound = X1Id == X2Id;
4723 if (IsUpdateExprFound) {
4724 V = FirstBinOp->getLHS();
4725 X = SecondBinOp->getLHS();
4726 E = SecondBinOp->getRHS();
4727 UE = nullptr;
4728 IsXLHSInRHSPart = false;
4729 IsPostfixUpdate = true;
4730 } else {
4731 ErrorFound = NotASpecificExpression;
4732 ErrorLoc = FirstBinOp->getExprLoc();
4733 ErrorRange = FirstBinOp->getSourceRange();
4734 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
4735 NoteRange = SecondBinOp->getRHS()->getSourceRange();
4736 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00004737 }
4738 }
4739 }
4740 }
4741 } else {
4742 NoteLoc = ErrorLoc = Body->getLocStart();
4743 NoteRange = ErrorRange =
4744 SourceRange(Body->getLocStart(), Body->getLocStart());
4745 ErrorFound = NotTwoSubstatements;
4746 }
4747 } else {
4748 NoteLoc = ErrorLoc = Body->getLocStart();
4749 NoteRange = ErrorRange =
4750 SourceRange(Body->getLocStart(), Body->getLocStart());
4751 ErrorFound = NotACompoundStatement;
4752 }
4753 if (ErrorFound != NoError) {
4754 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
4755 << ErrorRange;
4756 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
4757 return StmtError();
4758 } else if (CurContext->isDependentContext()) {
4759 UE = V = E = X = nullptr;
4760 }
Alexey Bataev459dec02014-07-24 06:46:57 +00004761 }
Alexey Bataevdea47612014-07-23 07:46:59 +00004762 }
Alexey Bataev0162e452014-07-22 10:10:35 +00004763
4764 getCurFunction()->setHasBranchProtectedScope();
4765
Alexey Bataev62cec442014-11-18 10:14:22 +00004766 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00004767 X, V, E, UE, IsXLHSInRHSPart,
4768 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00004769}
4770
Alexey Bataev0bd520b2014-09-19 08:19:49 +00004771StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
4772 Stmt *AStmt,
4773 SourceLocation StartLoc,
4774 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004775 if (!AStmt)
4776 return StmtError();
4777
4778 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev0bd520b2014-09-19 08:19:49 +00004779
Alexey Bataev13314bf2014-10-09 04:18:56 +00004780 // OpenMP [2.16, Nesting of Regions]
4781 // If specified, a teams construct must be contained within a target
4782 // construct. That target construct must contain no statements or directives
4783 // outside of the teams construct.
4784 if (DSAStack->hasInnerTeamsRegion()) {
4785 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
4786 bool OMPTeamsFound = true;
4787 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
4788 auto I = CS->body_begin();
4789 while (I != CS->body_end()) {
4790 auto OED = dyn_cast<OMPExecutableDirective>(*I);
4791 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
4792 OMPTeamsFound = false;
4793 break;
4794 }
4795 ++I;
4796 }
4797 assert(I != CS->body_end() && "Not found statement");
4798 S = *I;
4799 }
4800 if (!OMPTeamsFound) {
4801 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
4802 Diag(DSAStack->getInnerTeamsRegionLoc(),
4803 diag::note_omp_nested_teams_construct_here);
4804 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
4805 << isa<OMPExecutableDirective>(S);
4806 return StmtError();
4807 }
4808 }
4809
Alexey Bataev0bd520b2014-09-19 08:19:49 +00004810 getCurFunction()->setHasBranchProtectedScope();
4811
4812 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4813}
4814
Michael Wong65f367f2015-07-21 13:44:28 +00004815StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
4816 Stmt *AStmt,
4817 SourceLocation StartLoc,
4818 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004819 if (!AStmt)
4820 return StmtError();
4821
4822 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
4823
Michael Wong65f367f2015-07-21 13:44:28 +00004824 getCurFunction()->setHasBranchProtectedScope();
4825
4826 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
4827 AStmt);
4828}
4829
Alexey Bataev13314bf2014-10-09 04:18:56 +00004830StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
4831 Stmt *AStmt, SourceLocation StartLoc,
4832 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004833 if (!AStmt)
4834 return StmtError();
4835
Alexey Bataev13314bf2014-10-09 04:18:56 +00004836 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4837 // 1.2.2 OpenMP Language Terminology
4838 // Structured block - An executable statement with a single entry at the
4839 // top and a single exit at the bottom.
4840 // The point of exit cannot be a branch out of the structured block.
4841 // longjmp() and throw() must not violate the entry/exit criteria.
4842 CS->getCapturedDecl()->setNothrow();
4843
4844 getCurFunction()->setHasBranchProtectedScope();
4845
4846 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4847}
4848
Alexey Bataev6d4ed052015-07-01 06:57:41 +00004849StmtResult
4850Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
4851 SourceLocation EndLoc,
4852 OpenMPDirectiveKind CancelRegion) {
4853 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
4854 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
4855 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
4856 << getOpenMPDirectiveName(CancelRegion);
4857 return StmtError();
4858 }
4859 if (DSAStack->isParentNowaitRegion()) {
4860 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
4861 return StmtError();
4862 }
4863 if (DSAStack->isParentOrderedRegion()) {
4864 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
4865 return StmtError();
4866 }
4867 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
4868 CancelRegion);
4869}
4870
Alexey Bataev80909872015-07-02 11:25:17 +00004871StmtResult Sema::ActOnOpenMPCancelDirective(SourceLocation StartLoc,
4872 SourceLocation EndLoc,
4873 OpenMPDirectiveKind CancelRegion) {
4874 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
4875 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
4876 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
4877 << getOpenMPDirectiveName(CancelRegion);
4878 return StmtError();
4879 }
4880 if (DSAStack->isParentNowaitRegion()) {
4881 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
4882 return StmtError();
4883 }
4884 if (DSAStack->isParentOrderedRegion()) {
4885 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
4886 return StmtError();
4887 }
4888 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, CancelRegion);
4889}
4890
Alexey Bataeved09d242014-05-28 05:53:51 +00004891OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004892 SourceLocation StartLoc,
4893 SourceLocation LParenLoc,
4894 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004895 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004896 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00004897 case OMPC_final:
4898 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
4899 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00004900 case OMPC_num_threads:
4901 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
4902 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00004903 case OMPC_safelen:
4904 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
4905 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00004906 case OMPC_simdlen:
4907 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
4908 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00004909 case OMPC_collapse:
4910 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
4911 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004912 case OMPC_ordered:
4913 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
4914 break;
Michael Wonge710d542015-08-07 16:16:36 +00004915 case OMPC_device:
4916 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
4917 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004918 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004919 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004920 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00004921 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004922 case OMPC_private:
4923 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00004924 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004925 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00004926 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00004927 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00004928 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004929 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00004930 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00004931 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00004932 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00004933 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004934 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00004935 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004936 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00004937 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00004938 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00004939 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00004940 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00004941 case OMPC_depend:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004942 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004943 llvm_unreachable("Clause is not allowed.");
4944 }
4945 return Res;
4946}
4947
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004948OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
4949 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004950 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004951 SourceLocation NameModifierLoc,
4952 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004953 SourceLocation EndLoc) {
4954 Expr *ValExpr = Condition;
4955 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
4956 !Condition->isInstantiationDependent() &&
4957 !Condition->containsUnexpandedParameterPack()) {
4958 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00004959 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004960 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004961 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004962
Nikola Smiljanic01a75982014-05-29 10:55:11 +00004963 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004964 }
4965
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004966 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
4967 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004968}
4969
Alexey Bataev3778b602014-07-17 07:32:53 +00004970OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
4971 SourceLocation StartLoc,
4972 SourceLocation LParenLoc,
4973 SourceLocation EndLoc) {
4974 Expr *ValExpr = Condition;
4975 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
4976 !Condition->isInstantiationDependent() &&
4977 !Condition->containsUnexpandedParameterPack()) {
4978 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
4979 Condition->getExprLoc(), Condition);
4980 if (Val.isInvalid())
4981 return nullptr;
4982
4983 ValExpr = Val.get();
4984 }
4985
4986 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
4987}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004988ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
4989 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00004990 if (!Op)
4991 return ExprError();
4992
4993 class IntConvertDiagnoser : public ICEConvertDiagnoser {
4994 public:
4995 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00004996 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00004997 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
4998 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004999 return S.Diag(Loc, diag::err_omp_not_integral) << T;
5000 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005001 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
5002 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005003 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
5004 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005005 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
5006 QualType T,
5007 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005008 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
5009 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005010 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
5011 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005012 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00005013 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00005014 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005015 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
5016 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005017 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
5018 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005019 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
5020 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005021 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00005022 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00005023 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005024 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
5025 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005026 llvm_unreachable("conversion functions are permitted");
5027 }
5028 } ConvertDiagnoser;
5029 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
5030}
5031
5032OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
5033 SourceLocation StartLoc,
5034 SourceLocation LParenLoc,
5035 SourceLocation EndLoc) {
5036 Expr *ValExpr = NumThreads;
5037 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
Alexey Bataev568a8332014-03-06 06:15:19 +00005038 !NumThreads->containsUnexpandedParameterPack()) {
5039 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
5040 ExprResult Val =
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005041 PerformOpenMPImplicitIntegerConversion(NumThreadsLoc, NumThreads);
Alexey Bataev568a8332014-03-06 06:15:19 +00005042 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005043 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00005044
Nikola Smiljanic01a75982014-05-29 10:55:11 +00005045 ValExpr = Val.get();
Alexey Bataev568a8332014-03-06 06:15:19 +00005046
5047 // OpenMP [2.5, Restrictions]
5048 // The num_threads expression must evaluate to a positive integer value.
5049 llvm::APSInt Result;
Alexey Bataeved09d242014-05-28 05:53:51 +00005050 if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() &&
5051 !Result.isStrictlyPositive()) {
Alexey Bataev568a8332014-03-06 06:15:19 +00005052 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
5053 << "num_threads" << NumThreads->getSourceRange();
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005054 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00005055 }
5056 }
5057
Alexey Bataeved09d242014-05-28 05:53:51 +00005058 return new (Context)
5059 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00005060}
5061
Alexey Bataev62c87d22014-03-21 04:51:18 +00005062ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
5063 OpenMPClauseKind CKind) {
5064 if (!E)
5065 return ExprError();
5066 if (E->isValueDependent() || E->isTypeDependent() ||
5067 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00005068 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00005069 llvm::APSInt Result;
5070 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
5071 if (ICE.isInvalid())
5072 return ExprError();
5073 if (!Result.isStrictlyPositive()) {
5074 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
5075 << getOpenMPClauseName(CKind) << E->getSourceRange();
5076 return ExprError();
5077 }
Alexander Musman09184fe2014-09-30 05:29:28 +00005078 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
5079 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
5080 << E->getSourceRange();
5081 return ExprError();
5082 }
Alexey Bataev9c821032015-04-30 04:23:23 +00005083 if (CKind == OMPC_collapse) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00005084 DSAStack->setCollapseNumber(DSAStack->getCollapseNumber() - 1 +
5085 Result.getExtValue());
5086 } else if (CKind == OMPC_ordered) {
5087 DSAStack->setCollapseNumber(DSAStack->getCollapseNumber() - 1 +
5088 Result.getExtValue());
Alexey Bataev9c821032015-04-30 04:23:23 +00005089 }
Alexey Bataev62c87d22014-03-21 04:51:18 +00005090 return ICE;
5091}
5092
5093OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
5094 SourceLocation LParenLoc,
5095 SourceLocation EndLoc) {
5096 // OpenMP [2.8.1, simd construct, Description]
5097 // The parameter of the safelen clause must be a constant
5098 // positive integer expression.
5099 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
5100 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005101 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00005102 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00005103 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00005104}
5105
Alexey Bataev66b15b52015-08-21 11:14:16 +00005106OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
5107 SourceLocation LParenLoc,
5108 SourceLocation EndLoc) {
5109 // OpenMP [2.8.1, simd construct, Description]
5110 // The parameter of the simdlen clause must be a constant
5111 // positive integer expression.
5112 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
5113 if (Simdlen.isInvalid())
5114 return nullptr;
5115 return new (Context)
5116 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
5117}
5118
Alexander Musman64d33f12014-06-04 07:53:32 +00005119OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
5120 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00005121 SourceLocation LParenLoc,
5122 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00005123 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00005124 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00005125 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00005126 // The parameter of the collapse clause must be a constant
5127 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00005128 ExprResult NumForLoopsResult =
5129 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
5130 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00005131 return nullptr;
5132 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00005133 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00005134}
5135
Alexey Bataev10e775f2015-07-30 11:36:16 +00005136OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
5137 SourceLocation EndLoc,
5138 SourceLocation LParenLoc,
5139 Expr *NumForLoops) {
5140 DSAStack->setOrderedRegion();
5141 // OpenMP [2.7.1, loop construct, Description]
5142 // OpenMP [2.8.1, simd construct, Description]
5143 // OpenMP [2.9.6, distribute construct, Description]
5144 // The parameter of the ordered clause must be a constant
5145 // positive integer expression if any.
5146 if (NumForLoops && LParenLoc.isValid()) {
5147 ExprResult NumForLoopsResult =
5148 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
5149 if (NumForLoopsResult.isInvalid())
5150 return nullptr;
5151 NumForLoops = NumForLoopsResult.get();
5152 }
5153 return new (Context)
5154 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
5155}
5156
Alexey Bataeved09d242014-05-28 05:53:51 +00005157OMPClause *Sema::ActOnOpenMPSimpleClause(
5158 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
5159 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005160 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005161 switch (Kind) {
5162 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00005163 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00005164 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
5165 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005166 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005167 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00005168 Res = ActOnOpenMPProcBindClause(
5169 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
5170 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005171 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005172 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00005173 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00005174 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00005175 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00005176 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00005177 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005178 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005179 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005180 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00005181 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00005182 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00005183 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00005184 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00005185 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005186 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005187 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005188 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00005189 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005190 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005191 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005192 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00005193 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005194 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00005195 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00005196 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00005197 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005198 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005199 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00005200 case OMPC_device:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005201 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005202 llvm_unreachable("Clause is not allowed.");
5203 }
5204 return Res;
5205}
5206
5207OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
5208 SourceLocation KindKwLoc,
5209 SourceLocation StartLoc,
5210 SourceLocation LParenLoc,
5211 SourceLocation EndLoc) {
5212 if (Kind == OMPC_DEFAULT_unknown) {
5213 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005214 static_assert(OMPC_DEFAULT_unknown > 0,
5215 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00005216 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005217 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005218 Values += "'";
5219 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
5220 Values += "'";
5221 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005222 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005223 Values += " or ";
5224 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005225 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005226 break;
5227 default:
5228 Values += Sep;
5229 break;
5230 }
5231 }
5232 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00005233 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005234 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005235 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00005236 switch (Kind) {
5237 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005238 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005239 break;
5240 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005241 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005242 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005243 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005244 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00005245 break;
5246 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005247 return new (Context)
5248 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005249}
5250
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005251OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
5252 SourceLocation KindKwLoc,
5253 SourceLocation StartLoc,
5254 SourceLocation LParenLoc,
5255 SourceLocation EndLoc) {
5256 if (Kind == OMPC_PROC_BIND_unknown) {
5257 std::string Values;
5258 std::string Sep(", ");
5259 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
5260 Values += "'";
5261 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
5262 Values += "'";
5263 switch (i) {
5264 case OMPC_PROC_BIND_unknown - 2:
5265 Values += " or ";
5266 break;
5267 case OMPC_PROC_BIND_unknown - 1:
5268 break;
5269 default:
5270 Values += Sep;
5271 break;
5272 }
5273 }
5274 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00005275 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005276 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005277 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005278 return new (Context)
5279 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005280}
5281
Alexey Bataev56dafe82014-06-20 07:16:17 +00005282OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
5283 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
5284 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005285 SourceLocation ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00005286 SourceLocation EndLoc) {
5287 OMPClause *Res = nullptr;
5288 switch (Kind) {
5289 case OMPC_schedule:
5290 Res = ActOnOpenMPScheduleClause(
5291 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005292 LParenLoc, ArgumentLoc, DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00005293 break;
5294 case OMPC_if:
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005295 Res =
5296 ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument), Expr,
5297 StartLoc, LParenLoc, ArgumentLoc, DelimLoc, EndLoc);
5298 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00005299 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005300 case OMPC_num_threads:
5301 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00005302 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005303 case OMPC_collapse:
5304 case OMPC_default:
5305 case OMPC_proc_bind:
5306 case OMPC_private:
5307 case OMPC_firstprivate:
5308 case OMPC_lastprivate:
5309 case OMPC_shared:
5310 case OMPC_reduction:
5311 case OMPC_linear:
5312 case OMPC_aligned:
5313 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005314 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005315 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00005316 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005317 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005318 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005319 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00005320 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005321 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00005322 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00005323 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00005324 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005325 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005326 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00005327 case OMPC_device:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005328 case OMPC_unknown:
5329 llvm_unreachable("Clause is not allowed.");
5330 }
5331 return Res;
5332}
5333
5334OMPClause *Sema::ActOnOpenMPScheduleClause(
5335 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
5336 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
5337 SourceLocation EndLoc) {
5338 if (Kind == OMPC_SCHEDULE_unknown) {
5339 std::string Values;
5340 std::string Sep(", ");
5341 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) {
5342 Values += "'";
5343 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i);
5344 Values += "'";
5345 switch (i) {
5346 case OMPC_SCHEDULE_unknown - 2:
5347 Values += " or ";
5348 break;
5349 case OMPC_SCHEDULE_unknown - 1:
5350 break;
5351 default:
5352 Values += Sep;
5353 break;
5354 }
5355 }
5356 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
5357 << Values << getOpenMPClauseName(OMPC_schedule);
5358 return nullptr;
5359 }
5360 Expr *ValExpr = ChunkSize;
Alexey Bataev040d5402015-05-12 08:35:28 +00005361 Expr *HelperValExpr = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00005362 if (ChunkSize) {
5363 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
5364 !ChunkSize->isInstantiationDependent() &&
5365 !ChunkSize->containsUnexpandedParameterPack()) {
5366 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
5367 ExprResult Val =
5368 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
5369 if (Val.isInvalid())
5370 return nullptr;
5371
5372 ValExpr = Val.get();
5373
5374 // OpenMP [2.7.1, Restrictions]
5375 // chunk_size must be a loop invariant integer expression with a positive
5376 // value.
5377 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00005378 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
5379 if (Result.isSigned() && !Result.isStrictlyPositive()) {
5380 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
5381 << "schedule" << ChunkSize->getSourceRange();
5382 return nullptr;
5383 }
5384 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
5385 auto *ImpVar = buildVarDecl(*this, ChunkSize->getExprLoc(),
5386 ChunkSize->getType(), ".chunk.");
5387 auto *ImpVarRef = buildDeclRefExpr(*this, ImpVar, ChunkSize->getType(),
5388 ChunkSize->getExprLoc(),
5389 /*RefersToCapture=*/true);
5390 HelperValExpr = ImpVarRef;
Alexey Bataev56dafe82014-06-20 07:16:17 +00005391 }
5392 }
5393 }
5394
5395 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc,
Alexey Bataev040d5402015-05-12 08:35:28 +00005396 EndLoc, Kind, ValExpr, HelperValExpr);
Alexey Bataev56dafe82014-06-20 07:16:17 +00005397}
5398
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005399OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
5400 SourceLocation StartLoc,
5401 SourceLocation EndLoc) {
5402 OMPClause *Res = nullptr;
5403 switch (Kind) {
5404 case OMPC_ordered:
5405 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
5406 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00005407 case OMPC_nowait:
5408 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
5409 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005410 case OMPC_untied:
5411 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
5412 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005413 case OMPC_mergeable:
5414 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
5415 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005416 case OMPC_read:
5417 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
5418 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00005419 case OMPC_write:
5420 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
5421 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00005422 case OMPC_update:
5423 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
5424 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00005425 case OMPC_capture:
5426 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
5427 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005428 case OMPC_seq_cst:
5429 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
5430 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005431 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00005432 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005433 case OMPC_num_threads:
5434 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00005435 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005436 case OMPC_collapse:
5437 case OMPC_schedule:
5438 case OMPC_private:
5439 case OMPC_firstprivate:
5440 case OMPC_lastprivate:
5441 case OMPC_shared:
5442 case OMPC_reduction:
5443 case OMPC_linear:
5444 case OMPC_aligned:
5445 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005446 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005447 case OMPC_default:
5448 case OMPC_proc_bind:
5449 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00005450 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005451 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00005452 case OMPC_device:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005453 case OMPC_unknown:
5454 llvm_unreachable("Clause is not allowed.");
5455 }
5456 return Res;
5457}
5458
Alexey Bataev236070f2014-06-20 11:19:47 +00005459OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
5460 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00005461 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00005462 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
5463}
5464
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005465OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
5466 SourceLocation EndLoc) {
5467 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
5468}
5469
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005470OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
5471 SourceLocation EndLoc) {
5472 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
5473}
5474
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005475OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
5476 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005477 return new (Context) OMPReadClause(StartLoc, EndLoc);
5478}
5479
Alexey Bataevdea47612014-07-23 07:46:59 +00005480OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
5481 SourceLocation EndLoc) {
5482 return new (Context) OMPWriteClause(StartLoc, EndLoc);
5483}
5484
Alexey Bataev67a4f222014-07-23 10:25:33 +00005485OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
5486 SourceLocation EndLoc) {
5487 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
5488}
5489
Alexey Bataev459dec02014-07-24 06:46:57 +00005490OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
5491 SourceLocation EndLoc) {
5492 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
5493}
5494
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005495OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
5496 SourceLocation EndLoc) {
5497 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
5498}
5499
Alexey Bataevc5e02582014-06-16 07:08:35 +00005500OMPClause *Sema::ActOnOpenMPVarListClause(
5501 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
5502 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
5503 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005504 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Alexey Bataev182227b2015-08-20 10:54:39 +00005505 OpenMPLinearClauseKind LinKind, SourceLocation DepLinLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005506 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005507 switch (Kind) {
5508 case OMPC_private:
5509 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
5510 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005511 case OMPC_firstprivate:
5512 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
5513 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00005514 case OMPC_lastprivate:
5515 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
5516 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00005517 case OMPC_shared:
5518 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
5519 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005520 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00005521 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
5522 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00005523 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00005524 case OMPC_linear:
5525 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Alexey Bataev182227b2015-08-20 10:54:39 +00005526 LinKind, DepLinLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00005527 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00005528 case OMPC_aligned:
5529 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
5530 ColonLoc, EndLoc);
5531 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005532 case OMPC_copyin:
5533 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
5534 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00005535 case OMPC_copyprivate:
5536 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
5537 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00005538 case OMPC_flush:
5539 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
5540 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005541 case OMPC_depend:
Alexey Bataev182227b2015-08-20 10:54:39 +00005542 Res = ActOnOpenMPDependClause(DepKind, DepLinLoc, ColonLoc, VarList, StartLoc,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005543 LParenLoc, EndLoc);
5544 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005545 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00005546 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00005547 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00005548 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00005549 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00005550 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005551 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005552 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005553 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005554 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00005555 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005556 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005557 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005558 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005559 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00005560 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00005561 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00005562 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005563 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00005564 case OMPC_device:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005565 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005566 llvm_unreachable("Clause is not allowed.");
5567 }
5568 return Res;
5569}
5570
5571OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
5572 SourceLocation StartLoc,
5573 SourceLocation LParenLoc,
5574 SourceLocation EndLoc) {
5575 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00005576 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00005577 for (auto &RefExpr : VarList) {
5578 assert(RefExpr && "NULL expr in OpenMP private clause.");
5579 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005580 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00005581 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00005582 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005583 continue;
5584 }
5585
Alexey Bataeved09d242014-05-28 05:53:51 +00005586 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005587 // OpenMP [2.1, C/C++]
5588 // A list item is a variable name.
5589 // OpenMP [2.9.3.3, Restrictions, p.1]
5590 // A variable that is part of another variable (as an array or
5591 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00005592 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005593 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005594 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005595 continue;
5596 }
5597 Decl *D = DE->getDecl();
5598 VarDecl *VD = cast<VarDecl>(D);
5599
5600 QualType Type = VD->getType();
5601 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5602 // It will be analyzed later.
5603 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00005604 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005605 continue;
5606 }
5607
5608 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
5609 // A variable that appears in a private clause must not have an incomplete
5610 // type or a reference type.
5611 if (RequireCompleteType(ELoc, Type,
5612 diag::err_omp_private_incomplete_type)) {
5613 continue;
5614 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00005615 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005616
Alexey Bataev758e55e2013-09-06 18:03:48 +00005617 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
5618 // in a Construct]
5619 // Variables with the predetermined data-sharing attributes may not be
5620 // listed in data-sharing attributes clauses, except for the cases
5621 // listed below. For these exceptions only, listing a predetermined
5622 // variable in a data-sharing attribute clause is allowed and overrides
5623 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005624 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005625 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005626 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
5627 << getOpenMPClauseName(OMPC_private);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005628 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005629 continue;
5630 }
5631
Alexey Bataevccb59ec2015-05-19 08:44:56 +00005632 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00005633 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00005634 DSAStack->getCurrentDirective() == OMPD_task) {
5635 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
5636 << getOpenMPClauseName(OMPC_private) << Type
5637 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
5638 bool IsDecl =
5639 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5640 Diag(VD->getLocation(),
5641 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5642 << VD;
5643 continue;
5644 }
5645
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005646 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
5647 // A variable of class type (or array thereof) that appears in a private
5648 // clause requires an accessible, unambiguous default constructor for the
5649 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00005650 // Generate helper private variable and initialize it with the default
5651 // value. The address of the original variable is replaced by the address of
5652 // the new private variable in CodeGen. This new variable is not added to
5653 // IdResolver, so the code in the OpenMP region uses original variable for
5654 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005655 Type = Type.getUnqualifiedType();
5656 auto VDPrivate = buildVarDecl(*this, DE->getExprLoc(), Type, VD->getName());
Alexey Bataev39f915b82015-05-08 10:41:21 +00005657 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00005658 if (VDPrivate->isInvalidDecl())
5659 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005660 auto VDPrivateRefExpr = buildDeclRefExpr(
5661 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev03b340a2014-10-21 03:16:40 +00005662
Alexey Bataev758e55e2013-09-06 18:03:48 +00005663 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005664 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00005665 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005666 }
5667
Alexey Bataeved09d242014-05-28 05:53:51 +00005668 if (Vars.empty())
5669 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005670
Alexey Bataev03b340a2014-10-21 03:16:40 +00005671 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
5672 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005673}
5674
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005675namespace {
5676class DiagsUninitializedSeveretyRAII {
5677private:
5678 DiagnosticsEngine &Diags;
5679 SourceLocation SavedLoc;
5680 bool IsIgnored;
5681
5682public:
5683 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
5684 bool IsIgnored)
5685 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
5686 if (!IsIgnored) {
5687 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
5688 /*Map*/ diag::Severity::Ignored, Loc);
5689 }
5690 }
5691 ~DiagsUninitializedSeveretyRAII() {
5692 if (!IsIgnored)
5693 Diags.popMappings(SavedLoc);
5694 }
5695};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00005696}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005697
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005698OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
5699 SourceLocation StartLoc,
5700 SourceLocation LParenLoc,
5701 SourceLocation EndLoc) {
5702 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005703 SmallVector<Expr *, 8> PrivateCopies;
5704 SmallVector<Expr *, 8> Inits;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005705 bool IsImplicitClause =
5706 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
5707 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
5708
Alexey Bataeved09d242014-05-28 05:53:51 +00005709 for (auto &RefExpr : VarList) {
5710 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
5711 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005712 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00005713 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005714 PrivateCopies.push_back(nullptr);
5715 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005716 continue;
5717 }
5718
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005719 SourceLocation ELoc =
5720 IsImplicitClause ? ImplicitClauseLoc : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005721 // OpenMP [2.1, C/C++]
5722 // A list item is a variable name.
5723 // OpenMP [2.9.3.3, Restrictions, p.1]
5724 // A variable that is part of another variable (as an array or
5725 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00005726 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005727 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005728 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005729 continue;
5730 }
5731 Decl *D = DE->getDecl();
5732 VarDecl *VD = cast<VarDecl>(D);
5733
5734 QualType Type = VD->getType();
5735 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5736 // It will be analyzed later.
5737 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005738 PrivateCopies.push_back(nullptr);
5739 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005740 continue;
5741 }
5742
5743 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
5744 // A variable that appears in a private clause must not have an incomplete
5745 // type or a reference type.
5746 if (RequireCompleteType(ELoc, Type,
5747 diag::err_omp_firstprivate_incomplete_type)) {
5748 continue;
5749 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00005750 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005751
5752 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
5753 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00005754 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005755 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005756 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005757
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005758 // If an implicit firstprivate variable found it was checked already.
5759 if (!IsImplicitClause) {
5760 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005761 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005762 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
5763 // A list item that specifies a given variable may not appear in more
5764 // than one clause on the same directive, except that a variable may be
5765 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005766 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00005767 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005768 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00005769 << getOpenMPClauseName(DVar.CKind)
5770 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005771 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005772 continue;
5773 }
5774
5775 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
5776 // in a Construct]
5777 // Variables with the predetermined data-sharing attributes may not be
5778 // listed in data-sharing attributes clauses, except for the cases
5779 // listed below. For these exceptions only, listing a predetermined
5780 // variable in a data-sharing attribute clause is allowed and overrides
5781 // the variable's predetermined data-sharing attributes.
5782 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
5783 // in a Construct, C/C++, p.2]
5784 // Variables with const-qualified type having no mutable member may be
5785 // listed in a firstprivate clause, even if they are static data members.
5786 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
5787 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
5788 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00005789 << getOpenMPClauseName(DVar.CKind)
5790 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005791 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005792 continue;
5793 }
5794
Alexey Bataevf29276e2014-06-18 04:14:57 +00005795 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005796 // OpenMP [2.9.3.4, Restrictions, p.2]
5797 // A list item that is private within a parallel region must not appear
5798 // in a firstprivate clause on a worksharing construct if any of the
5799 // worksharing regions arising from the worksharing construct ever bind
5800 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00005801 if (isOpenMPWorksharingDirective(CurrDir) &&
5802 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005803 DVar = DSAStack->getImplicitDSA(VD, true);
5804 if (DVar.CKind != OMPC_shared &&
5805 (isOpenMPParallelDirective(DVar.DKind) ||
5806 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00005807 Diag(ELoc, diag::err_omp_required_access)
5808 << getOpenMPClauseName(OMPC_firstprivate)
5809 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005810 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005811 continue;
5812 }
5813 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005814 // OpenMP [2.9.3.4, Restrictions, p.3]
5815 // A list item that appears in a reduction clause of a parallel construct
5816 // must not appear in a firstprivate clause on a worksharing or task
5817 // construct if any of the worksharing or task regions arising from the
5818 // worksharing or task construct ever bind to any of the parallel regions
5819 // arising from the parallel construct.
5820 // OpenMP [2.9.3.4, Restrictions, p.4]
5821 // A list item that appears in a reduction clause in worksharing
5822 // construct must not appear in a firstprivate clause in a task construct
5823 // encountered during execution of any of the worksharing regions arising
5824 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005825 if (CurrDir == OMPD_task) {
5826 DVar =
5827 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
5828 [](OpenMPDirectiveKind K) -> bool {
5829 return isOpenMPParallelDirective(K) ||
5830 isOpenMPWorksharingDirective(K);
5831 },
5832 false);
5833 if (DVar.CKind == OMPC_reduction &&
5834 (isOpenMPParallelDirective(DVar.DKind) ||
5835 isOpenMPWorksharingDirective(DVar.DKind))) {
5836 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
5837 << getOpenMPDirectiveName(DVar.DKind);
5838 ReportOriginalDSA(*this, DSAStack, VD, DVar);
5839 continue;
5840 }
5841 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005842 }
5843
Alexey Bataevccb59ec2015-05-19 08:44:56 +00005844 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00005845 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00005846 DSAStack->getCurrentDirective() == OMPD_task) {
5847 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
5848 << getOpenMPClauseName(OMPC_firstprivate) << Type
5849 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
5850 bool IsDecl =
5851 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5852 Diag(VD->getLocation(),
5853 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5854 << VD;
5855 continue;
5856 }
5857
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005858 Type = Type.getUnqualifiedType();
5859 auto VDPrivate = buildVarDecl(*this, ELoc, Type, VD->getName());
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005860 // Generate helper private variable and initialize it with the value of the
5861 // original variable. The address of the original variable is replaced by
5862 // the address of the new private variable in the CodeGen. This new variable
5863 // is not added to IdResolver, so the code in the OpenMP region uses
5864 // original variable for proper diagnostics and variable capturing.
5865 Expr *VDInitRefExpr = nullptr;
5866 // For arrays generate initializer for single element and replace it by the
5867 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005868 if (Type->isArrayType()) {
5869 auto VDInit =
5870 buildVarDecl(*this, DE->getExprLoc(), ElemType, VD->getName());
5871 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005872 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005873 ElemType = ElemType.getUnqualifiedType();
5874 auto *VDInitTemp = buildVarDecl(*this, DE->getLocStart(), ElemType,
5875 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00005876 InitializedEntity Entity =
5877 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005878 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
5879
5880 InitializationSequence InitSeq(*this, Entity, Kind, Init);
5881 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
5882 if (Result.isInvalid())
5883 VDPrivate->setInvalidDecl();
5884 else
5885 VDPrivate->setInit(Result.getAs<Expr>());
5886 } else {
Alexey Bataev69c62a92015-04-15 04:52:20 +00005887 auto *VDInit =
Alexey Bataev39f915b82015-05-08 10:41:21 +00005888 buildVarDecl(*this, DE->getLocStart(), Type, ".firstprivate.temp");
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005889 VDInitRefExpr =
5890 buildDeclRefExpr(*this, VDInit, DE->getType(), DE->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00005891 AddInitializerToDecl(VDPrivate,
5892 DefaultLvalueConversion(VDInitRefExpr).get(),
5893 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005894 }
5895 if (VDPrivate->isInvalidDecl()) {
5896 if (IsImplicitClause) {
5897 Diag(DE->getExprLoc(),
5898 diag::note_omp_task_predetermined_firstprivate_here);
5899 }
5900 continue;
5901 }
5902 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00005903 auto VDPrivateRefExpr = buildDeclRefExpr(
5904 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005905 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
5906 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005907 PrivateCopies.push_back(VDPrivateRefExpr);
5908 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005909 }
5910
Alexey Bataeved09d242014-05-28 05:53:51 +00005911 if (Vars.empty())
5912 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005913
5914 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005915 Vars, PrivateCopies, Inits);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005916}
5917
Alexander Musman1bb328c2014-06-04 13:06:39 +00005918OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
5919 SourceLocation StartLoc,
5920 SourceLocation LParenLoc,
5921 SourceLocation EndLoc) {
5922 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00005923 SmallVector<Expr *, 8> SrcExprs;
5924 SmallVector<Expr *, 8> DstExprs;
5925 SmallVector<Expr *, 8> AssignmentOps;
Alexander Musman1bb328c2014-06-04 13:06:39 +00005926 for (auto &RefExpr : VarList) {
5927 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
5928 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
5929 // It will be analyzed later.
5930 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00005931 SrcExprs.push_back(nullptr);
5932 DstExprs.push_back(nullptr);
5933 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005934 continue;
5935 }
5936
5937 SourceLocation ELoc = RefExpr->getExprLoc();
5938 // OpenMP [2.1, C/C++]
5939 // A list item is a variable name.
5940 // OpenMP [2.14.3.5, Restrictions, p.1]
5941 // A variable that is part of another variable (as an array or structure
5942 // element) cannot appear in a lastprivate clause.
5943 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
5944 if (!DE || !isa<VarDecl>(DE->getDecl())) {
5945 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
5946 continue;
5947 }
5948 Decl *D = DE->getDecl();
5949 VarDecl *VD = cast<VarDecl>(D);
5950
5951 QualType Type = VD->getType();
5952 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5953 // It will be analyzed later.
5954 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00005955 SrcExprs.push_back(nullptr);
5956 DstExprs.push_back(nullptr);
5957 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005958 continue;
5959 }
5960
5961 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
5962 // A variable that appears in a lastprivate clause must not have an
5963 // incomplete type or a reference type.
5964 if (RequireCompleteType(ELoc, Type,
5965 diag::err_omp_lastprivate_incomplete_type)) {
5966 continue;
5967 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00005968 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00005969
5970 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
5971 // in a Construct]
5972 // Variables with the predetermined data-sharing attributes may not be
5973 // listed in data-sharing attributes clauses, except for the cases
5974 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005975 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005976 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
5977 DVar.CKind != OMPC_firstprivate &&
5978 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
5979 Diag(ELoc, diag::err_omp_wrong_dsa)
5980 << getOpenMPClauseName(DVar.CKind)
5981 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005982 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005983 continue;
5984 }
5985
Alexey Bataevf29276e2014-06-18 04:14:57 +00005986 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
5987 // OpenMP [2.14.3.5, Restrictions, p.2]
5988 // A list item that is private within a parallel region, or that appears in
5989 // the reduction clause of a parallel construct, must not appear in a
5990 // lastprivate clause on a worksharing construct if any of the corresponding
5991 // worksharing regions ever binds to any of the corresponding parallel
5992 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005993 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00005994 if (isOpenMPWorksharingDirective(CurrDir) &&
5995 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005996 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005997 if (DVar.CKind != OMPC_shared) {
5998 Diag(ELoc, diag::err_omp_required_access)
5999 << getOpenMPClauseName(OMPC_lastprivate)
6000 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006001 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006002 continue;
6003 }
6004 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00006005 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00006006 // A variable of class type (or array thereof) that appears in a
6007 // lastprivate clause requires an accessible, unambiguous default
6008 // constructor for the class type, unless the list item is also specified
6009 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00006010 // A variable of class type (or array thereof) that appears in a
6011 // lastprivate clause requires an accessible, unambiguous copy assignment
6012 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00006013 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00006014 auto *SrcVD = buildVarDecl(*this, DE->getLocStart(),
Alexey Bataev38e89532015-04-16 04:54:05 +00006015 Type.getUnqualifiedType(), ".lastprivate.src");
Alexey Bataev39f915b82015-05-08 10:41:21 +00006016 auto *PseudoSrcExpr = buildDeclRefExpr(
6017 *this, SrcVD, Type.getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00006018 auto *DstVD =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006019 buildVarDecl(*this, DE->getLocStart(), Type, ".lastprivate.dst");
Alexey Bataev38e89532015-04-16 04:54:05 +00006020 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006021 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00006022 // For arrays generate assignment operation for single element and replace
6023 // it by the original array element in CodeGen.
6024 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
6025 PseudoDstExpr, PseudoSrcExpr);
6026 if (AssignmentOp.isInvalid())
6027 continue;
6028 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
6029 /*DiscardedValue=*/true);
6030 if (AssignmentOp.isInvalid())
6031 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00006032
Alexey Bataev39f915b82015-05-08 10:41:21 +00006033 if (TopDVar.CKind != OMPC_firstprivate)
Alexey Bataevf29276e2014-06-18 04:14:57 +00006034 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006035 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00006036 SrcExprs.push_back(PseudoSrcExpr);
6037 DstExprs.push_back(PseudoDstExpr);
6038 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00006039 }
6040
6041 if (Vars.empty())
6042 return nullptr;
6043
6044 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00006045 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006046}
6047
Alexey Bataev758e55e2013-09-06 18:03:48 +00006048OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
6049 SourceLocation StartLoc,
6050 SourceLocation LParenLoc,
6051 SourceLocation EndLoc) {
6052 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00006053 for (auto &RefExpr : VarList) {
6054 assert(RefExpr && "NULL expr in OpenMP shared clause.");
6055 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00006056 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006057 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006058 continue;
6059 }
6060
Alexey Bataeved09d242014-05-28 05:53:51 +00006061 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00006062 // OpenMP [2.1, C/C++]
6063 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00006064 // OpenMP [2.14.3.2, Restrictions, p.1]
6065 // A variable that is part of another variable (as an array or structure
6066 // element) cannot appear in a shared unless it is a static data member
6067 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00006068 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006069 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006070 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00006071 continue;
6072 }
6073 Decl *D = DE->getDecl();
6074 VarDecl *VD = cast<VarDecl>(D);
6075
6076 QualType Type = VD->getType();
6077 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6078 // It will be analyzed later.
6079 Vars.push_back(DE);
6080 continue;
6081 }
6082
6083 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6084 // in a Construct]
6085 // Variables with the predetermined data-sharing attributes may not be
6086 // listed in data-sharing attributes clauses, except for the cases
6087 // listed below. For these exceptions only, listing a predetermined
6088 // variable in a data-sharing attribute clause is allowed and overrides
6089 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006090 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00006091 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
6092 DVar.RefExpr) {
6093 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
6094 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006095 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006096 continue;
6097 }
6098
6099 DSAStack->addDSA(VD, DE, OMPC_shared);
6100 Vars.push_back(DE);
6101 }
6102
Alexey Bataeved09d242014-05-28 05:53:51 +00006103 if (Vars.empty())
6104 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00006105
6106 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
6107}
6108
Alexey Bataevc5e02582014-06-16 07:08:35 +00006109namespace {
6110class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
6111 DSAStackTy *Stack;
6112
6113public:
6114 bool VisitDeclRefExpr(DeclRefExpr *E) {
6115 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006116 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006117 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
6118 return false;
6119 if (DVar.CKind != OMPC_unknown)
6120 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00006121 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006122 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006123 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00006124 return true;
6125 return false;
6126 }
6127 return false;
6128 }
6129 bool VisitStmt(Stmt *S) {
6130 for (auto Child : S->children()) {
6131 if (Child && Visit(Child))
6132 return true;
6133 }
6134 return false;
6135 }
Alexey Bataev23b69422014-06-18 07:08:49 +00006136 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00006137};
Alexey Bataev23b69422014-06-18 07:08:49 +00006138} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00006139
6140OMPClause *Sema::ActOnOpenMPReductionClause(
6141 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
6142 SourceLocation ColonLoc, SourceLocation EndLoc,
6143 CXXScopeSpec &ReductionIdScopeSpec,
6144 const DeclarationNameInfo &ReductionId) {
6145 // TODO: Allow scope specification search when 'declare reduction' is
6146 // supported.
6147 assert(ReductionIdScopeSpec.isEmpty() &&
6148 "No support for scoped reduction identifiers yet.");
6149
6150 auto DN = ReductionId.getName();
6151 auto OOK = DN.getCXXOverloadedOperator();
6152 BinaryOperatorKind BOK = BO_Comma;
6153
6154 // OpenMP [2.14.3.6, reduction clause]
6155 // C
6156 // reduction-identifier is either an identifier or one of the following
6157 // operators: +, -, *, &, |, ^, && and ||
6158 // C++
6159 // reduction-identifier is either an id-expression or one of the following
6160 // operators: +, -, *, &, |, ^, && and ||
6161 // FIXME: Only 'min' and 'max' identifiers are supported for now.
6162 switch (OOK) {
6163 case OO_Plus:
6164 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006165 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006166 break;
6167 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006168 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006169 break;
6170 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006171 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006172 break;
6173 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006174 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006175 break;
6176 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006177 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006178 break;
6179 case OO_AmpAmp:
6180 BOK = BO_LAnd;
6181 break;
6182 case OO_PipePipe:
6183 BOK = BO_LOr;
6184 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006185 case OO_New:
6186 case OO_Delete:
6187 case OO_Array_New:
6188 case OO_Array_Delete:
6189 case OO_Slash:
6190 case OO_Percent:
6191 case OO_Tilde:
6192 case OO_Exclaim:
6193 case OO_Equal:
6194 case OO_Less:
6195 case OO_Greater:
6196 case OO_LessEqual:
6197 case OO_GreaterEqual:
6198 case OO_PlusEqual:
6199 case OO_MinusEqual:
6200 case OO_StarEqual:
6201 case OO_SlashEqual:
6202 case OO_PercentEqual:
6203 case OO_CaretEqual:
6204 case OO_AmpEqual:
6205 case OO_PipeEqual:
6206 case OO_LessLess:
6207 case OO_GreaterGreater:
6208 case OO_LessLessEqual:
6209 case OO_GreaterGreaterEqual:
6210 case OO_EqualEqual:
6211 case OO_ExclaimEqual:
6212 case OO_PlusPlus:
6213 case OO_MinusMinus:
6214 case OO_Comma:
6215 case OO_ArrowStar:
6216 case OO_Arrow:
6217 case OO_Call:
6218 case OO_Subscript:
6219 case OO_Conditional:
6220 case NUM_OVERLOADED_OPERATORS:
6221 llvm_unreachable("Unexpected reduction identifier");
6222 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006223 if (auto II = DN.getAsIdentifierInfo()) {
6224 if (II->isStr("max"))
6225 BOK = BO_GT;
6226 else if (II->isStr("min"))
6227 BOK = BO_LT;
6228 }
6229 break;
6230 }
6231 SourceRange ReductionIdRange;
6232 if (ReductionIdScopeSpec.isValid()) {
6233 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
6234 }
6235 ReductionIdRange.setEnd(ReductionId.getEndLoc());
6236 if (BOK == BO_Comma) {
6237 // Not allowed reduction identifier is found.
6238 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
6239 << ReductionIdRange;
6240 return nullptr;
6241 }
6242
6243 SmallVector<Expr *, 8> Vars;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006244 SmallVector<Expr *, 8> LHSs;
6245 SmallVector<Expr *, 8> RHSs;
6246 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006247 for (auto RefExpr : VarList) {
6248 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
6249 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6250 // It will be analyzed later.
6251 Vars.push_back(RefExpr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006252 LHSs.push_back(nullptr);
6253 RHSs.push_back(nullptr);
6254 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006255 continue;
6256 }
6257
6258 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
6259 RefExpr->isInstantiationDependent() ||
6260 RefExpr->containsUnexpandedParameterPack()) {
6261 // It will be analyzed later.
6262 Vars.push_back(RefExpr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006263 LHSs.push_back(nullptr);
6264 RHSs.push_back(nullptr);
6265 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006266 continue;
6267 }
6268
6269 auto ELoc = RefExpr->getExprLoc();
6270 auto ERange = RefExpr->getSourceRange();
6271 // OpenMP [2.1, C/C++]
6272 // A list item is a variable or array section, subject to the restrictions
6273 // specified in Section 2.4 on page 42 and in each of the sections
6274 // describing clauses and directives for which a list appears.
6275 // OpenMP [2.14.3.3, Restrictions, p.1]
6276 // A variable that is part of another variable (as an array or
6277 // structure element) cannot appear in a private clause.
6278 auto DE = dyn_cast<DeclRefExpr>(RefExpr);
6279 if (!DE || !isa<VarDecl>(DE->getDecl())) {
6280 Diag(ELoc, diag::err_omp_expected_var_name) << ERange;
6281 continue;
6282 }
6283 auto D = DE->getDecl();
6284 auto VD = cast<VarDecl>(D);
6285 auto Type = VD->getType();
6286 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
6287 // A variable that appears in a private clause must not have an incomplete
6288 // type or a reference type.
6289 if (RequireCompleteType(ELoc, Type,
6290 diag::err_omp_reduction_incomplete_type))
6291 continue;
6292 // OpenMP [2.14.3.6, reduction clause, Restrictions]
6293 // Arrays may not appear in a reduction clause.
6294 if (Type.getNonReferenceType()->isArrayType()) {
6295 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
6296 bool IsDecl =
6297 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6298 Diag(VD->getLocation(),
6299 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6300 << VD;
6301 continue;
6302 }
6303 // OpenMP [2.14.3.6, reduction clause, Restrictions]
6304 // A list item that appears in a reduction clause must not be
6305 // const-qualified.
6306 if (Type.getNonReferenceType().isConstant(Context)) {
6307 Diag(ELoc, diag::err_omp_const_variable)
6308 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
6309 bool IsDecl =
6310 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6311 Diag(VD->getLocation(),
6312 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6313 << VD;
6314 continue;
6315 }
6316 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
6317 // If a list-item is a reference type then it must bind to the same object
6318 // for all threads of the team.
6319 VarDecl *VDDef = VD->getDefinition();
6320 if (Type->isReferenceType() && VDDef) {
6321 DSARefChecker Check(DSAStack);
6322 if (Check.Visit(VDDef->getInit())) {
6323 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
6324 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
6325 continue;
6326 }
6327 }
6328 // OpenMP [2.14.3.6, reduction clause, Restrictions]
6329 // The type of a list item that appears in a reduction clause must be valid
6330 // for the reduction-identifier. For a max or min reduction in C, the type
6331 // of the list item must be an allowed arithmetic data type: char, int,
6332 // float, double, or _Bool, possibly modified with long, short, signed, or
6333 // unsigned. For a max or min reduction in C++, the type of the list item
6334 // must be an allowed arithmetic data type: char, wchar_t, int, float,
6335 // double, or bool, possibly modified with long, short, signed, or unsigned.
6336 if ((BOK == BO_GT || BOK == BO_LT) &&
6337 !(Type->isScalarType() ||
6338 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
6339 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
6340 << getLangOpts().CPlusPlus;
6341 bool IsDecl =
6342 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6343 Diag(VD->getLocation(),
6344 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6345 << VD;
6346 continue;
6347 }
6348 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
6349 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
6350 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
6351 bool IsDecl =
6352 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6353 Diag(VD->getLocation(),
6354 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6355 << VD;
6356 continue;
6357 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00006358 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
6359 // in a Construct]
6360 // Variables with the predetermined data-sharing attributes may not be
6361 // listed in data-sharing attributes clauses, except for the cases
6362 // listed below. For these exceptions only, listing a predetermined
6363 // variable in a data-sharing attribute clause is allowed and overrides
6364 // the variable's predetermined data-sharing attributes.
6365 // OpenMP [2.14.3.6, Restrictions, p.3]
6366 // Any number of reduction clauses can be specified on the directive,
6367 // but a list item can appear only once in the reduction clauses for that
6368 // directive.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006369 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006370 if (DVar.CKind == OMPC_reduction) {
6371 Diag(ELoc, diag::err_omp_once_referenced)
6372 << getOpenMPClauseName(OMPC_reduction);
6373 if (DVar.RefExpr) {
6374 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
6375 }
6376 } else if (DVar.CKind != OMPC_unknown) {
6377 Diag(ELoc, diag::err_omp_wrong_dsa)
6378 << getOpenMPClauseName(DVar.CKind)
6379 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006380 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006381 continue;
6382 }
6383
6384 // OpenMP [2.14.3.6, Restrictions, p.1]
6385 // A list item that appears in a reduction clause of a worksharing
6386 // construct must be shared in the parallel regions to which any of the
6387 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf29276e2014-06-18 04:14:57 +00006388 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataev549210e2014-06-24 04:39:47 +00006389 if (isOpenMPWorksharingDirective(CurrDir) &&
6390 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006391 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006392 if (DVar.CKind != OMPC_shared) {
6393 Diag(ELoc, diag::err_omp_required_access)
6394 << getOpenMPClauseName(OMPC_reduction)
6395 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006396 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006397 continue;
6398 }
6399 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006400 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00006401 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs");
6402 auto *RHSVD = buildVarDecl(*this, ELoc, Type, VD->getName());
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006403 // Add initializer for private variable.
6404 Expr *Init = nullptr;
6405 switch (BOK) {
6406 case BO_Add:
6407 case BO_Xor:
6408 case BO_Or:
6409 case BO_LOr:
6410 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
6411 if (Type->isScalarType() || Type->isAnyComplexType()) {
6412 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00006413 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006414 break;
6415 case BO_Mul:
6416 case BO_LAnd:
6417 if (Type->isScalarType() || Type->isAnyComplexType()) {
6418 // '*' and '&&' reduction ops - initializer is '1'.
6419 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
6420 }
6421 break;
6422 case BO_And: {
6423 // '&' reduction op - initializer is '~0'.
6424 QualType OrigType = Type;
6425 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) {
6426 Type = ComplexTy->getElementType();
6427 }
6428 if (Type->isRealFloatingType()) {
6429 llvm::APFloat InitValue =
6430 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
6431 /*isIEEE=*/true);
6432 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
6433 Type, ELoc);
6434 } else if (Type->isScalarType()) {
6435 auto Size = Context.getTypeSize(Type);
6436 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
6437 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
6438 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
6439 }
6440 if (Init && OrigType->isAnyComplexType()) {
6441 // Init = 0xFFFF + 0xFFFFi;
6442 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
6443 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
6444 }
6445 Type = OrigType;
6446 break;
6447 }
6448 case BO_LT:
6449 case BO_GT: {
6450 // 'min' reduction op - initializer is 'Largest representable number in
6451 // the reduction list item type'.
6452 // 'max' reduction op - initializer is 'Least representable number in
6453 // the reduction list item type'.
6454 if (Type->isIntegerType() || Type->isPointerType()) {
6455 bool IsSigned = Type->hasSignedIntegerRepresentation();
6456 auto Size = Context.getTypeSize(Type);
6457 QualType IntTy =
6458 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
6459 llvm::APInt InitValue =
6460 (BOK != BO_LT)
6461 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
6462 : llvm::APInt::getMinValue(Size)
6463 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
6464 : llvm::APInt::getMaxValue(Size);
6465 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
6466 if (Type->isPointerType()) {
6467 // Cast to pointer type.
6468 auto CastExpr = BuildCStyleCastExpr(
6469 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
6470 SourceLocation(), Init);
6471 if (CastExpr.isInvalid())
6472 continue;
6473 Init = CastExpr.get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00006474 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006475 } else if (Type->isRealFloatingType()) {
6476 llvm::APFloat InitValue = llvm::APFloat::getLargest(
6477 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
6478 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
6479 Type, ELoc);
6480 }
6481 break;
6482 }
6483 case BO_PtrMemD:
6484 case BO_PtrMemI:
6485 case BO_MulAssign:
6486 case BO_Div:
6487 case BO_Rem:
6488 case BO_Sub:
6489 case BO_Shl:
6490 case BO_Shr:
6491 case BO_LE:
6492 case BO_GE:
6493 case BO_EQ:
6494 case BO_NE:
6495 case BO_AndAssign:
6496 case BO_XorAssign:
6497 case BO_OrAssign:
6498 case BO_Assign:
6499 case BO_AddAssign:
6500 case BO_SubAssign:
6501 case BO_DivAssign:
6502 case BO_RemAssign:
6503 case BO_ShlAssign:
6504 case BO_ShrAssign:
6505 case BO_Comma:
6506 llvm_unreachable("Unexpected reduction operation");
6507 }
6508 if (Init) {
6509 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
6510 /*TypeMayContainAuto=*/false);
6511 } else {
6512 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
6513 }
6514 if (!RHSVD->hasInit()) {
6515 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
6516 << ReductionIdRange;
6517 bool IsDecl =
6518 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6519 Diag(VD->getLocation(),
6520 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6521 << VD;
6522 continue;
6523 }
Alexey Bataev39f915b82015-05-08 10:41:21 +00006524 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
6525 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006526 ExprResult ReductionOp =
6527 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
6528 LHSDRE, RHSDRE);
6529 if (ReductionOp.isUsable()) {
Alexey Bataev69a47792015-05-07 03:54:03 +00006530 if (BOK != BO_LT && BOK != BO_GT) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006531 ReductionOp =
6532 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
6533 BO_Assign, LHSDRE, ReductionOp.get());
6534 } else {
6535 auto *ConditionalOp = new (Context) ConditionalOperator(
6536 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
6537 RHSDRE, Type, VK_LValue, OK_Ordinary);
6538 ReductionOp =
6539 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
6540 BO_Assign, LHSDRE, ConditionalOp);
6541 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006542 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00006543 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006544 if (ReductionOp.isInvalid())
6545 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006546
6547 DSAStack->addDSA(VD, DE, OMPC_reduction);
6548 Vars.push_back(DE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006549 LHSs.push_back(LHSDRE);
6550 RHSs.push_back(RHSDRE);
6551 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00006552 }
6553
6554 if (Vars.empty())
6555 return nullptr;
6556
6557 return OMPReductionClause::Create(
6558 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006559 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, LHSs,
6560 RHSs, ReductionOps);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006561}
6562
Alexey Bataev182227b2015-08-20 10:54:39 +00006563OMPClause *Sema::ActOnOpenMPLinearClause(
6564 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
6565 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
6566 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00006567 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006568 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00006569 SmallVector<Expr *, 8> Inits;
Alexey Bataev182227b2015-08-20 10:54:39 +00006570 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
6571 LinKind == OMPC_LINEAR_unknown) {
6572 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
6573 LinKind = OMPC_LINEAR_val;
6574 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006575 for (auto &RefExpr : VarList) {
6576 assert(RefExpr && "NULL expr in OpenMP linear clause.");
6577 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00006578 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006579 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006580 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00006581 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00006582 continue;
6583 }
6584
6585 // OpenMP [2.14.3.7, linear clause]
6586 // A list item that appears in a linear clause is subject to the private
6587 // clause semantics described in Section 2.14.3.3 on page 159 except as
6588 // noted. In addition, the value of the new list item on each iteration
6589 // of the associated loop(s) corresponds to the value of the original
6590 // list item before entering the construct plus the logical number of
6591 // the iteration times linear-step.
6592
Alexey Bataeved09d242014-05-28 05:53:51 +00006593 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00006594 // OpenMP [2.1, C/C++]
6595 // A list item is a variable name.
6596 // OpenMP [2.14.3.3, Restrictions, p.1]
6597 // A variable that is part of another variable (as an array or
6598 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00006599 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00006600 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006601 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00006602 continue;
6603 }
6604
6605 VarDecl *VD = cast<VarDecl>(DE->getDecl());
6606
6607 // OpenMP [2.14.3.7, linear clause]
6608 // A list-item cannot appear in more than one linear clause.
6609 // A list-item that appears in a linear clause cannot appear in any
6610 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006611 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00006612 if (DVar.RefExpr) {
6613 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
6614 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006615 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00006616 continue;
6617 }
6618
6619 QualType QType = VD->getType();
6620 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
6621 // It will be analyzed later.
6622 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006623 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00006624 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00006625 continue;
6626 }
6627
6628 // A variable must not have an incomplete type or a reference type.
6629 if (RequireCompleteType(ELoc, QType,
6630 diag::err_omp_linear_incomplete_type)) {
6631 continue;
6632 }
Alexey Bataev1185e192015-08-20 12:15:57 +00006633 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
6634 !QType->isReferenceType()) {
6635 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
6636 << QType << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
6637 continue;
6638 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006639 QType = QType.getNonReferenceType();
Alexander Musman8dba6642014-04-22 13:09:42 +00006640
6641 // A list item must not be const-qualified.
6642 if (QType.isConstant(Context)) {
6643 Diag(ELoc, diag::err_omp_const_variable)
6644 << getOpenMPClauseName(OMPC_linear);
6645 bool IsDecl =
6646 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6647 Diag(VD->getLocation(),
6648 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6649 << VD;
6650 continue;
6651 }
6652
6653 // A list item must be of integral or pointer type.
6654 QType = QType.getUnqualifiedType().getCanonicalType();
6655 const Type *Ty = QType.getTypePtrOrNull();
6656 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
6657 !Ty->isPointerType())) {
6658 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
6659 bool IsDecl =
6660 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6661 Diag(VD->getLocation(),
6662 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6663 << VD;
6664 continue;
6665 }
6666
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006667 // Build private copy of original var.
6668 auto *Private = buildVarDecl(*this, ELoc, QType, VD->getName());
6669 auto *PrivateRef = buildDeclRefExpr(
6670 *this, Private, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman3276a272015-03-21 10:12:56 +00006671 // Build var to save initial value.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006672 VarDecl *Init = buildVarDecl(*this, ELoc, QType, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00006673 Expr *InitExpr;
6674 if (LinKind == OMPC_LINEAR_uval)
6675 InitExpr = VD->getInit();
6676 else
6677 InitExpr = DE;
6678 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexander Musman3276a272015-03-21 10:12:56 +00006679 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006680 auto InitRef = buildDeclRefExpr(
6681 *this, Init, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman8dba6642014-04-22 13:09:42 +00006682 DSAStack->addDSA(VD, DE, OMPC_linear);
6683 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006684 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00006685 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00006686 }
6687
6688 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006689 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00006690
6691 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00006692 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00006693 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
6694 !Step->isInstantiationDependent() &&
6695 !Step->containsUnexpandedParameterPack()) {
6696 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006697 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00006698 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006699 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006700 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00006701
Alexander Musman3276a272015-03-21 10:12:56 +00006702 // Build var to save the step value.
6703 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006704 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00006705 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006706 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00006707 ExprResult CalcStep =
6708 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006709 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00006710
Alexander Musman8dba6642014-04-22 13:09:42 +00006711 // Warn about zero linear step (it would be probably better specified as
6712 // making corresponding variables 'const').
6713 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00006714 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
6715 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00006716 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
6717 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00006718 if (!IsConstant && CalcStep.isUsable()) {
6719 // Calculate the step beforehand instead of doing this on each iteration.
6720 // (This is not used if the number of iterations may be kfold-ed).
6721 CalcStepExpr = CalcStep.get();
6722 }
Alexander Musman8dba6642014-04-22 13:09:42 +00006723 }
6724
Alexey Bataev182227b2015-08-20 10:54:39 +00006725 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
6726 ColonLoc, EndLoc, Vars, Privates, Inits,
6727 StepExpr, CalcStepExpr);
Alexander Musman3276a272015-03-21 10:12:56 +00006728}
6729
6730static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
6731 Expr *NumIterations, Sema &SemaRef,
6732 Scope *S) {
6733 // Walk the vars and build update/final expressions for the CodeGen.
6734 SmallVector<Expr *, 8> Updates;
6735 SmallVector<Expr *, 8> Finals;
6736 Expr *Step = Clause.getStep();
6737 Expr *CalcStep = Clause.getCalcStep();
6738 // OpenMP [2.14.3.7, linear clause]
6739 // If linear-step is not specified it is assumed to be 1.
6740 if (Step == nullptr)
6741 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
6742 else if (CalcStep)
6743 Step = cast<BinaryOperator>(CalcStep)->getLHS();
6744 bool HasErrors = false;
6745 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006746 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00006747 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00006748 for (auto &RefExpr : Clause.varlists()) {
6749 Expr *InitExpr = *CurInit;
6750
6751 // Build privatized reference to the current linear var.
6752 auto DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00006753 Expr *CapturedRef;
6754 if (LinKind == OMPC_LINEAR_uval)
6755 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
6756 else
6757 CapturedRef =
6758 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
6759 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
6760 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00006761
6762 // Build update: Var = InitExpr + IV * Step
6763 ExprResult Update =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006764 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
Alexander Musman3276a272015-03-21 10:12:56 +00006765 InitExpr, IV, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006766 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
6767 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00006768
6769 // Build final: Var = InitExpr + NumIterations * Step
6770 ExprResult Final =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006771 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
Alexey Bataev39f915b82015-05-08 10:41:21 +00006772 InitExpr, NumIterations, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006773 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
6774 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00006775 if (!Update.isUsable() || !Final.isUsable()) {
6776 Updates.push_back(nullptr);
6777 Finals.push_back(nullptr);
6778 HasErrors = true;
6779 } else {
6780 Updates.push_back(Update.get());
6781 Finals.push_back(Final.get());
6782 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006783 ++CurInit, ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00006784 }
6785 Clause.setUpdates(Updates);
6786 Clause.setFinals(Finals);
6787 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00006788}
6789
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006790OMPClause *Sema::ActOnOpenMPAlignedClause(
6791 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
6792 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
6793
6794 SmallVector<Expr *, 8> Vars;
6795 for (auto &RefExpr : VarList) {
6796 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
6797 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6798 // It will be analyzed later.
6799 Vars.push_back(RefExpr);
6800 continue;
6801 }
6802
6803 SourceLocation ELoc = RefExpr->getExprLoc();
6804 // OpenMP [2.1, C/C++]
6805 // A list item is a variable name.
6806 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
6807 if (!DE || !isa<VarDecl>(DE->getDecl())) {
6808 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
6809 continue;
6810 }
6811
6812 VarDecl *VD = cast<VarDecl>(DE->getDecl());
6813
6814 // OpenMP [2.8.1, simd construct, Restrictions]
6815 // The type of list items appearing in the aligned clause must be
6816 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006817 QualType QType = VD->getType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006818 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006819 const Type *Ty = QType.getTypePtrOrNull();
6820 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
6821 !Ty->isPointerType())) {
6822 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
6823 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
6824 bool IsDecl =
6825 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6826 Diag(VD->getLocation(),
6827 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6828 << VD;
6829 continue;
6830 }
6831
6832 // OpenMP [2.8.1, simd construct, Restrictions]
6833 // A list-item cannot appear in more than one aligned clause.
6834 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
6835 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
6836 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
6837 << getOpenMPClauseName(OMPC_aligned);
6838 continue;
6839 }
6840
6841 Vars.push_back(DE);
6842 }
6843
6844 // OpenMP [2.8.1, simd construct, Description]
6845 // The parameter of the aligned clause, alignment, must be a constant
6846 // positive integer expression.
6847 // If no optional parameter is specified, implementation-defined default
6848 // alignments for SIMD instructions on the target platforms are assumed.
6849 if (Alignment != nullptr) {
6850 ExprResult AlignResult =
6851 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
6852 if (AlignResult.isInvalid())
6853 return nullptr;
6854 Alignment = AlignResult.get();
6855 }
6856 if (Vars.empty())
6857 return nullptr;
6858
6859 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
6860 EndLoc, Vars, Alignment);
6861}
6862
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006863OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
6864 SourceLocation StartLoc,
6865 SourceLocation LParenLoc,
6866 SourceLocation EndLoc) {
6867 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006868 SmallVector<Expr *, 8> SrcExprs;
6869 SmallVector<Expr *, 8> DstExprs;
6870 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00006871 for (auto &RefExpr : VarList) {
6872 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
6873 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006874 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006875 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006876 SrcExprs.push_back(nullptr);
6877 DstExprs.push_back(nullptr);
6878 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006879 continue;
6880 }
6881
Alexey Bataeved09d242014-05-28 05:53:51 +00006882 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006883 // OpenMP [2.1, C/C++]
6884 // A list item is a variable name.
6885 // OpenMP [2.14.4.1, Restrictions, p.1]
6886 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00006887 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006888 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006889 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006890 continue;
6891 }
6892
6893 Decl *D = DE->getDecl();
6894 VarDecl *VD = cast<VarDecl>(D);
6895
6896 QualType Type = VD->getType();
6897 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6898 // It will be analyzed later.
6899 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006900 SrcExprs.push_back(nullptr);
6901 DstExprs.push_back(nullptr);
6902 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006903 continue;
6904 }
6905
6906 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
6907 // A list item that appears in a copyin clause must be threadprivate.
6908 if (!DSAStack->isThreadPrivate(VD)) {
6909 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00006910 << getOpenMPClauseName(OMPC_copyin)
6911 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006912 continue;
6913 }
6914
6915 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
6916 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00006917 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006918 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006919 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00006920 auto *SrcVD = buildVarDecl(*this, DE->getLocStart(),
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006921 ElemType.getUnqualifiedType(), ".copyin.src");
Alexey Bataev39f915b82015-05-08 10:41:21 +00006922 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006923 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
6924 auto *DstVD =
6925 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst");
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006926 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006927 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006928 // For arrays generate assignment operation for single element and replace
6929 // it by the original array element in CodeGen.
6930 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
6931 PseudoDstExpr, PseudoSrcExpr);
6932 if (AssignmentOp.isInvalid())
6933 continue;
6934 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
6935 /*DiscardedValue=*/true);
6936 if (AssignmentOp.isInvalid())
6937 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006938
6939 DSAStack->addDSA(VD, DE, OMPC_copyin);
6940 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006941 SrcExprs.push_back(PseudoSrcExpr);
6942 DstExprs.push_back(PseudoDstExpr);
6943 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006944 }
6945
Alexey Bataeved09d242014-05-28 05:53:51 +00006946 if (Vars.empty())
6947 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006948
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006949 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
6950 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006951}
6952
Alexey Bataevbae9a792014-06-27 10:37:06 +00006953OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
6954 SourceLocation StartLoc,
6955 SourceLocation LParenLoc,
6956 SourceLocation EndLoc) {
6957 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00006958 SmallVector<Expr *, 8> SrcExprs;
6959 SmallVector<Expr *, 8> DstExprs;
6960 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00006961 for (auto &RefExpr : VarList) {
6962 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
6963 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6964 // It will be analyzed later.
6965 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00006966 SrcExprs.push_back(nullptr);
6967 DstExprs.push_back(nullptr);
6968 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00006969 continue;
6970 }
6971
6972 SourceLocation ELoc = RefExpr->getExprLoc();
6973 // OpenMP [2.1, C/C++]
6974 // A list item is a variable name.
6975 // OpenMP [2.14.4.1, Restrictions, p.1]
6976 // A list item that appears in a copyin clause must be threadprivate.
6977 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
6978 if (!DE || !isa<VarDecl>(DE->getDecl())) {
6979 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
6980 continue;
6981 }
6982
6983 Decl *D = DE->getDecl();
6984 VarDecl *VD = cast<VarDecl>(D);
6985
6986 QualType Type = VD->getType();
6987 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6988 // It will be analyzed later.
6989 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00006990 SrcExprs.push_back(nullptr);
6991 DstExprs.push_back(nullptr);
6992 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00006993 continue;
6994 }
6995
6996 // OpenMP [2.14.4.2, Restrictions, p.2]
6997 // A list item that appears in a copyprivate clause may not appear in a
6998 // private or firstprivate clause on the single construct.
6999 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007000 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00007001 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
7002 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00007003 Diag(ELoc, diag::err_omp_wrong_dsa)
7004 << getOpenMPClauseName(DVar.CKind)
7005 << getOpenMPClauseName(OMPC_copyprivate);
7006 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7007 continue;
7008 }
7009
7010 // OpenMP [2.11.4.2, Restrictions, p.1]
7011 // All list items that appear in a copyprivate clause must be either
7012 // threadprivate or private in the enclosing context.
7013 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007014 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00007015 if (DVar.CKind == OMPC_shared) {
7016 Diag(ELoc, diag::err_omp_required_access)
7017 << getOpenMPClauseName(OMPC_copyprivate)
7018 << "threadprivate or private in the enclosing context";
7019 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7020 continue;
7021 }
7022 }
7023 }
7024
Alexey Bataev7a3e5852015-05-19 08:19:24 +00007025 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007026 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00007027 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007028 << getOpenMPClauseName(OMPC_copyprivate) << Type
7029 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00007030 bool IsDecl =
7031 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7032 Diag(VD->getLocation(),
7033 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7034 << VD;
7035 continue;
7036 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007037
Alexey Bataevbae9a792014-06-27 10:37:06 +00007038 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
7039 // A variable of class type (or array thereof) that appears in a
7040 // copyin clause requires an accessible, unambiguous copy assignment
7041 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007042 Type = Context.getBaseElementType(Type.getNonReferenceType())
7043 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +00007044 auto *SrcVD =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007045 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.src");
Alexey Bataev420d45b2015-04-14 05:11:24 +00007046 auto *PseudoSrcExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007047 buildDeclRefExpr(*this, SrcVD, Type, DE->getExprLoc());
Alexey Bataev420d45b2015-04-14 05:11:24 +00007048 auto *DstVD =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007049 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.dst");
Alexey Bataev420d45b2015-04-14 05:11:24 +00007050 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007051 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataeva63048e2015-03-23 06:18:07 +00007052 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
7053 PseudoDstExpr, PseudoSrcExpr);
7054 if (AssignmentOp.isInvalid())
7055 continue;
7056 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
7057 /*DiscardedValue=*/true);
7058 if (AssignmentOp.isInvalid())
7059 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00007060
7061 // No need to mark vars as copyprivate, they are already threadprivate or
7062 // implicitly private.
7063 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00007064 SrcExprs.push_back(PseudoSrcExpr);
7065 DstExprs.push_back(PseudoDstExpr);
7066 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +00007067 }
7068
7069 if (Vars.empty())
7070 return nullptr;
7071
Alexey Bataeva63048e2015-03-23 06:18:07 +00007072 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
7073 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00007074}
7075
Alexey Bataev6125da92014-07-21 11:26:11 +00007076OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
7077 SourceLocation StartLoc,
7078 SourceLocation LParenLoc,
7079 SourceLocation EndLoc) {
7080 if (VarList.empty())
7081 return nullptr;
7082
7083 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
7084}
Alexey Bataevdea47612014-07-23 07:46:59 +00007085
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007086OMPClause *
7087Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
7088 SourceLocation DepLoc, SourceLocation ColonLoc,
7089 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
7090 SourceLocation LParenLoc, SourceLocation EndLoc) {
7091 if (DepKind == OMPC_DEPEND_unknown) {
7092 std::string Values;
7093 std::string Sep(", ");
7094 for (unsigned i = 0; i < OMPC_DEPEND_unknown; ++i) {
7095 Values += "'";
7096 Values += getOpenMPSimpleClauseTypeName(OMPC_depend, i);
7097 Values += "'";
7098 switch (i) {
7099 case OMPC_DEPEND_unknown - 2:
7100 Values += " or ";
7101 break;
7102 case OMPC_DEPEND_unknown - 1:
7103 break;
7104 default:
7105 Values += Sep;
7106 break;
7107 }
7108 }
7109 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
7110 << Values << getOpenMPClauseName(OMPC_depend);
7111 return nullptr;
7112 }
7113 SmallVector<Expr *, 8> Vars;
7114 for (auto &RefExpr : VarList) {
7115 assert(RefExpr && "NULL expr in OpenMP shared clause.");
7116 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7117 // It will be analyzed later.
7118 Vars.push_back(RefExpr);
7119 continue;
7120 }
7121
7122 SourceLocation ELoc = RefExpr->getExprLoc();
7123 // OpenMP [2.11.1.1, Restrictions, p.3]
7124 // A variable that is part of another variable (such as a field of a
7125 // structure) but is not an array element or an array section cannot appear
7126 // in a depend clause.
7127 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
Alexey Bataev1a3320e2015-08-25 14:24:04 +00007128 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
7129 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
7130 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
7131 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
7132 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007133 (ASE && !ASE->getBase()->getType()->isAnyPointerType() &&
7134 !ASE->getBase()->getType()->isArrayType())) {
7135 Diag(ELoc, diag::err_omp_expected_var_name_or_array_item)
7136 << RefExpr->getSourceRange();
7137 continue;
7138 }
7139
7140 Vars.push_back(RefExpr->IgnoreParenImpCasts());
7141 }
7142
7143 if (Vars.empty())
7144 return nullptr;
7145
7146 return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
7147 DepLoc, ColonLoc, Vars);
7148}
Michael Wonge710d542015-08-07 16:16:36 +00007149
7150OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
7151 SourceLocation LParenLoc,
7152 SourceLocation EndLoc) {
7153 Expr *ValExpr = Device;
7154 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
7155 !ValExpr->isInstantiationDependent()) {
7156 SourceLocation Loc = ValExpr->getExprLoc();
7157 ExprResult Value = PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
7158 if (Value.isInvalid())
7159 return nullptr;
7160
7161 // OpenMP [2.9.1, Restrictions]
7162 // The device expression must evaluate to a non-negative integer value.
7163 llvm::APSInt Result;
7164 if (Value.get()->isIntegerConstantExpr(Result, Context) &&
7165 Result.isSigned() && !Result.isStrictlyPositive()) {
7166 Diag(Loc, diag::err_omp_negative_expression_in_clause)
7167 << "device" << ValExpr->getSourceRange();
7168 return nullptr;
7169 }
7170 }
7171
7172 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
7173}