blob: 7286301fe6ee245c3a0fbb2ec7a18a91eb60234a [file] [log] [blame]
Alexey Bataev9959db52014-05-06 10:08:46 +00001//===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===//
Alexey Bataeva769e072013-03-22 06:34:35 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10/// \brief This file implements semantic analysis for OpenMP directives and
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000011/// clauses.
Alexey Bataeva769e072013-03-22 06:34:35 +000012///
13//===----------------------------------------------------------------------===//
14
Alexey Bataev9959db52014-05-06 10:08:46 +000015#include "clang/AST/ASTContext.h"
Alexey Bataev97720002014-11-11 04:05:39 +000016#include "clang/AST/ASTMutationListener.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000017#include "clang/AST/Decl.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000018#include "clang/AST/DeclCXX.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000019#include "clang/AST/DeclOpenMP.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000020#include "clang/AST/StmtCXX.h"
21#include "clang/AST/StmtOpenMP.h"
22#include "clang/AST/StmtVisitor.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000023#include "clang/Basic/OpenMPKinds.h"
24#include "clang/Lex/Preprocessor.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000025#include "clang/Sema/Initialization.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000026#include "clang/Sema/Lookup.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000027#include "clang/Sema/Scope.h"
28#include "clang/Sema/ScopeInfo.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000029#include "clang/Sema/SemaInternal.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000030using namespace clang;
31
Alexey Bataev758e55e2013-09-06 18:03:48 +000032//===----------------------------------------------------------------------===//
33// Stack of data-sharing attributes for variables
34//===----------------------------------------------------------------------===//
35
36namespace {
37/// \brief Default data sharing attributes, which can be applied to directive.
38enum DefaultDataSharingAttributes {
Alexey Bataeved09d242014-05-28 05:53:51 +000039 DSA_unspecified = 0, /// \brief Data sharing attribute not specified.
40 DSA_none = 1 << 0, /// \brief Default data sharing attribute 'none'.
41 DSA_shared = 1 << 1 /// \brief Default data sharing attribute 'shared'.
Alexey Bataev758e55e2013-09-06 18:03:48 +000042};
Alexey Bataev7ff55242014-06-19 09:13:45 +000043
Alexey Bataevf29276e2014-06-18 04:14:57 +000044template <class T> struct MatchesAny {
Alexey Bataev23b69422014-06-18 07:08:49 +000045 explicit MatchesAny(ArrayRef<T> Arr) : Arr(std::move(Arr)) {}
Alexey Bataevf29276e2014-06-18 04:14:57 +000046 bool operator()(T Kind) {
47 for (auto KindEl : Arr)
48 if (KindEl == Kind)
49 return true;
50 return false;
51 }
52
53private:
54 ArrayRef<T> Arr;
55};
Alexey Bataev23b69422014-06-18 07:08:49 +000056struct MatchesAlways {
Alexey Bataevf29276e2014-06-18 04:14:57 +000057 MatchesAlways() {}
Alexey Bataev7ff55242014-06-19 09:13:45 +000058 template <class T> bool operator()(T) { return true; }
Alexey Bataevf29276e2014-06-18 04:14:57 +000059};
60
61typedef MatchesAny<OpenMPClauseKind> MatchesAnyClause;
62typedef MatchesAny<OpenMPDirectiveKind> MatchesAnyDirective;
Alexey Bataev758e55e2013-09-06 18:03:48 +000063
64/// \brief Stack for tracking declarations used in OpenMP directives and
65/// clauses and their data-sharing attributes.
66class DSAStackTy {
67public:
68 struct DSAVarData {
69 OpenMPDirectiveKind DKind;
70 OpenMPClauseKind CKind;
71 DeclRefExpr *RefExpr;
Alexey Bataevbae9a792014-06-27 10:37:06 +000072 SourceLocation ImplicitDSALoc;
73 DSAVarData()
74 : DKind(OMPD_unknown), CKind(OMPC_unknown), RefExpr(nullptr),
75 ImplicitDSALoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000076 };
Alexey Bataeved09d242014-05-28 05:53:51 +000077
Alexey Bataev758e55e2013-09-06 18:03:48 +000078private:
79 struct DSAInfo {
80 OpenMPClauseKind Attributes;
81 DeclRefExpr *RefExpr;
82 };
83 typedef llvm::SmallDenseMap<VarDecl *, DSAInfo, 64> DeclSAMapTy;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000084 typedef llvm::SmallDenseMap<VarDecl *, DeclRefExpr *, 64> AlignedMapTy;
Alexey Bataev9c821032015-04-30 04:23:23 +000085 typedef llvm::DenseSet<VarDecl *> LoopControlVariablesSetTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +000086
87 struct SharingMapTy {
88 DeclSAMapTy SharingMap;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000089 AlignedMapTy AlignedMap;
Alexey Bataev9c821032015-04-30 04:23:23 +000090 LoopControlVariablesSetTy LCVSet;
Alexey Bataev758e55e2013-09-06 18:03:48 +000091 DefaultDataSharingAttributes DefaultAttr;
Alexey Bataevbae9a792014-06-27 10:37:06 +000092 SourceLocation DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +000093 OpenMPDirectiveKind Directive;
94 DeclarationNameInfo DirectiveName;
95 Scope *CurScope;
Alexey Bataevbae9a792014-06-27 10:37:06 +000096 SourceLocation ConstructLoc;
Alexey Bataev9fb6e642014-07-22 06:45:04 +000097 bool OrderedRegion;
Alexey Bataev6d4ed052015-07-01 06:57:41 +000098 bool NowaitRegion;
Alexey Bataev9c821032015-04-30 04:23:23 +000099 unsigned CollapseNumber;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000100 SourceLocation InnerTeamsRegionLoc;
Alexey Bataeved09d242014-05-28 05:53:51 +0000101 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000102 Scope *CurScope, SourceLocation Loc)
Alexey Bataev9c821032015-04-30 04:23:23 +0000103 : SharingMap(), AlignedMap(), LCVSet(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000104 Directive(DKind), DirectiveName(std::move(Name)), CurScope(CurScope),
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000105 ConstructLoc(Loc), OrderedRegion(false), NowaitRegion(false),
106 CollapseNumber(1), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000107 SharingMapTy()
Alexey Bataev9c821032015-04-30 04:23:23 +0000108 : SharingMap(), AlignedMap(), LCVSet(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000109 Directive(OMPD_unknown), DirectiveName(), CurScope(nullptr),
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000110 ConstructLoc(), OrderedRegion(false), NowaitRegion(false),
111 CollapseNumber(1), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000112 };
113
114 typedef SmallVector<SharingMapTy, 64> StackTy;
115
116 /// \brief Stack of used declaration and their data-sharing attributes.
117 StackTy Stack;
Alexey Bataev39f915b82015-05-08 10:41:21 +0000118 /// \brief true, if check for DSA must be from parent directive, false, if
119 /// from current directive.
Alexey Bataevaac108a2015-06-23 04:51:00 +0000120 OpenMPClauseKind ClauseKindMode;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000121 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000122
123 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
124
125 DSAVarData getDSA(StackTy::reverse_iterator Iter, VarDecl *D);
Alexey Bataevec3da872014-01-31 05:15:34 +0000126
127 /// \brief Checks if the variable is a local for OpenMP region.
128 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
Alexey Bataeved09d242014-05-28 05:53:51 +0000129
Alexey Bataev758e55e2013-09-06 18:03:48 +0000130public:
Alexey Bataevaac108a2015-06-23 04:51:00 +0000131 explicit DSAStackTy(Sema &S)
132 : Stack(1), ClauseKindMode(OMPC_unknown), SemaRef(S) {}
Alexey Bataev39f915b82015-05-08 10:41:21 +0000133
Alexey Bataevaac108a2015-06-23 04:51:00 +0000134 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
135 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000136
137 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000138 Scope *CurScope, SourceLocation Loc) {
139 Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc));
140 Stack.back().DefaultAttrLoc = Loc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000141 }
142
143 void pop() {
144 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
145 Stack.pop_back();
146 }
147
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000148 /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000149 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000150 /// for diagnostics.
151 DeclRefExpr *addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE);
152
Alexey Bataev9c821032015-04-30 04:23:23 +0000153 /// \brief Register specified variable as loop control variable.
154 void addLoopControlVariable(VarDecl *D);
155 /// \brief Check if the specified variable is a loop control variable for
156 /// current region.
157 bool isLoopControlVariable(VarDecl *D);
158
Alexey Bataev758e55e2013-09-06 18:03:48 +0000159 /// \brief Adds explicit data sharing attribute to the specified declaration.
160 void addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A);
161
Alexey Bataev758e55e2013-09-06 18:03:48 +0000162 /// \brief Returns data sharing attributes from top of the stack for the
163 /// specified declaration.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000164 DSAVarData getTopDSA(VarDecl *D, bool FromParent);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000165 /// \brief Returns data-sharing attributes for the specified declaration.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000166 DSAVarData getImplicitDSA(VarDecl *D, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000167 /// \brief Checks if the specified variables has data-sharing attributes which
168 /// match specified \a CPred predicate in any directive which matches \a DPred
169 /// predicate.
170 template <class ClausesPredicate, class DirectivesPredicate>
171 DSAVarData hasDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000172 DirectivesPredicate DPred, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000173 /// \brief Checks if the specified variables has data-sharing attributes which
174 /// match specified \a CPred predicate in any innermost directive which
175 /// matches \a DPred predicate.
176 template <class ClausesPredicate, class DirectivesPredicate>
177 DSAVarData hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000178 DirectivesPredicate DPred,
179 bool FromParent);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000180 /// \brief Checks if the specified variables has explicit data-sharing
181 /// attributes which match specified \a CPred predicate at the specified
182 /// OpenMP region.
183 bool hasExplicitDSA(VarDecl *D,
184 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
185 unsigned Level);
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000186 /// \brief Finds a directive which matches specified \a DPred predicate.
187 template <class NamedDirectivesPredicate>
188 bool hasDirective(NamedDirectivesPredicate DPred, bool FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000189
Alexey Bataev758e55e2013-09-06 18:03:48 +0000190 /// \brief Returns currently analyzed directive.
191 OpenMPDirectiveKind getCurrentDirective() const {
192 return Stack.back().Directive;
193 }
Alexey Bataev549210e2014-06-24 04:39:47 +0000194 /// \brief Returns parent directive.
195 OpenMPDirectiveKind getParentDirective() const {
196 if (Stack.size() > 2)
197 return Stack[Stack.size() - 2].Directive;
198 return OMPD_unknown;
199 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000200
201 /// \brief Set default data sharing attribute to none.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000202 void setDefaultDSANone(SourceLocation Loc) {
203 Stack.back().DefaultAttr = DSA_none;
204 Stack.back().DefaultAttrLoc = Loc;
205 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000206 /// \brief Set default data sharing attribute to shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000207 void setDefaultDSAShared(SourceLocation Loc) {
208 Stack.back().DefaultAttr = DSA_shared;
209 Stack.back().DefaultAttrLoc = Loc;
210 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000211
212 DefaultDataSharingAttributes getDefaultDSA() const {
213 return Stack.back().DefaultAttr;
214 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000215 SourceLocation getDefaultDSALocation() const {
216 return Stack.back().DefaultAttrLoc;
217 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000218
Alexey Bataevf29276e2014-06-18 04:14:57 +0000219 /// \brief Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000220 bool isThreadPrivate(VarDecl *D) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000221 DSAVarData DVar = getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000222 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000223 }
224
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000225 /// \brief Marks current region as ordered (it has an 'ordered' clause).
226 void setOrderedRegion(bool IsOrdered = true) {
227 Stack.back().OrderedRegion = IsOrdered;
228 }
229 /// \brief Returns true, if parent region is ordered (has associated
230 /// 'ordered' clause), false - otherwise.
231 bool isParentOrderedRegion() const {
232 if (Stack.size() > 2)
233 return Stack[Stack.size() - 2].OrderedRegion;
234 return false;
235 }
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000236 /// \brief Marks current region as nowait (it has a 'nowait' clause).
237 void setNowaitRegion(bool IsNowait = true) {
238 Stack.back().NowaitRegion = IsNowait;
239 }
240 /// \brief Returns true, if parent region is nowait (has associated
241 /// 'nowait' clause), false - otherwise.
242 bool isParentNowaitRegion() const {
243 if (Stack.size() > 2)
244 return Stack[Stack.size() - 2].NowaitRegion;
245 return false;
246 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000247
Alexey Bataev9c821032015-04-30 04:23:23 +0000248 /// \brief Set collapse value for the region.
249 void setCollapseNumber(unsigned Val) { Stack.back().CollapseNumber = Val; }
250 /// \brief Return collapse value for region.
251 unsigned getCollapseNumber() const {
252 return Stack.back().CollapseNumber;
253 }
254
Alexey Bataev13314bf2014-10-09 04:18:56 +0000255 /// \brief Marks current target region as one with closely nested teams
256 /// region.
257 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
258 if (Stack.size() > 2)
259 Stack[Stack.size() - 2].InnerTeamsRegionLoc = TeamsRegionLoc;
260 }
261 /// \brief Returns true, if current region has closely nested teams region.
262 bool hasInnerTeamsRegion() const {
263 return getInnerTeamsRegionLoc().isValid();
264 }
265 /// \brief Returns location of the nested teams region (if any).
266 SourceLocation getInnerTeamsRegionLoc() const {
267 if (Stack.size() > 1)
268 return Stack.back().InnerTeamsRegionLoc;
269 return SourceLocation();
270 }
271
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000272 Scope *getCurScope() const { return Stack.back().CurScope; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000273 Scope *getCurScope() { return Stack.back().CurScope; }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000274 SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000275};
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000276bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
277 return isOpenMPParallelDirective(DKind) || DKind == OMPD_task ||
Alexey Bataev13314bf2014-10-09 04:18:56 +0000278 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000279}
Alexey Bataeved09d242014-05-28 05:53:51 +0000280} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000281
282DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter,
283 VarDecl *D) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000284 D = D->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000285 DSAVarData DVar;
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000286 if (Iter == std::prev(Stack.rend())) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000287 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
288 // in a region but not in construct]
289 // File-scope or namespace-scope variables referenced in called routines
290 // in the region are shared unless they appear in a threadprivate
291 // directive.
Alexey Bataev8b9cb982014-07-24 02:33:58 +0000292 if (!D->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D))
Alexey Bataev750a58b2014-03-18 12:19:12 +0000293 DVar.CKind = OMPC_shared;
294
295 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
296 // in a region but not in construct]
297 // Variables with static storage duration that are declared in called
298 // routines in the region are shared.
299 if (D->hasGlobalStorage())
300 DVar.CKind = OMPC_shared;
301
Alexey Bataev758e55e2013-09-06 18:03:48 +0000302 return DVar;
303 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000304
Alexey Bataev758e55e2013-09-06 18:03:48 +0000305 DVar.DKind = Iter->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +0000306 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
307 // in a Construct, C/C++, predetermined, p.1]
308 // Variables with automatic storage duration that are declared in a scope
309 // inside the construct are private.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000310 if (isOpenMPLocal(D, Iter) && D->isLocalVarDecl() &&
311 (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) {
312 DVar.CKind = OMPC_private;
313 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000314 }
315
Alexey Bataev758e55e2013-09-06 18:03:48 +0000316 // Explicitly specified attributes and local variables with predetermined
317 // attributes.
318 if (Iter->SharingMap.count(D)) {
319 DVar.RefExpr = Iter->SharingMap[D].RefExpr;
320 DVar.CKind = Iter->SharingMap[D].Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000321 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000322 return DVar;
323 }
324
325 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
326 // in a Construct, C/C++, implicitly determined, p.1]
327 // In a parallel or task construct, the data-sharing attributes of these
328 // variables are determined by the default clause, if present.
329 switch (Iter->DefaultAttr) {
330 case DSA_shared:
331 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000332 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000333 return DVar;
334 case DSA_none:
335 return DVar;
336 case DSA_unspecified:
337 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
338 // in a Construct, implicitly determined, p.2]
339 // In a parallel construct, if no default clause is present, these
340 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000341 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000342 if (isOpenMPParallelDirective(DVar.DKind) ||
343 isOpenMPTeamsDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000344 DVar.CKind = OMPC_shared;
345 return DVar;
346 }
347
348 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
349 // in a Construct, implicitly determined, p.4]
350 // In a task construct, if no default clause is present, a variable that in
351 // the enclosing context is determined to be shared by all implicit tasks
352 // bound to the current team is shared.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000353 if (DVar.DKind == OMPD_task) {
354 DSAVarData DVarTemp;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000355 for (StackTy::reverse_iterator I = std::next(Iter), EE = Stack.rend();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000356 I != EE; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000357 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
358 // Referenced
Alexey Bataev758e55e2013-09-06 18:03:48 +0000359 // in a Construct, implicitly determined, p.6]
360 // In a task construct, if no default clause is present, a variable
361 // whose data-sharing attribute is not determined by the rules above is
362 // firstprivate.
363 DVarTemp = getDSA(I, D);
364 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000365 DVar.RefExpr = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000366 DVar.DKind = OMPD_task;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000367 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000368 return DVar;
369 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000370 if (isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000371 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000372 }
373 DVar.DKind = OMPD_task;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000374 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000375 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000376 return DVar;
377 }
378 }
379 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
380 // in a Construct, implicitly determined, p.3]
381 // For constructs other than task, if no default clause is present, these
382 // variables inherit their data-sharing attributes from the enclosing
383 // context.
Benjamin Kramer167e9992014-03-02 12:20:24 +0000384 return getDSA(std::next(Iter), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000385}
386
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000387DeclRefExpr *DSAStackTy::addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE) {
388 assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000389 D = D->getCanonicalDecl();
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000390 auto It = Stack.back().AlignedMap.find(D);
391 if (It == Stack.back().AlignedMap.end()) {
392 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
393 Stack.back().AlignedMap[D] = NewDE;
394 return nullptr;
395 } else {
396 assert(It->second && "Unexpected nullptr expr in the aligned map");
397 return It->second;
398 }
399 return nullptr;
400}
401
Alexey Bataev9c821032015-04-30 04:23:23 +0000402void DSAStackTy::addLoopControlVariable(VarDecl *D) {
403 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
404 D = D->getCanonicalDecl();
405 Stack.back().LCVSet.insert(D);
406}
407
408bool DSAStackTy::isLoopControlVariable(VarDecl *D) {
409 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
410 D = D->getCanonicalDecl();
411 return Stack.back().LCVSet.count(D) > 0;
412}
413
Alexey Bataev758e55e2013-09-06 18:03:48 +0000414void DSAStackTy::addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000415 D = D->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000416 if (A == OMPC_threadprivate) {
417 Stack[0].SharingMap[D].Attributes = A;
418 Stack[0].SharingMap[D].RefExpr = E;
419 } else {
420 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
421 Stack.back().SharingMap[D].Attributes = A;
422 Stack.back().SharingMap[D].RefExpr = E;
423 }
424}
425
Alexey Bataeved09d242014-05-28 05:53:51 +0000426bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000427 D = D->getCanonicalDecl();
Alexey Bataevec3da872014-01-31 05:15:34 +0000428 if (Stack.size() > 2) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000429 reverse_iterator I = Iter, E = std::prev(Stack.rend());
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000430 Scope *TopScope = nullptr;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000431 while (I != E && !isParallelOrTaskRegion(I->Directive)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000432 ++I;
433 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000434 if (I == E)
435 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000436 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000437 Scope *CurScope = getCurScope();
438 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000439 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000440 }
441 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000442 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000443 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000444}
445
Alexey Bataev39f915b82015-05-08 10:41:21 +0000446/// \brief Build a variable declaration for OpenMP loop iteration variable.
447static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
448 StringRef Name) {
449 DeclContext *DC = SemaRef.CurContext;
450 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
451 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
452 VarDecl *Decl =
453 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
454 Decl->setImplicit();
455 return Decl;
456}
457
458static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
459 SourceLocation Loc,
460 bool RefersToCapture = false) {
461 D->setReferenced();
462 D->markUsed(S.Context);
463 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
464 SourceLocation(), D, RefersToCapture, Loc, Ty,
465 VK_LValue);
466}
467
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000468DSAStackTy::DSAVarData DSAStackTy::getTopDSA(VarDecl *D, bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000469 D = D->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000470 DSAVarData DVar;
471
472 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
473 // in a Construct, C/C++, predetermined, p.1]
474 // Variables appearing in threadprivate directives are threadprivate.
Alexey Bataev26a39242015-01-13 03:35:30 +0000475 if (D->getTLSKind() != VarDecl::TLS_None ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +0000476 (D->getStorageClass() == SC_Register && D->hasAttr<AsmLabelAttr>() &&
477 !D->isLocalVarDecl())) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000478 addDSA(D, buildDeclRefExpr(SemaRef, D, D->getType().getNonReferenceType(),
479 D->getLocation()),
Alexey Bataevf2453a02015-05-06 07:25:08 +0000480 OMPC_threadprivate);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000481 }
482 if (Stack[0].SharingMap.count(D)) {
483 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
484 DVar.CKind = OMPC_threadprivate;
485 return DVar;
486 }
487
488 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
489 // in a Construct, C/C++, predetermined, p.1]
490 // Variables with automatic storage duration that are declared in a scope
491 // inside the construct are private.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000492 OpenMPDirectiveKind Kind =
493 FromParent ? getParentDirective() : getCurrentDirective();
494 auto StartI = std::next(Stack.rbegin());
495 auto EndI = std::prev(Stack.rend());
496 if (FromParent && StartI != EndI) {
497 StartI = std::next(StartI);
498 }
499 if (!isParallelOrTaskRegion(Kind)) {
Alexey Bataev8b9cb982014-07-24 02:33:58 +0000500 if (isOpenMPLocal(D, StartI) &&
501 ((D->isLocalVarDecl() && (D->getStorageClass() == SC_Auto ||
502 D->getStorageClass() == SC_None)) ||
503 isa<ParmVarDecl>(D))) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000504 DVar.CKind = OMPC_private;
505 return DVar;
Alexander Musman8dba6642014-04-22 13:09:42 +0000506 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000507
Alexey Bataev24b04aa2015-01-16 07:11:33 +0000508 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
509 // in a Construct, C/C++, predetermined, p.4]
510 // Static data members are shared.
Alexey Bataev24b04aa2015-01-16 07:11:33 +0000511 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
512 // in a Construct, C/C++, predetermined, p.7]
513 // Variables with static storage duration that are declared in a scope
514 // inside the construct are shared.
Alexey Bataev42971a32015-01-20 07:03:46 +0000515 if (D->isStaticDataMember() || D->isStaticLocal()) {
516 DSAVarData DVarTemp =
517 hasDSA(D, isOpenMPPrivate, MatchesAlways(), FromParent);
518 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
519 return DVar;
520
Alexey Bataev24b04aa2015-01-16 07:11:33 +0000521 DVar.CKind = OMPC_shared;
522 return DVar;
523 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000524 }
525
526 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +0000527 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
528 Type = SemaRef.getASTContext().getBaseElementType(Type);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000529 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
530 // in a Construct, C/C++, predetermined, p.6]
531 // Variables with const qualified type having no mutable member are
532 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000533 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000534 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000535 if (IsConstant &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000536 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000537 // Variables with const-qualified type having no mutable member may be
538 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000539 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
540 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000541 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
542 return DVar;
543
Alexey Bataev758e55e2013-09-06 18:03:48 +0000544 DVar.CKind = OMPC_shared;
545 return DVar;
546 }
547
Alexey Bataev758e55e2013-09-06 18:03:48 +0000548 // Explicitly specified attributes and local variables with predetermined
549 // attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000550 auto I = std::prev(StartI);
551 if (I->SharingMap.count(D)) {
552 DVar.RefExpr = I->SharingMap[D].RefExpr;
553 DVar.CKind = I->SharingMap[D].Attributes;
554 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000555 }
556
557 return DVar;
558}
559
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000560DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D, bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000561 D = D->getCanonicalDecl();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000562 auto StartI = Stack.rbegin();
563 auto EndI = std::prev(Stack.rend());
564 if (FromParent && StartI != EndI) {
565 StartI = std::next(StartI);
566 }
567 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000568}
569
Alexey Bataevf29276e2014-06-18 04:14:57 +0000570template <class ClausesPredicate, class DirectivesPredicate>
571DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000572 DirectivesPredicate DPred,
573 bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000574 D = D->getCanonicalDecl();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000575 auto StartI = std::next(Stack.rbegin());
576 auto EndI = std::prev(Stack.rend());
577 if (FromParent && StartI != EndI) {
578 StartI = std::next(StartI);
579 }
580 for (auto I = StartI, EE = EndI; I != EE; ++I) {
581 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000582 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000583 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000584 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000585 return DVar;
586 }
587 return DSAVarData();
588}
589
Alexey Bataevf29276e2014-06-18 04:14:57 +0000590template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000591DSAStackTy::DSAVarData
592DSAStackTy::hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
593 DirectivesPredicate DPred, bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000594 D = D->getCanonicalDecl();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000595 auto StartI = std::next(Stack.rbegin());
596 auto EndI = std::prev(Stack.rend());
597 if (FromParent && StartI != EndI) {
598 StartI = std::next(StartI);
599 }
600 for (auto I = StartI, EE = EndI; I != EE; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000601 if (!DPred(I->Directive))
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000602 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000603 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000604 if (CPred(DVar.CKind))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000605 return DVar;
606 return DSAVarData();
607 }
608 return DSAVarData();
609}
610
Alexey Bataevaac108a2015-06-23 04:51:00 +0000611bool DSAStackTy::hasExplicitDSA(
612 VarDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
613 unsigned Level) {
614 if (CPred(ClauseKindMode))
615 return true;
616 if (isClauseParsingMode())
617 ++Level;
618 D = D->getCanonicalDecl();
619 auto StartI = Stack.rbegin();
620 auto EndI = std::prev(Stack.rend());
NAKAMURA Takumi0332eda2015-06-23 10:01:20 +0000621 if (std::distance(StartI, EndI) <= (int)Level)
Alexey Bataevaac108a2015-06-23 04:51:00 +0000622 return false;
623 std::advance(StartI, Level);
624 return (StartI->SharingMap.count(D) > 0) && StartI->SharingMap[D].RefExpr &&
625 CPred(StartI->SharingMap[D].Attributes);
626}
627
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000628template <class NamedDirectivesPredicate>
629bool DSAStackTy::hasDirective(NamedDirectivesPredicate DPred, bool FromParent) {
630 auto StartI = std::next(Stack.rbegin());
631 auto EndI = std::prev(Stack.rend());
632 if (FromParent && StartI != EndI) {
633 StartI = std::next(StartI);
634 }
635 for (auto I = StartI, EE = EndI; I != EE; ++I) {
636 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
637 return true;
638 }
639 return false;
640}
641
Alexey Bataev758e55e2013-09-06 18:03:48 +0000642void Sema::InitDataSharingAttributesStack() {
643 VarDataSharingAttributesStack = new DSAStackTy(*this);
644}
645
646#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
647
Alexey Bataevf841bd92014-12-16 07:00:22 +0000648bool Sema::IsOpenMPCapturedVar(VarDecl *VD) {
649 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000650 VD = VD->getCanonicalDecl();
Alexey Bataevf841bd92014-12-16 07:00:22 +0000651 if (DSAStack->getCurrentDirective() != OMPD_unknown) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000652 if (DSAStack->isLoopControlVariable(VD) ||
653 (VD->hasLocalStorage() &&
654 isParallelOrTaskRegion(DSAStack->getCurrentDirective())))
Alexey Bataev9c821032015-04-30 04:23:23 +0000655 return true;
Alexey Bataevaac108a2015-06-23 04:51:00 +0000656 auto DVarPrivate = DSAStack->getTopDSA(VD, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000657 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
658 return true;
659 DVarPrivate = DSAStack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(),
Alexey Bataevaac108a2015-06-23 04:51:00 +0000660 DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000661 return DVarPrivate.CKind != OMPC_unknown;
662 }
663 return false;
664}
665
Alexey Bataevaac108a2015-06-23 04:51:00 +0000666bool Sema::isOpenMPPrivateVar(VarDecl *VD, unsigned Level) {
667 assert(LangOpts.OpenMP && "OpenMP is not allowed");
668 return DSAStack->hasExplicitDSA(
669 VD, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level);
670}
671
Alexey Bataeved09d242014-05-28 05:53:51 +0000672void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000673
674void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
675 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000676 Scope *CurScope, SourceLocation Loc) {
677 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000678 PushExpressionEvaluationContext(PotentiallyEvaluated);
679}
680
Alexey Bataevaac108a2015-06-23 04:51:00 +0000681void Sema::StartOpenMPClause(OpenMPClauseKind K) {
682 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000683}
684
Alexey Bataevaac108a2015-06-23 04:51:00 +0000685void Sema::EndOpenMPClause() {
686 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000687}
688
Alexey Bataev758e55e2013-09-06 18:03:48 +0000689void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000690 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
691 // A variable of class type (or array thereof) that appears in a lastprivate
692 // clause requires an accessible, unambiguous default constructor for the
693 // class type, unless the list item is also specified in a firstprivate
694 // clause.
695 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000696 for (auto *C : D->clauses()) {
697 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
698 SmallVector<Expr *, 8> PrivateCopies;
699 for (auto *DE : Clause->varlists()) {
700 if (DE->isValueDependent() || DE->isTypeDependent()) {
701 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000702 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +0000703 }
704 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(DE)->getDecl());
Alexey Bataevf120c0d2015-05-19 07:46:42 +0000705 QualType Type = VD->getType();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000706 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000707 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000708 // Generate helper private variable and initialize it with the
709 // default value. The address of the original variable is replaced
710 // by the address of the new private variable in CodeGen. This new
711 // variable is not added to IdResolver, so the code in the OpenMP
712 // region uses original variable for proper diagnostics.
Alexey Bataev39f915b82015-05-08 10:41:21 +0000713 auto *VDPrivate =
Alexey Bataevf120c0d2015-05-19 07:46:42 +0000714 buildVarDecl(*this, DE->getExprLoc(), Type.getUnqualifiedType(),
715 VD->getName());
Alexey Bataev38e89532015-04-16 04:54:05 +0000716 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
717 if (VDPrivate->isInvalidDecl())
718 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +0000719 PrivateCopies.push_back(buildDeclRefExpr(
720 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +0000721 } else {
722 // The variable is also a firstprivate, so initialization sequence
723 // for private copy is generated already.
724 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000725 }
726 }
Alexey Bataev38e89532015-04-16 04:54:05 +0000727 // Set initializers to private copies if no errors were found.
728 if (PrivateCopies.size() == Clause->varlist_size()) {
729 Clause->setPrivateCopies(PrivateCopies);
730 }
Alexey Bataevf29276e2014-06-18 04:14:57 +0000731 }
732 }
733 }
734
Alexey Bataev758e55e2013-09-06 18:03:48 +0000735 DSAStack->pop();
736 DiscardCleanupsInEvaluationContext();
737 PopExpressionEvaluationContext();
738}
739
Alexander Musman3276a272015-03-21 10:12:56 +0000740static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
741 Expr *NumIterations, Sema &SemaRef,
742 Scope *S);
743
Alexey Bataeva769e072013-03-22 06:34:35 +0000744namespace {
745
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000746class VarDeclFilterCCC : public CorrectionCandidateCallback {
747private:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000748 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +0000749
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000750public:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000751 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +0000752 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000753 NamedDecl *ND = Candidate.getCorrectionDecl();
754 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
755 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000756 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
757 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +0000758 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000759 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000760 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000761};
Alexey Bataeved09d242014-05-28 05:53:51 +0000762} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000763
764ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
765 CXXScopeSpec &ScopeSpec,
766 const DeclarationNameInfo &Id) {
767 LookupResult Lookup(*this, Id, LookupOrdinaryName);
768 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
769
770 if (Lookup.isAmbiguous())
771 return ExprError();
772
773 VarDecl *VD;
774 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +0000775 if (TypoCorrection Corrected = CorrectTypo(
776 Id, LookupOrdinaryName, CurScope, nullptr,
777 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +0000778 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000779 PDiag(Lookup.empty()
780 ? diag::err_undeclared_var_use_suggest
781 : diag::err_omp_expected_var_arg_suggest)
782 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +0000783 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000784 } else {
Richard Smithf9b15102013-08-17 00:46:16 +0000785 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
786 : diag::err_omp_expected_var_arg)
787 << Id.getName();
788 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000789 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000790 } else {
791 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000792 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000793 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
794 return ExprError();
795 }
796 }
797 Lookup.suppressDiagnostics();
798
799 // OpenMP [2.9.2, Syntax, C/C++]
800 // Variables must be file-scope, namespace-scope, or static block-scope.
801 if (!VD->hasGlobalStorage()) {
802 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000803 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
804 bool IsDecl =
805 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000806 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +0000807 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
808 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000809 return ExprError();
810 }
811
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000812 VarDecl *CanonicalVD = VD->getCanonicalDecl();
813 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000814 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
815 // A threadprivate directive for file-scope variables must appear outside
816 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000817 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
818 !getCurLexicalContext()->isTranslationUnit()) {
819 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000820 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
821 bool IsDecl =
822 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
823 Diag(VD->getLocation(),
824 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
825 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000826 return ExprError();
827 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000828 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
829 // A threadprivate directive for static class member variables must appear
830 // in the class definition, in the same scope in which the member
831 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000832 if (CanonicalVD->isStaticDataMember() &&
833 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
834 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000835 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
836 bool IsDecl =
837 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
838 Diag(VD->getLocation(),
839 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
840 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000841 return ExprError();
842 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000843 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
844 // A threadprivate directive for namespace-scope variables must appear
845 // outside any definition or declaration other than the namespace
846 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000847 if (CanonicalVD->getDeclContext()->isNamespace() &&
848 (!getCurLexicalContext()->isFileContext() ||
849 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
850 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000851 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
852 bool IsDecl =
853 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
854 Diag(VD->getLocation(),
855 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
856 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000857 return ExprError();
858 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000859 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
860 // A threadprivate directive for static block-scope variables must appear
861 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000862 if (CanonicalVD->isStaticLocal() && CurScope &&
863 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000864 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000865 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
866 bool IsDecl =
867 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
868 Diag(VD->getLocation(),
869 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
870 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000871 return ExprError();
872 }
873
874 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
875 // A threadprivate directive must lexically precede all references to any
876 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000877 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000878 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +0000879 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000880 return ExprError();
881 }
882
883 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +0000884 ExprResult DE = buildDeclRefExpr(*this, VD, ExprType, Id.getLoc());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000885 return DE;
886}
887
Alexey Bataeved09d242014-05-28 05:53:51 +0000888Sema::DeclGroupPtrTy
889Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
890 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000891 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000892 CurContext->addDecl(D);
893 return DeclGroupPtrTy::make(DeclGroupRef(D));
894 }
895 return DeclGroupPtrTy();
896}
897
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000898namespace {
899class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
900 Sema &SemaRef;
901
902public:
903 bool VisitDeclRefExpr(const DeclRefExpr *E) {
904 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
905 if (VD->hasLocalStorage()) {
906 SemaRef.Diag(E->getLocStart(),
907 diag::err_omp_local_var_in_threadprivate_init)
908 << E->getSourceRange();
909 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
910 << VD << VD->getSourceRange();
911 return true;
912 }
913 }
914 return false;
915 }
916 bool VisitStmt(const Stmt *S) {
917 for (auto Child : S->children()) {
918 if (Child && Visit(Child))
919 return true;
920 }
921 return false;
922 }
Alexey Bataev23b69422014-06-18 07:08:49 +0000923 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000924};
925} // namespace
926
Alexey Bataeved09d242014-05-28 05:53:51 +0000927OMPThreadPrivateDecl *
928Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000929 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +0000930 for (auto &RefExpr : VarList) {
931 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000932 VarDecl *VD = cast<VarDecl>(DE->getDecl());
933 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +0000934
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000935 QualType QType = VD->getType();
936 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
937 // It will be analyzed later.
938 Vars.push_back(DE);
939 continue;
940 }
941
Alexey Bataeva769e072013-03-22 06:34:35 +0000942 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
943 // A threadprivate variable must not have an incomplete type.
944 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000945 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000946 continue;
947 }
948
949 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
950 // A threadprivate variable must not have a reference type.
951 if (VD->getType()->isReferenceType()) {
952 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000953 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
954 bool IsDecl =
955 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
956 Diag(VD->getLocation(),
957 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
958 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000959 continue;
960 }
961
Richard Smithfd3834f2013-04-13 02:43:54 +0000962 // Check if this is a TLS variable.
Alexey Bataev26a39242015-01-13 03:35:30 +0000963 if (VD->getTLSKind() != VarDecl::TLS_None ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +0000964 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
965 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +0000966 Diag(ILoc, diag::err_omp_var_thread_local)
967 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +0000968 bool IsDecl =
969 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
970 Diag(VD->getLocation(),
971 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
972 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000973 continue;
974 }
975
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000976 // Check if initial value of threadprivate variable reference variable with
977 // local storage (it is not supported by runtime).
978 if (auto Init = VD->getAnyInitializer()) {
979 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000980 if (Checker.Visit(Init))
981 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000982 }
983
Alexey Bataeved09d242014-05-28 05:53:51 +0000984 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +0000985 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +0000986 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
987 Context, SourceRange(Loc, Loc)));
988 if (auto *ML = Context.getASTMutationListener())
989 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +0000990 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000991 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000992 if (!Vars.empty()) {
993 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
994 Vars);
995 D->setAccess(AS_public);
996 }
997 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +0000998}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000999
Alexey Bataev7ff55242014-06-19 09:13:45 +00001000static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
1001 const VarDecl *VD, DSAStackTy::DSAVarData DVar,
1002 bool IsLoopIterVar = false) {
1003 if (DVar.RefExpr) {
1004 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1005 << getOpenMPClauseName(DVar.CKind);
1006 return;
1007 }
1008 enum {
1009 PDSA_StaticMemberShared,
1010 PDSA_StaticLocalVarShared,
1011 PDSA_LoopIterVarPrivate,
1012 PDSA_LoopIterVarLinear,
1013 PDSA_LoopIterVarLastprivate,
1014 PDSA_ConstVarShared,
1015 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001016 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001017 PDSA_LocalVarPrivate,
1018 PDSA_Implicit
1019 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001020 bool ReportHint = false;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001021 auto ReportLoc = VD->getLocation();
Alexey Bataev7ff55242014-06-19 09:13:45 +00001022 if (IsLoopIterVar) {
1023 if (DVar.CKind == OMPC_private)
1024 Reason = PDSA_LoopIterVarPrivate;
1025 else if (DVar.CKind == OMPC_lastprivate)
1026 Reason = PDSA_LoopIterVarLastprivate;
1027 else
1028 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001029 } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) {
1030 Reason = PDSA_TaskVarFirstprivate;
1031 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001032 } else if (VD->isStaticLocal())
1033 Reason = PDSA_StaticLocalVarShared;
1034 else if (VD->isStaticDataMember())
1035 Reason = PDSA_StaticMemberShared;
1036 else if (VD->isFileVarDecl())
1037 Reason = PDSA_GlobalVarShared;
1038 else if (VD->getType().isConstant(SemaRef.getASTContext()))
1039 Reason = PDSA_ConstVarShared;
Alexey Bataevbae9a792014-06-27 10:37:06 +00001040 else if (VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00001041 ReportHint = true;
1042 Reason = PDSA_LocalVarPrivate;
1043 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00001044 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001045 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00001046 << Reason << ReportHint
1047 << getOpenMPDirectiveName(Stack->getCurrentDirective());
1048 } else if (DVar.ImplicitDSALoc.isValid()) {
1049 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1050 << getOpenMPClauseName(DVar.CKind);
1051 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00001052}
1053
Alexey Bataev758e55e2013-09-06 18:03:48 +00001054namespace {
1055class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1056 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001057 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001058 bool ErrorFound;
1059 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001060 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev4acb8592014-07-07 13:01:15 +00001061 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +00001062
Alexey Bataev758e55e2013-09-06 18:03:48 +00001063public:
1064 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001065 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001066 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +00001067 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1068 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001069
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001070 auto DVar = Stack->getTopDSA(VD, false);
1071 // Check if the variable has explicit DSA set and stop analysis if it so.
1072 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001073
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001074 auto ELoc = E->getExprLoc();
1075 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001076 // The default(none) clause requires that each variable that is referenced
1077 // in the construct, and does not have a predetermined data-sharing
1078 // attribute, must have its data-sharing attribute explicitly determined
1079 // by being listed in a data-sharing attribute clause.
1080 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001081 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001082 VarsWithInheritedDSA.count(VD) == 0) {
1083 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001084 return;
1085 }
1086
1087 // OpenMP [2.9.3.6, Restrictions, p.2]
1088 // A list item that appears in a reduction clause of the innermost
1089 // enclosing worksharing or parallel construct may not be accessed in an
1090 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +00001091 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001092 [](OpenMPDirectiveKind K) -> bool {
1093 return isOpenMPParallelDirective(K) ||
Alexey Bataev13314bf2014-10-09 04:18:56 +00001094 isOpenMPWorksharingDirective(K) ||
1095 isOpenMPTeamsDirective(K);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001096 },
1097 false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001098 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
1099 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001100 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1101 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001102 return;
1103 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001104
1105 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001106 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001107 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001108 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001109 }
1110 }
1111 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001112 for (auto *C : S->clauses()) {
1113 // Skip analysis of arguments of implicitly defined firstprivate clause
1114 // for task directives.
1115 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1116 for (auto *CC : C->children()) {
1117 if (CC)
1118 Visit(CC);
1119 }
1120 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001121 }
1122 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001123 for (auto *C : S->children()) {
1124 if (C && !isa<OMPExecutableDirective>(C))
1125 Visit(C);
1126 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001127 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001128
1129 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001130 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001131 llvm::DenseMap<VarDecl *, Expr *> &getVarsWithInheritedDSA() {
1132 return VarsWithInheritedDSA;
1133 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001134
Alexey Bataev7ff55242014-06-19 09:13:45 +00001135 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1136 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00001137};
Alexey Bataeved09d242014-05-28 05:53:51 +00001138} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001139
Alexey Bataevbae9a792014-06-27 10:37:06 +00001140void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001141 switch (DKind) {
1142 case OMPD_parallel: {
1143 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1144 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001145 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001146 std::make_pair(".global_tid.", KmpInt32PtrTy),
1147 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1148 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001149 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001150 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1151 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001152 break;
1153 }
1154 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001155 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001156 std::make_pair(StringRef(), QualType()) // __context with shared vars
1157 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001158 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1159 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001160 break;
1161 }
1162 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001163 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001164 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001165 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001166 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1167 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001168 break;
1169 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001170 case OMPD_for_simd: {
1171 Sema::CapturedParamNameType Params[] = {
1172 std::make_pair(StringRef(), QualType()) // __context with shared vars
1173 };
1174 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1175 Params);
1176 break;
1177 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001178 case OMPD_sections: {
1179 Sema::CapturedParamNameType Params[] = {
1180 std::make_pair(StringRef(), QualType()) // __context with shared vars
1181 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001182 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1183 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001184 break;
1185 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001186 case OMPD_section: {
1187 Sema::CapturedParamNameType Params[] = {
1188 std::make_pair(StringRef(), QualType()) // __context with shared vars
1189 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001190 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1191 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001192 break;
1193 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001194 case OMPD_single: {
1195 Sema::CapturedParamNameType Params[] = {
1196 std::make_pair(StringRef(), QualType()) // __context with shared vars
1197 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001198 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1199 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001200 break;
1201 }
Alexander Musman80c22892014-07-17 08:54:58 +00001202 case OMPD_master: {
1203 Sema::CapturedParamNameType Params[] = {
1204 std::make_pair(StringRef(), QualType()) // __context with shared vars
1205 };
1206 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1207 Params);
1208 break;
1209 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001210 case OMPD_critical: {
1211 Sema::CapturedParamNameType Params[] = {
1212 std::make_pair(StringRef(), QualType()) // __context with shared vars
1213 };
1214 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1215 Params);
1216 break;
1217 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001218 case OMPD_parallel_for: {
1219 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1220 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1221 Sema::CapturedParamNameType Params[] = {
1222 std::make_pair(".global_tid.", KmpInt32PtrTy),
1223 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1224 std::make_pair(StringRef(), QualType()) // __context with shared vars
1225 };
1226 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1227 Params);
1228 break;
1229 }
Alexander Musmane4e893b2014-09-23 09:33:00 +00001230 case OMPD_parallel_for_simd: {
1231 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1232 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1233 Sema::CapturedParamNameType Params[] = {
1234 std::make_pair(".global_tid.", KmpInt32PtrTy),
1235 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1236 std::make_pair(StringRef(), QualType()) // __context with shared vars
1237 };
1238 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1239 Params);
1240 break;
1241 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001242 case OMPD_parallel_sections: {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001243 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1244 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001245 Sema::CapturedParamNameType Params[] = {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001246 std::make_pair(".global_tid.", KmpInt32PtrTy),
1247 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001248 std::make_pair(StringRef(), QualType()) // __context with shared vars
1249 };
1250 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1251 Params);
1252 break;
1253 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001254 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001255 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001256 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1257 FunctionProtoType::ExtProtoInfo EPI;
1258 EPI.Variadic = true;
1259 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001260 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001261 std::make_pair(".global_tid.", KmpInt32Ty),
1262 std::make_pair(".part_id.", KmpInt32Ty),
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001263 std::make_pair(".privates.",
1264 Context.VoidPtrTy.withConst().withRestrict()),
1265 std::make_pair(
1266 ".copy_fn.",
1267 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001268 std::make_pair(StringRef(), QualType()) // __context with shared vars
1269 };
1270 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1271 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001272 // Mark this captured region as inlined, because we don't use outlined
1273 // function directly.
1274 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1275 AlwaysInlineAttr::CreateImplicit(
1276 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001277 break;
1278 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001279 case OMPD_ordered: {
1280 Sema::CapturedParamNameType Params[] = {
1281 std::make_pair(StringRef(), QualType()) // __context with shared vars
1282 };
1283 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1284 Params);
1285 break;
1286 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001287 case OMPD_atomic: {
1288 Sema::CapturedParamNameType Params[] = {
1289 std::make_pair(StringRef(), QualType()) // __context with shared vars
1290 };
1291 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1292 Params);
1293 break;
1294 }
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001295 case OMPD_target: {
1296 Sema::CapturedParamNameType Params[] = {
1297 std::make_pair(StringRef(), QualType()) // __context with shared vars
1298 };
1299 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1300 Params);
1301 break;
1302 }
Alexey Bataev13314bf2014-10-09 04:18:56 +00001303 case OMPD_teams: {
1304 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1305 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1306 Sema::CapturedParamNameType Params[] = {
1307 std::make_pair(".global_tid.", KmpInt32PtrTy),
1308 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1309 std::make_pair(StringRef(), QualType()) // __context with shared vars
1310 };
1311 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1312 Params);
1313 break;
1314 }
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001315 case OMPD_taskgroup: {
1316 Sema::CapturedParamNameType Params[] = {
1317 std::make_pair(StringRef(), QualType()) // __context with shared vars
1318 };
1319 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1320 Params);
1321 break;
1322 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001323 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001324 case OMPD_taskyield:
1325 case OMPD_barrier:
1326 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001327 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001328 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00001329 case OMPD_flush:
Alexey Bataev9959db52014-05-06 10:08:46 +00001330 llvm_unreachable("OpenMP Directive is not allowed");
1331 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001332 llvm_unreachable("Unknown OpenMP directive");
1333 }
1334}
1335
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001336StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1337 ArrayRef<OMPClause *> Clauses) {
1338 if (!S.isUsable()) {
1339 ActOnCapturedRegionError();
1340 return StmtError();
1341 }
Alexey Bataev040d5402015-05-12 08:35:28 +00001342 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001343 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001344 if (isOpenMPPrivate(Clause->getClauseKind()) ||
1345 Clause->getClauseKind() == OMPC_copyprivate) {
Alexey Bataev040d5402015-05-12 08:35:28 +00001346 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001347 for (auto *VarRef : Clause->children()) {
1348 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001349 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001350 }
1351 }
Alexey Bataev040d5402015-05-12 08:35:28 +00001352 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
1353 Clause->getClauseKind() == OMPC_schedule) {
1354 // Mark all variables in private list clauses as used in inner region.
1355 // Required for proper codegen of combined directives.
1356 // TODO: add processing for other clauses.
1357 if (auto *E = cast_or_null<Expr>(
1358 cast<OMPScheduleClause>(Clause)->getHelperChunkSize())) {
1359 MarkDeclarationsReferencedInExpr(E);
1360 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001361 }
1362 }
1363 return ActOnCapturedRegionEnd(S.get());
1364}
1365
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001366static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1367 OpenMPDirectiveKind CurrentRegion,
1368 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001369 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001370 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001371 // Allowed nesting of constructs
1372 // +------------------+-----------------+------------------------------------+
1373 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1374 // +------------------+-----------------+------------------------------------+
1375 // | parallel | parallel | * |
1376 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001377 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001378 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001379 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001380 // | parallel | simd | * |
1381 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001382 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001383 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001384 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001385 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001386 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001387 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001388 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001389 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001390 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001391 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001392 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001393 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001394 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001395 // | parallel | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001396 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001397 // | parallel | cancellation | |
1398 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001399 // | parallel | cancel | ! |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001400 // +------------------+-----------------+------------------------------------+
1401 // | for | parallel | * |
1402 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001403 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001404 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001405 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001406 // | for | simd | * |
1407 // | for | sections | + |
1408 // | for | section | + |
1409 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001410 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001411 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001412 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001413 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001414 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001415 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001416 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001417 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001418 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001419 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001420 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001421 // | for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001422 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001423 // | for | cancellation | |
1424 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001425 // | for | cancel | ! |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001426 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001427 // | master | parallel | * |
1428 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001429 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001430 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001431 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001432 // | master | simd | * |
1433 // | master | sections | + |
1434 // | master | section | + |
1435 // | master | single | + |
1436 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001437 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001438 // | master |parallel sections| * |
1439 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001440 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001441 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001442 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001443 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001444 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001445 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001446 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001447 // | master | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001448 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001449 // | master | cancellation | |
1450 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001451 // | master | cancel | |
Alexander Musman80c22892014-07-17 08:54:58 +00001452 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001453 // | critical | parallel | * |
1454 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001455 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001456 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001457 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001458 // | critical | simd | * |
1459 // | critical | sections | + |
1460 // | critical | section | + |
1461 // | critical | single | + |
1462 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001463 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001464 // | critical |parallel sections| * |
1465 // | critical | task | * |
1466 // | critical | taskyield | * |
1467 // | critical | barrier | + |
1468 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001469 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001470 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001471 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001472 // | critical | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001473 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001474 // | critical | cancellation | |
1475 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001476 // | critical | cancel | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001477 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001478 // | simd | parallel | |
1479 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001480 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00001481 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001482 // | simd | critical | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001483 // | simd | simd | |
1484 // | simd | sections | |
1485 // | simd | section | |
1486 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001487 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001488 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001489 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001490 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00001491 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001492 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001493 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001494 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00001495 // | simd | flush | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001496 // | simd | ordered | |
Alexey Bataev0162e452014-07-22 10:10:35 +00001497 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001498 // | simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001499 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001500 // | simd | cancellation | |
1501 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001502 // | simd | cancel | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001503 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00001504 // | for simd | parallel | |
1505 // | for simd | for | |
1506 // | for simd | for simd | |
1507 // | for simd | master | |
1508 // | for simd | critical | |
1509 // | for simd | simd | |
1510 // | for simd | sections | |
1511 // | for simd | section | |
1512 // | for simd | single | |
1513 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001514 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001515 // | for simd |parallel sections| |
1516 // | for simd | task | |
1517 // | for simd | taskyield | |
1518 // | for simd | barrier | |
1519 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001520 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001521 // | for simd | flush | |
1522 // | for simd | ordered | |
1523 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001524 // | for simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001525 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001526 // | for simd | cancellation | |
1527 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001528 // | for simd | cancel | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001529 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00001530 // | parallel for simd| parallel | |
1531 // | parallel for simd| for | |
1532 // | parallel for simd| for simd | |
1533 // | parallel for simd| master | |
1534 // | parallel for simd| critical | |
1535 // | parallel for simd| simd | |
1536 // | parallel for simd| sections | |
1537 // | parallel for simd| section | |
1538 // | parallel for simd| single | |
1539 // | parallel for simd| parallel for | |
1540 // | parallel for simd|parallel for simd| |
1541 // | parallel for simd|parallel sections| |
1542 // | parallel for simd| task | |
1543 // | parallel for simd| taskyield | |
1544 // | parallel for simd| barrier | |
1545 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001546 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001547 // | parallel for simd| flush | |
1548 // | parallel for simd| ordered | |
1549 // | parallel for simd| atomic | |
1550 // | parallel for simd| target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001551 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001552 // | parallel for simd| cancellation | |
1553 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001554 // | parallel for simd| cancel | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001555 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001556 // | sections | parallel | * |
1557 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001558 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001559 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001560 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001561 // | sections | simd | * |
1562 // | sections | sections | + |
1563 // | sections | section | * |
1564 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001565 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001566 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001567 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001568 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001569 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001570 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001571 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001572 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001573 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001574 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001575 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001576 // | sections | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001577 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001578 // | sections | cancellation | |
1579 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001580 // | sections | cancel | ! |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001581 // +------------------+-----------------+------------------------------------+
1582 // | section | parallel | * |
1583 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001584 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001585 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001586 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001587 // | section | simd | * |
1588 // | section | sections | + |
1589 // | section | section | + |
1590 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001591 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001592 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001593 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001594 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001595 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001596 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001597 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001598 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001599 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001600 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001601 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001602 // | section | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001603 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001604 // | section | cancellation | |
1605 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001606 // | section | cancel | ! |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001607 // +------------------+-----------------+------------------------------------+
1608 // | single | parallel | * |
1609 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001610 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001611 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001612 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001613 // | single | simd | * |
1614 // | single | sections | + |
1615 // | single | section | + |
1616 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001617 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001618 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001619 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001620 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001621 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001622 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001623 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001624 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001625 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001626 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001627 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001628 // | single | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001629 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001630 // | single | cancellation | |
1631 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001632 // | single | cancel | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001633 // +------------------+-----------------+------------------------------------+
1634 // | parallel for | parallel | * |
1635 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001636 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001637 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001638 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001639 // | parallel for | simd | * |
1640 // | parallel for | sections | + |
1641 // | parallel for | section | + |
1642 // | parallel for | single | + |
1643 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001644 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001645 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001646 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001647 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001648 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001649 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001650 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001651 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001652 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001653 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001654 // | parallel for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001655 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001656 // | parallel for | cancellation | |
1657 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001658 // | parallel for | cancel | ! |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001659 // +------------------+-----------------+------------------------------------+
1660 // | parallel sections| parallel | * |
1661 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001662 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001663 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001664 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001665 // | parallel sections| simd | * |
1666 // | parallel sections| sections | + |
1667 // | parallel sections| section | * |
1668 // | parallel sections| single | + |
1669 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001670 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001671 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001672 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001673 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001674 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001675 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001676 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001677 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001678 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001679 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001680 // | parallel sections| target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001681 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001682 // | parallel sections| cancellation | |
1683 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001684 // | parallel sections| cancel | ! |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001685 // +------------------+-----------------+------------------------------------+
1686 // | task | parallel | * |
1687 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001688 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001689 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001690 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001691 // | task | simd | * |
1692 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001693 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001694 // | task | single | + |
1695 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001696 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001697 // | task |parallel sections| * |
1698 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001699 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001700 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001701 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001702 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001703 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001704 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001705 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001706 // | task | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001707 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001708 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00001709 // | | point | ! |
1710 // | task | cancel | ! |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001711 // +------------------+-----------------+------------------------------------+
1712 // | ordered | parallel | * |
1713 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001714 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001715 // | ordered | master | * |
1716 // | ordered | critical | * |
1717 // | ordered | simd | * |
1718 // | ordered | sections | + |
1719 // | ordered | section | + |
1720 // | ordered | single | + |
1721 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001722 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001723 // | ordered |parallel sections| * |
1724 // | ordered | task | * |
1725 // | ordered | taskyield | * |
1726 // | ordered | barrier | + |
1727 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001728 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001729 // | ordered | flush | * |
1730 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001731 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001732 // | ordered | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001733 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001734 // | ordered | cancellation | |
1735 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001736 // | ordered | cancel | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001737 // +------------------+-----------------+------------------------------------+
1738 // | atomic | parallel | |
1739 // | atomic | for | |
1740 // | atomic | for simd | |
1741 // | atomic | master | |
1742 // | atomic | critical | |
1743 // | atomic | simd | |
1744 // | atomic | sections | |
1745 // | atomic | section | |
1746 // | atomic | single | |
1747 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001748 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001749 // | atomic |parallel sections| |
1750 // | atomic | task | |
1751 // | atomic | taskyield | |
1752 // | atomic | barrier | |
1753 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001754 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001755 // | atomic | flush | |
1756 // | atomic | ordered | |
1757 // | atomic | atomic | |
1758 // | atomic | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001759 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001760 // | atomic | cancellation | |
1761 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001762 // | atomic | cancel | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001763 // +------------------+-----------------+------------------------------------+
1764 // | target | parallel | * |
1765 // | target | for | * |
1766 // | target | for simd | * |
1767 // | target | master | * |
1768 // | target | critical | * |
1769 // | target | simd | * |
1770 // | target | sections | * |
1771 // | target | section | * |
1772 // | target | single | * |
1773 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001774 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001775 // | target |parallel sections| * |
1776 // | target | task | * |
1777 // | target | taskyield | * |
1778 // | target | barrier | * |
1779 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001780 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001781 // | target | flush | * |
1782 // | target | ordered | * |
1783 // | target | atomic | * |
1784 // | target | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001785 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001786 // | target | cancellation | |
1787 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001788 // | target | cancel | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001789 // +------------------+-----------------+------------------------------------+
1790 // | teams | parallel | * |
1791 // | teams | for | + |
1792 // | teams | for simd | + |
1793 // | teams | master | + |
1794 // | teams | critical | + |
1795 // | teams | simd | + |
1796 // | teams | sections | + |
1797 // | teams | section | + |
1798 // | teams | single | + |
1799 // | teams | parallel for | * |
1800 // | teams |parallel for simd| * |
1801 // | teams |parallel sections| * |
1802 // | teams | task | + |
1803 // | teams | taskyield | + |
1804 // | teams | barrier | + |
1805 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00001806 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001807 // | teams | flush | + |
1808 // | teams | ordered | + |
1809 // | teams | atomic | + |
1810 // | teams | target | + |
1811 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001812 // | teams | cancellation | |
1813 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001814 // | teams | cancel | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001815 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00001816 if (Stack->getCurScope()) {
1817 auto ParentRegion = Stack->getParentDirective();
1818 bool NestingProhibited = false;
1819 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001820 enum {
1821 NoRecommend,
1822 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00001823 ShouldBeInOrderedRegion,
1824 ShouldBeInTargetRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001825 } Recommend = NoRecommend;
Alexey Bataev549210e2014-06-24 04:39:47 +00001826 if (isOpenMPSimdDirective(ParentRegion)) {
1827 // OpenMP [2.16, Nesting of Regions]
1828 // OpenMP constructs may not be nested inside a simd region.
1829 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
1830 return true;
1831 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001832 if (ParentRegion == OMPD_atomic) {
1833 // OpenMP [2.16, Nesting of Regions]
1834 // OpenMP constructs may not be nested inside an atomic region.
1835 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
1836 return true;
1837 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001838 if (CurrentRegion == OMPD_section) {
1839 // OpenMP [2.7.2, sections Construct, Restrictions]
1840 // Orphaned section directives are prohibited. That is, the section
1841 // directives must appear within the sections construct and must not be
1842 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001843 if (ParentRegion != OMPD_sections &&
1844 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001845 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
1846 << (ParentRegion != OMPD_unknown)
1847 << getOpenMPDirectiveName(ParentRegion);
1848 return true;
1849 }
1850 return false;
1851 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001852 // Allow some constructs to be orphaned (they could be used in functions,
1853 // called from OpenMP regions with the required preconditions).
1854 if (ParentRegion == OMPD_unknown)
1855 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00001856 if (CurrentRegion == OMPD_cancellation_point ||
1857 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001858 // OpenMP [2.16, Nesting of Regions]
1859 // A cancellation point construct for which construct-type-clause is
1860 // taskgroup must be nested inside a task construct. A cancellation
1861 // point construct for which construct-type-clause is not taskgroup must
1862 // be closely nested inside an OpenMP construct that matches the type
1863 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00001864 // A cancel construct for which construct-type-clause is taskgroup must be
1865 // nested inside a task construct. A cancel construct for which
1866 // construct-type-clause is not taskgroup must be closely nested inside an
1867 // OpenMP construct that matches the type specified in
1868 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001869 NestingProhibited =
1870 !((CancelRegion == OMPD_parallel && ParentRegion == OMPD_parallel) ||
1871 (CancelRegion == OMPD_for && ParentRegion == OMPD_for) ||
1872 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
1873 (CancelRegion == OMPD_sections &&
1874 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections)));
1875 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00001876 // OpenMP [2.16, Nesting of Regions]
1877 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00001878 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00001879 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
1880 ParentRegion == OMPD_task;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001881 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
1882 // OpenMP [2.16, Nesting of Regions]
1883 // A critical region may not be nested (closely or otherwise) inside a
1884 // critical region with the same name. Note that this restriction is not
1885 // sufficient to prevent deadlock.
1886 SourceLocation PreviousCriticalLoc;
1887 bool DeadLock =
1888 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
1889 OpenMPDirectiveKind K,
1890 const DeclarationNameInfo &DNI,
1891 SourceLocation Loc)
1892 ->bool {
1893 if (K == OMPD_critical &&
1894 DNI.getName() == CurrentName.getName()) {
1895 PreviousCriticalLoc = Loc;
1896 return true;
1897 } else
1898 return false;
1899 },
1900 false /* skip top directive */);
1901 if (DeadLock) {
1902 SemaRef.Diag(StartLoc,
1903 diag::err_omp_prohibited_region_critical_same_name)
1904 << CurrentName.getName();
1905 if (PreviousCriticalLoc.isValid())
1906 SemaRef.Diag(PreviousCriticalLoc,
1907 diag::note_omp_previous_critical_region);
1908 return true;
1909 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001910 } else if (CurrentRegion == OMPD_barrier) {
1911 // OpenMP [2.16, Nesting of Regions]
1912 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00001913 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001914 NestingProhibited =
1915 isOpenMPWorksharingDirective(ParentRegion) ||
1916 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1917 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00001918 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00001919 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00001920 // OpenMP [2.16, Nesting of Regions]
1921 // A worksharing region may not be closely nested inside a worksharing,
1922 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001923 NestingProhibited =
Alexander Musmanf82886e2014-09-18 05:12:34 +00001924 isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001925 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1926 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
1927 Recommend = ShouldBeInParallelRegion;
1928 } else if (CurrentRegion == OMPD_ordered) {
1929 // OpenMP [2.16, Nesting of Regions]
1930 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00001931 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001932 // An ordered region must be closely nested inside a loop region (or
1933 // parallel loop region) with an ordered clause.
1934 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00001935 ParentRegion == OMPD_task ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001936 !Stack->isParentOrderedRegion();
1937 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00001938 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
1939 // OpenMP [2.16, Nesting of Regions]
1940 // If specified, a teams construct must be contained within a target
1941 // construct.
1942 NestingProhibited = ParentRegion != OMPD_target;
1943 Recommend = ShouldBeInTargetRegion;
1944 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
1945 }
1946 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
1947 // OpenMP [2.16, Nesting of Regions]
1948 // distribute, parallel, parallel sections, parallel workshare, and the
1949 // parallel loop and parallel loop SIMD constructs are the only OpenMP
1950 // constructs that can be closely nested in the teams region.
1951 // TODO: add distribute directive.
1952 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion);
1953 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00001954 }
1955 if (NestingProhibited) {
1956 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001957 << CloseNesting << getOpenMPDirectiveName(ParentRegion) << Recommend
1958 << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00001959 return true;
1960 }
1961 }
1962 return false;
1963}
1964
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001965StmtResult Sema::ActOnOpenMPExecutableDirective(
1966 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
1967 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
1968 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001969 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001970 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
1971 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00001972 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001973
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001974 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev68446b72014-07-18 07:47:19 +00001975 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001976 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00001977 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00001978 if (AStmt) {
1979 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
1980
1981 // Check default data sharing attributes for referenced variables.
1982 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
1983 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
1984 if (DSAChecker.isErrorFound())
1985 return StmtError();
1986 // Generate list of implicitly defined firstprivate variables.
1987 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00001988
1989 if (!DSAChecker.getImplicitFirstprivate().empty()) {
1990 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
1991 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
1992 SourceLocation(), SourceLocation())) {
1993 ClausesWithImplicit.push_back(Implicit);
1994 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
1995 DSAChecker.getImplicitFirstprivate().size();
1996 } else
1997 ErrorFound = true;
1998 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001999 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002000
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002001 switch (Kind) {
2002 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00002003 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
2004 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002005 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002006 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002007 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2008 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002009 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002010 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002011 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2012 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002013 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00002014 case OMPD_for_simd:
2015 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2016 EndLoc, VarsWithInheritedDSA);
2017 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002018 case OMPD_sections:
2019 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
2020 EndLoc);
2021 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002022 case OMPD_section:
2023 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00002024 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002025 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
2026 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002027 case OMPD_single:
2028 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
2029 EndLoc);
2030 break;
Alexander Musman80c22892014-07-17 08:54:58 +00002031 case OMPD_master:
2032 assert(ClausesWithImplicit.empty() &&
2033 "No clauses are allowed for 'omp master' directive");
2034 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
2035 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002036 case OMPD_critical:
2037 assert(ClausesWithImplicit.empty() &&
2038 "No clauses are allowed for 'omp critical' directive");
2039 Res = ActOnOpenMPCriticalDirective(DirName, AStmt, StartLoc, EndLoc);
2040 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002041 case OMPD_parallel_for:
2042 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
2043 EndLoc, VarsWithInheritedDSA);
2044 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00002045 case OMPD_parallel_for_simd:
2046 Res = ActOnOpenMPParallelForSimdDirective(
2047 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
2048 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002049 case OMPD_parallel_sections:
2050 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
2051 StartLoc, EndLoc);
2052 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002053 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002054 Res =
2055 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
2056 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00002057 case OMPD_taskyield:
2058 assert(ClausesWithImplicit.empty() &&
2059 "No clauses are allowed for 'omp taskyield' directive");
2060 assert(AStmt == nullptr &&
2061 "No associated statement allowed for 'omp taskyield' directive");
2062 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
2063 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002064 case OMPD_barrier:
2065 assert(ClausesWithImplicit.empty() &&
2066 "No clauses are allowed for 'omp barrier' directive");
2067 assert(AStmt == nullptr &&
2068 "No associated statement allowed for 'omp barrier' directive");
2069 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
2070 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00002071 case OMPD_taskwait:
2072 assert(ClausesWithImplicit.empty() &&
2073 "No clauses are allowed for 'omp taskwait' directive");
2074 assert(AStmt == nullptr &&
2075 "No associated statement allowed for 'omp taskwait' directive");
2076 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
2077 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002078 case OMPD_taskgroup:
2079 assert(ClausesWithImplicit.empty() &&
2080 "No clauses are allowed for 'omp taskgroup' directive");
2081 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
2082 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00002083 case OMPD_flush:
2084 assert(AStmt == nullptr &&
2085 "No associated statement allowed for 'omp flush' directive");
2086 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
2087 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002088 case OMPD_ordered:
2089 assert(ClausesWithImplicit.empty() &&
2090 "No clauses are allowed for 'omp ordered' directive");
2091 Res = ActOnOpenMPOrderedDirective(AStmt, StartLoc, EndLoc);
2092 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00002093 case OMPD_atomic:
2094 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
2095 EndLoc);
2096 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002097 case OMPD_teams:
2098 Res =
2099 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
2100 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002101 case OMPD_target:
2102 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
2103 EndLoc);
2104 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002105 case OMPD_cancellation_point:
2106 assert(ClausesWithImplicit.empty() &&
2107 "No clauses are allowed for 'omp cancellation point' directive");
2108 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
2109 "cancellation point' directive");
2110 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
2111 break;
Alexey Bataev80909872015-07-02 11:25:17 +00002112 case OMPD_cancel:
2113 assert(ClausesWithImplicit.empty() &&
2114 "No clauses are allowed for 'omp cancel' directive");
2115 assert(AStmt == nullptr &&
2116 "No associated statement allowed for 'omp cancel' directive");
2117 Res = ActOnOpenMPCancelDirective(StartLoc, EndLoc, CancelRegion);
2118 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002119 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002120 llvm_unreachable("OpenMP Directive is not allowed");
2121 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002122 llvm_unreachable("Unknown OpenMP directive");
2123 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002124
Alexey Bataev4acb8592014-07-07 13:01:15 +00002125 for (auto P : VarsWithInheritedDSA) {
2126 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
2127 << P.first << P.second->getSourceRange();
2128 }
2129 if (!VarsWithInheritedDSA.empty())
2130 return StmtError();
2131
Alexey Bataeved09d242014-05-28 05:53:51 +00002132 if (ErrorFound)
2133 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002134 return Res;
2135}
2136
2137StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
2138 Stmt *AStmt,
2139 SourceLocation StartLoc,
2140 SourceLocation EndLoc) {
Alexey Bataev9959db52014-05-06 10:08:46 +00002141 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2142 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
2143 // 1.2.2 OpenMP Language Terminology
2144 // Structured block - An executable statement with a single entry at the
2145 // top and a single exit at the bottom.
2146 // The point of exit cannot be a branch out of the structured block.
2147 // longjmp() and throw() must not violate the entry/exit criteria.
2148 CS->getCapturedDecl()->setNothrow();
2149
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002150 getCurFunction()->setHasBranchProtectedScope();
2151
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00002152 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
2153 AStmt);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002154}
2155
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002156namespace {
2157/// \brief Helper class for checking canonical form of the OpenMP loops and
2158/// extracting iteration space of each loop in the loop nest, that will be used
2159/// for IR generation.
2160class OpenMPIterationSpaceChecker {
2161 /// \brief Reference to Sema.
2162 Sema &SemaRef;
2163 /// \brief A location for diagnostics (when there is no some better location).
2164 SourceLocation DefaultLoc;
2165 /// \brief A location for diagnostics (when increment is not compatible).
2166 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002167 /// \brief A source location for referring to loop init later.
2168 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002169 /// \brief A source location for referring to condition later.
2170 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002171 /// \brief A source location for referring to increment later.
2172 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002173 /// \brief Loop variable.
2174 VarDecl *Var;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002175 /// \brief Reference to loop variable.
2176 DeclRefExpr *VarRef;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002177 /// \brief Lower bound (initializer for the var).
2178 Expr *LB;
2179 /// \brief Upper bound.
2180 Expr *UB;
2181 /// \brief Loop step (increment).
2182 Expr *Step;
2183 /// \brief This flag is true when condition is one of:
2184 /// Var < UB
2185 /// Var <= UB
2186 /// UB > Var
2187 /// UB >= Var
2188 bool TestIsLessOp;
2189 /// \brief This flag is true when condition is strict ( < or > ).
2190 bool TestIsStrictOp;
2191 /// \brief This flag is true when step is subtracted on each iteration.
2192 bool SubtractStep;
2193
2194public:
2195 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
2196 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
Alexander Musmana5f070a2014-10-01 06:03:56 +00002197 InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()),
2198 IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002199 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
2200 TestIsStrictOp(false), SubtractStep(false) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002201 /// \brief Check init-expr for canonical loop form and save loop counter
2202 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00002203 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002204 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
2205 /// for less/greater and for strict/non-strict comparison.
2206 bool CheckCond(Expr *S);
2207 /// \brief Check incr-expr for canonical loop form and return true if it
2208 /// does not conform, otherwise save loop step (#Step).
2209 bool CheckInc(Expr *S);
2210 /// \brief Return the loop counter variable.
2211 VarDecl *GetLoopVar() const { return Var; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002212 /// \brief Return the reference expression to loop counter variable.
2213 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002214 /// \brief Source range of the loop init.
2215 SourceRange GetInitSrcRange() const { return InitSrcRange; }
2216 /// \brief Source range of the loop condition.
2217 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
2218 /// \brief Source range of the loop increment.
2219 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
2220 /// \brief True if the step should be subtracted.
2221 bool ShouldSubtractStep() const { return SubtractStep; }
2222 /// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00002223 Expr *BuildNumIterations(Scope *S, const bool LimitedType) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00002224 /// \brief Build the precondition expression for the loops.
2225 Expr *BuildPreCond(Scope *S, Expr *Cond) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002226 /// \brief Build reference expression to the counter be used for codegen.
2227 Expr *BuildCounterVar() const;
2228 /// \brief Build initization of the counter be used for codegen.
2229 Expr *BuildCounterInit() const;
2230 /// \brief Build step of the counter be used for codegen.
2231 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002232 /// \brief Return true if any expression is dependent.
2233 bool Dependent() const;
2234
2235private:
2236 /// \brief Check the right-hand side of an assignment in the increment
2237 /// expression.
2238 bool CheckIncRHS(Expr *RHS);
2239 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002240 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002241 /// \brief Helper to set upper bound.
2242 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, const SourceRange &SR,
2243 const SourceLocation &SL);
2244 /// \brief Helper to set loop increment.
2245 bool SetStep(Expr *NewStep, bool Subtract);
2246};
2247
2248bool OpenMPIterationSpaceChecker::Dependent() const {
2249 if (!Var) {
2250 assert(!LB && !UB && !Step);
2251 return false;
2252 }
2253 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
2254 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
2255}
2256
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002257bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
2258 DeclRefExpr *NewVarRefExpr,
2259 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002260 // State consistency checking to ensure correct usage.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002261 assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
2262 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002263 if (!NewVar || !NewLB)
2264 return true;
2265 Var = NewVar;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002266 VarRef = NewVarRefExpr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002267 LB = NewLB;
2268 return false;
2269}
2270
2271bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
2272 const SourceRange &SR,
2273 const SourceLocation &SL) {
2274 // State consistency checking to ensure correct usage.
2275 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
2276 !TestIsLessOp && !TestIsStrictOp);
2277 if (!NewUB)
2278 return true;
2279 UB = NewUB;
2280 TestIsLessOp = LessOp;
2281 TestIsStrictOp = StrictOp;
2282 ConditionSrcRange = SR;
2283 ConditionLoc = SL;
2284 return false;
2285}
2286
2287bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
2288 // State consistency checking to ensure correct usage.
2289 assert(Var != nullptr && LB != nullptr && Step == nullptr);
2290 if (!NewStep)
2291 return true;
2292 if (!NewStep->isValueDependent()) {
2293 // Check that the step is integer expression.
2294 SourceLocation StepLoc = NewStep->getLocStart();
2295 ExprResult Val =
2296 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
2297 if (Val.isInvalid())
2298 return true;
2299 NewStep = Val.get();
2300
2301 // OpenMP [2.6, Canonical Loop Form, Restrictions]
2302 // If test-expr is of form var relational-op b and relational-op is < or
2303 // <= then incr-expr must cause var to increase on each iteration of the
2304 // loop. If test-expr is of form var relational-op b and relational-op is
2305 // > or >= then incr-expr must cause var to decrease on each iteration of
2306 // the loop.
2307 // If test-expr is of form b relational-op var and relational-op is < or
2308 // <= then incr-expr must cause var to decrease on each iteration of the
2309 // loop. If test-expr is of form b relational-op var and relational-op is
2310 // > or >= then incr-expr must cause var to increase on each iteration of
2311 // the loop.
2312 llvm::APSInt Result;
2313 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
2314 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
2315 bool IsConstNeg =
2316 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00002317 bool IsConstPos =
2318 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002319 bool IsConstZero = IsConstant && !Result.getBoolValue();
2320 if (UB && (IsConstZero ||
2321 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00002322 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002323 SemaRef.Diag(NewStep->getExprLoc(),
2324 diag::err_omp_loop_incr_not_compatible)
2325 << Var << TestIsLessOp << NewStep->getSourceRange();
2326 SemaRef.Diag(ConditionLoc,
2327 diag::note_omp_loop_cond_requres_compatible_incr)
2328 << TestIsLessOp << ConditionSrcRange;
2329 return true;
2330 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002331 if (TestIsLessOp == Subtract) {
2332 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
2333 NewStep).get();
2334 Subtract = !Subtract;
2335 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002336 }
2337
2338 Step = NewStep;
2339 SubtractStep = Subtract;
2340 return false;
2341}
2342
Alexey Bataev9c821032015-04-30 04:23:23 +00002343bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002344 // Check init-expr for canonical loop form and save loop counter
2345 // variable - #Var and its initialization value - #LB.
2346 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
2347 // var = lb
2348 // integer-type var = lb
2349 // random-access-iterator-type var = lb
2350 // pointer-type var = lb
2351 //
2352 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00002353 if (EmitDiags) {
2354 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
2355 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002356 return true;
2357 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002358 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002359 if (Expr *E = dyn_cast<Expr>(S))
2360 S = E->IgnoreParens();
2361 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2362 if (BO->getOpcode() == BO_Assign)
2363 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002364 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002365 BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002366 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
2367 if (DS->isSingleDecl()) {
2368 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
2369 if (Var->hasInit()) {
2370 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00002371 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002372 SemaRef.Diag(S->getLocStart(),
2373 diag::ext_omp_loop_not_canonical_init)
2374 << S->getSourceRange();
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002375 return SetVarAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002376 }
2377 }
2378 }
2379 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
2380 if (CE->getOperator() == OO_Equal)
2381 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002382 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
2383 CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002384
Alexey Bataev9c821032015-04-30 04:23:23 +00002385 if (EmitDiags) {
2386 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
2387 << S->getSourceRange();
2388 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002389 return true;
2390}
2391
Alexey Bataev23b69422014-06-18 07:08:49 +00002392/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002393/// variable (which may be the loop variable) if possible.
2394static const VarDecl *GetInitVarDecl(const Expr *E) {
2395 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00002396 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002397 E = E->IgnoreParenImpCasts();
2398 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
2399 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
2400 if (Ctor->isCopyConstructor() && CE->getNumArgs() == 1 &&
2401 CE->getArg(0) != nullptr)
2402 E = CE->getArg(0)->IgnoreParenImpCasts();
2403 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
2404 if (!DRE)
2405 return nullptr;
2406 return dyn_cast<VarDecl>(DRE->getDecl());
2407}
2408
2409bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
2410 // Check test-expr for canonical form, save upper-bound UB, flags for
2411 // less/greater and for strict/non-strict comparison.
2412 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2413 // var relational-op b
2414 // b relational-op var
2415 //
2416 if (!S) {
2417 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
2418 return true;
2419 }
2420 S = S->IgnoreParenImpCasts();
2421 SourceLocation CondLoc = S->getLocStart();
2422 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2423 if (BO->isRelationalOp()) {
2424 if (GetInitVarDecl(BO->getLHS()) == Var)
2425 return SetUB(BO->getRHS(),
2426 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
2427 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2428 BO->getSourceRange(), BO->getOperatorLoc());
2429 if (GetInitVarDecl(BO->getRHS()) == Var)
2430 return SetUB(BO->getLHS(),
2431 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
2432 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2433 BO->getSourceRange(), BO->getOperatorLoc());
2434 }
2435 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2436 if (CE->getNumArgs() == 2) {
2437 auto Op = CE->getOperator();
2438 switch (Op) {
2439 case OO_Greater:
2440 case OO_GreaterEqual:
2441 case OO_Less:
2442 case OO_LessEqual:
2443 if (GetInitVarDecl(CE->getArg(0)) == Var)
2444 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
2445 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2446 CE->getOperatorLoc());
2447 if (GetInitVarDecl(CE->getArg(1)) == Var)
2448 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
2449 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2450 CE->getOperatorLoc());
2451 break;
2452 default:
2453 break;
2454 }
2455 }
2456 }
2457 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
2458 << S->getSourceRange() << Var;
2459 return true;
2460}
2461
2462bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
2463 // RHS of canonical loop form increment can be:
2464 // var + incr
2465 // incr + var
2466 // var - incr
2467 //
2468 RHS = RHS->IgnoreParenImpCasts();
2469 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
2470 if (BO->isAdditiveOp()) {
2471 bool IsAdd = BO->getOpcode() == BO_Add;
2472 if (GetInitVarDecl(BO->getLHS()) == Var)
2473 return SetStep(BO->getRHS(), !IsAdd);
2474 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
2475 return SetStep(BO->getLHS(), false);
2476 }
2477 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
2478 bool IsAdd = CE->getOperator() == OO_Plus;
2479 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
2480 if (GetInitVarDecl(CE->getArg(0)) == Var)
2481 return SetStep(CE->getArg(1), !IsAdd);
2482 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
2483 return SetStep(CE->getArg(0), false);
2484 }
2485 }
2486 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2487 << RHS->getSourceRange() << Var;
2488 return true;
2489}
2490
2491bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
2492 // Check incr-expr for canonical loop form and return true if it
2493 // does not conform.
2494 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2495 // ++var
2496 // var++
2497 // --var
2498 // var--
2499 // var += incr
2500 // var -= incr
2501 // var = var + incr
2502 // var = incr + var
2503 // var = var - incr
2504 //
2505 if (!S) {
2506 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
2507 return true;
2508 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002509 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002510 S = S->IgnoreParens();
2511 if (auto UO = dyn_cast<UnaryOperator>(S)) {
2512 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
2513 return SetStep(
2514 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
2515 (UO->isDecrementOp() ? -1 : 1)).get(),
2516 false);
2517 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
2518 switch (BO->getOpcode()) {
2519 case BO_AddAssign:
2520 case BO_SubAssign:
2521 if (GetInitVarDecl(BO->getLHS()) == Var)
2522 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
2523 break;
2524 case BO_Assign:
2525 if (GetInitVarDecl(BO->getLHS()) == Var)
2526 return CheckIncRHS(BO->getRHS());
2527 break;
2528 default:
2529 break;
2530 }
2531 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2532 switch (CE->getOperator()) {
2533 case OO_PlusPlus:
2534 case OO_MinusMinus:
2535 if (GetInitVarDecl(CE->getArg(0)) == Var)
2536 return SetStep(
2537 SemaRef.ActOnIntegerConstant(
2538 CE->getLocStart(),
2539 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
2540 false);
2541 break;
2542 case OO_PlusEqual:
2543 case OO_MinusEqual:
2544 if (GetInitVarDecl(CE->getArg(0)) == Var)
2545 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
2546 break;
2547 case OO_Equal:
2548 if (GetInitVarDecl(CE->getArg(0)) == Var)
2549 return CheckIncRHS(CE->getArg(1));
2550 break;
2551 default:
2552 break;
2553 }
2554 }
2555 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2556 << S->getSourceRange() << Var;
2557 return true;
2558}
Alexander Musmana5f070a2014-10-01 06:03:56 +00002559
2560/// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00002561Expr *
2562OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S,
2563 const bool LimitedType) const {
Alexander Musmana5f070a2014-10-01 06:03:56 +00002564 ExprResult Diff;
2565 if (Var->getType()->isIntegerType() || Var->getType()->isPointerType() ||
2566 SemaRef.getLangOpts().CPlusPlus) {
2567 // Upper - Lower
2568 Expr *Upper = TestIsLessOp ? UB : LB;
2569 Expr *Lower = TestIsLessOp ? LB : UB;
2570
2571 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
2572
2573 if (!Diff.isUsable() && Var->getType()->getAsCXXRecordDecl()) {
2574 // BuildBinOp already emitted error, this one is to point user to upper
2575 // and lower bound, and to tell what is passed to 'operator-'.
2576 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
2577 << Upper->getSourceRange() << Lower->getSourceRange();
2578 return nullptr;
2579 }
2580 }
2581
2582 if (!Diff.isUsable())
2583 return nullptr;
2584
2585 // Upper - Lower [- 1]
2586 if (TestIsStrictOp)
2587 Diff = SemaRef.BuildBinOp(
2588 S, DefaultLoc, BO_Sub, Diff.get(),
2589 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
2590 if (!Diff.isUsable())
2591 return nullptr;
2592
2593 // Upper - Lower [- 1] + Step
2594 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(),
2595 Step->IgnoreImplicit());
2596 if (!Diff.isUsable())
2597 return nullptr;
2598
2599 // Parentheses (for dumping/debugging purposes only).
2600 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
2601 if (!Diff.isUsable())
2602 return nullptr;
2603
2604 // (Upper - Lower [- 1] + Step) / Step
2605 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(),
2606 Step->IgnoreImplicit());
2607 if (!Diff.isUsable())
2608 return nullptr;
2609
Alexander Musman174b3ca2014-10-06 11:16:29 +00002610 // OpenMP runtime requires 32-bit or 64-bit loop variables.
2611 if (LimitedType) {
2612 auto &C = SemaRef.Context;
2613 QualType Type = Diff.get()->getType();
2614 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
2615 if (NewSize != C.getTypeSize(Type)) {
2616 if (NewSize < C.getTypeSize(Type)) {
2617 assert(NewSize == 64 && "incorrect loop var size");
2618 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
2619 << InitSrcRange << ConditionSrcRange;
2620 }
2621 QualType NewType = C.getIntTypeForBitwidth(
2622 NewSize, Type->hasSignedIntegerRepresentation());
2623 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
2624 Sema::AA_Converting, true);
2625 if (!Diff.isUsable())
2626 return nullptr;
2627 }
2628 }
2629
Alexander Musmana5f070a2014-10-01 06:03:56 +00002630 return Diff.get();
2631}
2632
Alexey Bataev62dbb972015-04-22 11:59:37 +00002633Expr *OpenMPIterationSpaceChecker::BuildPreCond(Scope *S, Expr *Cond) const {
2634 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
2635 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
2636 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
2637 auto CondExpr = SemaRef.BuildBinOp(
2638 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
2639 : (TestIsStrictOp ? BO_GT : BO_GE),
2640 LB, UB);
2641 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
2642 // Otherwise use original loop conditon and evaluate it in runtime.
2643 return CondExpr.isUsable() ? CondExpr.get() : Cond;
2644}
2645
Alexander Musmana5f070a2014-10-01 06:03:56 +00002646/// \brief Build reference expression to the counter be used for codegen.
2647Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const {
Alexey Bataev39f915b82015-05-08 10:41:21 +00002648 return buildDeclRefExpr(SemaRef, Var, Var->getType(), DefaultLoc);
Alexander Musmana5f070a2014-10-01 06:03:56 +00002649}
2650
2651/// \brief Build initization of the counter be used for codegen.
2652Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
2653
2654/// \brief Build step of the counter be used for codegen.
2655Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
2656
2657/// \brief Iteration space of a single for loop.
2658struct LoopIterationSpace {
Alexey Bataev62dbb972015-04-22 11:59:37 +00002659 /// \brief Condition of the loop.
2660 Expr *PreCond;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002661 /// \brief This expression calculates the number of iterations in the loop.
2662 /// It is always possible to calculate it before starting the loop.
2663 Expr *NumIterations;
2664 /// \brief The loop counter variable.
2665 Expr *CounterVar;
2666 /// \brief This is initializer for the initial value of #CounterVar.
2667 Expr *CounterInit;
2668 /// \brief This is step for the #CounterVar used to generate its update:
2669 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
2670 Expr *CounterStep;
2671 /// \brief Should step be subtracted?
2672 bool Subtract;
2673 /// \brief Source range of the loop init.
2674 SourceRange InitSrcRange;
2675 /// \brief Source range of the loop condition.
2676 SourceRange CondSrcRange;
2677 /// \brief Source range of the loop increment.
2678 SourceRange IncSrcRange;
2679};
2680
Alexey Bataev23b69422014-06-18 07:08:49 +00002681} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002682
Alexey Bataev9c821032015-04-30 04:23:23 +00002683void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
2684 assert(getLangOpts().OpenMP && "OpenMP is not active.");
2685 assert(Init && "Expected loop in canonical form.");
2686 unsigned CollapseIteration = DSAStack->getCollapseNumber();
2687 if (CollapseIteration > 0 &&
2688 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
2689 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
2690 if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
2691 DSAStack->addLoopControlVariable(ISC.GetLoopVar());
2692 }
2693 DSAStack->setCollapseNumber(CollapseIteration - 1);
2694 }
2695}
2696
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002697/// \brief Called on a for stmt to check and extract its iteration space
2698/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00002699static bool CheckOpenMPIterationSpace(
2700 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
2701 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
2702 Expr *NestedLoopCountExpr,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002703 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
2704 LoopIterationSpace &ResultIterSpace) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002705 // OpenMP [2.6, Canonical Loop Form]
2706 // for (init-expr; test-expr; incr-expr) structured-block
2707 auto For = dyn_cast_or_null<ForStmt>(S);
2708 if (!For) {
2709 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002710 << (NestedLoopCountExpr != nullptr) << getOpenMPDirectiveName(DKind)
2711 << NestedLoopCount << (CurrentNestedLoopCount > 0)
2712 << CurrentNestedLoopCount;
2713 if (NestedLoopCount > 1)
2714 SemaRef.Diag(NestedLoopCountExpr->getExprLoc(),
2715 diag::note_omp_collapse_expr)
2716 << NestedLoopCountExpr->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002717 return true;
2718 }
2719 assert(For->getBody());
2720
2721 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
2722
2723 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002724 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002725 if (ISC.CheckInit(Init)) {
2726 return true;
2727 }
2728
2729 bool HasErrors = false;
2730
2731 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002732 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002733
2734 // OpenMP [2.6, Canonical Loop Form]
2735 // Var is one of the following:
2736 // A variable of signed or unsigned integer type.
2737 // For C++, a variable of a random access iterator type.
2738 // For C, a variable of a pointer type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002739 auto VarType = Var->getType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002740 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
2741 !VarType->isPointerType() &&
2742 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
2743 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
2744 << SemaRef.getLangOpts().CPlusPlus;
2745 HasErrors = true;
2746 }
2747
Alexey Bataev4acb8592014-07-07 13:01:15 +00002748 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
2749 // Construct
2750 // The loop iteration variable(s) in the associated for-loop(s) of a for or
2751 // parallel for construct is (are) private.
2752 // The loop iteration variable in the associated for-loop of a simd construct
2753 // with just one associated for-loop is linear with a constant-linear-step
2754 // that is the increment of the associated for-loop.
2755 // Exclude loop var from the list of variables with implicitly defined data
2756 // sharing attributes.
Benjamin Kramerad8e0792014-10-10 15:32:48 +00002757 VarsWithImplicitDSA.erase(Var);
Alexey Bataev4acb8592014-07-07 13:01:15 +00002758
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002759 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
2760 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00002761 // The loop iteration variable in the associated for-loop of a simd construct
2762 // with just one associated for-loop may be listed in a linear clause with a
2763 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002764 // The loop iteration variable(s) in the associated for-loop(s) of a for or
2765 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002766 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002767 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
2768 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
2769 // declared in the loop and it is predetermined as a private.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002770 auto PredeterminedCKind =
2771 isOpenMPSimdDirective(DKind)
2772 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
2773 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002774 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataev0c024df2015-05-12 09:02:07 +00002775 DVar.CKind != OMPC_threadprivate && DVar.CKind != PredeterminedCKind) ||
Alexander Musmanf82886e2014-09-18 05:12:34 +00002776 (isOpenMPWorksharingDirective(DKind) && !isOpenMPSimdDirective(DKind) &&
2777 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private &&
Alexey Bataev0c024df2015-05-12 09:02:07 +00002778 DVar.CKind != OMPC_lastprivate && DVar.CKind != OMPC_threadprivate)) &&
2779 ((DVar.CKind != OMPC_private && DVar.CKind != OMPC_threadprivate) ||
2780 DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002781 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00002782 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
2783 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataevf2453a02015-05-06 07:25:08 +00002784 if (DVar.RefExpr == nullptr)
2785 DVar.CKind = PredeterminedCKind;
2786 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, /*IsLoopIterVar=*/true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002787 HasErrors = true;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002788 } else if (LoopVarRefExpr != nullptr) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00002789 // Make the loop iteration variable private (for worksharing constructs),
2790 // linear (for simd directives with the only one associated loop) or
2791 // lastprivate (for simd directives with several collapsed loops).
Alexey Bataev9aba41c2014-11-14 04:08:45 +00002792 if (DVar.CKind == OMPC_unknown)
2793 DVar = DSA.hasDSA(Var, isOpenMPPrivate, MatchesAlways(),
2794 /*FromParent=*/false);
Alexey Bataev9c821032015-04-30 04:23:23 +00002795 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002796 }
2797
Alexey Bataev7ff55242014-06-19 09:13:45 +00002798 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00002799
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002800 // Check test-expr.
2801 HasErrors |= ISC.CheckCond(For->getCond());
2802
2803 // Check incr-expr.
2804 HasErrors |= ISC.CheckInc(For->getInc());
2805
Alexander Musmana5f070a2014-10-01 06:03:56 +00002806 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002807 return HasErrors;
2808
Alexander Musmana5f070a2014-10-01 06:03:56 +00002809 // Build the loop's iteration space representation.
Alexey Bataev62dbb972015-04-22 11:59:37 +00002810 ResultIterSpace.PreCond = ISC.BuildPreCond(DSA.getCurScope(), For->getCond());
Alexander Musman174b3ca2014-10-06 11:16:29 +00002811 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
2812 DSA.getCurScope(), /* LimitedType */ isOpenMPWorksharingDirective(DKind));
Alexander Musmana5f070a2014-10-01 06:03:56 +00002813 ResultIterSpace.CounterVar = ISC.BuildCounterVar();
2814 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
2815 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
2816 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
2817 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
2818 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
2819 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
2820
Alexey Bataev62dbb972015-04-22 11:59:37 +00002821 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
2822 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00002823 ResultIterSpace.CounterVar == nullptr ||
2824 ResultIterSpace.CounterInit == nullptr ||
2825 ResultIterSpace.CounterStep == nullptr);
2826
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002827 return HasErrors;
2828}
2829
Alexander Musmana5f070a2014-10-01 06:03:56 +00002830/// \brief Build 'VarRef = Start + Iter * Step'.
2831static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S,
2832 SourceLocation Loc, ExprResult VarRef,
2833 ExprResult Start, ExprResult Iter,
2834 ExprResult Step, bool Subtract) {
2835 // Add parentheses (for debugging purposes only).
2836 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
2837 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
2838 !Step.isUsable())
2839 return ExprError();
2840
2841 ExprResult Update = SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(),
2842 Step.get()->IgnoreImplicit());
2843 if (!Update.isUsable())
2844 return ExprError();
2845
2846 // Build 'VarRef = Start + Iter * Step'.
2847 Update = SemaRef.BuildBinOp(S, Loc, (Subtract ? BO_Sub : BO_Add),
2848 Start.get()->IgnoreImplicit(), Update.get());
2849 if (!Update.isUsable())
2850 return ExprError();
2851
2852 Update = SemaRef.PerformImplicitConversion(
2853 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
2854 if (!Update.isUsable())
2855 return ExprError();
2856
2857 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
2858 return Update;
2859}
2860
2861/// \brief Convert integer expression \a E to make it have at least \a Bits
2862/// bits.
2863static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
2864 Sema &SemaRef) {
2865 if (E == nullptr)
2866 return ExprError();
2867 auto &C = SemaRef.Context;
2868 QualType OldType = E->getType();
2869 unsigned HasBits = C.getTypeSize(OldType);
2870 if (HasBits >= Bits)
2871 return ExprResult(E);
2872 // OK to convert to signed, because new type has more bits than old.
2873 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
2874 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
2875 true);
2876}
2877
2878/// \brief Check if the given expression \a E is a constant integer that fits
2879/// into \a Bits bits.
2880static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
2881 if (E == nullptr)
2882 return false;
2883 llvm::APSInt Result;
2884 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
2885 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
2886 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002887}
2888
2889/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00002890/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
2891/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002892static unsigned
2893CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *NestedLoopCountExpr,
2894 Stmt *AStmt, Sema &SemaRef, DSAStackTy &DSA,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002895 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00002896 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002897 unsigned NestedLoopCount = 1;
2898 if (NestedLoopCountExpr) {
2899 // Found 'collapse' clause - calculate collapse number.
2900 llvm::APSInt Result;
2901 if (NestedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
2902 NestedLoopCount = Result.getLimitedValue();
2903 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002904 // This is helper routine for loop directives (e.g., 'for', 'simd',
2905 // 'for simd', etc.).
Alexander Musmana5f070a2014-10-01 06:03:56 +00002906 SmallVector<LoopIterationSpace, 4> IterSpaces;
2907 IterSpaces.resize(NestedLoopCount);
2908 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002909 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002910 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev4acb8592014-07-07 13:01:15 +00002911 NestedLoopCount, NestedLoopCountExpr,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002912 VarsWithImplicitDSA, IterSpaces[Cnt]))
Alexey Bataevabfc0692014-06-25 06:52:00 +00002913 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002914 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00002915 // OpenMP [2.8.1, simd construct, Restrictions]
2916 // All loops associated with the construct must be perfectly nested; that
2917 // is, there must be no intervening code nor any OpenMP directive between
2918 // any two loops.
2919 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002920 }
2921
Alexander Musmana5f070a2014-10-01 06:03:56 +00002922 Built.clear(/* size */ NestedLoopCount);
2923
2924 if (SemaRef.CurContext->isDependentContext())
2925 return NestedLoopCount;
2926
2927 // An example of what is generated for the following code:
2928 //
2929 // #pragma omp simd collapse(2)
2930 // for (i = 0; i < NI; ++i)
2931 // for (j = J0; j < NJ; j+=2) {
2932 // <loop body>
2933 // }
2934 //
2935 // We generate the code below.
2936 // Note: the loop body may be outlined in CodeGen.
2937 // Note: some counters may be C++ classes, operator- is used to find number of
2938 // iterations and operator+= to calculate counter value.
2939 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
2940 // or i64 is currently supported).
2941 //
2942 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
2943 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
2944 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
2945 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
2946 // // similar updates for vars in clauses (e.g. 'linear')
2947 // <loop body (using local i and j)>
2948 // }
2949 // i = NI; // assign final values of counters
2950 // j = NJ;
2951 //
2952
2953 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
2954 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00002955 // Precondition tests if there is at least one iteration (all conditions are
2956 // true).
2957 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00002958 auto N0 = IterSpaces[0].NumIterations;
2959 ExprResult LastIteration32 = WidenIterationCount(32 /* Bits */, N0, SemaRef);
2960 ExprResult LastIteration64 = WidenIterationCount(64 /* Bits */, N0, SemaRef);
2961
2962 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
2963 return NestedLoopCount;
2964
2965 auto &C = SemaRef.Context;
2966 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
2967
2968 Scope *CurScope = DSA.getCurScope();
2969 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00002970 if (PreCond.isUsable()) {
2971 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
2972 PreCond.get(), IterSpaces[Cnt].PreCond);
2973 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002974 auto N = IterSpaces[Cnt].NumIterations;
2975 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
2976 if (LastIteration32.isUsable())
2977 LastIteration32 = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_Mul,
2978 LastIteration32.get(), N);
2979 if (LastIteration64.isUsable())
2980 LastIteration64 = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_Mul,
2981 LastIteration64.get(), N);
2982 }
2983
2984 // Choose either the 32-bit or 64-bit version.
2985 ExprResult LastIteration = LastIteration64;
2986 if (LastIteration32.isUsable() &&
2987 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
2988 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
2989 FitsInto(
2990 32 /* Bits */,
2991 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
2992 LastIteration64.get(), SemaRef)))
2993 LastIteration = LastIteration32;
2994
2995 if (!LastIteration.isUsable())
2996 return 0;
2997
2998 // Save the number of iterations.
2999 ExprResult NumIterations = LastIteration;
3000 {
3001 LastIteration = SemaRef.BuildBinOp(
3002 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
3003 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3004 if (!LastIteration.isUsable())
3005 return 0;
3006 }
3007
3008 // Calculate the last iteration number beforehand instead of doing this on
3009 // each iteration. Do not do this if the number of iterations may be kfold-ed.
3010 llvm::APSInt Result;
3011 bool IsConstant =
3012 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
3013 ExprResult CalcLastIteration;
3014 if (!IsConstant) {
3015 SourceLocation SaveLoc;
3016 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00003017 buildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00003018 ".omp.last.iteration");
Alexey Bataev39f915b82015-05-08 10:41:21 +00003019 ExprResult SaveRef = buildDeclRefExpr(
3020 SemaRef, SaveVar, LastIteration.get()->getType(), SaveLoc);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003021 CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign,
3022 SaveRef.get(), LastIteration.get());
3023 LastIteration = SaveRef;
3024
3025 // Prepare SaveRef + 1.
3026 NumIterations = SemaRef.BuildBinOp(
3027 CurScope, SaveLoc, BO_Add, SaveRef.get(),
3028 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3029 if (!NumIterations.isUsable())
3030 return 0;
3031 }
3032
3033 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
3034
Alexander Musmanc6388682014-12-15 07:07:06 +00003035 QualType VType = LastIteration.get()->getType();
3036 // Build variables passed into runtime, nesessary for worksharing directives.
3037 ExprResult LB, UB, IL, ST, EUB;
3038 if (isOpenMPWorksharingDirective(DKind)) {
3039 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00003040 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
3041 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003042 SemaRef.AddInitializerToDecl(
3043 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
3044 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3045
3046 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00003047 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
3048 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003049 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
3050 /*DirectInit*/ false,
3051 /*TypeMayContainAuto*/ false);
3052
3053 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
3054 // This will be used to implement clause 'lastprivate'.
3055 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00003056 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
3057 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003058 SemaRef.AddInitializerToDecl(
3059 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
3060 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3061
3062 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev39f915b82015-05-08 10:41:21 +00003063 VarDecl *STDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.stride");
3064 ST = buildDeclRefExpr(SemaRef, STDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003065 SemaRef.AddInitializerToDecl(
3066 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
3067 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3068
3069 // Build expression: UB = min(UB, LastIteration)
3070 // It is nesessary for CodeGen of directives with static scheduling.
3071 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
3072 UB.get(), LastIteration.get());
3073 ExprResult CondOp = SemaRef.ActOnConditionalOp(
3074 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
3075 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
3076 CondOp.get());
3077 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
3078 }
3079
3080 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003081 ExprResult IV;
3082 ExprResult Init;
3083 {
Alexey Bataev39f915b82015-05-08 10:41:21 +00003084 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.iv");
3085 IV = buildDeclRefExpr(SemaRef, IVDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003086 Expr *RHS = isOpenMPWorksharingDirective(DKind)
3087 ? LB.get()
3088 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
3089 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
3090 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003091 }
3092
Alexander Musmanc6388682014-12-15 07:07:06 +00003093 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003094 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00003095 ExprResult Cond =
3096 isOpenMPWorksharingDirective(DKind)
3097 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
3098 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
3099 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003100
3101 // Loop increment (IV = IV + 1)
3102 SourceLocation IncLoc;
3103 ExprResult Inc =
3104 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
3105 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
3106 if (!Inc.isUsable())
3107 return 0;
3108 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00003109 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
3110 if (!Inc.isUsable())
3111 return 0;
3112
3113 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
3114 // Used for directives with static scheduling.
3115 ExprResult NextLB, NextUB;
3116 if (isOpenMPWorksharingDirective(DKind)) {
3117 // LB + ST
3118 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
3119 if (!NextLB.isUsable())
3120 return 0;
3121 // LB = LB + ST
3122 NextLB =
3123 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
3124 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
3125 if (!NextLB.isUsable())
3126 return 0;
3127 // UB + ST
3128 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
3129 if (!NextUB.isUsable())
3130 return 0;
3131 // UB = UB + ST
3132 NextUB =
3133 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
3134 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
3135 if (!NextUB.isUsable())
3136 return 0;
3137 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003138
3139 // Build updates and final values of the loop counters.
3140 bool HasErrors = false;
3141 Built.Counters.resize(NestedLoopCount);
3142 Built.Updates.resize(NestedLoopCount);
3143 Built.Finals.resize(NestedLoopCount);
3144 {
3145 ExprResult Div;
3146 // Go from inner nested loop to outer.
3147 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
3148 LoopIterationSpace &IS = IterSpaces[Cnt];
3149 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
3150 // Build: Iter = (IV / Div) % IS.NumIters
3151 // where Div is product of previous iterations' IS.NumIters.
3152 ExprResult Iter;
3153 if (Div.isUsable()) {
3154 Iter =
3155 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
3156 } else {
3157 Iter = IV;
3158 assert((Cnt == (int)NestedLoopCount - 1) &&
3159 "unusable div expected on first iteration only");
3160 }
3161
3162 if (Cnt != 0 && Iter.isUsable())
3163 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
3164 IS.NumIterations);
3165 if (!Iter.isUsable()) {
3166 HasErrors = true;
3167 break;
3168 }
3169
Alexey Bataev39f915b82015-05-08 10:41:21 +00003170 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
3171 auto *CounterVar = buildDeclRefExpr(
3172 SemaRef, cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()),
3173 IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
3174 /*RefersToCapture=*/true);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003175 ExprResult Update =
Alexey Bataev39f915b82015-05-08 10:41:21 +00003176 BuildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003177 IS.CounterInit, Iter, IS.CounterStep, IS.Subtract);
3178 if (!Update.isUsable()) {
3179 HasErrors = true;
3180 break;
3181 }
3182
3183 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
3184 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00003185 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003186 IS.NumIterations, IS.CounterStep, IS.Subtract);
3187 if (!Final.isUsable()) {
3188 HasErrors = true;
3189 break;
3190 }
3191
3192 // Build Div for the next iteration: Div <- Div * IS.NumIters
3193 if (Cnt != 0) {
3194 if (Div.isUnset())
3195 Div = IS.NumIterations;
3196 else
3197 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
3198 IS.NumIterations);
3199
3200 // Add parentheses (for debugging purposes only).
3201 if (Div.isUsable())
3202 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
3203 if (!Div.isUsable()) {
3204 HasErrors = true;
3205 break;
3206 }
3207 }
3208 if (!Update.isUsable() || !Final.isUsable()) {
3209 HasErrors = true;
3210 break;
3211 }
3212 // Save results
3213 Built.Counters[Cnt] = IS.CounterVar;
3214 Built.Updates[Cnt] = Update.get();
3215 Built.Finals[Cnt] = Final.get();
3216 }
3217 }
3218
3219 if (HasErrors)
3220 return 0;
3221
3222 // Save results
3223 Built.IterationVarRef = IV.get();
3224 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00003225 Built.NumIterations = NumIterations.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003226 Built.CalcLastIteration = CalcLastIteration.get();
3227 Built.PreCond = PreCond.get();
3228 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003229 Built.Init = Init.get();
3230 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00003231 Built.LB = LB.get();
3232 Built.UB = UB.get();
3233 Built.IL = IL.get();
3234 Built.ST = ST.get();
3235 Built.EUB = EUB.get();
3236 Built.NLB = NextLB.get();
3237 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003238
Alexey Bataevabfc0692014-06-25 06:52:00 +00003239 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003240}
3241
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003242static Expr *GetCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Alexey Bataevc925aa32015-04-27 08:00:32 +00003243 auto &&CollapseFilter = [](const OMPClause *C) -> bool {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003244 return C->getClauseKind() == OMPC_collapse;
3245 };
3246 OMPExecutableDirective::filtered_clause_iterator<decltype(CollapseFilter)> I(
Alexey Bataevc925aa32015-04-27 08:00:32 +00003247 Clauses, std::move(CollapseFilter));
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003248 if (I)
3249 return cast<OMPCollapseClause>(*I)->getNumForLoops();
3250 return nullptr;
3251}
3252
Alexey Bataev4acb8592014-07-07 13:01:15 +00003253StmtResult Sema::ActOnOpenMPSimdDirective(
3254 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3255 SourceLocation EndLoc,
3256 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmanc6388682014-12-15 07:07:06 +00003257 OMPLoopDirective::HelperExprs B;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003258 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003259 unsigned NestedLoopCount =
3260 CheckOpenMPLoop(OMPD_simd, GetCollapseNumberExpr(Clauses), AStmt, *this,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003261 *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00003262 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003263 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003264
Alexander Musmana5f070a2014-10-01 06:03:56 +00003265 assert((CurContext->isDependentContext() || B.builtAll()) &&
3266 "omp simd loop exprs were not built");
3267
Alexander Musman3276a272015-03-21 10:12:56 +00003268 if (!CurContext->isDependentContext()) {
3269 // Finalize the clauses that need pre-built expressions for CodeGen.
3270 for (auto C : Clauses) {
3271 if (auto LC = dyn_cast<OMPLinearClause>(C))
3272 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3273 B.NumIterations, *this, CurScope))
3274 return StmtError();
3275 }
3276 }
3277
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003278 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003279 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
3280 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003281}
3282
Alexey Bataev4acb8592014-07-07 13:01:15 +00003283StmtResult Sema::ActOnOpenMPForDirective(
3284 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3285 SourceLocation EndLoc,
3286 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmanc6388682014-12-15 07:07:06 +00003287 OMPLoopDirective::HelperExprs B;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003288 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003289 unsigned NestedLoopCount =
3290 CheckOpenMPLoop(OMPD_for, GetCollapseNumberExpr(Clauses), AStmt, *this,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003291 *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00003292 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00003293 return StmtError();
3294
Alexander Musmana5f070a2014-10-01 06:03:56 +00003295 assert((CurContext->isDependentContext() || B.builtAll()) &&
3296 "omp for loop exprs were not built");
3297
Alexey Bataevf29276e2014-06-18 04:14:57 +00003298 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003299 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
3300 Clauses, AStmt, B);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003301}
3302
Alexander Musmanf82886e2014-09-18 05:12:34 +00003303StmtResult Sema::ActOnOpenMPForSimdDirective(
3304 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3305 SourceLocation EndLoc,
3306 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmanc6388682014-12-15 07:07:06 +00003307 OMPLoopDirective::HelperExprs B;
Alexander Musmanf82886e2014-09-18 05:12:34 +00003308 // In presence of clause 'collapse', it will define the nested loops number.
3309 unsigned NestedLoopCount =
3310 CheckOpenMPLoop(OMPD_for_simd, GetCollapseNumberExpr(Clauses), AStmt,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003311 *this, *DSAStack, VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00003312 if (NestedLoopCount == 0)
3313 return StmtError();
3314
Alexander Musmanc6388682014-12-15 07:07:06 +00003315 assert((CurContext->isDependentContext() || B.builtAll()) &&
3316 "omp for simd loop exprs were not built");
3317
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00003318 if (!CurContext->isDependentContext()) {
3319 // Finalize the clauses that need pre-built expressions for CodeGen.
3320 for (auto C : Clauses) {
3321 if (auto LC = dyn_cast<OMPLinearClause>(C))
3322 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3323 B.NumIterations, *this, CurScope))
3324 return StmtError();
3325 }
3326 }
3327
Alexander Musmanf82886e2014-09-18 05:12:34 +00003328 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003329 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
3330 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00003331}
3332
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003333StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
3334 Stmt *AStmt,
3335 SourceLocation StartLoc,
3336 SourceLocation EndLoc) {
3337 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3338 auto BaseStmt = AStmt;
3339 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
3340 BaseStmt = CS->getCapturedStmt();
3341 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
3342 auto S = C->children();
3343 if (!S)
3344 return StmtError();
3345 // All associated statements must be '#pragma omp section' except for
3346 // the first one.
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003347 for (++S; S; ++S) {
3348 auto SectionStmt = *S;
3349 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
3350 if (SectionStmt)
3351 Diag(SectionStmt->getLocStart(),
3352 diag::err_omp_sections_substmt_not_section);
3353 return StmtError();
3354 }
3355 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003356 } else {
3357 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
3358 return StmtError();
3359 }
3360
3361 getCurFunction()->setHasBranchProtectedScope();
3362
3363 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses,
3364 AStmt);
3365}
3366
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003367StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
3368 SourceLocation StartLoc,
3369 SourceLocation EndLoc) {
3370 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3371
3372 getCurFunction()->setHasBranchProtectedScope();
3373
3374 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt);
3375}
3376
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003377StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
3378 Stmt *AStmt,
3379 SourceLocation StartLoc,
3380 SourceLocation EndLoc) {
Alexey Bataev74a05c92014-07-15 02:55:09 +00003381 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3382
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003383 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00003384
Alexey Bataev3255bf32015-01-19 05:20:46 +00003385 // OpenMP [2.7.3, single Construct, Restrictions]
3386 // The copyprivate clause must not be used with the nowait clause.
3387 OMPClause *Nowait = nullptr;
3388 OMPClause *Copyprivate = nullptr;
3389 for (auto *Clause : Clauses) {
3390 if (Clause->getClauseKind() == OMPC_nowait)
3391 Nowait = Clause;
3392 else if (Clause->getClauseKind() == OMPC_copyprivate)
3393 Copyprivate = Clause;
3394 if (Copyprivate && Nowait) {
3395 Diag(Copyprivate->getLocStart(),
3396 diag::err_omp_single_copyprivate_with_nowait);
3397 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
3398 return StmtError();
3399 }
3400 }
3401
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003402 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3403}
3404
Alexander Musman80c22892014-07-17 08:54:58 +00003405StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
3406 SourceLocation StartLoc,
3407 SourceLocation EndLoc) {
3408 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3409
3410 getCurFunction()->setHasBranchProtectedScope();
3411
3412 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
3413}
3414
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003415StmtResult
3416Sema::ActOnOpenMPCriticalDirective(const DeclarationNameInfo &DirName,
3417 Stmt *AStmt, SourceLocation StartLoc,
3418 SourceLocation EndLoc) {
3419 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3420
3421 getCurFunction()->setHasBranchProtectedScope();
3422
3423 return OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
3424 AStmt);
3425}
3426
Alexey Bataev4acb8592014-07-07 13:01:15 +00003427StmtResult Sema::ActOnOpenMPParallelForDirective(
3428 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3429 SourceLocation EndLoc,
3430 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
3431 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3432 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3433 // 1.2.2 OpenMP Language Terminology
3434 // Structured block - An executable statement with a single entry at the
3435 // top and a single exit at the bottom.
3436 // The point of exit cannot be a branch out of the structured block.
3437 // longjmp() and throw() must not violate the entry/exit criteria.
3438 CS->getCapturedDecl()->setNothrow();
3439
Alexander Musmanc6388682014-12-15 07:07:06 +00003440 OMPLoopDirective::HelperExprs B;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003441 // In presence of clause 'collapse', it will define the nested loops number.
3442 unsigned NestedLoopCount =
3443 CheckOpenMPLoop(OMPD_parallel_for, GetCollapseNumberExpr(Clauses), AStmt,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003444 *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003445 if (NestedLoopCount == 0)
3446 return StmtError();
3447
Alexander Musmana5f070a2014-10-01 06:03:56 +00003448 assert((CurContext->isDependentContext() || B.builtAll()) &&
3449 "omp parallel for loop exprs were not built");
3450
Alexey Bataev4acb8592014-07-07 13:01:15 +00003451 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003452 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
3453 NestedLoopCount, Clauses, AStmt, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003454}
3455
Alexander Musmane4e893b2014-09-23 09:33:00 +00003456StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
3457 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3458 SourceLocation EndLoc,
3459 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
3460 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3461 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3462 // 1.2.2 OpenMP Language Terminology
3463 // Structured block - An executable statement with a single entry at the
3464 // top and a single exit at the bottom.
3465 // The point of exit cannot be a branch out of the structured block.
3466 // longjmp() and throw() must not violate the entry/exit criteria.
3467 CS->getCapturedDecl()->setNothrow();
3468
Alexander Musmanc6388682014-12-15 07:07:06 +00003469 OMPLoopDirective::HelperExprs B;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003470 // In presence of clause 'collapse', it will define the nested loops number.
3471 unsigned NestedLoopCount =
3472 CheckOpenMPLoop(OMPD_parallel_for_simd, GetCollapseNumberExpr(Clauses),
Alexander Musmana5f070a2014-10-01 06:03:56 +00003473 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003474 if (NestedLoopCount == 0)
3475 return StmtError();
3476
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00003477 if (!CurContext->isDependentContext()) {
3478 // Finalize the clauses that need pre-built expressions for CodeGen.
3479 for (auto C : Clauses) {
3480 if (auto LC = dyn_cast<OMPLinearClause>(C))
3481 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3482 B.NumIterations, *this, CurScope))
3483 return StmtError();
3484 }
3485 }
3486
Alexander Musmane4e893b2014-09-23 09:33:00 +00003487 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003488 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00003489 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003490}
3491
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003492StmtResult
3493Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
3494 Stmt *AStmt, SourceLocation StartLoc,
3495 SourceLocation EndLoc) {
3496 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3497 auto BaseStmt = AStmt;
3498 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
3499 BaseStmt = CS->getCapturedStmt();
3500 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
3501 auto S = C->children();
3502 if (!S)
3503 return StmtError();
3504 // All associated statements must be '#pragma omp section' except for
3505 // the first one.
3506 for (++S; S; ++S) {
3507 auto SectionStmt = *S;
3508 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
3509 if (SectionStmt)
3510 Diag(SectionStmt->getLocStart(),
3511 diag::err_omp_parallel_sections_substmt_not_section);
3512 return StmtError();
3513 }
3514 }
3515 } else {
3516 Diag(AStmt->getLocStart(),
3517 diag::err_omp_parallel_sections_not_compound_stmt);
3518 return StmtError();
3519 }
3520
3521 getCurFunction()->setHasBranchProtectedScope();
3522
3523 return OMPParallelSectionsDirective::Create(Context, StartLoc, EndLoc,
3524 Clauses, AStmt);
3525}
3526
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003527StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
3528 Stmt *AStmt, SourceLocation StartLoc,
3529 SourceLocation EndLoc) {
3530 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3531 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3532 // 1.2.2 OpenMP Language Terminology
3533 // Structured block - An executable statement with a single entry at the
3534 // top and a single exit at the bottom.
3535 // The point of exit cannot be a branch out of the structured block.
3536 // longjmp() and throw() must not violate the entry/exit criteria.
3537 CS->getCapturedDecl()->setNothrow();
3538
3539 getCurFunction()->setHasBranchProtectedScope();
3540
3541 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3542}
3543
Alexey Bataev68446b72014-07-18 07:47:19 +00003544StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
3545 SourceLocation EndLoc) {
3546 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
3547}
3548
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003549StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
3550 SourceLocation EndLoc) {
3551 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
3552}
3553
Alexey Bataev2df347a2014-07-18 10:17:07 +00003554StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
3555 SourceLocation EndLoc) {
3556 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
3557}
3558
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003559StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
3560 SourceLocation StartLoc,
3561 SourceLocation EndLoc) {
3562 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3563
3564 getCurFunction()->setHasBranchProtectedScope();
3565
3566 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
3567}
3568
Alexey Bataev6125da92014-07-21 11:26:11 +00003569StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
3570 SourceLocation StartLoc,
3571 SourceLocation EndLoc) {
3572 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
3573 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
3574}
3575
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003576StmtResult Sema::ActOnOpenMPOrderedDirective(Stmt *AStmt,
3577 SourceLocation StartLoc,
3578 SourceLocation EndLoc) {
3579 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3580
3581 getCurFunction()->setHasBranchProtectedScope();
3582
3583 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, AStmt);
3584}
3585
Alexey Bataev1d160b12015-03-13 12:27:31 +00003586namespace {
3587/// \brief Helper class for checking expression in 'omp atomic [update]'
3588/// construct.
3589class OpenMPAtomicUpdateChecker {
3590 /// \brief Error results for atomic update expressions.
3591 enum ExprAnalysisErrorCode {
3592 /// \brief A statement is not an expression statement.
3593 NotAnExpression,
3594 /// \brief Expression is not builtin binary or unary operation.
3595 NotABinaryOrUnaryExpression,
3596 /// \brief Unary operation is not post-/pre- increment/decrement operation.
3597 NotAnUnaryIncDecExpression,
3598 /// \brief An expression is not of scalar type.
3599 NotAScalarType,
3600 /// \brief A binary operation is not an assignment operation.
3601 NotAnAssignmentOp,
3602 /// \brief RHS part of the binary operation is not a binary expression.
3603 NotABinaryExpression,
3604 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
3605 /// expression.
3606 NotABinaryOperator,
3607 /// \brief RHS binary operation does not have reference to the updated LHS
3608 /// part.
3609 NotAnUpdateExpression,
3610 /// \brief No errors is found.
3611 NoError
3612 };
3613 /// \brief Reference to Sema.
3614 Sema &SemaRef;
3615 /// \brief A location for note diagnostics (when error is found).
3616 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003617 /// \brief 'x' lvalue part of the source atomic expression.
3618 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003619 /// \brief 'expr' rvalue part of the source atomic expression.
3620 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00003621 /// \brief Helper expression of the form
3622 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
3623 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
3624 Expr *UpdateExpr;
3625 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
3626 /// important for non-associative operations.
3627 bool IsXLHSInRHSPart;
3628 BinaryOperatorKind Op;
3629 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00003630 /// \brief true if the source expression is a postfix unary operation, false
3631 /// if it is a prefix unary operation.
3632 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003633
3634public:
3635 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00003636 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00003637 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00003638 /// \brief Check specified statement that it is suitable for 'atomic update'
3639 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00003640 /// expression. If DiagId and NoteId == 0, then only check is performed
3641 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00003642 /// \param DiagId Diagnostic which should be emitted if error is found.
3643 /// \param NoteId Diagnostic note for the main error message.
3644 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00003645 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00003646 /// \brief Return the 'x' lvalue part of the source atomic expression.
3647 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00003648 /// \brief Return the 'expr' rvalue part of the source atomic expression.
3649 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00003650 /// \brief Return the update expression used in calculation of the updated
3651 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
3652 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
3653 Expr *getUpdateExpr() const { return UpdateExpr; }
3654 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
3655 /// false otherwise.
3656 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
3657
Alexey Bataevb78ca832015-04-01 03:33:17 +00003658 /// \brief true if the source expression is a postfix unary operation, false
3659 /// if it is a prefix unary operation.
3660 bool isPostfixUpdate() const { return IsPostfixUpdate; }
3661
Alexey Bataev1d160b12015-03-13 12:27:31 +00003662private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00003663 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
3664 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00003665};
3666} // namespace
3667
3668bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
3669 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
3670 ExprAnalysisErrorCode ErrorFound = NoError;
3671 SourceLocation ErrorLoc, NoteLoc;
3672 SourceRange ErrorRange, NoteRange;
3673 // Allowed constructs are:
3674 // x = x binop expr;
3675 // x = expr binop x;
3676 if (AtomicBinOp->getOpcode() == BO_Assign) {
3677 X = AtomicBinOp->getLHS();
3678 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
3679 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
3680 if (AtomicInnerBinOp->isMultiplicativeOp() ||
3681 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
3682 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00003683 Op = AtomicInnerBinOp->getOpcode();
3684 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00003685 auto *LHS = AtomicInnerBinOp->getLHS();
3686 auto *RHS = AtomicInnerBinOp->getRHS();
3687 llvm::FoldingSetNodeID XId, LHSId, RHSId;
3688 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
3689 /*Canonical=*/true);
3690 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
3691 /*Canonical=*/true);
3692 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
3693 /*Canonical=*/true);
3694 if (XId == LHSId) {
3695 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00003696 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003697 } else if (XId == RHSId) {
3698 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00003699 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003700 } else {
3701 ErrorLoc = AtomicInnerBinOp->getExprLoc();
3702 ErrorRange = AtomicInnerBinOp->getSourceRange();
3703 NoteLoc = X->getExprLoc();
3704 NoteRange = X->getSourceRange();
3705 ErrorFound = NotAnUpdateExpression;
3706 }
3707 } else {
3708 ErrorLoc = AtomicInnerBinOp->getExprLoc();
3709 ErrorRange = AtomicInnerBinOp->getSourceRange();
3710 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
3711 NoteRange = SourceRange(NoteLoc, NoteLoc);
3712 ErrorFound = NotABinaryOperator;
3713 }
3714 } else {
3715 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
3716 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
3717 ErrorFound = NotABinaryExpression;
3718 }
3719 } else {
3720 ErrorLoc = AtomicBinOp->getExprLoc();
3721 ErrorRange = AtomicBinOp->getSourceRange();
3722 NoteLoc = AtomicBinOp->getOperatorLoc();
3723 NoteRange = SourceRange(NoteLoc, NoteLoc);
3724 ErrorFound = NotAnAssignmentOp;
3725 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00003726 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00003727 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
3728 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
3729 return true;
3730 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00003731 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00003732 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003733}
3734
3735bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
3736 unsigned NoteId) {
3737 ExprAnalysisErrorCode ErrorFound = NoError;
3738 SourceLocation ErrorLoc, NoteLoc;
3739 SourceRange ErrorRange, NoteRange;
3740 // Allowed constructs are:
3741 // x++;
3742 // x--;
3743 // ++x;
3744 // --x;
3745 // x binop= expr;
3746 // x = x binop expr;
3747 // x = expr binop x;
3748 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
3749 AtomicBody = AtomicBody->IgnoreParenImpCasts();
3750 if (AtomicBody->getType()->isScalarType() ||
3751 AtomicBody->isInstantiationDependent()) {
3752 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
3753 AtomicBody->IgnoreParenImpCasts())) {
3754 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00003755 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00003756 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00003757 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00003758 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00003759 X = AtomicCompAssignOp->getLHS();
3760 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003761 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
3762 AtomicBody->IgnoreParenImpCasts())) {
3763 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00003764 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
3765 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003766 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00003767 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
3768 // Check for Unary Operation
3769 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00003770 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00003771 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
3772 OpLoc = AtomicUnaryOp->getOperatorLoc();
3773 X = AtomicUnaryOp->getSubExpr();
3774 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
3775 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003776 } else {
3777 ErrorFound = NotAnUnaryIncDecExpression;
3778 ErrorLoc = AtomicUnaryOp->getExprLoc();
3779 ErrorRange = AtomicUnaryOp->getSourceRange();
3780 NoteLoc = AtomicUnaryOp->getOperatorLoc();
3781 NoteRange = SourceRange(NoteLoc, NoteLoc);
3782 }
3783 } else {
3784 ErrorFound = NotABinaryOrUnaryExpression;
3785 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
3786 NoteRange = ErrorRange = AtomicBody->getSourceRange();
3787 }
3788 } else {
3789 ErrorFound = NotAScalarType;
3790 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
3791 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
3792 }
3793 } else {
3794 ErrorFound = NotAnExpression;
3795 NoteLoc = ErrorLoc = S->getLocStart();
3796 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
3797 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00003798 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00003799 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
3800 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
3801 return true;
3802 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00003803 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00003804 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00003805 // Build an update expression of form 'OpaqueValueExpr(x) binop
3806 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
3807 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
3808 auto *OVEX = new (SemaRef.getASTContext())
3809 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
3810 auto *OVEExpr = new (SemaRef.getASTContext())
3811 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
3812 auto Update =
3813 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
3814 IsXLHSInRHSPart ? OVEExpr : OVEX);
3815 if (Update.isInvalid())
3816 return true;
3817 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
3818 Sema::AA_Casting);
3819 if (Update.isInvalid())
3820 return true;
3821 UpdateExpr = Update.get();
3822 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00003823 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003824}
3825
Alexey Bataev0162e452014-07-22 10:10:35 +00003826StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
3827 Stmt *AStmt,
3828 SourceLocation StartLoc,
3829 SourceLocation EndLoc) {
3830 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003831 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00003832 // 1.2.2 OpenMP Language Terminology
3833 // Structured block - An executable statement with a single entry at the
3834 // top and a single exit at the bottom.
3835 // The point of exit cannot be a branch out of the structured block.
3836 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00003837 OpenMPClauseKind AtomicKind = OMPC_unknown;
3838 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003839 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00003840 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00003841 C->getClauseKind() == OMPC_update ||
3842 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00003843 if (AtomicKind != OMPC_unknown) {
3844 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
3845 << SourceRange(C->getLocStart(), C->getLocEnd());
3846 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
3847 << getOpenMPClauseName(AtomicKind);
3848 } else {
3849 AtomicKind = C->getClauseKind();
3850 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003851 }
3852 }
3853 }
Alexey Bataev62cec442014-11-18 10:14:22 +00003854
Alexey Bataev459dec02014-07-24 06:46:57 +00003855 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00003856 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
3857 Body = EWC->getSubExpr();
3858
Alexey Bataev62cec442014-11-18 10:14:22 +00003859 Expr *X = nullptr;
3860 Expr *V = nullptr;
3861 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00003862 Expr *UE = nullptr;
3863 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00003864 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00003865 // OpenMP [2.12.6, atomic Construct]
3866 // In the next expressions:
3867 // * x and v (as applicable) are both l-value expressions with scalar type.
3868 // * During the execution of an atomic region, multiple syntactic
3869 // occurrences of x must designate the same storage location.
3870 // * Neither of v and expr (as applicable) may access the storage location
3871 // designated by x.
3872 // * Neither of x and expr (as applicable) may access the storage location
3873 // designated by v.
3874 // * expr is an expression with scalar type.
3875 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
3876 // * binop, binop=, ++, and -- are not overloaded operators.
3877 // * The expression x binop expr must be numerically equivalent to x binop
3878 // (expr). This requirement is satisfied if the operators in expr have
3879 // precedence greater than binop, or by using parentheses around expr or
3880 // subexpressions of expr.
3881 // * The expression expr binop x must be numerically equivalent to (expr)
3882 // binop x. This requirement is satisfied if the operators in expr have
3883 // precedence equal to or greater than binop, or by using parentheses around
3884 // expr or subexpressions of expr.
3885 // * For forms that allow multiple occurrences of x, the number of times
3886 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00003887 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00003888 enum {
3889 NotAnExpression,
3890 NotAnAssignmentOp,
3891 NotAScalarType,
3892 NotAnLValue,
3893 NoError
3894 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00003895 SourceLocation ErrorLoc, NoteLoc;
3896 SourceRange ErrorRange, NoteRange;
3897 // If clause is read:
3898 // v = x;
3899 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
3900 auto AtomicBinOp =
3901 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
3902 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
3903 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
3904 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
3905 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
3906 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
3907 if (!X->isLValue() || !V->isLValue()) {
3908 auto NotLValueExpr = X->isLValue() ? V : X;
3909 ErrorFound = NotAnLValue;
3910 ErrorLoc = AtomicBinOp->getExprLoc();
3911 ErrorRange = AtomicBinOp->getSourceRange();
3912 NoteLoc = NotLValueExpr->getExprLoc();
3913 NoteRange = NotLValueExpr->getSourceRange();
3914 }
3915 } else if (!X->isInstantiationDependent() ||
3916 !V->isInstantiationDependent()) {
3917 auto NotScalarExpr =
3918 (X->isInstantiationDependent() || X->getType()->isScalarType())
3919 ? V
3920 : X;
3921 ErrorFound = NotAScalarType;
3922 ErrorLoc = AtomicBinOp->getExprLoc();
3923 ErrorRange = AtomicBinOp->getSourceRange();
3924 NoteLoc = NotScalarExpr->getExprLoc();
3925 NoteRange = NotScalarExpr->getSourceRange();
3926 }
3927 } else {
3928 ErrorFound = NotAnAssignmentOp;
3929 ErrorLoc = AtomicBody->getExprLoc();
3930 ErrorRange = AtomicBody->getSourceRange();
3931 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
3932 : AtomicBody->getExprLoc();
3933 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
3934 : AtomicBody->getSourceRange();
3935 }
3936 } else {
3937 ErrorFound = NotAnExpression;
3938 NoteLoc = ErrorLoc = Body->getLocStart();
3939 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00003940 }
Alexey Bataev62cec442014-11-18 10:14:22 +00003941 if (ErrorFound != NoError) {
3942 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
3943 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00003944 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
3945 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00003946 return StmtError();
3947 } else if (CurContext->isDependentContext())
3948 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00003949 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00003950 enum {
3951 NotAnExpression,
3952 NotAnAssignmentOp,
3953 NotAScalarType,
3954 NotAnLValue,
3955 NoError
3956 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00003957 SourceLocation ErrorLoc, NoteLoc;
3958 SourceRange ErrorRange, NoteRange;
3959 // If clause is write:
3960 // x = expr;
3961 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
3962 auto AtomicBinOp =
3963 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
3964 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00003965 X = AtomicBinOp->getLHS();
3966 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00003967 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
3968 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
3969 if (!X->isLValue()) {
3970 ErrorFound = NotAnLValue;
3971 ErrorLoc = AtomicBinOp->getExprLoc();
3972 ErrorRange = AtomicBinOp->getSourceRange();
3973 NoteLoc = X->getExprLoc();
3974 NoteRange = X->getSourceRange();
3975 }
3976 } else if (!X->isInstantiationDependent() ||
3977 !E->isInstantiationDependent()) {
3978 auto NotScalarExpr =
3979 (X->isInstantiationDependent() || X->getType()->isScalarType())
3980 ? E
3981 : X;
3982 ErrorFound = NotAScalarType;
3983 ErrorLoc = AtomicBinOp->getExprLoc();
3984 ErrorRange = AtomicBinOp->getSourceRange();
3985 NoteLoc = NotScalarExpr->getExprLoc();
3986 NoteRange = NotScalarExpr->getSourceRange();
3987 }
3988 } else {
3989 ErrorFound = NotAnAssignmentOp;
3990 ErrorLoc = AtomicBody->getExprLoc();
3991 ErrorRange = AtomicBody->getSourceRange();
3992 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
3993 : AtomicBody->getExprLoc();
3994 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
3995 : AtomicBody->getSourceRange();
3996 }
3997 } else {
3998 ErrorFound = NotAnExpression;
3999 NoteLoc = ErrorLoc = Body->getLocStart();
4000 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00004001 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00004002 if (ErrorFound != NoError) {
4003 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
4004 << ErrorRange;
4005 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
4006 << NoteRange;
4007 return StmtError();
4008 } else if (CurContext->isDependentContext())
4009 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00004010 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004011 // If clause is update:
4012 // x++;
4013 // x--;
4014 // ++x;
4015 // --x;
4016 // x binop= expr;
4017 // x = x binop expr;
4018 // x = expr binop x;
4019 OpenMPAtomicUpdateChecker Checker(*this);
4020 if (Checker.checkStatement(
4021 Body, (AtomicKind == OMPC_update)
4022 ? diag::err_omp_atomic_update_not_expression_statement
4023 : diag::err_omp_atomic_not_expression_statement,
4024 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00004025 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00004026 if (!CurContext->isDependentContext()) {
4027 E = Checker.getExpr();
4028 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00004029 UE = Checker.getUpdateExpr();
4030 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00004031 }
Alexey Bataev459dec02014-07-24 06:46:57 +00004032 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004033 enum {
4034 NotAnAssignmentOp,
4035 NotACompoundStatement,
4036 NotTwoSubstatements,
4037 NotASpecificExpression,
4038 NoError
4039 } ErrorFound = NoError;
4040 SourceLocation ErrorLoc, NoteLoc;
4041 SourceRange ErrorRange, NoteRange;
4042 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
4043 // If clause is a capture:
4044 // v = x++;
4045 // v = x--;
4046 // v = ++x;
4047 // v = --x;
4048 // v = x binop= expr;
4049 // v = x = x binop expr;
4050 // v = x = expr binop x;
4051 auto *AtomicBinOp =
4052 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
4053 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
4054 V = AtomicBinOp->getLHS();
4055 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
4056 OpenMPAtomicUpdateChecker Checker(*this);
4057 if (Checker.checkStatement(
4058 Body, diag::err_omp_atomic_capture_not_expression_statement,
4059 diag::note_omp_atomic_update))
4060 return StmtError();
4061 E = Checker.getExpr();
4062 X = Checker.getX();
4063 UE = Checker.getUpdateExpr();
4064 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
4065 IsPostfixUpdate = Checker.isPostfixUpdate();
4066 } else {
4067 ErrorLoc = AtomicBody->getExprLoc();
4068 ErrorRange = AtomicBody->getSourceRange();
4069 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
4070 : AtomicBody->getExprLoc();
4071 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
4072 : AtomicBody->getSourceRange();
4073 ErrorFound = NotAnAssignmentOp;
4074 }
4075 if (ErrorFound != NoError) {
4076 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
4077 << ErrorRange;
4078 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
4079 return StmtError();
4080 } else if (CurContext->isDependentContext()) {
4081 UE = V = E = X = nullptr;
4082 }
4083 } else {
4084 // If clause is a capture:
4085 // { v = x; x = expr; }
4086 // { v = x; x++; }
4087 // { v = x; x--; }
4088 // { v = x; ++x; }
4089 // { v = x; --x; }
4090 // { v = x; x binop= expr; }
4091 // { v = x; x = x binop expr; }
4092 // { v = x; x = expr binop x; }
4093 // { x++; v = x; }
4094 // { x--; v = x; }
4095 // { ++x; v = x; }
4096 // { --x; v = x; }
4097 // { x binop= expr; v = x; }
4098 // { x = x binop expr; v = x; }
4099 // { x = expr binop x; v = x; }
4100 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
4101 // Check that this is { expr1; expr2; }
4102 if (CS->size() == 2) {
4103 auto *First = CS->body_front();
4104 auto *Second = CS->body_back();
4105 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
4106 First = EWC->getSubExpr()->IgnoreParenImpCasts();
4107 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
4108 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
4109 // Need to find what subexpression is 'v' and what is 'x'.
4110 OpenMPAtomicUpdateChecker Checker(*this);
4111 bool IsUpdateExprFound = !Checker.checkStatement(Second);
4112 BinaryOperator *BinOp = nullptr;
4113 if (IsUpdateExprFound) {
4114 BinOp = dyn_cast<BinaryOperator>(First);
4115 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
4116 }
4117 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
4118 // { v = x; x++; }
4119 // { v = x; x--; }
4120 // { v = x; ++x; }
4121 // { v = x; --x; }
4122 // { v = x; x binop= expr; }
4123 // { v = x; x = x binop expr; }
4124 // { v = x; x = expr binop x; }
4125 // Check that the first expression has form v = x.
4126 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
4127 llvm::FoldingSetNodeID XId, PossibleXId;
4128 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
4129 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
4130 IsUpdateExprFound = XId == PossibleXId;
4131 if (IsUpdateExprFound) {
4132 V = BinOp->getLHS();
4133 X = Checker.getX();
4134 E = Checker.getExpr();
4135 UE = Checker.getUpdateExpr();
4136 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00004137 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004138 }
4139 }
4140 if (!IsUpdateExprFound) {
4141 IsUpdateExprFound = !Checker.checkStatement(First);
4142 BinOp = nullptr;
4143 if (IsUpdateExprFound) {
4144 BinOp = dyn_cast<BinaryOperator>(Second);
4145 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
4146 }
4147 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
4148 // { x++; v = x; }
4149 // { x--; v = x; }
4150 // { ++x; v = x; }
4151 // { --x; v = x; }
4152 // { x binop= expr; v = x; }
4153 // { x = x binop expr; v = x; }
4154 // { x = expr binop x; v = x; }
4155 // Check that the second expression has form v = x.
4156 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
4157 llvm::FoldingSetNodeID XId, PossibleXId;
4158 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
4159 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
4160 IsUpdateExprFound = XId == PossibleXId;
4161 if (IsUpdateExprFound) {
4162 V = BinOp->getLHS();
4163 X = Checker.getX();
4164 E = Checker.getExpr();
4165 UE = Checker.getUpdateExpr();
4166 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00004167 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004168 }
4169 }
4170 }
4171 if (!IsUpdateExprFound) {
4172 // { v = x; x = expr; }
4173 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
4174 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
4175 ErrorFound = NotAnAssignmentOp;
4176 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
4177 : First->getLocStart();
4178 NoteRange = ErrorRange = FirstBinOp
4179 ? FirstBinOp->getSourceRange()
4180 : SourceRange(ErrorLoc, ErrorLoc);
4181 } else {
4182 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
4183 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
4184 ErrorFound = NotAnAssignmentOp;
4185 NoteLoc = ErrorLoc = SecondBinOp ? SecondBinOp->getOperatorLoc()
4186 : Second->getLocStart();
4187 NoteRange = ErrorRange = SecondBinOp
4188 ? SecondBinOp->getSourceRange()
4189 : SourceRange(ErrorLoc, ErrorLoc);
4190 } else {
4191 auto *PossibleXRHSInFirst =
4192 FirstBinOp->getRHS()->IgnoreParenImpCasts();
4193 auto *PossibleXLHSInSecond =
4194 SecondBinOp->getLHS()->IgnoreParenImpCasts();
4195 llvm::FoldingSetNodeID X1Id, X2Id;
4196 PossibleXRHSInFirst->Profile(X1Id, Context, /*Canonical=*/true);
4197 PossibleXLHSInSecond->Profile(X2Id, Context,
4198 /*Canonical=*/true);
4199 IsUpdateExprFound = X1Id == X2Id;
4200 if (IsUpdateExprFound) {
4201 V = FirstBinOp->getLHS();
4202 X = SecondBinOp->getLHS();
4203 E = SecondBinOp->getRHS();
4204 UE = nullptr;
4205 IsXLHSInRHSPart = false;
4206 IsPostfixUpdate = true;
4207 } else {
4208 ErrorFound = NotASpecificExpression;
4209 ErrorLoc = FirstBinOp->getExprLoc();
4210 ErrorRange = FirstBinOp->getSourceRange();
4211 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
4212 NoteRange = SecondBinOp->getRHS()->getSourceRange();
4213 }
4214 }
4215 }
4216 }
4217 } else {
4218 NoteLoc = ErrorLoc = Body->getLocStart();
4219 NoteRange = ErrorRange =
4220 SourceRange(Body->getLocStart(), Body->getLocStart());
4221 ErrorFound = NotTwoSubstatements;
4222 }
4223 } else {
4224 NoteLoc = ErrorLoc = Body->getLocStart();
4225 NoteRange = ErrorRange =
4226 SourceRange(Body->getLocStart(), Body->getLocStart());
4227 ErrorFound = NotACompoundStatement;
4228 }
4229 if (ErrorFound != NoError) {
4230 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
4231 << ErrorRange;
4232 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
4233 return StmtError();
4234 } else if (CurContext->isDependentContext()) {
4235 UE = V = E = X = nullptr;
4236 }
Alexey Bataev459dec02014-07-24 06:46:57 +00004237 }
Alexey Bataevdea47612014-07-23 07:46:59 +00004238 }
Alexey Bataev0162e452014-07-22 10:10:35 +00004239
4240 getCurFunction()->setHasBranchProtectedScope();
4241
Alexey Bataev62cec442014-11-18 10:14:22 +00004242 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00004243 X, V, E, UE, IsXLHSInRHSPart,
4244 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00004245}
4246
Alexey Bataev0bd520b2014-09-19 08:19:49 +00004247StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
4248 Stmt *AStmt,
4249 SourceLocation StartLoc,
4250 SourceLocation EndLoc) {
4251 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
4252
Alexey Bataev13314bf2014-10-09 04:18:56 +00004253 // OpenMP [2.16, Nesting of Regions]
4254 // If specified, a teams construct must be contained within a target
4255 // construct. That target construct must contain no statements or directives
4256 // outside of the teams construct.
4257 if (DSAStack->hasInnerTeamsRegion()) {
4258 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
4259 bool OMPTeamsFound = true;
4260 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
4261 auto I = CS->body_begin();
4262 while (I != CS->body_end()) {
4263 auto OED = dyn_cast<OMPExecutableDirective>(*I);
4264 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
4265 OMPTeamsFound = false;
4266 break;
4267 }
4268 ++I;
4269 }
4270 assert(I != CS->body_end() && "Not found statement");
4271 S = *I;
4272 }
4273 if (!OMPTeamsFound) {
4274 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
4275 Diag(DSAStack->getInnerTeamsRegionLoc(),
4276 diag::note_omp_nested_teams_construct_here);
4277 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
4278 << isa<OMPExecutableDirective>(S);
4279 return StmtError();
4280 }
4281 }
4282
Alexey Bataev0bd520b2014-09-19 08:19:49 +00004283 getCurFunction()->setHasBranchProtectedScope();
4284
4285 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4286}
4287
Alexey Bataev13314bf2014-10-09 04:18:56 +00004288StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
4289 Stmt *AStmt, SourceLocation StartLoc,
4290 SourceLocation EndLoc) {
4291 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
4292 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4293 // 1.2.2 OpenMP Language Terminology
4294 // Structured block - An executable statement with a single entry at the
4295 // top and a single exit at the bottom.
4296 // The point of exit cannot be a branch out of the structured block.
4297 // longjmp() and throw() must not violate the entry/exit criteria.
4298 CS->getCapturedDecl()->setNothrow();
4299
4300 getCurFunction()->setHasBranchProtectedScope();
4301
4302 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4303}
4304
Alexey Bataev6d4ed052015-07-01 06:57:41 +00004305StmtResult
4306Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
4307 SourceLocation EndLoc,
4308 OpenMPDirectiveKind CancelRegion) {
4309 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
4310 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
4311 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
4312 << getOpenMPDirectiveName(CancelRegion);
4313 return StmtError();
4314 }
4315 if (DSAStack->isParentNowaitRegion()) {
4316 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
4317 return StmtError();
4318 }
4319 if (DSAStack->isParentOrderedRegion()) {
4320 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
4321 return StmtError();
4322 }
4323 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
4324 CancelRegion);
4325}
4326
Alexey Bataev80909872015-07-02 11:25:17 +00004327StmtResult Sema::ActOnOpenMPCancelDirective(SourceLocation StartLoc,
4328 SourceLocation EndLoc,
4329 OpenMPDirectiveKind CancelRegion) {
4330 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
4331 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
4332 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
4333 << getOpenMPDirectiveName(CancelRegion);
4334 return StmtError();
4335 }
4336 if (DSAStack->isParentNowaitRegion()) {
4337 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
4338 return StmtError();
4339 }
4340 if (DSAStack->isParentOrderedRegion()) {
4341 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
4342 return StmtError();
4343 }
4344 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, CancelRegion);
4345}
4346
Alexey Bataeved09d242014-05-28 05:53:51 +00004347OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004348 SourceLocation StartLoc,
4349 SourceLocation LParenLoc,
4350 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004351 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004352 switch (Kind) {
4353 case OMPC_if:
4354 Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc);
4355 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00004356 case OMPC_final:
4357 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
4358 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00004359 case OMPC_num_threads:
4360 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
4361 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00004362 case OMPC_safelen:
4363 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
4364 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00004365 case OMPC_collapse:
4366 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
4367 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004368 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004369 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00004370 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004371 case OMPC_private:
4372 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00004373 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004374 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00004375 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00004376 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00004377 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004378 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00004379 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004380 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00004381 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00004382 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00004383 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004384 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00004385 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004386 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00004387 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00004388 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00004389 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00004390 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00004391 case OMPC_depend:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004392 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004393 llvm_unreachable("Clause is not allowed.");
4394 }
4395 return Res;
4396}
4397
Alexey Bataeved09d242014-05-28 05:53:51 +00004398OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004399 SourceLocation LParenLoc,
4400 SourceLocation EndLoc) {
4401 Expr *ValExpr = Condition;
4402 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
4403 !Condition->isInstantiationDependent() &&
4404 !Condition->containsUnexpandedParameterPack()) {
4405 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00004406 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004407 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004408 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004409
Nikola Smiljanic01a75982014-05-29 10:55:11 +00004410 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004411 }
4412
4413 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc);
4414}
4415
Alexey Bataev3778b602014-07-17 07:32:53 +00004416OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
4417 SourceLocation StartLoc,
4418 SourceLocation LParenLoc,
4419 SourceLocation EndLoc) {
4420 Expr *ValExpr = Condition;
4421 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
4422 !Condition->isInstantiationDependent() &&
4423 !Condition->containsUnexpandedParameterPack()) {
4424 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
4425 Condition->getExprLoc(), Condition);
4426 if (Val.isInvalid())
4427 return nullptr;
4428
4429 ValExpr = Val.get();
4430 }
4431
4432 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
4433}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004434ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
4435 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00004436 if (!Op)
4437 return ExprError();
4438
4439 class IntConvertDiagnoser : public ICEConvertDiagnoser {
4440 public:
4441 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00004442 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00004443 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
4444 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004445 return S.Diag(Loc, diag::err_omp_not_integral) << T;
4446 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004447 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
4448 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004449 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
4450 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004451 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
4452 QualType T,
4453 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004454 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
4455 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004456 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
4457 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004458 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00004459 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00004460 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004461 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
4462 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004463 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
4464 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004465 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
4466 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004467 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00004468 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00004469 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004470 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
4471 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004472 llvm_unreachable("conversion functions are permitted");
4473 }
4474 } ConvertDiagnoser;
4475 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
4476}
4477
4478OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
4479 SourceLocation StartLoc,
4480 SourceLocation LParenLoc,
4481 SourceLocation EndLoc) {
4482 Expr *ValExpr = NumThreads;
4483 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
Alexey Bataev568a8332014-03-06 06:15:19 +00004484 !NumThreads->containsUnexpandedParameterPack()) {
4485 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
4486 ExprResult Val =
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004487 PerformOpenMPImplicitIntegerConversion(NumThreadsLoc, NumThreads);
Alexey Bataev568a8332014-03-06 06:15:19 +00004488 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004489 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00004490
Nikola Smiljanic01a75982014-05-29 10:55:11 +00004491 ValExpr = Val.get();
Alexey Bataev568a8332014-03-06 06:15:19 +00004492
4493 // OpenMP [2.5, Restrictions]
4494 // The num_threads expression must evaluate to a positive integer value.
4495 llvm::APSInt Result;
Alexey Bataeved09d242014-05-28 05:53:51 +00004496 if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() &&
4497 !Result.isStrictlyPositive()) {
Alexey Bataev568a8332014-03-06 06:15:19 +00004498 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
4499 << "num_threads" << NumThreads->getSourceRange();
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004500 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00004501 }
4502 }
4503
Alexey Bataeved09d242014-05-28 05:53:51 +00004504 return new (Context)
4505 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00004506}
4507
Alexey Bataev62c87d22014-03-21 04:51:18 +00004508ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
4509 OpenMPClauseKind CKind) {
4510 if (!E)
4511 return ExprError();
4512 if (E->isValueDependent() || E->isTypeDependent() ||
4513 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00004514 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00004515 llvm::APSInt Result;
4516 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
4517 if (ICE.isInvalid())
4518 return ExprError();
4519 if (!Result.isStrictlyPositive()) {
4520 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
4521 << getOpenMPClauseName(CKind) << E->getSourceRange();
4522 return ExprError();
4523 }
Alexander Musman09184fe2014-09-30 05:29:28 +00004524 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
4525 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
4526 << E->getSourceRange();
4527 return ExprError();
4528 }
Alexey Bataev9c821032015-04-30 04:23:23 +00004529 if (CKind == OMPC_collapse) {
4530 DSAStack->setCollapseNumber(Result.getExtValue());
4531 }
Alexey Bataev62c87d22014-03-21 04:51:18 +00004532 return ICE;
4533}
4534
4535OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
4536 SourceLocation LParenLoc,
4537 SourceLocation EndLoc) {
4538 // OpenMP [2.8.1, simd construct, Description]
4539 // The parameter of the safelen clause must be a constant
4540 // positive integer expression.
4541 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
4542 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004543 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00004544 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00004545 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00004546}
4547
Alexander Musman64d33f12014-06-04 07:53:32 +00004548OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
4549 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00004550 SourceLocation LParenLoc,
4551 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00004552 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00004553 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00004554 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00004555 // The parameter of the collapse clause must be a constant
4556 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00004557 ExprResult NumForLoopsResult =
4558 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
4559 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00004560 return nullptr;
4561 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00004562 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00004563}
4564
Alexey Bataeved09d242014-05-28 05:53:51 +00004565OMPClause *Sema::ActOnOpenMPSimpleClause(
4566 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
4567 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004568 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004569 switch (Kind) {
4570 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00004571 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00004572 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
4573 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004574 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004575 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00004576 Res = ActOnOpenMPProcBindClause(
4577 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
4578 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004579 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004580 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00004581 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00004582 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00004583 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00004584 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00004585 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004586 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004587 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00004588 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00004589 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00004590 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00004591 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00004592 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004593 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00004594 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004595 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00004596 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00004597 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00004598 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004599 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00004600 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004601 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00004602 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00004603 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00004604 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00004605 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00004606 case OMPC_depend:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004607 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004608 llvm_unreachable("Clause is not allowed.");
4609 }
4610 return Res;
4611}
4612
4613OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
4614 SourceLocation KindKwLoc,
4615 SourceLocation StartLoc,
4616 SourceLocation LParenLoc,
4617 SourceLocation EndLoc) {
4618 if (Kind == OMPC_DEFAULT_unknown) {
4619 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00004620 static_assert(OMPC_DEFAULT_unknown > 0,
4621 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00004622 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00004623 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004624 Values += "'";
4625 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
4626 Values += "'";
4627 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00004628 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004629 Values += " or ";
4630 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00004631 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004632 break;
4633 default:
4634 Values += Sep;
4635 break;
4636 }
4637 }
4638 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00004639 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004640 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004641 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00004642 switch (Kind) {
4643 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00004644 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004645 break;
4646 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00004647 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004648 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004649 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004650 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00004651 break;
4652 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004653 return new (Context)
4654 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004655}
4656
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004657OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
4658 SourceLocation KindKwLoc,
4659 SourceLocation StartLoc,
4660 SourceLocation LParenLoc,
4661 SourceLocation EndLoc) {
4662 if (Kind == OMPC_PROC_BIND_unknown) {
4663 std::string Values;
4664 std::string Sep(", ");
4665 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
4666 Values += "'";
4667 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
4668 Values += "'";
4669 switch (i) {
4670 case OMPC_PROC_BIND_unknown - 2:
4671 Values += " or ";
4672 break;
4673 case OMPC_PROC_BIND_unknown - 1:
4674 break;
4675 default:
4676 Values += Sep;
4677 break;
4678 }
4679 }
4680 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00004681 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004682 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004683 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004684 return new (Context)
4685 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004686}
4687
Alexey Bataev56dafe82014-06-20 07:16:17 +00004688OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
4689 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
4690 SourceLocation StartLoc, SourceLocation LParenLoc,
4691 SourceLocation ArgumentLoc, SourceLocation CommaLoc,
4692 SourceLocation EndLoc) {
4693 OMPClause *Res = nullptr;
4694 switch (Kind) {
4695 case OMPC_schedule:
4696 Res = ActOnOpenMPScheduleClause(
4697 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
4698 LParenLoc, ArgumentLoc, CommaLoc, EndLoc);
4699 break;
4700 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00004701 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00004702 case OMPC_num_threads:
4703 case OMPC_safelen:
4704 case OMPC_collapse:
4705 case OMPC_default:
4706 case OMPC_proc_bind:
4707 case OMPC_private:
4708 case OMPC_firstprivate:
4709 case OMPC_lastprivate:
4710 case OMPC_shared:
4711 case OMPC_reduction:
4712 case OMPC_linear:
4713 case OMPC_aligned:
4714 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00004715 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004716 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00004717 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00004718 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00004719 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00004720 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00004721 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004722 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00004723 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00004724 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00004725 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00004726 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00004727 case OMPC_depend:
Alexey Bataev56dafe82014-06-20 07:16:17 +00004728 case OMPC_unknown:
4729 llvm_unreachable("Clause is not allowed.");
4730 }
4731 return Res;
4732}
4733
4734OMPClause *Sema::ActOnOpenMPScheduleClause(
4735 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
4736 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
4737 SourceLocation EndLoc) {
4738 if (Kind == OMPC_SCHEDULE_unknown) {
4739 std::string Values;
4740 std::string Sep(", ");
4741 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) {
4742 Values += "'";
4743 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i);
4744 Values += "'";
4745 switch (i) {
4746 case OMPC_SCHEDULE_unknown - 2:
4747 Values += " or ";
4748 break;
4749 case OMPC_SCHEDULE_unknown - 1:
4750 break;
4751 default:
4752 Values += Sep;
4753 break;
4754 }
4755 }
4756 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
4757 << Values << getOpenMPClauseName(OMPC_schedule);
4758 return nullptr;
4759 }
4760 Expr *ValExpr = ChunkSize;
Alexey Bataev040d5402015-05-12 08:35:28 +00004761 Expr *HelperValExpr = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00004762 if (ChunkSize) {
4763 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
4764 !ChunkSize->isInstantiationDependent() &&
4765 !ChunkSize->containsUnexpandedParameterPack()) {
4766 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
4767 ExprResult Val =
4768 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
4769 if (Val.isInvalid())
4770 return nullptr;
4771
4772 ValExpr = Val.get();
4773
4774 // OpenMP [2.7.1, Restrictions]
4775 // chunk_size must be a loop invariant integer expression with a positive
4776 // value.
4777 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00004778 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
4779 if (Result.isSigned() && !Result.isStrictlyPositive()) {
4780 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
4781 << "schedule" << ChunkSize->getSourceRange();
4782 return nullptr;
4783 }
4784 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
4785 auto *ImpVar = buildVarDecl(*this, ChunkSize->getExprLoc(),
4786 ChunkSize->getType(), ".chunk.");
4787 auto *ImpVarRef = buildDeclRefExpr(*this, ImpVar, ChunkSize->getType(),
4788 ChunkSize->getExprLoc(),
4789 /*RefersToCapture=*/true);
4790 HelperValExpr = ImpVarRef;
Alexey Bataev56dafe82014-06-20 07:16:17 +00004791 }
4792 }
4793 }
4794
4795 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc,
Alexey Bataev040d5402015-05-12 08:35:28 +00004796 EndLoc, Kind, ValExpr, HelperValExpr);
Alexey Bataev56dafe82014-06-20 07:16:17 +00004797}
4798
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004799OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
4800 SourceLocation StartLoc,
4801 SourceLocation EndLoc) {
4802 OMPClause *Res = nullptr;
4803 switch (Kind) {
4804 case OMPC_ordered:
4805 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
4806 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00004807 case OMPC_nowait:
4808 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
4809 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00004810 case OMPC_untied:
4811 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
4812 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00004813 case OMPC_mergeable:
4814 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
4815 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004816 case OMPC_read:
4817 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
4818 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00004819 case OMPC_write:
4820 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
4821 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00004822 case OMPC_update:
4823 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
4824 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00004825 case OMPC_capture:
4826 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
4827 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00004828 case OMPC_seq_cst:
4829 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
4830 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004831 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00004832 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004833 case OMPC_num_threads:
4834 case OMPC_safelen:
4835 case OMPC_collapse:
4836 case OMPC_schedule:
4837 case OMPC_private:
4838 case OMPC_firstprivate:
4839 case OMPC_lastprivate:
4840 case OMPC_shared:
4841 case OMPC_reduction:
4842 case OMPC_linear:
4843 case OMPC_aligned:
4844 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00004845 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004846 case OMPC_default:
4847 case OMPC_proc_bind:
4848 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00004849 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00004850 case OMPC_depend:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004851 case OMPC_unknown:
4852 llvm_unreachable("Clause is not allowed.");
4853 }
4854 return Res;
4855}
4856
4857OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
4858 SourceLocation EndLoc) {
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004859 DSAStack->setOrderedRegion();
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004860 return new (Context) OMPOrderedClause(StartLoc, EndLoc);
4861}
4862
Alexey Bataev236070f2014-06-20 11:19:47 +00004863OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
4864 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00004865 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00004866 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
4867}
4868
Alexey Bataev7aea99a2014-07-17 12:19:31 +00004869OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
4870 SourceLocation EndLoc) {
4871 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
4872}
4873
Alexey Bataev74ba3a52014-07-17 12:47:03 +00004874OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
4875 SourceLocation EndLoc) {
4876 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
4877}
4878
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004879OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
4880 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004881 return new (Context) OMPReadClause(StartLoc, EndLoc);
4882}
4883
Alexey Bataevdea47612014-07-23 07:46:59 +00004884OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
4885 SourceLocation EndLoc) {
4886 return new (Context) OMPWriteClause(StartLoc, EndLoc);
4887}
4888
Alexey Bataev67a4f222014-07-23 10:25:33 +00004889OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
4890 SourceLocation EndLoc) {
4891 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
4892}
4893
Alexey Bataev459dec02014-07-24 06:46:57 +00004894OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
4895 SourceLocation EndLoc) {
4896 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
4897}
4898
Alexey Bataev82bad8b2014-07-24 08:55:34 +00004899OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
4900 SourceLocation EndLoc) {
4901 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
4902}
4903
Alexey Bataevc5e02582014-06-16 07:08:35 +00004904OMPClause *Sema::ActOnOpenMPVarListClause(
4905 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
4906 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
4907 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00004908 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
4909 SourceLocation DepLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004910 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004911 switch (Kind) {
4912 case OMPC_private:
4913 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
4914 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004915 case OMPC_firstprivate:
4916 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
4917 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00004918 case OMPC_lastprivate:
4919 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
4920 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00004921 case OMPC_shared:
4922 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
4923 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00004924 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00004925 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
4926 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00004927 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00004928 case OMPC_linear:
4929 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
4930 ColonLoc, EndLoc);
4931 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00004932 case OMPC_aligned:
4933 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
4934 ColonLoc, EndLoc);
4935 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004936 case OMPC_copyin:
4937 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
4938 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00004939 case OMPC_copyprivate:
4940 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
4941 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00004942 case OMPC_flush:
4943 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
4944 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00004945 case OMPC_depend:
4946 Res = ActOnOpenMPDependClause(DepKind, DepLoc, ColonLoc, VarList, StartLoc,
4947 LParenLoc, EndLoc);
4948 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004949 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00004950 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00004951 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00004952 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00004953 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004954 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004955 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00004956 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004957 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00004958 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00004959 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00004960 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004961 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004962 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00004963 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00004964 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00004965 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00004966 case OMPC_seq_cst:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004967 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004968 llvm_unreachable("Clause is not allowed.");
4969 }
4970 return Res;
4971}
4972
4973OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
4974 SourceLocation StartLoc,
4975 SourceLocation LParenLoc,
4976 SourceLocation EndLoc) {
4977 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00004978 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00004979 for (auto &RefExpr : VarList) {
4980 assert(RefExpr && "NULL expr in OpenMP private clause.");
4981 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004982 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00004983 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00004984 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004985 continue;
4986 }
4987
Alexey Bataeved09d242014-05-28 05:53:51 +00004988 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004989 // OpenMP [2.1, C/C++]
4990 // A list item is a variable name.
4991 // OpenMP [2.9.3.3, Restrictions, p.1]
4992 // A variable that is part of another variable (as an array or
4993 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00004994 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004995 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00004996 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004997 continue;
4998 }
4999 Decl *D = DE->getDecl();
5000 VarDecl *VD = cast<VarDecl>(D);
5001
5002 QualType Type = VD->getType();
5003 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5004 // It will be analyzed later.
5005 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00005006 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005007 continue;
5008 }
5009
5010 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
5011 // A variable that appears in a private clause must not have an incomplete
5012 // type or a reference type.
5013 if (RequireCompleteType(ELoc, Type,
5014 diag::err_omp_private_incomplete_type)) {
5015 continue;
5016 }
5017 if (Type->isReferenceType()) {
5018 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00005019 << getOpenMPClauseName(OMPC_private) << Type;
5020 bool IsDecl =
5021 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5022 Diag(VD->getLocation(),
5023 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5024 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005025 continue;
5026 }
5027
Alexey Bataev758e55e2013-09-06 18:03:48 +00005028 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
5029 // in a Construct]
5030 // Variables with the predetermined data-sharing attributes may not be
5031 // listed in data-sharing attributes clauses, except for the cases
5032 // listed below. For these exceptions only, listing a predetermined
5033 // variable in a data-sharing attribute clause is allowed and overrides
5034 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005035 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005036 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005037 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
5038 << getOpenMPClauseName(OMPC_private);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005039 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005040 continue;
5041 }
5042
Alexey Bataevccb59ec2015-05-19 08:44:56 +00005043 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00005044 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00005045 DSAStack->getCurrentDirective() == OMPD_task) {
5046 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
5047 << getOpenMPClauseName(OMPC_private) << Type
5048 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
5049 bool IsDecl =
5050 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5051 Diag(VD->getLocation(),
5052 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5053 << VD;
5054 continue;
5055 }
5056
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005057 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
5058 // A variable of class type (or array thereof) that appears in a private
5059 // clause requires an accessible, unambiguous default constructor for the
5060 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00005061 // Generate helper private variable and initialize it with the default
5062 // value. The address of the original variable is replaced by the address of
5063 // the new private variable in CodeGen. This new variable is not added to
5064 // IdResolver, so the code in the OpenMP region uses original variable for
5065 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005066 Type = Type.getUnqualifiedType();
5067 auto VDPrivate = buildVarDecl(*this, DE->getExprLoc(), Type, VD->getName());
Alexey Bataev39f915b82015-05-08 10:41:21 +00005068 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00005069 if (VDPrivate->isInvalidDecl())
5070 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005071 auto VDPrivateRefExpr = buildDeclRefExpr(
5072 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev03b340a2014-10-21 03:16:40 +00005073
Alexey Bataev758e55e2013-09-06 18:03:48 +00005074 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005075 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00005076 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005077 }
5078
Alexey Bataeved09d242014-05-28 05:53:51 +00005079 if (Vars.empty())
5080 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005081
Alexey Bataev03b340a2014-10-21 03:16:40 +00005082 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
5083 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005084}
5085
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005086namespace {
5087class DiagsUninitializedSeveretyRAII {
5088private:
5089 DiagnosticsEngine &Diags;
5090 SourceLocation SavedLoc;
5091 bool IsIgnored;
5092
5093public:
5094 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
5095 bool IsIgnored)
5096 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
5097 if (!IsIgnored) {
5098 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
5099 /*Map*/ diag::Severity::Ignored, Loc);
5100 }
5101 }
5102 ~DiagsUninitializedSeveretyRAII() {
5103 if (!IsIgnored)
5104 Diags.popMappings(SavedLoc);
5105 }
5106};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00005107}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005108
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005109OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
5110 SourceLocation StartLoc,
5111 SourceLocation LParenLoc,
5112 SourceLocation EndLoc) {
5113 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005114 SmallVector<Expr *, 8> PrivateCopies;
5115 SmallVector<Expr *, 8> Inits;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005116 bool IsImplicitClause =
5117 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
5118 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
5119
Alexey Bataeved09d242014-05-28 05:53:51 +00005120 for (auto &RefExpr : VarList) {
5121 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
5122 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005123 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00005124 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005125 PrivateCopies.push_back(nullptr);
5126 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005127 continue;
5128 }
5129
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005130 SourceLocation ELoc =
5131 IsImplicitClause ? ImplicitClauseLoc : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005132 // OpenMP [2.1, C/C++]
5133 // A list item is a variable name.
5134 // OpenMP [2.9.3.3, Restrictions, p.1]
5135 // A variable that is part of another variable (as an array or
5136 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00005137 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005138 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005139 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005140 continue;
5141 }
5142 Decl *D = DE->getDecl();
5143 VarDecl *VD = cast<VarDecl>(D);
5144
5145 QualType Type = VD->getType();
5146 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5147 // It will be analyzed later.
5148 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005149 PrivateCopies.push_back(nullptr);
5150 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005151 continue;
5152 }
5153
5154 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
5155 // A variable that appears in a private clause must not have an incomplete
5156 // type or a reference type.
5157 if (RequireCompleteType(ELoc, Type,
5158 diag::err_omp_firstprivate_incomplete_type)) {
5159 continue;
5160 }
5161 if (Type->isReferenceType()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005162 if (IsImplicitClause) {
5163 Diag(ImplicitClauseLoc,
5164 diag::err_omp_task_predetermined_firstprivate_ref_type_arg)
5165 << Type;
5166 Diag(RefExpr->getExprLoc(), diag::note_used_here);
5167 } else {
5168 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
5169 << getOpenMPClauseName(OMPC_firstprivate) << Type;
5170 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005171 bool IsDecl =
5172 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5173 Diag(VD->getLocation(),
5174 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5175 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005176 continue;
5177 }
5178
5179 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
5180 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00005181 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005182 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005183 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005184
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005185 // If an implicit firstprivate variable found it was checked already.
5186 if (!IsImplicitClause) {
5187 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005188 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005189 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
5190 // A list item that specifies a given variable may not appear in more
5191 // than one clause on the same directive, except that a variable may be
5192 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005193 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00005194 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005195 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00005196 << getOpenMPClauseName(DVar.CKind)
5197 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005198 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005199 continue;
5200 }
5201
5202 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
5203 // in a Construct]
5204 // Variables with the predetermined data-sharing attributes may not be
5205 // listed in data-sharing attributes clauses, except for the cases
5206 // listed below. For these exceptions only, listing a predetermined
5207 // variable in a data-sharing attribute clause is allowed and overrides
5208 // the variable's predetermined data-sharing attributes.
5209 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
5210 // in a Construct, C/C++, p.2]
5211 // Variables with const-qualified type having no mutable member may be
5212 // listed in a firstprivate clause, even if they are static data members.
5213 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
5214 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
5215 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00005216 << getOpenMPClauseName(DVar.CKind)
5217 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005218 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005219 continue;
5220 }
5221
Alexey Bataevf29276e2014-06-18 04:14:57 +00005222 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005223 // OpenMP [2.9.3.4, Restrictions, p.2]
5224 // A list item that is private within a parallel region must not appear
5225 // in a firstprivate clause on a worksharing construct if any of the
5226 // worksharing regions arising from the worksharing construct ever bind
5227 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00005228 if (isOpenMPWorksharingDirective(CurrDir) &&
5229 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005230 DVar = DSAStack->getImplicitDSA(VD, true);
5231 if (DVar.CKind != OMPC_shared &&
5232 (isOpenMPParallelDirective(DVar.DKind) ||
5233 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00005234 Diag(ELoc, diag::err_omp_required_access)
5235 << getOpenMPClauseName(OMPC_firstprivate)
5236 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005237 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005238 continue;
5239 }
5240 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005241 // OpenMP [2.9.3.4, Restrictions, p.3]
5242 // A list item that appears in a reduction clause of a parallel construct
5243 // must not appear in a firstprivate clause on a worksharing or task
5244 // construct if any of the worksharing or task regions arising from the
5245 // worksharing or task construct ever bind to any of the parallel regions
5246 // arising from the parallel construct.
5247 // OpenMP [2.9.3.4, Restrictions, p.4]
5248 // A list item that appears in a reduction clause in worksharing
5249 // construct must not appear in a firstprivate clause in a task construct
5250 // encountered during execution of any of the worksharing regions arising
5251 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005252 if (CurrDir == OMPD_task) {
5253 DVar =
5254 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
5255 [](OpenMPDirectiveKind K) -> bool {
5256 return isOpenMPParallelDirective(K) ||
5257 isOpenMPWorksharingDirective(K);
5258 },
5259 false);
5260 if (DVar.CKind == OMPC_reduction &&
5261 (isOpenMPParallelDirective(DVar.DKind) ||
5262 isOpenMPWorksharingDirective(DVar.DKind))) {
5263 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
5264 << getOpenMPDirectiveName(DVar.DKind);
5265 ReportOriginalDSA(*this, DSAStack, VD, DVar);
5266 continue;
5267 }
5268 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005269 }
5270
Alexey Bataevccb59ec2015-05-19 08:44:56 +00005271 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00005272 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00005273 DSAStack->getCurrentDirective() == OMPD_task) {
5274 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
5275 << getOpenMPClauseName(OMPC_firstprivate) << Type
5276 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
5277 bool IsDecl =
5278 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5279 Diag(VD->getLocation(),
5280 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5281 << VD;
5282 continue;
5283 }
5284
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005285 Type = Type.getUnqualifiedType();
5286 auto VDPrivate = buildVarDecl(*this, ELoc, Type, VD->getName());
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005287 // Generate helper private variable and initialize it with the value of the
5288 // original variable. The address of the original variable is replaced by
5289 // the address of the new private variable in the CodeGen. This new variable
5290 // is not added to IdResolver, so the code in the OpenMP region uses
5291 // original variable for proper diagnostics and variable capturing.
5292 Expr *VDInitRefExpr = nullptr;
5293 // For arrays generate initializer for single element and replace it by the
5294 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005295 if (Type->isArrayType()) {
5296 auto VDInit =
5297 buildVarDecl(*this, DE->getExprLoc(), ElemType, VD->getName());
5298 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005299 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005300 ElemType = ElemType.getUnqualifiedType();
5301 auto *VDInitTemp = buildVarDecl(*this, DE->getLocStart(), ElemType,
5302 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00005303 InitializedEntity Entity =
5304 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005305 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
5306
5307 InitializationSequence InitSeq(*this, Entity, Kind, Init);
5308 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
5309 if (Result.isInvalid())
5310 VDPrivate->setInvalidDecl();
5311 else
5312 VDPrivate->setInit(Result.getAs<Expr>());
5313 } else {
Alexey Bataev69c62a92015-04-15 04:52:20 +00005314 auto *VDInit =
Alexey Bataev39f915b82015-05-08 10:41:21 +00005315 buildVarDecl(*this, DE->getLocStart(), Type, ".firstprivate.temp");
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005316 VDInitRefExpr =
5317 buildDeclRefExpr(*this, VDInit, DE->getType(), DE->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00005318 AddInitializerToDecl(VDPrivate,
5319 DefaultLvalueConversion(VDInitRefExpr).get(),
5320 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005321 }
5322 if (VDPrivate->isInvalidDecl()) {
5323 if (IsImplicitClause) {
5324 Diag(DE->getExprLoc(),
5325 diag::note_omp_task_predetermined_firstprivate_here);
5326 }
5327 continue;
5328 }
5329 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00005330 auto VDPrivateRefExpr = buildDeclRefExpr(
5331 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005332 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
5333 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005334 PrivateCopies.push_back(VDPrivateRefExpr);
5335 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005336 }
5337
Alexey Bataeved09d242014-05-28 05:53:51 +00005338 if (Vars.empty())
5339 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005340
5341 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005342 Vars, PrivateCopies, Inits);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005343}
5344
Alexander Musman1bb328c2014-06-04 13:06:39 +00005345OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
5346 SourceLocation StartLoc,
5347 SourceLocation LParenLoc,
5348 SourceLocation EndLoc) {
5349 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00005350 SmallVector<Expr *, 8> SrcExprs;
5351 SmallVector<Expr *, 8> DstExprs;
5352 SmallVector<Expr *, 8> AssignmentOps;
Alexander Musman1bb328c2014-06-04 13:06:39 +00005353 for (auto &RefExpr : VarList) {
5354 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
5355 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
5356 // It will be analyzed later.
5357 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00005358 SrcExprs.push_back(nullptr);
5359 DstExprs.push_back(nullptr);
5360 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005361 continue;
5362 }
5363
5364 SourceLocation ELoc = RefExpr->getExprLoc();
5365 // OpenMP [2.1, C/C++]
5366 // A list item is a variable name.
5367 // OpenMP [2.14.3.5, Restrictions, p.1]
5368 // A variable that is part of another variable (as an array or structure
5369 // element) cannot appear in a lastprivate clause.
5370 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
5371 if (!DE || !isa<VarDecl>(DE->getDecl())) {
5372 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
5373 continue;
5374 }
5375 Decl *D = DE->getDecl();
5376 VarDecl *VD = cast<VarDecl>(D);
5377
5378 QualType Type = VD->getType();
5379 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5380 // It will be analyzed later.
5381 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00005382 SrcExprs.push_back(nullptr);
5383 DstExprs.push_back(nullptr);
5384 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005385 continue;
5386 }
5387
5388 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
5389 // A variable that appears in a lastprivate clause must not have an
5390 // incomplete type or a reference type.
5391 if (RequireCompleteType(ELoc, Type,
5392 diag::err_omp_lastprivate_incomplete_type)) {
5393 continue;
5394 }
5395 if (Type->isReferenceType()) {
5396 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
5397 << getOpenMPClauseName(OMPC_lastprivate) << Type;
5398 bool IsDecl =
5399 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5400 Diag(VD->getLocation(),
5401 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5402 << VD;
5403 continue;
5404 }
5405
5406 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
5407 // in a Construct]
5408 // Variables with the predetermined data-sharing attributes may not be
5409 // listed in data-sharing attributes clauses, except for the cases
5410 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005411 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005412 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
5413 DVar.CKind != OMPC_firstprivate &&
5414 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
5415 Diag(ELoc, diag::err_omp_wrong_dsa)
5416 << getOpenMPClauseName(DVar.CKind)
5417 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005418 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005419 continue;
5420 }
5421
Alexey Bataevf29276e2014-06-18 04:14:57 +00005422 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
5423 // OpenMP [2.14.3.5, Restrictions, p.2]
5424 // A list item that is private within a parallel region, or that appears in
5425 // the reduction clause of a parallel construct, must not appear in a
5426 // lastprivate clause on a worksharing construct if any of the corresponding
5427 // worksharing regions ever binds to any of the corresponding parallel
5428 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005429 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00005430 if (isOpenMPWorksharingDirective(CurrDir) &&
5431 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005432 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005433 if (DVar.CKind != OMPC_shared) {
5434 Diag(ELoc, diag::err_omp_required_access)
5435 << getOpenMPClauseName(OMPC_lastprivate)
5436 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005437 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005438 continue;
5439 }
5440 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00005441 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00005442 // A variable of class type (or array thereof) that appears in a
5443 // lastprivate clause requires an accessible, unambiguous default
5444 // constructor for the class type, unless the list item is also specified
5445 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00005446 // A variable of class type (or array thereof) that appears in a
5447 // lastprivate clause requires an accessible, unambiguous copy assignment
5448 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00005449 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00005450 auto *SrcVD = buildVarDecl(*this, DE->getLocStart(),
Alexey Bataev38e89532015-04-16 04:54:05 +00005451 Type.getUnqualifiedType(), ".lastprivate.src");
Alexey Bataev39f915b82015-05-08 10:41:21 +00005452 auto *PseudoSrcExpr = buildDeclRefExpr(
5453 *this, SrcVD, Type.getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00005454 auto *DstVD =
Alexey Bataev39f915b82015-05-08 10:41:21 +00005455 buildVarDecl(*this, DE->getLocStart(), Type, ".lastprivate.dst");
Alexey Bataev38e89532015-04-16 04:54:05 +00005456 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00005457 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00005458 // For arrays generate assignment operation for single element and replace
5459 // it by the original array element in CodeGen.
5460 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
5461 PseudoDstExpr, PseudoSrcExpr);
5462 if (AssignmentOp.isInvalid())
5463 continue;
5464 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
5465 /*DiscardedValue=*/true);
5466 if (AssignmentOp.isInvalid())
5467 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00005468
Alexey Bataev39f915b82015-05-08 10:41:21 +00005469 if (TopDVar.CKind != OMPC_firstprivate)
Alexey Bataevf29276e2014-06-18 04:14:57 +00005470 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005471 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00005472 SrcExprs.push_back(PseudoSrcExpr);
5473 DstExprs.push_back(PseudoDstExpr);
5474 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00005475 }
5476
5477 if (Vars.empty())
5478 return nullptr;
5479
5480 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00005481 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005482}
5483
Alexey Bataev758e55e2013-09-06 18:03:48 +00005484OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
5485 SourceLocation StartLoc,
5486 SourceLocation LParenLoc,
5487 SourceLocation EndLoc) {
5488 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00005489 for (auto &RefExpr : VarList) {
5490 assert(RefExpr && "NULL expr in OpenMP shared clause.");
5491 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00005492 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00005493 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005494 continue;
5495 }
5496
Alexey Bataeved09d242014-05-28 05:53:51 +00005497 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00005498 // OpenMP [2.1, C/C++]
5499 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00005500 // OpenMP [2.14.3.2, Restrictions, p.1]
5501 // A variable that is part of another variable (as an array or structure
5502 // element) cannot appear in a shared unless it is a static data member
5503 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00005504 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005505 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005506 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00005507 continue;
5508 }
5509 Decl *D = DE->getDecl();
5510 VarDecl *VD = cast<VarDecl>(D);
5511
5512 QualType Type = VD->getType();
5513 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5514 // It will be analyzed later.
5515 Vars.push_back(DE);
5516 continue;
5517 }
5518
5519 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
5520 // in a Construct]
5521 // Variables with the predetermined data-sharing attributes may not be
5522 // listed in data-sharing attributes clauses, except for the cases
5523 // listed below. For these exceptions only, listing a predetermined
5524 // variable in a data-sharing attribute clause is allowed and overrides
5525 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005526 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00005527 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
5528 DVar.RefExpr) {
5529 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
5530 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005531 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005532 continue;
5533 }
5534
5535 DSAStack->addDSA(VD, DE, OMPC_shared);
5536 Vars.push_back(DE);
5537 }
5538
Alexey Bataeved09d242014-05-28 05:53:51 +00005539 if (Vars.empty())
5540 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00005541
5542 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
5543}
5544
Alexey Bataevc5e02582014-06-16 07:08:35 +00005545namespace {
5546class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
5547 DSAStackTy *Stack;
5548
5549public:
5550 bool VisitDeclRefExpr(DeclRefExpr *E) {
5551 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005552 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00005553 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
5554 return false;
5555 if (DVar.CKind != OMPC_unknown)
5556 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00005557 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005558 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005559 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00005560 return true;
5561 return false;
5562 }
5563 return false;
5564 }
5565 bool VisitStmt(Stmt *S) {
5566 for (auto Child : S->children()) {
5567 if (Child && Visit(Child))
5568 return true;
5569 }
5570 return false;
5571 }
Alexey Bataev23b69422014-06-18 07:08:49 +00005572 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00005573};
Alexey Bataev23b69422014-06-18 07:08:49 +00005574} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00005575
5576OMPClause *Sema::ActOnOpenMPReductionClause(
5577 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
5578 SourceLocation ColonLoc, SourceLocation EndLoc,
5579 CXXScopeSpec &ReductionIdScopeSpec,
5580 const DeclarationNameInfo &ReductionId) {
5581 // TODO: Allow scope specification search when 'declare reduction' is
5582 // supported.
5583 assert(ReductionIdScopeSpec.isEmpty() &&
5584 "No support for scoped reduction identifiers yet.");
5585
5586 auto DN = ReductionId.getName();
5587 auto OOK = DN.getCXXOverloadedOperator();
5588 BinaryOperatorKind BOK = BO_Comma;
5589
5590 // OpenMP [2.14.3.6, reduction clause]
5591 // C
5592 // reduction-identifier is either an identifier or one of the following
5593 // operators: +, -, *, &, |, ^, && and ||
5594 // C++
5595 // reduction-identifier is either an id-expression or one of the following
5596 // operators: +, -, *, &, |, ^, && and ||
5597 // FIXME: Only 'min' and 'max' identifiers are supported for now.
5598 switch (OOK) {
5599 case OO_Plus:
5600 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005601 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005602 break;
5603 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005604 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005605 break;
5606 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005607 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005608 break;
5609 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005610 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005611 break;
5612 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005613 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005614 break;
5615 case OO_AmpAmp:
5616 BOK = BO_LAnd;
5617 break;
5618 case OO_PipePipe:
5619 BOK = BO_LOr;
5620 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005621 case OO_New:
5622 case OO_Delete:
5623 case OO_Array_New:
5624 case OO_Array_Delete:
5625 case OO_Slash:
5626 case OO_Percent:
5627 case OO_Tilde:
5628 case OO_Exclaim:
5629 case OO_Equal:
5630 case OO_Less:
5631 case OO_Greater:
5632 case OO_LessEqual:
5633 case OO_GreaterEqual:
5634 case OO_PlusEqual:
5635 case OO_MinusEqual:
5636 case OO_StarEqual:
5637 case OO_SlashEqual:
5638 case OO_PercentEqual:
5639 case OO_CaretEqual:
5640 case OO_AmpEqual:
5641 case OO_PipeEqual:
5642 case OO_LessLess:
5643 case OO_GreaterGreater:
5644 case OO_LessLessEqual:
5645 case OO_GreaterGreaterEqual:
5646 case OO_EqualEqual:
5647 case OO_ExclaimEqual:
5648 case OO_PlusPlus:
5649 case OO_MinusMinus:
5650 case OO_Comma:
5651 case OO_ArrowStar:
5652 case OO_Arrow:
5653 case OO_Call:
5654 case OO_Subscript:
5655 case OO_Conditional:
5656 case NUM_OVERLOADED_OPERATORS:
5657 llvm_unreachable("Unexpected reduction identifier");
5658 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00005659 if (auto II = DN.getAsIdentifierInfo()) {
5660 if (II->isStr("max"))
5661 BOK = BO_GT;
5662 else if (II->isStr("min"))
5663 BOK = BO_LT;
5664 }
5665 break;
5666 }
5667 SourceRange ReductionIdRange;
5668 if (ReductionIdScopeSpec.isValid()) {
5669 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
5670 }
5671 ReductionIdRange.setEnd(ReductionId.getEndLoc());
5672 if (BOK == BO_Comma) {
5673 // Not allowed reduction identifier is found.
5674 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
5675 << ReductionIdRange;
5676 return nullptr;
5677 }
5678
5679 SmallVector<Expr *, 8> Vars;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005680 SmallVector<Expr *, 8> LHSs;
5681 SmallVector<Expr *, 8> RHSs;
5682 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005683 for (auto RefExpr : VarList) {
5684 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
5685 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
5686 // It will be analyzed later.
5687 Vars.push_back(RefExpr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005688 LHSs.push_back(nullptr);
5689 RHSs.push_back(nullptr);
5690 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00005691 continue;
5692 }
5693
5694 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
5695 RefExpr->isInstantiationDependent() ||
5696 RefExpr->containsUnexpandedParameterPack()) {
5697 // It will be analyzed later.
5698 Vars.push_back(RefExpr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005699 LHSs.push_back(nullptr);
5700 RHSs.push_back(nullptr);
5701 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00005702 continue;
5703 }
5704
5705 auto ELoc = RefExpr->getExprLoc();
5706 auto ERange = RefExpr->getSourceRange();
5707 // OpenMP [2.1, C/C++]
5708 // A list item is a variable or array section, subject to the restrictions
5709 // specified in Section 2.4 on page 42 and in each of the sections
5710 // describing clauses and directives for which a list appears.
5711 // OpenMP [2.14.3.3, Restrictions, p.1]
5712 // A variable that is part of another variable (as an array or
5713 // structure element) cannot appear in a private clause.
5714 auto DE = dyn_cast<DeclRefExpr>(RefExpr);
5715 if (!DE || !isa<VarDecl>(DE->getDecl())) {
5716 Diag(ELoc, diag::err_omp_expected_var_name) << ERange;
5717 continue;
5718 }
5719 auto D = DE->getDecl();
5720 auto VD = cast<VarDecl>(D);
5721 auto Type = VD->getType();
5722 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
5723 // A variable that appears in a private clause must not have an incomplete
5724 // type or a reference type.
5725 if (RequireCompleteType(ELoc, Type,
5726 diag::err_omp_reduction_incomplete_type))
5727 continue;
5728 // OpenMP [2.14.3.6, reduction clause, Restrictions]
5729 // Arrays may not appear in a reduction clause.
5730 if (Type.getNonReferenceType()->isArrayType()) {
5731 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
5732 bool IsDecl =
5733 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5734 Diag(VD->getLocation(),
5735 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5736 << VD;
5737 continue;
5738 }
5739 // OpenMP [2.14.3.6, reduction clause, Restrictions]
5740 // A list item that appears in a reduction clause must not be
5741 // const-qualified.
5742 if (Type.getNonReferenceType().isConstant(Context)) {
5743 Diag(ELoc, diag::err_omp_const_variable)
5744 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
5745 bool IsDecl =
5746 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5747 Diag(VD->getLocation(),
5748 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5749 << VD;
5750 continue;
5751 }
5752 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
5753 // If a list-item is a reference type then it must bind to the same object
5754 // for all threads of the team.
5755 VarDecl *VDDef = VD->getDefinition();
5756 if (Type->isReferenceType() && VDDef) {
5757 DSARefChecker Check(DSAStack);
5758 if (Check.Visit(VDDef->getInit())) {
5759 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
5760 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
5761 continue;
5762 }
5763 }
5764 // OpenMP [2.14.3.6, reduction clause, Restrictions]
5765 // The type of a list item that appears in a reduction clause must be valid
5766 // for the reduction-identifier. For a max or min reduction in C, the type
5767 // of the list item must be an allowed arithmetic data type: char, int,
5768 // float, double, or _Bool, possibly modified with long, short, signed, or
5769 // unsigned. For a max or min reduction in C++, the type of the list item
5770 // must be an allowed arithmetic data type: char, wchar_t, int, float,
5771 // double, or bool, possibly modified with long, short, signed, or unsigned.
5772 if ((BOK == BO_GT || BOK == BO_LT) &&
5773 !(Type->isScalarType() ||
5774 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
5775 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
5776 << getLangOpts().CPlusPlus;
5777 bool IsDecl =
5778 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5779 Diag(VD->getLocation(),
5780 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5781 << VD;
5782 continue;
5783 }
5784 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
5785 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
5786 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
5787 bool IsDecl =
5788 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5789 Diag(VD->getLocation(),
5790 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5791 << VD;
5792 continue;
5793 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00005794 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
5795 // in a Construct]
5796 // Variables with the predetermined data-sharing attributes may not be
5797 // listed in data-sharing attributes clauses, except for the cases
5798 // listed below. For these exceptions only, listing a predetermined
5799 // variable in a data-sharing attribute clause is allowed and overrides
5800 // the variable's predetermined data-sharing attributes.
5801 // OpenMP [2.14.3.6, Restrictions, p.3]
5802 // Any number of reduction clauses can be specified on the directive,
5803 // but a list item can appear only once in the reduction clauses for that
5804 // directive.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005805 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00005806 if (DVar.CKind == OMPC_reduction) {
5807 Diag(ELoc, diag::err_omp_once_referenced)
5808 << getOpenMPClauseName(OMPC_reduction);
5809 if (DVar.RefExpr) {
5810 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
5811 }
5812 } else if (DVar.CKind != OMPC_unknown) {
5813 Diag(ELoc, diag::err_omp_wrong_dsa)
5814 << getOpenMPClauseName(DVar.CKind)
5815 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005816 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00005817 continue;
5818 }
5819
5820 // OpenMP [2.14.3.6, Restrictions, p.1]
5821 // A list item that appears in a reduction clause of a worksharing
5822 // construct must be shared in the parallel regions to which any of the
5823 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf29276e2014-06-18 04:14:57 +00005824 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataev549210e2014-06-24 04:39:47 +00005825 if (isOpenMPWorksharingDirective(CurrDir) &&
5826 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005827 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005828 if (DVar.CKind != OMPC_shared) {
5829 Diag(ELoc, diag::err_omp_required_access)
5830 << getOpenMPClauseName(OMPC_reduction)
5831 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005832 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005833 continue;
5834 }
5835 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005836 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00005837 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs");
5838 auto *RHSVD = buildVarDecl(*this, ELoc, Type, VD->getName());
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005839 // Add initializer for private variable.
5840 Expr *Init = nullptr;
5841 switch (BOK) {
5842 case BO_Add:
5843 case BO_Xor:
5844 case BO_Or:
5845 case BO_LOr:
5846 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
5847 if (Type->isScalarType() || Type->isAnyComplexType()) {
5848 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00005849 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005850 break;
5851 case BO_Mul:
5852 case BO_LAnd:
5853 if (Type->isScalarType() || Type->isAnyComplexType()) {
5854 // '*' and '&&' reduction ops - initializer is '1'.
5855 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
5856 }
5857 break;
5858 case BO_And: {
5859 // '&' reduction op - initializer is '~0'.
5860 QualType OrigType = Type;
5861 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) {
5862 Type = ComplexTy->getElementType();
5863 }
5864 if (Type->isRealFloatingType()) {
5865 llvm::APFloat InitValue =
5866 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
5867 /*isIEEE=*/true);
5868 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
5869 Type, ELoc);
5870 } else if (Type->isScalarType()) {
5871 auto Size = Context.getTypeSize(Type);
5872 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
5873 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
5874 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
5875 }
5876 if (Init && OrigType->isAnyComplexType()) {
5877 // Init = 0xFFFF + 0xFFFFi;
5878 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
5879 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
5880 }
5881 Type = OrigType;
5882 break;
5883 }
5884 case BO_LT:
5885 case BO_GT: {
5886 // 'min' reduction op - initializer is 'Largest representable number in
5887 // the reduction list item type'.
5888 // 'max' reduction op - initializer is 'Least representable number in
5889 // the reduction list item type'.
5890 if (Type->isIntegerType() || Type->isPointerType()) {
5891 bool IsSigned = Type->hasSignedIntegerRepresentation();
5892 auto Size = Context.getTypeSize(Type);
5893 QualType IntTy =
5894 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
5895 llvm::APInt InitValue =
5896 (BOK != BO_LT)
5897 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
5898 : llvm::APInt::getMinValue(Size)
5899 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
5900 : llvm::APInt::getMaxValue(Size);
5901 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
5902 if (Type->isPointerType()) {
5903 // Cast to pointer type.
5904 auto CastExpr = BuildCStyleCastExpr(
5905 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
5906 SourceLocation(), Init);
5907 if (CastExpr.isInvalid())
5908 continue;
5909 Init = CastExpr.get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00005910 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005911 } else if (Type->isRealFloatingType()) {
5912 llvm::APFloat InitValue = llvm::APFloat::getLargest(
5913 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
5914 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
5915 Type, ELoc);
5916 }
5917 break;
5918 }
5919 case BO_PtrMemD:
5920 case BO_PtrMemI:
5921 case BO_MulAssign:
5922 case BO_Div:
5923 case BO_Rem:
5924 case BO_Sub:
5925 case BO_Shl:
5926 case BO_Shr:
5927 case BO_LE:
5928 case BO_GE:
5929 case BO_EQ:
5930 case BO_NE:
5931 case BO_AndAssign:
5932 case BO_XorAssign:
5933 case BO_OrAssign:
5934 case BO_Assign:
5935 case BO_AddAssign:
5936 case BO_SubAssign:
5937 case BO_DivAssign:
5938 case BO_RemAssign:
5939 case BO_ShlAssign:
5940 case BO_ShrAssign:
5941 case BO_Comma:
5942 llvm_unreachable("Unexpected reduction operation");
5943 }
5944 if (Init) {
5945 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
5946 /*TypeMayContainAuto=*/false);
5947 } else {
5948 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
5949 }
5950 if (!RHSVD->hasInit()) {
5951 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
5952 << ReductionIdRange;
5953 bool IsDecl =
5954 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5955 Diag(VD->getLocation(),
5956 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5957 << VD;
5958 continue;
5959 }
Alexey Bataev39f915b82015-05-08 10:41:21 +00005960 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
5961 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005962 ExprResult ReductionOp =
5963 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
5964 LHSDRE, RHSDRE);
5965 if (ReductionOp.isUsable()) {
Alexey Bataev69a47792015-05-07 03:54:03 +00005966 if (BOK != BO_LT && BOK != BO_GT) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005967 ReductionOp =
5968 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
5969 BO_Assign, LHSDRE, ReductionOp.get());
5970 } else {
5971 auto *ConditionalOp = new (Context) ConditionalOperator(
5972 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
5973 RHSDRE, Type, VK_LValue, OK_Ordinary);
5974 ReductionOp =
5975 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
5976 BO_Assign, LHSDRE, ConditionalOp);
5977 }
5978 if (ReductionOp.isUsable()) {
5979 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00005980 }
5981 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005982 if (ReductionOp.isInvalid())
5983 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005984
5985 DSAStack->addDSA(VD, DE, OMPC_reduction);
5986 Vars.push_back(DE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005987 LHSs.push_back(LHSDRE);
5988 RHSs.push_back(RHSDRE);
5989 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00005990 }
5991
5992 if (Vars.empty())
5993 return nullptr;
5994
5995 return OMPReductionClause::Create(
5996 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005997 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, LHSs,
5998 RHSs, ReductionOps);
Alexey Bataevc5e02582014-06-16 07:08:35 +00005999}
6000
Alexander Musman8dba6642014-04-22 13:09:42 +00006001OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step,
6002 SourceLocation StartLoc,
6003 SourceLocation LParenLoc,
6004 SourceLocation ColonLoc,
6005 SourceLocation EndLoc) {
6006 SmallVector<Expr *, 8> Vars;
Alexander Musman3276a272015-03-21 10:12:56 +00006007 SmallVector<Expr *, 8> Inits;
Alexey Bataeved09d242014-05-28 05:53:51 +00006008 for (auto &RefExpr : VarList) {
6009 assert(RefExpr && "NULL expr in OpenMP linear clause.");
6010 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00006011 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006012 Vars.push_back(RefExpr);
Alexander Musman3276a272015-03-21 10:12:56 +00006013 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00006014 continue;
6015 }
6016
6017 // OpenMP [2.14.3.7, linear clause]
6018 // A list item that appears in a linear clause is subject to the private
6019 // clause semantics described in Section 2.14.3.3 on page 159 except as
6020 // noted. In addition, the value of the new list item on each iteration
6021 // of the associated loop(s) corresponds to the value of the original
6022 // list item before entering the construct plus the logical number of
6023 // the iteration times linear-step.
6024
Alexey Bataeved09d242014-05-28 05:53:51 +00006025 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00006026 // OpenMP [2.1, C/C++]
6027 // A list item is a variable name.
6028 // OpenMP [2.14.3.3, Restrictions, p.1]
6029 // A variable that is part of another variable (as an array or
6030 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00006031 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00006032 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006033 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00006034 continue;
6035 }
6036
6037 VarDecl *VD = cast<VarDecl>(DE->getDecl());
6038
6039 // OpenMP [2.14.3.7, linear clause]
6040 // A list-item cannot appear in more than one linear clause.
6041 // A list-item that appears in a linear clause cannot appear in any
6042 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006043 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00006044 if (DVar.RefExpr) {
6045 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
6046 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006047 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00006048 continue;
6049 }
6050
6051 QualType QType = VD->getType();
6052 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
6053 // It will be analyzed later.
6054 Vars.push_back(DE);
Alexander Musman3276a272015-03-21 10:12:56 +00006055 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00006056 continue;
6057 }
6058
6059 // A variable must not have an incomplete type or a reference type.
6060 if (RequireCompleteType(ELoc, QType,
6061 diag::err_omp_linear_incomplete_type)) {
6062 continue;
6063 }
6064 if (QType->isReferenceType()) {
6065 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
6066 << getOpenMPClauseName(OMPC_linear) << QType;
6067 bool IsDecl =
6068 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6069 Diag(VD->getLocation(),
6070 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6071 << VD;
6072 continue;
6073 }
6074
6075 // A list item must not be const-qualified.
6076 if (QType.isConstant(Context)) {
6077 Diag(ELoc, diag::err_omp_const_variable)
6078 << getOpenMPClauseName(OMPC_linear);
6079 bool IsDecl =
6080 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6081 Diag(VD->getLocation(),
6082 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6083 << VD;
6084 continue;
6085 }
6086
6087 // A list item must be of integral or pointer type.
6088 QType = QType.getUnqualifiedType().getCanonicalType();
6089 const Type *Ty = QType.getTypePtrOrNull();
6090 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
6091 !Ty->isPointerType())) {
6092 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
6093 bool IsDecl =
6094 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6095 Diag(VD->getLocation(),
6096 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6097 << VD;
6098 continue;
6099 }
6100
Alexander Musman3276a272015-03-21 10:12:56 +00006101 // Build var to save initial value.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006102 VarDecl *Init = buildVarDecl(*this, ELoc, QType, ".linear.start");
Alexander Musman3276a272015-03-21 10:12:56 +00006103 AddInitializerToDecl(Init, DefaultLvalueConversion(DE).get(),
6104 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006105 auto InitRef = buildDeclRefExpr(
6106 *this, Init, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman8dba6642014-04-22 13:09:42 +00006107 DSAStack->addDSA(VD, DE, OMPC_linear);
6108 Vars.push_back(DE);
Alexander Musman3276a272015-03-21 10:12:56 +00006109 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00006110 }
6111
6112 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006113 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00006114
6115 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00006116 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00006117 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
6118 !Step->isInstantiationDependent() &&
6119 !Step->containsUnexpandedParameterPack()) {
6120 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006121 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00006122 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006123 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006124 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00006125
Alexander Musman3276a272015-03-21 10:12:56 +00006126 // Build var to save the step value.
6127 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006128 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00006129 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006130 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00006131 ExprResult CalcStep =
6132 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
6133
Alexander Musman8dba6642014-04-22 13:09:42 +00006134 // Warn about zero linear step (it would be probably better specified as
6135 // making corresponding variables 'const').
6136 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00006137 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
6138 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00006139 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
6140 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00006141 if (!IsConstant && CalcStep.isUsable()) {
6142 // Calculate the step beforehand instead of doing this on each iteration.
6143 // (This is not used if the number of iterations may be kfold-ed).
6144 CalcStepExpr = CalcStep.get();
6145 }
Alexander Musman8dba6642014-04-22 13:09:42 +00006146 }
6147
6148 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc,
Alexander Musman3276a272015-03-21 10:12:56 +00006149 Vars, Inits, StepExpr, CalcStepExpr);
6150}
6151
6152static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
6153 Expr *NumIterations, Sema &SemaRef,
6154 Scope *S) {
6155 // Walk the vars and build update/final expressions for the CodeGen.
6156 SmallVector<Expr *, 8> Updates;
6157 SmallVector<Expr *, 8> Finals;
6158 Expr *Step = Clause.getStep();
6159 Expr *CalcStep = Clause.getCalcStep();
6160 // OpenMP [2.14.3.7, linear clause]
6161 // If linear-step is not specified it is assumed to be 1.
6162 if (Step == nullptr)
6163 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
6164 else if (CalcStep)
6165 Step = cast<BinaryOperator>(CalcStep)->getLHS();
6166 bool HasErrors = false;
6167 auto CurInit = Clause.inits().begin();
6168 for (auto &RefExpr : Clause.varlists()) {
6169 Expr *InitExpr = *CurInit;
6170
6171 // Build privatized reference to the current linear var.
6172 auto DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00006173 auto PrivateRef =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006174 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
6175 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
6176 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00006177
6178 // Build update: Var = InitExpr + IV * Step
6179 ExprResult Update =
6180 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), PrivateRef,
6181 InitExpr, IV, Step, /* Subtract */ false);
6182 Update = SemaRef.ActOnFinishFullExpr(Update.get());
6183
6184 // Build final: Var = InitExpr + NumIterations * Step
6185 ExprResult Final =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006186 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), PrivateRef,
6187 InitExpr, NumIterations, Step, /* Subtract */ false);
Alexander Musman3276a272015-03-21 10:12:56 +00006188 Final = SemaRef.ActOnFinishFullExpr(Final.get());
6189 if (!Update.isUsable() || !Final.isUsable()) {
6190 Updates.push_back(nullptr);
6191 Finals.push_back(nullptr);
6192 HasErrors = true;
6193 } else {
6194 Updates.push_back(Update.get());
6195 Finals.push_back(Final.get());
6196 }
6197 ++CurInit;
6198 }
6199 Clause.setUpdates(Updates);
6200 Clause.setFinals(Finals);
6201 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00006202}
6203
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006204OMPClause *Sema::ActOnOpenMPAlignedClause(
6205 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
6206 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
6207
6208 SmallVector<Expr *, 8> Vars;
6209 for (auto &RefExpr : VarList) {
6210 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
6211 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6212 // It will be analyzed later.
6213 Vars.push_back(RefExpr);
6214 continue;
6215 }
6216
6217 SourceLocation ELoc = RefExpr->getExprLoc();
6218 // OpenMP [2.1, C/C++]
6219 // A list item is a variable name.
6220 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
6221 if (!DE || !isa<VarDecl>(DE->getDecl())) {
6222 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
6223 continue;
6224 }
6225
6226 VarDecl *VD = cast<VarDecl>(DE->getDecl());
6227
6228 // OpenMP [2.8.1, simd construct, Restrictions]
6229 // The type of list items appearing in the aligned clause must be
6230 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006231 QualType QType = VD->getType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006232 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006233 const Type *Ty = QType.getTypePtrOrNull();
6234 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
6235 !Ty->isPointerType())) {
6236 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
6237 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
6238 bool IsDecl =
6239 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6240 Diag(VD->getLocation(),
6241 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6242 << VD;
6243 continue;
6244 }
6245
6246 // OpenMP [2.8.1, simd construct, Restrictions]
6247 // A list-item cannot appear in more than one aligned clause.
6248 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
6249 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
6250 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
6251 << getOpenMPClauseName(OMPC_aligned);
6252 continue;
6253 }
6254
6255 Vars.push_back(DE);
6256 }
6257
6258 // OpenMP [2.8.1, simd construct, Description]
6259 // The parameter of the aligned clause, alignment, must be a constant
6260 // positive integer expression.
6261 // If no optional parameter is specified, implementation-defined default
6262 // alignments for SIMD instructions on the target platforms are assumed.
6263 if (Alignment != nullptr) {
6264 ExprResult AlignResult =
6265 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
6266 if (AlignResult.isInvalid())
6267 return nullptr;
6268 Alignment = AlignResult.get();
6269 }
6270 if (Vars.empty())
6271 return nullptr;
6272
6273 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
6274 EndLoc, Vars, Alignment);
6275}
6276
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006277OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
6278 SourceLocation StartLoc,
6279 SourceLocation LParenLoc,
6280 SourceLocation EndLoc) {
6281 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006282 SmallVector<Expr *, 8> SrcExprs;
6283 SmallVector<Expr *, 8> DstExprs;
6284 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00006285 for (auto &RefExpr : VarList) {
6286 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
6287 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006288 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006289 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006290 SrcExprs.push_back(nullptr);
6291 DstExprs.push_back(nullptr);
6292 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006293 continue;
6294 }
6295
Alexey Bataeved09d242014-05-28 05:53:51 +00006296 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006297 // OpenMP [2.1, C/C++]
6298 // A list item is a variable name.
6299 // OpenMP [2.14.4.1, Restrictions, p.1]
6300 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00006301 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006302 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006303 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006304 continue;
6305 }
6306
6307 Decl *D = DE->getDecl();
6308 VarDecl *VD = cast<VarDecl>(D);
6309
6310 QualType Type = VD->getType();
6311 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6312 // It will be analyzed later.
6313 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006314 SrcExprs.push_back(nullptr);
6315 DstExprs.push_back(nullptr);
6316 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006317 continue;
6318 }
6319
6320 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
6321 // A list item that appears in a copyin clause must be threadprivate.
6322 if (!DSAStack->isThreadPrivate(VD)) {
6323 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00006324 << getOpenMPClauseName(OMPC_copyin)
6325 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006326 continue;
6327 }
6328
6329 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
6330 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00006331 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006332 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006333 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00006334 auto *SrcVD = buildVarDecl(*this, DE->getLocStart(),
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006335 ElemType.getUnqualifiedType(), ".copyin.src");
Alexey Bataev39f915b82015-05-08 10:41:21 +00006336 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006337 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
6338 auto *DstVD =
6339 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst");
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006340 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006341 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006342 // For arrays generate assignment operation for single element and replace
6343 // it by the original array element in CodeGen.
6344 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
6345 PseudoDstExpr, PseudoSrcExpr);
6346 if (AssignmentOp.isInvalid())
6347 continue;
6348 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
6349 /*DiscardedValue=*/true);
6350 if (AssignmentOp.isInvalid())
6351 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006352
6353 DSAStack->addDSA(VD, DE, OMPC_copyin);
6354 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006355 SrcExprs.push_back(PseudoSrcExpr);
6356 DstExprs.push_back(PseudoDstExpr);
6357 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006358 }
6359
Alexey Bataeved09d242014-05-28 05:53:51 +00006360 if (Vars.empty())
6361 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006362
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006363 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
6364 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006365}
6366
Alexey Bataevbae9a792014-06-27 10:37:06 +00006367OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
6368 SourceLocation StartLoc,
6369 SourceLocation LParenLoc,
6370 SourceLocation EndLoc) {
6371 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00006372 SmallVector<Expr *, 8> SrcExprs;
6373 SmallVector<Expr *, 8> DstExprs;
6374 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00006375 for (auto &RefExpr : VarList) {
6376 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
6377 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6378 // It will be analyzed later.
6379 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00006380 SrcExprs.push_back(nullptr);
6381 DstExprs.push_back(nullptr);
6382 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00006383 continue;
6384 }
6385
6386 SourceLocation ELoc = RefExpr->getExprLoc();
6387 // OpenMP [2.1, C/C++]
6388 // A list item is a variable name.
6389 // OpenMP [2.14.4.1, Restrictions, p.1]
6390 // A list item that appears in a copyin clause must be threadprivate.
6391 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
6392 if (!DE || !isa<VarDecl>(DE->getDecl())) {
6393 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
6394 continue;
6395 }
6396
6397 Decl *D = DE->getDecl();
6398 VarDecl *VD = cast<VarDecl>(D);
6399
6400 QualType Type = VD->getType();
6401 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6402 // It will be analyzed later.
6403 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00006404 SrcExprs.push_back(nullptr);
6405 DstExprs.push_back(nullptr);
6406 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00006407 continue;
6408 }
6409
6410 // OpenMP [2.14.4.2, Restrictions, p.2]
6411 // A list item that appears in a copyprivate clause may not appear in a
6412 // private or firstprivate clause on the single construct.
6413 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006414 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00006415 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
6416 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00006417 Diag(ELoc, diag::err_omp_wrong_dsa)
6418 << getOpenMPClauseName(DVar.CKind)
6419 << getOpenMPClauseName(OMPC_copyprivate);
6420 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6421 continue;
6422 }
6423
6424 // OpenMP [2.11.4.2, Restrictions, p.1]
6425 // All list items that appear in a copyprivate clause must be either
6426 // threadprivate or private in the enclosing context.
6427 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006428 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00006429 if (DVar.CKind == OMPC_shared) {
6430 Diag(ELoc, diag::err_omp_required_access)
6431 << getOpenMPClauseName(OMPC_copyprivate)
6432 << "threadprivate or private in the enclosing context";
6433 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6434 continue;
6435 }
6436 }
6437 }
6438
Alexey Bataev7a3e5852015-05-19 08:19:24 +00006439 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00006440 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00006441 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006442 << getOpenMPClauseName(OMPC_copyprivate) << Type
6443 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00006444 bool IsDecl =
6445 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6446 Diag(VD->getLocation(),
6447 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6448 << VD;
6449 continue;
6450 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006451
Alexey Bataevbae9a792014-06-27 10:37:06 +00006452 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
6453 // A variable of class type (or array thereof) that appears in a
6454 // copyin clause requires an accessible, unambiguous copy assignment
6455 // operator for the class type.
Alexey Bataev420d45b2015-04-14 05:11:24 +00006456 Type = Context.getBaseElementType(Type).getUnqualifiedType();
6457 auto *SrcVD =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006458 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.src");
Alexey Bataev420d45b2015-04-14 05:11:24 +00006459 auto *PseudoSrcExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006460 buildDeclRefExpr(*this, SrcVD, Type, DE->getExprLoc());
Alexey Bataev420d45b2015-04-14 05:11:24 +00006461 auto *DstVD =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006462 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.dst");
Alexey Bataev420d45b2015-04-14 05:11:24 +00006463 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006464 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataeva63048e2015-03-23 06:18:07 +00006465 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
6466 PseudoDstExpr, PseudoSrcExpr);
6467 if (AssignmentOp.isInvalid())
6468 continue;
6469 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
6470 /*DiscardedValue=*/true);
6471 if (AssignmentOp.isInvalid())
6472 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00006473
6474 // No need to mark vars as copyprivate, they are already threadprivate or
6475 // implicitly private.
6476 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00006477 SrcExprs.push_back(PseudoSrcExpr);
6478 DstExprs.push_back(PseudoDstExpr);
6479 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +00006480 }
6481
6482 if (Vars.empty())
6483 return nullptr;
6484
Alexey Bataeva63048e2015-03-23 06:18:07 +00006485 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
6486 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00006487}
6488
Alexey Bataev6125da92014-07-21 11:26:11 +00006489OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
6490 SourceLocation StartLoc,
6491 SourceLocation LParenLoc,
6492 SourceLocation EndLoc) {
6493 if (VarList.empty())
6494 return nullptr;
6495
6496 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
6497}
Alexey Bataevdea47612014-07-23 07:46:59 +00006498
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006499OMPClause *
6500Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
6501 SourceLocation DepLoc, SourceLocation ColonLoc,
6502 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
6503 SourceLocation LParenLoc, SourceLocation EndLoc) {
6504 if (DepKind == OMPC_DEPEND_unknown) {
6505 std::string Values;
6506 std::string Sep(", ");
6507 for (unsigned i = 0; i < OMPC_DEPEND_unknown; ++i) {
6508 Values += "'";
6509 Values += getOpenMPSimpleClauseTypeName(OMPC_depend, i);
6510 Values += "'";
6511 switch (i) {
6512 case OMPC_DEPEND_unknown - 2:
6513 Values += " or ";
6514 break;
6515 case OMPC_DEPEND_unknown - 1:
6516 break;
6517 default:
6518 Values += Sep;
6519 break;
6520 }
6521 }
6522 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
6523 << Values << getOpenMPClauseName(OMPC_depend);
6524 return nullptr;
6525 }
6526 SmallVector<Expr *, 8> Vars;
6527 for (auto &RefExpr : VarList) {
6528 assert(RefExpr && "NULL expr in OpenMP shared clause.");
6529 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6530 // It will be analyzed later.
6531 Vars.push_back(RefExpr);
6532 continue;
6533 }
6534
6535 SourceLocation ELoc = RefExpr->getExprLoc();
6536 // OpenMP [2.11.1.1, Restrictions, p.3]
6537 // A variable that is part of another variable (such as a field of a
6538 // structure) but is not an array element or an array section cannot appear
6539 // in a depend clause.
6540 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
6541 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
6542 ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
6543 if (!RefExpr->IgnoreParenImpCasts()->isLValue() || (!ASE && !DE) ||
6544 (DE && !isa<VarDecl>(DE->getDecl())) ||
6545 (ASE && !ASE->getBase()->getType()->isAnyPointerType() &&
6546 !ASE->getBase()->getType()->isArrayType())) {
6547 Diag(ELoc, diag::err_omp_expected_var_name_or_array_item)
6548 << RefExpr->getSourceRange();
6549 continue;
6550 }
6551
6552 Vars.push_back(RefExpr->IgnoreParenImpCasts());
6553 }
6554
6555 if (Vars.empty())
6556 return nullptr;
6557
6558 return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
6559 DepLoc, ColonLoc, Vars);
6560}
6561