blob: 4030d9e66e02185e2a339eaaa70a0cecbcb967c7 [file] [log] [blame]
Alexey Bataev9959db52014-05-06 10:08:46 +00001//===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===//
Alexey Bataeva769e072013-03-22 06:34:35 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10/// \brief This file implements semantic analysis for OpenMP directives and
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000011/// clauses.
Alexey Bataeva769e072013-03-22 06:34:35 +000012///
13//===----------------------------------------------------------------------===//
14
Alexey Bataev9959db52014-05-06 10:08:46 +000015#include "clang/AST/ASTContext.h"
Alexey Bataev97720002014-11-11 04:05:39 +000016#include "clang/AST/ASTMutationListener.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000017#include "clang/AST/Decl.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000018#include "clang/AST/DeclCXX.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000019#include "clang/AST/DeclOpenMP.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000020#include "clang/AST/StmtCXX.h"
21#include "clang/AST/StmtOpenMP.h"
22#include "clang/AST/StmtVisitor.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000023#include "clang/Basic/OpenMPKinds.h"
Samuel Antaof8b50122015-07-13 22:54:53 +000024#include "clang/Basic/TargetInfo.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000025#include "clang/Lex/Preprocessor.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000026#include "clang/Sema/Initialization.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000027#include "clang/Sema/Lookup.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000028#include "clang/Sema/Scope.h"
29#include "clang/Sema/ScopeInfo.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000030#include "clang/Sema/SemaInternal.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000031using namespace clang;
32
Alexey Bataev758e55e2013-09-06 18:03:48 +000033//===----------------------------------------------------------------------===//
34// Stack of data-sharing attributes for variables
35//===----------------------------------------------------------------------===//
36
37namespace {
38/// \brief Default data sharing attributes, which can be applied to directive.
39enum DefaultDataSharingAttributes {
Alexey Bataeved09d242014-05-28 05:53:51 +000040 DSA_unspecified = 0, /// \brief Data sharing attribute not specified.
41 DSA_none = 1 << 0, /// \brief Default data sharing attribute 'none'.
42 DSA_shared = 1 << 1 /// \brief Default data sharing attribute 'shared'.
Alexey Bataev758e55e2013-09-06 18:03:48 +000043};
Alexey Bataev7ff55242014-06-19 09:13:45 +000044
Alexey Bataevf29276e2014-06-18 04:14:57 +000045template <class T> struct MatchesAny {
Alexey Bataev23b69422014-06-18 07:08:49 +000046 explicit MatchesAny(ArrayRef<T> Arr) : Arr(std::move(Arr)) {}
Alexey Bataevf29276e2014-06-18 04:14:57 +000047 bool operator()(T Kind) {
48 for (auto KindEl : Arr)
49 if (KindEl == Kind)
50 return true;
51 return false;
52 }
53
54private:
55 ArrayRef<T> Arr;
56};
Alexey Bataev23b69422014-06-18 07:08:49 +000057struct MatchesAlways {
Alexey Bataevf29276e2014-06-18 04:14:57 +000058 MatchesAlways() {}
Alexey Bataev7ff55242014-06-19 09:13:45 +000059 template <class T> bool operator()(T) { return true; }
Alexey Bataevf29276e2014-06-18 04:14:57 +000060};
61
62typedef MatchesAny<OpenMPClauseKind> MatchesAnyClause;
63typedef MatchesAny<OpenMPDirectiveKind> MatchesAnyDirective;
Alexey Bataev758e55e2013-09-06 18:03:48 +000064
65/// \brief Stack for tracking declarations used in OpenMP directives and
66/// clauses and their data-sharing attributes.
67class DSAStackTy {
68public:
69 struct DSAVarData {
70 OpenMPDirectiveKind DKind;
71 OpenMPClauseKind CKind;
72 DeclRefExpr *RefExpr;
Alexey Bataevbae9a792014-06-27 10:37:06 +000073 SourceLocation ImplicitDSALoc;
74 DSAVarData()
75 : DKind(OMPD_unknown), CKind(OMPC_unknown), RefExpr(nullptr),
76 ImplicitDSALoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000077 };
Alexey Bataeved09d242014-05-28 05:53:51 +000078
Alexey Bataev758e55e2013-09-06 18:03:48 +000079private:
80 struct DSAInfo {
81 OpenMPClauseKind Attributes;
82 DeclRefExpr *RefExpr;
83 };
84 typedef llvm::SmallDenseMap<VarDecl *, DSAInfo, 64> DeclSAMapTy;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000085 typedef llvm::SmallDenseMap<VarDecl *, DeclRefExpr *, 64> AlignedMapTy;
Alexey Bataev9c821032015-04-30 04:23:23 +000086 typedef llvm::DenseSet<VarDecl *> LoopControlVariablesSetTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +000087
88 struct SharingMapTy {
89 DeclSAMapTy SharingMap;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000090 AlignedMapTy AlignedMap;
Alexey Bataev9c821032015-04-30 04:23:23 +000091 LoopControlVariablesSetTy LCVSet;
Alexey Bataev758e55e2013-09-06 18:03:48 +000092 DefaultDataSharingAttributes DefaultAttr;
Alexey Bataevbae9a792014-06-27 10:37:06 +000093 SourceLocation DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +000094 OpenMPDirectiveKind Directive;
95 DeclarationNameInfo DirectiveName;
96 Scope *CurScope;
Alexey Bataevbae9a792014-06-27 10:37:06 +000097 SourceLocation ConstructLoc;
Alexey Bataev9fb6e642014-07-22 06:45:04 +000098 bool OrderedRegion;
Alexey Bataev6d4ed052015-07-01 06:57:41 +000099 bool NowaitRegion;
Alexey Bataev9c821032015-04-30 04:23:23 +0000100 unsigned CollapseNumber;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000101 SourceLocation InnerTeamsRegionLoc;
Alexey Bataeved09d242014-05-28 05:53:51 +0000102 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000103 Scope *CurScope, SourceLocation Loc)
Alexey Bataev9c821032015-04-30 04:23:23 +0000104 : SharingMap(), AlignedMap(), LCVSet(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000105 Directive(DKind), DirectiveName(std::move(Name)), CurScope(CurScope),
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000106 ConstructLoc(Loc), OrderedRegion(false), NowaitRegion(false),
107 CollapseNumber(1), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000108 SharingMapTy()
Alexey Bataev9c821032015-04-30 04:23:23 +0000109 : SharingMap(), AlignedMap(), LCVSet(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000110 Directive(OMPD_unknown), DirectiveName(), CurScope(nullptr),
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000111 ConstructLoc(), OrderedRegion(false), NowaitRegion(false),
112 CollapseNumber(1), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000113 };
114
115 typedef SmallVector<SharingMapTy, 64> StackTy;
116
117 /// \brief Stack of used declaration and their data-sharing attributes.
118 StackTy Stack;
Alexey Bataev39f915b82015-05-08 10:41:21 +0000119 /// \brief true, if check for DSA must be from parent directive, false, if
120 /// from current directive.
Alexey Bataevaac108a2015-06-23 04:51:00 +0000121 OpenMPClauseKind ClauseKindMode;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000122 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000123
124 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
125
126 DSAVarData getDSA(StackTy::reverse_iterator Iter, VarDecl *D);
Alexey Bataevec3da872014-01-31 05:15:34 +0000127
128 /// \brief Checks if the variable is a local for OpenMP region.
129 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
Alexey Bataeved09d242014-05-28 05:53:51 +0000130
Alexey Bataev758e55e2013-09-06 18:03:48 +0000131public:
Alexey Bataevaac108a2015-06-23 04:51:00 +0000132 explicit DSAStackTy(Sema &S)
133 : Stack(1), ClauseKindMode(OMPC_unknown), SemaRef(S) {}
Alexey Bataev39f915b82015-05-08 10:41:21 +0000134
Alexey Bataevaac108a2015-06-23 04:51:00 +0000135 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
136 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000137
138 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000139 Scope *CurScope, SourceLocation Loc) {
140 Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc));
141 Stack.back().DefaultAttrLoc = Loc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000142 }
143
144 void pop() {
145 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
146 Stack.pop_back();
147 }
148
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000149 /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000150 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000151 /// for diagnostics.
152 DeclRefExpr *addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE);
153
Alexey Bataev9c821032015-04-30 04:23:23 +0000154 /// \brief Register specified variable as loop control variable.
155 void addLoopControlVariable(VarDecl *D);
156 /// \brief Check if the specified variable is a loop control variable for
157 /// current region.
158 bool isLoopControlVariable(VarDecl *D);
159
Alexey Bataev758e55e2013-09-06 18:03:48 +0000160 /// \brief Adds explicit data sharing attribute to the specified declaration.
161 void addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A);
162
Alexey Bataev758e55e2013-09-06 18:03:48 +0000163 /// \brief Returns data sharing attributes from top of the stack for the
164 /// specified declaration.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000165 DSAVarData getTopDSA(VarDecl *D, bool FromParent);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000166 /// \brief Returns data-sharing attributes for the specified declaration.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000167 DSAVarData getImplicitDSA(VarDecl *D, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000168 /// \brief Checks if the specified variables has data-sharing attributes which
169 /// match specified \a CPred predicate in any directive which matches \a DPred
170 /// predicate.
171 template <class ClausesPredicate, class DirectivesPredicate>
172 DSAVarData hasDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000173 DirectivesPredicate DPred, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000174 /// \brief Checks if the specified variables has data-sharing attributes which
175 /// match specified \a CPred predicate in any innermost directive which
176 /// matches \a DPred predicate.
177 template <class ClausesPredicate, class DirectivesPredicate>
178 DSAVarData hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000179 DirectivesPredicate DPred,
180 bool FromParent);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000181 /// \brief Checks if the specified variables has explicit data-sharing
182 /// attributes which match specified \a CPred predicate at the specified
183 /// OpenMP region.
184 bool hasExplicitDSA(VarDecl *D,
185 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
186 unsigned Level);
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000187 /// \brief Finds a directive which matches specified \a DPred predicate.
188 template <class NamedDirectivesPredicate>
189 bool hasDirective(NamedDirectivesPredicate DPred, bool FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000190
Alexey Bataev758e55e2013-09-06 18:03:48 +0000191 /// \brief Returns currently analyzed directive.
192 OpenMPDirectiveKind getCurrentDirective() const {
193 return Stack.back().Directive;
194 }
Alexey Bataev549210e2014-06-24 04:39:47 +0000195 /// \brief Returns parent directive.
196 OpenMPDirectiveKind getParentDirective() const {
197 if (Stack.size() > 2)
198 return Stack[Stack.size() - 2].Directive;
199 return OMPD_unknown;
200 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000201
202 /// \brief Set default data sharing attribute to none.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000203 void setDefaultDSANone(SourceLocation Loc) {
204 Stack.back().DefaultAttr = DSA_none;
205 Stack.back().DefaultAttrLoc = Loc;
206 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000207 /// \brief Set default data sharing attribute to shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000208 void setDefaultDSAShared(SourceLocation Loc) {
209 Stack.back().DefaultAttr = DSA_shared;
210 Stack.back().DefaultAttrLoc = Loc;
211 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000212
213 DefaultDataSharingAttributes getDefaultDSA() const {
214 return Stack.back().DefaultAttr;
215 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000216 SourceLocation getDefaultDSALocation() const {
217 return Stack.back().DefaultAttrLoc;
218 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000219
Alexey Bataevf29276e2014-06-18 04:14:57 +0000220 /// \brief Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000221 bool isThreadPrivate(VarDecl *D) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000222 DSAVarData DVar = getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000223 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000224 }
225
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000226 /// \brief Marks current region as ordered (it has an 'ordered' clause).
227 void setOrderedRegion(bool IsOrdered = true) {
228 Stack.back().OrderedRegion = IsOrdered;
229 }
230 /// \brief Returns true, if parent region is ordered (has associated
231 /// 'ordered' clause), false - otherwise.
232 bool isParentOrderedRegion() const {
233 if (Stack.size() > 2)
234 return Stack[Stack.size() - 2].OrderedRegion;
235 return false;
236 }
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000237 /// \brief Marks current region as nowait (it has a 'nowait' clause).
238 void setNowaitRegion(bool IsNowait = true) {
239 Stack.back().NowaitRegion = IsNowait;
240 }
241 /// \brief Returns true, if parent region is nowait (has associated
242 /// 'nowait' clause), false - otherwise.
243 bool isParentNowaitRegion() const {
244 if (Stack.size() > 2)
245 return Stack[Stack.size() - 2].NowaitRegion;
246 return false;
247 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000248
Alexey Bataev9c821032015-04-30 04:23:23 +0000249 /// \brief Set collapse value for the region.
250 void setCollapseNumber(unsigned Val) { Stack.back().CollapseNumber = Val; }
251 /// \brief Return collapse value for region.
252 unsigned getCollapseNumber() const {
253 return Stack.back().CollapseNumber;
254 }
255
Alexey Bataev13314bf2014-10-09 04:18:56 +0000256 /// \brief Marks current target region as one with closely nested teams
257 /// region.
258 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
259 if (Stack.size() > 2)
260 Stack[Stack.size() - 2].InnerTeamsRegionLoc = TeamsRegionLoc;
261 }
262 /// \brief Returns true, if current region has closely nested teams region.
263 bool hasInnerTeamsRegion() const {
264 return getInnerTeamsRegionLoc().isValid();
265 }
266 /// \brief Returns location of the nested teams region (if any).
267 SourceLocation getInnerTeamsRegionLoc() const {
268 if (Stack.size() > 1)
269 return Stack.back().InnerTeamsRegionLoc;
270 return SourceLocation();
271 }
272
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000273 Scope *getCurScope() const { return Stack.back().CurScope; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000274 Scope *getCurScope() { return Stack.back().CurScope; }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000275 SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000276};
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000277bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
278 return isOpenMPParallelDirective(DKind) || DKind == OMPD_task ||
Alexey Bataev13314bf2014-10-09 04:18:56 +0000279 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000280}
Alexey Bataeved09d242014-05-28 05:53:51 +0000281} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000282
283DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter,
284 VarDecl *D) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000285 D = D->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000286 DSAVarData DVar;
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000287 if (Iter == std::prev(Stack.rend())) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000288 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
289 // in a region but not in construct]
290 // File-scope or namespace-scope variables referenced in called routines
291 // in the region are shared unless they appear in a threadprivate
292 // directive.
Alexey Bataev8b9cb982014-07-24 02:33:58 +0000293 if (!D->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D))
Alexey Bataev750a58b2014-03-18 12:19:12 +0000294 DVar.CKind = OMPC_shared;
295
296 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
297 // in a region but not in construct]
298 // Variables with static storage duration that are declared in called
299 // routines in the region are shared.
300 if (D->hasGlobalStorage())
301 DVar.CKind = OMPC_shared;
302
Alexey Bataev758e55e2013-09-06 18:03:48 +0000303 return DVar;
304 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000305
Alexey Bataev758e55e2013-09-06 18:03:48 +0000306 DVar.DKind = Iter->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +0000307 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
308 // in a Construct, C/C++, predetermined, p.1]
309 // Variables with automatic storage duration that are declared in a scope
310 // inside the construct are private.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000311 if (isOpenMPLocal(D, Iter) && D->isLocalVarDecl() &&
312 (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) {
313 DVar.CKind = OMPC_private;
314 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000315 }
316
Alexey Bataev758e55e2013-09-06 18:03:48 +0000317 // Explicitly specified attributes and local variables with predetermined
318 // attributes.
319 if (Iter->SharingMap.count(D)) {
320 DVar.RefExpr = Iter->SharingMap[D].RefExpr;
321 DVar.CKind = Iter->SharingMap[D].Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000322 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000323 return DVar;
324 }
325
326 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
327 // in a Construct, C/C++, implicitly determined, p.1]
328 // In a parallel or task construct, the data-sharing attributes of these
329 // variables are determined by the default clause, if present.
330 switch (Iter->DefaultAttr) {
331 case DSA_shared:
332 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000333 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000334 return DVar;
335 case DSA_none:
336 return DVar;
337 case DSA_unspecified:
338 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
339 // in a Construct, implicitly determined, p.2]
340 // In a parallel construct, if no default clause is present, these
341 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000342 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000343 if (isOpenMPParallelDirective(DVar.DKind) ||
344 isOpenMPTeamsDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000345 DVar.CKind = OMPC_shared;
346 return DVar;
347 }
348
349 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
350 // in a Construct, implicitly determined, p.4]
351 // In a task construct, if no default clause is present, a variable that in
352 // the enclosing context is determined to be shared by all implicit tasks
353 // bound to the current team is shared.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000354 if (DVar.DKind == OMPD_task) {
355 DSAVarData DVarTemp;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000356 for (StackTy::reverse_iterator I = std::next(Iter), EE = Stack.rend();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000357 I != EE; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000358 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
359 // Referenced
Alexey Bataev758e55e2013-09-06 18:03:48 +0000360 // in a Construct, implicitly determined, p.6]
361 // In a task construct, if no default clause is present, a variable
362 // whose data-sharing attribute is not determined by the rules above is
363 // firstprivate.
364 DVarTemp = getDSA(I, D);
365 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000366 DVar.RefExpr = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000367 DVar.DKind = OMPD_task;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000368 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000369 return DVar;
370 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000371 if (isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000372 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000373 }
374 DVar.DKind = OMPD_task;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000375 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000376 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000377 return DVar;
378 }
379 }
380 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
381 // in a Construct, implicitly determined, p.3]
382 // For constructs other than task, if no default clause is present, these
383 // variables inherit their data-sharing attributes from the enclosing
384 // context.
Benjamin Kramer167e9992014-03-02 12:20:24 +0000385 return getDSA(std::next(Iter), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000386}
387
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000388DeclRefExpr *DSAStackTy::addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE) {
389 assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000390 D = D->getCanonicalDecl();
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000391 auto It = Stack.back().AlignedMap.find(D);
392 if (It == Stack.back().AlignedMap.end()) {
393 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
394 Stack.back().AlignedMap[D] = NewDE;
395 return nullptr;
396 } else {
397 assert(It->second && "Unexpected nullptr expr in the aligned map");
398 return It->second;
399 }
400 return nullptr;
401}
402
Alexey Bataev9c821032015-04-30 04:23:23 +0000403void DSAStackTy::addLoopControlVariable(VarDecl *D) {
404 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
405 D = D->getCanonicalDecl();
406 Stack.back().LCVSet.insert(D);
407}
408
409bool DSAStackTy::isLoopControlVariable(VarDecl *D) {
410 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
411 D = D->getCanonicalDecl();
412 return Stack.back().LCVSet.count(D) > 0;
413}
414
Alexey Bataev758e55e2013-09-06 18:03:48 +0000415void DSAStackTy::addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000416 D = D->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000417 if (A == OMPC_threadprivate) {
418 Stack[0].SharingMap[D].Attributes = A;
419 Stack[0].SharingMap[D].RefExpr = E;
420 } else {
421 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
422 Stack.back().SharingMap[D].Attributes = A;
423 Stack.back().SharingMap[D].RefExpr = E;
424 }
425}
426
Alexey Bataeved09d242014-05-28 05:53:51 +0000427bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000428 D = D->getCanonicalDecl();
Alexey Bataevec3da872014-01-31 05:15:34 +0000429 if (Stack.size() > 2) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000430 reverse_iterator I = Iter, E = std::prev(Stack.rend());
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000431 Scope *TopScope = nullptr;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000432 while (I != E && !isParallelOrTaskRegion(I->Directive)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000433 ++I;
434 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000435 if (I == E)
436 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000437 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000438 Scope *CurScope = getCurScope();
439 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000440 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000441 }
442 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000443 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000444 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000445}
446
Alexey Bataev39f915b82015-05-08 10:41:21 +0000447/// \brief Build a variable declaration for OpenMP loop iteration variable.
448static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
449 StringRef Name) {
450 DeclContext *DC = SemaRef.CurContext;
451 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
452 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
453 VarDecl *Decl =
454 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
455 Decl->setImplicit();
456 return Decl;
457}
458
459static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
460 SourceLocation Loc,
461 bool RefersToCapture = false) {
462 D->setReferenced();
463 D->markUsed(S.Context);
464 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
465 SourceLocation(), D, RefersToCapture, Loc, Ty,
466 VK_LValue);
467}
468
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000469DSAStackTy::DSAVarData DSAStackTy::getTopDSA(VarDecl *D, bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000470 D = D->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000471 DSAVarData DVar;
472
473 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
474 // in a Construct, C/C++, predetermined, p.1]
475 // Variables appearing in threadprivate directives are threadprivate.
Samuel Antaof8b50122015-07-13 22:54:53 +0000476 if ((D->getTLSKind() != VarDecl::TLS_None &&
477 !(D->hasAttr<OMPThreadPrivateDeclAttr>() &&
478 SemaRef.getLangOpts().OpenMPUseTLS &&
479 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +0000480 (D->getStorageClass() == SC_Register && D->hasAttr<AsmLabelAttr>() &&
481 !D->isLocalVarDecl())) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000482 addDSA(D, buildDeclRefExpr(SemaRef, D, D->getType().getNonReferenceType(),
483 D->getLocation()),
Alexey Bataevf2453a02015-05-06 07:25:08 +0000484 OMPC_threadprivate);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000485 }
486 if (Stack[0].SharingMap.count(D)) {
487 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
488 DVar.CKind = OMPC_threadprivate;
489 return DVar;
490 }
491
492 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
493 // in a Construct, C/C++, predetermined, p.1]
494 // Variables with automatic storage duration that are declared in a scope
495 // inside the construct are private.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000496 OpenMPDirectiveKind Kind =
497 FromParent ? getParentDirective() : getCurrentDirective();
498 auto StartI = std::next(Stack.rbegin());
499 auto EndI = std::prev(Stack.rend());
500 if (FromParent && StartI != EndI) {
501 StartI = std::next(StartI);
502 }
503 if (!isParallelOrTaskRegion(Kind)) {
Alexey Bataev8b9cb982014-07-24 02:33:58 +0000504 if (isOpenMPLocal(D, StartI) &&
505 ((D->isLocalVarDecl() && (D->getStorageClass() == SC_Auto ||
506 D->getStorageClass() == SC_None)) ||
507 isa<ParmVarDecl>(D))) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000508 DVar.CKind = OMPC_private;
509 return DVar;
Alexander Musman8dba6642014-04-22 13:09:42 +0000510 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000511
Alexey Bataev24b04aa2015-01-16 07:11:33 +0000512 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
513 // in a Construct, C/C++, predetermined, p.4]
514 // Static data members are shared.
Alexey Bataev24b04aa2015-01-16 07:11:33 +0000515 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
516 // in a Construct, C/C++, predetermined, p.7]
517 // Variables with static storage duration that are declared in a scope
518 // inside the construct are shared.
Alexey Bataev42971a32015-01-20 07:03:46 +0000519 if (D->isStaticDataMember() || D->isStaticLocal()) {
520 DSAVarData DVarTemp =
521 hasDSA(D, isOpenMPPrivate, MatchesAlways(), FromParent);
522 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
523 return DVar;
524
Alexey Bataev24b04aa2015-01-16 07:11:33 +0000525 DVar.CKind = OMPC_shared;
526 return DVar;
527 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000528 }
529
530 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +0000531 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
532 Type = SemaRef.getASTContext().getBaseElementType(Type);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000533 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
534 // in a Construct, C/C++, predetermined, p.6]
535 // Variables with const qualified type having no mutable member are
536 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000537 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000538 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000539 if (IsConstant &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000540 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000541 // Variables with const-qualified type having no mutable member may be
542 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000543 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
544 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000545 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
546 return DVar;
547
Alexey Bataev758e55e2013-09-06 18:03:48 +0000548 DVar.CKind = OMPC_shared;
549 return DVar;
550 }
551
Alexey Bataev758e55e2013-09-06 18:03:48 +0000552 // Explicitly specified attributes and local variables with predetermined
553 // attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000554 auto I = std::prev(StartI);
555 if (I->SharingMap.count(D)) {
556 DVar.RefExpr = I->SharingMap[D].RefExpr;
557 DVar.CKind = I->SharingMap[D].Attributes;
558 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000559 }
560
561 return DVar;
562}
563
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000564DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D, bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000565 D = D->getCanonicalDecl();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000566 auto StartI = Stack.rbegin();
567 auto EndI = std::prev(Stack.rend());
568 if (FromParent && StartI != EndI) {
569 StartI = std::next(StartI);
570 }
571 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000572}
573
Alexey Bataevf29276e2014-06-18 04:14:57 +0000574template <class ClausesPredicate, class DirectivesPredicate>
575DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000576 DirectivesPredicate DPred,
577 bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000578 D = D->getCanonicalDecl();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000579 auto StartI = std::next(Stack.rbegin());
580 auto EndI = std::prev(Stack.rend());
581 if (FromParent && StartI != EndI) {
582 StartI = std::next(StartI);
583 }
584 for (auto I = StartI, EE = EndI; I != EE; ++I) {
585 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000586 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000587 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000588 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000589 return DVar;
590 }
591 return DSAVarData();
592}
593
Alexey Bataevf29276e2014-06-18 04:14:57 +0000594template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000595DSAStackTy::DSAVarData
596DSAStackTy::hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
597 DirectivesPredicate DPred, bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000598 D = D->getCanonicalDecl();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000599 auto StartI = std::next(Stack.rbegin());
600 auto EndI = std::prev(Stack.rend());
601 if (FromParent && StartI != EndI) {
602 StartI = std::next(StartI);
603 }
604 for (auto I = StartI, EE = EndI; I != EE; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000605 if (!DPred(I->Directive))
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000606 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000607 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000608 if (CPred(DVar.CKind))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000609 return DVar;
610 return DSAVarData();
611 }
612 return DSAVarData();
613}
614
Alexey Bataevaac108a2015-06-23 04:51:00 +0000615bool DSAStackTy::hasExplicitDSA(
616 VarDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
617 unsigned Level) {
618 if (CPred(ClauseKindMode))
619 return true;
620 if (isClauseParsingMode())
621 ++Level;
622 D = D->getCanonicalDecl();
623 auto StartI = Stack.rbegin();
624 auto EndI = std::prev(Stack.rend());
NAKAMURA Takumi0332eda2015-06-23 10:01:20 +0000625 if (std::distance(StartI, EndI) <= (int)Level)
Alexey Bataevaac108a2015-06-23 04:51:00 +0000626 return false;
627 std::advance(StartI, Level);
628 return (StartI->SharingMap.count(D) > 0) && StartI->SharingMap[D].RefExpr &&
629 CPred(StartI->SharingMap[D].Attributes);
630}
631
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000632template <class NamedDirectivesPredicate>
633bool DSAStackTy::hasDirective(NamedDirectivesPredicate DPred, bool FromParent) {
634 auto StartI = std::next(Stack.rbegin());
635 auto EndI = std::prev(Stack.rend());
636 if (FromParent && StartI != EndI) {
637 StartI = std::next(StartI);
638 }
639 for (auto I = StartI, EE = EndI; I != EE; ++I) {
640 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
641 return true;
642 }
643 return false;
644}
645
Alexey Bataev758e55e2013-09-06 18:03:48 +0000646void Sema::InitDataSharingAttributesStack() {
647 VarDataSharingAttributesStack = new DSAStackTy(*this);
648}
649
650#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
651
Alexey Bataevf841bd92014-12-16 07:00:22 +0000652bool Sema::IsOpenMPCapturedVar(VarDecl *VD) {
653 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000654 VD = VD->getCanonicalDecl();
Alexey Bataevf841bd92014-12-16 07:00:22 +0000655 if (DSAStack->getCurrentDirective() != OMPD_unknown) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000656 if (DSAStack->isLoopControlVariable(VD) ||
657 (VD->hasLocalStorage() &&
658 isParallelOrTaskRegion(DSAStack->getCurrentDirective())))
Alexey Bataev9c821032015-04-30 04:23:23 +0000659 return true;
Alexey Bataevaac108a2015-06-23 04:51:00 +0000660 auto DVarPrivate = DSAStack->getTopDSA(VD, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000661 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
662 return true;
663 DVarPrivate = DSAStack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(),
Alexey Bataevaac108a2015-06-23 04:51:00 +0000664 DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000665 return DVarPrivate.CKind != OMPC_unknown;
666 }
667 return false;
668}
669
Alexey Bataevaac108a2015-06-23 04:51:00 +0000670bool Sema::isOpenMPPrivateVar(VarDecl *VD, unsigned Level) {
671 assert(LangOpts.OpenMP && "OpenMP is not allowed");
672 return DSAStack->hasExplicitDSA(
673 VD, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level);
674}
675
Alexey Bataeved09d242014-05-28 05:53:51 +0000676void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000677
678void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
679 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000680 Scope *CurScope, SourceLocation Loc) {
681 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000682 PushExpressionEvaluationContext(PotentiallyEvaluated);
683}
684
Alexey Bataevaac108a2015-06-23 04:51:00 +0000685void Sema::StartOpenMPClause(OpenMPClauseKind K) {
686 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000687}
688
Alexey Bataevaac108a2015-06-23 04:51:00 +0000689void Sema::EndOpenMPClause() {
690 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000691}
692
Alexey Bataev758e55e2013-09-06 18:03:48 +0000693void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000694 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
695 // A variable of class type (or array thereof) that appears in a lastprivate
696 // clause requires an accessible, unambiguous default constructor for the
697 // class type, unless the list item is also specified in a firstprivate
698 // clause.
699 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000700 for (auto *C : D->clauses()) {
701 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
702 SmallVector<Expr *, 8> PrivateCopies;
703 for (auto *DE : Clause->varlists()) {
704 if (DE->isValueDependent() || DE->isTypeDependent()) {
705 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000706 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +0000707 }
708 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(DE)->getDecl());
Alexey Bataevf120c0d2015-05-19 07:46:42 +0000709 QualType Type = VD->getType();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000710 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000711 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000712 // Generate helper private variable and initialize it with the
713 // default value. The address of the original variable is replaced
714 // by the address of the new private variable in CodeGen. This new
715 // variable is not added to IdResolver, so the code in the OpenMP
716 // region uses original variable for proper diagnostics.
Alexey Bataev39f915b82015-05-08 10:41:21 +0000717 auto *VDPrivate =
Alexey Bataevf120c0d2015-05-19 07:46:42 +0000718 buildVarDecl(*this, DE->getExprLoc(), Type.getUnqualifiedType(),
719 VD->getName());
Alexey Bataev38e89532015-04-16 04:54:05 +0000720 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
721 if (VDPrivate->isInvalidDecl())
722 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +0000723 PrivateCopies.push_back(buildDeclRefExpr(
724 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +0000725 } else {
726 // The variable is also a firstprivate, so initialization sequence
727 // for private copy is generated already.
728 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000729 }
730 }
Alexey Bataev38e89532015-04-16 04:54:05 +0000731 // Set initializers to private copies if no errors were found.
732 if (PrivateCopies.size() == Clause->varlist_size()) {
733 Clause->setPrivateCopies(PrivateCopies);
734 }
Alexey Bataevf29276e2014-06-18 04:14:57 +0000735 }
736 }
737 }
738
Alexey Bataev758e55e2013-09-06 18:03:48 +0000739 DSAStack->pop();
740 DiscardCleanupsInEvaluationContext();
741 PopExpressionEvaluationContext();
742}
743
Alexander Musman3276a272015-03-21 10:12:56 +0000744static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
745 Expr *NumIterations, Sema &SemaRef,
746 Scope *S);
747
Alexey Bataeva769e072013-03-22 06:34:35 +0000748namespace {
749
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000750class VarDeclFilterCCC : public CorrectionCandidateCallback {
751private:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000752 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +0000753
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000754public:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000755 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +0000756 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000757 NamedDecl *ND = Candidate.getCorrectionDecl();
758 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
759 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000760 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
761 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +0000762 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000763 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000764 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000765};
Alexey Bataeved09d242014-05-28 05:53:51 +0000766} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000767
768ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
769 CXXScopeSpec &ScopeSpec,
770 const DeclarationNameInfo &Id) {
771 LookupResult Lookup(*this, Id, LookupOrdinaryName);
772 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
773
774 if (Lookup.isAmbiguous())
775 return ExprError();
776
777 VarDecl *VD;
778 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +0000779 if (TypoCorrection Corrected = CorrectTypo(
780 Id, LookupOrdinaryName, CurScope, nullptr,
781 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +0000782 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000783 PDiag(Lookup.empty()
784 ? diag::err_undeclared_var_use_suggest
785 : diag::err_omp_expected_var_arg_suggest)
786 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +0000787 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000788 } else {
Richard Smithf9b15102013-08-17 00:46:16 +0000789 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
790 : diag::err_omp_expected_var_arg)
791 << Id.getName();
792 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000793 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000794 } else {
795 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000796 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000797 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
798 return ExprError();
799 }
800 }
801 Lookup.suppressDiagnostics();
802
803 // OpenMP [2.9.2, Syntax, C/C++]
804 // Variables must be file-scope, namespace-scope, or static block-scope.
805 if (!VD->hasGlobalStorage()) {
806 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000807 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
808 bool IsDecl =
809 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000810 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +0000811 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
812 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000813 return ExprError();
814 }
815
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000816 VarDecl *CanonicalVD = VD->getCanonicalDecl();
817 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000818 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
819 // A threadprivate directive for file-scope variables must appear outside
820 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000821 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
822 !getCurLexicalContext()->isTranslationUnit()) {
823 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000824 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
825 bool IsDecl =
826 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
827 Diag(VD->getLocation(),
828 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
829 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000830 return ExprError();
831 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000832 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
833 // A threadprivate directive for static class member variables must appear
834 // in the class definition, in the same scope in which the member
835 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000836 if (CanonicalVD->isStaticDataMember() &&
837 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
838 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000839 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
840 bool IsDecl =
841 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
842 Diag(VD->getLocation(),
843 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
844 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000845 return ExprError();
846 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000847 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
848 // A threadprivate directive for namespace-scope variables must appear
849 // outside any definition or declaration other than the namespace
850 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000851 if (CanonicalVD->getDeclContext()->isNamespace() &&
852 (!getCurLexicalContext()->isFileContext() ||
853 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
854 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000855 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
856 bool IsDecl =
857 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
858 Diag(VD->getLocation(),
859 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
860 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000861 return ExprError();
862 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000863 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
864 // A threadprivate directive for static block-scope variables must appear
865 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +0000866 if (CanonicalVD->isStaticLocal() && CurScope &&
867 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000868 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +0000869 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
870 bool IsDecl =
871 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
872 Diag(VD->getLocation(),
873 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
874 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000875 return ExprError();
876 }
877
878 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
879 // A threadprivate directive must lexically precede all references to any
880 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000881 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000882 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +0000883 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000884 return ExprError();
885 }
886
887 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +0000888 ExprResult DE = buildDeclRefExpr(*this, VD, ExprType, Id.getLoc());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000889 return DE;
890}
891
Alexey Bataeved09d242014-05-28 05:53:51 +0000892Sema::DeclGroupPtrTy
893Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
894 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000895 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000896 CurContext->addDecl(D);
897 return DeclGroupPtrTy::make(DeclGroupRef(D));
898 }
899 return DeclGroupPtrTy();
900}
901
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000902namespace {
903class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
904 Sema &SemaRef;
905
906public:
907 bool VisitDeclRefExpr(const DeclRefExpr *E) {
908 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
909 if (VD->hasLocalStorage()) {
910 SemaRef.Diag(E->getLocStart(),
911 diag::err_omp_local_var_in_threadprivate_init)
912 << E->getSourceRange();
913 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
914 << VD << VD->getSourceRange();
915 return true;
916 }
917 }
918 return false;
919 }
920 bool VisitStmt(const Stmt *S) {
921 for (auto Child : S->children()) {
922 if (Child && Visit(Child))
923 return true;
924 }
925 return false;
926 }
Alexey Bataev23b69422014-06-18 07:08:49 +0000927 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000928};
929} // namespace
930
Alexey Bataeved09d242014-05-28 05:53:51 +0000931OMPThreadPrivateDecl *
932Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000933 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +0000934 for (auto &RefExpr : VarList) {
935 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000936 VarDecl *VD = cast<VarDecl>(DE->getDecl());
937 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +0000938
Alexey Bataevf56f98c2015-04-16 05:39:01 +0000939 QualType QType = VD->getType();
940 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
941 // It will be analyzed later.
942 Vars.push_back(DE);
943 continue;
944 }
945
Alexey Bataeva769e072013-03-22 06:34:35 +0000946 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
947 // A threadprivate variable must not have an incomplete type.
948 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000949 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +0000950 continue;
951 }
952
953 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
954 // A threadprivate variable must not have a reference type.
955 if (VD->getType()->isReferenceType()) {
956 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +0000957 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
958 bool IsDecl =
959 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
960 Diag(VD->getLocation(),
961 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
962 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000963 continue;
964 }
965
Samuel Antaof8b50122015-07-13 22:54:53 +0000966 // Check if this is a TLS variable. If TLS is not being supported, produce
967 // the corresponding diagnostic.
968 if ((VD->getTLSKind() != VarDecl::TLS_None &&
969 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
970 getLangOpts().OpenMPUseTLS &&
971 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +0000972 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
973 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +0000974 Diag(ILoc, diag::err_omp_var_thread_local)
975 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +0000976 bool IsDecl =
977 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
978 Diag(VD->getLocation(),
979 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
980 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +0000981 continue;
982 }
983
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000984 // Check if initial value of threadprivate variable reference variable with
985 // local storage (it is not supported by runtime).
986 if (auto Init = VD->getAnyInitializer()) {
987 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000988 if (Checker.Visit(Init))
989 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +0000990 }
991
Alexey Bataeved09d242014-05-28 05:53:51 +0000992 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +0000993 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +0000994 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
995 Context, SourceRange(Loc, Loc)));
996 if (auto *ML = Context.getASTMutationListener())
997 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +0000998 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000999 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001000 if (!Vars.empty()) {
1001 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1002 Vars);
1003 D->setAccess(AS_public);
1004 }
1005 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00001006}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001007
Alexey Bataev7ff55242014-06-19 09:13:45 +00001008static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
1009 const VarDecl *VD, DSAStackTy::DSAVarData DVar,
1010 bool IsLoopIterVar = false) {
1011 if (DVar.RefExpr) {
1012 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1013 << getOpenMPClauseName(DVar.CKind);
1014 return;
1015 }
1016 enum {
1017 PDSA_StaticMemberShared,
1018 PDSA_StaticLocalVarShared,
1019 PDSA_LoopIterVarPrivate,
1020 PDSA_LoopIterVarLinear,
1021 PDSA_LoopIterVarLastprivate,
1022 PDSA_ConstVarShared,
1023 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001024 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001025 PDSA_LocalVarPrivate,
1026 PDSA_Implicit
1027 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001028 bool ReportHint = false;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001029 auto ReportLoc = VD->getLocation();
Alexey Bataev7ff55242014-06-19 09:13:45 +00001030 if (IsLoopIterVar) {
1031 if (DVar.CKind == OMPC_private)
1032 Reason = PDSA_LoopIterVarPrivate;
1033 else if (DVar.CKind == OMPC_lastprivate)
1034 Reason = PDSA_LoopIterVarLastprivate;
1035 else
1036 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001037 } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) {
1038 Reason = PDSA_TaskVarFirstprivate;
1039 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001040 } else if (VD->isStaticLocal())
1041 Reason = PDSA_StaticLocalVarShared;
1042 else if (VD->isStaticDataMember())
1043 Reason = PDSA_StaticMemberShared;
1044 else if (VD->isFileVarDecl())
1045 Reason = PDSA_GlobalVarShared;
1046 else if (VD->getType().isConstant(SemaRef.getASTContext()))
1047 Reason = PDSA_ConstVarShared;
Alexey Bataevbae9a792014-06-27 10:37:06 +00001048 else if (VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00001049 ReportHint = true;
1050 Reason = PDSA_LocalVarPrivate;
1051 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00001052 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001053 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00001054 << Reason << ReportHint
1055 << getOpenMPDirectiveName(Stack->getCurrentDirective());
1056 } else if (DVar.ImplicitDSALoc.isValid()) {
1057 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1058 << getOpenMPClauseName(DVar.CKind);
1059 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00001060}
1061
Alexey Bataev758e55e2013-09-06 18:03:48 +00001062namespace {
1063class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1064 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001065 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001066 bool ErrorFound;
1067 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001068 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev4acb8592014-07-07 13:01:15 +00001069 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +00001070
Alexey Bataev758e55e2013-09-06 18:03:48 +00001071public:
1072 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001073 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001074 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +00001075 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1076 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001077
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001078 auto DVar = Stack->getTopDSA(VD, false);
1079 // Check if the variable has explicit DSA set and stop analysis if it so.
1080 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001081
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001082 auto ELoc = E->getExprLoc();
1083 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001084 // The default(none) clause requires that each variable that is referenced
1085 // in the construct, and does not have a predetermined data-sharing
1086 // attribute, must have its data-sharing attribute explicitly determined
1087 // by being listed in a data-sharing attribute clause.
1088 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001089 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001090 VarsWithInheritedDSA.count(VD) == 0) {
1091 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001092 return;
1093 }
1094
1095 // OpenMP [2.9.3.6, Restrictions, p.2]
1096 // A list item that appears in a reduction clause of the innermost
1097 // enclosing worksharing or parallel construct may not be accessed in an
1098 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +00001099 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001100 [](OpenMPDirectiveKind K) -> bool {
1101 return isOpenMPParallelDirective(K) ||
Alexey Bataev13314bf2014-10-09 04:18:56 +00001102 isOpenMPWorksharingDirective(K) ||
1103 isOpenMPTeamsDirective(K);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001104 },
1105 false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001106 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
1107 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001108 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1109 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001110 return;
1111 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001112
1113 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001114 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001115 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001116 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001117 }
1118 }
1119 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001120 for (auto *C : S->clauses()) {
1121 // Skip analysis of arguments of implicitly defined firstprivate clause
1122 // for task directives.
1123 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1124 for (auto *CC : C->children()) {
1125 if (CC)
1126 Visit(CC);
1127 }
1128 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001129 }
1130 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001131 for (auto *C : S->children()) {
1132 if (C && !isa<OMPExecutableDirective>(C))
1133 Visit(C);
1134 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001135 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001136
1137 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001138 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001139 llvm::DenseMap<VarDecl *, Expr *> &getVarsWithInheritedDSA() {
1140 return VarsWithInheritedDSA;
1141 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001142
Alexey Bataev7ff55242014-06-19 09:13:45 +00001143 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1144 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00001145};
Alexey Bataeved09d242014-05-28 05:53:51 +00001146} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001147
Alexey Bataevbae9a792014-06-27 10:37:06 +00001148void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001149 switch (DKind) {
1150 case OMPD_parallel: {
1151 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1152 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001153 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001154 std::make_pair(".global_tid.", KmpInt32PtrTy),
1155 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1156 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001157 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001158 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1159 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001160 break;
1161 }
1162 case OMPD_simd: {
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
1165 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001166 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1167 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001168 break;
1169 }
1170 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001171 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001172 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001173 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001174 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1175 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001176 break;
1177 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001178 case OMPD_for_simd: {
1179 Sema::CapturedParamNameType Params[] = {
1180 std::make_pair(StringRef(), QualType()) // __context with shared vars
1181 };
1182 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1183 Params);
1184 break;
1185 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001186 case OMPD_sections: {
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 Bataevd3f8dd22014-06-25 11:44:49 +00001192 break;
1193 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001194 case OMPD_section: {
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 Bataev1e0498a2014-06-26 08:21:58 +00001200 break;
1201 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001202 case OMPD_single: {
1203 Sema::CapturedParamNameType Params[] = {
1204 std::make_pair(StringRef(), QualType()) // __context with shared vars
1205 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001206 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1207 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001208 break;
1209 }
Alexander Musman80c22892014-07-17 08:54:58 +00001210 case OMPD_master: {
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 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001218 case OMPD_critical: {
1219 Sema::CapturedParamNameType Params[] = {
1220 std::make_pair(StringRef(), QualType()) // __context with shared vars
1221 };
1222 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1223 Params);
1224 break;
1225 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001226 case OMPD_parallel_for: {
1227 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1228 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1229 Sema::CapturedParamNameType Params[] = {
1230 std::make_pair(".global_tid.", KmpInt32PtrTy),
1231 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1232 std::make_pair(StringRef(), QualType()) // __context with shared vars
1233 };
1234 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1235 Params);
1236 break;
1237 }
Alexander Musmane4e893b2014-09-23 09:33:00 +00001238 case OMPD_parallel_for_simd: {
1239 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1240 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1241 Sema::CapturedParamNameType Params[] = {
1242 std::make_pair(".global_tid.", KmpInt32PtrTy),
1243 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1244 std::make_pair(StringRef(), QualType()) // __context with shared vars
1245 };
1246 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1247 Params);
1248 break;
1249 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001250 case OMPD_parallel_sections: {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001251 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1252 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001253 Sema::CapturedParamNameType Params[] = {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001254 std::make_pair(".global_tid.", KmpInt32PtrTy),
1255 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001256 std::make_pair(StringRef(), QualType()) // __context with shared vars
1257 };
1258 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1259 Params);
1260 break;
1261 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001262 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001263 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001264 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1265 FunctionProtoType::ExtProtoInfo EPI;
1266 EPI.Variadic = true;
1267 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001268 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001269 std::make_pair(".global_tid.", KmpInt32Ty),
1270 std::make_pair(".part_id.", KmpInt32Ty),
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001271 std::make_pair(".privates.",
1272 Context.VoidPtrTy.withConst().withRestrict()),
1273 std::make_pair(
1274 ".copy_fn.",
1275 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001276 std::make_pair(StringRef(), QualType()) // __context with shared vars
1277 };
1278 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1279 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001280 // Mark this captured region as inlined, because we don't use outlined
1281 // function directly.
1282 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1283 AlwaysInlineAttr::CreateImplicit(
1284 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001285 break;
1286 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001287 case OMPD_ordered: {
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 Bataev0162e452014-07-22 10:10:35 +00001295 case OMPD_atomic: {
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 Bataev0bd520b2014-09-19 08:19:49 +00001303 case OMPD_target: {
1304 Sema::CapturedParamNameType Params[] = {
1305 std::make_pair(StringRef(), QualType()) // __context with shared vars
1306 };
1307 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1308 Params);
1309 break;
1310 }
Alexey Bataev13314bf2014-10-09 04:18:56 +00001311 case OMPD_teams: {
1312 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1313 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1314 Sema::CapturedParamNameType Params[] = {
1315 std::make_pair(".global_tid.", KmpInt32PtrTy),
1316 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1317 std::make_pair(StringRef(), QualType()) // __context with shared vars
1318 };
1319 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1320 Params);
1321 break;
1322 }
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001323 case OMPD_taskgroup: {
1324 Sema::CapturedParamNameType Params[] = {
1325 std::make_pair(StringRef(), QualType()) // __context with shared vars
1326 };
1327 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1328 Params);
1329 break;
1330 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001331 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001332 case OMPD_taskyield:
1333 case OMPD_barrier:
1334 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001335 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001336 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00001337 case OMPD_flush:
Alexey Bataev9959db52014-05-06 10:08:46 +00001338 llvm_unreachable("OpenMP Directive is not allowed");
1339 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001340 llvm_unreachable("Unknown OpenMP directive");
1341 }
1342}
1343
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001344StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1345 ArrayRef<OMPClause *> Clauses) {
1346 if (!S.isUsable()) {
1347 ActOnCapturedRegionError();
1348 return StmtError();
1349 }
Alexey Bataev040d5402015-05-12 08:35:28 +00001350 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001351 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001352 if (isOpenMPPrivate(Clause->getClauseKind()) ||
1353 Clause->getClauseKind() == OMPC_copyprivate) {
Alexey Bataev040d5402015-05-12 08:35:28 +00001354 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001355 for (auto *VarRef : Clause->children()) {
1356 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001357 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001358 }
1359 }
Alexey Bataev040d5402015-05-12 08:35:28 +00001360 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
1361 Clause->getClauseKind() == OMPC_schedule) {
1362 // Mark all variables in private list clauses as used in inner region.
1363 // Required for proper codegen of combined directives.
1364 // TODO: add processing for other clauses.
1365 if (auto *E = cast_or_null<Expr>(
1366 cast<OMPScheduleClause>(Clause)->getHelperChunkSize())) {
1367 MarkDeclarationsReferencedInExpr(E);
1368 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001369 }
1370 }
1371 return ActOnCapturedRegionEnd(S.get());
1372}
1373
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001374static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1375 OpenMPDirectiveKind CurrentRegion,
1376 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001377 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001378 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001379 // Allowed nesting of constructs
1380 // +------------------+-----------------+------------------------------------+
1381 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1382 // +------------------+-----------------+------------------------------------+
1383 // | parallel | parallel | * |
1384 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001385 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001386 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001387 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001388 // | parallel | simd | * |
1389 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001390 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001391 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001392 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001393 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001394 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001395 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001396 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001397 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001398 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001399 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001400 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001401 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001402 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001403 // | parallel | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001404 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001405 // | parallel | cancellation | |
1406 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001407 // | parallel | cancel | ! |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001408 // +------------------+-----------------+------------------------------------+
1409 // | for | parallel | * |
1410 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001411 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001412 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001413 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001414 // | for | simd | * |
1415 // | for | sections | + |
1416 // | for | section | + |
1417 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001418 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001419 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001420 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001421 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001422 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001423 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001424 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001425 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001426 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001427 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001428 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001429 // | for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001430 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001431 // | for | cancellation | |
1432 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001433 // | for | cancel | ! |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001434 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001435 // | master | parallel | * |
1436 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001437 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001438 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001439 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001440 // | master | simd | * |
1441 // | master | sections | + |
1442 // | master | section | + |
1443 // | master | single | + |
1444 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001445 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001446 // | master |parallel sections| * |
1447 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001448 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001449 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001450 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001451 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001452 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001453 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001454 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001455 // | master | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001456 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001457 // | master | cancellation | |
1458 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001459 // | master | cancel | |
Alexander Musman80c22892014-07-17 08:54:58 +00001460 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001461 // | critical | parallel | * |
1462 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001463 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001464 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001465 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001466 // | critical | simd | * |
1467 // | critical | sections | + |
1468 // | critical | section | + |
1469 // | critical | single | + |
1470 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001471 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001472 // | critical |parallel sections| * |
1473 // | critical | task | * |
1474 // | critical | taskyield | * |
1475 // | critical | barrier | + |
1476 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001477 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001478 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001479 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001480 // | critical | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001481 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001482 // | critical | cancellation | |
1483 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001484 // | critical | cancel | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001485 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001486 // | simd | parallel | |
1487 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001488 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00001489 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001490 // | simd | critical | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001491 // | simd | simd | |
1492 // | simd | sections | |
1493 // | simd | section | |
1494 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001495 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001496 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001497 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001498 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00001499 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001500 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001501 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001502 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00001503 // | simd | flush | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001504 // | simd | ordered | |
Alexey Bataev0162e452014-07-22 10:10:35 +00001505 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001506 // | simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001507 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001508 // | simd | cancellation | |
1509 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001510 // | simd | cancel | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001511 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00001512 // | for simd | parallel | |
1513 // | for simd | for | |
1514 // | for simd | for simd | |
1515 // | for simd | master | |
1516 // | for simd | critical | |
1517 // | for simd | simd | |
1518 // | for simd | sections | |
1519 // | for simd | section | |
1520 // | for simd | single | |
1521 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001522 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001523 // | for simd |parallel sections| |
1524 // | for simd | task | |
1525 // | for simd | taskyield | |
1526 // | for simd | barrier | |
1527 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001528 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001529 // | for simd | flush | |
1530 // | for simd | ordered | |
1531 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001532 // | for simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001533 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001534 // | for simd | cancellation | |
1535 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001536 // | for simd | cancel | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001537 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00001538 // | parallel for simd| parallel | |
1539 // | parallel for simd| for | |
1540 // | parallel for simd| for simd | |
1541 // | parallel for simd| master | |
1542 // | parallel for simd| critical | |
1543 // | parallel for simd| simd | |
1544 // | parallel for simd| sections | |
1545 // | parallel for simd| section | |
1546 // | parallel for simd| single | |
1547 // | parallel for simd| parallel for | |
1548 // | parallel for simd|parallel for simd| |
1549 // | parallel for simd|parallel sections| |
1550 // | parallel for simd| task | |
1551 // | parallel for simd| taskyield | |
1552 // | parallel for simd| barrier | |
1553 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001554 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001555 // | parallel for simd| flush | |
1556 // | parallel for simd| ordered | |
1557 // | parallel for simd| atomic | |
1558 // | parallel for simd| target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001559 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001560 // | parallel for simd| cancellation | |
1561 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001562 // | parallel for simd| cancel | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001563 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001564 // | sections | parallel | * |
1565 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001566 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001567 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001568 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001569 // | sections | simd | * |
1570 // | sections | sections | + |
1571 // | sections | section | * |
1572 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001573 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001574 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001575 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001576 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001577 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001578 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001579 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001580 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001581 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001582 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001583 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001584 // | sections | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001585 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001586 // | sections | cancellation | |
1587 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001588 // | sections | cancel | ! |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001589 // +------------------+-----------------+------------------------------------+
1590 // | section | parallel | * |
1591 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001592 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001593 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001594 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001595 // | section | simd | * |
1596 // | section | sections | + |
1597 // | section | section | + |
1598 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001599 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001600 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001601 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001602 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001603 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001604 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001605 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001606 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001607 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001608 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001609 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001610 // | section | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001611 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001612 // | section | cancellation | |
1613 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001614 // | section | cancel | ! |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001615 // +------------------+-----------------+------------------------------------+
1616 // | single | parallel | * |
1617 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001618 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001619 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001620 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001621 // | single | simd | * |
1622 // | single | sections | + |
1623 // | single | section | + |
1624 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001625 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001626 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001627 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001628 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001629 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001630 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001631 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001632 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001633 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001634 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001635 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001636 // | single | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001637 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001638 // | single | cancellation | |
1639 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001640 // | single | cancel | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001641 // +------------------+-----------------+------------------------------------+
1642 // | parallel for | parallel | * |
1643 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001644 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001645 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001646 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001647 // | parallel for | simd | * |
1648 // | parallel for | sections | + |
1649 // | parallel for | section | + |
1650 // | parallel for | single | + |
1651 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001652 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001653 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001654 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001655 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001656 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001657 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001658 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001659 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001660 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001661 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001662 // | parallel for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001663 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001664 // | parallel for | cancellation | |
1665 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001666 // | parallel for | cancel | ! |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001667 // +------------------+-----------------+------------------------------------+
1668 // | parallel sections| parallel | * |
1669 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001670 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001671 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001672 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001673 // | parallel sections| simd | * |
1674 // | parallel sections| sections | + |
1675 // | parallel sections| section | * |
1676 // | parallel sections| single | + |
1677 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001678 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001679 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001680 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001681 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001682 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001683 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001684 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001685 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001686 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001687 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001688 // | parallel sections| target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001689 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001690 // | parallel sections| cancellation | |
1691 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001692 // | parallel sections| cancel | ! |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001693 // +------------------+-----------------+------------------------------------+
1694 // | task | parallel | * |
1695 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001696 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001697 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001698 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001699 // | task | simd | * |
1700 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001701 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001702 // | task | single | + |
1703 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001704 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001705 // | task |parallel sections| * |
1706 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001707 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001708 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001709 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001710 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001711 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001712 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001713 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001714 // | task | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001715 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001716 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00001717 // | | point | ! |
1718 // | task | cancel | ! |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001719 // +------------------+-----------------+------------------------------------+
1720 // | ordered | parallel | * |
1721 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001722 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001723 // | ordered | master | * |
1724 // | ordered | critical | * |
1725 // | ordered | simd | * |
1726 // | ordered | sections | + |
1727 // | ordered | section | + |
1728 // | ordered | single | + |
1729 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001730 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001731 // | ordered |parallel sections| * |
1732 // | ordered | task | * |
1733 // | ordered | taskyield | * |
1734 // | ordered | barrier | + |
1735 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001736 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001737 // | ordered | flush | * |
1738 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001739 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001740 // | ordered | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001741 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001742 // | ordered | cancellation | |
1743 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001744 // | ordered | cancel | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001745 // +------------------+-----------------+------------------------------------+
1746 // | atomic | parallel | |
1747 // | atomic | for | |
1748 // | atomic | for simd | |
1749 // | atomic | master | |
1750 // | atomic | critical | |
1751 // | atomic | simd | |
1752 // | atomic | sections | |
1753 // | atomic | section | |
1754 // | atomic | single | |
1755 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001756 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001757 // | atomic |parallel sections| |
1758 // | atomic | task | |
1759 // | atomic | taskyield | |
1760 // | atomic | barrier | |
1761 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001762 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001763 // | atomic | flush | |
1764 // | atomic | ordered | |
1765 // | atomic | atomic | |
1766 // | atomic | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001767 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001768 // | atomic | cancellation | |
1769 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001770 // | atomic | cancel | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001771 // +------------------+-----------------+------------------------------------+
1772 // | target | parallel | * |
1773 // | target | for | * |
1774 // | target | for simd | * |
1775 // | target | master | * |
1776 // | target | critical | * |
1777 // | target | simd | * |
1778 // | target | sections | * |
1779 // | target | section | * |
1780 // | target | single | * |
1781 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001782 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001783 // | target |parallel sections| * |
1784 // | target | task | * |
1785 // | target | taskyield | * |
1786 // | target | barrier | * |
1787 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001788 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001789 // | target | flush | * |
1790 // | target | ordered | * |
1791 // | target | atomic | * |
1792 // | target | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001793 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001794 // | target | cancellation | |
1795 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001796 // | target | cancel | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001797 // +------------------+-----------------+------------------------------------+
1798 // | teams | parallel | * |
1799 // | teams | for | + |
1800 // | teams | for simd | + |
1801 // | teams | master | + |
1802 // | teams | critical | + |
1803 // | teams | simd | + |
1804 // | teams | sections | + |
1805 // | teams | section | + |
1806 // | teams | single | + |
1807 // | teams | parallel for | * |
1808 // | teams |parallel for simd| * |
1809 // | teams |parallel sections| * |
1810 // | teams | task | + |
1811 // | teams | taskyield | + |
1812 // | teams | barrier | + |
1813 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00001814 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001815 // | teams | flush | + |
1816 // | teams | ordered | + |
1817 // | teams | atomic | + |
1818 // | teams | target | + |
1819 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001820 // | teams | cancellation | |
1821 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001822 // | teams | cancel | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001823 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00001824 if (Stack->getCurScope()) {
1825 auto ParentRegion = Stack->getParentDirective();
1826 bool NestingProhibited = false;
1827 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001828 enum {
1829 NoRecommend,
1830 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00001831 ShouldBeInOrderedRegion,
1832 ShouldBeInTargetRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001833 } Recommend = NoRecommend;
Alexey Bataev549210e2014-06-24 04:39:47 +00001834 if (isOpenMPSimdDirective(ParentRegion)) {
1835 // OpenMP [2.16, Nesting of Regions]
1836 // OpenMP constructs may not be nested inside a simd region.
1837 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
1838 return true;
1839 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001840 if (ParentRegion == OMPD_atomic) {
1841 // OpenMP [2.16, Nesting of Regions]
1842 // OpenMP constructs may not be nested inside an atomic region.
1843 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
1844 return true;
1845 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001846 if (CurrentRegion == OMPD_section) {
1847 // OpenMP [2.7.2, sections Construct, Restrictions]
1848 // Orphaned section directives are prohibited. That is, the section
1849 // directives must appear within the sections construct and must not be
1850 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001851 if (ParentRegion != OMPD_sections &&
1852 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001853 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
1854 << (ParentRegion != OMPD_unknown)
1855 << getOpenMPDirectiveName(ParentRegion);
1856 return true;
1857 }
1858 return false;
1859 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001860 // Allow some constructs to be orphaned (they could be used in functions,
1861 // called from OpenMP regions with the required preconditions).
1862 if (ParentRegion == OMPD_unknown)
1863 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00001864 if (CurrentRegion == OMPD_cancellation_point ||
1865 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001866 // OpenMP [2.16, Nesting of Regions]
1867 // A cancellation point construct for which construct-type-clause is
1868 // taskgroup must be nested inside a task construct. A cancellation
1869 // point construct for which construct-type-clause is not taskgroup must
1870 // be closely nested inside an OpenMP construct that matches the type
1871 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00001872 // A cancel construct for which construct-type-clause is taskgroup must be
1873 // nested inside a task construct. A cancel construct for which
1874 // construct-type-clause is not taskgroup must be closely nested inside an
1875 // OpenMP construct that matches the type specified in
1876 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001877 NestingProhibited =
1878 !((CancelRegion == OMPD_parallel && ParentRegion == OMPD_parallel) ||
1879 (CancelRegion == OMPD_for && ParentRegion == OMPD_for) ||
1880 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
1881 (CancelRegion == OMPD_sections &&
1882 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections)));
1883 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00001884 // OpenMP [2.16, Nesting of Regions]
1885 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00001886 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00001887 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
1888 ParentRegion == OMPD_task;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001889 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
1890 // OpenMP [2.16, Nesting of Regions]
1891 // A critical region may not be nested (closely or otherwise) inside a
1892 // critical region with the same name. Note that this restriction is not
1893 // sufficient to prevent deadlock.
1894 SourceLocation PreviousCriticalLoc;
1895 bool DeadLock =
1896 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
1897 OpenMPDirectiveKind K,
1898 const DeclarationNameInfo &DNI,
1899 SourceLocation Loc)
1900 ->bool {
1901 if (K == OMPD_critical &&
1902 DNI.getName() == CurrentName.getName()) {
1903 PreviousCriticalLoc = Loc;
1904 return true;
1905 } else
1906 return false;
1907 },
1908 false /* skip top directive */);
1909 if (DeadLock) {
1910 SemaRef.Diag(StartLoc,
1911 diag::err_omp_prohibited_region_critical_same_name)
1912 << CurrentName.getName();
1913 if (PreviousCriticalLoc.isValid())
1914 SemaRef.Diag(PreviousCriticalLoc,
1915 diag::note_omp_previous_critical_region);
1916 return true;
1917 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001918 } else if (CurrentRegion == OMPD_barrier) {
1919 // OpenMP [2.16, Nesting of Regions]
1920 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00001921 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001922 NestingProhibited =
1923 isOpenMPWorksharingDirective(ParentRegion) ||
1924 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1925 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00001926 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00001927 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00001928 // OpenMP [2.16, Nesting of Regions]
1929 // A worksharing region may not be closely nested inside a worksharing,
1930 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001931 NestingProhibited =
Alexander Musmanf82886e2014-09-18 05:12:34 +00001932 isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001933 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1934 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
1935 Recommend = ShouldBeInParallelRegion;
1936 } else if (CurrentRegion == OMPD_ordered) {
1937 // OpenMP [2.16, Nesting of Regions]
1938 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00001939 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001940 // An ordered region must be closely nested inside a loop region (or
1941 // parallel loop region) with an ordered clause.
1942 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00001943 ParentRegion == OMPD_task ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001944 !Stack->isParentOrderedRegion();
1945 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00001946 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
1947 // OpenMP [2.16, Nesting of Regions]
1948 // If specified, a teams construct must be contained within a target
1949 // construct.
1950 NestingProhibited = ParentRegion != OMPD_target;
1951 Recommend = ShouldBeInTargetRegion;
1952 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
1953 }
1954 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
1955 // OpenMP [2.16, Nesting of Regions]
1956 // distribute, parallel, parallel sections, parallel workshare, and the
1957 // parallel loop and parallel loop SIMD constructs are the only OpenMP
1958 // constructs that can be closely nested in the teams region.
1959 // TODO: add distribute directive.
1960 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion);
1961 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00001962 }
1963 if (NestingProhibited) {
1964 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001965 << CloseNesting << getOpenMPDirectiveName(ParentRegion) << Recommend
1966 << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00001967 return true;
1968 }
1969 }
1970 return false;
1971}
1972
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001973StmtResult Sema::ActOnOpenMPExecutableDirective(
1974 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
1975 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
1976 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001977 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001978 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
1979 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00001980 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001981
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001982 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev68446b72014-07-18 07:47:19 +00001983 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001984 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00001985 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00001986 if (AStmt) {
1987 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
1988
1989 // Check default data sharing attributes for referenced variables.
1990 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
1991 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
1992 if (DSAChecker.isErrorFound())
1993 return StmtError();
1994 // Generate list of implicitly defined firstprivate variables.
1995 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00001996
1997 if (!DSAChecker.getImplicitFirstprivate().empty()) {
1998 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
1999 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
2000 SourceLocation(), SourceLocation())) {
2001 ClausesWithImplicit.push_back(Implicit);
2002 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
2003 DSAChecker.getImplicitFirstprivate().size();
2004 } else
2005 ErrorFound = true;
2006 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002007 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002008
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002009 switch (Kind) {
2010 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00002011 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
2012 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002013 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002014 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002015 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2016 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002017 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002018 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002019 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2020 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002021 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00002022 case OMPD_for_simd:
2023 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2024 EndLoc, VarsWithInheritedDSA);
2025 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002026 case OMPD_sections:
2027 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
2028 EndLoc);
2029 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002030 case OMPD_section:
2031 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00002032 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002033 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
2034 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002035 case OMPD_single:
2036 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
2037 EndLoc);
2038 break;
Alexander Musman80c22892014-07-17 08:54:58 +00002039 case OMPD_master:
2040 assert(ClausesWithImplicit.empty() &&
2041 "No clauses are allowed for 'omp master' directive");
2042 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
2043 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002044 case OMPD_critical:
2045 assert(ClausesWithImplicit.empty() &&
2046 "No clauses are allowed for 'omp critical' directive");
2047 Res = ActOnOpenMPCriticalDirective(DirName, AStmt, StartLoc, EndLoc);
2048 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002049 case OMPD_parallel_for:
2050 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
2051 EndLoc, VarsWithInheritedDSA);
2052 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00002053 case OMPD_parallel_for_simd:
2054 Res = ActOnOpenMPParallelForSimdDirective(
2055 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
2056 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002057 case OMPD_parallel_sections:
2058 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
2059 StartLoc, EndLoc);
2060 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002061 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002062 Res =
2063 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
2064 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00002065 case OMPD_taskyield:
2066 assert(ClausesWithImplicit.empty() &&
2067 "No clauses are allowed for 'omp taskyield' directive");
2068 assert(AStmt == nullptr &&
2069 "No associated statement allowed for 'omp taskyield' directive");
2070 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
2071 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002072 case OMPD_barrier:
2073 assert(ClausesWithImplicit.empty() &&
2074 "No clauses are allowed for 'omp barrier' directive");
2075 assert(AStmt == nullptr &&
2076 "No associated statement allowed for 'omp barrier' directive");
2077 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
2078 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00002079 case OMPD_taskwait:
2080 assert(ClausesWithImplicit.empty() &&
2081 "No clauses are allowed for 'omp taskwait' directive");
2082 assert(AStmt == nullptr &&
2083 "No associated statement allowed for 'omp taskwait' directive");
2084 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
2085 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002086 case OMPD_taskgroup:
2087 assert(ClausesWithImplicit.empty() &&
2088 "No clauses are allowed for 'omp taskgroup' directive");
2089 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
2090 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00002091 case OMPD_flush:
2092 assert(AStmt == nullptr &&
2093 "No associated statement allowed for 'omp flush' directive");
2094 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
2095 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002096 case OMPD_ordered:
2097 assert(ClausesWithImplicit.empty() &&
2098 "No clauses are allowed for 'omp ordered' directive");
2099 Res = ActOnOpenMPOrderedDirective(AStmt, StartLoc, EndLoc);
2100 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00002101 case OMPD_atomic:
2102 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
2103 EndLoc);
2104 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002105 case OMPD_teams:
2106 Res =
2107 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
2108 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002109 case OMPD_target:
2110 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
2111 EndLoc);
2112 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002113 case OMPD_cancellation_point:
2114 assert(ClausesWithImplicit.empty() &&
2115 "No clauses are allowed for 'omp cancellation point' directive");
2116 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
2117 "cancellation point' directive");
2118 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
2119 break;
Alexey Bataev80909872015-07-02 11:25:17 +00002120 case OMPD_cancel:
2121 assert(ClausesWithImplicit.empty() &&
2122 "No clauses are allowed for 'omp cancel' directive");
2123 assert(AStmt == nullptr &&
2124 "No associated statement allowed for 'omp cancel' directive");
2125 Res = ActOnOpenMPCancelDirective(StartLoc, EndLoc, CancelRegion);
2126 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002127 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002128 llvm_unreachable("OpenMP Directive is not allowed");
2129 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002130 llvm_unreachable("Unknown OpenMP directive");
2131 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002132
Alexey Bataev4acb8592014-07-07 13:01:15 +00002133 for (auto P : VarsWithInheritedDSA) {
2134 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
2135 << P.first << P.second->getSourceRange();
2136 }
2137 if (!VarsWithInheritedDSA.empty())
2138 return StmtError();
2139
Alexey Bataeved09d242014-05-28 05:53:51 +00002140 if (ErrorFound)
2141 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002142 return Res;
2143}
2144
2145StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
2146 Stmt *AStmt,
2147 SourceLocation StartLoc,
2148 SourceLocation EndLoc) {
Alexey Bataev9959db52014-05-06 10:08:46 +00002149 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2150 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
2151 // 1.2.2 OpenMP Language Terminology
2152 // Structured block - An executable statement with a single entry at the
2153 // top and a single exit at the bottom.
2154 // The point of exit cannot be a branch out of the structured block.
2155 // longjmp() and throw() must not violate the entry/exit criteria.
2156 CS->getCapturedDecl()->setNothrow();
2157
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002158 getCurFunction()->setHasBranchProtectedScope();
2159
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00002160 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
2161 AStmt);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002162}
2163
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002164namespace {
2165/// \brief Helper class for checking canonical form of the OpenMP loops and
2166/// extracting iteration space of each loop in the loop nest, that will be used
2167/// for IR generation.
2168class OpenMPIterationSpaceChecker {
2169 /// \brief Reference to Sema.
2170 Sema &SemaRef;
2171 /// \brief A location for diagnostics (when there is no some better location).
2172 SourceLocation DefaultLoc;
2173 /// \brief A location for diagnostics (when increment is not compatible).
2174 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002175 /// \brief A source location for referring to loop init later.
2176 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002177 /// \brief A source location for referring to condition later.
2178 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002179 /// \brief A source location for referring to increment later.
2180 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002181 /// \brief Loop variable.
2182 VarDecl *Var;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002183 /// \brief Reference to loop variable.
2184 DeclRefExpr *VarRef;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002185 /// \brief Lower bound (initializer for the var).
2186 Expr *LB;
2187 /// \brief Upper bound.
2188 Expr *UB;
2189 /// \brief Loop step (increment).
2190 Expr *Step;
2191 /// \brief This flag is true when condition is one of:
2192 /// Var < UB
2193 /// Var <= UB
2194 /// UB > Var
2195 /// UB >= Var
2196 bool TestIsLessOp;
2197 /// \brief This flag is true when condition is strict ( < or > ).
2198 bool TestIsStrictOp;
2199 /// \brief This flag is true when step is subtracted on each iteration.
2200 bool SubtractStep;
2201
2202public:
2203 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
2204 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
Alexander Musmana5f070a2014-10-01 06:03:56 +00002205 InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()),
2206 IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002207 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
2208 TestIsStrictOp(false), SubtractStep(false) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002209 /// \brief Check init-expr for canonical loop form and save loop counter
2210 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00002211 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002212 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
2213 /// for less/greater and for strict/non-strict comparison.
2214 bool CheckCond(Expr *S);
2215 /// \brief Check incr-expr for canonical loop form and return true if it
2216 /// does not conform, otherwise save loop step (#Step).
2217 bool CheckInc(Expr *S);
2218 /// \brief Return the loop counter variable.
2219 VarDecl *GetLoopVar() const { return Var; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002220 /// \brief Return the reference expression to loop counter variable.
2221 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002222 /// \brief Source range of the loop init.
2223 SourceRange GetInitSrcRange() const { return InitSrcRange; }
2224 /// \brief Source range of the loop condition.
2225 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
2226 /// \brief Source range of the loop increment.
2227 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
2228 /// \brief True if the step should be subtracted.
2229 bool ShouldSubtractStep() const { return SubtractStep; }
2230 /// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00002231 Expr *BuildNumIterations(Scope *S, const bool LimitedType) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00002232 /// \brief Build the precondition expression for the loops.
2233 Expr *BuildPreCond(Scope *S, Expr *Cond) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002234 /// \brief Build reference expression to the counter be used for codegen.
2235 Expr *BuildCounterVar() const;
2236 /// \brief Build initization of the counter be used for codegen.
2237 Expr *BuildCounterInit() const;
2238 /// \brief Build step of the counter be used for codegen.
2239 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002240 /// \brief Return true if any expression is dependent.
2241 bool Dependent() const;
2242
2243private:
2244 /// \brief Check the right-hand side of an assignment in the increment
2245 /// expression.
2246 bool CheckIncRHS(Expr *RHS);
2247 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002248 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002249 /// \brief Helper to set upper bound.
2250 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, const SourceRange &SR,
2251 const SourceLocation &SL);
2252 /// \brief Helper to set loop increment.
2253 bool SetStep(Expr *NewStep, bool Subtract);
2254};
2255
2256bool OpenMPIterationSpaceChecker::Dependent() const {
2257 if (!Var) {
2258 assert(!LB && !UB && !Step);
2259 return false;
2260 }
2261 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
2262 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
2263}
2264
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002265bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
2266 DeclRefExpr *NewVarRefExpr,
2267 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002268 // State consistency checking to ensure correct usage.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002269 assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
2270 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002271 if (!NewVar || !NewLB)
2272 return true;
2273 Var = NewVar;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002274 VarRef = NewVarRefExpr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002275 LB = NewLB;
2276 return false;
2277}
2278
2279bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
2280 const SourceRange &SR,
2281 const SourceLocation &SL) {
2282 // State consistency checking to ensure correct usage.
2283 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
2284 !TestIsLessOp && !TestIsStrictOp);
2285 if (!NewUB)
2286 return true;
2287 UB = NewUB;
2288 TestIsLessOp = LessOp;
2289 TestIsStrictOp = StrictOp;
2290 ConditionSrcRange = SR;
2291 ConditionLoc = SL;
2292 return false;
2293}
2294
2295bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
2296 // State consistency checking to ensure correct usage.
2297 assert(Var != nullptr && LB != nullptr && Step == nullptr);
2298 if (!NewStep)
2299 return true;
2300 if (!NewStep->isValueDependent()) {
2301 // Check that the step is integer expression.
2302 SourceLocation StepLoc = NewStep->getLocStart();
2303 ExprResult Val =
2304 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
2305 if (Val.isInvalid())
2306 return true;
2307 NewStep = Val.get();
2308
2309 // OpenMP [2.6, Canonical Loop Form, Restrictions]
2310 // If test-expr is of form var relational-op b and relational-op is < or
2311 // <= then incr-expr must cause var to increase on each iteration of the
2312 // loop. If test-expr is of form var relational-op b and relational-op is
2313 // > or >= then incr-expr must cause var to decrease on each iteration of
2314 // the loop.
2315 // If test-expr is of form b relational-op var and relational-op is < or
2316 // <= then incr-expr must cause var to decrease on each iteration of the
2317 // loop. If test-expr is of form b relational-op var and relational-op is
2318 // > or >= then incr-expr must cause var to increase on each iteration of
2319 // the loop.
2320 llvm::APSInt Result;
2321 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
2322 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
2323 bool IsConstNeg =
2324 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00002325 bool IsConstPos =
2326 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002327 bool IsConstZero = IsConstant && !Result.getBoolValue();
2328 if (UB && (IsConstZero ||
2329 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00002330 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002331 SemaRef.Diag(NewStep->getExprLoc(),
2332 diag::err_omp_loop_incr_not_compatible)
2333 << Var << TestIsLessOp << NewStep->getSourceRange();
2334 SemaRef.Diag(ConditionLoc,
2335 diag::note_omp_loop_cond_requres_compatible_incr)
2336 << TestIsLessOp << ConditionSrcRange;
2337 return true;
2338 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002339 if (TestIsLessOp == Subtract) {
2340 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
2341 NewStep).get();
2342 Subtract = !Subtract;
2343 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002344 }
2345
2346 Step = NewStep;
2347 SubtractStep = Subtract;
2348 return false;
2349}
2350
Alexey Bataev9c821032015-04-30 04:23:23 +00002351bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002352 // Check init-expr for canonical loop form and save loop counter
2353 // variable - #Var and its initialization value - #LB.
2354 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
2355 // var = lb
2356 // integer-type var = lb
2357 // random-access-iterator-type var = lb
2358 // pointer-type var = lb
2359 //
2360 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00002361 if (EmitDiags) {
2362 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
2363 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002364 return true;
2365 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002366 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002367 if (Expr *E = dyn_cast<Expr>(S))
2368 S = E->IgnoreParens();
2369 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2370 if (BO->getOpcode() == BO_Assign)
2371 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002372 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002373 BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002374 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
2375 if (DS->isSingleDecl()) {
2376 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
2377 if (Var->hasInit()) {
2378 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00002379 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002380 SemaRef.Diag(S->getLocStart(),
2381 diag::ext_omp_loop_not_canonical_init)
2382 << S->getSourceRange();
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002383 return SetVarAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002384 }
2385 }
2386 }
2387 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
2388 if (CE->getOperator() == OO_Equal)
2389 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002390 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
2391 CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002392
Alexey Bataev9c821032015-04-30 04:23:23 +00002393 if (EmitDiags) {
2394 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
2395 << S->getSourceRange();
2396 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002397 return true;
2398}
2399
Alexey Bataev23b69422014-06-18 07:08:49 +00002400/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002401/// variable (which may be the loop variable) if possible.
2402static const VarDecl *GetInitVarDecl(const Expr *E) {
2403 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00002404 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002405 E = E->IgnoreParenImpCasts();
2406 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
2407 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
2408 if (Ctor->isCopyConstructor() && CE->getNumArgs() == 1 &&
2409 CE->getArg(0) != nullptr)
2410 E = CE->getArg(0)->IgnoreParenImpCasts();
2411 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
2412 if (!DRE)
2413 return nullptr;
2414 return dyn_cast<VarDecl>(DRE->getDecl());
2415}
2416
2417bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
2418 // Check test-expr for canonical form, save upper-bound UB, flags for
2419 // less/greater and for strict/non-strict comparison.
2420 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2421 // var relational-op b
2422 // b relational-op var
2423 //
2424 if (!S) {
2425 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
2426 return true;
2427 }
2428 S = S->IgnoreParenImpCasts();
2429 SourceLocation CondLoc = S->getLocStart();
2430 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2431 if (BO->isRelationalOp()) {
2432 if (GetInitVarDecl(BO->getLHS()) == Var)
2433 return SetUB(BO->getRHS(),
2434 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
2435 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2436 BO->getSourceRange(), BO->getOperatorLoc());
2437 if (GetInitVarDecl(BO->getRHS()) == Var)
2438 return SetUB(BO->getLHS(),
2439 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
2440 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2441 BO->getSourceRange(), BO->getOperatorLoc());
2442 }
2443 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2444 if (CE->getNumArgs() == 2) {
2445 auto Op = CE->getOperator();
2446 switch (Op) {
2447 case OO_Greater:
2448 case OO_GreaterEqual:
2449 case OO_Less:
2450 case OO_LessEqual:
2451 if (GetInitVarDecl(CE->getArg(0)) == Var)
2452 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
2453 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2454 CE->getOperatorLoc());
2455 if (GetInitVarDecl(CE->getArg(1)) == Var)
2456 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
2457 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2458 CE->getOperatorLoc());
2459 break;
2460 default:
2461 break;
2462 }
2463 }
2464 }
2465 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
2466 << S->getSourceRange() << Var;
2467 return true;
2468}
2469
2470bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
2471 // RHS of canonical loop form increment can be:
2472 // var + incr
2473 // incr + var
2474 // var - incr
2475 //
2476 RHS = RHS->IgnoreParenImpCasts();
2477 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
2478 if (BO->isAdditiveOp()) {
2479 bool IsAdd = BO->getOpcode() == BO_Add;
2480 if (GetInitVarDecl(BO->getLHS()) == Var)
2481 return SetStep(BO->getRHS(), !IsAdd);
2482 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
2483 return SetStep(BO->getLHS(), false);
2484 }
2485 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
2486 bool IsAdd = CE->getOperator() == OO_Plus;
2487 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
2488 if (GetInitVarDecl(CE->getArg(0)) == Var)
2489 return SetStep(CE->getArg(1), !IsAdd);
2490 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
2491 return SetStep(CE->getArg(0), false);
2492 }
2493 }
2494 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2495 << RHS->getSourceRange() << Var;
2496 return true;
2497}
2498
2499bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
2500 // Check incr-expr for canonical loop form and return true if it
2501 // does not conform.
2502 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2503 // ++var
2504 // var++
2505 // --var
2506 // var--
2507 // var += incr
2508 // var -= incr
2509 // var = var + incr
2510 // var = incr + var
2511 // var = var - incr
2512 //
2513 if (!S) {
2514 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
2515 return true;
2516 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002517 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002518 S = S->IgnoreParens();
2519 if (auto UO = dyn_cast<UnaryOperator>(S)) {
2520 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
2521 return SetStep(
2522 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
2523 (UO->isDecrementOp() ? -1 : 1)).get(),
2524 false);
2525 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
2526 switch (BO->getOpcode()) {
2527 case BO_AddAssign:
2528 case BO_SubAssign:
2529 if (GetInitVarDecl(BO->getLHS()) == Var)
2530 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
2531 break;
2532 case BO_Assign:
2533 if (GetInitVarDecl(BO->getLHS()) == Var)
2534 return CheckIncRHS(BO->getRHS());
2535 break;
2536 default:
2537 break;
2538 }
2539 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2540 switch (CE->getOperator()) {
2541 case OO_PlusPlus:
2542 case OO_MinusMinus:
2543 if (GetInitVarDecl(CE->getArg(0)) == Var)
2544 return SetStep(
2545 SemaRef.ActOnIntegerConstant(
2546 CE->getLocStart(),
2547 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
2548 false);
2549 break;
2550 case OO_PlusEqual:
2551 case OO_MinusEqual:
2552 if (GetInitVarDecl(CE->getArg(0)) == Var)
2553 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
2554 break;
2555 case OO_Equal:
2556 if (GetInitVarDecl(CE->getArg(0)) == Var)
2557 return CheckIncRHS(CE->getArg(1));
2558 break;
2559 default:
2560 break;
2561 }
2562 }
2563 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2564 << S->getSourceRange() << Var;
2565 return true;
2566}
Alexander Musmana5f070a2014-10-01 06:03:56 +00002567
2568/// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00002569Expr *
2570OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S,
2571 const bool LimitedType) const {
Alexander Musmana5f070a2014-10-01 06:03:56 +00002572 ExprResult Diff;
2573 if (Var->getType()->isIntegerType() || Var->getType()->isPointerType() ||
2574 SemaRef.getLangOpts().CPlusPlus) {
2575 // Upper - Lower
2576 Expr *Upper = TestIsLessOp ? UB : LB;
2577 Expr *Lower = TestIsLessOp ? LB : UB;
2578
2579 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
2580
2581 if (!Diff.isUsable() && Var->getType()->getAsCXXRecordDecl()) {
2582 // BuildBinOp already emitted error, this one is to point user to upper
2583 // and lower bound, and to tell what is passed to 'operator-'.
2584 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
2585 << Upper->getSourceRange() << Lower->getSourceRange();
2586 return nullptr;
2587 }
2588 }
2589
2590 if (!Diff.isUsable())
2591 return nullptr;
2592
2593 // Upper - Lower [- 1]
2594 if (TestIsStrictOp)
2595 Diff = SemaRef.BuildBinOp(
2596 S, DefaultLoc, BO_Sub, Diff.get(),
2597 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
2598 if (!Diff.isUsable())
2599 return nullptr;
2600
2601 // Upper - Lower [- 1] + Step
2602 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(),
2603 Step->IgnoreImplicit());
2604 if (!Diff.isUsable())
2605 return nullptr;
2606
2607 // Parentheses (for dumping/debugging purposes only).
2608 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
2609 if (!Diff.isUsable())
2610 return nullptr;
2611
2612 // (Upper - Lower [- 1] + Step) / Step
2613 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(),
2614 Step->IgnoreImplicit());
2615 if (!Diff.isUsable())
2616 return nullptr;
2617
Alexander Musman174b3ca2014-10-06 11:16:29 +00002618 // OpenMP runtime requires 32-bit or 64-bit loop variables.
2619 if (LimitedType) {
2620 auto &C = SemaRef.Context;
2621 QualType Type = Diff.get()->getType();
2622 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
2623 if (NewSize != C.getTypeSize(Type)) {
2624 if (NewSize < C.getTypeSize(Type)) {
2625 assert(NewSize == 64 && "incorrect loop var size");
2626 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
2627 << InitSrcRange << ConditionSrcRange;
2628 }
2629 QualType NewType = C.getIntTypeForBitwidth(
2630 NewSize, Type->hasSignedIntegerRepresentation());
2631 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
2632 Sema::AA_Converting, true);
2633 if (!Diff.isUsable())
2634 return nullptr;
2635 }
2636 }
2637
Alexander Musmana5f070a2014-10-01 06:03:56 +00002638 return Diff.get();
2639}
2640
Alexey Bataev62dbb972015-04-22 11:59:37 +00002641Expr *OpenMPIterationSpaceChecker::BuildPreCond(Scope *S, Expr *Cond) const {
2642 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
2643 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
2644 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
2645 auto CondExpr = SemaRef.BuildBinOp(
2646 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
2647 : (TestIsStrictOp ? BO_GT : BO_GE),
2648 LB, UB);
2649 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
2650 // Otherwise use original loop conditon and evaluate it in runtime.
2651 return CondExpr.isUsable() ? CondExpr.get() : Cond;
2652}
2653
Alexander Musmana5f070a2014-10-01 06:03:56 +00002654/// \brief Build reference expression to the counter be used for codegen.
2655Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const {
Alexey Bataev39f915b82015-05-08 10:41:21 +00002656 return buildDeclRefExpr(SemaRef, Var, Var->getType(), DefaultLoc);
Alexander Musmana5f070a2014-10-01 06:03:56 +00002657}
2658
2659/// \brief Build initization of the counter be used for codegen.
2660Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
2661
2662/// \brief Build step of the counter be used for codegen.
2663Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
2664
2665/// \brief Iteration space of a single for loop.
2666struct LoopIterationSpace {
Alexey Bataev62dbb972015-04-22 11:59:37 +00002667 /// \brief Condition of the loop.
2668 Expr *PreCond;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002669 /// \brief This expression calculates the number of iterations in the loop.
2670 /// It is always possible to calculate it before starting the loop.
2671 Expr *NumIterations;
2672 /// \brief The loop counter variable.
2673 Expr *CounterVar;
2674 /// \brief This is initializer for the initial value of #CounterVar.
2675 Expr *CounterInit;
2676 /// \brief This is step for the #CounterVar used to generate its update:
2677 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
2678 Expr *CounterStep;
2679 /// \brief Should step be subtracted?
2680 bool Subtract;
2681 /// \brief Source range of the loop init.
2682 SourceRange InitSrcRange;
2683 /// \brief Source range of the loop condition.
2684 SourceRange CondSrcRange;
2685 /// \brief Source range of the loop increment.
2686 SourceRange IncSrcRange;
2687};
2688
Alexey Bataev23b69422014-06-18 07:08:49 +00002689} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002690
Alexey Bataev9c821032015-04-30 04:23:23 +00002691void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
2692 assert(getLangOpts().OpenMP && "OpenMP is not active.");
2693 assert(Init && "Expected loop in canonical form.");
2694 unsigned CollapseIteration = DSAStack->getCollapseNumber();
2695 if (CollapseIteration > 0 &&
2696 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
2697 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
2698 if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
2699 DSAStack->addLoopControlVariable(ISC.GetLoopVar());
2700 }
2701 DSAStack->setCollapseNumber(CollapseIteration - 1);
2702 }
2703}
2704
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002705/// \brief Called on a for stmt to check and extract its iteration space
2706/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00002707static bool CheckOpenMPIterationSpace(
2708 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
2709 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
2710 Expr *NestedLoopCountExpr,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002711 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
2712 LoopIterationSpace &ResultIterSpace) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002713 // OpenMP [2.6, Canonical Loop Form]
2714 // for (init-expr; test-expr; incr-expr) structured-block
2715 auto For = dyn_cast_or_null<ForStmt>(S);
2716 if (!For) {
2717 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002718 << (NestedLoopCountExpr != nullptr) << getOpenMPDirectiveName(DKind)
2719 << NestedLoopCount << (CurrentNestedLoopCount > 0)
2720 << CurrentNestedLoopCount;
2721 if (NestedLoopCount > 1)
2722 SemaRef.Diag(NestedLoopCountExpr->getExprLoc(),
2723 diag::note_omp_collapse_expr)
2724 << NestedLoopCountExpr->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002725 return true;
2726 }
2727 assert(For->getBody());
2728
2729 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
2730
2731 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002732 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002733 if (ISC.CheckInit(Init)) {
2734 return true;
2735 }
2736
2737 bool HasErrors = false;
2738
2739 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002740 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002741
2742 // OpenMP [2.6, Canonical Loop Form]
2743 // Var is one of the following:
2744 // A variable of signed or unsigned integer type.
2745 // For C++, a variable of a random access iterator type.
2746 // For C, a variable of a pointer type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002747 auto VarType = Var->getType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002748 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
2749 !VarType->isPointerType() &&
2750 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
2751 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
2752 << SemaRef.getLangOpts().CPlusPlus;
2753 HasErrors = true;
2754 }
2755
Alexey Bataev4acb8592014-07-07 13:01:15 +00002756 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
2757 // Construct
2758 // The loop iteration variable(s) in the associated for-loop(s) of a for or
2759 // parallel for construct is (are) private.
2760 // The loop iteration variable in the associated for-loop of a simd construct
2761 // with just one associated for-loop is linear with a constant-linear-step
2762 // that is the increment of the associated for-loop.
2763 // Exclude loop var from the list of variables with implicitly defined data
2764 // sharing attributes.
Benjamin Kramerad8e0792014-10-10 15:32:48 +00002765 VarsWithImplicitDSA.erase(Var);
Alexey Bataev4acb8592014-07-07 13:01:15 +00002766
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002767 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
2768 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00002769 // The loop iteration variable in the associated for-loop of a simd construct
2770 // with just one associated for-loop may be listed in a linear clause with a
2771 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002772 // The loop iteration variable(s) in the associated for-loop(s) of a for or
2773 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002774 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002775 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
2776 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
2777 // declared in the loop and it is predetermined as a private.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002778 auto PredeterminedCKind =
2779 isOpenMPSimdDirective(DKind)
2780 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
2781 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002782 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataev0c024df2015-05-12 09:02:07 +00002783 DVar.CKind != OMPC_threadprivate && DVar.CKind != PredeterminedCKind) ||
Alexander Musmanf82886e2014-09-18 05:12:34 +00002784 (isOpenMPWorksharingDirective(DKind) && !isOpenMPSimdDirective(DKind) &&
2785 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private &&
Alexey Bataev0c024df2015-05-12 09:02:07 +00002786 DVar.CKind != OMPC_lastprivate && DVar.CKind != OMPC_threadprivate)) &&
2787 ((DVar.CKind != OMPC_private && DVar.CKind != OMPC_threadprivate) ||
2788 DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002789 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00002790 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
2791 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataevf2453a02015-05-06 07:25:08 +00002792 if (DVar.RefExpr == nullptr)
2793 DVar.CKind = PredeterminedCKind;
2794 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, /*IsLoopIterVar=*/true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002795 HasErrors = true;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002796 } else if (LoopVarRefExpr != nullptr) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00002797 // Make the loop iteration variable private (for worksharing constructs),
2798 // linear (for simd directives with the only one associated loop) or
2799 // lastprivate (for simd directives with several collapsed loops).
Alexey Bataev9aba41c2014-11-14 04:08:45 +00002800 if (DVar.CKind == OMPC_unknown)
2801 DVar = DSA.hasDSA(Var, isOpenMPPrivate, MatchesAlways(),
2802 /*FromParent=*/false);
Alexey Bataev9c821032015-04-30 04:23:23 +00002803 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002804 }
2805
Alexey Bataev7ff55242014-06-19 09:13:45 +00002806 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00002807
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002808 // Check test-expr.
2809 HasErrors |= ISC.CheckCond(For->getCond());
2810
2811 // Check incr-expr.
2812 HasErrors |= ISC.CheckInc(For->getInc());
2813
Alexander Musmana5f070a2014-10-01 06:03:56 +00002814 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002815 return HasErrors;
2816
Alexander Musmana5f070a2014-10-01 06:03:56 +00002817 // Build the loop's iteration space representation.
Alexey Bataev62dbb972015-04-22 11:59:37 +00002818 ResultIterSpace.PreCond = ISC.BuildPreCond(DSA.getCurScope(), For->getCond());
Alexander Musman174b3ca2014-10-06 11:16:29 +00002819 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
2820 DSA.getCurScope(), /* LimitedType */ isOpenMPWorksharingDirective(DKind));
Alexander Musmana5f070a2014-10-01 06:03:56 +00002821 ResultIterSpace.CounterVar = ISC.BuildCounterVar();
2822 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
2823 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
2824 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
2825 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
2826 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
2827 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
2828
Alexey Bataev62dbb972015-04-22 11:59:37 +00002829 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
2830 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00002831 ResultIterSpace.CounterVar == nullptr ||
2832 ResultIterSpace.CounterInit == nullptr ||
2833 ResultIterSpace.CounterStep == nullptr);
2834
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002835 return HasErrors;
2836}
2837
Alexander Musmana5f070a2014-10-01 06:03:56 +00002838/// \brief Build 'VarRef = Start + Iter * Step'.
2839static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S,
2840 SourceLocation Loc, ExprResult VarRef,
2841 ExprResult Start, ExprResult Iter,
2842 ExprResult Step, bool Subtract) {
2843 // Add parentheses (for debugging purposes only).
2844 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
2845 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
2846 !Step.isUsable())
2847 return ExprError();
2848
2849 ExprResult Update = SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(),
2850 Step.get()->IgnoreImplicit());
2851 if (!Update.isUsable())
2852 return ExprError();
2853
2854 // Build 'VarRef = Start + Iter * Step'.
2855 Update = SemaRef.BuildBinOp(S, Loc, (Subtract ? BO_Sub : BO_Add),
2856 Start.get()->IgnoreImplicit(), Update.get());
2857 if (!Update.isUsable())
2858 return ExprError();
2859
2860 Update = SemaRef.PerformImplicitConversion(
2861 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
2862 if (!Update.isUsable())
2863 return ExprError();
2864
2865 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
2866 return Update;
2867}
2868
2869/// \brief Convert integer expression \a E to make it have at least \a Bits
2870/// bits.
2871static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
2872 Sema &SemaRef) {
2873 if (E == nullptr)
2874 return ExprError();
2875 auto &C = SemaRef.Context;
2876 QualType OldType = E->getType();
2877 unsigned HasBits = C.getTypeSize(OldType);
2878 if (HasBits >= Bits)
2879 return ExprResult(E);
2880 // OK to convert to signed, because new type has more bits than old.
2881 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
2882 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
2883 true);
2884}
2885
2886/// \brief Check if the given expression \a E is a constant integer that fits
2887/// into \a Bits bits.
2888static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
2889 if (E == nullptr)
2890 return false;
2891 llvm::APSInt Result;
2892 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
2893 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
2894 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002895}
2896
2897/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00002898/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
2899/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002900static unsigned
2901CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *NestedLoopCountExpr,
2902 Stmt *AStmt, Sema &SemaRef, DSAStackTy &DSA,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002903 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00002904 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002905 unsigned NestedLoopCount = 1;
2906 if (NestedLoopCountExpr) {
2907 // Found 'collapse' clause - calculate collapse number.
2908 llvm::APSInt Result;
2909 if (NestedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
2910 NestedLoopCount = Result.getLimitedValue();
2911 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002912 // This is helper routine for loop directives (e.g., 'for', 'simd',
2913 // 'for simd', etc.).
Alexander Musmana5f070a2014-10-01 06:03:56 +00002914 SmallVector<LoopIterationSpace, 4> IterSpaces;
2915 IterSpaces.resize(NestedLoopCount);
2916 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002917 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002918 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev4acb8592014-07-07 13:01:15 +00002919 NestedLoopCount, NestedLoopCountExpr,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002920 VarsWithImplicitDSA, IterSpaces[Cnt]))
Alexey Bataevabfc0692014-06-25 06:52:00 +00002921 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002922 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00002923 // OpenMP [2.8.1, simd construct, Restrictions]
2924 // All loops associated with the construct must be perfectly nested; that
2925 // is, there must be no intervening code nor any OpenMP directive between
2926 // any two loops.
2927 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002928 }
2929
Alexander Musmana5f070a2014-10-01 06:03:56 +00002930 Built.clear(/* size */ NestedLoopCount);
2931
2932 if (SemaRef.CurContext->isDependentContext())
2933 return NestedLoopCount;
2934
2935 // An example of what is generated for the following code:
2936 //
2937 // #pragma omp simd collapse(2)
2938 // for (i = 0; i < NI; ++i)
2939 // for (j = J0; j < NJ; j+=2) {
2940 // <loop body>
2941 // }
2942 //
2943 // We generate the code below.
2944 // Note: the loop body may be outlined in CodeGen.
2945 // Note: some counters may be C++ classes, operator- is used to find number of
2946 // iterations and operator+= to calculate counter value.
2947 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
2948 // or i64 is currently supported).
2949 //
2950 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
2951 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
2952 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
2953 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
2954 // // similar updates for vars in clauses (e.g. 'linear')
2955 // <loop body (using local i and j)>
2956 // }
2957 // i = NI; // assign final values of counters
2958 // j = NJ;
2959 //
2960
2961 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
2962 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00002963 // Precondition tests if there is at least one iteration (all conditions are
2964 // true).
2965 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00002966 auto N0 = IterSpaces[0].NumIterations;
2967 ExprResult LastIteration32 = WidenIterationCount(32 /* Bits */, N0, SemaRef);
2968 ExprResult LastIteration64 = WidenIterationCount(64 /* Bits */, N0, SemaRef);
2969
2970 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
2971 return NestedLoopCount;
2972
2973 auto &C = SemaRef.Context;
2974 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
2975
2976 Scope *CurScope = DSA.getCurScope();
2977 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00002978 if (PreCond.isUsable()) {
2979 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
2980 PreCond.get(), IterSpaces[Cnt].PreCond);
2981 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002982 auto N = IterSpaces[Cnt].NumIterations;
2983 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
2984 if (LastIteration32.isUsable())
2985 LastIteration32 = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_Mul,
2986 LastIteration32.get(), N);
2987 if (LastIteration64.isUsable())
2988 LastIteration64 = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_Mul,
2989 LastIteration64.get(), N);
2990 }
2991
2992 // Choose either the 32-bit or 64-bit version.
2993 ExprResult LastIteration = LastIteration64;
2994 if (LastIteration32.isUsable() &&
2995 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
2996 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
2997 FitsInto(
2998 32 /* Bits */,
2999 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
3000 LastIteration64.get(), SemaRef)))
3001 LastIteration = LastIteration32;
3002
3003 if (!LastIteration.isUsable())
3004 return 0;
3005
3006 // Save the number of iterations.
3007 ExprResult NumIterations = LastIteration;
3008 {
3009 LastIteration = SemaRef.BuildBinOp(
3010 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
3011 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3012 if (!LastIteration.isUsable())
3013 return 0;
3014 }
3015
3016 // Calculate the last iteration number beforehand instead of doing this on
3017 // each iteration. Do not do this if the number of iterations may be kfold-ed.
3018 llvm::APSInt Result;
3019 bool IsConstant =
3020 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
3021 ExprResult CalcLastIteration;
3022 if (!IsConstant) {
3023 SourceLocation SaveLoc;
3024 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00003025 buildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00003026 ".omp.last.iteration");
Alexey Bataev39f915b82015-05-08 10:41:21 +00003027 ExprResult SaveRef = buildDeclRefExpr(
3028 SemaRef, SaveVar, LastIteration.get()->getType(), SaveLoc);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003029 CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign,
3030 SaveRef.get(), LastIteration.get());
3031 LastIteration = SaveRef;
3032
3033 // Prepare SaveRef + 1.
3034 NumIterations = SemaRef.BuildBinOp(
3035 CurScope, SaveLoc, BO_Add, SaveRef.get(),
3036 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3037 if (!NumIterations.isUsable())
3038 return 0;
3039 }
3040
3041 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
3042
Alexander Musmanc6388682014-12-15 07:07:06 +00003043 QualType VType = LastIteration.get()->getType();
3044 // Build variables passed into runtime, nesessary for worksharing directives.
3045 ExprResult LB, UB, IL, ST, EUB;
3046 if (isOpenMPWorksharingDirective(DKind)) {
3047 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00003048 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
3049 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003050 SemaRef.AddInitializerToDecl(
3051 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
3052 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3053
3054 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00003055 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
3056 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003057 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
3058 /*DirectInit*/ false,
3059 /*TypeMayContainAuto*/ false);
3060
3061 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
3062 // This will be used to implement clause 'lastprivate'.
3063 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00003064 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
3065 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003066 SemaRef.AddInitializerToDecl(
3067 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
3068 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3069
3070 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev39f915b82015-05-08 10:41:21 +00003071 VarDecl *STDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.stride");
3072 ST = buildDeclRefExpr(SemaRef, STDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003073 SemaRef.AddInitializerToDecl(
3074 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
3075 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3076
3077 // Build expression: UB = min(UB, LastIteration)
3078 // It is nesessary for CodeGen of directives with static scheduling.
3079 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
3080 UB.get(), LastIteration.get());
3081 ExprResult CondOp = SemaRef.ActOnConditionalOp(
3082 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
3083 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
3084 CondOp.get());
3085 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
3086 }
3087
3088 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003089 ExprResult IV;
3090 ExprResult Init;
3091 {
Alexey Bataev39f915b82015-05-08 10:41:21 +00003092 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.iv");
3093 IV = buildDeclRefExpr(SemaRef, IVDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003094 Expr *RHS = isOpenMPWorksharingDirective(DKind)
3095 ? LB.get()
3096 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
3097 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
3098 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003099 }
3100
Alexander Musmanc6388682014-12-15 07:07:06 +00003101 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003102 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00003103 ExprResult Cond =
3104 isOpenMPWorksharingDirective(DKind)
3105 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
3106 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
3107 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003108
3109 // Loop increment (IV = IV + 1)
3110 SourceLocation IncLoc;
3111 ExprResult Inc =
3112 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
3113 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
3114 if (!Inc.isUsable())
3115 return 0;
3116 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00003117 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
3118 if (!Inc.isUsable())
3119 return 0;
3120
3121 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
3122 // Used for directives with static scheduling.
3123 ExprResult NextLB, NextUB;
3124 if (isOpenMPWorksharingDirective(DKind)) {
3125 // LB + ST
3126 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
3127 if (!NextLB.isUsable())
3128 return 0;
3129 // LB = LB + ST
3130 NextLB =
3131 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
3132 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
3133 if (!NextLB.isUsable())
3134 return 0;
3135 // UB + ST
3136 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
3137 if (!NextUB.isUsable())
3138 return 0;
3139 // UB = UB + ST
3140 NextUB =
3141 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
3142 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
3143 if (!NextUB.isUsable())
3144 return 0;
3145 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003146
3147 // Build updates and final values of the loop counters.
3148 bool HasErrors = false;
3149 Built.Counters.resize(NestedLoopCount);
3150 Built.Updates.resize(NestedLoopCount);
3151 Built.Finals.resize(NestedLoopCount);
3152 {
3153 ExprResult Div;
3154 // Go from inner nested loop to outer.
3155 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
3156 LoopIterationSpace &IS = IterSpaces[Cnt];
3157 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
3158 // Build: Iter = (IV / Div) % IS.NumIters
3159 // where Div is product of previous iterations' IS.NumIters.
3160 ExprResult Iter;
3161 if (Div.isUsable()) {
3162 Iter =
3163 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
3164 } else {
3165 Iter = IV;
3166 assert((Cnt == (int)NestedLoopCount - 1) &&
3167 "unusable div expected on first iteration only");
3168 }
3169
3170 if (Cnt != 0 && Iter.isUsable())
3171 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
3172 IS.NumIterations);
3173 if (!Iter.isUsable()) {
3174 HasErrors = true;
3175 break;
3176 }
3177
Alexey Bataev39f915b82015-05-08 10:41:21 +00003178 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
3179 auto *CounterVar = buildDeclRefExpr(
3180 SemaRef, cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()),
3181 IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
3182 /*RefersToCapture=*/true);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003183 ExprResult Update =
Alexey Bataev39f915b82015-05-08 10:41:21 +00003184 BuildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003185 IS.CounterInit, Iter, IS.CounterStep, IS.Subtract);
3186 if (!Update.isUsable()) {
3187 HasErrors = true;
3188 break;
3189 }
3190
3191 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
3192 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00003193 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003194 IS.NumIterations, IS.CounterStep, IS.Subtract);
3195 if (!Final.isUsable()) {
3196 HasErrors = true;
3197 break;
3198 }
3199
3200 // Build Div for the next iteration: Div <- Div * IS.NumIters
3201 if (Cnt != 0) {
3202 if (Div.isUnset())
3203 Div = IS.NumIterations;
3204 else
3205 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
3206 IS.NumIterations);
3207
3208 // Add parentheses (for debugging purposes only).
3209 if (Div.isUsable())
3210 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
3211 if (!Div.isUsable()) {
3212 HasErrors = true;
3213 break;
3214 }
3215 }
3216 if (!Update.isUsable() || !Final.isUsable()) {
3217 HasErrors = true;
3218 break;
3219 }
3220 // Save results
3221 Built.Counters[Cnt] = IS.CounterVar;
3222 Built.Updates[Cnt] = Update.get();
3223 Built.Finals[Cnt] = Final.get();
3224 }
3225 }
3226
3227 if (HasErrors)
3228 return 0;
3229
3230 // Save results
3231 Built.IterationVarRef = IV.get();
3232 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00003233 Built.NumIterations = NumIterations.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003234 Built.CalcLastIteration = CalcLastIteration.get();
3235 Built.PreCond = PreCond.get();
3236 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003237 Built.Init = Init.get();
3238 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00003239 Built.LB = LB.get();
3240 Built.UB = UB.get();
3241 Built.IL = IL.get();
3242 Built.ST = ST.get();
3243 Built.EUB = EUB.get();
3244 Built.NLB = NextLB.get();
3245 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003246
Alexey Bataevabfc0692014-06-25 06:52:00 +00003247 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003248}
3249
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003250static Expr *GetCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Alexey Bataevc925aa32015-04-27 08:00:32 +00003251 auto &&CollapseFilter = [](const OMPClause *C) -> bool {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003252 return C->getClauseKind() == OMPC_collapse;
3253 };
3254 OMPExecutableDirective::filtered_clause_iterator<decltype(CollapseFilter)> I(
Alexey Bataevc925aa32015-04-27 08:00:32 +00003255 Clauses, std::move(CollapseFilter));
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003256 if (I)
3257 return cast<OMPCollapseClause>(*I)->getNumForLoops();
3258 return nullptr;
3259}
3260
Alexey Bataev4acb8592014-07-07 13:01:15 +00003261StmtResult Sema::ActOnOpenMPSimdDirective(
3262 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3263 SourceLocation EndLoc,
3264 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmanc6388682014-12-15 07:07:06 +00003265 OMPLoopDirective::HelperExprs B;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003266 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003267 unsigned NestedLoopCount =
3268 CheckOpenMPLoop(OMPD_simd, GetCollapseNumberExpr(Clauses), AStmt, *this,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003269 *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00003270 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003271 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003272
Alexander Musmana5f070a2014-10-01 06:03:56 +00003273 assert((CurContext->isDependentContext() || B.builtAll()) &&
3274 "omp simd loop exprs were not built");
3275
Alexander Musman3276a272015-03-21 10:12:56 +00003276 if (!CurContext->isDependentContext()) {
3277 // Finalize the clauses that need pre-built expressions for CodeGen.
3278 for (auto C : Clauses) {
3279 if (auto LC = dyn_cast<OMPLinearClause>(C))
3280 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3281 B.NumIterations, *this, CurScope))
3282 return StmtError();
3283 }
3284 }
3285
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003286 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003287 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
3288 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003289}
3290
Alexey Bataev4acb8592014-07-07 13:01:15 +00003291StmtResult Sema::ActOnOpenMPForDirective(
3292 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3293 SourceLocation EndLoc,
3294 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmanc6388682014-12-15 07:07:06 +00003295 OMPLoopDirective::HelperExprs B;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003296 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003297 unsigned NestedLoopCount =
3298 CheckOpenMPLoop(OMPD_for, GetCollapseNumberExpr(Clauses), AStmt, *this,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003299 *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00003300 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00003301 return StmtError();
3302
Alexander Musmana5f070a2014-10-01 06:03:56 +00003303 assert((CurContext->isDependentContext() || B.builtAll()) &&
3304 "omp for loop exprs were not built");
3305
Alexey Bataevf29276e2014-06-18 04:14:57 +00003306 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003307 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
3308 Clauses, AStmt, B);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003309}
3310
Alexander Musmanf82886e2014-09-18 05:12:34 +00003311StmtResult Sema::ActOnOpenMPForSimdDirective(
3312 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3313 SourceLocation EndLoc,
3314 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmanc6388682014-12-15 07:07:06 +00003315 OMPLoopDirective::HelperExprs B;
Alexander Musmanf82886e2014-09-18 05:12:34 +00003316 // In presence of clause 'collapse', it will define the nested loops number.
3317 unsigned NestedLoopCount =
3318 CheckOpenMPLoop(OMPD_for_simd, GetCollapseNumberExpr(Clauses), AStmt,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003319 *this, *DSAStack, VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00003320 if (NestedLoopCount == 0)
3321 return StmtError();
3322
Alexander Musmanc6388682014-12-15 07:07:06 +00003323 assert((CurContext->isDependentContext() || B.builtAll()) &&
3324 "omp for simd loop exprs were not built");
3325
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00003326 if (!CurContext->isDependentContext()) {
3327 // Finalize the clauses that need pre-built expressions for CodeGen.
3328 for (auto C : Clauses) {
3329 if (auto LC = dyn_cast<OMPLinearClause>(C))
3330 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3331 B.NumIterations, *this, CurScope))
3332 return StmtError();
3333 }
3334 }
3335
Alexander Musmanf82886e2014-09-18 05:12:34 +00003336 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003337 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
3338 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00003339}
3340
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003341StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
3342 Stmt *AStmt,
3343 SourceLocation StartLoc,
3344 SourceLocation EndLoc) {
3345 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3346 auto BaseStmt = AStmt;
3347 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
3348 BaseStmt = CS->getCapturedStmt();
3349 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
3350 auto S = C->children();
3351 if (!S)
3352 return StmtError();
3353 // All associated statements must be '#pragma omp section' except for
3354 // the first one.
Benjamin Kramer642f1732015-07-02 21:03:14 +00003355 for (Stmt *SectionStmt : ++S) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003356 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
3357 if (SectionStmt)
3358 Diag(SectionStmt->getLocStart(),
3359 diag::err_omp_sections_substmt_not_section);
3360 return StmtError();
3361 }
3362 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003363 } else {
3364 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
3365 return StmtError();
3366 }
3367
3368 getCurFunction()->setHasBranchProtectedScope();
3369
3370 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses,
3371 AStmt);
3372}
3373
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003374StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
3375 SourceLocation StartLoc,
3376 SourceLocation EndLoc) {
3377 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3378
3379 getCurFunction()->setHasBranchProtectedScope();
3380
3381 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt);
3382}
3383
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003384StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
3385 Stmt *AStmt,
3386 SourceLocation StartLoc,
3387 SourceLocation EndLoc) {
Alexey Bataev74a05c92014-07-15 02:55:09 +00003388 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3389
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003390 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00003391
Alexey Bataev3255bf32015-01-19 05:20:46 +00003392 // OpenMP [2.7.3, single Construct, Restrictions]
3393 // The copyprivate clause must not be used with the nowait clause.
3394 OMPClause *Nowait = nullptr;
3395 OMPClause *Copyprivate = nullptr;
3396 for (auto *Clause : Clauses) {
3397 if (Clause->getClauseKind() == OMPC_nowait)
3398 Nowait = Clause;
3399 else if (Clause->getClauseKind() == OMPC_copyprivate)
3400 Copyprivate = Clause;
3401 if (Copyprivate && Nowait) {
3402 Diag(Copyprivate->getLocStart(),
3403 diag::err_omp_single_copyprivate_with_nowait);
3404 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
3405 return StmtError();
3406 }
3407 }
3408
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003409 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3410}
3411
Alexander Musman80c22892014-07-17 08:54:58 +00003412StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
3413 SourceLocation StartLoc,
3414 SourceLocation EndLoc) {
3415 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3416
3417 getCurFunction()->setHasBranchProtectedScope();
3418
3419 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
3420}
3421
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003422StmtResult
3423Sema::ActOnOpenMPCriticalDirective(const DeclarationNameInfo &DirName,
3424 Stmt *AStmt, SourceLocation StartLoc,
3425 SourceLocation EndLoc) {
3426 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3427
3428 getCurFunction()->setHasBranchProtectedScope();
3429
3430 return OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
3431 AStmt);
3432}
3433
Alexey Bataev4acb8592014-07-07 13:01:15 +00003434StmtResult Sema::ActOnOpenMPParallelForDirective(
3435 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3436 SourceLocation EndLoc,
3437 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
3438 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3439 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3440 // 1.2.2 OpenMP Language Terminology
3441 // Structured block - An executable statement with a single entry at the
3442 // top and a single exit at the bottom.
3443 // The point of exit cannot be a branch out of the structured block.
3444 // longjmp() and throw() must not violate the entry/exit criteria.
3445 CS->getCapturedDecl()->setNothrow();
3446
Alexander Musmanc6388682014-12-15 07:07:06 +00003447 OMPLoopDirective::HelperExprs B;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003448 // In presence of clause 'collapse', it will define the nested loops number.
3449 unsigned NestedLoopCount =
3450 CheckOpenMPLoop(OMPD_parallel_for, GetCollapseNumberExpr(Clauses), AStmt,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003451 *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003452 if (NestedLoopCount == 0)
3453 return StmtError();
3454
Alexander Musmana5f070a2014-10-01 06:03:56 +00003455 assert((CurContext->isDependentContext() || B.builtAll()) &&
3456 "omp parallel for loop exprs were not built");
3457
Alexey Bataev4acb8592014-07-07 13:01:15 +00003458 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003459 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
3460 NestedLoopCount, Clauses, AStmt, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003461}
3462
Alexander Musmane4e893b2014-09-23 09:33:00 +00003463StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
3464 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3465 SourceLocation EndLoc,
3466 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
3467 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3468 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3469 // 1.2.2 OpenMP Language Terminology
3470 // Structured block - An executable statement with a single entry at the
3471 // top and a single exit at the bottom.
3472 // The point of exit cannot be a branch out of the structured block.
3473 // longjmp() and throw() must not violate the entry/exit criteria.
3474 CS->getCapturedDecl()->setNothrow();
3475
Alexander Musmanc6388682014-12-15 07:07:06 +00003476 OMPLoopDirective::HelperExprs B;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003477 // In presence of clause 'collapse', it will define the nested loops number.
3478 unsigned NestedLoopCount =
3479 CheckOpenMPLoop(OMPD_parallel_for_simd, GetCollapseNumberExpr(Clauses),
Alexander Musmana5f070a2014-10-01 06:03:56 +00003480 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003481 if (NestedLoopCount == 0)
3482 return StmtError();
3483
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00003484 if (!CurContext->isDependentContext()) {
3485 // Finalize the clauses that need pre-built expressions for CodeGen.
3486 for (auto C : Clauses) {
3487 if (auto LC = dyn_cast<OMPLinearClause>(C))
3488 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3489 B.NumIterations, *this, CurScope))
3490 return StmtError();
3491 }
3492 }
3493
Alexander Musmane4e893b2014-09-23 09:33:00 +00003494 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003495 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00003496 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003497}
3498
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003499StmtResult
3500Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
3501 Stmt *AStmt, SourceLocation StartLoc,
3502 SourceLocation EndLoc) {
3503 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3504 auto BaseStmt = AStmt;
3505 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
3506 BaseStmt = CS->getCapturedStmt();
3507 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
3508 auto S = C->children();
3509 if (!S)
3510 return StmtError();
3511 // All associated statements must be '#pragma omp section' except for
3512 // the first one.
Benjamin Kramer642f1732015-07-02 21:03:14 +00003513 for (Stmt *SectionStmt : ++S) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003514 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
3515 if (SectionStmt)
3516 Diag(SectionStmt->getLocStart(),
3517 diag::err_omp_parallel_sections_substmt_not_section);
3518 return StmtError();
3519 }
3520 }
3521 } else {
3522 Diag(AStmt->getLocStart(),
3523 diag::err_omp_parallel_sections_not_compound_stmt);
3524 return StmtError();
3525 }
3526
3527 getCurFunction()->setHasBranchProtectedScope();
3528
3529 return OMPParallelSectionsDirective::Create(Context, StartLoc, EndLoc,
3530 Clauses, AStmt);
3531}
3532
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003533StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
3534 Stmt *AStmt, SourceLocation StartLoc,
3535 SourceLocation EndLoc) {
3536 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3537 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3538 // 1.2.2 OpenMP Language Terminology
3539 // Structured block - An executable statement with a single entry at the
3540 // top and a single exit at the bottom.
3541 // The point of exit cannot be a branch out of the structured block.
3542 // longjmp() and throw() must not violate the entry/exit criteria.
3543 CS->getCapturedDecl()->setNothrow();
3544
3545 getCurFunction()->setHasBranchProtectedScope();
3546
3547 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3548}
3549
Alexey Bataev68446b72014-07-18 07:47:19 +00003550StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
3551 SourceLocation EndLoc) {
3552 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
3553}
3554
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003555StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
3556 SourceLocation EndLoc) {
3557 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
3558}
3559
Alexey Bataev2df347a2014-07-18 10:17:07 +00003560StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
3561 SourceLocation EndLoc) {
3562 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
3563}
3564
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003565StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
3566 SourceLocation StartLoc,
3567 SourceLocation EndLoc) {
3568 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3569
3570 getCurFunction()->setHasBranchProtectedScope();
3571
3572 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
3573}
3574
Alexey Bataev6125da92014-07-21 11:26:11 +00003575StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
3576 SourceLocation StartLoc,
3577 SourceLocation EndLoc) {
3578 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
3579 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
3580}
3581
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003582StmtResult Sema::ActOnOpenMPOrderedDirective(Stmt *AStmt,
3583 SourceLocation StartLoc,
3584 SourceLocation EndLoc) {
3585 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3586
3587 getCurFunction()->setHasBranchProtectedScope();
3588
3589 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, AStmt);
3590}
3591
Alexey Bataev1d160b12015-03-13 12:27:31 +00003592namespace {
3593/// \brief Helper class for checking expression in 'omp atomic [update]'
3594/// construct.
3595class OpenMPAtomicUpdateChecker {
3596 /// \brief Error results for atomic update expressions.
3597 enum ExprAnalysisErrorCode {
3598 /// \brief A statement is not an expression statement.
3599 NotAnExpression,
3600 /// \brief Expression is not builtin binary or unary operation.
3601 NotABinaryOrUnaryExpression,
3602 /// \brief Unary operation is not post-/pre- increment/decrement operation.
3603 NotAnUnaryIncDecExpression,
3604 /// \brief An expression is not of scalar type.
3605 NotAScalarType,
3606 /// \brief A binary operation is not an assignment operation.
3607 NotAnAssignmentOp,
3608 /// \brief RHS part of the binary operation is not a binary expression.
3609 NotABinaryExpression,
3610 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
3611 /// expression.
3612 NotABinaryOperator,
3613 /// \brief RHS binary operation does not have reference to the updated LHS
3614 /// part.
3615 NotAnUpdateExpression,
3616 /// \brief No errors is found.
3617 NoError
3618 };
3619 /// \brief Reference to Sema.
3620 Sema &SemaRef;
3621 /// \brief A location for note diagnostics (when error is found).
3622 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003623 /// \brief 'x' lvalue part of the source atomic expression.
3624 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003625 /// \brief 'expr' rvalue part of the source atomic expression.
3626 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00003627 /// \brief Helper expression of the form
3628 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
3629 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
3630 Expr *UpdateExpr;
3631 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
3632 /// important for non-associative operations.
3633 bool IsXLHSInRHSPart;
3634 BinaryOperatorKind Op;
3635 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00003636 /// \brief true if the source expression is a postfix unary operation, false
3637 /// if it is a prefix unary operation.
3638 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003639
3640public:
3641 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00003642 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00003643 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00003644 /// \brief Check specified statement that it is suitable for 'atomic update'
3645 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00003646 /// expression. If DiagId and NoteId == 0, then only check is performed
3647 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00003648 /// \param DiagId Diagnostic which should be emitted if error is found.
3649 /// \param NoteId Diagnostic note for the main error message.
3650 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00003651 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00003652 /// \brief Return the 'x' lvalue part of the source atomic expression.
3653 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00003654 /// \brief Return the 'expr' rvalue part of the source atomic expression.
3655 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00003656 /// \brief Return the update expression used in calculation of the updated
3657 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
3658 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
3659 Expr *getUpdateExpr() const { return UpdateExpr; }
3660 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
3661 /// false otherwise.
3662 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
3663
Alexey Bataevb78ca832015-04-01 03:33:17 +00003664 /// \brief true if the source expression is a postfix unary operation, false
3665 /// if it is a prefix unary operation.
3666 bool isPostfixUpdate() const { return IsPostfixUpdate; }
3667
Alexey Bataev1d160b12015-03-13 12:27:31 +00003668private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00003669 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
3670 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00003671};
3672} // namespace
3673
3674bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
3675 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
3676 ExprAnalysisErrorCode ErrorFound = NoError;
3677 SourceLocation ErrorLoc, NoteLoc;
3678 SourceRange ErrorRange, NoteRange;
3679 // Allowed constructs are:
3680 // x = x binop expr;
3681 // x = expr binop x;
3682 if (AtomicBinOp->getOpcode() == BO_Assign) {
3683 X = AtomicBinOp->getLHS();
3684 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
3685 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
3686 if (AtomicInnerBinOp->isMultiplicativeOp() ||
3687 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
3688 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00003689 Op = AtomicInnerBinOp->getOpcode();
3690 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00003691 auto *LHS = AtomicInnerBinOp->getLHS();
3692 auto *RHS = AtomicInnerBinOp->getRHS();
3693 llvm::FoldingSetNodeID XId, LHSId, RHSId;
3694 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
3695 /*Canonical=*/true);
3696 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
3697 /*Canonical=*/true);
3698 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
3699 /*Canonical=*/true);
3700 if (XId == LHSId) {
3701 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00003702 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003703 } else if (XId == RHSId) {
3704 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00003705 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003706 } else {
3707 ErrorLoc = AtomicInnerBinOp->getExprLoc();
3708 ErrorRange = AtomicInnerBinOp->getSourceRange();
3709 NoteLoc = X->getExprLoc();
3710 NoteRange = X->getSourceRange();
3711 ErrorFound = NotAnUpdateExpression;
3712 }
3713 } else {
3714 ErrorLoc = AtomicInnerBinOp->getExprLoc();
3715 ErrorRange = AtomicInnerBinOp->getSourceRange();
3716 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
3717 NoteRange = SourceRange(NoteLoc, NoteLoc);
3718 ErrorFound = NotABinaryOperator;
3719 }
3720 } else {
3721 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
3722 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
3723 ErrorFound = NotABinaryExpression;
3724 }
3725 } else {
3726 ErrorLoc = AtomicBinOp->getExprLoc();
3727 ErrorRange = AtomicBinOp->getSourceRange();
3728 NoteLoc = AtomicBinOp->getOperatorLoc();
3729 NoteRange = SourceRange(NoteLoc, NoteLoc);
3730 ErrorFound = NotAnAssignmentOp;
3731 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00003732 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00003733 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
3734 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
3735 return true;
3736 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00003737 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00003738 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003739}
3740
3741bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
3742 unsigned NoteId) {
3743 ExprAnalysisErrorCode ErrorFound = NoError;
3744 SourceLocation ErrorLoc, NoteLoc;
3745 SourceRange ErrorRange, NoteRange;
3746 // Allowed constructs are:
3747 // x++;
3748 // x--;
3749 // ++x;
3750 // --x;
3751 // x binop= expr;
3752 // x = x binop expr;
3753 // x = expr binop x;
3754 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
3755 AtomicBody = AtomicBody->IgnoreParenImpCasts();
3756 if (AtomicBody->getType()->isScalarType() ||
3757 AtomicBody->isInstantiationDependent()) {
3758 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
3759 AtomicBody->IgnoreParenImpCasts())) {
3760 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00003761 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00003762 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00003763 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00003764 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00003765 X = AtomicCompAssignOp->getLHS();
3766 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003767 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
3768 AtomicBody->IgnoreParenImpCasts())) {
3769 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00003770 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
3771 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003772 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00003773 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
3774 // Check for Unary Operation
3775 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00003776 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00003777 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
3778 OpLoc = AtomicUnaryOp->getOperatorLoc();
3779 X = AtomicUnaryOp->getSubExpr();
3780 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
3781 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003782 } else {
3783 ErrorFound = NotAnUnaryIncDecExpression;
3784 ErrorLoc = AtomicUnaryOp->getExprLoc();
3785 ErrorRange = AtomicUnaryOp->getSourceRange();
3786 NoteLoc = AtomicUnaryOp->getOperatorLoc();
3787 NoteRange = SourceRange(NoteLoc, NoteLoc);
3788 }
3789 } else {
3790 ErrorFound = NotABinaryOrUnaryExpression;
3791 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
3792 NoteRange = ErrorRange = AtomicBody->getSourceRange();
3793 }
3794 } else {
3795 ErrorFound = NotAScalarType;
3796 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
3797 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
3798 }
3799 } else {
3800 ErrorFound = NotAnExpression;
3801 NoteLoc = ErrorLoc = S->getLocStart();
3802 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
3803 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00003804 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00003805 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
3806 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
3807 return true;
3808 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00003809 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00003810 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00003811 // Build an update expression of form 'OpaqueValueExpr(x) binop
3812 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
3813 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
3814 auto *OVEX = new (SemaRef.getASTContext())
3815 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
3816 auto *OVEExpr = new (SemaRef.getASTContext())
3817 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
3818 auto Update =
3819 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
3820 IsXLHSInRHSPart ? OVEExpr : OVEX);
3821 if (Update.isInvalid())
3822 return true;
3823 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
3824 Sema::AA_Casting);
3825 if (Update.isInvalid())
3826 return true;
3827 UpdateExpr = Update.get();
3828 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00003829 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003830}
3831
Alexey Bataev0162e452014-07-22 10:10:35 +00003832StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
3833 Stmt *AStmt,
3834 SourceLocation StartLoc,
3835 SourceLocation EndLoc) {
3836 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003837 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00003838 // 1.2.2 OpenMP Language Terminology
3839 // Structured block - An executable statement with a single entry at the
3840 // top and a single exit at the bottom.
3841 // The point of exit cannot be a branch out of the structured block.
3842 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00003843 OpenMPClauseKind AtomicKind = OMPC_unknown;
3844 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003845 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00003846 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00003847 C->getClauseKind() == OMPC_update ||
3848 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00003849 if (AtomicKind != OMPC_unknown) {
3850 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
3851 << SourceRange(C->getLocStart(), C->getLocEnd());
3852 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
3853 << getOpenMPClauseName(AtomicKind);
3854 } else {
3855 AtomicKind = C->getClauseKind();
3856 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003857 }
3858 }
3859 }
Alexey Bataev62cec442014-11-18 10:14:22 +00003860
Alexey Bataev459dec02014-07-24 06:46:57 +00003861 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00003862 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
3863 Body = EWC->getSubExpr();
3864
Alexey Bataev62cec442014-11-18 10:14:22 +00003865 Expr *X = nullptr;
3866 Expr *V = nullptr;
3867 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00003868 Expr *UE = nullptr;
3869 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00003870 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00003871 // OpenMP [2.12.6, atomic Construct]
3872 // In the next expressions:
3873 // * x and v (as applicable) are both l-value expressions with scalar type.
3874 // * During the execution of an atomic region, multiple syntactic
3875 // occurrences of x must designate the same storage location.
3876 // * Neither of v and expr (as applicable) may access the storage location
3877 // designated by x.
3878 // * Neither of x and expr (as applicable) may access the storage location
3879 // designated by v.
3880 // * expr is an expression with scalar type.
3881 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
3882 // * binop, binop=, ++, and -- are not overloaded operators.
3883 // * The expression x binop expr must be numerically equivalent to x binop
3884 // (expr). This requirement is satisfied if the operators in expr have
3885 // precedence greater than binop, or by using parentheses around expr or
3886 // subexpressions of expr.
3887 // * The expression expr binop x must be numerically equivalent to (expr)
3888 // binop x. This requirement is satisfied if the operators in expr have
3889 // precedence equal to or greater than binop, or by using parentheses around
3890 // expr or subexpressions of expr.
3891 // * For forms that allow multiple occurrences of x, the number of times
3892 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00003893 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00003894 enum {
3895 NotAnExpression,
3896 NotAnAssignmentOp,
3897 NotAScalarType,
3898 NotAnLValue,
3899 NoError
3900 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00003901 SourceLocation ErrorLoc, NoteLoc;
3902 SourceRange ErrorRange, NoteRange;
3903 // If clause is read:
3904 // v = x;
3905 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
3906 auto AtomicBinOp =
3907 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
3908 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
3909 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
3910 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
3911 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
3912 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
3913 if (!X->isLValue() || !V->isLValue()) {
3914 auto NotLValueExpr = X->isLValue() ? V : X;
3915 ErrorFound = NotAnLValue;
3916 ErrorLoc = AtomicBinOp->getExprLoc();
3917 ErrorRange = AtomicBinOp->getSourceRange();
3918 NoteLoc = NotLValueExpr->getExprLoc();
3919 NoteRange = NotLValueExpr->getSourceRange();
3920 }
3921 } else if (!X->isInstantiationDependent() ||
3922 !V->isInstantiationDependent()) {
3923 auto NotScalarExpr =
3924 (X->isInstantiationDependent() || X->getType()->isScalarType())
3925 ? V
3926 : X;
3927 ErrorFound = NotAScalarType;
3928 ErrorLoc = AtomicBinOp->getExprLoc();
3929 ErrorRange = AtomicBinOp->getSourceRange();
3930 NoteLoc = NotScalarExpr->getExprLoc();
3931 NoteRange = NotScalarExpr->getSourceRange();
3932 }
3933 } else {
3934 ErrorFound = NotAnAssignmentOp;
3935 ErrorLoc = AtomicBody->getExprLoc();
3936 ErrorRange = AtomicBody->getSourceRange();
3937 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
3938 : AtomicBody->getExprLoc();
3939 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
3940 : AtomicBody->getSourceRange();
3941 }
3942 } else {
3943 ErrorFound = NotAnExpression;
3944 NoteLoc = ErrorLoc = Body->getLocStart();
3945 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00003946 }
Alexey Bataev62cec442014-11-18 10:14:22 +00003947 if (ErrorFound != NoError) {
3948 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
3949 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00003950 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
3951 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00003952 return StmtError();
3953 } else if (CurContext->isDependentContext())
3954 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00003955 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00003956 enum {
3957 NotAnExpression,
3958 NotAnAssignmentOp,
3959 NotAScalarType,
3960 NotAnLValue,
3961 NoError
3962 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00003963 SourceLocation ErrorLoc, NoteLoc;
3964 SourceRange ErrorRange, NoteRange;
3965 // If clause is write:
3966 // x = expr;
3967 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
3968 auto AtomicBinOp =
3969 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
3970 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00003971 X = AtomicBinOp->getLHS();
3972 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00003973 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
3974 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
3975 if (!X->isLValue()) {
3976 ErrorFound = NotAnLValue;
3977 ErrorLoc = AtomicBinOp->getExprLoc();
3978 ErrorRange = AtomicBinOp->getSourceRange();
3979 NoteLoc = X->getExprLoc();
3980 NoteRange = X->getSourceRange();
3981 }
3982 } else if (!X->isInstantiationDependent() ||
3983 !E->isInstantiationDependent()) {
3984 auto NotScalarExpr =
3985 (X->isInstantiationDependent() || X->getType()->isScalarType())
3986 ? E
3987 : X;
3988 ErrorFound = NotAScalarType;
3989 ErrorLoc = AtomicBinOp->getExprLoc();
3990 ErrorRange = AtomicBinOp->getSourceRange();
3991 NoteLoc = NotScalarExpr->getExprLoc();
3992 NoteRange = NotScalarExpr->getSourceRange();
3993 }
3994 } else {
3995 ErrorFound = NotAnAssignmentOp;
3996 ErrorLoc = AtomicBody->getExprLoc();
3997 ErrorRange = AtomicBody->getSourceRange();
3998 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
3999 : AtomicBody->getExprLoc();
4000 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
4001 : AtomicBody->getSourceRange();
4002 }
4003 } else {
4004 ErrorFound = NotAnExpression;
4005 NoteLoc = ErrorLoc = Body->getLocStart();
4006 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00004007 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00004008 if (ErrorFound != NoError) {
4009 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
4010 << ErrorRange;
4011 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
4012 << NoteRange;
4013 return StmtError();
4014 } else if (CurContext->isDependentContext())
4015 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00004016 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004017 // If clause is update:
4018 // x++;
4019 // x--;
4020 // ++x;
4021 // --x;
4022 // x binop= expr;
4023 // x = x binop expr;
4024 // x = expr binop x;
4025 OpenMPAtomicUpdateChecker Checker(*this);
4026 if (Checker.checkStatement(
4027 Body, (AtomicKind == OMPC_update)
4028 ? diag::err_omp_atomic_update_not_expression_statement
4029 : diag::err_omp_atomic_not_expression_statement,
4030 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00004031 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00004032 if (!CurContext->isDependentContext()) {
4033 E = Checker.getExpr();
4034 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00004035 UE = Checker.getUpdateExpr();
4036 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00004037 }
Alexey Bataev459dec02014-07-24 06:46:57 +00004038 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004039 enum {
4040 NotAnAssignmentOp,
4041 NotACompoundStatement,
4042 NotTwoSubstatements,
4043 NotASpecificExpression,
4044 NoError
4045 } ErrorFound = NoError;
4046 SourceLocation ErrorLoc, NoteLoc;
4047 SourceRange ErrorRange, NoteRange;
4048 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
4049 // If clause is a capture:
4050 // v = x++;
4051 // v = x--;
4052 // v = ++x;
4053 // v = --x;
4054 // v = x binop= expr;
4055 // v = x = x binop expr;
4056 // v = x = expr binop x;
4057 auto *AtomicBinOp =
4058 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
4059 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
4060 V = AtomicBinOp->getLHS();
4061 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
4062 OpenMPAtomicUpdateChecker Checker(*this);
4063 if (Checker.checkStatement(
4064 Body, diag::err_omp_atomic_capture_not_expression_statement,
4065 diag::note_omp_atomic_update))
4066 return StmtError();
4067 E = Checker.getExpr();
4068 X = Checker.getX();
4069 UE = Checker.getUpdateExpr();
4070 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
4071 IsPostfixUpdate = Checker.isPostfixUpdate();
4072 } else {
4073 ErrorLoc = AtomicBody->getExprLoc();
4074 ErrorRange = AtomicBody->getSourceRange();
4075 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
4076 : AtomicBody->getExprLoc();
4077 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
4078 : AtomicBody->getSourceRange();
4079 ErrorFound = NotAnAssignmentOp;
4080 }
4081 if (ErrorFound != NoError) {
4082 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
4083 << ErrorRange;
4084 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
4085 return StmtError();
4086 } else if (CurContext->isDependentContext()) {
4087 UE = V = E = X = nullptr;
4088 }
4089 } else {
4090 // If clause is a capture:
4091 // { v = x; x = expr; }
4092 // { v = x; x++; }
4093 // { v = x; x--; }
4094 // { v = x; ++x; }
4095 // { v = x; --x; }
4096 // { v = x; x binop= expr; }
4097 // { v = x; x = x binop expr; }
4098 // { v = x; x = expr binop x; }
4099 // { x++; v = x; }
4100 // { x--; v = x; }
4101 // { ++x; v = x; }
4102 // { --x; v = x; }
4103 // { x binop= expr; v = x; }
4104 // { x = x binop expr; v = x; }
4105 // { x = expr binop x; v = x; }
4106 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
4107 // Check that this is { expr1; expr2; }
4108 if (CS->size() == 2) {
4109 auto *First = CS->body_front();
4110 auto *Second = CS->body_back();
4111 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
4112 First = EWC->getSubExpr()->IgnoreParenImpCasts();
4113 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
4114 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
4115 // Need to find what subexpression is 'v' and what is 'x'.
4116 OpenMPAtomicUpdateChecker Checker(*this);
4117 bool IsUpdateExprFound = !Checker.checkStatement(Second);
4118 BinaryOperator *BinOp = nullptr;
4119 if (IsUpdateExprFound) {
4120 BinOp = dyn_cast<BinaryOperator>(First);
4121 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
4122 }
4123 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
4124 // { v = x; x++; }
4125 // { v = x; x--; }
4126 // { v = x; ++x; }
4127 // { v = x; --x; }
4128 // { v = x; x binop= expr; }
4129 // { v = x; x = x binop expr; }
4130 // { v = x; x = expr binop x; }
4131 // Check that the first expression has form v = x.
4132 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
4133 llvm::FoldingSetNodeID XId, PossibleXId;
4134 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
4135 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
4136 IsUpdateExprFound = XId == PossibleXId;
4137 if (IsUpdateExprFound) {
4138 V = BinOp->getLHS();
4139 X = Checker.getX();
4140 E = Checker.getExpr();
4141 UE = Checker.getUpdateExpr();
4142 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00004143 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004144 }
4145 }
4146 if (!IsUpdateExprFound) {
4147 IsUpdateExprFound = !Checker.checkStatement(First);
4148 BinOp = nullptr;
4149 if (IsUpdateExprFound) {
4150 BinOp = dyn_cast<BinaryOperator>(Second);
4151 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
4152 }
4153 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
4154 // { x++; v = x; }
4155 // { x--; v = x; }
4156 // { ++x; v = x; }
4157 // { --x; v = x; }
4158 // { x binop= expr; v = x; }
4159 // { x = x binop expr; v = x; }
4160 // { x = expr binop x; v = x; }
4161 // Check that the second expression has form v = x.
4162 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
4163 llvm::FoldingSetNodeID XId, PossibleXId;
4164 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
4165 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
4166 IsUpdateExprFound = XId == PossibleXId;
4167 if (IsUpdateExprFound) {
4168 V = BinOp->getLHS();
4169 X = Checker.getX();
4170 E = Checker.getExpr();
4171 UE = Checker.getUpdateExpr();
4172 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00004173 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004174 }
4175 }
4176 }
4177 if (!IsUpdateExprFound) {
4178 // { v = x; x = expr; }
4179 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
4180 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
4181 ErrorFound = NotAnAssignmentOp;
4182 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
4183 : First->getLocStart();
4184 NoteRange = ErrorRange = FirstBinOp
4185 ? FirstBinOp->getSourceRange()
4186 : SourceRange(ErrorLoc, ErrorLoc);
4187 } else {
4188 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
4189 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
4190 ErrorFound = NotAnAssignmentOp;
4191 NoteLoc = ErrorLoc = SecondBinOp ? SecondBinOp->getOperatorLoc()
4192 : Second->getLocStart();
4193 NoteRange = ErrorRange = SecondBinOp
4194 ? SecondBinOp->getSourceRange()
4195 : SourceRange(ErrorLoc, ErrorLoc);
4196 } else {
4197 auto *PossibleXRHSInFirst =
4198 FirstBinOp->getRHS()->IgnoreParenImpCasts();
4199 auto *PossibleXLHSInSecond =
4200 SecondBinOp->getLHS()->IgnoreParenImpCasts();
4201 llvm::FoldingSetNodeID X1Id, X2Id;
4202 PossibleXRHSInFirst->Profile(X1Id, Context, /*Canonical=*/true);
4203 PossibleXLHSInSecond->Profile(X2Id, Context,
4204 /*Canonical=*/true);
4205 IsUpdateExprFound = X1Id == X2Id;
4206 if (IsUpdateExprFound) {
4207 V = FirstBinOp->getLHS();
4208 X = SecondBinOp->getLHS();
4209 E = SecondBinOp->getRHS();
4210 UE = nullptr;
4211 IsXLHSInRHSPart = false;
4212 IsPostfixUpdate = true;
4213 } else {
4214 ErrorFound = NotASpecificExpression;
4215 ErrorLoc = FirstBinOp->getExprLoc();
4216 ErrorRange = FirstBinOp->getSourceRange();
4217 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
4218 NoteRange = SecondBinOp->getRHS()->getSourceRange();
4219 }
4220 }
4221 }
4222 }
4223 } else {
4224 NoteLoc = ErrorLoc = Body->getLocStart();
4225 NoteRange = ErrorRange =
4226 SourceRange(Body->getLocStart(), Body->getLocStart());
4227 ErrorFound = NotTwoSubstatements;
4228 }
4229 } else {
4230 NoteLoc = ErrorLoc = Body->getLocStart();
4231 NoteRange = ErrorRange =
4232 SourceRange(Body->getLocStart(), Body->getLocStart());
4233 ErrorFound = NotACompoundStatement;
4234 }
4235 if (ErrorFound != NoError) {
4236 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
4237 << ErrorRange;
4238 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
4239 return StmtError();
4240 } else if (CurContext->isDependentContext()) {
4241 UE = V = E = X = nullptr;
4242 }
Alexey Bataev459dec02014-07-24 06:46:57 +00004243 }
Alexey Bataevdea47612014-07-23 07:46:59 +00004244 }
Alexey Bataev0162e452014-07-22 10:10:35 +00004245
4246 getCurFunction()->setHasBranchProtectedScope();
4247
Alexey Bataev62cec442014-11-18 10:14:22 +00004248 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00004249 X, V, E, UE, IsXLHSInRHSPart,
4250 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00004251}
4252
Alexey Bataev0bd520b2014-09-19 08:19:49 +00004253StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
4254 Stmt *AStmt,
4255 SourceLocation StartLoc,
4256 SourceLocation EndLoc) {
4257 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
4258
Alexey Bataev13314bf2014-10-09 04:18:56 +00004259 // OpenMP [2.16, Nesting of Regions]
4260 // If specified, a teams construct must be contained within a target
4261 // construct. That target construct must contain no statements or directives
4262 // outside of the teams construct.
4263 if (DSAStack->hasInnerTeamsRegion()) {
4264 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
4265 bool OMPTeamsFound = true;
4266 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
4267 auto I = CS->body_begin();
4268 while (I != CS->body_end()) {
4269 auto OED = dyn_cast<OMPExecutableDirective>(*I);
4270 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
4271 OMPTeamsFound = false;
4272 break;
4273 }
4274 ++I;
4275 }
4276 assert(I != CS->body_end() && "Not found statement");
4277 S = *I;
4278 }
4279 if (!OMPTeamsFound) {
4280 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
4281 Diag(DSAStack->getInnerTeamsRegionLoc(),
4282 diag::note_omp_nested_teams_construct_here);
4283 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
4284 << isa<OMPExecutableDirective>(S);
4285 return StmtError();
4286 }
4287 }
4288
Alexey Bataev0bd520b2014-09-19 08:19:49 +00004289 getCurFunction()->setHasBranchProtectedScope();
4290
4291 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4292}
4293
Alexey Bataev13314bf2014-10-09 04:18:56 +00004294StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
4295 Stmt *AStmt, SourceLocation StartLoc,
4296 SourceLocation EndLoc) {
4297 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
4298 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4299 // 1.2.2 OpenMP Language Terminology
4300 // Structured block - An executable statement with a single entry at the
4301 // top and a single exit at the bottom.
4302 // The point of exit cannot be a branch out of the structured block.
4303 // longjmp() and throw() must not violate the entry/exit criteria.
4304 CS->getCapturedDecl()->setNothrow();
4305
4306 getCurFunction()->setHasBranchProtectedScope();
4307
4308 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4309}
4310
Alexey Bataev6d4ed052015-07-01 06:57:41 +00004311StmtResult
4312Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
4313 SourceLocation EndLoc,
4314 OpenMPDirectiveKind CancelRegion) {
4315 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
4316 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
4317 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
4318 << getOpenMPDirectiveName(CancelRegion);
4319 return StmtError();
4320 }
4321 if (DSAStack->isParentNowaitRegion()) {
4322 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
4323 return StmtError();
4324 }
4325 if (DSAStack->isParentOrderedRegion()) {
4326 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
4327 return StmtError();
4328 }
4329 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
4330 CancelRegion);
4331}
4332
Alexey Bataev80909872015-07-02 11:25:17 +00004333StmtResult Sema::ActOnOpenMPCancelDirective(SourceLocation StartLoc,
4334 SourceLocation EndLoc,
4335 OpenMPDirectiveKind CancelRegion) {
4336 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
4337 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
4338 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
4339 << getOpenMPDirectiveName(CancelRegion);
4340 return StmtError();
4341 }
4342 if (DSAStack->isParentNowaitRegion()) {
4343 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
4344 return StmtError();
4345 }
4346 if (DSAStack->isParentOrderedRegion()) {
4347 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
4348 return StmtError();
4349 }
4350 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, CancelRegion);
4351}
4352
Alexey Bataeved09d242014-05-28 05:53:51 +00004353OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004354 SourceLocation StartLoc,
4355 SourceLocation LParenLoc,
4356 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004357 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004358 switch (Kind) {
4359 case OMPC_if:
4360 Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc);
4361 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00004362 case OMPC_final:
4363 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
4364 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00004365 case OMPC_num_threads:
4366 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
4367 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00004368 case OMPC_safelen:
4369 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
4370 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00004371 case OMPC_collapse:
4372 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
4373 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004374 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004375 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00004376 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004377 case OMPC_private:
4378 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00004379 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004380 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00004381 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00004382 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00004383 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004384 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00004385 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004386 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00004387 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00004388 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00004389 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004390 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00004391 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004392 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00004393 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00004394 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00004395 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00004396 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00004397 case OMPC_depend:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004398 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004399 llvm_unreachable("Clause is not allowed.");
4400 }
4401 return Res;
4402}
4403
Alexey Bataeved09d242014-05-28 05:53:51 +00004404OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004405 SourceLocation LParenLoc,
4406 SourceLocation EndLoc) {
4407 Expr *ValExpr = Condition;
4408 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
4409 !Condition->isInstantiationDependent() &&
4410 !Condition->containsUnexpandedParameterPack()) {
4411 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00004412 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004413 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004414 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004415
Nikola Smiljanic01a75982014-05-29 10:55:11 +00004416 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004417 }
4418
4419 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc);
4420}
4421
Alexey Bataev3778b602014-07-17 07:32:53 +00004422OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
4423 SourceLocation StartLoc,
4424 SourceLocation LParenLoc,
4425 SourceLocation EndLoc) {
4426 Expr *ValExpr = Condition;
4427 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
4428 !Condition->isInstantiationDependent() &&
4429 !Condition->containsUnexpandedParameterPack()) {
4430 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
4431 Condition->getExprLoc(), Condition);
4432 if (Val.isInvalid())
4433 return nullptr;
4434
4435 ValExpr = Val.get();
4436 }
4437
4438 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
4439}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004440ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
4441 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00004442 if (!Op)
4443 return ExprError();
4444
4445 class IntConvertDiagnoser : public ICEConvertDiagnoser {
4446 public:
4447 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00004448 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00004449 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
4450 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004451 return S.Diag(Loc, diag::err_omp_not_integral) << T;
4452 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004453 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
4454 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004455 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
4456 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004457 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
4458 QualType T,
4459 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004460 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
4461 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004462 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
4463 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004464 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00004465 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00004466 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004467 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
4468 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004469 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
4470 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004471 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
4472 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004473 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00004474 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00004475 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004476 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
4477 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004478 llvm_unreachable("conversion functions are permitted");
4479 }
4480 } ConvertDiagnoser;
4481 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
4482}
4483
4484OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
4485 SourceLocation StartLoc,
4486 SourceLocation LParenLoc,
4487 SourceLocation EndLoc) {
4488 Expr *ValExpr = NumThreads;
4489 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
Alexey Bataev568a8332014-03-06 06:15:19 +00004490 !NumThreads->containsUnexpandedParameterPack()) {
4491 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
4492 ExprResult Val =
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004493 PerformOpenMPImplicitIntegerConversion(NumThreadsLoc, NumThreads);
Alexey Bataev568a8332014-03-06 06:15:19 +00004494 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004495 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00004496
Nikola Smiljanic01a75982014-05-29 10:55:11 +00004497 ValExpr = Val.get();
Alexey Bataev568a8332014-03-06 06:15:19 +00004498
4499 // OpenMP [2.5, Restrictions]
4500 // The num_threads expression must evaluate to a positive integer value.
4501 llvm::APSInt Result;
Alexey Bataeved09d242014-05-28 05:53:51 +00004502 if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() &&
4503 !Result.isStrictlyPositive()) {
Alexey Bataev568a8332014-03-06 06:15:19 +00004504 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
4505 << "num_threads" << NumThreads->getSourceRange();
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004506 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00004507 }
4508 }
4509
Alexey Bataeved09d242014-05-28 05:53:51 +00004510 return new (Context)
4511 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00004512}
4513
Alexey Bataev62c87d22014-03-21 04:51:18 +00004514ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
4515 OpenMPClauseKind CKind) {
4516 if (!E)
4517 return ExprError();
4518 if (E->isValueDependent() || E->isTypeDependent() ||
4519 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00004520 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00004521 llvm::APSInt Result;
4522 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
4523 if (ICE.isInvalid())
4524 return ExprError();
4525 if (!Result.isStrictlyPositive()) {
4526 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
4527 << getOpenMPClauseName(CKind) << E->getSourceRange();
4528 return ExprError();
4529 }
Alexander Musman09184fe2014-09-30 05:29:28 +00004530 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
4531 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
4532 << E->getSourceRange();
4533 return ExprError();
4534 }
Alexey Bataev9c821032015-04-30 04:23:23 +00004535 if (CKind == OMPC_collapse) {
4536 DSAStack->setCollapseNumber(Result.getExtValue());
4537 }
Alexey Bataev62c87d22014-03-21 04:51:18 +00004538 return ICE;
4539}
4540
4541OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
4542 SourceLocation LParenLoc,
4543 SourceLocation EndLoc) {
4544 // OpenMP [2.8.1, simd construct, Description]
4545 // The parameter of the safelen clause must be a constant
4546 // positive integer expression.
4547 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
4548 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004549 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00004550 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00004551 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00004552}
4553
Alexander Musman64d33f12014-06-04 07:53:32 +00004554OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
4555 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00004556 SourceLocation LParenLoc,
4557 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00004558 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00004559 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00004560 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00004561 // The parameter of the collapse clause must be a constant
4562 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00004563 ExprResult NumForLoopsResult =
4564 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
4565 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00004566 return nullptr;
4567 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00004568 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00004569}
4570
Alexey Bataeved09d242014-05-28 05:53:51 +00004571OMPClause *Sema::ActOnOpenMPSimpleClause(
4572 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
4573 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004574 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004575 switch (Kind) {
4576 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00004577 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00004578 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
4579 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004580 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004581 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00004582 Res = ActOnOpenMPProcBindClause(
4583 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
4584 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004585 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004586 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00004587 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00004588 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00004589 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00004590 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00004591 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004592 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004593 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00004594 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00004595 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00004596 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00004597 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00004598 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004599 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00004600 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004601 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00004602 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00004603 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00004604 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004605 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00004606 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004607 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00004608 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00004609 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00004610 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00004611 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00004612 case OMPC_depend:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004613 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004614 llvm_unreachable("Clause is not allowed.");
4615 }
4616 return Res;
4617}
4618
4619OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
4620 SourceLocation KindKwLoc,
4621 SourceLocation StartLoc,
4622 SourceLocation LParenLoc,
4623 SourceLocation EndLoc) {
4624 if (Kind == OMPC_DEFAULT_unknown) {
4625 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00004626 static_assert(OMPC_DEFAULT_unknown > 0,
4627 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00004628 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00004629 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004630 Values += "'";
4631 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
4632 Values += "'";
4633 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00004634 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004635 Values += " or ";
4636 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00004637 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004638 break;
4639 default:
4640 Values += Sep;
4641 break;
4642 }
4643 }
4644 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00004645 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004646 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004647 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00004648 switch (Kind) {
4649 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00004650 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004651 break;
4652 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00004653 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004654 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004655 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004656 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00004657 break;
4658 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004659 return new (Context)
4660 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004661}
4662
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004663OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
4664 SourceLocation KindKwLoc,
4665 SourceLocation StartLoc,
4666 SourceLocation LParenLoc,
4667 SourceLocation EndLoc) {
4668 if (Kind == OMPC_PROC_BIND_unknown) {
4669 std::string Values;
4670 std::string Sep(", ");
4671 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
4672 Values += "'";
4673 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
4674 Values += "'";
4675 switch (i) {
4676 case OMPC_PROC_BIND_unknown - 2:
4677 Values += " or ";
4678 break;
4679 case OMPC_PROC_BIND_unknown - 1:
4680 break;
4681 default:
4682 Values += Sep;
4683 break;
4684 }
4685 }
4686 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00004687 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004688 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004689 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004690 return new (Context)
4691 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004692}
4693
Alexey Bataev56dafe82014-06-20 07:16:17 +00004694OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
4695 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
4696 SourceLocation StartLoc, SourceLocation LParenLoc,
4697 SourceLocation ArgumentLoc, SourceLocation CommaLoc,
4698 SourceLocation EndLoc) {
4699 OMPClause *Res = nullptr;
4700 switch (Kind) {
4701 case OMPC_schedule:
4702 Res = ActOnOpenMPScheduleClause(
4703 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
4704 LParenLoc, ArgumentLoc, CommaLoc, EndLoc);
4705 break;
4706 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00004707 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00004708 case OMPC_num_threads:
4709 case OMPC_safelen:
4710 case OMPC_collapse:
4711 case OMPC_default:
4712 case OMPC_proc_bind:
4713 case OMPC_private:
4714 case OMPC_firstprivate:
4715 case OMPC_lastprivate:
4716 case OMPC_shared:
4717 case OMPC_reduction:
4718 case OMPC_linear:
4719 case OMPC_aligned:
4720 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00004721 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004722 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00004723 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00004724 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00004725 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00004726 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00004727 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004728 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00004729 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00004730 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00004731 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00004732 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00004733 case OMPC_depend:
Alexey Bataev56dafe82014-06-20 07:16:17 +00004734 case OMPC_unknown:
4735 llvm_unreachable("Clause is not allowed.");
4736 }
4737 return Res;
4738}
4739
4740OMPClause *Sema::ActOnOpenMPScheduleClause(
4741 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
4742 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
4743 SourceLocation EndLoc) {
4744 if (Kind == OMPC_SCHEDULE_unknown) {
4745 std::string Values;
4746 std::string Sep(", ");
4747 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) {
4748 Values += "'";
4749 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i);
4750 Values += "'";
4751 switch (i) {
4752 case OMPC_SCHEDULE_unknown - 2:
4753 Values += " or ";
4754 break;
4755 case OMPC_SCHEDULE_unknown - 1:
4756 break;
4757 default:
4758 Values += Sep;
4759 break;
4760 }
4761 }
4762 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
4763 << Values << getOpenMPClauseName(OMPC_schedule);
4764 return nullptr;
4765 }
4766 Expr *ValExpr = ChunkSize;
Alexey Bataev040d5402015-05-12 08:35:28 +00004767 Expr *HelperValExpr = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00004768 if (ChunkSize) {
4769 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
4770 !ChunkSize->isInstantiationDependent() &&
4771 !ChunkSize->containsUnexpandedParameterPack()) {
4772 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
4773 ExprResult Val =
4774 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
4775 if (Val.isInvalid())
4776 return nullptr;
4777
4778 ValExpr = Val.get();
4779
4780 // OpenMP [2.7.1, Restrictions]
4781 // chunk_size must be a loop invariant integer expression with a positive
4782 // value.
4783 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00004784 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
4785 if (Result.isSigned() && !Result.isStrictlyPositive()) {
4786 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
4787 << "schedule" << ChunkSize->getSourceRange();
4788 return nullptr;
4789 }
4790 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
4791 auto *ImpVar = buildVarDecl(*this, ChunkSize->getExprLoc(),
4792 ChunkSize->getType(), ".chunk.");
4793 auto *ImpVarRef = buildDeclRefExpr(*this, ImpVar, ChunkSize->getType(),
4794 ChunkSize->getExprLoc(),
4795 /*RefersToCapture=*/true);
4796 HelperValExpr = ImpVarRef;
Alexey Bataev56dafe82014-06-20 07:16:17 +00004797 }
4798 }
4799 }
4800
4801 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc,
Alexey Bataev040d5402015-05-12 08:35:28 +00004802 EndLoc, Kind, ValExpr, HelperValExpr);
Alexey Bataev56dafe82014-06-20 07:16:17 +00004803}
4804
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004805OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
4806 SourceLocation StartLoc,
4807 SourceLocation EndLoc) {
4808 OMPClause *Res = nullptr;
4809 switch (Kind) {
4810 case OMPC_ordered:
4811 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
4812 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00004813 case OMPC_nowait:
4814 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
4815 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00004816 case OMPC_untied:
4817 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
4818 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00004819 case OMPC_mergeable:
4820 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
4821 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004822 case OMPC_read:
4823 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
4824 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00004825 case OMPC_write:
4826 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
4827 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00004828 case OMPC_update:
4829 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
4830 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00004831 case OMPC_capture:
4832 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
4833 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00004834 case OMPC_seq_cst:
4835 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
4836 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004837 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00004838 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004839 case OMPC_num_threads:
4840 case OMPC_safelen:
4841 case OMPC_collapse:
4842 case OMPC_schedule:
4843 case OMPC_private:
4844 case OMPC_firstprivate:
4845 case OMPC_lastprivate:
4846 case OMPC_shared:
4847 case OMPC_reduction:
4848 case OMPC_linear:
4849 case OMPC_aligned:
4850 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00004851 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004852 case OMPC_default:
4853 case OMPC_proc_bind:
4854 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00004855 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00004856 case OMPC_depend:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004857 case OMPC_unknown:
4858 llvm_unreachable("Clause is not allowed.");
4859 }
4860 return Res;
4861}
4862
4863OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
4864 SourceLocation EndLoc) {
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004865 DSAStack->setOrderedRegion();
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004866 return new (Context) OMPOrderedClause(StartLoc, EndLoc);
4867}
4868
Alexey Bataev236070f2014-06-20 11:19:47 +00004869OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
4870 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00004871 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00004872 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
4873}
4874
Alexey Bataev7aea99a2014-07-17 12:19:31 +00004875OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
4876 SourceLocation EndLoc) {
4877 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
4878}
4879
Alexey Bataev74ba3a52014-07-17 12:47:03 +00004880OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
4881 SourceLocation EndLoc) {
4882 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
4883}
4884
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004885OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
4886 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004887 return new (Context) OMPReadClause(StartLoc, EndLoc);
4888}
4889
Alexey Bataevdea47612014-07-23 07:46:59 +00004890OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
4891 SourceLocation EndLoc) {
4892 return new (Context) OMPWriteClause(StartLoc, EndLoc);
4893}
4894
Alexey Bataev67a4f222014-07-23 10:25:33 +00004895OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
4896 SourceLocation EndLoc) {
4897 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
4898}
4899
Alexey Bataev459dec02014-07-24 06:46:57 +00004900OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
4901 SourceLocation EndLoc) {
4902 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
4903}
4904
Alexey Bataev82bad8b2014-07-24 08:55:34 +00004905OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
4906 SourceLocation EndLoc) {
4907 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
4908}
4909
Alexey Bataevc5e02582014-06-16 07:08:35 +00004910OMPClause *Sema::ActOnOpenMPVarListClause(
4911 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
4912 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
4913 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00004914 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
4915 SourceLocation DepLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004916 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004917 switch (Kind) {
4918 case OMPC_private:
4919 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
4920 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004921 case OMPC_firstprivate:
4922 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
4923 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00004924 case OMPC_lastprivate:
4925 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
4926 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00004927 case OMPC_shared:
4928 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
4929 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00004930 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00004931 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
4932 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00004933 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00004934 case OMPC_linear:
4935 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
4936 ColonLoc, EndLoc);
4937 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00004938 case OMPC_aligned:
4939 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
4940 ColonLoc, EndLoc);
4941 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004942 case OMPC_copyin:
4943 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
4944 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00004945 case OMPC_copyprivate:
4946 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
4947 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00004948 case OMPC_flush:
4949 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
4950 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00004951 case OMPC_depend:
4952 Res = ActOnOpenMPDependClause(DepKind, DepLoc, ColonLoc, VarList, StartLoc,
4953 LParenLoc, EndLoc);
4954 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004955 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00004956 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00004957 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00004958 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00004959 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004960 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004961 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00004962 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004963 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00004964 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00004965 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00004966 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004967 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004968 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00004969 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00004970 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00004971 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00004972 case OMPC_seq_cst:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004973 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004974 llvm_unreachable("Clause is not allowed.");
4975 }
4976 return Res;
4977}
4978
4979OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
4980 SourceLocation StartLoc,
4981 SourceLocation LParenLoc,
4982 SourceLocation EndLoc) {
4983 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00004984 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00004985 for (auto &RefExpr : VarList) {
4986 assert(RefExpr && "NULL expr in OpenMP private clause.");
4987 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004988 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00004989 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00004990 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004991 continue;
4992 }
4993
Alexey Bataeved09d242014-05-28 05:53:51 +00004994 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004995 // OpenMP [2.1, C/C++]
4996 // A list item is a variable name.
4997 // OpenMP [2.9.3.3, Restrictions, p.1]
4998 // A variable that is part of another variable (as an array or
4999 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00005000 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005001 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005002 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005003 continue;
5004 }
5005 Decl *D = DE->getDecl();
5006 VarDecl *VD = cast<VarDecl>(D);
5007
5008 QualType Type = VD->getType();
5009 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5010 // It will be analyzed later.
5011 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00005012 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005013 continue;
5014 }
5015
5016 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
5017 // A variable that appears in a private clause must not have an incomplete
5018 // type or a reference type.
5019 if (RequireCompleteType(ELoc, Type,
5020 diag::err_omp_private_incomplete_type)) {
5021 continue;
5022 }
5023 if (Type->isReferenceType()) {
5024 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00005025 << getOpenMPClauseName(OMPC_private) << Type;
5026 bool IsDecl =
5027 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5028 Diag(VD->getLocation(),
5029 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5030 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005031 continue;
5032 }
5033
Alexey Bataev758e55e2013-09-06 18:03:48 +00005034 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
5035 // in a Construct]
5036 // Variables with the predetermined data-sharing attributes may not be
5037 // listed in data-sharing attributes clauses, except for the cases
5038 // listed below. For these exceptions only, listing a predetermined
5039 // variable in a data-sharing attribute clause is allowed and overrides
5040 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005041 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005042 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005043 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
5044 << getOpenMPClauseName(OMPC_private);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005045 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005046 continue;
5047 }
5048
Alexey Bataevccb59ec2015-05-19 08:44:56 +00005049 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00005050 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00005051 DSAStack->getCurrentDirective() == OMPD_task) {
5052 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
5053 << getOpenMPClauseName(OMPC_private) << Type
5054 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
5055 bool IsDecl =
5056 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5057 Diag(VD->getLocation(),
5058 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5059 << VD;
5060 continue;
5061 }
5062
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005063 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
5064 // A variable of class type (or array thereof) that appears in a private
5065 // clause requires an accessible, unambiguous default constructor for the
5066 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00005067 // Generate helper private variable and initialize it with the default
5068 // value. The address of the original variable is replaced by the address of
5069 // the new private variable in CodeGen. This new variable is not added to
5070 // IdResolver, so the code in the OpenMP region uses original variable for
5071 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005072 Type = Type.getUnqualifiedType();
5073 auto VDPrivate = buildVarDecl(*this, DE->getExprLoc(), Type, VD->getName());
Alexey Bataev39f915b82015-05-08 10:41:21 +00005074 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00005075 if (VDPrivate->isInvalidDecl())
5076 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005077 auto VDPrivateRefExpr = buildDeclRefExpr(
5078 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev03b340a2014-10-21 03:16:40 +00005079
Alexey Bataev758e55e2013-09-06 18:03:48 +00005080 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005081 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00005082 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005083 }
5084
Alexey Bataeved09d242014-05-28 05:53:51 +00005085 if (Vars.empty())
5086 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005087
Alexey Bataev03b340a2014-10-21 03:16:40 +00005088 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
5089 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005090}
5091
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005092namespace {
5093class DiagsUninitializedSeveretyRAII {
5094private:
5095 DiagnosticsEngine &Diags;
5096 SourceLocation SavedLoc;
5097 bool IsIgnored;
5098
5099public:
5100 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
5101 bool IsIgnored)
5102 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
5103 if (!IsIgnored) {
5104 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
5105 /*Map*/ diag::Severity::Ignored, Loc);
5106 }
5107 }
5108 ~DiagsUninitializedSeveretyRAII() {
5109 if (!IsIgnored)
5110 Diags.popMappings(SavedLoc);
5111 }
5112};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00005113}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005114
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005115OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
5116 SourceLocation StartLoc,
5117 SourceLocation LParenLoc,
5118 SourceLocation EndLoc) {
5119 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005120 SmallVector<Expr *, 8> PrivateCopies;
5121 SmallVector<Expr *, 8> Inits;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005122 bool IsImplicitClause =
5123 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
5124 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
5125
Alexey Bataeved09d242014-05-28 05:53:51 +00005126 for (auto &RefExpr : VarList) {
5127 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
5128 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005129 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00005130 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005131 PrivateCopies.push_back(nullptr);
5132 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005133 continue;
5134 }
5135
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005136 SourceLocation ELoc =
5137 IsImplicitClause ? ImplicitClauseLoc : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005138 // OpenMP [2.1, C/C++]
5139 // A list item is a variable name.
5140 // OpenMP [2.9.3.3, Restrictions, p.1]
5141 // A variable that is part of another variable (as an array or
5142 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00005143 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005144 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005145 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005146 continue;
5147 }
5148 Decl *D = DE->getDecl();
5149 VarDecl *VD = cast<VarDecl>(D);
5150
5151 QualType Type = VD->getType();
5152 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5153 // It will be analyzed later.
5154 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005155 PrivateCopies.push_back(nullptr);
5156 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005157 continue;
5158 }
5159
5160 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
5161 // A variable that appears in a private clause must not have an incomplete
5162 // type or a reference type.
5163 if (RequireCompleteType(ELoc, Type,
5164 diag::err_omp_firstprivate_incomplete_type)) {
5165 continue;
5166 }
5167 if (Type->isReferenceType()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005168 if (IsImplicitClause) {
5169 Diag(ImplicitClauseLoc,
5170 diag::err_omp_task_predetermined_firstprivate_ref_type_arg)
5171 << Type;
5172 Diag(RefExpr->getExprLoc(), diag::note_used_here);
5173 } else {
5174 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
5175 << getOpenMPClauseName(OMPC_firstprivate) << Type;
5176 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005177 bool IsDecl =
5178 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5179 Diag(VD->getLocation(),
5180 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5181 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005182 continue;
5183 }
5184
5185 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
5186 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00005187 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005188 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005189 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005190
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005191 // If an implicit firstprivate variable found it was checked already.
5192 if (!IsImplicitClause) {
5193 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005194 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005195 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
5196 // A list item that specifies a given variable may not appear in more
5197 // than one clause on the same directive, except that a variable may be
5198 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005199 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00005200 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005201 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00005202 << getOpenMPClauseName(DVar.CKind)
5203 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005204 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005205 continue;
5206 }
5207
5208 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
5209 // in a Construct]
5210 // Variables with the predetermined data-sharing attributes may not be
5211 // listed in data-sharing attributes clauses, except for the cases
5212 // listed below. For these exceptions only, listing a predetermined
5213 // variable in a data-sharing attribute clause is allowed and overrides
5214 // the variable's predetermined data-sharing attributes.
5215 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
5216 // in a Construct, C/C++, p.2]
5217 // Variables with const-qualified type having no mutable member may be
5218 // listed in a firstprivate clause, even if they are static data members.
5219 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
5220 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
5221 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00005222 << getOpenMPClauseName(DVar.CKind)
5223 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005224 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005225 continue;
5226 }
5227
Alexey Bataevf29276e2014-06-18 04:14:57 +00005228 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005229 // OpenMP [2.9.3.4, Restrictions, p.2]
5230 // A list item that is private within a parallel region must not appear
5231 // in a firstprivate clause on a worksharing construct if any of the
5232 // worksharing regions arising from the worksharing construct ever bind
5233 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00005234 if (isOpenMPWorksharingDirective(CurrDir) &&
5235 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005236 DVar = DSAStack->getImplicitDSA(VD, true);
5237 if (DVar.CKind != OMPC_shared &&
5238 (isOpenMPParallelDirective(DVar.DKind) ||
5239 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00005240 Diag(ELoc, diag::err_omp_required_access)
5241 << getOpenMPClauseName(OMPC_firstprivate)
5242 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005243 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005244 continue;
5245 }
5246 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005247 // OpenMP [2.9.3.4, Restrictions, p.3]
5248 // A list item that appears in a reduction clause of a parallel construct
5249 // must not appear in a firstprivate clause on a worksharing or task
5250 // construct if any of the worksharing or task regions arising from the
5251 // worksharing or task construct ever bind to any of the parallel regions
5252 // arising from the parallel construct.
5253 // OpenMP [2.9.3.4, Restrictions, p.4]
5254 // A list item that appears in a reduction clause in worksharing
5255 // construct must not appear in a firstprivate clause in a task construct
5256 // encountered during execution of any of the worksharing regions arising
5257 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005258 if (CurrDir == OMPD_task) {
5259 DVar =
5260 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
5261 [](OpenMPDirectiveKind K) -> bool {
5262 return isOpenMPParallelDirective(K) ||
5263 isOpenMPWorksharingDirective(K);
5264 },
5265 false);
5266 if (DVar.CKind == OMPC_reduction &&
5267 (isOpenMPParallelDirective(DVar.DKind) ||
5268 isOpenMPWorksharingDirective(DVar.DKind))) {
5269 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
5270 << getOpenMPDirectiveName(DVar.DKind);
5271 ReportOriginalDSA(*this, DSAStack, VD, DVar);
5272 continue;
5273 }
5274 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005275 }
5276
Alexey Bataevccb59ec2015-05-19 08:44:56 +00005277 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00005278 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00005279 DSAStack->getCurrentDirective() == OMPD_task) {
5280 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
5281 << getOpenMPClauseName(OMPC_firstprivate) << Type
5282 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
5283 bool IsDecl =
5284 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5285 Diag(VD->getLocation(),
5286 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5287 << VD;
5288 continue;
5289 }
5290
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005291 Type = Type.getUnqualifiedType();
5292 auto VDPrivate = buildVarDecl(*this, ELoc, Type, VD->getName());
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005293 // Generate helper private variable and initialize it with the value of the
5294 // original variable. The address of the original variable is replaced by
5295 // the address of the new private variable in the CodeGen. This new variable
5296 // is not added to IdResolver, so the code in the OpenMP region uses
5297 // original variable for proper diagnostics and variable capturing.
5298 Expr *VDInitRefExpr = nullptr;
5299 // For arrays generate initializer for single element and replace it by the
5300 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005301 if (Type->isArrayType()) {
5302 auto VDInit =
5303 buildVarDecl(*this, DE->getExprLoc(), ElemType, VD->getName());
5304 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005305 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005306 ElemType = ElemType.getUnqualifiedType();
5307 auto *VDInitTemp = buildVarDecl(*this, DE->getLocStart(), ElemType,
5308 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00005309 InitializedEntity Entity =
5310 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005311 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
5312
5313 InitializationSequence InitSeq(*this, Entity, Kind, Init);
5314 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
5315 if (Result.isInvalid())
5316 VDPrivate->setInvalidDecl();
5317 else
5318 VDPrivate->setInit(Result.getAs<Expr>());
5319 } else {
Alexey Bataev69c62a92015-04-15 04:52:20 +00005320 auto *VDInit =
Alexey Bataev39f915b82015-05-08 10:41:21 +00005321 buildVarDecl(*this, DE->getLocStart(), Type, ".firstprivate.temp");
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005322 VDInitRefExpr =
5323 buildDeclRefExpr(*this, VDInit, DE->getType(), DE->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00005324 AddInitializerToDecl(VDPrivate,
5325 DefaultLvalueConversion(VDInitRefExpr).get(),
5326 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005327 }
5328 if (VDPrivate->isInvalidDecl()) {
5329 if (IsImplicitClause) {
5330 Diag(DE->getExprLoc(),
5331 diag::note_omp_task_predetermined_firstprivate_here);
5332 }
5333 continue;
5334 }
5335 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00005336 auto VDPrivateRefExpr = buildDeclRefExpr(
5337 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005338 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
5339 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005340 PrivateCopies.push_back(VDPrivateRefExpr);
5341 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005342 }
5343
Alexey Bataeved09d242014-05-28 05:53:51 +00005344 if (Vars.empty())
5345 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005346
5347 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005348 Vars, PrivateCopies, Inits);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005349}
5350
Alexander Musman1bb328c2014-06-04 13:06:39 +00005351OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
5352 SourceLocation StartLoc,
5353 SourceLocation LParenLoc,
5354 SourceLocation EndLoc) {
5355 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00005356 SmallVector<Expr *, 8> SrcExprs;
5357 SmallVector<Expr *, 8> DstExprs;
5358 SmallVector<Expr *, 8> AssignmentOps;
Alexander Musman1bb328c2014-06-04 13:06:39 +00005359 for (auto &RefExpr : VarList) {
5360 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
5361 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
5362 // It will be analyzed later.
5363 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00005364 SrcExprs.push_back(nullptr);
5365 DstExprs.push_back(nullptr);
5366 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005367 continue;
5368 }
5369
5370 SourceLocation ELoc = RefExpr->getExprLoc();
5371 // OpenMP [2.1, C/C++]
5372 // A list item is a variable name.
5373 // OpenMP [2.14.3.5, Restrictions, p.1]
5374 // A variable that is part of another variable (as an array or structure
5375 // element) cannot appear in a lastprivate clause.
5376 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
5377 if (!DE || !isa<VarDecl>(DE->getDecl())) {
5378 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
5379 continue;
5380 }
5381 Decl *D = DE->getDecl();
5382 VarDecl *VD = cast<VarDecl>(D);
5383
5384 QualType Type = VD->getType();
5385 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5386 // It will be analyzed later.
5387 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00005388 SrcExprs.push_back(nullptr);
5389 DstExprs.push_back(nullptr);
5390 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005391 continue;
5392 }
5393
5394 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
5395 // A variable that appears in a lastprivate clause must not have an
5396 // incomplete type or a reference type.
5397 if (RequireCompleteType(ELoc, Type,
5398 diag::err_omp_lastprivate_incomplete_type)) {
5399 continue;
5400 }
5401 if (Type->isReferenceType()) {
5402 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
5403 << getOpenMPClauseName(OMPC_lastprivate) << Type;
5404 bool IsDecl =
5405 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5406 Diag(VD->getLocation(),
5407 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5408 << VD;
5409 continue;
5410 }
5411
5412 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
5413 // in a Construct]
5414 // Variables with the predetermined data-sharing attributes may not be
5415 // listed in data-sharing attributes clauses, except for the cases
5416 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005417 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005418 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
5419 DVar.CKind != OMPC_firstprivate &&
5420 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
5421 Diag(ELoc, diag::err_omp_wrong_dsa)
5422 << getOpenMPClauseName(DVar.CKind)
5423 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005424 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005425 continue;
5426 }
5427
Alexey Bataevf29276e2014-06-18 04:14:57 +00005428 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
5429 // OpenMP [2.14.3.5, Restrictions, p.2]
5430 // A list item that is private within a parallel region, or that appears in
5431 // the reduction clause of a parallel construct, must not appear in a
5432 // lastprivate clause on a worksharing construct if any of the corresponding
5433 // worksharing regions ever binds to any of the corresponding parallel
5434 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005435 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00005436 if (isOpenMPWorksharingDirective(CurrDir) &&
5437 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005438 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005439 if (DVar.CKind != OMPC_shared) {
5440 Diag(ELoc, diag::err_omp_required_access)
5441 << getOpenMPClauseName(OMPC_lastprivate)
5442 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005443 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005444 continue;
5445 }
5446 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00005447 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00005448 // A variable of class type (or array thereof) that appears in a
5449 // lastprivate clause requires an accessible, unambiguous default
5450 // constructor for the class type, unless the list item is also specified
5451 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00005452 // A variable of class type (or array thereof) that appears in a
5453 // lastprivate clause requires an accessible, unambiguous copy assignment
5454 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00005455 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00005456 auto *SrcVD = buildVarDecl(*this, DE->getLocStart(),
Alexey Bataev38e89532015-04-16 04:54:05 +00005457 Type.getUnqualifiedType(), ".lastprivate.src");
Alexey Bataev39f915b82015-05-08 10:41:21 +00005458 auto *PseudoSrcExpr = buildDeclRefExpr(
5459 *this, SrcVD, Type.getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00005460 auto *DstVD =
Alexey Bataev39f915b82015-05-08 10:41:21 +00005461 buildVarDecl(*this, DE->getLocStart(), Type, ".lastprivate.dst");
Alexey Bataev38e89532015-04-16 04:54:05 +00005462 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00005463 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00005464 // For arrays generate assignment operation for single element and replace
5465 // it by the original array element in CodeGen.
5466 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
5467 PseudoDstExpr, PseudoSrcExpr);
5468 if (AssignmentOp.isInvalid())
5469 continue;
5470 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
5471 /*DiscardedValue=*/true);
5472 if (AssignmentOp.isInvalid())
5473 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00005474
Alexey Bataev39f915b82015-05-08 10:41:21 +00005475 if (TopDVar.CKind != OMPC_firstprivate)
Alexey Bataevf29276e2014-06-18 04:14:57 +00005476 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005477 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00005478 SrcExprs.push_back(PseudoSrcExpr);
5479 DstExprs.push_back(PseudoDstExpr);
5480 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00005481 }
5482
5483 if (Vars.empty())
5484 return nullptr;
5485
5486 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00005487 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005488}
5489
Alexey Bataev758e55e2013-09-06 18:03:48 +00005490OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
5491 SourceLocation StartLoc,
5492 SourceLocation LParenLoc,
5493 SourceLocation EndLoc) {
5494 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00005495 for (auto &RefExpr : VarList) {
5496 assert(RefExpr && "NULL expr in OpenMP shared clause.");
5497 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00005498 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00005499 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005500 continue;
5501 }
5502
Alexey Bataeved09d242014-05-28 05:53:51 +00005503 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00005504 // OpenMP [2.1, C/C++]
5505 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00005506 // OpenMP [2.14.3.2, Restrictions, p.1]
5507 // A variable that is part of another variable (as an array or structure
5508 // element) cannot appear in a shared unless it is a static data member
5509 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00005510 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005511 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005512 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00005513 continue;
5514 }
5515 Decl *D = DE->getDecl();
5516 VarDecl *VD = cast<VarDecl>(D);
5517
5518 QualType Type = VD->getType();
5519 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5520 // It will be analyzed later.
5521 Vars.push_back(DE);
5522 continue;
5523 }
5524
5525 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
5526 // in a Construct]
5527 // Variables with the predetermined data-sharing attributes may not be
5528 // listed in data-sharing attributes clauses, except for the cases
5529 // listed below. For these exceptions only, listing a predetermined
5530 // variable in a data-sharing attribute clause is allowed and overrides
5531 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005532 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00005533 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
5534 DVar.RefExpr) {
5535 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
5536 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005537 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005538 continue;
5539 }
5540
5541 DSAStack->addDSA(VD, DE, OMPC_shared);
5542 Vars.push_back(DE);
5543 }
5544
Alexey Bataeved09d242014-05-28 05:53:51 +00005545 if (Vars.empty())
5546 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00005547
5548 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
5549}
5550
Alexey Bataevc5e02582014-06-16 07:08:35 +00005551namespace {
5552class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
5553 DSAStackTy *Stack;
5554
5555public:
5556 bool VisitDeclRefExpr(DeclRefExpr *E) {
5557 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005558 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00005559 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
5560 return false;
5561 if (DVar.CKind != OMPC_unknown)
5562 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00005563 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005564 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005565 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00005566 return true;
5567 return false;
5568 }
5569 return false;
5570 }
5571 bool VisitStmt(Stmt *S) {
5572 for (auto Child : S->children()) {
5573 if (Child && Visit(Child))
5574 return true;
5575 }
5576 return false;
5577 }
Alexey Bataev23b69422014-06-18 07:08:49 +00005578 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00005579};
Alexey Bataev23b69422014-06-18 07:08:49 +00005580} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00005581
5582OMPClause *Sema::ActOnOpenMPReductionClause(
5583 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
5584 SourceLocation ColonLoc, SourceLocation EndLoc,
5585 CXXScopeSpec &ReductionIdScopeSpec,
5586 const DeclarationNameInfo &ReductionId) {
5587 // TODO: Allow scope specification search when 'declare reduction' is
5588 // supported.
5589 assert(ReductionIdScopeSpec.isEmpty() &&
5590 "No support for scoped reduction identifiers yet.");
5591
5592 auto DN = ReductionId.getName();
5593 auto OOK = DN.getCXXOverloadedOperator();
5594 BinaryOperatorKind BOK = BO_Comma;
5595
5596 // OpenMP [2.14.3.6, reduction clause]
5597 // C
5598 // reduction-identifier is either an identifier or one of the following
5599 // operators: +, -, *, &, |, ^, && and ||
5600 // C++
5601 // reduction-identifier is either an id-expression or one of the following
5602 // operators: +, -, *, &, |, ^, && and ||
5603 // FIXME: Only 'min' and 'max' identifiers are supported for now.
5604 switch (OOK) {
5605 case OO_Plus:
5606 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005607 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005608 break;
5609 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005610 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005611 break;
5612 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005613 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005614 break;
5615 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005616 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005617 break;
5618 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005619 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005620 break;
5621 case OO_AmpAmp:
5622 BOK = BO_LAnd;
5623 break;
5624 case OO_PipePipe:
5625 BOK = BO_LOr;
5626 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005627 case OO_New:
5628 case OO_Delete:
5629 case OO_Array_New:
5630 case OO_Array_Delete:
5631 case OO_Slash:
5632 case OO_Percent:
5633 case OO_Tilde:
5634 case OO_Exclaim:
5635 case OO_Equal:
5636 case OO_Less:
5637 case OO_Greater:
5638 case OO_LessEqual:
5639 case OO_GreaterEqual:
5640 case OO_PlusEqual:
5641 case OO_MinusEqual:
5642 case OO_StarEqual:
5643 case OO_SlashEqual:
5644 case OO_PercentEqual:
5645 case OO_CaretEqual:
5646 case OO_AmpEqual:
5647 case OO_PipeEqual:
5648 case OO_LessLess:
5649 case OO_GreaterGreater:
5650 case OO_LessLessEqual:
5651 case OO_GreaterGreaterEqual:
5652 case OO_EqualEqual:
5653 case OO_ExclaimEqual:
5654 case OO_PlusPlus:
5655 case OO_MinusMinus:
5656 case OO_Comma:
5657 case OO_ArrowStar:
5658 case OO_Arrow:
5659 case OO_Call:
5660 case OO_Subscript:
5661 case OO_Conditional:
5662 case NUM_OVERLOADED_OPERATORS:
5663 llvm_unreachable("Unexpected reduction identifier");
5664 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00005665 if (auto II = DN.getAsIdentifierInfo()) {
5666 if (II->isStr("max"))
5667 BOK = BO_GT;
5668 else if (II->isStr("min"))
5669 BOK = BO_LT;
5670 }
5671 break;
5672 }
5673 SourceRange ReductionIdRange;
5674 if (ReductionIdScopeSpec.isValid()) {
5675 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
5676 }
5677 ReductionIdRange.setEnd(ReductionId.getEndLoc());
5678 if (BOK == BO_Comma) {
5679 // Not allowed reduction identifier is found.
5680 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
5681 << ReductionIdRange;
5682 return nullptr;
5683 }
5684
5685 SmallVector<Expr *, 8> Vars;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005686 SmallVector<Expr *, 8> LHSs;
5687 SmallVector<Expr *, 8> RHSs;
5688 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005689 for (auto RefExpr : VarList) {
5690 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
5691 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
5692 // It will be analyzed later.
5693 Vars.push_back(RefExpr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005694 LHSs.push_back(nullptr);
5695 RHSs.push_back(nullptr);
5696 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00005697 continue;
5698 }
5699
5700 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
5701 RefExpr->isInstantiationDependent() ||
5702 RefExpr->containsUnexpandedParameterPack()) {
5703 // It will be analyzed later.
5704 Vars.push_back(RefExpr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005705 LHSs.push_back(nullptr);
5706 RHSs.push_back(nullptr);
5707 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00005708 continue;
5709 }
5710
5711 auto ELoc = RefExpr->getExprLoc();
5712 auto ERange = RefExpr->getSourceRange();
5713 // OpenMP [2.1, C/C++]
5714 // A list item is a variable or array section, subject to the restrictions
5715 // specified in Section 2.4 on page 42 and in each of the sections
5716 // describing clauses and directives for which a list appears.
5717 // OpenMP [2.14.3.3, Restrictions, p.1]
5718 // A variable that is part of another variable (as an array or
5719 // structure element) cannot appear in a private clause.
5720 auto DE = dyn_cast<DeclRefExpr>(RefExpr);
5721 if (!DE || !isa<VarDecl>(DE->getDecl())) {
5722 Diag(ELoc, diag::err_omp_expected_var_name) << ERange;
5723 continue;
5724 }
5725 auto D = DE->getDecl();
5726 auto VD = cast<VarDecl>(D);
5727 auto Type = VD->getType();
5728 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
5729 // A variable that appears in a private clause must not have an incomplete
5730 // type or a reference type.
5731 if (RequireCompleteType(ELoc, Type,
5732 diag::err_omp_reduction_incomplete_type))
5733 continue;
5734 // OpenMP [2.14.3.6, reduction clause, Restrictions]
5735 // Arrays may not appear in a reduction clause.
5736 if (Type.getNonReferenceType()->isArrayType()) {
5737 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
5738 bool IsDecl =
5739 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5740 Diag(VD->getLocation(),
5741 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5742 << VD;
5743 continue;
5744 }
5745 // OpenMP [2.14.3.6, reduction clause, Restrictions]
5746 // A list item that appears in a reduction clause must not be
5747 // const-qualified.
5748 if (Type.getNonReferenceType().isConstant(Context)) {
5749 Diag(ELoc, diag::err_omp_const_variable)
5750 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
5751 bool IsDecl =
5752 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5753 Diag(VD->getLocation(),
5754 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5755 << VD;
5756 continue;
5757 }
5758 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
5759 // If a list-item is a reference type then it must bind to the same object
5760 // for all threads of the team.
5761 VarDecl *VDDef = VD->getDefinition();
5762 if (Type->isReferenceType() && VDDef) {
5763 DSARefChecker Check(DSAStack);
5764 if (Check.Visit(VDDef->getInit())) {
5765 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
5766 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
5767 continue;
5768 }
5769 }
5770 // OpenMP [2.14.3.6, reduction clause, Restrictions]
5771 // The type of a list item that appears in a reduction clause must be valid
5772 // for the reduction-identifier. For a max or min reduction in C, the type
5773 // of the list item must be an allowed arithmetic data type: char, int,
5774 // float, double, or _Bool, possibly modified with long, short, signed, or
5775 // unsigned. For a max or min reduction in C++, the type of the list item
5776 // must be an allowed arithmetic data type: char, wchar_t, int, float,
5777 // double, or bool, possibly modified with long, short, signed, or unsigned.
5778 if ((BOK == BO_GT || BOK == BO_LT) &&
5779 !(Type->isScalarType() ||
5780 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
5781 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
5782 << getLangOpts().CPlusPlus;
5783 bool IsDecl =
5784 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5785 Diag(VD->getLocation(),
5786 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5787 << VD;
5788 continue;
5789 }
5790 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
5791 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
5792 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
5793 bool IsDecl =
5794 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5795 Diag(VD->getLocation(),
5796 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5797 << VD;
5798 continue;
5799 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00005800 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
5801 // in a Construct]
5802 // Variables with the predetermined data-sharing attributes may not be
5803 // listed in data-sharing attributes clauses, except for the cases
5804 // listed below. For these exceptions only, listing a predetermined
5805 // variable in a data-sharing attribute clause is allowed and overrides
5806 // the variable's predetermined data-sharing attributes.
5807 // OpenMP [2.14.3.6, Restrictions, p.3]
5808 // Any number of reduction clauses can be specified on the directive,
5809 // but a list item can appear only once in the reduction clauses for that
5810 // directive.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005811 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00005812 if (DVar.CKind == OMPC_reduction) {
5813 Diag(ELoc, diag::err_omp_once_referenced)
5814 << getOpenMPClauseName(OMPC_reduction);
5815 if (DVar.RefExpr) {
5816 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
5817 }
5818 } else if (DVar.CKind != OMPC_unknown) {
5819 Diag(ELoc, diag::err_omp_wrong_dsa)
5820 << getOpenMPClauseName(DVar.CKind)
5821 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005822 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00005823 continue;
5824 }
5825
5826 // OpenMP [2.14.3.6, Restrictions, p.1]
5827 // A list item that appears in a reduction clause of a worksharing
5828 // construct must be shared in the parallel regions to which any of the
5829 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf29276e2014-06-18 04:14:57 +00005830 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataev549210e2014-06-24 04:39:47 +00005831 if (isOpenMPWorksharingDirective(CurrDir) &&
5832 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005833 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005834 if (DVar.CKind != OMPC_shared) {
5835 Diag(ELoc, diag::err_omp_required_access)
5836 << getOpenMPClauseName(OMPC_reduction)
5837 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005838 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005839 continue;
5840 }
5841 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005842 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00005843 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs");
5844 auto *RHSVD = buildVarDecl(*this, ELoc, Type, VD->getName());
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005845 // Add initializer for private variable.
5846 Expr *Init = nullptr;
5847 switch (BOK) {
5848 case BO_Add:
5849 case BO_Xor:
5850 case BO_Or:
5851 case BO_LOr:
5852 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
5853 if (Type->isScalarType() || Type->isAnyComplexType()) {
5854 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00005855 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005856 break;
5857 case BO_Mul:
5858 case BO_LAnd:
5859 if (Type->isScalarType() || Type->isAnyComplexType()) {
5860 // '*' and '&&' reduction ops - initializer is '1'.
5861 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
5862 }
5863 break;
5864 case BO_And: {
5865 // '&' reduction op - initializer is '~0'.
5866 QualType OrigType = Type;
5867 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) {
5868 Type = ComplexTy->getElementType();
5869 }
5870 if (Type->isRealFloatingType()) {
5871 llvm::APFloat InitValue =
5872 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
5873 /*isIEEE=*/true);
5874 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
5875 Type, ELoc);
5876 } else if (Type->isScalarType()) {
5877 auto Size = Context.getTypeSize(Type);
5878 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
5879 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
5880 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
5881 }
5882 if (Init && OrigType->isAnyComplexType()) {
5883 // Init = 0xFFFF + 0xFFFFi;
5884 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
5885 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
5886 }
5887 Type = OrigType;
5888 break;
5889 }
5890 case BO_LT:
5891 case BO_GT: {
5892 // 'min' reduction op - initializer is 'Largest representable number in
5893 // the reduction list item type'.
5894 // 'max' reduction op - initializer is 'Least representable number in
5895 // the reduction list item type'.
5896 if (Type->isIntegerType() || Type->isPointerType()) {
5897 bool IsSigned = Type->hasSignedIntegerRepresentation();
5898 auto Size = Context.getTypeSize(Type);
5899 QualType IntTy =
5900 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
5901 llvm::APInt InitValue =
5902 (BOK != BO_LT)
5903 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
5904 : llvm::APInt::getMinValue(Size)
5905 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
5906 : llvm::APInt::getMaxValue(Size);
5907 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
5908 if (Type->isPointerType()) {
5909 // Cast to pointer type.
5910 auto CastExpr = BuildCStyleCastExpr(
5911 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
5912 SourceLocation(), Init);
5913 if (CastExpr.isInvalid())
5914 continue;
5915 Init = CastExpr.get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00005916 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005917 } else if (Type->isRealFloatingType()) {
5918 llvm::APFloat InitValue = llvm::APFloat::getLargest(
5919 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
5920 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
5921 Type, ELoc);
5922 }
5923 break;
5924 }
5925 case BO_PtrMemD:
5926 case BO_PtrMemI:
5927 case BO_MulAssign:
5928 case BO_Div:
5929 case BO_Rem:
5930 case BO_Sub:
5931 case BO_Shl:
5932 case BO_Shr:
5933 case BO_LE:
5934 case BO_GE:
5935 case BO_EQ:
5936 case BO_NE:
5937 case BO_AndAssign:
5938 case BO_XorAssign:
5939 case BO_OrAssign:
5940 case BO_Assign:
5941 case BO_AddAssign:
5942 case BO_SubAssign:
5943 case BO_DivAssign:
5944 case BO_RemAssign:
5945 case BO_ShlAssign:
5946 case BO_ShrAssign:
5947 case BO_Comma:
5948 llvm_unreachable("Unexpected reduction operation");
5949 }
5950 if (Init) {
5951 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
5952 /*TypeMayContainAuto=*/false);
5953 } else {
5954 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
5955 }
5956 if (!RHSVD->hasInit()) {
5957 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
5958 << ReductionIdRange;
5959 bool IsDecl =
5960 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5961 Diag(VD->getLocation(),
5962 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5963 << VD;
5964 continue;
5965 }
Alexey Bataev39f915b82015-05-08 10:41:21 +00005966 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
5967 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005968 ExprResult ReductionOp =
5969 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
5970 LHSDRE, RHSDRE);
5971 if (ReductionOp.isUsable()) {
Alexey Bataev69a47792015-05-07 03:54:03 +00005972 if (BOK != BO_LT && BOK != BO_GT) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005973 ReductionOp =
5974 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
5975 BO_Assign, LHSDRE, ReductionOp.get());
5976 } else {
5977 auto *ConditionalOp = new (Context) ConditionalOperator(
5978 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
5979 RHSDRE, Type, VK_LValue, OK_Ordinary);
5980 ReductionOp =
5981 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
5982 BO_Assign, LHSDRE, ConditionalOp);
5983 }
5984 if (ReductionOp.isUsable()) {
5985 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00005986 }
5987 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005988 if (ReductionOp.isInvalid())
5989 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005990
5991 DSAStack->addDSA(VD, DE, OMPC_reduction);
5992 Vars.push_back(DE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005993 LHSs.push_back(LHSDRE);
5994 RHSs.push_back(RHSDRE);
5995 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00005996 }
5997
5998 if (Vars.empty())
5999 return nullptr;
6000
6001 return OMPReductionClause::Create(
6002 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006003 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, LHSs,
6004 RHSs, ReductionOps);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006005}
6006
Alexander Musman8dba6642014-04-22 13:09:42 +00006007OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step,
6008 SourceLocation StartLoc,
6009 SourceLocation LParenLoc,
6010 SourceLocation ColonLoc,
6011 SourceLocation EndLoc) {
6012 SmallVector<Expr *, 8> Vars;
Alexander Musman3276a272015-03-21 10:12:56 +00006013 SmallVector<Expr *, 8> Inits;
Alexey Bataeved09d242014-05-28 05:53:51 +00006014 for (auto &RefExpr : VarList) {
6015 assert(RefExpr && "NULL expr in OpenMP linear clause.");
6016 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00006017 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006018 Vars.push_back(RefExpr);
Alexander Musman3276a272015-03-21 10:12:56 +00006019 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00006020 continue;
6021 }
6022
6023 // OpenMP [2.14.3.7, linear clause]
6024 // A list item that appears in a linear clause is subject to the private
6025 // clause semantics described in Section 2.14.3.3 on page 159 except as
6026 // noted. In addition, the value of the new list item on each iteration
6027 // of the associated loop(s) corresponds to the value of the original
6028 // list item before entering the construct plus the logical number of
6029 // the iteration times linear-step.
6030
Alexey Bataeved09d242014-05-28 05:53:51 +00006031 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00006032 // OpenMP [2.1, C/C++]
6033 // A list item is a variable name.
6034 // OpenMP [2.14.3.3, Restrictions, p.1]
6035 // A variable that is part of another variable (as an array or
6036 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00006037 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00006038 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006039 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00006040 continue;
6041 }
6042
6043 VarDecl *VD = cast<VarDecl>(DE->getDecl());
6044
6045 // OpenMP [2.14.3.7, linear clause]
6046 // A list-item cannot appear in more than one linear clause.
6047 // A list-item that appears in a linear clause cannot appear in any
6048 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006049 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00006050 if (DVar.RefExpr) {
6051 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
6052 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006053 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00006054 continue;
6055 }
6056
6057 QualType QType = VD->getType();
6058 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
6059 // It will be analyzed later.
6060 Vars.push_back(DE);
Alexander Musman3276a272015-03-21 10:12:56 +00006061 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00006062 continue;
6063 }
6064
6065 // A variable must not have an incomplete type or a reference type.
6066 if (RequireCompleteType(ELoc, QType,
6067 diag::err_omp_linear_incomplete_type)) {
6068 continue;
6069 }
6070 if (QType->isReferenceType()) {
6071 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
6072 << getOpenMPClauseName(OMPC_linear) << QType;
6073 bool IsDecl =
6074 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6075 Diag(VD->getLocation(),
6076 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6077 << VD;
6078 continue;
6079 }
6080
6081 // A list item must not be const-qualified.
6082 if (QType.isConstant(Context)) {
6083 Diag(ELoc, diag::err_omp_const_variable)
6084 << getOpenMPClauseName(OMPC_linear);
6085 bool IsDecl =
6086 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6087 Diag(VD->getLocation(),
6088 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6089 << VD;
6090 continue;
6091 }
6092
6093 // A list item must be of integral or pointer type.
6094 QType = QType.getUnqualifiedType().getCanonicalType();
6095 const Type *Ty = QType.getTypePtrOrNull();
6096 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
6097 !Ty->isPointerType())) {
6098 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
6099 bool IsDecl =
6100 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6101 Diag(VD->getLocation(),
6102 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6103 << VD;
6104 continue;
6105 }
6106
Alexander Musman3276a272015-03-21 10:12:56 +00006107 // Build var to save initial value.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006108 VarDecl *Init = buildVarDecl(*this, ELoc, QType, ".linear.start");
Alexander Musman3276a272015-03-21 10:12:56 +00006109 AddInitializerToDecl(Init, DefaultLvalueConversion(DE).get(),
6110 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006111 auto InitRef = buildDeclRefExpr(
6112 *this, Init, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman8dba6642014-04-22 13:09:42 +00006113 DSAStack->addDSA(VD, DE, OMPC_linear);
6114 Vars.push_back(DE);
Alexander Musman3276a272015-03-21 10:12:56 +00006115 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00006116 }
6117
6118 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006119 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00006120
6121 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00006122 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00006123 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
6124 !Step->isInstantiationDependent() &&
6125 !Step->containsUnexpandedParameterPack()) {
6126 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006127 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00006128 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006129 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006130 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00006131
Alexander Musman3276a272015-03-21 10:12:56 +00006132 // Build var to save the step value.
6133 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006134 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00006135 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006136 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00006137 ExprResult CalcStep =
6138 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
6139
Alexander Musman8dba6642014-04-22 13:09:42 +00006140 // Warn about zero linear step (it would be probably better specified as
6141 // making corresponding variables 'const').
6142 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00006143 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
6144 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00006145 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
6146 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00006147 if (!IsConstant && CalcStep.isUsable()) {
6148 // Calculate the step beforehand instead of doing this on each iteration.
6149 // (This is not used if the number of iterations may be kfold-ed).
6150 CalcStepExpr = CalcStep.get();
6151 }
Alexander Musman8dba6642014-04-22 13:09:42 +00006152 }
6153
6154 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc,
Alexander Musman3276a272015-03-21 10:12:56 +00006155 Vars, Inits, StepExpr, CalcStepExpr);
6156}
6157
6158static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
6159 Expr *NumIterations, Sema &SemaRef,
6160 Scope *S) {
6161 // Walk the vars and build update/final expressions for the CodeGen.
6162 SmallVector<Expr *, 8> Updates;
6163 SmallVector<Expr *, 8> Finals;
6164 Expr *Step = Clause.getStep();
6165 Expr *CalcStep = Clause.getCalcStep();
6166 // OpenMP [2.14.3.7, linear clause]
6167 // If linear-step is not specified it is assumed to be 1.
6168 if (Step == nullptr)
6169 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
6170 else if (CalcStep)
6171 Step = cast<BinaryOperator>(CalcStep)->getLHS();
6172 bool HasErrors = false;
6173 auto CurInit = Clause.inits().begin();
6174 for (auto &RefExpr : Clause.varlists()) {
6175 Expr *InitExpr = *CurInit;
6176
6177 // Build privatized reference to the current linear var.
6178 auto DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00006179 auto PrivateRef =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006180 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
6181 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
6182 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00006183
6184 // Build update: Var = InitExpr + IV * Step
6185 ExprResult Update =
6186 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), PrivateRef,
6187 InitExpr, IV, Step, /* Subtract */ false);
6188 Update = SemaRef.ActOnFinishFullExpr(Update.get());
6189
6190 // Build final: Var = InitExpr + NumIterations * Step
6191 ExprResult Final =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006192 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), PrivateRef,
6193 InitExpr, NumIterations, Step, /* Subtract */ false);
Alexander Musman3276a272015-03-21 10:12:56 +00006194 Final = SemaRef.ActOnFinishFullExpr(Final.get());
6195 if (!Update.isUsable() || !Final.isUsable()) {
6196 Updates.push_back(nullptr);
6197 Finals.push_back(nullptr);
6198 HasErrors = true;
6199 } else {
6200 Updates.push_back(Update.get());
6201 Finals.push_back(Final.get());
6202 }
6203 ++CurInit;
6204 }
6205 Clause.setUpdates(Updates);
6206 Clause.setFinals(Finals);
6207 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00006208}
6209
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006210OMPClause *Sema::ActOnOpenMPAlignedClause(
6211 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
6212 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
6213
6214 SmallVector<Expr *, 8> Vars;
6215 for (auto &RefExpr : VarList) {
6216 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
6217 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6218 // It will be analyzed later.
6219 Vars.push_back(RefExpr);
6220 continue;
6221 }
6222
6223 SourceLocation ELoc = RefExpr->getExprLoc();
6224 // OpenMP [2.1, C/C++]
6225 // A list item is a variable name.
6226 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
6227 if (!DE || !isa<VarDecl>(DE->getDecl())) {
6228 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
6229 continue;
6230 }
6231
6232 VarDecl *VD = cast<VarDecl>(DE->getDecl());
6233
6234 // OpenMP [2.8.1, simd construct, Restrictions]
6235 // The type of list items appearing in the aligned clause must be
6236 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006237 QualType QType = VD->getType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006238 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006239 const Type *Ty = QType.getTypePtrOrNull();
6240 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
6241 !Ty->isPointerType())) {
6242 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
6243 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
6244 bool IsDecl =
6245 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6246 Diag(VD->getLocation(),
6247 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6248 << VD;
6249 continue;
6250 }
6251
6252 // OpenMP [2.8.1, simd construct, Restrictions]
6253 // A list-item cannot appear in more than one aligned clause.
6254 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
6255 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
6256 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
6257 << getOpenMPClauseName(OMPC_aligned);
6258 continue;
6259 }
6260
6261 Vars.push_back(DE);
6262 }
6263
6264 // OpenMP [2.8.1, simd construct, Description]
6265 // The parameter of the aligned clause, alignment, must be a constant
6266 // positive integer expression.
6267 // If no optional parameter is specified, implementation-defined default
6268 // alignments for SIMD instructions on the target platforms are assumed.
6269 if (Alignment != nullptr) {
6270 ExprResult AlignResult =
6271 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
6272 if (AlignResult.isInvalid())
6273 return nullptr;
6274 Alignment = AlignResult.get();
6275 }
6276 if (Vars.empty())
6277 return nullptr;
6278
6279 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
6280 EndLoc, Vars, Alignment);
6281}
6282
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006283OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
6284 SourceLocation StartLoc,
6285 SourceLocation LParenLoc,
6286 SourceLocation EndLoc) {
6287 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006288 SmallVector<Expr *, 8> SrcExprs;
6289 SmallVector<Expr *, 8> DstExprs;
6290 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00006291 for (auto &RefExpr : VarList) {
6292 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
6293 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006294 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006295 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006296 SrcExprs.push_back(nullptr);
6297 DstExprs.push_back(nullptr);
6298 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006299 continue;
6300 }
6301
Alexey Bataeved09d242014-05-28 05:53:51 +00006302 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006303 // OpenMP [2.1, C/C++]
6304 // A list item is a variable name.
6305 // OpenMP [2.14.4.1, Restrictions, p.1]
6306 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00006307 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006308 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006309 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006310 continue;
6311 }
6312
6313 Decl *D = DE->getDecl();
6314 VarDecl *VD = cast<VarDecl>(D);
6315
6316 QualType Type = VD->getType();
6317 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6318 // It will be analyzed later.
6319 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006320 SrcExprs.push_back(nullptr);
6321 DstExprs.push_back(nullptr);
6322 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006323 continue;
6324 }
6325
6326 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
6327 // A list item that appears in a copyin clause must be threadprivate.
6328 if (!DSAStack->isThreadPrivate(VD)) {
6329 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00006330 << getOpenMPClauseName(OMPC_copyin)
6331 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006332 continue;
6333 }
6334
6335 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
6336 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00006337 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006338 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006339 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00006340 auto *SrcVD = buildVarDecl(*this, DE->getLocStart(),
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006341 ElemType.getUnqualifiedType(), ".copyin.src");
Alexey Bataev39f915b82015-05-08 10:41:21 +00006342 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006343 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
6344 auto *DstVD =
6345 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst");
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006346 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006347 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006348 // For arrays generate assignment operation for single element and replace
6349 // it by the original array element in CodeGen.
6350 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
6351 PseudoDstExpr, PseudoSrcExpr);
6352 if (AssignmentOp.isInvalid())
6353 continue;
6354 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
6355 /*DiscardedValue=*/true);
6356 if (AssignmentOp.isInvalid())
6357 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006358
6359 DSAStack->addDSA(VD, DE, OMPC_copyin);
6360 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006361 SrcExprs.push_back(PseudoSrcExpr);
6362 DstExprs.push_back(PseudoDstExpr);
6363 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006364 }
6365
Alexey Bataeved09d242014-05-28 05:53:51 +00006366 if (Vars.empty())
6367 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006368
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006369 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
6370 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006371}
6372
Alexey Bataevbae9a792014-06-27 10:37:06 +00006373OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
6374 SourceLocation StartLoc,
6375 SourceLocation LParenLoc,
6376 SourceLocation EndLoc) {
6377 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00006378 SmallVector<Expr *, 8> SrcExprs;
6379 SmallVector<Expr *, 8> DstExprs;
6380 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00006381 for (auto &RefExpr : VarList) {
6382 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
6383 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6384 // It will be analyzed later.
6385 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00006386 SrcExprs.push_back(nullptr);
6387 DstExprs.push_back(nullptr);
6388 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00006389 continue;
6390 }
6391
6392 SourceLocation ELoc = RefExpr->getExprLoc();
6393 // OpenMP [2.1, C/C++]
6394 // A list item is a variable name.
6395 // OpenMP [2.14.4.1, Restrictions, p.1]
6396 // A list item that appears in a copyin clause must be threadprivate.
6397 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
6398 if (!DE || !isa<VarDecl>(DE->getDecl())) {
6399 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
6400 continue;
6401 }
6402
6403 Decl *D = DE->getDecl();
6404 VarDecl *VD = cast<VarDecl>(D);
6405
6406 QualType Type = VD->getType();
6407 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6408 // It will be analyzed later.
6409 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00006410 SrcExprs.push_back(nullptr);
6411 DstExprs.push_back(nullptr);
6412 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00006413 continue;
6414 }
6415
6416 // OpenMP [2.14.4.2, Restrictions, p.2]
6417 // A list item that appears in a copyprivate clause may not appear in a
6418 // private or firstprivate clause on the single construct.
6419 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006420 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00006421 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
6422 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00006423 Diag(ELoc, diag::err_omp_wrong_dsa)
6424 << getOpenMPClauseName(DVar.CKind)
6425 << getOpenMPClauseName(OMPC_copyprivate);
6426 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6427 continue;
6428 }
6429
6430 // OpenMP [2.11.4.2, Restrictions, p.1]
6431 // All list items that appear in a copyprivate clause must be either
6432 // threadprivate or private in the enclosing context.
6433 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006434 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00006435 if (DVar.CKind == OMPC_shared) {
6436 Diag(ELoc, diag::err_omp_required_access)
6437 << getOpenMPClauseName(OMPC_copyprivate)
6438 << "threadprivate or private in the enclosing context";
6439 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6440 continue;
6441 }
6442 }
6443 }
6444
Alexey Bataev7a3e5852015-05-19 08:19:24 +00006445 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00006446 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00006447 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006448 << getOpenMPClauseName(OMPC_copyprivate) << Type
6449 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00006450 bool IsDecl =
6451 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6452 Diag(VD->getLocation(),
6453 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6454 << VD;
6455 continue;
6456 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006457
Alexey Bataevbae9a792014-06-27 10:37:06 +00006458 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
6459 // A variable of class type (or array thereof) that appears in a
6460 // copyin clause requires an accessible, unambiguous copy assignment
6461 // operator for the class type.
Alexey Bataev420d45b2015-04-14 05:11:24 +00006462 Type = Context.getBaseElementType(Type).getUnqualifiedType();
6463 auto *SrcVD =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006464 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.src");
Alexey Bataev420d45b2015-04-14 05:11:24 +00006465 auto *PseudoSrcExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006466 buildDeclRefExpr(*this, SrcVD, Type, DE->getExprLoc());
Alexey Bataev420d45b2015-04-14 05:11:24 +00006467 auto *DstVD =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006468 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.dst");
Alexey Bataev420d45b2015-04-14 05:11:24 +00006469 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006470 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataeva63048e2015-03-23 06:18:07 +00006471 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
6472 PseudoDstExpr, PseudoSrcExpr);
6473 if (AssignmentOp.isInvalid())
6474 continue;
6475 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
6476 /*DiscardedValue=*/true);
6477 if (AssignmentOp.isInvalid())
6478 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00006479
6480 // No need to mark vars as copyprivate, they are already threadprivate or
6481 // implicitly private.
6482 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00006483 SrcExprs.push_back(PseudoSrcExpr);
6484 DstExprs.push_back(PseudoDstExpr);
6485 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +00006486 }
6487
6488 if (Vars.empty())
6489 return nullptr;
6490
Alexey Bataeva63048e2015-03-23 06:18:07 +00006491 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
6492 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00006493}
6494
Alexey Bataev6125da92014-07-21 11:26:11 +00006495OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
6496 SourceLocation StartLoc,
6497 SourceLocation LParenLoc,
6498 SourceLocation EndLoc) {
6499 if (VarList.empty())
6500 return nullptr;
6501
6502 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
6503}
Alexey Bataevdea47612014-07-23 07:46:59 +00006504
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006505OMPClause *
6506Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
6507 SourceLocation DepLoc, SourceLocation ColonLoc,
6508 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
6509 SourceLocation LParenLoc, SourceLocation EndLoc) {
6510 if (DepKind == OMPC_DEPEND_unknown) {
6511 std::string Values;
6512 std::string Sep(", ");
6513 for (unsigned i = 0; i < OMPC_DEPEND_unknown; ++i) {
6514 Values += "'";
6515 Values += getOpenMPSimpleClauseTypeName(OMPC_depend, i);
6516 Values += "'";
6517 switch (i) {
6518 case OMPC_DEPEND_unknown - 2:
6519 Values += " or ";
6520 break;
6521 case OMPC_DEPEND_unknown - 1:
6522 break;
6523 default:
6524 Values += Sep;
6525 break;
6526 }
6527 }
6528 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
6529 << Values << getOpenMPClauseName(OMPC_depend);
6530 return nullptr;
6531 }
6532 SmallVector<Expr *, 8> Vars;
6533 for (auto &RefExpr : VarList) {
6534 assert(RefExpr && "NULL expr in OpenMP shared clause.");
6535 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6536 // It will be analyzed later.
6537 Vars.push_back(RefExpr);
6538 continue;
6539 }
6540
6541 SourceLocation ELoc = RefExpr->getExprLoc();
6542 // OpenMP [2.11.1.1, Restrictions, p.3]
6543 // A variable that is part of another variable (such as a field of a
6544 // structure) but is not an array element or an array section cannot appear
6545 // in a depend clause.
6546 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
6547 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
6548 ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
6549 if (!RefExpr->IgnoreParenImpCasts()->isLValue() || (!ASE && !DE) ||
6550 (DE && !isa<VarDecl>(DE->getDecl())) ||
6551 (ASE && !ASE->getBase()->getType()->isAnyPointerType() &&
6552 !ASE->getBase()->getType()->isArrayType())) {
6553 Diag(ELoc, diag::err_omp_expected_var_name_or_array_item)
6554 << RefExpr->getSourceRange();
6555 continue;
6556 }
6557
6558 Vars.push_back(RefExpr->IgnoreParenImpCasts());
6559 }
6560
6561 if (Vars.empty())
6562 return nullptr;
6563
6564 return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
6565 DepLoc, ColonLoc, Vars);
6566}
6567