blob: 1a7c7760ef47640ecbcf8397e37460a0889ec11a [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);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001161 QualType KmpInt32PtrTy =
1162 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001163 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001164 std::make_pair(".global_tid.", KmpInt32PtrTy),
1165 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1166 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001167 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001168 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1169 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001170 break;
1171 }
1172 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001173 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001174 std::make_pair(StringRef(), QualType()) // __context with shared vars
1175 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001176 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1177 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001178 break;
1179 }
1180 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001181 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001182 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001183 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001184 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1185 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001186 break;
1187 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001188 case OMPD_for_simd: {
1189 Sema::CapturedParamNameType Params[] = {
1190 std::make_pair(StringRef(), QualType()) // __context with shared vars
1191 };
1192 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1193 Params);
1194 break;
1195 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001196 case OMPD_sections: {
1197 Sema::CapturedParamNameType Params[] = {
1198 std::make_pair(StringRef(), QualType()) // __context with shared vars
1199 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001200 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1201 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001202 break;
1203 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001204 case OMPD_section: {
1205 Sema::CapturedParamNameType Params[] = {
1206 std::make_pair(StringRef(), QualType()) // __context with shared vars
1207 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001208 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1209 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001210 break;
1211 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001212 case OMPD_single: {
1213 Sema::CapturedParamNameType Params[] = {
1214 std::make_pair(StringRef(), QualType()) // __context with shared vars
1215 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001216 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1217 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001218 break;
1219 }
Alexander Musman80c22892014-07-17 08:54:58 +00001220 case OMPD_master: {
1221 Sema::CapturedParamNameType Params[] = {
1222 std::make_pair(StringRef(), QualType()) // __context with shared vars
1223 };
1224 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1225 Params);
1226 break;
1227 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001228 case OMPD_critical: {
1229 Sema::CapturedParamNameType Params[] = {
1230 std::make_pair(StringRef(), QualType()) // __context with shared vars
1231 };
1232 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1233 Params);
1234 break;
1235 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001236 case OMPD_parallel_for: {
1237 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001238 QualType KmpInt32PtrTy =
1239 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev4acb8592014-07-07 13:01:15 +00001240 Sema::CapturedParamNameType Params[] = {
1241 std::make_pair(".global_tid.", KmpInt32PtrTy),
1242 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1243 std::make_pair(StringRef(), QualType()) // __context with shared vars
1244 };
1245 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1246 Params);
1247 break;
1248 }
Alexander Musmane4e893b2014-09-23 09:33:00 +00001249 case OMPD_parallel_for_simd: {
1250 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001251 QualType KmpInt32PtrTy =
1252 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexander Musmane4e893b2014-09-23 09:33:00 +00001253 Sema::CapturedParamNameType Params[] = {
1254 std::make_pair(".global_tid.", KmpInt32PtrTy),
1255 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1256 std::make_pair(StringRef(), QualType()) // __context with shared vars
1257 };
1258 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1259 Params);
1260 break;
1261 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001262 case OMPD_parallel_sections: {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001263 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001264 QualType KmpInt32PtrTy =
1265 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001266 Sema::CapturedParamNameType Params[] = {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001267 std::make_pair(".global_tid.", KmpInt32PtrTy),
1268 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001269 std::make_pair(StringRef(), QualType()) // __context with shared vars
1270 };
1271 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1272 Params);
1273 break;
1274 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001275 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001276 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001277 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1278 FunctionProtoType::ExtProtoInfo EPI;
1279 EPI.Variadic = true;
1280 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001281 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001282 std::make_pair(".global_tid.", KmpInt32Ty),
1283 std::make_pair(".part_id.", KmpInt32Ty),
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001284 std::make_pair(".privates.",
1285 Context.VoidPtrTy.withConst().withRestrict()),
1286 std::make_pair(
1287 ".copy_fn.",
1288 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001289 std::make_pair(StringRef(), QualType()) // __context with shared vars
1290 };
1291 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1292 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001293 // Mark this captured region as inlined, because we don't use outlined
1294 // function directly.
1295 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1296 AlwaysInlineAttr::CreateImplicit(
1297 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001298 break;
1299 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001300 case OMPD_ordered: {
1301 Sema::CapturedParamNameType Params[] = {
1302 std::make_pair(StringRef(), QualType()) // __context with shared vars
1303 };
1304 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1305 Params);
1306 break;
1307 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001308 case OMPD_atomic: {
1309 Sema::CapturedParamNameType Params[] = {
1310 std::make_pair(StringRef(), QualType()) // __context with shared vars
1311 };
1312 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1313 Params);
1314 break;
1315 }
Michael Wong65f367f2015-07-21 13:44:28 +00001316 case OMPD_target_data:
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001317 case OMPD_target: {
1318 Sema::CapturedParamNameType Params[] = {
1319 std::make_pair(StringRef(), QualType()) // __context with shared vars
1320 };
1321 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1322 Params);
1323 break;
1324 }
Alexey Bataev13314bf2014-10-09 04:18:56 +00001325 case OMPD_teams: {
1326 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001327 QualType KmpInt32PtrTy =
1328 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev13314bf2014-10-09 04:18:56 +00001329 Sema::CapturedParamNameType Params[] = {
1330 std::make_pair(".global_tid.", KmpInt32PtrTy),
1331 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1332 std::make_pair(StringRef(), QualType()) // __context with shared vars
1333 };
1334 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1335 Params);
1336 break;
1337 }
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001338 case OMPD_taskgroup: {
1339 Sema::CapturedParamNameType Params[] = {
1340 std::make_pair(StringRef(), QualType()) // __context with shared vars
1341 };
1342 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1343 Params);
1344 break;
1345 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001346 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001347 case OMPD_taskyield:
1348 case OMPD_barrier:
1349 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001350 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001351 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00001352 case OMPD_flush:
Alexey Bataev9959db52014-05-06 10:08:46 +00001353 llvm_unreachable("OpenMP Directive is not allowed");
1354 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001355 llvm_unreachable("Unknown OpenMP directive");
1356 }
1357}
1358
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001359StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1360 ArrayRef<OMPClause *> Clauses) {
1361 if (!S.isUsable()) {
1362 ActOnCapturedRegionError();
1363 return StmtError();
1364 }
Alexey Bataev040d5402015-05-12 08:35:28 +00001365 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001366 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001367 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001368 Clause->getClauseKind() == OMPC_copyprivate ||
1369 (getLangOpts().OpenMPUseTLS &&
1370 getASTContext().getTargetInfo().isTLSSupported() &&
1371 Clause->getClauseKind() == OMPC_copyin)) {
1372 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001373 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001374 for (auto *VarRef : Clause->children()) {
1375 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001376 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001377 }
1378 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001379 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev040d5402015-05-12 08:35:28 +00001380 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
1381 Clause->getClauseKind() == OMPC_schedule) {
1382 // Mark all variables in private list clauses as used in inner region.
1383 // Required for proper codegen of combined directives.
1384 // TODO: add processing for other clauses.
1385 if (auto *E = cast_or_null<Expr>(
1386 cast<OMPScheduleClause>(Clause)->getHelperChunkSize())) {
1387 MarkDeclarationsReferencedInExpr(E);
1388 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001389 }
1390 }
1391 return ActOnCapturedRegionEnd(S.get());
1392}
1393
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001394static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1395 OpenMPDirectiveKind CurrentRegion,
1396 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001397 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001398 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001399 // Allowed nesting of constructs
1400 // +------------------+-----------------+------------------------------------+
1401 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1402 // +------------------+-----------------+------------------------------------+
1403 // | parallel | parallel | * |
1404 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001405 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001406 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001407 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001408 // | parallel | simd | * |
1409 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001410 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001411 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001412 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001413 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001414 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001415 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001416 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001417 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001418 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001419 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001420 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001421 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001422 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001423 // | parallel | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001424 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001425 // | parallel | cancellation | |
1426 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001427 // | parallel | cancel | ! |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001428 // +------------------+-----------------+------------------------------------+
1429 // | for | parallel | * |
1430 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001431 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001432 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001433 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001434 // | for | simd | * |
1435 // | for | sections | + |
1436 // | for | section | + |
1437 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001438 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001439 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001440 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001441 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001442 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001443 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001444 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001445 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001446 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001447 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001448 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001449 // | for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001450 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001451 // | for | cancellation | |
1452 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001453 // | for | cancel | ! |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001454 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001455 // | master | parallel | * |
1456 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001457 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001458 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001459 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001460 // | master | simd | * |
1461 // | master | sections | + |
1462 // | master | section | + |
1463 // | master | single | + |
1464 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001465 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001466 // | master |parallel sections| * |
1467 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001468 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001469 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001470 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001471 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001472 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001473 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001474 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001475 // | master | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001476 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001477 // | master | cancellation | |
1478 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001479 // | master | cancel | |
Alexander Musman80c22892014-07-17 08:54:58 +00001480 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001481 // | critical | parallel | * |
1482 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001483 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001484 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001485 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001486 // | critical | simd | * |
1487 // | critical | sections | + |
1488 // | critical | section | + |
1489 // | critical | single | + |
1490 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001491 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001492 // | critical |parallel sections| * |
1493 // | critical | task | * |
1494 // | critical | taskyield | * |
1495 // | critical | barrier | + |
1496 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001497 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001498 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001499 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001500 // | critical | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001501 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001502 // | critical | cancellation | |
1503 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001504 // | critical | cancel | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001505 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001506 // | simd | parallel | |
1507 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001508 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00001509 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001510 // | simd | critical | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001511 // | simd | simd | |
1512 // | simd | sections | |
1513 // | simd | section | |
1514 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001515 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001516 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001517 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001518 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00001519 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001520 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001521 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001522 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00001523 // | simd | flush | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001524 // | simd | ordered | |
Alexey Bataev0162e452014-07-22 10:10:35 +00001525 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001526 // | simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001527 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001528 // | simd | cancellation | |
1529 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001530 // | simd | cancel | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001531 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00001532 // | for simd | parallel | |
1533 // | for simd | for | |
1534 // | for simd | for simd | |
1535 // | for simd | master | |
1536 // | for simd | critical | |
1537 // | for simd | simd | |
1538 // | for simd | sections | |
1539 // | for simd | section | |
1540 // | for simd | single | |
1541 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001542 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001543 // | for simd |parallel sections| |
1544 // | for simd | task | |
1545 // | for simd | taskyield | |
1546 // | for simd | barrier | |
1547 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001548 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001549 // | for simd | flush | |
1550 // | for simd | ordered | |
1551 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001552 // | for simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001553 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001554 // | for simd | cancellation | |
1555 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001556 // | for simd | cancel | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001557 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00001558 // | parallel for simd| parallel | |
1559 // | parallel for simd| for | |
1560 // | parallel for simd| for simd | |
1561 // | parallel for simd| master | |
1562 // | parallel for simd| critical | |
1563 // | parallel for simd| simd | |
1564 // | parallel for simd| sections | |
1565 // | parallel for simd| section | |
1566 // | parallel for simd| single | |
1567 // | parallel for simd| parallel for | |
1568 // | parallel for simd|parallel for simd| |
1569 // | parallel for simd|parallel sections| |
1570 // | parallel for simd| task | |
1571 // | parallel for simd| taskyield | |
1572 // | parallel for simd| barrier | |
1573 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001574 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001575 // | parallel for simd| flush | |
1576 // | parallel for simd| ordered | |
1577 // | parallel for simd| atomic | |
1578 // | parallel for simd| target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001579 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001580 // | parallel for simd| cancellation | |
1581 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001582 // | parallel for simd| cancel | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001583 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001584 // | sections | parallel | * |
1585 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001586 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001587 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001588 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001589 // | sections | simd | * |
1590 // | sections | sections | + |
1591 // | sections | section | * |
1592 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001593 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001594 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001595 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001596 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001597 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001598 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001599 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001600 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001601 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001602 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001603 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001604 // | sections | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001605 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001606 // | sections | cancellation | |
1607 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001608 // | sections | cancel | ! |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001609 // +------------------+-----------------+------------------------------------+
1610 // | section | parallel | * |
1611 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001612 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001613 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001614 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001615 // | section | simd | * |
1616 // | section | sections | + |
1617 // | section | section | + |
1618 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001619 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001620 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001621 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001622 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001623 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001624 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001625 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001626 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001627 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001628 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001629 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001630 // | section | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001631 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001632 // | section | cancellation | |
1633 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001634 // | section | cancel | ! |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001635 // +------------------+-----------------+------------------------------------+
1636 // | single | parallel | * |
1637 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001638 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001639 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001640 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001641 // | single | simd | * |
1642 // | single | sections | + |
1643 // | single | section | + |
1644 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001645 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001646 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001647 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001648 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001649 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001650 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001651 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001652 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001653 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001654 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001655 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001656 // | single | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001657 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001658 // | single | cancellation | |
1659 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001660 // | single | cancel | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001661 // +------------------+-----------------+------------------------------------+
1662 // | parallel for | parallel | * |
1663 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001664 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001665 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001666 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001667 // | parallel for | simd | * |
1668 // | parallel for | sections | + |
1669 // | parallel for | section | + |
1670 // | parallel for | single | + |
1671 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001672 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001673 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001674 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001675 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001676 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001677 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001678 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001679 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001680 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001681 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001682 // | parallel for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001683 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001684 // | parallel for | cancellation | |
1685 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001686 // | parallel for | cancel | ! |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001687 // +------------------+-----------------+------------------------------------+
1688 // | parallel sections| parallel | * |
1689 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001690 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001691 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001692 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001693 // | parallel sections| simd | * |
1694 // | parallel sections| sections | + |
1695 // | parallel sections| section | * |
1696 // | parallel sections| single | + |
1697 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001698 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001699 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001700 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001701 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001702 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001703 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001704 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001705 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001706 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001707 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001708 // | parallel sections| target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001709 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001710 // | parallel sections| cancellation | |
1711 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001712 // | parallel sections| cancel | ! |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001713 // +------------------+-----------------+------------------------------------+
1714 // | task | parallel | * |
1715 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001716 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001717 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001718 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001719 // | task | simd | * |
1720 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001721 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001722 // | task | single | + |
1723 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001724 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001725 // | task |parallel sections| * |
1726 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001727 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001728 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001729 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001730 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001731 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001732 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001733 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001734 // | task | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001735 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001736 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00001737 // | | point | ! |
1738 // | task | cancel | ! |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001739 // +------------------+-----------------+------------------------------------+
1740 // | ordered | parallel | * |
1741 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001742 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001743 // | ordered | master | * |
1744 // | ordered | critical | * |
1745 // | ordered | simd | * |
1746 // | ordered | sections | + |
1747 // | ordered | section | + |
1748 // | ordered | single | + |
1749 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001750 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001751 // | ordered |parallel sections| * |
1752 // | ordered | task | * |
1753 // | ordered | taskyield | * |
1754 // | ordered | barrier | + |
1755 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001756 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001757 // | ordered | flush | * |
1758 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001759 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001760 // | ordered | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001761 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001762 // | ordered | cancellation | |
1763 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001764 // | ordered | cancel | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001765 // +------------------+-----------------+------------------------------------+
1766 // | atomic | parallel | |
1767 // | atomic | for | |
1768 // | atomic | for simd | |
1769 // | atomic | master | |
1770 // | atomic | critical | |
1771 // | atomic | simd | |
1772 // | atomic | sections | |
1773 // | atomic | section | |
1774 // | atomic | single | |
1775 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001776 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001777 // | atomic |parallel sections| |
1778 // | atomic | task | |
1779 // | atomic | taskyield | |
1780 // | atomic | barrier | |
1781 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001782 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001783 // | atomic | flush | |
1784 // | atomic | ordered | |
1785 // | atomic | atomic | |
1786 // | atomic | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001787 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001788 // | atomic | cancellation | |
1789 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001790 // | atomic | cancel | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001791 // +------------------+-----------------+------------------------------------+
1792 // | target | parallel | * |
1793 // | target | for | * |
1794 // | target | for simd | * |
1795 // | target | master | * |
1796 // | target | critical | * |
1797 // | target | simd | * |
1798 // | target | sections | * |
1799 // | target | section | * |
1800 // | target | single | * |
1801 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001802 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001803 // | target |parallel sections| * |
1804 // | target | task | * |
1805 // | target | taskyield | * |
1806 // | target | barrier | * |
1807 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001808 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001809 // | target | flush | * |
1810 // | target | ordered | * |
1811 // | target | atomic | * |
1812 // | target | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001813 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001814 // | target | cancellation | |
1815 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001816 // | target | cancel | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001817 // +------------------+-----------------+------------------------------------+
1818 // | teams | parallel | * |
1819 // | teams | for | + |
1820 // | teams | for simd | + |
1821 // | teams | master | + |
1822 // | teams | critical | + |
1823 // | teams | simd | + |
1824 // | teams | sections | + |
1825 // | teams | section | + |
1826 // | teams | single | + |
1827 // | teams | parallel for | * |
1828 // | teams |parallel for simd| * |
1829 // | teams |parallel sections| * |
1830 // | teams | task | + |
1831 // | teams | taskyield | + |
1832 // | teams | barrier | + |
1833 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00001834 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001835 // | teams | flush | + |
1836 // | teams | ordered | + |
1837 // | teams | atomic | + |
1838 // | teams | target | + |
1839 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001840 // | teams | cancellation | |
1841 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001842 // | teams | cancel | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001843 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00001844 if (Stack->getCurScope()) {
1845 auto ParentRegion = Stack->getParentDirective();
1846 bool NestingProhibited = false;
1847 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001848 enum {
1849 NoRecommend,
1850 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00001851 ShouldBeInOrderedRegion,
1852 ShouldBeInTargetRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001853 } Recommend = NoRecommend;
Alexey Bataev549210e2014-06-24 04:39:47 +00001854 if (isOpenMPSimdDirective(ParentRegion)) {
1855 // OpenMP [2.16, Nesting of Regions]
1856 // OpenMP constructs may not be nested inside a simd region.
1857 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
1858 return true;
1859 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001860 if (ParentRegion == OMPD_atomic) {
1861 // OpenMP [2.16, Nesting of Regions]
1862 // OpenMP constructs may not be nested inside an atomic region.
1863 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
1864 return true;
1865 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001866 if (CurrentRegion == OMPD_section) {
1867 // OpenMP [2.7.2, sections Construct, Restrictions]
1868 // Orphaned section directives are prohibited. That is, the section
1869 // directives must appear within the sections construct and must not be
1870 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001871 if (ParentRegion != OMPD_sections &&
1872 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001873 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
1874 << (ParentRegion != OMPD_unknown)
1875 << getOpenMPDirectiveName(ParentRegion);
1876 return true;
1877 }
1878 return false;
1879 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001880 // Allow some constructs to be orphaned (they could be used in functions,
1881 // called from OpenMP regions with the required preconditions).
1882 if (ParentRegion == OMPD_unknown)
1883 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00001884 if (CurrentRegion == OMPD_cancellation_point ||
1885 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001886 // OpenMP [2.16, Nesting of Regions]
1887 // A cancellation point construct for which construct-type-clause is
1888 // taskgroup must be nested inside a task construct. A cancellation
1889 // point construct for which construct-type-clause is not taskgroup must
1890 // be closely nested inside an OpenMP construct that matches the type
1891 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00001892 // A cancel construct for which construct-type-clause is taskgroup must be
1893 // nested inside a task construct. A cancel construct for which
1894 // construct-type-clause is not taskgroup must be closely nested inside an
1895 // OpenMP construct that matches the type specified in
1896 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001897 NestingProhibited =
1898 !((CancelRegion == OMPD_parallel && ParentRegion == OMPD_parallel) ||
1899 (CancelRegion == OMPD_for && ParentRegion == OMPD_for) ||
1900 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
1901 (CancelRegion == OMPD_sections &&
1902 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections)));
1903 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00001904 // OpenMP [2.16, Nesting of Regions]
1905 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00001906 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00001907 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
1908 ParentRegion == OMPD_task;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001909 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
1910 // OpenMP [2.16, Nesting of Regions]
1911 // A critical region may not be nested (closely or otherwise) inside a
1912 // critical region with the same name. Note that this restriction is not
1913 // sufficient to prevent deadlock.
1914 SourceLocation PreviousCriticalLoc;
1915 bool DeadLock =
1916 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
1917 OpenMPDirectiveKind K,
1918 const DeclarationNameInfo &DNI,
1919 SourceLocation Loc)
1920 ->bool {
1921 if (K == OMPD_critical &&
1922 DNI.getName() == CurrentName.getName()) {
1923 PreviousCriticalLoc = Loc;
1924 return true;
1925 } else
1926 return false;
1927 },
1928 false /* skip top directive */);
1929 if (DeadLock) {
1930 SemaRef.Diag(StartLoc,
1931 diag::err_omp_prohibited_region_critical_same_name)
1932 << CurrentName.getName();
1933 if (PreviousCriticalLoc.isValid())
1934 SemaRef.Diag(PreviousCriticalLoc,
1935 diag::note_omp_previous_critical_region);
1936 return true;
1937 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001938 } else if (CurrentRegion == OMPD_barrier) {
1939 // OpenMP [2.16, Nesting of Regions]
1940 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00001941 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001942 NestingProhibited =
1943 isOpenMPWorksharingDirective(ParentRegion) ||
1944 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1945 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00001946 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00001947 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00001948 // OpenMP [2.16, Nesting of Regions]
1949 // A worksharing region may not be closely nested inside a worksharing,
1950 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001951 NestingProhibited =
Alexander Musmanf82886e2014-09-18 05:12:34 +00001952 isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001953 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1954 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
1955 Recommend = ShouldBeInParallelRegion;
1956 } else if (CurrentRegion == OMPD_ordered) {
1957 // OpenMP [2.16, Nesting of Regions]
1958 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00001959 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001960 // An ordered region must be closely nested inside a loop region (or
1961 // parallel loop region) with an ordered clause.
1962 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00001963 ParentRegion == OMPD_task ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001964 !Stack->isParentOrderedRegion();
1965 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00001966 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
1967 // OpenMP [2.16, Nesting of Regions]
1968 // If specified, a teams construct must be contained within a target
1969 // construct.
1970 NestingProhibited = ParentRegion != OMPD_target;
1971 Recommend = ShouldBeInTargetRegion;
1972 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
1973 }
1974 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
1975 // OpenMP [2.16, Nesting of Regions]
1976 // distribute, parallel, parallel sections, parallel workshare, and the
1977 // parallel loop and parallel loop SIMD constructs are the only OpenMP
1978 // constructs that can be closely nested in the teams region.
1979 // TODO: add distribute directive.
1980 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion);
1981 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00001982 }
1983 if (NestingProhibited) {
1984 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001985 << CloseNesting << getOpenMPDirectiveName(ParentRegion) << Recommend
1986 << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00001987 return true;
1988 }
1989 }
1990 return false;
1991}
1992
Alexey Bataev6b8046a2015-09-03 07:23:48 +00001993static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
1994 ArrayRef<OMPClause *> Clauses,
1995 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
1996 bool ErrorFound = false;
1997 unsigned NamedModifiersNumber = 0;
1998 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
1999 OMPD_unknown + 1);
2000 for (const auto *C : Clauses) {
2001 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
2002 // At most one if clause without a directive-name-modifier can appear on
2003 // the directive.
2004 OpenMPDirectiveKind CurNM = IC->getNameModifier();
2005 if (FoundNameModifiers[CurNM]) {
2006 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
2007 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
2008 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
2009 ErrorFound = true;
2010 } else if (CurNM != OMPD_unknown)
2011 ++NamedModifiersNumber;
2012 FoundNameModifiers[CurNM] = IC;
2013 if (CurNM == OMPD_unknown)
2014 continue;
2015 // Check if the specified name modifier is allowed for the current
2016 // directive.
2017 // At most one if clause with the particular directive-name-modifier can
2018 // appear on the directive.
2019 bool MatchFound = false;
2020 for (auto NM : AllowedNameModifiers) {
2021 if (CurNM == NM) {
2022 MatchFound = true;
2023 break;
2024 }
2025 }
2026 if (!MatchFound) {
2027 S.Diag(IC->getNameModifierLoc(),
2028 diag::err_omp_wrong_if_directive_name_modifier)
2029 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
2030 ErrorFound = true;
2031 }
2032 }
2033 }
2034 // If any if clause on the directive includes a directive-name-modifier then
2035 // all if clauses on the directive must include a directive-name-modifier.
2036 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
2037 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
2038 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
2039 diag::err_omp_no_more_if_clause);
2040 } else {
2041 std::string Values;
2042 std::string Sep(", ");
2043 unsigned AllowedCnt = 0;
2044 unsigned TotalAllowedNum =
2045 AllowedNameModifiers.size() - NamedModifiersNumber;
2046 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
2047 ++Cnt) {
2048 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
2049 if (!FoundNameModifiers[NM]) {
2050 Values += "'";
2051 Values += getOpenMPDirectiveName(NM);
2052 Values += "'";
2053 if (AllowedCnt + 2 == TotalAllowedNum)
2054 Values += " or ";
2055 else if (AllowedCnt + 1 != TotalAllowedNum)
2056 Values += Sep;
2057 ++AllowedCnt;
2058 }
2059 }
2060 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
2061 diag::err_omp_unnamed_if_clause)
2062 << (TotalAllowedNum > 1) << Values;
2063 }
2064 ErrorFound = true;
2065 }
2066 return ErrorFound;
2067}
2068
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002069StmtResult Sema::ActOnOpenMPExecutableDirective(
2070 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
2071 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
2072 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002073 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002074 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
2075 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00002076 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002077
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002078 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev68446b72014-07-18 07:47:19 +00002079 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002080 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00002081 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00002082 if (AStmt) {
2083 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
2084
2085 // Check default data sharing attributes for referenced variables.
2086 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
2087 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
2088 if (DSAChecker.isErrorFound())
2089 return StmtError();
2090 // Generate list of implicitly defined firstprivate variables.
2091 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00002092
2093 if (!DSAChecker.getImplicitFirstprivate().empty()) {
2094 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
2095 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
2096 SourceLocation(), SourceLocation())) {
2097 ClausesWithImplicit.push_back(Implicit);
2098 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
2099 DSAChecker.getImplicitFirstprivate().size();
2100 } else
2101 ErrorFound = true;
2102 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002103 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002104
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002105 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002106 switch (Kind) {
2107 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00002108 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
2109 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002110 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002111 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002112 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002113 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2114 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002115 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002116 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002117 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2118 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002119 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00002120 case OMPD_for_simd:
2121 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2122 EndLoc, VarsWithInheritedDSA);
2123 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002124 case OMPD_sections:
2125 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
2126 EndLoc);
2127 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002128 case OMPD_section:
2129 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00002130 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002131 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
2132 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002133 case OMPD_single:
2134 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
2135 EndLoc);
2136 break;
Alexander Musman80c22892014-07-17 08:54:58 +00002137 case OMPD_master:
2138 assert(ClausesWithImplicit.empty() &&
2139 "No clauses are allowed for 'omp master' directive");
2140 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
2141 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002142 case OMPD_critical:
2143 assert(ClausesWithImplicit.empty() &&
2144 "No clauses are allowed for 'omp critical' directive");
2145 Res = ActOnOpenMPCriticalDirective(DirName, AStmt, StartLoc, EndLoc);
2146 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002147 case OMPD_parallel_for:
2148 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
2149 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002150 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00002151 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00002152 case OMPD_parallel_for_simd:
2153 Res = ActOnOpenMPParallelForSimdDirective(
2154 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002155 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00002156 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002157 case OMPD_parallel_sections:
2158 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
2159 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002160 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002161 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002162 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002163 Res =
2164 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002165 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002166 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00002167 case OMPD_taskyield:
2168 assert(ClausesWithImplicit.empty() &&
2169 "No clauses are allowed for 'omp taskyield' directive");
2170 assert(AStmt == nullptr &&
2171 "No associated statement allowed for 'omp taskyield' directive");
2172 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
2173 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002174 case OMPD_barrier:
2175 assert(ClausesWithImplicit.empty() &&
2176 "No clauses are allowed for 'omp barrier' directive");
2177 assert(AStmt == nullptr &&
2178 "No associated statement allowed for 'omp barrier' directive");
2179 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
2180 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00002181 case OMPD_taskwait:
2182 assert(ClausesWithImplicit.empty() &&
2183 "No clauses are allowed for 'omp taskwait' directive");
2184 assert(AStmt == nullptr &&
2185 "No associated statement allowed for 'omp taskwait' directive");
2186 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
2187 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002188 case OMPD_taskgroup:
2189 assert(ClausesWithImplicit.empty() &&
2190 "No clauses are allowed for 'omp taskgroup' directive");
2191 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
2192 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00002193 case OMPD_flush:
2194 assert(AStmt == nullptr &&
2195 "No associated statement allowed for 'omp flush' directive");
2196 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
2197 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002198 case OMPD_ordered:
2199 assert(ClausesWithImplicit.empty() &&
2200 "No clauses are allowed for 'omp ordered' directive");
2201 Res = ActOnOpenMPOrderedDirective(AStmt, StartLoc, EndLoc);
2202 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00002203 case OMPD_atomic:
2204 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
2205 EndLoc);
2206 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002207 case OMPD_teams:
2208 Res =
2209 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
2210 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002211 case OMPD_target:
2212 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
2213 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002214 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002215 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002216 case OMPD_cancellation_point:
2217 assert(ClausesWithImplicit.empty() &&
2218 "No clauses are allowed for 'omp cancellation point' directive");
2219 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
2220 "cancellation point' directive");
2221 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
2222 break;
Alexey Bataev80909872015-07-02 11:25:17 +00002223 case OMPD_cancel:
2224 assert(ClausesWithImplicit.empty() &&
2225 "No clauses are allowed for 'omp cancel' directive");
2226 assert(AStmt == nullptr &&
2227 "No associated statement allowed for 'omp cancel' directive");
2228 Res = ActOnOpenMPCancelDirective(StartLoc, EndLoc, CancelRegion);
2229 break;
Michael Wong65f367f2015-07-21 13:44:28 +00002230 case OMPD_target_data:
2231 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
2232 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002233 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00002234 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002235 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002236 llvm_unreachable("OpenMP Directive is not allowed");
2237 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002238 llvm_unreachable("Unknown OpenMP directive");
2239 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002240
Alexey Bataev4acb8592014-07-07 13:01:15 +00002241 for (auto P : VarsWithInheritedDSA) {
2242 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
2243 << P.first << P.second->getSourceRange();
2244 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002245 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
2246
2247 if (!AllowedNameModifiers.empty())
2248 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
2249 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002250
Alexey Bataeved09d242014-05-28 05:53:51 +00002251 if (ErrorFound)
2252 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002253 return Res;
2254}
2255
2256StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
2257 Stmt *AStmt,
2258 SourceLocation StartLoc,
2259 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002260 if (!AStmt)
2261 return StmtError();
2262
Alexey Bataev9959db52014-05-06 10:08:46 +00002263 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
2264 // 1.2.2 OpenMP Language Terminology
2265 // Structured block - An executable statement with a single entry at the
2266 // top and a single exit at the bottom.
2267 // The point of exit cannot be a branch out of the structured block.
2268 // longjmp() and throw() must not violate the entry/exit criteria.
2269 CS->getCapturedDecl()->setNothrow();
2270
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002271 getCurFunction()->setHasBranchProtectedScope();
2272
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00002273 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
2274 AStmt);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002275}
2276
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002277namespace {
2278/// \brief Helper class for checking canonical form of the OpenMP loops and
2279/// extracting iteration space of each loop in the loop nest, that will be used
2280/// for IR generation.
2281class OpenMPIterationSpaceChecker {
2282 /// \brief Reference to Sema.
2283 Sema &SemaRef;
2284 /// \brief A location for diagnostics (when there is no some better location).
2285 SourceLocation DefaultLoc;
2286 /// \brief A location for diagnostics (when increment is not compatible).
2287 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002288 /// \brief A source location for referring to loop init later.
2289 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002290 /// \brief A source location for referring to condition later.
2291 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002292 /// \brief A source location for referring to increment later.
2293 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002294 /// \brief Loop variable.
2295 VarDecl *Var;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002296 /// \brief Reference to loop variable.
2297 DeclRefExpr *VarRef;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002298 /// \brief Lower bound (initializer for the var).
2299 Expr *LB;
2300 /// \brief Upper bound.
2301 Expr *UB;
2302 /// \brief Loop step (increment).
2303 Expr *Step;
2304 /// \brief This flag is true when condition is one of:
2305 /// Var < UB
2306 /// Var <= UB
2307 /// UB > Var
2308 /// UB >= Var
2309 bool TestIsLessOp;
2310 /// \brief This flag is true when condition is strict ( < or > ).
2311 bool TestIsStrictOp;
2312 /// \brief This flag is true when step is subtracted on each iteration.
2313 bool SubtractStep;
2314
2315public:
2316 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
2317 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
Alexander Musmana5f070a2014-10-01 06:03:56 +00002318 InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()),
2319 IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002320 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
2321 TestIsStrictOp(false), SubtractStep(false) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002322 /// \brief Check init-expr for canonical loop form and save loop counter
2323 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00002324 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002325 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
2326 /// for less/greater and for strict/non-strict comparison.
2327 bool CheckCond(Expr *S);
2328 /// \brief Check incr-expr for canonical loop form and return true if it
2329 /// does not conform, otherwise save loop step (#Step).
2330 bool CheckInc(Expr *S);
2331 /// \brief Return the loop counter variable.
2332 VarDecl *GetLoopVar() const { return Var; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002333 /// \brief Return the reference expression to loop counter variable.
2334 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002335 /// \brief Source range of the loop init.
2336 SourceRange GetInitSrcRange() const { return InitSrcRange; }
2337 /// \brief Source range of the loop condition.
2338 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
2339 /// \brief Source range of the loop increment.
2340 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
2341 /// \brief True if the step should be subtracted.
2342 bool ShouldSubtractStep() const { return SubtractStep; }
2343 /// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00002344 Expr *BuildNumIterations(Scope *S, const bool LimitedType) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00002345 /// \brief Build the precondition expression for the loops.
2346 Expr *BuildPreCond(Scope *S, Expr *Cond) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002347 /// \brief Build reference expression to the counter be used for codegen.
2348 Expr *BuildCounterVar() const;
Alexey Bataeva8899172015-08-06 12:30:57 +00002349 /// \brief Build reference expression to the private counter be used for
2350 /// codegen.
2351 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002352 /// \brief Build initization of the counter be used for codegen.
2353 Expr *BuildCounterInit() const;
2354 /// \brief Build step of the counter be used for codegen.
2355 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002356 /// \brief Return true if any expression is dependent.
2357 bool Dependent() const;
2358
2359private:
2360 /// \brief Check the right-hand side of an assignment in the increment
2361 /// expression.
2362 bool CheckIncRHS(Expr *RHS);
2363 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002364 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002365 /// \brief Helper to set upper bound.
2366 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, const SourceRange &SR,
2367 const SourceLocation &SL);
2368 /// \brief Helper to set loop increment.
2369 bool SetStep(Expr *NewStep, bool Subtract);
2370};
2371
2372bool OpenMPIterationSpaceChecker::Dependent() const {
2373 if (!Var) {
2374 assert(!LB && !UB && !Step);
2375 return false;
2376 }
2377 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
2378 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
2379}
2380
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002381template <typename T>
2382static T *getExprAsWritten(T *E) {
2383 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
2384 E = ExprTemp->getSubExpr();
2385
2386 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
2387 E = MTE->GetTemporaryExpr();
2388
2389 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
2390 E = Binder->getSubExpr();
2391
2392 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
2393 E = ICE->getSubExprAsWritten();
2394 return E->IgnoreParens();
2395}
2396
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002397bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
2398 DeclRefExpr *NewVarRefExpr,
2399 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002400 // State consistency checking to ensure correct usage.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002401 assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
2402 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002403 if (!NewVar || !NewLB)
2404 return true;
2405 Var = NewVar;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002406 VarRef = NewVarRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002407 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
2408 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00002409 if ((Ctor->isCopyOrMoveConstructor() ||
2410 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
2411 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002412 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002413 LB = NewLB;
2414 return false;
2415}
2416
2417bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
2418 const SourceRange &SR,
2419 const SourceLocation &SL) {
2420 // State consistency checking to ensure correct usage.
2421 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
2422 !TestIsLessOp && !TestIsStrictOp);
2423 if (!NewUB)
2424 return true;
2425 UB = NewUB;
2426 TestIsLessOp = LessOp;
2427 TestIsStrictOp = StrictOp;
2428 ConditionSrcRange = SR;
2429 ConditionLoc = SL;
2430 return false;
2431}
2432
2433bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
2434 // State consistency checking to ensure correct usage.
2435 assert(Var != nullptr && LB != nullptr && Step == nullptr);
2436 if (!NewStep)
2437 return true;
2438 if (!NewStep->isValueDependent()) {
2439 // Check that the step is integer expression.
2440 SourceLocation StepLoc = NewStep->getLocStart();
2441 ExprResult Val =
2442 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
2443 if (Val.isInvalid())
2444 return true;
2445 NewStep = Val.get();
2446
2447 // OpenMP [2.6, Canonical Loop Form, Restrictions]
2448 // If test-expr is of form var relational-op b and relational-op is < or
2449 // <= then incr-expr must cause var to increase on each iteration of the
2450 // loop. If test-expr is of form var relational-op b and relational-op is
2451 // > or >= then incr-expr must cause var to decrease on each iteration of
2452 // the loop.
2453 // If test-expr is of form b relational-op var and relational-op is < or
2454 // <= then incr-expr must cause var to decrease on each iteration of the
2455 // loop. If test-expr is of form b relational-op var and relational-op is
2456 // > or >= then incr-expr must cause var to increase on each iteration of
2457 // the loop.
2458 llvm::APSInt Result;
2459 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
2460 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
2461 bool IsConstNeg =
2462 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00002463 bool IsConstPos =
2464 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002465 bool IsConstZero = IsConstant && !Result.getBoolValue();
2466 if (UB && (IsConstZero ||
2467 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00002468 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002469 SemaRef.Diag(NewStep->getExprLoc(),
2470 diag::err_omp_loop_incr_not_compatible)
2471 << Var << TestIsLessOp << NewStep->getSourceRange();
2472 SemaRef.Diag(ConditionLoc,
2473 diag::note_omp_loop_cond_requres_compatible_incr)
2474 << TestIsLessOp << ConditionSrcRange;
2475 return true;
2476 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002477 if (TestIsLessOp == Subtract) {
2478 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
2479 NewStep).get();
2480 Subtract = !Subtract;
2481 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002482 }
2483
2484 Step = NewStep;
2485 SubtractStep = Subtract;
2486 return false;
2487}
2488
Alexey Bataev9c821032015-04-30 04:23:23 +00002489bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002490 // Check init-expr for canonical loop form and save loop counter
2491 // variable - #Var and its initialization value - #LB.
2492 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
2493 // var = lb
2494 // integer-type var = lb
2495 // random-access-iterator-type var = lb
2496 // pointer-type var = lb
2497 //
2498 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00002499 if (EmitDiags) {
2500 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
2501 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002502 return true;
2503 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002504 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002505 if (Expr *E = dyn_cast<Expr>(S))
2506 S = E->IgnoreParens();
2507 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2508 if (BO->getOpcode() == BO_Assign)
2509 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002510 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002511 BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002512 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
2513 if (DS->isSingleDecl()) {
2514 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00002515 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002516 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00002517 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002518 SemaRef.Diag(S->getLocStart(),
2519 diag::ext_omp_loop_not_canonical_init)
2520 << S->getSourceRange();
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002521 return SetVarAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002522 }
2523 }
2524 }
2525 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
2526 if (CE->getOperator() == OO_Equal)
2527 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002528 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
2529 CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002530
Alexey Bataev9c821032015-04-30 04:23:23 +00002531 if (EmitDiags) {
2532 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
2533 << S->getSourceRange();
2534 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002535 return true;
2536}
2537
Alexey Bataev23b69422014-06-18 07:08:49 +00002538/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002539/// variable (which may be the loop variable) if possible.
2540static const VarDecl *GetInitVarDecl(const Expr *E) {
2541 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00002542 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002543 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002544 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
2545 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00002546 if ((Ctor->isCopyOrMoveConstructor() ||
2547 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
2548 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002549 E = CE->getArg(0)->IgnoreParenImpCasts();
2550 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
2551 if (!DRE)
2552 return nullptr;
2553 return dyn_cast<VarDecl>(DRE->getDecl());
2554}
2555
2556bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
2557 // Check test-expr for canonical form, save upper-bound UB, flags for
2558 // less/greater and for strict/non-strict comparison.
2559 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2560 // var relational-op b
2561 // b relational-op var
2562 //
2563 if (!S) {
2564 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
2565 return true;
2566 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002567 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002568 SourceLocation CondLoc = S->getLocStart();
2569 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2570 if (BO->isRelationalOp()) {
2571 if (GetInitVarDecl(BO->getLHS()) == Var)
2572 return SetUB(BO->getRHS(),
2573 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
2574 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2575 BO->getSourceRange(), BO->getOperatorLoc());
2576 if (GetInitVarDecl(BO->getRHS()) == Var)
2577 return SetUB(BO->getLHS(),
2578 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
2579 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2580 BO->getSourceRange(), BO->getOperatorLoc());
2581 }
2582 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2583 if (CE->getNumArgs() == 2) {
2584 auto Op = CE->getOperator();
2585 switch (Op) {
2586 case OO_Greater:
2587 case OO_GreaterEqual:
2588 case OO_Less:
2589 case OO_LessEqual:
2590 if (GetInitVarDecl(CE->getArg(0)) == Var)
2591 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
2592 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2593 CE->getOperatorLoc());
2594 if (GetInitVarDecl(CE->getArg(1)) == Var)
2595 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
2596 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2597 CE->getOperatorLoc());
2598 break;
2599 default:
2600 break;
2601 }
2602 }
2603 }
2604 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
2605 << S->getSourceRange() << Var;
2606 return true;
2607}
2608
2609bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
2610 // RHS of canonical loop form increment can be:
2611 // var + incr
2612 // incr + var
2613 // var - incr
2614 //
2615 RHS = RHS->IgnoreParenImpCasts();
2616 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
2617 if (BO->isAdditiveOp()) {
2618 bool IsAdd = BO->getOpcode() == BO_Add;
2619 if (GetInitVarDecl(BO->getLHS()) == Var)
2620 return SetStep(BO->getRHS(), !IsAdd);
2621 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
2622 return SetStep(BO->getLHS(), false);
2623 }
2624 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
2625 bool IsAdd = CE->getOperator() == OO_Plus;
2626 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
2627 if (GetInitVarDecl(CE->getArg(0)) == Var)
2628 return SetStep(CE->getArg(1), !IsAdd);
2629 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
2630 return SetStep(CE->getArg(0), false);
2631 }
2632 }
2633 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2634 << RHS->getSourceRange() << Var;
2635 return true;
2636}
2637
2638bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
2639 // Check incr-expr for canonical loop form and return true if it
2640 // does not conform.
2641 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2642 // ++var
2643 // var++
2644 // --var
2645 // var--
2646 // var += incr
2647 // var -= incr
2648 // var = var + incr
2649 // var = incr + var
2650 // var = var - incr
2651 //
2652 if (!S) {
2653 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
2654 return true;
2655 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002656 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002657 S = S->IgnoreParens();
2658 if (auto UO = dyn_cast<UnaryOperator>(S)) {
2659 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
2660 return SetStep(
2661 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
2662 (UO->isDecrementOp() ? -1 : 1)).get(),
2663 false);
2664 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
2665 switch (BO->getOpcode()) {
2666 case BO_AddAssign:
2667 case BO_SubAssign:
2668 if (GetInitVarDecl(BO->getLHS()) == Var)
2669 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
2670 break;
2671 case BO_Assign:
2672 if (GetInitVarDecl(BO->getLHS()) == Var)
2673 return CheckIncRHS(BO->getRHS());
2674 break;
2675 default:
2676 break;
2677 }
2678 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2679 switch (CE->getOperator()) {
2680 case OO_PlusPlus:
2681 case OO_MinusMinus:
2682 if (GetInitVarDecl(CE->getArg(0)) == Var)
2683 return SetStep(
2684 SemaRef.ActOnIntegerConstant(
2685 CE->getLocStart(),
2686 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
2687 false);
2688 break;
2689 case OO_PlusEqual:
2690 case OO_MinusEqual:
2691 if (GetInitVarDecl(CE->getArg(0)) == Var)
2692 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
2693 break;
2694 case OO_Equal:
2695 if (GetInitVarDecl(CE->getArg(0)) == Var)
2696 return CheckIncRHS(CE->getArg(1));
2697 break;
2698 default:
2699 break;
2700 }
2701 }
2702 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2703 << S->getSourceRange() << Var;
2704 return true;
2705}
Alexander Musmana5f070a2014-10-01 06:03:56 +00002706
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002707namespace {
2708// Transform variables declared in GNU statement expressions to new ones to
2709// avoid crash on codegen.
2710class TransformToNewDefs : public TreeTransform<TransformToNewDefs> {
2711 typedef TreeTransform<TransformToNewDefs> BaseTransform;
2712
2713public:
2714 TransformToNewDefs(Sema &SemaRef) : BaseTransform(SemaRef) {}
2715
2716 Decl *TransformDefinition(SourceLocation Loc, Decl *D) {
2717 if (auto *VD = cast<VarDecl>(D))
2718 if (!isa<ParmVarDecl>(D) && !isa<VarTemplateSpecializationDecl>(D) &&
2719 !isa<ImplicitParamDecl>(D)) {
2720 auto *NewVD = VarDecl::Create(
2721 SemaRef.Context, VD->getDeclContext(), VD->getLocStart(),
2722 VD->getLocation(), VD->getIdentifier(), VD->getType(),
2723 VD->getTypeSourceInfo(), VD->getStorageClass());
2724 NewVD->setTSCSpec(VD->getTSCSpec());
2725 NewVD->setInit(VD->getInit());
2726 NewVD->setInitStyle(VD->getInitStyle());
2727 NewVD->setExceptionVariable(VD->isExceptionVariable());
2728 NewVD->setNRVOVariable(VD->isNRVOVariable());
2729 NewVD->setCXXForRangeDecl(VD->isInExternCXXContext());
2730 NewVD->setConstexpr(VD->isConstexpr());
2731 NewVD->setInitCapture(VD->isInitCapture());
2732 NewVD->setPreviousDeclInSameBlockScope(
2733 VD->isPreviousDeclInSameBlockScope());
2734 VD->getDeclContext()->addHiddenDecl(NewVD);
2735 transformedLocalDecl(VD, NewVD);
2736 return NewVD;
2737 }
2738 return BaseTransform::TransformDefinition(Loc, D);
2739 }
2740
2741 ExprResult TransformDeclRefExpr(DeclRefExpr *E) {
2742 if (auto *NewD = TransformDecl(E->getExprLoc(), E->getDecl()))
2743 if (E->getDecl() != NewD) {
2744 NewD->setReferenced();
2745 NewD->markUsed(SemaRef.Context);
2746 return DeclRefExpr::Create(
2747 SemaRef.Context, E->getQualifierLoc(), E->getTemplateKeywordLoc(),
2748 cast<ValueDecl>(NewD), E->refersToEnclosingVariableOrCapture(),
2749 E->getNameInfo(), E->getType(), E->getValueKind());
2750 }
2751 return BaseTransform::TransformDeclRefExpr(E);
2752 }
2753};
2754}
2755
Alexander Musmana5f070a2014-10-01 06:03:56 +00002756/// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00002757Expr *
2758OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S,
2759 const bool LimitedType) const {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002760 TransformToNewDefs Transform(SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00002761 ExprResult Diff;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002762 auto VarType = Var->getType().getNonReferenceType();
2763 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00002764 SemaRef.getLangOpts().CPlusPlus) {
2765 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002766 auto *UBExpr = TestIsLessOp ? UB : LB;
2767 auto *LBExpr = TestIsLessOp ? LB : UB;
2768 Expr *Upper = Transform.TransformExpr(UBExpr).get();
2769 Expr *Lower = Transform.TransformExpr(LBExpr).get();
2770 if (!Upper || !Lower)
2771 return nullptr;
2772 Upper = SemaRef.PerformImplicitConversion(Upper, UBExpr->getType(),
2773 Sema::AA_Converting,
2774 /*AllowExplicit=*/true)
2775 .get();
2776 Lower = SemaRef.PerformImplicitConversion(Lower, LBExpr->getType(),
2777 Sema::AA_Converting,
2778 /*AllowExplicit=*/true)
2779 .get();
2780 if (!Upper || !Lower)
2781 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002782
2783 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
2784
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002785 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00002786 // BuildBinOp already emitted error, this one is to point user to upper
2787 // and lower bound, and to tell what is passed to 'operator-'.
2788 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
2789 << Upper->getSourceRange() << Lower->getSourceRange();
2790 return nullptr;
2791 }
2792 }
2793
2794 if (!Diff.isUsable())
2795 return nullptr;
2796
2797 // Upper - Lower [- 1]
2798 if (TestIsStrictOp)
2799 Diff = SemaRef.BuildBinOp(
2800 S, DefaultLoc, BO_Sub, Diff.get(),
2801 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
2802 if (!Diff.isUsable())
2803 return nullptr;
2804
2805 // Upper - Lower [- 1] + Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002806 auto NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
2807 if (NewStep.isInvalid())
2808 return nullptr;
2809 NewStep = SemaRef.PerformImplicitConversion(
2810 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
2811 /*AllowExplicit=*/true);
2812 if (NewStep.isInvalid())
2813 return nullptr;
2814 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00002815 if (!Diff.isUsable())
2816 return nullptr;
2817
2818 // Parentheses (for dumping/debugging purposes only).
2819 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
2820 if (!Diff.isUsable())
2821 return nullptr;
2822
2823 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002824 NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
2825 if (NewStep.isInvalid())
2826 return nullptr;
2827 NewStep = SemaRef.PerformImplicitConversion(
2828 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
2829 /*AllowExplicit=*/true);
2830 if (NewStep.isInvalid())
2831 return nullptr;
2832 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00002833 if (!Diff.isUsable())
2834 return nullptr;
2835
Alexander Musman174b3ca2014-10-06 11:16:29 +00002836 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002837 QualType Type = Diff.get()->getType();
2838 auto &C = SemaRef.Context;
2839 bool UseVarType = VarType->hasIntegerRepresentation() &&
2840 C.getTypeSize(Type) > C.getTypeSize(VarType);
2841 if (!Type->isIntegerType() || UseVarType) {
2842 unsigned NewSize =
2843 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
2844 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
2845 : Type->hasSignedIntegerRepresentation();
2846 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
2847 Diff = SemaRef.PerformImplicitConversion(
2848 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
2849 if (!Diff.isUsable())
2850 return nullptr;
2851 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00002852 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00002853 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
2854 if (NewSize != C.getTypeSize(Type)) {
2855 if (NewSize < C.getTypeSize(Type)) {
2856 assert(NewSize == 64 && "incorrect loop var size");
2857 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
2858 << InitSrcRange << ConditionSrcRange;
2859 }
2860 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002861 NewSize, Type->hasSignedIntegerRepresentation() ||
2862 C.getTypeSize(Type) < NewSize);
Alexander Musman174b3ca2014-10-06 11:16:29 +00002863 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
2864 Sema::AA_Converting, true);
2865 if (!Diff.isUsable())
2866 return nullptr;
2867 }
2868 }
2869
Alexander Musmana5f070a2014-10-01 06:03:56 +00002870 return Diff.get();
2871}
2872
Alexey Bataev62dbb972015-04-22 11:59:37 +00002873Expr *OpenMPIterationSpaceChecker::BuildPreCond(Scope *S, Expr *Cond) const {
2874 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
2875 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
2876 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002877 TransformToNewDefs Transform(SemaRef);
2878
2879 auto NewLB = Transform.TransformExpr(LB);
2880 auto NewUB = Transform.TransformExpr(UB);
2881 if (NewLB.isInvalid() || NewUB.isInvalid())
2882 return Cond;
2883 NewLB = SemaRef.PerformImplicitConversion(NewLB.get(), LB->getType(),
2884 Sema::AA_Converting,
2885 /*AllowExplicit=*/true);
2886 NewUB = SemaRef.PerformImplicitConversion(NewUB.get(), UB->getType(),
2887 Sema::AA_Converting,
2888 /*AllowExplicit=*/true);
2889 if (NewLB.isInvalid() || NewUB.isInvalid())
2890 return Cond;
Alexey Bataev62dbb972015-04-22 11:59:37 +00002891 auto CondExpr = SemaRef.BuildBinOp(
2892 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
2893 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002894 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002895 if (CondExpr.isUsable()) {
2896 CondExpr = SemaRef.PerformImplicitConversion(
2897 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
2898 /*AllowExplicit=*/true);
2899 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00002900 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
2901 // Otherwise use original loop conditon and evaluate it in runtime.
2902 return CondExpr.isUsable() ? CondExpr.get() : Cond;
2903}
2904
Alexander Musmana5f070a2014-10-01 06:03:56 +00002905/// \brief Build reference expression to the counter be used for codegen.
2906Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const {
Alexey Bataeva8899172015-08-06 12:30:57 +00002907 return buildDeclRefExpr(SemaRef, Var, Var->getType().getNonReferenceType(),
2908 DefaultLoc);
2909}
2910
2911Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
2912 if (Var && !Var->isInvalidDecl()) {
2913 auto Type = Var->getType().getNonReferenceType();
2914 auto *PrivateVar = buildVarDecl(SemaRef, DefaultLoc, Type, Var->getName());
2915 if (PrivateVar->isInvalidDecl())
2916 return nullptr;
2917 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
2918 }
2919 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002920}
2921
2922/// \brief Build initization of the counter be used for codegen.
2923Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
2924
2925/// \brief Build step of the counter be used for codegen.
2926Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
2927
2928/// \brief Iteration space of a single for loop.
2929struct LoopIterationSpace {
Alexey Bataev62dbb972015-04-22 11:59:37 +00002930 /// \brief Condition of the loop.
2931 Expr *PreCond;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002932 /// \brief This expression calculates the number of iterations in the loop.
2933 /// It is always possible to calculate it before starting the loop.
2934 Expr *NumIterations;
2935 /// \brief The loop counter variable.
2936 Expr *CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00002937 /// \brief Private loop counter variable.
2938 Expr *PrivateCounterVar;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002939 /// \brief This is initializer for the initial value of #CounterVar.
2940 Expr *CounterInit;
2941 /// \brief This is step for the #CounterVar used to generate its update:
2942 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
2943 Expr *CounterStep;
2944 /// \brief Should step be subtracted?
2945 bool Subtract;
2946 /// \brief Source range of the loop init.
2947 SourceRange InitSrcRange;
2948 /// \brief Source range of the loop condition.
2949 SourceRange CondSrcRange;
2950 /// \brief Source range of the loop increment.
2951 SourceRange IncSrcRange;
2952};
2953
Alexey Bataev23b69422014-06-18 07:08:49 +00002954} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002955
Alexey Bataev9c821032015-04-30 04:23:23 +00002956void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
2957 assert(getLangOpts().OpenMP && "OpenMP is not active.");
2958 assert(Init && "Expected loop in canonical form.");
2959 unsigned CollapseIteration = DSAStack->getCollapseNumber();
2960 if (CollapseIteration > 0 &&
2961 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
2962 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
2963 if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
2964 DSAStack->addLoopControlVariable(ISC.GetLoopVar());
2965 }
2966 DSAStack->setCollapseNumber(CollapseIteration - 1);
2967 }
2968}
2969
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002970/// \brief Called on a for stmt to check and extract its iteration space
2971/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00002972static bool CheckOpenMPIterationSpace(
2973 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
2974 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00002975 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002976 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
2977 LoopIterationSpace &ResultIterSpace) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002978 // OpenMP [2.6, Canonical Loop Form]
2979 // for (init-expr; test-expr; incr-expr) structured-block
2980 auto For = dyn_cast_or_null<ForStmt>(S);
2981 if (!For) {
2982 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00002983 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
2984 << getOpenMPDirectiveName(DKind) << NestedLoopCount
2985 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
2986 if (NestedLoopCount > 1) {
2987 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
2988 SemaRef.Diag(DSA.getConstructLoc(),
2989 diag::note_omp_collapse_ordered_expr)
2990 << 2 << CollapseLoopCountExpr->getSourceRange()
2991 << OrderedLoopCountExpr->getSourceRange();
2992 else if (CollapseLoopCountExpr)
2993 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
2994 diag::note_omp_collapse_ordered_expr)
2995 << 0 << CollapseLoopCountExpr->getSourceRange();
2996 else
2997 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
2998 diag::note_omp_collapse_ordered_expr)
2999 << 1 << OrderedLoopCountExpr->getSourceRange();
3000 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003001 return true;
3002 }
3003 assert(For->getBody());
3004
3005 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
3006
3007 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003008 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003009 if (ISC.CheckInit(Init)) {
3010 return true;
3011 }
3012
3013 bool HasErrors = false;
3014
3015 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003016 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003017
3018 // OpenMP [2.6, Canonical Loop Form]
3019 // Var is one of the following:
3020 // A variable of signed or unsigned integer type.
3021 // For C++, a variable of a random access iterator type.
3022 // For C, a variable of a pointer type.
Alexey Bataeva8899172015-08-06 12:30:57 +00003023 auto VarType = Var->getType().getNonReferenceType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003024 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
3025 !VarType->isPointerType() &&
3026 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
3027 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
3028 << SemaRef.getLangOpts().CPlusPlus;
3029 HasErrors = true;
3030 }
3031
Alexey Bataev4acb8592014-07-07 13:01:15 +00003032 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
3033 // Construct
3034 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3035 // parallel for construct is (are) private.
3036 // The loop iteration variable in the associated for-loop of a simd construct
3037 // with just one associated for-loop is linear with a constant-linear-step
3038 // that is the increment of the associated for-loop.
3039 // Exclude loop var from the list of variables with implicitly defined data
3040 // sharing attributes.
Benjamin Kramerad8e0792014-10-10 15:32:48 +00003041 VarsWithImplicitDSA.erase(Var);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003042
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003043 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
3044 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00003045 // The loop iteration variable in the associated for-loop of a simd construct
3046 // with just one associated for-loop may be listed in a linear clause with a
3047 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00003048 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3049 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003050 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003051 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
3052 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
3053 // declared in the loop and it is predetermined as a private.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003054 auto PredeterminedCKind =
3055 isOpenMPSimdDirective(DKind)
3056 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
3057 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003058 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataev0c024df2015-05-12 09:02:07 +00003059 DVar.CKind != OMPC_threadprivate && DVar.CKind != PredeterminedCKind) ||
Alexander Musmanf82886e2014-09-18 05:12:34 +00003060 (isOpenMPWorksharingDirective(DKind) && !isOpenMPSimdDirective(DKind) &&
3061 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private &&
Alexey Bataev0c024df2015-05-12 09:02:07 +00003062 DVar.CKind != OMPC_lastprivate && DVar.CKind != OMPC_threadprivate)) &&
3063 ((DVar.CKind != OMPC_private && DVar.CKind != OMPC_threadprivate) ||
3064 DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003065 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00003066 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
3067 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataevf2453a02015-05-06 07:25:08 +00003068 if (DVar.RefExpr == nullptr)
3069 DVar.CKind = PredeterminedCKind;
3070 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, /*IsLoopIterVar=*/true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003071 HasErrors = true;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003072 } else if (LoopVarRefExpr != nullptr) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00003073 // Make the loop iteration variable private (for worksharing constructs),
3074 // linear (for simd directives with the only one associated loop) or
Alexey Bataev10e775f2015-07-30 11:36:16 +00003075 // lastprivate (for simd directives with several collapsed or ordered
3076 // loops).
Alexey Bataev9aba41c2014-11-14 04:08:45 +00003077 if (DVar.CKind == OMPC_unknown)
3078 DVar = DSA.hasDSA(Var, isOpenMPPrivate, MatchesAlways(),
3079 /*FromParent=*/false);
Alexey Bataev9c821032015-04-30 04:23:23 +00003080 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003081 }
3082
Alexey Bataev7ff55242014-06-19 09:13:45 +00003083 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00003084
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003085 // Check test-expr.
3086 HasErrors |= ISC.CheckCond(For->getCond());
3087
3088 // Check incr-expr.
3089 HasErrors |= ISC.CheckInc(For->getInc());
3090
Alexander Musmana5f070a2014-10-01 06:03:56 +00003091 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003092 return HasErrors;
3093
Alexander Musmana5f070a2014-10-01 06:03:56 +00003094 // Build the loop's iteration space representation.
Alexey Bataev62dbb972015-04-22 11:59:37 +00003095 ResultIterSpace.PreCond = ISC.BuildPreCond(DSA.getCurScope(), For->getCond());
Alexander Musman174b3ca2014-10-06 11:16:29 +00003096 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
3097 DSA.getCurScope(), /* LimitedType */ isOpenMPWorksharingDirective(DKind));
Alexander Musmana5f070a2014-10-01 06:03:56 +00003098 ResultIterSpace.CounterVar = ISC.BuildCounterVar();
Alexey Bataeva8899172015-08-06 12:30:57 +00003099 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003100 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
3101 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
3102 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
3103 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
3104 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
3105 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
3106
Alexey Bataev62dbb972015-04-22 11:59:37 +00003107 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
3108 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003109 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00003110 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003111 ResultIterSpace.CounterInit == nullptr ||
3112 ResultIterSpace.CounterStep == nullptr);
3113
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003114 return HasErrors;
3115}
3116
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003117/// \brief Build 'VarRef = Start.
3118static ExprResult BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc,
3119 ExprResult VarRef, ExprResult Start) {
3120 TransformToNewDefs Transform(SemaRef);
3121 // Build 'VarRef = Start.
3122 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
3123 if (NewStart.isInvalid())
3124 return ExprError();
3125 NewStart = SemaRef.PerformImplicitConversion(
3126 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
3127 Sema::AA_Converting,
3128 /*AllowExplicit=*/true);
3129 if (NewStart.isInvalid())
3130 return ExprError();
3131 NewStart = SemaRef.PerformImplicitConversion(
3132 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
3133 /*AllowExplicit=*/true);
3134 if (!NewStart.isUsable())
3135 return ExprError();
3136
3137 auto Init =
3138 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
3139 return Init;
3140}
3141
Alexander Musmana5f070a2014-10-01 06:03:56 +00003142/// \brief Build 'VarRef = Start + Iter * Step'.
3143static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S,
3144 SourceLocation Loc, ExprResult VarRef,
3145 ExprResult Start, ExprResult Iter,
3146 ExprResult Step, bool Subtract) {
3147 // Add parentheses (for debugging purposes only).
3148 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
3149 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
3150 !Step.isUsable())
3151 return ExprError();
3152
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003153 TransformToNewDefs Transform(SemaRef);
3154 auto NewStep = Transform.TransformExpr(Step.get()->IgnoreImplicit());
3155 if (NewStep.isInvalid())
3156 return ExprError();
3157 NewStep = SemaRef.PerformImplicitConversion(
3158 NewStep.get(), Step.get()->IgnoreImplicit()->getType(),
3159 Sema::AA_Converting,
3160 /*AllowExplicit=*/true);
3161 if (NewStep.isInvalid())
3162 return ExprError();
3163 ExprResult Update =
3164 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003165 if (!Update.isUsable())
3166 return ExprError();
3167
3168 // Build 'VarRef = Start + Iter * Step'.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003169 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
3170 if (NewStart.isInvalid())
3171 return ExprError();
3172 NewStart = SemaRef.PerformImplicitConversion(
3173 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
3174 Sema::AA_Converting,
3175 /*AllowExplicit=*/true);
3176 if (NewStart.isInvalid())
3177 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003178 Update = SemaRef.BuildBinOp(S, Loc, (Subtract ? BO_Sub : BO_Add),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003179 NewStart.get(), Update.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003180 if (!Update.isUsable())
3181 return ExprError();
3182
3183 Update = SemaRef.PerformImplicitConversion(
3184 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
3185 if (!Update.isUsable())
3186 return ExprError();
3187
3188 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
3189 return Update;
3190}
3191
3192/// \brief Convert integer expression \a E to make it have at least \a Bits
3193/// bits.
3194static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
3195 Sema &SemaRef) {
3196 if (E == nullptr)
3197 return ExprError();
3198 auto &C = SemaRef.Context;
3199 QualType OldType = E->getType();
3200 unsigned HasBits = C.getTypeSize(OldType);
3201 if (HasBits >= Bits)
3202 return ExprResult(E);
3203 // OK to convert to signed, because new type has more bits than old.
3204 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
3205 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
3206 true);
3207}
3208
3209/// \brief Check if the given expression \a E is a constant integer that fits
3210/// into \a Bits bits.
3211static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
3212 if (E == nullptr)
3213 return false;
3214 llvm::APSInt Result;
3215 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
3216 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
3217 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003218}
3219
3220/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00003221/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
3222/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003223static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00003224CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
3225 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
3226 DSAStackTy &DSA,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003227 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00003228 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003229 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003230 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003231 // Found 'collapse' clause - calculate collapse number.
3232 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003233 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
3234 NestedLoopCount += Result.getLimitedValue() - 1;
3235 }
3236 if (OrderedLoopCountExpr) {
3237 // Found 'ordered' clause - calculate collapse number.
3238 llvm::APSInt Result;
3239 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
3240 NestedLoopCount += Result.getLimitedValue() - 1;
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003241 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003242 // This is helper routine for loop directives (e.g., 'for', 'simd',
3243 // 'for simd', etc.).
Alexander Musmana5f070a2014-10-01 06:03:56 +00003244 SmallVector<LoopIterationSpace, 4> IterSpaces;
3245 IterSpaces.resize(NestedLoopCount);
3246 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003247 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003248 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003249 NestedLoopCount, CollapseLoopCountExpr,
3250 OrderedLoopCountExpr, VarsWithImplicitDSA,
3251 IterSpaces[Cnt]))
Alexey Bataevabfc0692014-06-25 06:52:00 +00003252 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003253 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003254 // OpenMP [2.8.1, simd construct, Restrictions]
3255 // All loops associated with the construct must be perfectly nested; that
3256 // is, there must be no intervening code nor any OpenMP directive between
3257 // any two loops.
3258 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003259 }
3260
Alexander Musmana5f070a2014-10-01 06:03:56 +00003261 Built.clear(/* size */ NestedLoopCount);
3262
3263 if (SemaRef.CurContext->isDependentContext())
3264 return NestedLoopCount;
3265
3266 // An example of what is generated for the following code:
3267 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00003268 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00003269 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003270 // for (k = 0; k < NK; ++k)
3271 // for (j = J0; j < NJ; j+=2) {
3272 // <loop body>
3273 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003274 //
3275 // We generate the code below.
3276 // Note: the loop body may be outlined in CodeGen.
3277 // Note: some counters may be C++ classes, operator- is used to find number of
3278 // iterations and operator+= to calculate counter value.
3279 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
3280 // or i64 is currently supported).
3281 //
3282 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
3283 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
3284 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
3285 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
3286 // // similar updates for vars in clauses (e.g. 'linear')
3287 // <loop body (using local i and j)>
3288 // }
3289 // i = NI; // assign final values of counters
3290 // j = NJ;
3291 //
3292
3293 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
3294 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00003295 // Precondition tests if there is at least one iteration (all conditions are
3296 // true).
3297 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003298 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003299 ExprResult LastIteration32 = WidenIterationCount(
3300 32 /* Bits */, SemaRef.PerformImplicitConversion(
3301 N0->IgnoreImpCasts(), N0->getType(),
3302 Sema::AA_Converting, /*AllowExplicit=*/true)
3303 .get(),
3304 SemaRef);
3305 ExprResult LastIteration64 = WidenIterationCount(
3306 64 /* Bits */, SemaRef.PerformImplicitConversion(
3307 N0->IgnoreImpCasts(), N0->getType(),
3308 Sema::AA_Converting, /*AllowExplicit=*/true)
3309 .get(),
3310 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003311
3312 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
3313 return NestedLoopCount;
3314
3315 auto &C = SemaRef.Context;
3316 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
3317
3318 Scope *CurScope = DSA.getCurScope();
3319 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003320 if (PreCond.isUsable()) {
3321 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
3322 PreCond.get(), IterSpaces[Cnt].PreCond);
3323 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003324 auto N = IterSpaces[Cnt].NumIterations;
3325 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
3326 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003327 LastIteration32 = SemaRef.BuildBinOp(
3328 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
3329 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
3330 Sema::AA_Converting,
3331 /*AllowExplicit=*/true)
3332 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003333 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003334 LastIteration64 = SemaRef.BuildBinOp(
3335 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
3336 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
3337 Sema::AA_Converting,
3338 /*AllowExplicit=*/true)
3339 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003340 }
3341
3342 // Choose either the 32-bit or 64-bit version.
3343 ExprResult LastIteration = LastIteration64;
3344 if (LastIteration32.isUsable() &&
3345 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
3346 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
3347 FitsInto(
3348 32 /* Bits */,
3349 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
3350 LastIteration64.get(), SemaRef)))
3351 LastIteration = LastIteration32;
3352
3353 if (!LastIteration.isUsable())
3354 return 0;
3355
3356 // Save the number of iterations.
3357 ExprResult NumIterations = LastIteration;
3358 {
3359 LastIteration = SemaRef.BuildBinOp(
3360 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
3361 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3362 if (!LastIteration.isUsable())
3363 return 0;
3364 }
3365
3366 // Calculate the last iteration number beforehand instead of doing this on
3367 // each iteration. Do not do this if the number of iterations may be kfold-ed.
3368 llvm::APSInt Result;
3369 bool IsConstant =
3370 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
3371 ExprResult CalcLastIteration;
3372 if (!IsConstant) {
3373 SourceLocation SaveLoc;
3374 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00003375 buildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00003376 ".omp.last.iteration");
Alexey Bataev39f915b82015-05-08 10:41:21 +00003377 ExprResult SaveRef = buildDeclRefExpr(
3378 SemaRef, SaveVar, LastIteration.get()->getType(), SaveLoc);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003379 CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign,
3380 SaveRef.get(), LastIteration.get());
3381 LastIteration = SaveRef;
3382
3383 // Prepare SaveRef + 1.
3384 NumIterations = SemaRef.BuildBinOp(
3385 CurScope, SaveLoc, BO_Add, SaveRef.get(),
3386 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3387 if (!NumIterations.isUsable())
3388 return 0;
3389 }
3390
3391 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
3392
Alexander Musmanc6388682014-12-15 07:07:06 +00003393 QualType VType = LastIteration.get()->getType();
3394 // Build variables passed into runtime, nesessary for worksharing directives.
3395 ExprResult LB, UB, IL, ST, EUB;
3396 if (isOpenMPWorksharingDirective(DKind)) {
3397 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00003398 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
3399 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003400 SemaRef.AddInitializerToDecl(
3401 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
3402 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3403
3404 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00003405 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
3406 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003407 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
3408 /*DirectInit*/ false,
3409 /*TypeMayContainAuto*/ false);
3410
3411 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
3412 // This will be used to implement clause 'lastprivate'.
3413 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00003414 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
3415 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003416 SemaRef.AddInitializerToDecl(
3417 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
3418 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3419
3420 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev39f915b82015-05-08 10:41:21 +00003421 VarDecl *STDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.stride");
3422 ST = buildDeclRefExpr(SemaRef, STDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003423 SemaRef.AddInitializerToDecl(
3424 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
3425 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3426
3427 // Build expression: UB = min(UB, LastIteration)
3428 // It is nesessary for CodeGen of directives with static scheduling.
3429 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
3430 UB.get(), LastIteration.get());
3431 ExprResult CondOp = SemaRef.ActOnConditionalOp(
3432 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
3433 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
3434 CondOp.get());
3435 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
3436 }
3437
3438 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003439 ExprResult IV;
3440 ExprResult Init;
3441 {
Alexey Bataev39f915b82015-05-08 10:41:21 +00003442 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.iv");
3443 IV = buildDeclRefExpr(SemaRef, IVDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003444 Expr *RHS = isOpenMPWorksharingDirective(DKind)
3445 ? LB.get()
3446 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
3447 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
3448 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003449 }
3450
Alexander Musmanc6388682014-12-15 07:07:06 +00003451 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003452 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00003453 ExprResult Cond =
3454 isOpenMPWorksharingDirective(DKind)
3455 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
3456 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
3457 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003458
3459 // Loop increment (IV = IV + 1)
3460 SourceLocation IncLoc;
3461 ExprResult Inc =
3462 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
3463 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
3464 if (!Inc.isUsable())
3465 return 0;
3466 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00003467 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
3468 if (!Inc.isUsable())
3469 return 0;
3470
3471 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
3472 // Used for directives with static scheduling.
3473 ExprResult NextLB, NextUB;
3474 if (isOpenMPWorksharingDirective(DKind)) {
3475 // LB + ST
3476 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
3477 if (!NextLB.isUsable())
3478 return 0;
3479 // LB = LB + ST
3480 NextLB =
3481 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
3482 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
3483 if (!NextLB.isUsable())
3484 return 0;
3485 // UB + ST
3486 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
3487 if (!NextUB.isUsable())
3488 return 0;
3489 // UB = UB + ST
3490 NextUB =
3491 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
3492 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
3493 if (!NextUB.isUsable())
3494 return 0;
3495 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003496
3497 // Build updates and final values of the loop counters.
3498 bool HasErrors = false;
3499 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003500 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003501 Built.Updates.resize(NestedLoopCount);
3502 Built.Finals.resize(NestedLoopCount);
3503 {
3504 ExprResult Div;
3505 // Go from inner nested loop to outer.
3506 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
3507 LoopIterationSpace &IS = IterSpaces[Cnt];
3508 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
3509 // Build: Iter = (IV / Div) % IS.NumIters
3510 // where Div is product of previous iterations' IS.NumIters.
3511 ExprResult Iter;
3512 if (Div.isUsable()) {
3513 Iter =
3514 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
3515 } else {
3516 Iter = IV;
3517 assert((Cnt == (int)NestedLoopCount - 1) &&
3518 "unusable div expected on first iteration only");
3519 }
3520
3521 if (Cnt != 0 && Iter.isUsable())
3522 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
3523 IS.NumIterations);
3524 if (!Iter.isUsable()) {
3525 HasErrors = true;
3526 break;
3527 }
3528
Alexey Bataev39f915b82015-05-08 10:41:21 +00003529 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
3530 auto *CounterVar = buildDeclRefExpr(
3531 SemaRef, cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()),
3532 IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
3533 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003534 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
3535 IS.CounterInit);
3536 if (!Init.isUsable()) {
3537 HasErrors = true;
3538 break;
3539 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003540 ExprResult Update =
Alexey Bataev39f915b82015-05-08 10:41:21 +00003541 BuildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003542 IS.CounterInit, Iter, IS.CounterStep, IS.Subtract);
3543 if (!Update.isUsable()) {
3544 HasErrors = true;
3545 break;
3546 }
3547
3548 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
3549 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00003550 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003551 IS.NumIterations, IS.CounterStep, IS.Subtract);
3552 if (!Final.isUsable()) {
3553 HasErrors = true;
3554 break;
3555 }
3556
3557 // Build Div for the next iteration: Div <- Div * IS.NumIters
3558 if (Cnt != 0) {
3559 if (Div.isUnset())
3560 Div = IS.NumIterations;
3561 else
3562 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
3563 IS.NumIterations);
3564
3565 // Add parentheses (for debugging purposes only).
3566 if (Div.isUsable())
3567 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
3568 if (!Div.isUsable()) {
3569 HasErrors = true;
3570 break;
3571 }
3572 }
3573 if (!Update.isUsable() || !Final.isUsable()) {
3574 HasErrors = true;
3575 break;
3576 }
3577 // Save results
3578 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00003579 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003580 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003581 Built.Updates[Cnt] = Update.get();
3582 Built.Finals[Cnt] = Final.get();
3583 }
3584 }
3585
3586 if (HasErrors)
3587 return 0;
3588
3589 // Save results
3590 Built.IterationVarRef = IV.get();
3591 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00003592 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003593 Built.CalcLastIteration =
3594 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003595 Built.PreCond = PreCond.get();
3596 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003597 Built.Init = Init.get();
3598 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00003599 Built.LB = LB.get();
3600 Built.UB = UB.get();
3601 Built.IL = IL.get();
3602 Built.ST = ST.get();
3603 Built.EUB = EUB.get();
3604 Built.NLB = NextLB.get();
3605 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003606
Alexey Bataevabfc0692014-06-25 06:52:00 +00003607 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003608}
3609
Alexey Bataev10e775f2015-07-30 11:36:16 +00003610static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00003611 auto CollapseClauses =
3612 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
3613 if (CollapseClauses.begin() != CollapseClauses.end())
3614 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003615 return nullptr;
3616}
3617
Alexey Bataev10e775f2015-07-30 11:36:16 +00003618static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00003619 auto OrderedClauses =
3620 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
3621 if (OrderedClauses.begin() != OrderedClauses.end())
3622 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00003623 return nullptr;
3624}
3625
Alexey Bataev66b15b52015-08-21 11:14:16 +00003626static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen,
3627 const Expr *Safelen) {
3628 llvm::APSInt SimdlenRes, SafelenRes;
3629 if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() ||
3630 Simdlen->isInstantiationDependent() ||
3631 Simdlen->containsUnexpandedParameterPack())
3632 return false;
3633 if (Safelen->isValueDependent() || Safelen->isTypeDependent() ||
3634 Safelen->isInstantiationDependent() ||
3635 Safelen->containsUnexpandedParameterPack())
3636 return false;
3637 Simdlen->EvaluateAsInt(SimdlenRes, S.Context);
3638 Safelen->EvaluateAsInt(SafelenRes, S.Context);
3639 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
3640 // If both simdlen and safelen clauses are specified, the value of the simdlen
3641 // parameter must be less than or equal to the value of the safelen parameter.
3642 if (SimdlenRes > SafelenRes) {
3643 S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values)
3644 << Simdlen->getSourceRange() << Safelen->getSourceRange();
3645 return true;
3646 }
3647 return false;
3648}
3649
Alexey Bataev4acb8592014-07-07 13:01:15 +00003650StmtResult Sema::ActOnOpenMPSimdDirective(
3651 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3652 SourceLocation EndLoc,
3653 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003654 if (!AStmt)
3655 return StmtError();
3656
3657 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00003658 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003659 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
3660 // define the nested loops number.
3661 unsigned NestedLoopCount = CheckOpenMPLoop(
3662 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
3663 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00003664 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003665 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003666
Alexander Musmana5f070a2014-10-01 06:03:56 +00003667 assert((CurContext->isDependentContext() || B.builtAll()) &&
3668 "omp simd loop exprs were not built");
3669
Alexander Musman3276a272015-03-21 10:12:56 +00003670 if (!CurContext->isDependentContext()) {
3671 // Finalize the clauses that need pre-built expressions for CodeGen.
3672 for (auto C : Clauses) {
3673 if (auto LC = dyn_cast<OMPLinearClause>(C))
3674 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3675 B.NumIterations, *this, CurScope))
3676 return StmtError();
3677 }
3678 }
3679
Alexey Bataev66b15b52015-08-21 11:14:16 +00003680 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
3681 // If both simdlen and safelen clauses are specified, the value of the simdlen
3682 // parameter must be less than or equal to the value of the safelen parameter.
3683 OMPSafelenClause *Safelen = nullptr;
3684 OMPSimdlenClause *Simdlen = nullptr;
3685 for (auto *Clause : Clauses) {
3686 if (Clause->getClauseKind() == OMPC_safelen)
3687 Safelen = cast<OMPSafelenClause>(Clause);
3688 else if (Clause->getClauseKind() == OMPC_simdlen)
3689 Simdlen = cast<OMPSimdlenClause>(Clause);
3690 if (Safelen && Simdlen)
3691 break;
3692 }
3693 if (Simdlen && Safelen &&
3694 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
3695 Safelen->getSafelen()))
3696 return StmtError();
3697
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003698 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003699 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
3700 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003701}
3702
Alexey Bataev4acb8592014-07-07 13:01:15 +00003703StmtResult Sema::ActOnOpenMPForDirective(
3704 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3705 SourceLocation EndLoc,
3706 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003707 if (!AStmt)
3708 return StmtError();
3709
3710 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00003711 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003712 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
3713 // define the nested loops number.
3714 unsigned NestedLoopCount = CheckOpenMPLoop(
3715 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
3716 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00003717 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00003718 return StmtError();
3719
Alexander Musmana5f070a2014-10-01 06:03:56 +00003720 assert((CurContext->isDependentContext() || B.builtAll()) &&
3721 "omp for loop exprs were not built");
3722
Alexey Bataev54acd402015-08-04 11:18:19 +00003723 if (!CurContext->isDependentContext()) {
3724 // Finalize the clauses that need pre-built expressions for CodeGen.
3725 for (auto C : Clauses) {
3726 if (auto LC = dyn_cast<OMPLinearClause>(C))
3727 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3728 B.NumIterations, *this, CurScope))
3729 return StmtError();
3730 }
3731 }
3732
Alexey Bataevf29276e2014-06-18 04:14:57 +00003733 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003734 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
3735 Clauses, AStmt, B);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003736}
3737
Alexander Musmanf82886e2014-09-18 05:12:34 +00003738StmtResult Sema::ActOnOpenMPForSimdDirective(
3739 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3740 SourceLocation EndLoc,
3741 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003742 if (!AStmt)
3743 return StmtError();
3744
3745 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00003746 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003747 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
3748 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00003749 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00003750 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
3751 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
3752 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00003753 if (NestedLoopCount == 0)
3754 return StmtError();
3755
Alexander Musmanc6388682014-12-15 07:07:06 +00003756 assert((CurContext->isDependentContext() || B.builtAll()) &&
3757 "omp for simd loop exprs were not built");
3758
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00003759 if (!CurContext->isDependentContext()) {
3760 // Finalize the clauses that need pre-built expressions for CodeGen.
3761 for (auto C : Clauses) {
3762 if (auto LC = dyn_cast<OMPLinearClause>(C))
3763 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3764 B.NumIterations, *this, CurScope))
3765 return StmtError();
3766 }
3767 }
3768
Alexey Bataev66b15b52015-08-21 11:14:16 +00003769 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
3770 // If both simdlen and safelen clauses are specified, the value of the simdlen
3771 // parameter must be less than or equal to the value of the safelen parameter.
3772 OMPSafelenClause *Safelen = nullptr;
3773 OMPSimdlenClause *Simdlen = nullptr;
3774 for (auto *Clause : Clauses) {
3775 if (Clause->getClauseKind() == OMPC_safelen)
3776 Safelen = cast<OMPSafelenClause>(Clause);
3777 else if (Clause->getClauseKind() == OMPC_simdlen)
3778 Simdlen = cast<OMPSimdlenClause>(Clause);
3779 if (Safelen && Simdlen)
3780 break;
3781 }
3782 if (Simdlen && Safelen &&
3783 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
3784 Safelen->getSafelen()))
3785 return StmtError();
3786
Alexander Musmanf82886e2014-09-18 05:12:34 +00003787 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003788 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
3789 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00003790}
3791
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003792StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
3793 Stmt *AStmt,
3794 SourceLocation StartLoc,
3795 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003796 if (!AStmt)
3797 return StmtError();
3798
3799 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003800 auto BaseStmt = AStmt;
3801 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
3802 BaseStmt = CS->getCapturedStmt();
3803 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
3804 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00003805 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003806 return StmtError();
3807 // All associated statements must be '#pragma omp section' except for
3808 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00003809 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003810 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
3811 if (SectionStmt)
3812 Diag(SectionStmt->getLocStart(),
3813 diag::err_omp_sections_substmt_not_section);
3814 return StmtError();
3815 }
3816 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003817 } else {
3818 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
3819 return StmtError();
3820 }
3821
3822 getCurFunction()->setHasBranchProtectedScope();
3823
3824 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses,
3825 AStmt);
3826}
3827
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003828StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
3829 SourceLocation StartLoc,
3830 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003831 if (!AStmt)
3832 return StmtError();
3833
3834 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003835
3836 getCurFunction()->setHasBranchProtectedScope();
3837
3838 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt);
3839}
3840
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003841StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
3842 Stmt *AStmt,
3843 SourceLocation StartLoc,
3844 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003845 if (!AStmt)
3846 return StmtError();
3847
3848 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00003849
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003850 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00003851
Alexey Bataev3255bf32015-01-19 05:20:46 +00003852 // OpenMP [2.7.3, single Construct, Restrictions]
3853 // The copyprivate clause must not be used with the nowait clause.
3854 OMPClause *Nowait = nullptr;
3855 OMPClause *Copyprivate = nullptr;
3856 for (auto *Clause : Clauses) {
3857 if (Clause->getClauseKind() == OMPC_nowait)
3858 Nowait = Clause;
3859 else if (Clause->getClauseKind() == OMPC_copyprivate)
3860 Copyprivate = Clause;
3861 if (Copyprivate && Nowait) {
3862 Diag(Copyprivate->getLocStart(),
3863 diag::err_omp_single_copyprivate_with_nowait);
3864 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
3865 return StmtError();
3866 }
3867 }
3868
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003869 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3870}
3871
Alexander Musman80c22892014-07-17 08:54:58 +00003872StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
3873 SourceLocation StartLoc,
3874 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003875 if (!AStmt)
3876 return StmtError();
3877
3878 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00003879
3880 getCurFunction()->setHasBranchProtectedScope();
3881
3882 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
3883}
3884
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003885StmtResult
3886Sema::ActOnOpenMPCriticalDirective(const DeclarationNameInfo &DirName,
3887 Stmt *AStmt, SourceLocation StartLoc,
3888 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003889 if (!AStmt)
3890 return StmtError();
3891
3892 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003893
3894 getCurFunction()->setHasBranchProtectedScope();
3895
3896 return OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
3897 AStmt);
3898}
3899
Alexey Bataev4acb8592014-07-07 13:01:15 +00003900StmtResult Sema::ActOnOpenMPParallelForDirective(
3901 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3902 SourceLocation EndLoc,
3903 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003904 if (!AStmt)
3905 return StmtError();
3906
Alexey Bataev4acb8592014-07-07 13:01:15 +00003907 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3908 // 1.2.2 OpenMP Language Terminology
3909 // Structured block - An executable statement with a single entry at the
3910 // top and a single exit at the bottom.
3911 // The point of exit cannot be a branch out of the structured block.
3912 // longjmp() and throw() must not violate the entry/exit criteria.
3913 CS->getCapturedDecl()->setNothrow();
3914
Alexander Musmanc6388682014-12-15 07:07:06 +00003915 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003916 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
3917 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003918 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00003919 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
3920 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
3921 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003922 if (NestedLoopCount == 0)
3923 return StmtError();
3924
Alexander Musmana5f070a2014-10-01 06:03:56 +00003925 assert((CurContext->isDependentContext() || B.builtAll()) &&
3926 "omp parallel for loop exprs were not built");
3927
Alexey Bataev54acd402015-08-04 11:18:19 +00003928 if (!CurContext->isDependentContext()) {
3929 // Finalize the clauses that need pre-built expressions for CodeGen.
3930 for (auto C : Clauses) {
3931 if (auto LC = dyn_cast<OMPLinearClause>(C))
3932 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3933 B.NumIterations, *this, CurScope))
3934 return StmtError();
3935 }
3936 }
3937
Alexey Bataev4acb8592014-07-07 13:01:15 +00003938 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003939 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
3940 NestedLoopCount, Clauses, AStmt, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003941}
3942
Alexander Musmane4e893b2014-09-23 09:33:00 +00003943StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
3944 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3945 SourceLocation EndLoc,
3946 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003947 if (!AStmt)
3948 return StmtError();
3949
Alexander Musmane4e893b2014-09-23 09:33:00 +00003950 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3951 // 1.2.2 OpenMP Language Terminology
3952 // Structured block - An executable statement with a single entry at the
3953 // top and a single exit at the bottom.
3954 // The point of exit cannot be a branch out of the structured block.
3955 // longjmp() and throw() must not violate the entry/exit criteria.
3956 CS->getCapturedDecl()->setNothrow();
3957
Alexander Musmanc6388682014-12-15 07:07:06 +00003958 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003959 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
3960 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00003961 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00003962 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
3963 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
3964 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003965 if (NestedLoopCount == 0)
3966 return StmtError();
3967
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00003968 if (!CurContext->isDependentContext()) {
3969 // Finalize the clauses that need pre-built expressions for CodeGen.
3970 for (auto C : Clauses) {
3971 if (auto LC = dyn_cast<OMPLinearClause>(C))
3972 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3973 B.NumIterations, *this, CurScope))
3974 return StmtError();
3975 }
3976 }
3977
Alexey Bataev66b15b52015-08-21 11:14:16 +00003978 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
3979 // If both simdlen and safelen clauses are specified, the value of the simdlen
3980 // parameter must be less than or equal to the value of the safelen parameter.
3981 OMPSafelenClause *Safelen = nullptr;
3982 OMPSimdlenClause *Simdlen = nullptr;
3983 for (auto *Clause : Clauses) {
3984 if (Clause->getClauseKind() == OMPC_safelen)
3985 Safelen = cast<OMPSafelenClause>(Clause);
3986 else if (Clause->getClauseKind() == OMPC_simdlen)
3987 Simdlen = cast<OMPSimdlenClause>(Clause);
3988 if (Safelen && Simdlen)
3989 break;
3990 }
3991 if (Simdlen && Safelen &&
3992 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
3993 Safelen->getSafelen()))
3994 return StmtError();
3995
Alexander Musmane4e893b2014-09-23 09:33:00 +00003996 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003997 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00003998 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003999}
4000
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004001StmtResult
4002Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
4003 Stmt *AStmt, SourceLocation StartLoc,
4004 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004005 if (!AStmt)
4006 return StmtError();
4007
4008 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004009 auto BaseStmt = AStmt;
4010 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4011 BaseStmt = CS->getCapturedStmt();
4012 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4013 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004014 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004015 return StmtError();
4016 // All associated statements must be '#pragma omp section' except for
4017 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004018 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004019 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4020 if (SectionStmt)
4021 Diag(SectionStmt->getLocStart(),
4022 diag::err_omp_parallel_sections_substmt_not_section);
4023 return StmtError();
4024 }
4025 }
4026 } else {
4027 Diag(AStmt->getLocStart(),
4028 diag::err_omp_parallel_sections_not_compound_stmt);
4029 return StmtError();
4030 }
4031
4032 getCurFunction()->setHasBranchProtectedScope();
4033
4034 return OMPParallelSectionsDirective::Create(Context, StartLoc, EndLoc,
4035 Clauses, AStmt);
4036}
4037
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004038StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
4039 Stmt *AStmt, SourceLocation StartLoc,
4040 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004041 if (!AStmt)
4042 return StmtError();
4043
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004044 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4045 // 1.2.2 OpenMP Language Terminology
4046 // Structured block - An executable statement with a single entry at the
4047 // top and a single exit at the bottom.
4048 // The point of exit cannot be a branch out of the structured block.
4049 // longjmp() and throw() must not violate the entry/exit criteria.
4050 CS->getCapturedDecl()->setNothrow();
4051
4052 getCurFunction()->setHasBranchProtectedScope();
4053
4054 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4055}
4056
Alexey Bataev68446b72014-07-18 07:47:19 +00004057StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
4058 SourceLocation EndLoc) {
4059 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
4060}
4061
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00004062StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
4063 SourceLocation EndLoc) {
4064 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
4065}
4066
Alexey Bataev2df347a2014-07-18 10:17:07 +00004067StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
4068 SourceLocation EndLoc) {
4069 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
4070}
4071
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00004072StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
4073 SourceLocation StartLoc,
4074 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004075 if (!AStmt)
4076 return StmtError();
4077
4078 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00004079
4080 getCurFunction()->setHasBranchProtectedScope();
4081
4082 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
4083}
4084
Alexey Bataev6125da92014-07-21 11:26:11 +00004085StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
4086 SourceLocation StartLoc,
4087 SourceLocation EndLoc) {
4088 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
4089 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
4090}
4091
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004092StmtResult Sema::ActOnOpenMPOrderedDirective(Stmt *AStmt,
4093 SourceLocation StartLoc,
4094 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004095 if (!AStmt)
4096 return StmtError();
4097
4098 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004099
4100 getCurFunction()->setHasBranchProtectedScope();
4101
4102 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, AStmt);
4103}
4104
Alexey Bataev1d160b12015-03-13 12:27:31 +00004105namespace {
4106/// \brief Helper class for checking expression in 'omp atomic [update]'
4107/// construct.
4108class OpenMPAtomicUpdateChecker {
4109 /// \brief Error results for atomic update expressions.
4110 enum ExprAnalysisErrorCode {
4111 /// \brief A statement is not an expression statement.
4112 NotAnExpression,
4113 /// \brief Expression is not builtin binary or unary operation.
4114 NotABinaryOrUnaryExpression,
4115 /// \brief Unary operation is not post-/pre- increment/decrement operation.
4116 NotAnUnaryIncDecExpression,
4117 /// \brief An expression is not of scalar type.
4118 NotAScalarType,
4119 /// \brief A binary operation is not an assignment operation.
4120 NotAnAssignmentOp,
4121 /// \brief RHS part of the binary operation is not a binary expression.
4122 NotABinaryExpression,
4123 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
4124 /// expression.
4125 NotABinaryOperator,
4126 /// \brief RHS binary operation does not have reference to the updated LHS
4127 /// part.
4128 NotAnUpdateExpression,
4129 /// \brief No errors is found.
4130 NoError
4131 };
4132 /// \brief Reference to Sema.
4133 Sema &SemaRef;
4134 /// \brief A location for note diagnostics (when error is found).
4135 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004136 /// \brief 'x' lvalue part of the source atomic expression.
4137 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004138 /// \brief 'expr' rvalue part of the source atomic expression.
4139 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004140 /// \brief Helper expression of the form
4141 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
4142 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
4143 Expr *UpdateExpr;
4144 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
4145 /// important for non-associative operations.
4146 bool IsXLHSInRHSPart;
4147 BinaryOperatorKind Op;
4148 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004149 /// \brief true if the source expression is a postfix unary operation, false
4150 /// if it is a prefix unary operation.
4151 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004152
4153public:
4154 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00004155 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00004156 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00004157 /// \brief Check specified statement that it is suitable for 'atomic update'
4158 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00004159 /// expression. If DiagId and NoteId == 0, then only check is performed
4160 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00004161 /// \param DiagId Diagnostic which should be emitted if error is found.
4162 /// \param NoteId Diagnostic note for the main error message.
4163 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00004164 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00004165 /// \brief Return the 'x' lvalue part of the source atomic expression.
4166 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00004167 /// \brief Return the 'expr' rvalue part of the source atomic expression.
4168 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00004169 /// \brief Return the update expression used in calculation of the updated
4170 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
4171 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
4172 Expr *getUpdateExpr() const { return UpdateExpr; }
4173 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
4174 /// false otherwise.
4175 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
4176
Alexey Bataevb78ca832015-04-01 03:33:17 +00004177 /// \brief true if the source expression is a postfix unary operation, false
4178 /// if it is a prefix unary operation.
4179 bool isPostfixUpdate() const { return IsPostfixUpdate; }
4180
Alexey Bataev1d160b12015-03-13 12:27:31 +00004181private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00004182 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
4183 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00004184};
4185} // namespace
4186
4187bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
4188 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
4189 ExprAnalysisErrorCode ErrorFound = NoError;
4190 SourceLocation ErrorLoc, NoteLoc;
4191 SourceRange ErrorRange, NoteRange;
4192 // Allowed constructs are:
4193 // x = x binop expr;
4194 // x = expr binop x;
4195 if (AtomicBinOp->getOpcode() == BO_Assign) {
4196 X = AtomicBinOp->getLHS();
4197 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
4198 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
4199 if (AtomicInnerBinOp->isMultiplicativeOp() ||
4200 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
4201 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00004202 Op = AtomicInnerBinOp->getOpcode();
4203 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00004204 auto *LHS = AtomicInnerBinOp->getLHS();
4205 auto *RHS = AtomicInnerBinOp->getRHS();
4206 llvm::FoldingSetNodeID XId, LHSId, RHSId;
4207 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
4208 /*Canonical=*/true);
4209 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
4210 /*Canonical=*/true);
4211 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
4212 /*Canonical=*/true);
4213 if (XId == LHSId) {
4214 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004215 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004216 } else if (XId == RHSId) {
4217 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004218 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004219 } else {
4220 ErrorLoc = AtomicInnerBinOp->getExprLoc();
4221 ErrorRange = AtomicInnerBinOp->getSourceRange();
4222 NoteLoc = X->getExprLoc();
4223 NoteRange = X->getSourceRange();
4224 ErrorFound = NotAnUpdateExpression;
4225 }
4226 } else {
4227 ErrorLoc = AtomicInnerBinOp->getExprLoc();
4228 ErrorRange = AtomicInnerBinOp->getSourceRange();
4229 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
4230 NoteRange = SourceRange(NoteLoc, NoteLoc);
4231 ErrorFound = NotABinaryOperator;
4232 }
4233 } else {
4234 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
4235 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
4236 ErrorFound = NotABinaryExpression;
4237 }
4238 } else {
4239 ErrorLoc = AtomicBinOp->getExprLoc();
4240 ErrorRange = AtomicBinOp->getSourceRange();
4241 NoteLoc = AtomicBinOp->getOperatorLoc();
4242 NoteRange = SourceRange(NoteLoc, NoteLoc);
4243 ErrorFound = NotAnAssignmentOp;
4244 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00004245 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004246 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
4247 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
4248 return true;
4249 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00004250 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00004251 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004252}
4253
4254bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
4255 unsigned NoteId) {
4256 ExprAnalysisErrorCode ErrorFound = NoError;
4257 SourceLocation ErrorLoc, NoteLoc;
4258 SourceRange ErrorRange, NoteRange;
4259 // Allowed constructs are:
4260 // x++;
4261 // x--;
4262 // ++x;
4263 // --x;
4264 // x binop= expr;
4265 // x = x binop expr;
4266 // x = expr binop x;
4267 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
4268 AtomicBody = AtomicBody->IgnoreParenImpCasts();
4269 if (AtomicBody->getType()->isScalarType() ||
4270 AtomicBody->isInstantiationDependent()) {
4271 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
4272 AtomicBody->IgnoreParenImpCasts())) {
4273 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00004274 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00004275 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00004276 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00004277 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00004278 X = AtomicCompAssignOp->getLHS();
4279 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004280 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
4281 AtomicBody->IgnoreParenImpCasts())) {
4282 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00004283 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
4284 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004285 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00004286 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
4287 // Check for Unary Operation
4288 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004289 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00004290 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
4291 OpLoc = AtomicUnaryOp->getOperatorLoc();
4292 X = AtomicUnaryOp->getSubExpr();
4293 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
4294 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004295 } else {
4296 ErrorFound = NotAnUnaryIncDecExpression;
4297 ErrorLoc = AtomicUnaryOp->getExprLoc();
4298 ErrorRange = AtomicUnaryOp->getSourceRange();
4299 NoteLoc = AtomicUnaryOp->getOperatorLoc();
4300 NoteRange = SourceRange(NoteLoc, NoteLoc);
4301 }
Alexey Bataev5a195472015-09-04 12:55:50 +00004302 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004303 ErrorFound = NotABinaryOrUnaryExpression;
4304 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
4305 NoteRange = ErrorRange = AtomicBody->getSourceRange();
4306 }
4307 } else {
4308 ErrorFound = NotAScalarType;
4309 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
4310 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
4311 }
4312 } else {
4313 ErrorFound = NotAnExpression;
4314 NoteLoc = ErrorLoc = S->getLocStart();
4315 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
4316 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00004317 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004318 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
4319 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
4320 return true;
4321 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00004322 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00004323 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00004324 // Build an update expression of form 'OpaqueValueExpr(x) binop
4325 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
4326 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
4327 auto *OVEX = new (SemaRef.getASTContext())
4328 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
4329 auto *OVEExpr = new (SemaRef.getASTContext())
4330 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
4331 auto Update =
4332 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
4333 IsXLHSInRHSPart ? OVEExpr : OVEX);
4334 if (Update.isInvalid())
4335 return true;
4336 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
4337 Sema::AA_Casting);
4338 if (Update.isInvalid())
4339 return true;
4340 UpdateExpr = Update.get();
4341 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00004342 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004343}
4344
Alexey Bataev0162e452014-07-22 10:10:35 +00004345StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
4346 Stmt *AStmt,
4347 SourceLocation StartLoc,
4348 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004349 if (!AStmt)
4350 return StmtError();
4351
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004352 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00004353 // 1.2.2 OpenMP Language Terminology
4354 // Structured block - An executable statement with a single entry at the
4355 // top and a single exit at the bottom.
4356 // The point of exit cannot be a branch out of the structured block.
4357 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00004358 OpenMPClauseKind AtomicKind = OMPC_unknown;
4359 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004360 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00004361 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00004362 C->getClauseKind() == OMPC_update ||
4363 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00004364 if (AtomicKind != OMPC_unknown) {
4365 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
4366 << SourceRange(C->getLocStart(), C->getLocEnd());
4367 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
4368 << getOpenMPClauseName(AtomicKind);
4369 } else {
4370 AtomicKind = C->getClauseKind();
4371 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004372 }
4373 }
4374 }
Alexey Bataev62cec442014-11-18 10:14:22 +00004375
Alexey Bataev459dec02014-07-24 06:46:57 +00004376 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00004377 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
4378 Body = EWC->getSubExpr();
4379
Alexey Bataev62cec442014-11-18 10:14:22 +00004380 Expr *X = nullptr;
4381 Expr *V = nullptr;
4382 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004383 Expr *UE = nullptr;
4384 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004385 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00004386 // OpenMP [2.12.6, atomic Construct]
4387 // In the next expressions:
4388 // * x and v (as applicable) are both l-value expressions with scalar type.
4389 // * During the execution of an atomic region, multiple syntactic
4390 // occurrences of x must designate the same storage location.
4391 // * Neither of v and expr (as applicable) may access the storage location
4392 // designated by x.
4393 // * Neither of x and expr (as applicable) may access the storage location
4394 // designated by v.
4395 // * expr is an expression with scalar type.
4396 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
4397 // * binop, binop=, ++, and -- are not overloaded operators.
4398 // * The expression x binop expr must be numerically equivalent to x binop
4399 // (expr). This requirement is satisfied if the operators in expr have
4400 // precedence greater than binop, or by using parentheses around expr or
4401 // subexpressions of expr.
4402 // * The expression expr binop x must be numerically equivalent to (expr)
4403 // binop x. This requirement is satisfied if the operators in expr have
4404 // precedence equal to or greater than binop, or by using parentheses around
4405 // expr or subexpressions of expr.
4406 // * For forms that allow multiple occurrences of x, the number of times
4407 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00004408 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004409 enum {
4410 NotAnExpression,
4411 NotAnAssignmentOp,
4412 NotAScalarType,
4413 NotAnLValue,
4414 NoError
4415 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00004416 SourceLocation ErrorLoc, NoteLoc;
4417 SourceRange ErrorRange, NoteRange;
4418 // If clause is read:
4419 // v = x;
4420 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
4421 auto AtomicBinOp =
4422 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
4423 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
4424 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
4425 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
4426 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
4427 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
4428 if (!X->isLValue() || !V->isLValue()) {
4429 auto NotLValueExpr = X->isLValue() ? V : X;
4430 ErrorFound = NotAnLValue;
4431 ErrorLoc = AtomicBinOp->getExprLoc();
4432 ErrorRange = AtomicBinOp->getSourceRange();
4433 NoteLoc = NotLValueExpr->getExprLoc();
4434 NoteRange = NotLValueExpr->getSourceRange();
4435 }
4436 } else if (!X->isInstantiationDependent() ||
4437 !V->isInstantiationDependent()) {
4438 auto NotScalarExpr =
4439 (X->isInstantiationDependent() || X->getType()->isScalarType())
4440 ? V
4441 : X;
4442 ErrorFound = NotAScalarType;
4443 ErrorLoc = AtomicBinOp->getExprLoc();
4444 ErrorRange = AtomicBinOp->getSourceRange();
4445 NoteLoc = NotScalarExpr->getExprLoc();
4446 NoteRange = NotScalarExpr->getSourceRange();
4447 }
Alexey Bataev5a195472015-09-04 12:55:50 +00004448 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00004449 ErrorFound = NotAnAssignmentOp;
4450 ErrorLoc = AtomicBody->getExprLoc();
4451 ErrorRange = AtomicBody->getSourceRange();
4452 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
4453 : AtomicBody->getExprLoc();
4454 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
4455 : AtomicBody->getSourceRange();
4456 }
4457 } else {
4458 ErrorFound = NotAnExpression;
4459 NoteLoc = ErrorLoc = Body->getLocStart();
4460 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00004461 }
Alexey Bataev62cec442014-11-18 10:14:22 +00004462 if (ErrorFound != NoError) {
4463 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
4464 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00004465 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
4466 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00004467 return StmtError();
4468 } else if (CurContext->isDependentContext())
4469 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00004470 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004471 enum {
4472 NotAnExpression,
4473 NotAnAssignmentOp,
4474 NotAScalarType,
4475 NotAnLValue,
4476 NoError
4477 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00004478 SourceLocation ErrorLoc, NoteLoc;
4479 SourceRange ErrorRange, NoteRange;
4480 // If clause is write:
4481 // x = expr;
4482 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
4483 auto AtomicBinOp =
4484 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
4485 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00004486 X = AtomicBinOp->getLHS();
4487 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00004488 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
4489 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
4490 if (!X->isLValue()) {
4491 ErrorFound = NotAnLValue;
4492 ErrorLoc = AtomicBinOp->getExprLoc();
4493 ErrorRange = AtomicBinOp->getSourceRange();
4494 NoteLoc = X->getExprLoc();
4495 NoteRange = X->getSourceRange();
4496 }
4497 } else if (!X->isInstantiationDependent() ||
4498 !E->isInstantiationDependent()) {
4499 auto NotScalarExpr =
4500 (X->isInstantiationDependent() || X->getType()->isScalarType())
4501 ? E
4502 : X;
4503 ErrorFound = NotAScalarType;
4504 ErrorLoc = AtomicBinOp->getExprLoc();
4505 ErrorRange = AtomicBinOp->getSourceRange();
4506 NoteLoc = NotScalarExpr->getExprLoc();
4507 NoteRange = NotScalarExpr->getSourceRange();
4508 }
Alexey Bataev5a195472015-09-04 12:55:50 +00004509 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00004510 ErrorFound = NotAnAssignmentOp;
4511 ErrorLoc = AtomicBody->getExprLoc();
4512 ErrorRange = AtomicBody->getSourceRange();
4513 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
4514 : AtomicBody->getExprLoc();
4515 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
4516 : AtomicBody->getSourceRange();
4517 }
4518 } else {
4519 ErrorFound = NotAnExpression;
4520 NoteLoc = ErrorLoc = Body->getLocStart();
4521 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00004522 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00004523 if (ErrorFound != NoError) {
4524 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
4525 << ErrorRange;
4526 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
4527 << NoteRange;
4528 return StmtError();
4529 } else if (CurContext->isDependentContext())
4530 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00004531 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004532 // If clause is update:
4533 // x++;
4534 // x--;
4535 // ++x;
4536 // --x;
4537 // x binop= expr;
4538 // x = x binop expr;
4539 // x = expr binop x;
4540 OpenMPAtomicUpdateChecker Checker(*this);
4541 if (Checker.checkStatement(
4542 Body, (AtomicKind == OMPC_update)
4543 ? diag::err_omp_atomic_update_not_expression_statement
4544 : diag::err_omp_atomic_not_expression_statement,
4545 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00004546 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00004547 if (!CurContext->isDependentContext()) {
4548 E = Checker.getExpr();
4549 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00004550 UE = Checker.getUpdateExpr();
4551 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00004552 }
Alexey Bataev459dec02014-07-24 06:46:57 +00004553 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004554 enum {
4555 NotAnAssignmentOp,
4556 NotACompoundStatement,
4557 NotTwoSubstatements,
4558 NotASpecificExpression,
4559 NoError
4560 } ErrorFound = NoError;
4561 SourceLocation ErrorLoc, NoteLoc;
4562 SourceRange ErrorRange, NoteRange;
4563 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
4564 // If clause is a capture:
4565 // v = x++;
4566 // v = x--;
4567 // v = ++x;
4568 // v = --x;
4569 // v = x binop= expr;
4570 // v = x = x binop expr;
4571 // v = x = expr binop x;
4572 auto *AtomicBinOp =
4573 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
4574 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
4575 V = AtomicBinOp->getLHS();
4576 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
4577 OpenMPAtomicUpdateChecker Checker(*this);
4578 if (Checker.checkStatement(
4579 Body, diag::err_omp_atomic_capture_not_expression_statement,
4580 diag::note_omp_atomic_update))
4581 return StmtError();
4582 E = Checker.getExpr();
4583 X = Checker.getX();
4584 UE = Checker.getUpdateExpr();
4585 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
4586 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00004587 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004588 ErrorLoc = AtomicBody->getExprLoc();
4589 ErrorRange = AtomicBody->getSourceRange();
4590 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
4591 : AtomicBody->getExprLoc();
4592 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
4593 : AtomicBody->getSourceRange();
4594 ErrorFound = NotAnAssignmentOp;
4595 }
4596 if (ErrorFound != NoError) {
4597 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
4598 << ErrorRange;
4599 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
4600 return StmtError();
4601 } else if (CurContext->isDependentContext()) {
4602 UE = V = E = X = nullptr;
4603 }
4604 } else {
4605 // If clause is a capture:
4606 // { v = x; x = expr; }
4607 // { v = x; x++; }
4608 // { v = x; x--; }
4609 // { v = x; ++x; }
4610 // { v = x; --x; }
4611 // { v = x; x binop= expr; }
4612 // { v = x; x = x binop expr; }
4613 // { v = x; x = expr binop x; }
4614 // { x++; v = x; }
4615 // { x--; v = x; }
4616 // { ++x; v = x; }
4617 // { --x; v = x; }
4618 // { x binop= expr; v = x; }
4619 // { x = x binop expr; v = x; }
4620 // { x = expr binop x; v = x; }
4621 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
4622 // Check that this is { expr1; expr2; }
4623 if (CS->size() == 2) {
4624 auto *First = CS->body_front();
4625 auto *Second = CS->body_back();
4626 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
4627 First = EWC->getSubExpr()->IgnoreParenImpCasts();
4628 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
4629 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
4630 // Need to find what subexpression is 'v' and what is 'x'.
4631 OpenMPAtomicUpdateChecker Checker(*this);
4632 bool IsUpdateExprFound = !Checker.checkStatement(Second);
4633 BinaryOperator *BinOp = nullptr;
4634 if (IsUpdateExprFound) {
4635 BinOp = dyn_cast<BinaryOperator>(First);
4636 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
4637 }
4638 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
4639 // { v = x; x++; }
4640 // { v = x; x--; }
4641 // { v = x; ++x; }
4642 // { v = x; --x; }
4643 // { v = x; x binop= expr; }
4644 // { v = x; x = x binop expr; }
4645 // { v = x; x = expr binop x; }
4646 // Check that the first expression has form v = x.
4647 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
4648 llvm::FoldingSetNodeID XId, PossibleXId;
4649 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
4650 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
4651 IsUpdateExprFound = XId == PossibleXId;
4652 if (IsUpdateExprFound) {
4653 V = BinOp->getLHS();
4654 X = Checker.getX();
4655 E = Checker.getExpr();
4656 UE = Checker.getUpdateExpr();
4657 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00004658 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004659 }
4660 }
4661 if (!IsUpdateExprFound) {
4662 IsUpdateExprFound = !Checker.checkStatement(First);
4663 BinOp = nullptr;
4664 if (IsUpdateExprFound) {
4665 BinOp = dyn_cast<BinaryOperator>(Second);
4666 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
4667 }
4668 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
4669 // { x++; v = x; }
4670 // { x--; v = x; }
4671 // { ++x; v = x; }
4672 // { --x; v = x; }
4673 // { x binop= expr; v = x; }
4674 // { x = x binop expr; v = x; }
4675 // { x = expr binop x; v = x; }
4676 // Check that the second expression has form v = x.
4677 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
4678 llvm::FoldingSetNodeID XId, PossibleXId;
4679 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
4680 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
4681 IsUpdateExprFound = XId == PossibleXId;
4682 if (IsUpdateExprFound) {
4683 V = BinOp->getLHS();
4684 X = Checker.getX();
4685 E = Checker.getExpr();
4686 UE = Checker.getUpdateExpr();
4687 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00004688 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004689 }
4690 }
4691 }
4692 if (!IsUpdateExprFound) {
4693 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00004694 auto *FirstExpr = dyn_cast<Expr>(First);
4695 auto *SecondExpr = dyn_cast<Expr>(Second);
4696 if (!FirstExpr || !SecondExpr ||
4697 !(FirstExpr->isInstantiationDependent() ||
4698 SecondExpr->isInstantiationDependent())) {
4699 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
4700 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004701 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00004702 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
4703 : First->getLocStart();
4704 NoteRange = ErrorRange = FirstBinOp
4705 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00004706 : SourceRange(ErrorLoc, ErrorLoc);
4707 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00004708 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
4709 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
4710 ErrorFound = NotAnAssignmentOp;
4711 NoteLoc = ErrorLoc = SecondBinOp
4712 ? SecondBinOp->getOperatorLoc()
4713 : Second->getLocStart();
4714 NoteRange = ErrorRange =
4715 SecondBinOp ? SecondBinOp->getSourceRange()
4716 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00004717 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00004718 auto *PossibleXRHSInFirst =
4719 FirstBinOp->getRHS()->IgnoreParenImpCasts();
4720 auto *PossibleXLHSInSecond =
4721 SecondBinOp->getLHS()->IgnoreParenImpCasts();
4722 llvm::FoldingSetNodeID X1Id, X2Id;
4723 PossibleXRHSInFirst->Profile(X1Id, Context,
4724 /*Canonical=*/true);
4725 PossibleXLHSInSecond->Profile(X2Id, Context,
4726 /*Canonical=*/true);
4727 IsUpdateExprFound = X1Id == X2Id;
4728 if (IsUpdateExprFound) {
4729 V = FirstBinOp->getLHS();
4730 X = SecondBinOp->getLHS();
4731 E = SecondBinOp->getRHS();
4732 UE = nullptr;
4733 IsXLHSInRHSPart = false;
4734 IsPostfixUpdate = true;
4735 } else {
4736 ErrorFound = NotASpecificExpression;
4737 ErrorLoc = FirstBinOp->getExprLoc();
4738 ErrorRange = FirstBinOp->getSourceRange();
4739 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
4740 NoteRange = SecondBinOp->getRHS()->getSourceRange();
4741 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00004742 }
4743 }
4744 }
4745 }
4746 } else {
4747 NoteLoc = ErrorLoc = Body->getLocStart();
4748 NoteRange = ErrorRange =
4749 SourceRange(Body->getLocStart(), Body->getLocStart());
4750 ErrorFound = NotTwoSubstatements;
4751 }
4752 } else {
4753 NoteLoc = ErrorLoc = Body->getLocStart();
4754 NoteRange = ErrorRange =
4755 SourceRange(Body->getLocStart(), Body->getLocStart());
4756 ErrorFound = NotACompoundStatement;
4757 }
4758 if (ErrorFound != NoError) {
4759 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
4760 << ErrorRange;
4761 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
4762 return StmtError();
4763 } else if (CurContext->isDependentContext()) {
4764 UE = V = E = X = nullptr;
4765 }
Alexey Bataev459dec02014-07-24 06:46:57 +00004766 }
Alexey Bataevdea47612014-07-23 07:46:59 +00004767 }
Alexey Bataev0162e452014-07-22 10:10:35 +00004768
4769 getCurFunction()->setHasBranchProtectedScope();
4770
Alexey Bataev62cec442014-11-18 10:14:22 +00004771 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00004772 X, V, E, UE, IsXLHSInRHSPart,
4773 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00004774}
4775
Alexey Bataev0bd520b2014-09-19 08:19:49 +00004776StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
4777 Stmt *AStmt,
4778 SourceLocation StartLoc,
4779 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004780 if (!AStmt)
4781 return StmtError();
4782
4783 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev0bd520b2014-09-19 08:19:49 +00004784
Alexey Bataev13314bf2014-10-09 04:18:56 +00004785 // OpenMP [2.16, Nesting of Regions]
4786 // If specified, a teams construct must be contained within a target
4787 // construct. That target construct must contain no statements or directives
4788 // outside of the teams construct.
4789 if (DSAStack->hasInnerTeamsRegion()) {
4790 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
4791 bool OMPTeamsFound = true;
4792 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
4793 auto I = CS->body_begin();
4794 while (I != CS->body_end()) {
4795 auto OED = dyn_cast<OMPExecutableDirective>(*I);
4796 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
4797 OMPTeamsFound = false;
4798 break;
4799 }
4800 ++I;
4801 }
4802 assert(I != CS->body_end() && "Not found statement");
4803 S = *I;
4804 }
4805 if (!OMPTeamsFound) {
4806 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
4807 Diag(DSAStack->getInnerTeamsRegionLoc(),
4808 diag::note_omp_nested_teams_construct_here);
4809 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
4810 << isa<OMPExecutableDirective>(S);
4811 return StmtError();
4812 }
4813 }
4814
Alexey Bataev0bd520b2014-09-19 08:19:49 +00004815 getCurFunction()->setHasBranchProtectedScope();
4816
4817 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4818}
4819
Michael Wong65f367f2015-07-21 13:44:28 +00004820StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
4821 Stmt *AStmt,
4822 SourceLocation StartLoc,
4823 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004824 if (!AStmt)
4825 return StmtError();
4826
4827 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
4828
Michael Wong65f367f2015-07-21 13:44:28 +00004829 getCurFunction()->setHasBranchProtectedScope();
4830
4831 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
4832 AStmt);
4833}
4834
Alexey Bataev13314bf2014-10-09 04:18:56 +00004835StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
4836 Stmt *AStmt, SourceLocation StartLoc,
4837 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004838 if (!AStmt)
4839 return StmtError();
4840
Alexey Bataev13314bf2014-10-09 04:18:56 +00004841 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4842 // 1.2.2 OpenMP Language Terminology
4843 // Structured block - An executable statement with a single entry at the
4844 // top and a single exit at the bottom.
4845 // The point of exit cannot be a branch out of the structured block.
4846 // longjmp() and throw() must not violate the entry/exit criteria.
4847 CS->getCapturedDecl()->setNothrow();
4848
4849 getCurFunction()->setHasBranchProtectedScope();
4850
4851 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4852}
4853
Alexey Bataev6d4ed052015-07-01 06:57:41 +00004854StmtResult
4855Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
4856 SourceLocation EndLoc,
4857 OpenMPDirectiveKind CancelRegion) {
4858 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
4859 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
4860 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
4861 << getOpenMPDirectiveName(CancelRegion);
4862 return StmtError();
4863 }
4864 if (DSAStack->isParentNowaitRegion()) {
4865 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
4866 return StmtError();
4867 }
4868 if (DSAStack->isParentOrderedRegion()) {
4869 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
4870 return StmtError();
4871 }
4872 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
4873 CancelRegion);
4874}
4875
Alexey Bataev80909872015-07-02 11:25:17 +00004876StmtResult Sema::ActOnOpenMPCancelDirective(SourceLocation StartLoc,
4877 SourceLocation EndLoc,
4878 OpenMPDirectiveKind CancelRegion) {
4879 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
4880 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
4881 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
4882 << getOpenMPDirectiveName(CancelRegion);
4883 return StmtError();
4884 }
4885 if (DSAStack->isParentNowaitRegion()) {
4886 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
4887 return StmtError();
4888 }
4889 if (DSAStack->isParentOrderedRegion()) {
4890 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
4891 return StmtError();
4892 }
4893 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, CancelRegion);
4894}
4895
Alexey Bataeved09d242014-05-28 05:53:51 +00004896OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004897 SourceLocation StartLoc,
4898 SourceLocation LParenLoc,
4899 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004900 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004901 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00004902 case OMPC_final:
4903 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
4904 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00004905 case OMPC_num_threads:
4906 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
4907 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00004908 case OMPC_safelen:
4909 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
4910 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00004911 case OMPC_simdlen:
4912 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
4913 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00004914 case OMPC_collapse:
4915 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
4916 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004917 case OMPC_ordered:
4918 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
4919 break;
Michael Wonge710d542015-08-07 16:16:36 +00004920 case OMPC_device:
4921 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
4922 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004923 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004924 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004925 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00004926 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004927 case OMPC_private:
4928 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00004929 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004930 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00004931 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00004932 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00004933 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004934 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00004935 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00004936 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00004937 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00004938 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004939 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00004940 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004941 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00004942 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00004943 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00004944 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00004945 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00004946 case OMPC_depend:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004947 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004948 llvm_unreachable("Clause is not allowed.");
4949 }
4950 return Res;
4951}
4952
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004953OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
4954 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004955 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004956 SourceLocation NameModifierLoc,
4957 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004958 SourceLocation EndLoc) {
4959 Expr *ValExpr = Condition;
4960 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
4961 !Condition->isInstantiationDependent() &&
4962 !Condition->containsUnexpandedParameterPack()) {
4963 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00004964 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004965 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004966 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004967
Nikola Smiljanic01a75982014-05-29 10:55:11 +00004968 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004969 }
4970
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004971 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
4972 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004973}
4974
Alexey Bataev3778b602014-07-17 07:32:53 +00004975OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
4976 SourceLocation StartLoc,
4977 SourceLocation LParenLoc,
4978 SourceLocation EndLoc) {
4979 Expr *ValExpr = Condition;
4980 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
4981 !Condition->isInstantiationDependent() &&
4982 !Condition->containsUnexpandedParameterPack()) {
4983 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
4984 Condition->getExprLoc(), Condition);
4985 if (Val.isInvalid())
4986 return nullptr;
4987
4988 ValExpr = Val.get();
4989 }
4990
4991 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
4992}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004993ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
4994 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00004995 if (!Op)
4996 return ExprError();
4997
4998 class IntConvertDiagnoser : public ICEConvertDiagnoser {
4999 public:
5000 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00005001 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00005002 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
5003 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005004 return S.Diag(Loc, diag::err_omp_not_integral) << T;
5005 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005006 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
5007 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005008 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
5009 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005010 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
5011 QualType T,
5012 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005013 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
5014 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005015 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
5016 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005017 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00005018 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00005019 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005020 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
5021 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005022 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
5023 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005024 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
5025 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005026 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00005027 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00005028 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005029 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
5030 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005031 llvm_unreachable("conversion functions are permitted");
5032 }
5033 } ConvertDiagnoser;
5034 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
5035}
5036
5037OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
5038 SourceLocation StartLoc,
5039 SourceLocation LParenLoc,
5040 SourceLocation EndLoc) {
5041 Expr *ValExpr = NumThreads;
5042 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
Alexey Bataev568a8332014-03-06 06:15:19 +00005043 !NumThreads->containsUnexpandedParameterPack()) {
5044 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
5045 ExprResult Val =
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005046 PerformOpenMPImplicitIntegerConversion(NumThreadsLoc, NumThreads);
Alexey Bataev568a8332014-03-06 06:15:19 +00005047 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005048 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00005049
Nikola Smiljanic01a75982014-05-29 10:55:11 +00005050 ValExpr = Val.get();
Alexey Bataev568a8332014-03-06 06:15:19 +00005051
5052 // OpenMP [2.5, Restrictions]
5053 // The num_threads expression must evaluate to a positive integer value.
5054 llvm::APSInt Result;
Alexey Bataeved09d242014-05-28 05:53:51 +00005055 if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() &&
5056 !Result.isStrictlyPositive()) {
Alexey Bataev568a8332014-03-06 06:15:19 +00005057 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
5058 << "num_threads" << NumThreads->getSourceRange();
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005059 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00005060 }
5061 }
5062
Alexey Bataeved09d242014-05-28 05:53:51 +00005063 return new (Context)
5064 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00005065}
5066
Alexey Bataev62c87d22014-03-21 04:51:18 +00005067ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
5068 OpenMPClauseKind CKind) {
5069 if (!E)
5070 return ExprError();
5071 if (E->isValueDependent() || E->isTypeDependent() ||
5072 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00005073 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00005074 llvm::APSInt Result;
5075 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
5076 if (ICE.isInvalid())
5077 return ExprError();
5078 if (!Result.isStrictlyPositive()) {
5079 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
5080 << getOpenMPClauseName(CKind) << E->getSourceRange();
5081 return ExprError();
5082 }
Alexander Musman09184fe2014-09-30 05:29:28 +00005083 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
5084 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
5085 << E->getSourceRange();
5086 return ExprError();
5087 }
Alexey Bataev9c821032015-04-30 04:23:23 +00005088 if (CKind == OMPC_collapse) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00005089 DSAStack->setCollapseNumber(DSAStack->getCollapseNumber() - 1 +
5090 Result.getExtValue());
5091 } else if (CKind == OMPC_ordered) {
5092 DSAStack->setCollapseNumber(DSAStack->getCollapseNumber() - 1 +
5093 Result.getExtValue());
Alexey Bataev9c821032015-04-30 04:23:23 +00005094 }
Alexey Bataev62c87d22014-03-21 04:51:18 +00005095 return ICE;
5096}
5097
5098OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
5099 SourceLocation LParenLoc,
5100 SourceLocation EndLoc) {
5101 // OpenMP [2.8.1, simd construct, Description]
5102 // The parameter of the safelen clause must be a constant
5103 // positive integer expression.
5104 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
5105 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005106 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00005107 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00005108 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00005109}
5110
Alexey Bataev66b15b52015-08-21 11:14:16 +00005111OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
5112 SourceLocation LParenLoc,
5113 SourceLocation EndLoc) {
5114 // OpenMP [2.8.1, simd construct, Description]
5115 // The parameter of the simdlen clause must be a constant
5116 // positive integer expression.
5117 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
5118 if (Simdlen.isInvalid())
5119 return nullptr;
5120 return new (Context)
5121 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
5122}
5123
Alexander Musman64d33f12014-06-04 07:53:32 +00005124OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
5125 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00005126 SourceLocation LParenLoc,
5127 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00005128 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00005129 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00005130 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00005131 // The parameter of the collapse clause must be a constant
5132 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00005133 ExprResult NumForLoopsResult =
5134 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
5135 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00005136 return nullptr;
5137 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00005138 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00005139}
5140
Alexey Bataev10e775f2015-07-30 11:36:16 +00005141OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
5142 SourceLocation EndLoc,
5143 SourceLocation LParenLoc,
5144 Expr *NumForLoops) {
5145 DSAStack->setOrderedRegion();
5146 // OpenMP [2.7.1, loop construct, Description]
5147 // OpenMP [2.8.1, simd construct, Description]
5148 // OpenMP [2.9.6, distribute construct, Description]
5149 // The parameter of the ordered clause must be a constant
5150 // positive integer expression if any.
5151 if (NumForLoops && LParenLoc.isValid()) {
5152 ExprResult NumForLoopsResult =
5153 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
5154 if (NumForLoopsResult.isInvalid())
5155 return nullptr;
5156 NumForLoops = NumForLoopsResult.get();
5157 }
5158 return new (Context)
5159 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
5160}
5161
Alexey Bataeved09d242014-05-28 05:53:51 +00005162OMPClause *Sema::ActOnOpenMPSimpleClause(
5163 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
5164 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005165 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005166 switch (Kind) {
5167 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00005168 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00005169 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
5170 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005171 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005172 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00005173 Res = ActOnOpenMPProcBindClause(
5174 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
5175 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005176 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005177 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00005178 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00005179 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00005180 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00005181 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00005182 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005183 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005184 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005185 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00005186 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00005187 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00005188 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00005189 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00005190 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005191 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005192 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005193 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00005194 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005195 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005196 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005197 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00005198 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005199 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00005200 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00005201 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00005202 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005203 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005204 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00005205 case OMPC_device:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005206 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005207 llvm_unreachable("Clause is not allowed.");
5208 }
5209 return Res;
5210}
5211
5212OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
5213 SourceLocation KindKwLoc,
5214 SourceLocation StartLoc,
5215 SourceLocation LParenLoc,
5216 SourceLocation EndLoc) {
5217 if (Kind == OMPC_DEFAULT_unknown) {
5218 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005219 static_assert(OMPC_DEFAULT_unknown > 0,
5220 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00005221 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005222 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005223 Values += "'";
5224 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
5225 Values += "'";
5226 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005227 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005228 Values += " or ";
5229 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005230 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005231 break;
5232 default:
5233 Values += Sep;
5234 break;
5235 }
5236 }
5237 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00005238 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005239 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005240 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00005241 switch (Kind) {
5242 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005243 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005244 break;
5245 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005246 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005247 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005248 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005249 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00005250 break;
5251 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005252 return new (Context)
5253 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005254}
5255
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005256OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
5257 SourceLocation KindKwLoc,
5258 SourceLocation StartLoc,
5259 SourceLocation LParenLoc,
5260 SourceLocation EndLoc) {
5261 if (Kind == OMPC_PROC_BIND_unknown) {
5262 std::string Values;
5263 std::string Sep(", ");
5264 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
5265 Values += "'";
5266 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
5267 Values += "'";
5268 switch (i) {
5269 case OMPC_PROC_BIND_unknown - 2:
5270 Values += " or ";
5271 break;
5272 case OMPC_PROC_BIND_unknown - 1:
5273 break;
5274 default:
5275 Values += Sep;
5276 break;
5277 }
5278 }
5279 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00005280 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005281 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005282 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005283 return new (Context)
5284 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005285}
5286
Alexey Bataev56dafe82014-06-20 07:16:17 +00005287OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
5288 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
5289 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005290 SourceLocation ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00005291 SourceLocation EndLoc) {
5292 OMPClause *Res = nullptr;
5293 switch (Kind) {
5294 case OMPC_schedule:
5295 Res = ActOnOpenMPScheduleClause(
5296 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005297 LParenLoc, ArgumentLoc, DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00005298 break;
5299 case OMPC_if:
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005300 Res =
5301 ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument), Expr,
5302 StartLoc, LParenLoc, ArgumentLoc, DelimLoc, EndLoc);
5303 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00005304 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005305 case OMPC_num_threads:
5306 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00005307 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005308 case OMPC_collapse:
5309 case OMPC_default:
5310 case OMPC_proc_bind:
5311 case OMPC_private:
5312 case OMPC_firstprivate:
5313 case OMPC_lastprivate:
5314 case OMPC_shared:
5315 case OMPC_reduction:
5316 case OMPC_linear:
5317 case OMPC_aligned:
5318 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005319 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005320 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00005321 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005322 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005323 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005324 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00005325 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005326 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00005327 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00005328 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00005329 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005330 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005331 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00005332 case OMPC_device:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005333 case OMPC_unknown:
5334 llvm_unreachable("Clause is not allowed.");
5335 }
5336 return Res;
5337}
5338
5339OMPClause *Sema::ActOnOpenMPScheduleClause(
5340 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
5341 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
5342 SourceLocation EndLoc) {
5343 if (Kind == OMPC_SCHEDULE_unknown) {
5344 std::string Values;
5345 std::string Sep(", ");
5346 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) {
5347 Values += "'";
5348 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i);
5349 Values += "'";
5350 switch (i) {
5351 case OMPC_SCHEDULE_unknown - 2:
5352 Values += " or ";
5353 break;
5354 case OMPC_SCHEDULE_unknown - 1:
5355 break;
5356 default:
5357 Values += Sep;
5358 break;
5359 }
5360 }
5361 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
5362 << Values << getOpenMPClauseName(OMPC_schedule);
5363 return nullptr;
5364 }
5365 Expr *ValExpr = ChunkSize;
Alexey Bataev040d5402015-05-12 08:35:28 +00005366 Expr *HelperValExpr = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00005367 if (ChunkSize) {
5368 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
5369 !ChunkSize->isInstantiationDependent() &&
5370 !ChunkSize->containsUnexpandedParameterPack()) {
5371 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
5372 ExprResult Val =
5373 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
5374 if (Val.isInvalid())
5375 return nullptr;
5376
5377 ValExpr = Val.get();
5378
5379 // OpenMP [2.7.1, Restrictions]
5380 // chunk_size must be a loop invariant integer expression with a positive
5381 // value.
5382 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00005383 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
5384 if (Result.isSigned() && !Result.isStrictlyPositive()) {
5385 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
5386 << "schedule" << ChunkSize->getSourceRange();
5387 return nullptr;
5388 }
5389 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
5390 auto *ImpVar = buildVarDecl(*this, ChunkSize->getExprLoc(),
5391 ChunkSize->getType(), ".chunk.");
5392 auto *ImpVarRef = buildDeclRefExpr(*this, ImpVar, ChunkSize->getType(),
5393 ChunkSize->getExprLoc(),
5394 /*RefersToCapture=*/true);
5395 HelperValExpr = ImpVarRef;
Alexey Bataev56dafe82014-06-20 07:16:17 +00005396 }
5397 }
5398 }
5399
5400 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc,
Alexey Bataev040d5402015-05-12 08:35:28 +00005401 EndLoc, Kind, ValExpr, HelperValExpr);
Alexey Bataev56dafe82014-06-20 07:16:17 +00005402}
5403
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005404OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
5405 SourceLocation StartLoc,
5406 SourceLocation EndLoc) {
5407 OMPClause *Res = nullptr;
5408 switch (Kind) {
5409 case OMPC_ordered:
5410 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
5411 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00005412 case OMPC_nowait:
5413 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
5414 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005415 case OMPC_untied:
5416 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
5417 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005418 case OMPC_mergeable:
5419 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
5420 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005421 case OMPC_read:
5422 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
5423 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00005424 case OMPC_write:
5425 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
5426 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00005427 case OMPC_update:
5428 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
5429 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00005430 case OMPC_capture:
5431 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
5432 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005433 case OMPC_seq_cst:
5434 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
5435 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005436 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00005437 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005438 case OMPC_num_threads:
5439 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00005440 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005441 case OMPC_collapse:
5442 case OMPC_schedule:
5443 case OMPC_private:
5444 case OMPC_firstprivate:
5445 case OMPC_lastprivate:
5446 case OMPC_shared:
5447 case OMPC_reduction:
5448 case OMPC_linear:
5449 case OMPC_aligned:
5450 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005451 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005452 case OMPC_default:
5453 case OMPC_proc_bind:
5454 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00005455 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005456 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00005457 case OMPC_device:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005458 case OMPC_unknown:
5459 llvm_unreachable("Clause is not allowed.");
5460 }
5461 return Res;
5462}
5463
Alexey Bataev236070f2014-06-20 11:19:47 +00005464OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
5465 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00005466 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00005467 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
5468}
5469
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005470OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
5471 SourceLocation EndLoc) {
5472 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
5473}
5474
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005475OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
5476 SourceLocation EndLoc) {
5477 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
5478}
5479
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005480OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
5481 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005482 return new (Context) OMPReadClause(StartLoc, EndLoc);
5483}
5484
Alexey Bataevdea47612014-07-23 07:46:59 +00005485OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
5486 SourceLocation EndLoc) {
5487 return new (Context) OMPWriteClause(StartLoc, EndLoc);
5488}
5489
Alexey Bataev67a4f222014-07-23 10:25:33 +00005490OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
5491 SourceLocation EndLoc) {
5492 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
5493}
5494
Alexey Bataev459dec02014-07-24 06:46:57 +00005495OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
5496 SourceLocation EndLoc) {
5497 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
5498}
5499
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005500OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
5501 SourceLocation EndLoc) {
5502 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
5503}
5504
Alexey Bataevc5e02582014-06-16 07:08:35 +00005505OMPClause *Sema::ActOnOpenMPVarListClause(
5506 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
5507 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
5508 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005509 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Alexey Bataev182227b2015-08-20 10:54:39 +00005510 OpenMPLinearClauseKind LinKind, SourceLocation DepLinLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005511 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005512 switch (Kind) {
5513 case OMPC_private:
5514 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
5515 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005516 case OMPC_firstprivate:
5517 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
5518 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00005519 case OMPC_lastprivate:
5520 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
5521 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00005522 case OMPC_shared:
5523 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
5524 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005525 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00005526 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
5527 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00005528 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00005529 case OMPC_linear:
5530 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Alexey Bataev182227b2015-08-20 10:54:39 +00005531 LinKind, DepLinLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00005532 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00005533 case OMPC_aligned:
5534 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
5535 ColonLoc, EndLoc);
5536 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005537 case OMPC_copyin:
5538 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
5539 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00005540 case OMPC_copyprivate:
5541 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
5542 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00005543 case OMPC_flush:
5544 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
5545 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005546 case OMPC_depend:
Alexey Bataev182227b2015-08-20 10:54:39 +00005547 Res = ActOnOpenMPDependClause(DepKind, DepLinLoc, ColonLoc, VarList, StartLoc,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005548 LParenLoc, EndLoc);
5549 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005550 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00005551 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00005552 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00005553 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00005554 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00005555 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005556 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005557 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005558 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005559 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00005560 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005561 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005562 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005563 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005564 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00005565 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00005566 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00005567 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005568 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00005569 case OMPC_device:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005570 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005571 llvm_unreachable("Clause is not allowed.");
5572 }
5573 return Res;
5574}
5575
5576OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
5577 SourceLocation StartLoc,
5578 SourceLocation LParenLoc,
5579 SourceLocation EndLoc) {
5580 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00005581 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00005582 for (auto &RefExpr : VarList) {
5583 assert(RefExpr && "NULL expr in OpenMP private clause.");
5584 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005585 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00005586 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00005587 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005588 continue;
5589 }
5590
Alexey Bataeved09d242014-05-28 05:53:51 +00005591 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005592 // OpenMP [2.1, C/C++]
5593 // A list item is a variable name.
5594 // OpenMP [2.9.3.3, Restrictions, p.1]
5595 // A variable that is part of another variable (as an array or
5596 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00005597 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005598 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005599 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005600 continue;
5601 }
5602 Decl *D = DE->getDecl();
5603 VarDecl *VD = cast<VarDecl>(D);
5604
5605 QualType Type = VD->getType();
5606 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5607 // It will be analyzed later.
5608 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00005609 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005610 continue;
5611 }
5612
5613 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
5614 // A variable that appears in a private clause must not have an incomplete
5615 // type or a reference type.
5616 if (RequireCompleteType(ELoc, Type,
5617 diag::err_omp_private_incomplete_type)) {
5618 continue;
5619 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00005620 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005621
Alexey Bataev758e55e2013-09-06 18:03:48 +00005622 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
5623 // in a Construct]
5624 // Variables with the predetermined data-sharing attributes may not be
5625 // listed in data-sharing attributes clauses, except for the cases
5626 // listed below. For these exceptions only, listing a predetermined
5627 // variable in a data-sharing attribute clause is allowed and overrides
5628 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005629 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005630 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005631 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
5632 << getOpenMPClauseName(OMPC_private);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005633 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005634 continue;
5635 }
5636
Alexey Bataevccb59ec2015-05-19 08:44:56 +00005637 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00005638 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00005639 DSAStack->getCurrentDirective() == OMPD_task) {
5640 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
5641 << getOpenMPClauseName(OMPC_private) << Type
5642 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
5643 bool IsDecl =
5644 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5645 Diag(VD->getLocation(),
5646 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5647 << VD;
5648 continue;
5649 }
5650
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005651 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
5652 // A variable of class type (or array thereof) that appears in a private
5653 // clause requires an accessible, unambiguous default constructor for the
5654 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00005655 // Generate helper private variable and initialize it with the default
5656 // value. The address of the original variable is replaced by the address of
5657 // the new private variable in CodeGen. This new variable is not added to
5658 // IdResolver, so the code in the OpenMP region uses original variable for
5659 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005660 Type = Type.getUnqualifiedType();
5661 auto VDPrivate = buildVarDecl(*this, DE->getExprLoc(), Type, VD->getName());
Alexey Bataev39f915b82015-05-08 10:41:21 +00005662 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00005663 if (VDPrivate->isInvalidDecl())
5664 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005665 auto VDPrivateRefExpr = buildDeclRefExpr(
5666 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev03b340a2014-10-21 03:16:40 +00005667
Alexey Bataev758e55e2013-09-06 18:03:48 +00005668 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005669 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00005670 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005671 }
5672
Alexey Bataeved09d242014-05-28 05:53:51 +00005673 if (Vars.empty())
5674 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005675
Alexey Bataev03b340a2014-10-21 03:16:40 +00005676 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
5677 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005678}
5679
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005680namespace {
5681class DiagsUninitializedSeveretyRAII {
5682private:
5683 DiagnosticsEngine &Diags;
5684 SourceLocation SavedLoc;
5685 bool IsIgnored;
5686
5687public:
5688 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
5689 bool IsIgnored)
5690 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
5691 if (!IsIgnored) {
5692 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
5693 /*Map*/ diag::Severity::Ignored, Loc);
5694 }
5695 }
5696 ~DiagsUninitializedSeveretyRAII() {
5697 if (!IsIgnored)
5698 Diags.popMappings(SavedLoc);
5699 }
5700};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00005701}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005702
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005703OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
5704 SourceLocation StartLoc,
5705 SourceLocation LParenLoc,
5706 SourceLocation EndLoc) {
5707 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005708 SmallVector<Expr *, 8> PrivateCopies;
5709 SmallVector<Expr *, 8> Inits;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005710 bool IsImplicitClause =
5711 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
5712 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
5713
Alexey Bataeved09d242014-05-28 05:53:51 +00005714 for (auto &RefExpr : VarList) {
5715 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
5716 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005717 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00005718 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005719 PrivateCopies.push_back(nullptr);
5720 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005721 continue;
5722 }
5723
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005724 SourceLocation ELoc =
5725 IsImplicitClause ? ImplicitClauseLoc : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005726 // OpenMP [2.1, C/C++]
5727 // A list item is a variable name.
5728 // OpenMP [2.9.3.3, Restrictions, p.1]
5729 // A variable that is part of another variable (as an array or
5730 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00005731 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005732 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005733 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005734 continue;
5735 }
5736 Decl *D = DE->getDecl();
5737 VarDecl *VD = cast<VarDecl>(D);
5738
5739 QualType Type = VD->getType();
5740 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5741 // It will be analyzed later.
5742 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005743 PrivateCopies.push_back(nullptr);
5744 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005745 continue;
5746 }
5747
5748 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
5749 // A variable that appears in a private clause must not have an incomplete
5750 // type or a reference type.
5751 if (RequireCompleteType(ELoc, Type,
5752 diag::err_omp_firstprivate_incomplete_type)) {
5753 continue;
5754 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00005755 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005756
5757 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
5758 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00005759 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005760 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005761 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005762
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005763 // If an implicit firstprivate variable found it was checked already.
5764 if (!IsImplicitClause) {
5765 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005766 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005767 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
5768 // A list item that specifies a given variable may not appear in more
5769 // than one clause on the same directive, except that a variable may be
5770 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005771 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00005772 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005773 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00005774 << getOpenMPClauseName(DVar.CKind)
5775 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005776 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005777 continue;
5778 }
5779
5780 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
5781 // in a Construct]
5782 // Variables with the predetermined data-sharing attributes may not be
5783 // listed in data-sharing attributes clauses, except for the cases
5784 // listed below. For these exceptions only, listing a predetermined
5785 // variable in a data-sharing attribute clause is allowed and overrides
5786 // the variable's predetermined data-sharing attributes.
5787 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
5788 // in a Construct, C/C++, p.2]
5789 // Variables with const-qualified type having no mutable member may be
5790 // listed in a firstprivate clause, even if they are static data members.
5791 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
5792 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
5793 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00005794 << getOpenMPClauseName(DVar.CKind)
5795 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005796 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005797 continue;
5798 }
5799
Alexey Bataevf29276e2014-06-18 04:14:57 +00005800 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005801 // OpenMP [2.9.3.4, Restrictions, p.2]
5802 // A list item that is private within a parallel region must not appear
5803 // in a firstprivate clause on a worksharing construct if any of the
5804 // worksharing regions arising from the worksharing construct ever bind
5805 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00005806 if (isOpenMPWorksharingDirective(CurrDir) &&
5807 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005808 DVar = DSAStack->getImplicitDSA(VD, true);
5809 if (DVar.CKind != OMPC_shared &&
5810 (isOpenMPParallelDirective(DVar.DKind) ||
5811 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00005812 Diag(ELoc, diag::err_omp_required_access)
5813 << getOpenMPClauseName(OMPC_firstprivate)
5814 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005815 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005816 continue;
5817 }
5818 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005819 // OpenMP [2.9.3.4, Restrictions, p.3]
5820 // A list item that appears in a reduction clause of a parallel construct
5821 // must not appear in a firstprivate clause on a worksharing or task
5822 // construct if any of the worksharing or task regions arising from the
5823 // worksharing or task construct ever bind to any of the parallel regions
5824 // arising from the parallel construct.
5825 // OpenMP [2.9.3.4, Restrictions, p.4]
5826 // A list item that appears in a reduction clause in worksharing
5827 // construct must not appear in a firstprivate clause in a task construct
5828 // encountered during execution of any of the worksharing regions arising
5829 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005830 if (CurrDir == OMPD_task) {
5831 DVar =
5832 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
5833 [](OpenMPDirectiveKind K) -> bool {
5834 return isOpenMPParallelDirective(K) ||
5835 isOpenMPWorksharingDirective(K);
5836 },
5837 false);
5838 if (DVar.CKind == OMPC_reduction &&
5839 (isOpenMPParallelDirective(DVar.DKind) ||
5840 isOpenMPWorksharingDirective(DVar.DKind))) {
5841 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
5842 << getOpenMPDirectiveName(DVar.DKind);
5843 ReportOriginalDSA(*this, DSAStack, VD, DVar);
5844 continue;
5845 }
5846 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005847 }
5848
Alexey Bataevccb59ec2015-05-19 08:44:56 +00005849 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00005850 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00005851 DSAStack->getCurrentDirective() == OMPD_task) {
5852 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
5853 << getOpenMPClauseName(OMPC_firstprivate) << Type
5854 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
5855 bool IsDecl =
5856 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5857 Diag(VD->getLocation(),
5858 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5859 << VD;
5860 continue;
5861 }
5862
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005863 Type = Type.getUnqualifiedType();
5864 auto VDPrivate = buildVarDecl(*this, ELoc, Type, VD->getName());
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005865 // Generate helper private variable and initialize it with the value of the
5866 // original variable. The address of the original variable is replaced by
5867 // the address of the new private variable in the CodeGen. This new variable
5868 // is not added to IdResolver, so the code in the OpenMP region uses
5869 // original variable for proper diagnostics and variable capturing.
5870 Expr *VDInitRefExpr = nullptr;
5871 // For arrays generate initializer for single element and replace it by the
5872 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005873 if (Type->isArrayType()) {
5874 auto VDInit =
5875 buildVarDecl(*this, DE->getExprLoc(), ElemType, VD->getName());
5876 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005877 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005878 ElemType = ElemType.getUnqualifiedType();
5879 auto *VDInitTemp = buildVarDecl(*this, DE->getLocStart(), ElemType,
5880 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00005881 InitializedEntity Entity =
5882 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005883 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
5884
5885 InitializationSequence InitSeq(*this, Entity, Kind, Init);
5886 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
5887 if (Result.isInvalid())
5888 VDPrivate->setInvalidDecl();
5889 else
5890 VDPrivate->setInit(Result.getAs<Expr>());
5891 } else {
Alexey Bataev69c62a92015-04-15 04:52:20 +00005892 auto *VDInit =
Alexey Bataev39f915b82015-05-08 10:41:21 +00005893 buildVarDecl(*this, DE->getLocStart(), Type, ".firstprivate.temp");
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005894 VDInitRefExpr =
5895 buildDeclRefExpr(*this, VDInit, DE->getType(), DE->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00005896 AddInitializerToDecl(VDPrivate,
5897 DefaultLvalueConversion(VDInitRefExpr).get(),
5898 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005899 }
5900 if (VDPrivate->isInvalidDecl()) {
5901 if (IsImplicitClause) {
5902 Diag(DE->getExprLoc(),
5903 diag::note_omp_task_predetermined_firstprivate_here);
5904 }
5905 continue;
5906 }
5907 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00005908 auto VDPrivateRefExpr = buildDeclRefExpr(
5909 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005910 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
5911 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005912 PrivateCopies.push_back(VDPrivateRefExpr);
5913 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005914 }
5915
Alexey Bataeved09d242014-05-28 05:53:51 +00005916 if (Vars.empty())
5917 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005918
5919 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005920 Vars, PrivateCopies, Inits);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005921}
5922
Alexander Musman1bb328c2014-06-04 13:06:39 +00005923OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
5924 SourceLocation StartLoc,
5925 SourceLocation LParenLoc,
5926 SourceLocation EndLoc) {
5927 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00005928 SmallVector<Expr *, 8> SrcExprs;
5929 SmallVector<Expr *, 8> DstExprs;
5930 SmallVector<Expr *, 8> AssignmentOps;
Alexander Musman1bb328c2014-06-04 13:06:39 +00005931 for (auto &RefExpr : VarList) {
5932 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
5933 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
5934 // It will be analyzed later.
5935 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00005936 SrcExprs.push_back(nullptr);
5937 DstExprs.push_back(nullptr);
5938 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005939 continue;
5940 }
5941
5942 SourceLocation ELoc = RefExpr->getExprLoc();
5943 // OpenMP [2.1, C/C++]
5944 // A list item is a variable name.
5945 // OpenMP [2.14.3.5, Restrictions, p.1]
5946 // A variable that is part of another variable (as an array or structure
5947 // element) cannot appear in a lastprivate clause.
5948 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
5949 if (!DE || !isa<VarDecl>(DE->getDecl())) {
5950 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
5951 continue;
5952 }
5953 Decl *D = DE->getDecl();
5954 VarDecl *VD = cast<VarDecl>(D);
5955
5956 QualType Type = VD->getType();
5957 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5958 // It will be analyzed later.
5959 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00005960 SrcExprs.push_back(nullptr);
5961 DstExprs.push_back(nullptr);
5962 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005963 continue;
5964 }
5965
5966 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
5967 // A variable that appears in a lastprivate clause must not have an
5968 // incomplete type or a reference type.
5969 if (RequireCompleteType(ELoc, Type,
5970 diag::err_omp_lastprivate_incomplete_type)) {
5971 continue;
5972 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00005973 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00005974
5975 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
5976 // in a Construct]
5977 // Variables with the predetermined data-sharing attributes may not be
5978 // listed in data-sharing attributes clauses, except for the cases
5979 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005980 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005981 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
5982 DVar.CKind != OMPC_firstprivate &&
5983 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
5984 Diag(ELoc, diag::err_omp_wrong_dsa)
5985 << getOpenMPClauseName(DVar.CKind)
5986 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005987 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005988 continue;
5989 }
5990
Alexey Bataevf29276e2014-06-18 04:14:57 +00005991 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
5992 // OpenMP [2.14.3.5, Restrictions, p.2]
5993 // A list item that is private within a parallel region, or that appears in
5994 // the reduction clause of a parallel construct, must not appear in a
5995 // lastprivate clause on a worksharing construct if any of the corresponding
5996 // worksharing regions ever binds to any of the corresponding parallel
5997 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005998 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00005999 if (isOpenMPWorksharingDirective(CurrDir) &&
6000 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006001 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006002 if (DVar.CKind != OMPC_shared) {
6003 Diag(ELoc, diag::err_omp_required_access)
6004 << getOpenMPClauseName(OMPC_lastprivate)
6005 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006006 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006007 continue;
6008 }
6009 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00006010 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00006011 // A variable of class type (or array thereof) that appears in a
6012 // lastprivate clause requires an accessible, unambiguous default
6013 // constructor for the class type, unless the list item is also specified
6014 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00006015 // A variable of class type (or array thereof) that appears in a
6016 // lastprivate clause requires an accessible, unambiguous copy assignment
6017 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00006018 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00006019 auto *SrcVD = buildVarDecl(*this, DE->getLocStart(),
Alexey Bataev38e89532015-04-16 04:54:05 +00006020 Type.getUnqualifiedType(), ".lastprivate.src");
Alexey Bataev39f915b82015-05-08 10:41:21 +00006021 auto *PseudoSrcExpr = buildDeclRefExpr(
6022 *this, SrcVD, Type.getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00006023 auto *DstVD =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006024 buildVarDecl(*this, DE->getLocStart(), Type, ".lastprivate.dst");
Alexey Bataev38e89532015-04-16 04:54:05 +00006025 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006026 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00006027 // For arrays generate assignment operation for single element and replace
6028 // it by the original array element in CodeGen.
6029 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
6030 PseudoDstExpr, PseudoSrcExpr);
6031 if (AssignmentOp.isInvalid())
6032 continue;
6033 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
6034 /*DiscardedValue=*/true);
6035 if (AssignmentOp.isInvalid())
6036 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00006037
Alexey Bataev39f915b82015-05-08 10:41:21 +00006038 if (TopDVar.CKind != OMPC_firstprivate)
Alexey Bataevf29276e2014-06-18 04:14:57 +00006039 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006040 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00006041 SrcExprs.push_back(PseudoSrcExpr);
6042 DstExprs.push_back(PseudoDstExpr);
6043 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00006044 }
6045
6046 if (Vars.empty())
6047 return nullptr;
6048
6049 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00006050 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006051}
6052
Alexey Bataev758e55e2013-09-06 18:03:48 +00006053OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
6054 SourceLocation StartLoc,
6055 SourceLocation LParenLoc,
6056 SourceLocation EndLoc) {
6057 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00006058 for (auto &RefExpr : VarList) {
6059 assert(RefExpr && "NULL expr in OpenMP shared clause.");
6060 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00006061 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006062 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006063 continue;
6064 }
6065
Alexey Bataeved09d242014-05-28 05:53:51 +00006066 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00006067 // OpenMP [2.1, C/C++]
6068 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00006069 // OpenMP [2.14.3.2, Restrictions, p.1]
6070 // A variable that is part of another variable (as an array or structure
6071 // element) cannot appear in a shared unless it is a static data member
6072 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00006073 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006074 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006075 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00006076 continue;
6077 }
6078 Decl *D = DE->getDecl();
6079 VarDecl *VD = cast<VarDecl>(D);
6080
6081 QualType Type = VD->getType();
6082 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6083 // It will be analyzed later.
6084 Vars.push_back(DE);
6085 continue;
6086 }
6087
6088 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6089 // in a Construct]
6090 // Variables with the predetermined data-sharing attributes may not be
6091 // listed in data-sharing attributes clauses, except for the cases
6092 // listed below. For these exceptions only, listing a predetermined
6093 // variable in a data-sharing attribute clause is allowed and overrides
6094 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006095 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00006096 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
6097 DVar.RefExpr) {
6098 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
6099 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006100 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006101 continue;
6102 }
6103
6104 DSAStack->addDSA(VD, DE, OMPC_shared);
6105 Vars.push_back(DE);
6106 }
6107
Alexey Bataeved09d242014-05-28 05:53:51 +00006108 if (Vars.empty())
6109 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00006110
6111 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
6112}
6113
Alexey Bataevc5e02582014-06-16 07:08:35 +00006114namespace {
6115class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
6116 DSAStackTy *Stack;
6117
6118public:
6119 bool VisitDeclRefExpr(DeclRefExpr *E) {
6120 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006121 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006122 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
6123 return false;
6124 if (DVar.CKind != OMPC_unknown)
6125 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00006126 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006127 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006128 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00006129 return true;
6130 return false;
6131 }
6132 return false;
6133 }
6134 bool VisitStmt(Stmt *S) {
6135 for (auto Child : S->children()) {
6136 if (Child && Visit(Child))
6137 return true;
6138 }
6139 return false;
6140 }
Alexey Bataev23b69422014-06-18 07:08:49 +00006141 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00006142};
Alexey Bataev23b69422014-06-18 07:08:49 +00006143} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00006144
6145OMPClause *Sema::ActOnOpenMPReductionClause(
6146 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
6147 SourceLocation ColonLoc, SourceLocation EndLoc,
6148 CXXScopeSpec &ReductionIdScopeSpec,
6149 const DeclarationNameInfo &ReductionId) {
6150 // TODO: Allow scope specification search when 'declare reduction' is
6151 // supported.
6152 assert(ReductionIdScopeSpec.isEmpty() &&
6153 "No support for scoped reduction identifiers yet.");
6154
6155 auto DN = ReductionId.getName();
6156 auto OOK = DN.getCXXOverloadedOperator();
6157 BinaryOperatorKind BOK = BO_Comma;
6158
6159 // OpenMP [2.14.3.6, reduction clause]
6160 // C
6161 // reduction-identifier is either an identifier or one of the following
6162 // operators: +, -, *, &, |, ^, && and ||
6163 // C++
6164 // reduction-identifier is either an id-expression or one of the following
6165 // operators: +, -, *, &, |, ^, && and ||
6166 // FIXME: Only 'min' and 'max' identifiers are supported for now.
6167 switch (OOK) {
6168 case OO_Plus:
6169 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006170 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006171 break;
6172 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006173 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006174 break;
6175 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006176 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006177 break;
6178 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006179 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006180 break;
6181 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006182 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006183 break;
6184 case OO_AmpAmp:
6185 BOK = BO_LAnd;
6186 break;
6187 case OO_PipePipe:
6188 BOK = BO_LOr;
6189 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006190 case OO_New:
6191 case OO_Delete:
6192 case OO_Array_New:
6193 case OO_Array_Delete:
6194 case OO_Slash:
6195 case OO_Percent:
6196 case OO_Tilde:
6197 case OO_Exclaim:
6198 case OO_Equal:
6199 case OO_Less:
6200 case OO_Greater:
6201 case OO_LessEqual:
6202 case OO_GreaterEqual:
6203 case OO_PlusEqual:
6204 case OO_MinusEqual:
6205 case OO_StarEqual:
6206 case OO_SlashEqual:
6207 case OO_PercentEqual:
6208 case OO_CaretEqual:
6209 case OO_AmpEqual:
6210 case OO_PipeEqual:
6211 case OO_LessLess:
6212 case OO_GreaterGreater:
6213 case OO_LessLessEqual:
6214 case OO_GreaterGreaterEqual:
6215 case OO_EqualEqual:
6216 case OO_ExclaimEqual:
6217 case OO_PlusPlus:
6218 case OO_MinusMinus:
6219 case OO_Comma:
6220 case OO_ArrowStar:
6221 case OO_Arrow:
6222 case OO_Call:
6223 case OO_Subscript:
6224 case OO_Conditional:
6225 case NUM_OVERLOADED_OPERATORS:
6226 llvm_unreachable("Unexpected reduction identifier");
6227 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006228 if (auto II = DN.getAsIdentifierInfo()) {
6229 if (II->isStr("max"))
6230 BOK = BO_GT;
6231 else if (II->isStr("min"))
6232 BOK = BO_LT;
6233 }
6234 break;
6235 }
6236 SourceRange ReductionIdRange;
6237 if (ReductionIdScopeSpec.isValid()) {
6238 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
6239 }
6240 ReductionIdRange.setEnd(ReductionId.getEndLoc());
6241 if (BOK == BO_Comma) {
6242 // Not allowed reduction identifier is found.
6243 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
6244 << ReductionIdRange;
6245 return nullptr;
6246 }
6247
6248 SmallVector<Expr *, 8> Vars;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006249 SmallVector<Expr *, 8> LHSs;
6250 SmallVector<Expr *, 8> RHSs;
6251 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006252 for (auto RefExpr : VarList) {
6253 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
6254 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6255 // It will be analyzed later.
6256 Vars.push_back(RefExpr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006257 LHSs.push_back(nullptr);
6258 RHSs.push_back(nullptr);
6259 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006260 continue;
6261 }
6262
6263 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
6264 RefExpr->isInstantiationDependent() ||
6265 RefExpr->containsUnexpandedParameterPack()) {
6266 // It will be analyzed later.
6267 Vars.push_back(RefExpr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006268 LHSs.push_back(nullptr);
6269 RHSs.push_back(nullptr);
6270 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006271 continue;
6272 }
6273
6274 auto ELoc = RefExpr->getExprLoc();
6275 auto ERange = RefExpr->getSourceRange();
6276 // OpenMP [2.1, C/C++]
6277 // A list item is a variable or array section, subject to the restrictions
6278 // specified in Section 2.4 on page 42 and in each of the sections
6279 // describing clauses and directives for which a list appears.
6280 // OpenMP [2.14.3.3, Restrictions, p.1]
6281 // A variable that is part of another variable (as an array or
6282 // structure element) cannot appear in a private clause.
6283 auto DE = dyn_cast<DeclRefExpr>(RefExpr);
6284 if (!DE || !isa<VarDecl>(DE->getDecl())) {
6285 Diag(ELoc, diag::err_omp_expected_var_name) << ERange;
6286 continue;
6287 }
6288 auto D = DE->getDecl();
6289 auto VD = cast<VarDecl>(D);
6290 auto Type = VD->getType();
6291 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
6292 // A variable that appears in a private clause must not have an incomplete
6293 // type or a reference type.
6294 if (RequireCompleteType(ELoc, Type,
6295 diag::err_omp_reduction_incomplete_type))
6296 continue;
6297 // OpenMP [2.14.3.6, reduction clause, Restrictions]
6298 // Arrays may not appear in a reduction clause.
6299 if (Type.getNonReferenceType()->isArrayType()) {
6300 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
6301 bool IsDecl =
6302 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6303 Diag(VD->getLocation(),
6304 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6305 << VD;
6306 continue;
6307 }
6308 // OpenMP [2.14.3.6, reduction clause, Restrictions]
6309 // A list item that appears in a reduction clause must not be
6310 // const-qualified.
6311 if (Type.getNonReferenceType().isConstant(Context)) {
6312 Diag(ELoc, diag::err_omp_const_variable)
6313 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
6314 bool IsDecl =
6315 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6316 Diag(VD->getLocation(),
6317 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6318 << VD;
6319 continue;
6320 }
6321 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
6322 // If a list-item is a reference type then it must bind to the same object
6323 // for all threads of the team.
6324 VarDecl *VDDef = VD->getDefinition();
6325 if (Type->isReferenceType() && VDDef) {
6326 DSARefChecker Check(DSAStack);
6327 if (Check.Visit(VDDef->getInit())) {
6328 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
6329 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
6330 continue;
6331 }
6332 }
6333 // OpenMP [2.14.3.6, reduction clause, Restrictions]
6334 // The type of a list item that appears in a reduction clause must be valid
6335 // for the reduction-identifier. For a max or min reduction in C, the type
6336 // of the list item must be an allowed arithmetic data type: char, int,
6337 // float, double, or _Bool, possibly modified with long, short, signed, or
6338 // unsigned. For a max or min reduction in C++, the type of the list item
6339 // must be an allowed arithmetic data type: char, wchar_t, int, float,
6340 // double, or bool, possibly modified with long, short, signed, or unsigned.
6341 if ((BOK == BO_GT || BOK == BO_LT) &&
6342 !(Type->isScalarType() ||
6343 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
6344 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
6345 << getLangOpts().CPlusPlus;
6346 bool IsDecl =
6347 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6348 Diag(VD->getLocation(),
6349 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6350 << VD;
6351 continue;
6352 }
6353 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
6354 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
6355 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
6356 bool IsDecl =
6357 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6358 Diag(VD->getLocation(),
6359 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6360 << VD;
6361 continue;
6362 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00006363 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
6364 // in a Construct]
6365 // Variables with the predetermined data-sharing attributes may not be
6366 // listed in data-sharing attributes clauses, except for the cases
6367 // listed below. For these exceptions only, listing a predetermined
6368 // variable in a data-sharing attribute clause is allowed and overrides
6369 // the variable's predetermined data-sharing attributes.
6370 // OpenMP [2.14.3.6, Restrictions, p.3]
6371 // Any number of reduction clauses can be specified on the directive,
6372 // but a list item can appear only once in the reduction clauses for that
6373 // directive.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006374 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006375 if (DVar.CKind == OMPC_reduction) {
6376 Diag(ELoc, diag::err_omp_once_referenced)
6377 << getOpenMPClauseName(OMPC_reduction);
6378 if (DVar.RefExpr) {
6379 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
6380 }
6381 } else if (DVar.CKind != OMPC_unknown) {
6382 Diag(ELoc, diag::err_omp_wrong_dsa)
6383 << getOpenMPClauseName(DVar.CKind)
6384 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006385 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006386 continue;
6387 }
6388
6389 // OpenMP [2.14.3.6, Restrictions, p.1]
6390 // A list item that appears in a reduction clause of a worksharing
6391 // construct must be shared in the parallel regions to which any of the
6392 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf29276e2014-06-18 04:14:57 +00006393 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataev549210e2014-06-24 04:39:47 +00006394 if (isOpenMPWorksharingDirective(CurrDir) &&
6395 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006396 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006397 if (DVar.CKind != OMPC_shared) {
6398 Diag(ELoc, diag::err_omp_required_access)
6399 << getOpenMPClauseName(OMPC_reduction)
6400 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006401 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006402 continue;
6403 }
6404 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006405 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00006406 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs");
6407 auto *RHSVD = buildVarDecl(*this, ELoc, Type, VD->getName());
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006408 // Add initializer for private variable.
6409 Expr *Init = nullptr;
6410 switch (BOK) {
6411 case BO_Add:
6412 case BO_Xor:
6413 case BO_Or:
6414 case BO_LOr:
6415 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
6416 if (Type->isScalarType() || Type->isAnyComplexType()) {
6417 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00006418 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006419 break;
6420 case BO_Mul:
6421 case BO_LAnd:
6422 if (Type->isScalarType() || Type->isAnyComplexType()) {
6423 // '*' and '&&' reduction ops - initializer is '1'.
6424 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
6425 }
6426 break;
6427 case BO_And: {
6428 // '&' reduction op - initializer is '~0'.
6429 QualType OrigType = Type;
6430 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) {
6431 Type = ComplexTy->getElementType();
6432 }
6433 if (Type->isRealFloatingType()) {
6434 llvm::APFloat InitValue =
6435 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
6436 /*isIEEE=*/true);
6437 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
6438 Type, ELoc);
6439 } else if (Type->isScalarType()) {
6440 auto Size = Context.getTypeSize(Type);
6441 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
6442 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
6443 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
6444 }
6445 if (Init && OrigType->isAnyComplexType()) {
6446 // Init = 0xFFFF + 0xFFFFi;
6447 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
6448 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
6449 }
6450 Type = OrigType;
6451 break;
6452 }
6453 case BO_LT:
6454 case BO_GT: {
6455 // 'min' reduction op - initializer is 'Largest representable number in
6456 // the reduction list item type'.
6457 // 'max' reduction op - initializer is 'Least representable number in
6458 // the reduction list item type'.
6459 if (Type->isIntegerType() || Type->isPointerType()) {
6460 bool IsSigned = Type->hasSignedIntegerRepresentation();
6461 auto Size = Context.getTypeSize(Type);
6462 QualType IntTy =
6463 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
6464 llvm::APInt InitValue =
6465 (BOK != BO_LT)
6466 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
6467 : llvm::APInt::getMinValue(Size)
6468 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
6469 : llvm::APInt::getMaxValue(Size);
6470 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
6471 if (Type->isPointerType()) {
6472 // Cast to pointer type.
6473 auto CastExpr = BuildCStyleCastExpr(
6474 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
6475 SourceLocation(), Init);
6476 if (CastExpr.isInvalid())
6477 continue;
6478 Init = CastExpr.get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00006479 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006480 } else if (Type->isRealFloatingType()) {
6481 llvm::APFloat InitValue = llvm::APFloat::getLargest(
6482 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
6483 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
6484 Type, ELoc);
6485 }
6486 break;
6487 }
6488 case BO_PtrMemD:
6489 case BO_PtrMemI:
6490 case BO_MulAssign:
6491 case BO_Div:
6492 case BO_Rem:
6493 case BO_Sub:
6494 case BO_Shl:
6495 case BO_Shr:
6496 case BO_LE:
6497 case BO_GE:
6498 case BO_EQ:
6499 case BO_NE:
6500 case BO_AndAssign:
6501 case BO_XorAssign:
6502 case BO_OrAssign:
6503 case BO_Assign:
6504 case BO_AddAssign:
6505 case BO_SubAssign:
6506 case BO_DivAssign:
6507 case BO_RemAssign:
6508 case BO_ShlAssign:
6509 case BO_ShrAssign:
6510 case BO_Comma:
6511 llvm_unreachable("Unexpected reduction operation");
6512 }
6513 if (Init) {
6514 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
6515 /*TypeMayContainAuto=*/false);
6516 } else {
6517 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
6518 }
6519 if (!RHSVD->hasInit()) {
6520 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
6521 << ReductionIdRange;
6522 bool IsDecl =
6523 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6524 Diag(VD->getLocation(),
6525 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6526 << VD;
6527 continue;
6528 }
Alexey Bataev39f915b82015-05-08 10:41:21 +00006529 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
6530 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006531 ExprResult ReductionOp =
6532 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
6533 LHSDRE, RHSDRE);
6534 if (ReductionOp.isUsable()) {
Alexey Bataev69a47792015-05-07 03:54:03 +00006535 if (BOK != BO_LT && BOK != BO_GT) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006536 ReductionOp =
6537 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
6538 BO_Assign, LHSDRE, ReductionOp.get());
6539 } else {
6540 auto *ConditionalOp = new (Context) ConditionalOperator(
6541 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
6542 RHSDRE, Type, VK_LValue, OK_Ordinary);
6543 ReductionOp =
6544 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
6545 BO_Assign, LHSDRE, ConditionalOp);
6546 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006547 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00006548 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006549 if (ReductionOp.isInvalid())
6550 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006551
6552 DSAStack->addDSA(VD, DE, OMPC_reduction);
6553 Vars.push_back(DE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006554 LHSs.push_back(LHSDRE);
6555 RHSs.push_back(RHSDRE);
6556 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00006557 }
6558
6559 if (Vars.empty())
6560 return nullptr;
6561
6562 return OMPReductionClause::Create(
6563 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006564 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, LHSs,
6565 RHSs, ReductionOps);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006566}
6567
Alexey Bataev182227b2015-08-20 10:54:39 +00006568OMPClause *Sema::ActOnOpenMPLinearClause(
6569 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
6570 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
6571 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00006572 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006573 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00006574 SmallVector<Expr *, 8> Inits;
Alexey Bataev182227b2015-08-20 10:54:39 +00006575 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
6576 LinKind == OMPC_LINEAR_unknown) {
6577 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
6578 LinKind = OMPC_LINEAR_val;
6579 }
Alexey Bataeved09d242014-05-28 05:53:51 +00006580 for (auto &RefExpr : VarList) {
6581 assert(RefExpr && "NULL expr in OpenMP linear clause.");
6582 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00006583 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006584 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006585 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00006586 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00006587 continue;
6588 }
6589
6590 // OpenMP [2.14.3.7, linear clause]
6591 // A list item that appears in a linear clause is subject to the private
6592 // clause semantics described in Section 2.14.3.3 on page 159 except as
6593 // noted. In addition, the value of the new list item on each iteration
6594 // of the associated loop(s) corresponds to the value of the original
6595 // list item before entering the construct plus the logical number of
6596 // the iteration times linear-step.
6597
Alexey Bataeved09d242014-05-28 05:53:51 +00006598 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00006599 // OpenMP [2.1, C/C++]
6600 // A list item is a variable name.
6601 // OpenMP [2.14.3.3, Restrictions, p.1]
6602 // A variable that is part of another variable (as an array or
6603 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00006604 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00006605 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006606 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00006607 continue;
6608 }
6609
6610 VarDecl *VD = cast<VarDecl>(DE->getDecl());
6611
6612 // OpenMP [2.14.3.7, linear clause]
6613 // A list-item cannot appear in more than one linear clause.
6614 // A list-item that appears in a linear clause cannot appear in any
6615 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006616 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00006617 if (DVar.RefExpr) {
6618 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
6619 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006620 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00006621 continue;
6622 }
6623
6624 QualType QType = VD->getType();
6625 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
6626 // It will be analyzed later.
6627 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006628 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00006629 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00006630 continue;
6631 }
6632
6633 // A variable must not have an incomplete type or a reference type.
6634 if (RequireCompleteType(ELoc, QType,
6635 diag::err_omp_linear_incomplete_type)) {
6636 continue;
6637 }
Alexey Bataev1185e192015-08-20 12:15:57 +00006638 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
6639 !QType->isReferenceType()) {
6640 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
6641 << QType << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
6642 continue;
6643 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006644 QType = QType.getNonReferenceType();
Alexander Musman8dba6642014-04-22 13:09:42 +00006645
6646 // A list item must not be const-qualified.
6647 if (QType.isConstant(Context)) {
6648 Diag(ELoc, diag::err_omp_const_variable)
6649 << getOpenMPClauseName(OMPC_linear);
6650 bool IsDecl =
6651 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6652 Diag(VD->getLocation(),
6653 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6654 << VD;
6655 continue;
6656 }
6657
6658 // A list item must be of integral or pointer type.
6659 QType = QType.getUnqualifiedType().getCanonicalType();
6660 const Type *Ty = QType.getTypePtrOrNull();
6661 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
6662 !Ty->isPointerType())) {
6663 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
6664 bool IsDecl =
6665 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6666 Diag(VD->getLocation(),
6667 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6668 << VD;
6669 continue;
6670 }
6671
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006672 // Build private copy of original var.
6673 auto *Private = buildVarDecl(*this, ELoc, QType, VD->getName());
6674 auto *PrivateRef = buildDeclRefExpr(
6675 *this, Private, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman3276a272015-03-21 10:12:56 +00006676 // Build var to save initial value.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006677 VarDecl *Init = buildVarDecl(*this, ELoc, QType, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00006678 Expr *InitExpr;
6679 if (LinKind == OMPC_LINEAR_uval)
6680 InitExpr = VD->getInit();
6681 else
6682 InitExpr = DE;
6683 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexander Musman3276a272015-03-21 10:12:56 +00006684 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006685 auto InitRef = buildDeclRefExpr(
6686 *this, Init, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman8dba6642014-04-22 13:09:42 +00006687 DSAStack->addDSA(VD, DE, OMPC_linear);
6688 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006689 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00006690 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00006691 }
6692
6693 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006694 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00006695
6696 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00006697 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00006698 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
6699 !Step->isInstantiationDependent() &&
6700 !Step->containsUnexpandedParameterPack()) {
6701 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006702 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00006703 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006704 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006705 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00006706
Alexander Musman3276a272015-03-21 10:12:56 +00006707 // Build var to save the step value.
6708 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006709 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00006710 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006711 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00006712 ExprResult CalcStep =
6713 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006714 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00006715
Alexander Musman8dba6642014-04-22 13:09:42 +00006716 // Warn about zero linear step (it would be probably better specified as
6717 // making corresponding variables 'const').
6718 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00006719 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
6720 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00006721 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
6722 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00006723 if (!IsConstant && CalcStep.isUsable()) {
6724 // Calculate the step beforehand instead of doing this on each iteration.
6725 // (This is not used if the number of iterations may be kfold-ed).
6726 CalcStepExpr = CalcStep.get();
6727 }
Alexander Musman8dba6642014-04-22 13:09:42 +00006728 }
6729
Alexey Bataev182227b2015-08-20 10:54:39 +00006730 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
6731 ColonLoc, EndLoc, Vars, Privates, Inits,
6732 StepExpr, CalcStepExpr);
Alexander Musman3276a272015-03-21 10:12:56 +00006733}
6734
6735static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
6736 Expr *NumIterations, Sema &SemaRef,
6737 Scope *S) {
6738 // Walk the vars and build update/final expressions for the CodeGen.
6739 SmallVector<Expr *, 8> Updates;
6740 SmallVector<Expr *, 8> Finals;
6741 Expr *Step = Clause.getStep();
6742 Expr *CalcStep = Clause.getCalcStep();
6743 // OpenMP [2.14.3.7, linear clause]
6744 // If linear-step is not specified it is assumed to be 1.
6745 if (Step == nullptr)
6746 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
6747 else if (CalcStep)
6748 Step = cast<BinaryOperator>(CalcStep)->getLHS();
6749 bool HasErrors = false;
6750 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006751 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00006752 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00006753 for (auto &RefExpr : Clause.varlists()) {
6754 Expr *InitExpr = *CurInit;
6755
6756 // Build privatized reference to the current linear var.
6757 auto DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00006758 Expr *CapturedRef;
6759 if (LinKind == OMPC_LINEAR_uval)
6760 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
6761 else
6762 CapturedRef =
6763 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
6764 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
6765 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00006766
6767 // Build update: Var = InitExpr + IV * Step
6768 ExprResult Update =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006769 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
Alexander Musman3276a272015-03-21 10:12:56 +00006770 InitExpr, IV, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006771 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
6772 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00006773
6774 // Build final: Var = InitExpr + NumIterations * Step
6775 ExprResult Final =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006776 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
Alexey Bataev39f915b82015-05-08 10:41:21 +00006777 InitExpr, NumIterations, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006778 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
6779 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00006780 if (!Update.isUsable() || !Final.isUsable()) {
6781 Updates.push_back(nullptr);
6782 Finals.push_back(nullptr);
6783 HasErrors = true;
6784 } else {
6785 Updates.push_back(Update.get());
6786 Finals.push_back(Final.get());
6787 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006788 ++CurInit, ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00006789 }
6790 Clause.setUpdates(Updates);
6791 Clause.setFinals(Finals);
6792 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00006793}
6794
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006795OMPClause *Sema::ActOnOpenMPAlignedClause(
6796 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
6797 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
6798
6799 SmallVector<Expr *, 8> Vars;
6800 for (auto &RefExpr : VarList) {
6801 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
6802 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6803 // It will be analyzed later.
6804 Vars.push_back(RefExpr);
6805 continue;
6806 }
6807
6808 SourceLocation ELoc = RefExpr->getExprLoc();
6809 // OpenMP [2.1, C/C++]
6810 // A list item is a variable name.
6811 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
6812 if (!DE || !isa<VarDecl>(DE->getDecl())) {
6813 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
6814 continue;
6815 }
6816
6817 VarDecl *VD = cast<VarDecl>(DE->getDecl());
6818
6819 // OpenMP [2.8.1, simd construct, Restrictions]
6820 // The type of list items appearing in the aligned clause must be
6821 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006822 QualType QType = VD->getType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006823 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006824 const Type *Ty = QType.getTypePtrOrNull();
6825 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
6826 !Ty->isPointerType())) {
6827 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
6828 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
6829 bool IsDecl =
6830 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6831 Diag(VD->getLocation(),
6832 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6833 << VD;
6834 continue;
6835 }
6836
6837 // OpenMP [2.8.1, simd construct, Restrictions]
6838 // A list-item cannot appear in more than one aligned clause.
6839 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
6840 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
6841 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
6842 << getOpenMPClauseName(OMPC_aligned);
6843 continue;
6844 }
6845
6846 Vars.push_back(DE);
6847 }
6848
6849 // OpenMP [2.8.1, simd construct, Description]
6850 // The parameter of the aligned clause, alignment, must be a constant
6851 // positive integer expression.
6852 // If no optional parameter is specified, implementation-defined default
6853 // alignments for SIMD instructions on the target platforms are assumed.
6854 if (Alignment != nullptr) {
6855 ExprResult AlignResult =
6856 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
6857 if (AlignResult.isInvalid())
6858 return nullptr;
6859 Alignment = AlignResult.get();
6860 }
6861 if (Vars.empty())
6862 return nullptr;
6863
6864 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
6865 EndLoc, Vars, Alignment);
6866}
6867
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006868OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
6869 SourceLocation StartLoc,
6870 SourceLocation LParenLoc,
6871 SourceLocation EndLoc) {
6872 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006873 SmallVector<Expr *, 8> SrcExprs;
6874 SmallVector<Expr *, 8> DstExprs;
6875 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00006876 for (auto &RefExpr : VarList) {
6877 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
6878 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006879 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006880 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006881 SrcExprs.push_back(nullptr);
6882 DstExprs.push_back(nullptr);
6883 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006884 continue;
6885 }
6886
Alexey Bataeved09d242014-05-28 05:53:51 +00006887 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006888 // OpenMP [2.1, C/C++]
6889 // A list item is a variable name.
6890 // OpenMP [2.14.4.1, Restrictions, p.1]
6891 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00006892 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006893 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006894 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006895 continue;
6896 }
6897
6898 Decl *D = DE->getDecl();
6899 VarDecl *VD = cast<VarDecl>(D);
6900
6901 QualType Type = VD->getType();
6902 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6903 // It will be analyzed later.
6904 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006905 SrcExprs.push_back(nullptr);
6906 DstExprs.push_back(nullptr);
6907 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006908 continue;
6909 }
6910
6911 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
6912 // A list item that appears in a copyin clause must be threadprivate.
6913 if (!DSAStack->isThreadPrivate(VD)) {
6914 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00006915 << getOpenMPClauseName(OMPC_copyin)
6916 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006917 continue;
6918 }
6919
6920 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
6921 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00006922 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006923 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006924 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00006925 auto *SrcVD = buildVarDecl(*this, DE->getLocStart(),
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006926 ElemType.getUnqualifiedType(), ".copyin.src");
Alexey Bataev39f915b82015-05-08 10:41:21 +00006927 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006928 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
6929 auto *DstVD =
6930 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst");
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006931 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006932 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006933 // For arrays generate assignment operation for single element and replace
6934 // it by the original array element in CodeGen.
6935 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
6936 PseudoDstExpr, PseudoSrcExpr);
6937 if (AssignmentOp.isInvalid())
6938 continue;
6939 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
6940 /*DiscardedValue=*/true);
6941 if (AssignmentOp.isInvalid())
6942 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006943
6944 DSAStack->addDSA(VD, DE, OMPC_copyin);
6945 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006946 SrcExprs.push_back(PseudoSrcExpr);
6947 DstExprs.push_back(PseudoDstExpr);
6948 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006949 }
6950
Alexey Bataeved09d242014-05-28 05:53:51 +00006951 if (Vars.empty())
6952 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006953
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006954 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
6955 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006956}
6957
Alexey Bataevbae9a792014-06-27 10:37:06 +00006958OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
6959 SourceLocation StartLoc,
6960 SourceLocation LParenLoc,
6961 SourceLocation EndLoc) {
6962 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00006963 SmallVector<Expr *, 8> SrcExprs;
6964 SmallVector<Expr *, 8> DstExprs;
6965 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00006966 for (auto &RefExpr : VarList) {
6967 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
6968 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6969 // It will be analyzed later.
6970 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00006971 SrcExprs.push_back(nullptr);
6972 DstExprs.push_back(nullptr);
6973 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00006974 continue;
6975 }
6976
6977 SourceLocation ELoc = RefExpr->getExprLoc();
6978 // OpenMP [2.1, C/C++]
6979 // A list item is a variable name.
6980 // OpenMP [2.14.4.1, Restrictions, p.1]
6981 // A list item that appears in a copyin clause must be threadprivate.
6982 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
6983 if (!DE || !isa<VarDecl>(DE->getDecl())) {
6984 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
6985 continue;
6986 }
6987
6988 Decl *D = DE->getDecl();
6989 VarDecl *VD = cast<VarDecl>(D);
6990
6991 QualType Type = VD->getType();
6992 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6993 // It will be analyzed later.
6994 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00006995 SrcExprs.push_back(nullptr);
6996 DstExprs.push_back(nullptr);
6997 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00006998 continue;
6999 }
7000
7001 // OpenMP [2.14.4.2, Restrictions, p.2]
7002 // A list item that appears in a copyprivate clause may not appear in a
7003 // private or firstprivate clause on the single construct.
7004 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007005 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00007006 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
7007 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00007008 Diag(ELoc, diag::err_omp_wrong_dsa)
7009 << getOpenMPClauseName(DVar.CKind)
7010 << getOpenMPClauseName(OMPC_copyprivate);
7011 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7012 continue;
7013 }
7014
7015 // OpenMP [2.11.4.2, Restrictions, p.1]
7016 // All list items that appear in a copyprivate clause must be either
7017 // threadprivate or private in the enclosing context.
7018 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007019 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00007020 if (DVar.CKind == OMPC_shared) {
7021 Diag(ELoc, diag::err_omp_required_access)
7022 << getOpenMPClauseName(OMPC_copyprivate)
7023 << "threadprivate or private in the enclosing context";
7024 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7025 continue;
7026 }
7027 }
7028 }
7029
Alexey Bataev7a3e5852015-05-19 08:19:24 +00007030 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007031 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00007032 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007033 << getOpenMPClauseName(OMPC_copyprivate) << Type
7034 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00007035 bool IsDecl =
7036 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7037 Diag(VD->getLocation(),
7038 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7039 << VD;
7040 continue;
7041 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007042
Alexey Bataevbae9a792014-06-27 10:37:06 +00007043 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
7044 // A variable of class type (or array thereof) that appears in a
7045 // copyin clause requires an accessible, unambiguous copy assignment
7046 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007047 Type = Context.getBaseElementType(Type.getNonReferenceType())
7048 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +00007049 auto *SrcVD =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007050 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.src");
Alexey Bataev420d45b2015-04-14 05:11:24 +00007051 auto *PseudoSrcExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007052 buildDeclRefExpr(*this, SrcVD, Type, DE->getExprLoc());
Alexey Bataev420d45b2015-04-14 05:11:24 +00007053 auto *DstVD =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007054 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.dst");
Alexey Bataev420d45b2015-04-14 05:11:24 +00007055 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007056 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataeva63048e2015-03-23 06:18:07 +00007057 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
7058 PseudoDstExpr, PseudoSrcExpr);
7059 if (AssignmentOp.isInvalid())
7060 continue;
7061 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
7062 /*DiscardedValue=*/true);
7063 if (AssignmentOp.isInvalid())
7064 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00007065
7066 // No need to mark vars as copyprivate, they are already threadprivate or
7067 // implicitly private.
7068 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00007069 SrcExprs.push_back(PseudoSrcExpr);
7070 DstExprs.push_back(PseudoDstExpr);
7071 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +00007072 }
7073
7074 if (Vars.empty())
7075 return nullptr;
7076
Alexey Bataeva63048e2015-03-23 06:18:07 +00007077 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
7078 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00007079}
7080
Alexey Bataev6125da92014-07-21 11:26:11 +00007081OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
7082 SourceLocation StartLoc,
7083 SourceLocation LParenLoc,
7084 SourceLocation EndLoc) {
7085 if (VarList.empty())
7086 return nullptr;
7087
7088 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
7089}
Alexey Bataevdea47612014-07-23 07:46:59 +00007090
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007091OMPClause *
7092Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
7093 SourceLocation DepLoc, SourceLocation ColonLoc,
7094 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
7095 SourceLocation LParenLoc, SourceLocation EndLoc) {
7096 if (DepKind == OMPC_DEPEND_unknown) {
7097 std::string Values;
7098 std::string Sep(", ");
7099 for (unsigned i = 0; i < OMPC_DEPEND_unknown; ++i) {
7100 Values += "'";
7101 Values += getOpenMPSimpleClauseTypeName(OMPC_depend, i);
7102 Values += "'";
7103 switch (i) {
7104 case OMPC_DEPEND_unknown - 2:
7105 Values += " or ";
7106 break;
7107 case OMPC_DEPEND_unknown - 1:
7108 break;
7109 default:
7110 Values += Sep;
7111 break;
7112 }
7113 }
7114 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
7115 << Values << getOpenMPClauseName(OMPC_depend);
7116 return nullptr;
7117 }
7118 SmallVector<Expr *, 8> Vars;
7119 for (auto &RefExpr : VarList) {
7120 assert(RefExpr && "NULL expr in OpenMP shared clause.");
7121 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7122 // It will be analyzed later.
7123 Vars.push_back(RefExpr);
7124 continue;
7125 }
7126
7127 SourceLocation ELoc = RefExpr->getExprLoc();
7128 // OpenMP [2.11.1.1, Restrictions, p.3]
7129 // A variable that is part of another variable (such as a field of a
7130 // structure) but is not an array element or an array section cannot appear
7131 // in a depend clause.
7132 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
Alexey Bataev1a3320e2015-08-25 14:24:04 +00007133 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
7134 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
7135 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
7136 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
7137 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007138 (ASE && !ASE->getBase()->getType()->isAnyPointerType() &&
7139 !ASE->getBase()->getType()->isArrayType())) {
7140 Diag(ELoc, diag::err_omp_expected_var_name_or_array_item)
7141 << RefExpr->getSourceRange();
7142 continue;
7143 }
7144
7145 Vars.push_back(RefExpr->IgnoreParenImpCasts());
7146 }
7147
7148 if (Vars.empty())
7149 return nullptr;
7150
7151 return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
7152 DepLoc, ColonLoc, Vars);
7153}
Michael Wonge710d542015-08-07 16:16:36 +00007154
7155OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
7156 SourceLocation LParenLoc,
7157 SourceLocation EndLoc) {
7158 Expr *ValExpr = Device;
7159 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
7160 !ValExpr->isInstantiationDependent()) {
7161 SourceLocation Loc = ValExpr->getExprLoc();
7162 ExprResult Value = PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
7163 if (Value.isInvalid())
7164 return nullptr;
7165
7166 // OpenMP [2.9.1, Restrictions]
7167 // The device expression must evaluate to a non-negative integer value.
7168 llvm::APSInt Result;
7169 if (Value.get()->isIntegerConstantExpr(Result, Context) &&
7170 Result.isSigned() && !Result.isStrictlyPositive()) {
7171 Diag(Loc, diag::err_omp_negative_expression_in_clause)
7172 << "device" << ValExpr->getSourceRange();
7173 return nullptr;
7174 }
7175 }
7176
7177 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
7178}