blob: d1ce30712e2bb57804dcb413c24ae50a2be6aa3e [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 }
Michael Wong65f367f2015-07-21 13:44:28 +00001303 case OMPD_target_data:
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001304 case OMPD_target: {
1305 Sema::CapturedParamNameType Params[] = {
1306 std::make_pair(StringRef(), QualType()) // __context with shared vars
1307 };
1308 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1309 Params);
1310 break;
1311 }
Alexey Bataev13314bf2014-10-09 04:18:56 +00001312 case OMPD_teams: {
1313 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
1314 QualType KmpInt32PtrTy = Context.getPointerType(KmpInt32Ty);
1315 Sema::CapturedParamNameType Params[] = {
1316 std::make_pair(".global_tid.", KmpInt32PtrTy),
1317 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1318 std::make_pair(StringRef(), QualType()) // __context with shared vars
1319 };
1320 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1321 Params);
1322 break;
1323 }
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001324 case OMPD_taskgroup: {
1325 Sema::CapturedParamNameType Params[] = {
1326 std::make_pair(StringRef(), QualType()) // __context with shared vars
1327 };
1328 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1329 Params);
1330 break;
1331 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001332 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001333 case OMPD_taskyield:
1334 case OMPD_barrier:
1335 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001336 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001337 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00001338 case OMPD_flush:
Alexey Bataev9959db52014-05-06 10:08:46 +00001339 llvm_unreachable("OpenMP Directive is not allowed");
1340 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001341 llvm_unreachable("Unknown OpenMP directive");
1342 }
1343}
1344
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001345StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1346 ArrayRef<OMPClause *> Clauses) {
1347 if (!S.isUsable()) {
1348 ActOnCapturedRegionError();
1349 return StmtError();
1350 }
Alexey Bataev040d5402015-05-12 08:35:28 +00001351 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001352 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001353 if (isOpenMPPrivate(Clause->getClauseKind()) ||
1354 Clause->getClauseKind() == OMPC_copyprivate) {
Alexey Bataev040d5402015-05-12 08:35:28 +00001355 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001356 for (auto *VarRef : Clause->children()) {
1357 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001358 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001359 }
1360 }
Alexey Bataev040d5402015-05-12 08:35:28 +00001361 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
1362 Clause->getClauseKind() == OMPC_schedule) {
1363 // Mark all variables in private list clauses as used in inner region.
1364 // Required for proper codegen of combined directives.
1365 // TODO: add processing for other clauses.
1366 if (auto *E = cast_or_null<Expr>(
1367 cast<OMPScheduleClause>(Clause)->getHelperChunkSize())) {
1368 MarkDeclarationsReferencedInExpr(E);
1369 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001370 }
1371 }
1372 return ActOnCapturedRegionEnd(S.get());
1373}
1374
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001375static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1376 OpenMPDirectiveKind CurrentRegion,
1377 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001378 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001379 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001380 // Allowed nesting of constructs
1381 // +------------------+-----------------+------------------------------------+
1382 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1383 // +------------------+-----------------+------------------------------------+
1384 // | parallel | parallel | * |
1385 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001386 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001387 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001388 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001389 // | parallel | simd | * |
1390 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001391 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001392 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001393 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001394 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001395 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001396 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001397 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001398 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001399 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001400 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001401 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001402 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001403 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001404 // | parallel | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001405 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001406 // | parallel | cancellation | |
1407 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001408 // | parallel | cancel | ! |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001409 // +------------------+-----------------+------------------------------------+
1410 // | for | parallel | * |
1411 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001412 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001413 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001414 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001415 // | for | simd | * |
1416 // | for | sections | + |
1417 // | for | section | + |
1418 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001419 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001420 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001421 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001422 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001423 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001424 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001425 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001426 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001427 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001428 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001429 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001430 // | for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001431 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001432 // | for | cancellation | |
1433 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001434 // | for | cancel | ! |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001435 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001436 // | master | parallel | * |
1437 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001438 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001439 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001440 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001441 // | master | simd | * |
1442 // | master | sections | + |
1443 // | master | section | + |
1444 // | master | single | + |
1445 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001446 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001447 // | master |parallel sections| * |
1448 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001449 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001450 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001451 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001452 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001453 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001454 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001455 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001456 // | master | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001457 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001458 // | master | cancellation | |
1459 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001460 // | master | cancel | |
Alexander Musman80c22892014-07-17 08:54:58 +00001461 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001462 // | critical | parallel | * |
1463 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001464 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001465 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001466 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001467 // | critical | simd | * |
1468 // | critical | sections | + |
1469 // | critical | section | + |
1470 // | critical | single | + |
1471 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001472 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001473 // | critical |parallel sections| * |
1474 // | critical | task | * |
1475 // | critical | taskyield | * |
1476 // | critical | barrier | + |
1477 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001478 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001479 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001480 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001481 // | critical | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001482 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001483 // | critical | cancellation | |
1484 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001485 // | critical | cancel | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001486 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001487 // | simd | parallel | |
1488 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001489 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00001490 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001491 // | simd | critical | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001492 // | simd | simd | |
1493 // | simd | sections | |
1494 // | simd | section | |
1495 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001496 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001497 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001498 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001499 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00001500 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001501 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001502 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001503 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00001504 // | simd | flush | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001505 // | simd | ordered | |
Alexey Bataev0162e452014-07-22 10:10:35 +00001506 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001507 // | simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001508 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001509 // | simd | cancellation | |
1510 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001511 // | simd | cancel | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001512 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00001513 // | for simd | parallel | |
1514 // | for simd | for | |
1515 // | for simd | for simd | |
1516 // | for simd | master | |
1517 // | for simd | critical | |
1518 // | for simd | simd | |
1519 // | for simd | sections | |
1520 // | for simd | section | |
1521 // | for simd | single | |
1522 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001523 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001524 // | for simd |parallel sections| |
1525 // | for simd | task | |
1526 // | for simd | taskyield | |
1527 // | for simd | barrier | |
1528 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001529 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001530 // | for simd | flush | |
1531 // | for simd | ordered | |
1532 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001533 // | for simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001534 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001535 // | for simd | cancellation | |
1536 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001537 // | for simd | cancel | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001538 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00001539 // | parallel for simd| parallel | |
1540 // | parallel for simd| for | |
1541 // | parallel for simd| for simd | |
1542 // | parallel for simd| master | |
1543 // | parallel for simd| critical | |
1544 // | parallel for simd| simd | |
1545 // | parallel for simd| sections | |
1546 // | parallel for simd| section | |
1547 // | parallel for simd| single | |
1548 // | parallel for simd| parallel for | |
1549 // | parallel for simd|parallel for simd| |
1550 // | parallel for simd|parallel sections| |
1551 // | parallel for simd| task | |
1552 // | parallel for simd| taskyield | |
1553 // | parallel for simd| barrier | |
1554 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001555 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001556 // | parallel for simd| flush | |
1557 // | parallel for simd| ordered | |
1558 // | parallel for simd| atomic | |
1559 // | parallel for simd| target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001560 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001561 // | parallel for simd| cancellation | |
1562 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001563 // | parallel for simd| cancel | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001564 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001565 // | sections | parallel | * |
1566 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001567 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001568 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001569 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001570 // | sections | simd | * |
1571 // | sections | sections | + |
1572 // | sections | section | * |
1573 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001574 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001575 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001576 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001577 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001578 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001579 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001580 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001581 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001582 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001583 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001584 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001585 // | sections | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001586 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001587 // | sections | cancellation | |
1588 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001589 // | sections | cancel | ! |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001590 // +------------------+-----------------+------------------------------------+
1591 // | section | parallel | * |
1592 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001593 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001594 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001595 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001596 // | section | simd | * |
1597 // | section | sections | + |
1598 // | section | section | + |
1599 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001600 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001601 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001602 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001603 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001604 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001605 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001606 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001607 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001608 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001609 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001610 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001611 // | section | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001612 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001613 // | section | cancellation | |
1614 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001615 // | section | cancel | ! |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001616 // +------------------+-----------------+------------------------------------+
1617 // | single | parallel | * |
1618 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001619 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001620 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001621 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001622 // | single | simd | * |
1623 // | single | sections | + |
1624 // | single | section | + |
1625 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001626 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001627 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001628 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001629 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001630 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001631 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001632 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001633 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001634 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001635 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001636 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001637 // | single | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001638 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001639 // | single | cancellation | |
1640 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001641 // | single | cancel | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001642 // +------------------+-----------------+------------------------------------+
1643 // | parallel for | parallel | * |
1644 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001645 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001646 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001647 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001648 // | parallel for | simd | * |
1649 // | parallel for | sections | + |
1650 // | parallel for | section | + |
1651 // | parallel for | single | + |
1652 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001653 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001654 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001655 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001656 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001657 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001658 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001659 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001660 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001661 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001662 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001663 // | parallel for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001664 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001665 // | parallel for | cancellation | |
1666 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001667 // | parallel for | cancel | ! |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001668 // +------------------+-----------------+------------------------------------+
1669 // | parallel sections| parallel | * |
1670 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001671 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001672 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001673 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001674 // | parallel sections| simd | * |
1675 // | parallel sections| sections | + |
1676 // | parallel sections| section | * |
1677 // | parallel sections| single | + |
1678 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001679 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001680 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001681 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001682 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001683 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001684 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001685 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001686 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001687 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001688 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001689 // | parallel sections| target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001690 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001691 // | parallel sections| cancellation | |
1692 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001693 // | parallel sections| cancel | ! |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001694 // +------------------+-----------------+------------------------------------+
1695 // | task | parallel | * |
1696 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001697 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001698 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001699 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001700 // | task | simd | * |
1701 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001702 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001703 // | task | single | + |
1704 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001705 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001706 // | task |parallel sections| * |
1707 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001708 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001709 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001710 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001711 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001712 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001713 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001714 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001715 // | task | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001716 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001717 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00001718 // | | point | ! |
1719 // | task | cancel | ! |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001720 // +------------------+-----------------+------------------------------------+
1721 // | ordered | parallel | * |
1722 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001723 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001724 // | ordered | master | * |
1725 // | ordered | critical | * |
1726 // | ordered | simd | * |
1727 // | ordered | sections | + |
1728 // | ordered | section | + |
1729 // | ordered | single | + |
1730 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001731 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001732 // | ordered |parallel sections| * |
1733 // | ordered | task | * |
1734 // | ordered | taskyield | * |
1735 // | ordered | barrier | + |
1736 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001737 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001738 // | ordered | flush | * |
1739 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001740 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001741 // | ordered | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001742 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001743 // | ordered | cancellation | |
1744 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001745 // | ordered | cancel | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001746 // +------------------+-----------------+------------------------------------+
1747 // | atomic | parallel | |
1748 // | atomic | for | |
1749 // | atomic | for simd | |
1750 // | atomic | master | |
1751 // | atomic | critical | |
1752 // | atomic | simd | |
1753 // | atomic | sections | |
1754 // | atomic | section | |
1755 // | atomic | single | |
1756 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001757 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001758 // | atomic |parallel sections| |
1759 // | atomic | task | |
1760 // | atomic | taskyield | |
1761 // | atomic | barrier | |
1762 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001763 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001764 // | atomic | flush | |
1765 // | atomic | ordered | |
1766 // | atomic | atomic | |
1767 // | atomic | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001768 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001769 // | atomic | cancellation | |
1770 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001771 // | atomic | cancel | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001772 // +------------------+-----------------+------------------------------------+
1773 // | target | parallel | * |
1774 // | target | for | * |
1775 // | target | for simd | * |
1776 // | target | master | * |
1777 // | target | critical | * |
1778 // | target | simd | * |
1779 // | target | sections | * |
1780 // | target | section | * |
1781 // | target | single | * |
1782 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001783 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001784 // | target |parallel sections| * |
1785 // | target | task | * |
1786 // | target | taskyield | * |
1787 // | target | barrier | * |
1788 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001789 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001790 // | target | flush | * |
1791 // | target | ordered | * |
1792 // | target | atomic | * |
1793 // | target | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001794 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001795 // | target | cancellation | |
1796 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001797 // | target | cancel | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001798 // +------------------+-----------------+------------------------------------+
1799 // | teams | parallel | * |
1800 // | teams | for | + |
1801 // | teams | for simd | + |
1802 // | teams | master | + |
1803 // | teams | critical | + |
1804 // | teams | simd | + |
1805 // | teams | sections | + |
1806 // | teams | section | + |
1807 // | teams | single | + |
1808 // | teams | parallel for | * |
1809 // | teams |parallel for simd| * |
1810 // | teams |parallel sections| * |
1811 // | teams | task | + |
1812 // | teams | taskyield | + |
1813 // | teams | barrier | + |
1814 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00001815 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001816 // | teams | flush | + |
1817 // | teams | ordered | + |
1818 // | teams | atomic | + |
1819 // | teams | target | + |
1820 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001821 // | teams | cancellation | |
1822 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001823 // | teams | cancel | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001824 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00001825 if (Stack->getCurScope()) {
1826 auto ParentRegion = Stack->getParentDirective();
1827 bool NestingProhibited = false;
1828 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001829 enum {
1830 NoRecommend,
1831 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00001832 ShouldBeInOrderedRegion,
1833 ShouldBeInTargetRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001834 } Recommend = NoRecommend;
Alexey Bataev549210e2014-06-24 04:39:47 +00001835 if (isOpenMPSimdDirective(ParentRegion)) {
1836 // OpenMP [2.16, Nesting of Regions]
1837 // OpenMP constructs may not be nested inside a simd region.
1838 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
1839 return true;
1840 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001841 if (ParentRegion == OMPD_atomic) {
1842 // OpenMP [2.16, Nesting of Regions]
1843 // OpenMP constructs may not be nested inside an atomic region.
1844 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
1845 return true;
1846 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001847 if (CurrentRegion == OMPD_section) {
1848 // OpenMP [2.7.2, sections Construct, Restrictions]
1849 // Orphaned section directives are prohibited. That is, the section
1850 // directives must appear within the sections construct and must not be
1851 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001852 if (ParentRegion != OMPD_sections &&
1853 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001854 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
1855 << (ParentRegion != OMPD_unknown)
1856 << getOpenMPDirectiveName(ParentRegion);
1857 return true;
1858 }
1859 return false;
1860 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001861 // Allow some constructs to be orphaned (they could be used in functions,
1862 // called from OpenMP regions with the required preconditions).
1863 if (ParentRegion == OMPD_unknown)
1864 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00001865 if (CurrentRegion == OMPD_cancellation_point ||
1866 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001867 // OpenMP [2.16, Nesting of Regions]
1868 // A cancellation point construct for which construct-type-clause is
1869 // taskgroup must be nested inside a task construct. A cancellation
1870 // point construct for which construct-type-clause is not taskgroup must
1871 // be closely nested inside an OpenMP construct that matches the type
1872 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00001873 // A cancel construct for which construct-type-clause is taskgroup must be
1874 // nested inside a task construct. A cancel construct for which
1875 // construct-type-clause is not taskgroup must be closely nested inside an
1876 // OpenMP construct that matches the type specified in
1877 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001878 NestingProhibited =
1879 !((CancelRegion == OMPD_parallel && ParentRegion == OMPD_parallel) ||
1880 (CancelRegion == OMPD_for && ParentRegion == OMPD_for) ||
1881 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
1882 (CancelRegion == OMPD_sections &&
1883 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections)));
1884 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00001885 // OpenMP [2.16, Nesting of Regions]
1886 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00001887 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00001888 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
1889 ParentRegion == OMPD_task;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001890 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
1891 // OpenMP [2.16, Nesting of Regions]
1892 // A critical region may not be nested (closely or otherwise) inside a
1893 // critical region with the same name. Note that this restriction is not
1894 // sufficient to prevent deadlock.
1895 SourceLocation PreviousCriticalLoc;
1896 bool DeadLock =
1897 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
1898 OpenMPDirectiveKind K,
1899 const DeclarationNameInfo &DNI,
1900 SourceLocation Loc)
1901 ->bool {
1902 if (K == OMPD_critical &&
1903 DNI.getName() == CurrentName.getName()) {
1904 PreviousCriticalLoc = Loc;
1905 return true;
1906 } else
1907 return false;
1908 },
1909 false /* skip top directive */);
1910 if (DeadLock) {
1911 SemaRef.Diag(StartLoc,
1912 diag::err_omp_prohibited_region_critical_same_name)
1913 << CurrentName.getName();
1914 if (PreviousCriticalLoc.isValid())
1915 SemaRef.Diag(PreviousCriticalLoc,
1916 diag::note_omp_previous_critical_region);
1917 return true;
1918 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001919 } else if (CurrentRegion == OMPD_barrier) {
1920 // OpenMP [2.16, Nesting of Regions]
1921 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00001922 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001923 NestingProhibited =
1924 isOpenMPWorksharingDirective(ParentRegion) ||
1925 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1926 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00001927 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00001928 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00001929 // OpenMP [2.16, Nesting of Regions]
1930 // A worksharing region may not be closely nested inside a worksharing,
1931 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001932 NestingProhibited =
Alexander Musmanf82886e2014-09-18 05:12:34 +00001933 isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001934 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
1935 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered;
1936 Recommend = ShouldBeInParallelRegion;
1937 } else if (CurrentRegion == OMPD_ordered) {
1938 // OpenMP [2.16, Nesting of Regions]
1939 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00001940 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001941 // An ordered region must be closely nested inside a loop region (or
1942 // parallel loop region) with an ordered clause.
1943 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00001944 ParentRegion == OMPD_task ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001945 !Stack->isParentOrderedRegion();
1946 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00001947 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
1948 // OpenMP [2.16, Nesting of Regions]
1949 // If specified, a teams construct must be contained within a target
1950 // construct.
1951 NestingProhibited = ParentRegion != OMPD_target;
1952 Recommend = ShouldBeInTargetRegion;
1953 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
1954 }
1955 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
1956 // OpenMP [2.16, Nesting of Regions]
1957 // distribute, parallel, parallel sections, parallel workshare, and the
1958 // parallel loop and parallel loop SIMD constructs are the only OpenMP
1959 // constructs that can be closely nested in the teams region.
1960 // TODO: add distribute directive.
1961 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion);
1962 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00001963 }
1964 if (NestingProhibited) {
1965 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001966 << CloseNesting << getOpenMPDirectiveName(ParentRegion) << Recommend
1967 << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00001968 return true;
1969 }
1970 }
1971 return false;
1972}
1973
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001974StmtResult Sema::ActOnOpenMPExecutableDirective(
1975 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
1976 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
1977 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00001978 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001979 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
1980 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00001981 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001982
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001983 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev68446b72014-07-18 07:47:19 +00001984 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001985 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00001986 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00001987 if (AStmt) {
1988 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
1989
1990 // Check default data sharing attributes for referenced variables.
1991 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
1992 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
1993 if (DSAChecker.isErrorFound())
1994 return StmtError();
1995 // Generate list of implicitly defined firstprivate variables.
1996 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00001997
1998 if (!DSAChecker.getImplicitFirstprivate().empty()) {
1999 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
2000 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
2001 SourceLocation(), SourceLocation())) {
2002 ClausesWithImplicit.push_back(Implicit);
2003 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
2004 DSAChecker.getImplicitFirstprivate().size();
2005 } else
2006 ErrorFound = true;
2007 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002008 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002009
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002010 switch (Kind) {
2011 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00002012 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
2013 EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002014 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002015 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002016 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2017 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002018 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002019 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002020 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2021 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002022 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00002023 case OMPD_for_simd:
2024 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2025 EndLoc, VarsWithInheritedDSA);
2026 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002027 case OMPD_sections:
2028 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
2029 EndLoc);
2030 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002031 case OMPD_section:
2032 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00002033 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002034 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
2035 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002036 case OMPD_single:
2037 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
2038 EndLoc);
2039 break;
Alexander Musman80c22892014-07-17 08:54:58 +00002040 case OMPD_master:
2041 assert(ClausesWithImplicit.empty() &&
2042 "No clauses are allowed for 'omp master' directive");
2043 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
2044 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002045 case OMPD_critical:
2046 assert(ClausesWithImplicit.empty() &&
2047 "No clauses are allowed for 'omp critical' directive");
2048 Res = ActOnOpenMPCriticalDirective(DirName, AStmt, StartLoc, EndLoc);
2049 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002050 case OMPD_parallel_for:
2051 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
2052 EndLoc, VarsWithInheritedDSA);
2053 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00002054 case OMPD_parallel_for_simd:
2055 Res = ActOnOpenMPParallelForSimdDirective(
2056 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
2057 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002058 case OMPD_parallel_sections:
2059 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
2060 StartLoc, EndLoc);
2061 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002062 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002063 Res =
2064 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
2065 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00002066 case OMPD_taskyield:
2067 assert(ClausesWithImplicit.empty() &&
2068 "No clauses are allowed for 'omp taskyield' directive");
2069 assert(AStmt == nullptr &&
2070 "No associated statement allowed for 'omp taskyield' directive");
2071 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
2072 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002073 case OMPD_barrier:
2074 assert(ClausesWithImplicit.empty() &&
2075 "No clauses are allowed for 'omp barrier' directive");
2076 assert(AStmt == nullptr &&
2077 "No associated statement allowed for 'omp barrier' directive");
2078 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
2079 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00002080 case OMPD_taskwait:
2081 assert(ClausesWithImplicit.empty() &&
2082 "No clauses are allowed for 'omp taskwait' directive");
2083 assert(AStmt == nullptr &&
2084 "No associated statement allowed for 'omp taskwait' directive");
2085 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
2086 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002087 case OMPD_taskgroup:
2088 assert(ClausesWithImplicit.empty() &&
2089 "No clauses are allowed for 'omp taskgroup' directive");
2090 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
2091 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00002092 case OMPD_flush:
2093 assert(AStmt == nullptr &&
2094 "No associated statement allowed for 'omp flush' directive");
2095 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
2096 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002097 case OMPD_ordered:
2098 assert(ClausesWithImplicit.empty() &&
2099 "No clauses are allowed for 'omp ordered' directive");
2100 Res = ActOnOpenMPOrderedDirective(AStmt, StartLoc, EndLoc);
2101 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00002102 case OMPD_atomic:
2103 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
2104 EndLoc);
2105 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002106 case OMPD_teams:
2107 Res =
2108 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
2109 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002110 case OMPD_target:
2111 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
2112 EndLoc);
2113 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002114 case OMPD_cancellation_point:
2115 assert(ClausesWithImplicit.empty() &&
2116 "No clauses are allowed for 'omp cancellation point' directive");
2117 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
2118 "cancellation point' directive");
2119 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
2120 break;
Alexey Bataev80909872015-07-02 11:25:17 +00002121 case OMPD_cancel:
2122 assert(ClausesWithImplicit.empty() &&
2123 "No clauses are allowed for 'omp cancel' directive");
2124 assert(AStmt == nullptr &&
2125 "No associated statement allowed for 'omp cancel' directive");
2126 Res = ActOnOpenMPCancelDirective(StartLoc, EndLoc, CancelRegion);
2127 break;
Michael Wong65f367f2015-07-21 13:44:28 +00002128 case OMPD_target_data:
2129 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
2130 EndLoc);
2131 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002132 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002133 llvm_unreachable("OpenMP Directive is not allowed");
2134 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002135 llvm_unreachable("Unknown OpenMP directive");
2136 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002137
Alexey Bataev4acb8592014-07-07 13:01:15 +00002138 for (auto P : VarsWithInheritedDSA) {
2139 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
2140 << P.first << P.second->getSourceRange();
2141 }
2142 if (!VarsWithInheritedDSA.empty())
2143 return StmtError();
2144
Alexey Bataeved09d242014-05-28 05:53:51 +00002145 if (ErrorFound)
2146 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002147 return Res;
2148}
2149
2150StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
2151 Stmt *AStmt,
2152 SourceLocation StartLoc,
2153 SourceLocation EndLoc) {
Alexey Bataev9959db52014-05-06 10:08:46 +00002154 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
2155 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
2156 // 1.2.2 OpenMP Language Terminology
2157 // Structured block - An executable statement with a single entry at the
2158 // top and a single exit at the bottom.
2159 // The point of exit cannot be a branch out of the structured block.
2160 // longjmp() and throw() must not violate the entry/exit criteria.
2161 CS->getCapturedDecl()->setNothrow();
2162
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002163 getCurFunction()->setHasBranchProtectedScope();
2164
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00002165 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
2166 AStmt);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002167}
2168
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002169namespace {
2170/// \brief Helper class for checking canonical form of the OpenMP loops and
2171/// extracting iteration space of each loop in the loop nest, that will be used
2172/// for IR generation.
2173class OpenMPIterationSpaceChecker {
2174 /// \brief Reference to Sema.
2175 Sema &SemaRef;
2176 /// \brief A location for diagnostics (when there is no some better location).
2177 SourceLocation DefaultLoc;
2178 /// \brief A location for diagnostics (when increment is not compatible).
2179 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002180 /// \brief A source location for referring to loop init later.
2181 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002182 /// \brief A source location for referring to condition later.
2183 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002184 /// \brief A source location for referring to increment later.
2185 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002186 /// \brief Loop variable.
2187 VarDecl *Var;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002188 /// \brief Reference to loop variable.
2189 DeclRefExpr *VarRef;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002190 /// \brief Lower bound (initializer for the var).
2191 Expr *LB;
2192 /// \brief Upper bound.
2193 Expr *UB;
2194 /// \brief Loop step (increment).
2195 Expr *Step;
2196 /// \brief This flag is true when condition is one of:
2197 /// Var < UB
2198 /// Var <= UB
2199 /// UB > Var
2200 /// UB >= Var
2201 bool TestIsLessOp;
2202 /// \brief This flag is true when condition is strict ( < or > ).
2203 bool TestIsStrictOp;
2204 /// \brief This flag is true when step is subtracted on each iteration.
2205 bool SubtractStep;
2206
2207public:
2208 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
2209 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
Alexander Musmana5f070a2014-10-01 06:03:56 +00002210 InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()),
2211 IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002212 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
2213 TestIsStrictOp(false), SubtractStep(false) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002214 /// \brief Check init-expr for canonical loop form and save loop counter
2215 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00002216 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002217 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
2218 /// for less/greater and for strict/non-strict comparison.
2219 bool CheckCond(Expr *S);
2220 /// \brief Check incr-expr for canonical loop form and return true if it
2221 /// does not conform, otherwise save loop step (#Step).
2222 bool CheckInc(Expr *S);
2223 /// \brief Return the loop counter variable.
2224 VarDecl *GetLoopVar() const { return Var; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002225 /// \brief Return the reference expression to loop counter variable.
2226 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002227 /// \brief Source range of the loop init.
2228 SourceRange GetInitSrcRange() const { return InitSrcRange; }
2229 /// \brief Source range of the loop condition.
2230 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
2231 /// \brief Source range of the loop increment.
2232 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
2233 /// \brief True if the step should be subtracted.
2234 bool ShouldSubtractStep() const { return SubtractStep; }
2235 /// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00002236 Expr *BuildNumIterations(Scope *S, const bool LimitedType) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00002237 /// \brief Build the precondition expression for the loops.
2238 Expr *BuildPreCond(Scope *S, Expr *Cond) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002239 /// \brief Build reference expression to the counter be used for codegen.
2240 Expr *BuildCounterVar() const;
2241 /// \brief Build initization of the counter be used for codegen.
2242 Expr *BuildCounterInit() const;
2243 /// \brief Build step of the counter be used for codegen.
2244 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002245 /// \brief Return true if any expression is dependent.
2246 bool Dependent() const;
2247
2248private:
2249 /// \brief Check the right-hand side of an assignment in the increment
2250 /// expression.
2251 bool CheckIncRHS(Expr *RHS);
2252 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002253 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002254 /// \brief Helper to set upper bound.
2255 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, const SourceRange &SR,
2256 const SourceLocation &SL);
2257 /// \brief Helper to set loop increment.
2258 bool SetStep(Expr *NewStep, bool Subtract);
2259};
2260
2261bool OpenMPIterationSpaceChecker::Dependent() const {
2262 if (!Var) {
2263 assert(!LB && !UB && !Step);
2264 return false;
2265 }
2266 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
2267 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
2268}
2269
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002270template <typename T>
2271static T *getExprAsWritten(T *E) {
2272 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
2273 E = ExprTemp->getSubExpr();
2274
2275 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
2276 E = MTE->GetTemporaryExpr();
2277
2278 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
2279 E = Binder->getSubExpr();
2280
2281 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
2282 E = ICE->getSubExprAsWritten();
2283 return E->IgnoreParens();
2284}
2285
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002286bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
2287 DeclRefExpr *NewVarRefExpr,
2288 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002289 // State consistency checking to ensure correct usage.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002290 assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
2291 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002292 if (!NewVar || !NewLB)
2293 return true;
2294 Var = NewVar;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002295 VarRef = NewVarRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002296 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
2297 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00002298 if ((Ctor->isCopyOrMoveConstructor() ||
2299 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
2300 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002301 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002302 LB = NewLB;
2303 return false;
2304}
2305
2306bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
2307 const SourceRange &SR,
2308 const SourceLocation &SL) {
2309 // State consistency checking to ensure correct usage.
2310 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
2311 !TestIsLessOp && !TestIsStrictOp);
2312 if (!NewUB)
2313 return true;
2314 UB = NewUB;
2315 TestIsLessOp = LessOp;
2316 TestIsStrictOp = StrictOp;
2317 ConditionSrcRange = SR;
2318 ConditionLoc = SL;
2319 return false;
2320}
2321
2322bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
2323 // State consistency checking to ensure correct usage.
2324 assert(Var != nullptr && LB != nullptr && Step == nullptr);
2325 if (!NewStep)
2326 return true;
2327 if (!NewStep->isValueDependent()) {
2328 // Check that the step is integer expression.
2329 SourceLocation StepLoc = NewStep->getLocStart();
2330 ExprResult Val =
2331 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
2332 if (Val.isInvalid())
2333 return true;
2334 NewStep = Val.get();
2335
2336 // OpenMP [2.6, Canonical Loop Form, Restrictions]
2337 // If test-expr is of form var relational-op b and relational-op is < or
2338 // <= then incr-expr must cause var to increase on each iteration of the
2339 // loop. If test-expr is of form var relational-op b and relational-op is
2340 // > or >= then incr-expr must cause var to decrease on each iteration of
2341 // the loop.
2342 // If test-expr is of form b relational-op var and relational-op is < or
2343 // <= then incr-expr must cause var to decrease on each iteration of the
2344 // loop. If test-expr is of form b relational-op var and relational-op is
2345 // > or >= then incr-expr must cause var to increase on each iteration of
2346 // the loop.
2347 llvm::APSInt Result;
2348 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
2349 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
2350 bool IsConstNeg =
2351 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00002352 bool IsConstPos =
2353 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002354 bool IsConstZero = IsConstant && !Result.getBoolValue();
2355 if (UB && (IsConstZero ||
2356 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00002357 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002358 SemaRef.Diag(NewStep->getExprLoc(),
2359 diag::err_omp_loop_incr_not_compatible)
2360 << Var << TestIsLessOp << NewStep->getSourceRange();
2361 SemaRef.Diag(ConditionLoc,
2362 diag::note_omp_loop_cond_requres_compatible_incr)
2363 << TestIsLessOp << ConditionSrcRange;
2364 return true;
2365 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002366 if (TestIsLessOp == Subtract) {
2367 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
2368 NewStep).get();
2369 Subtract = !Subtract;
2370 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002371 }
2372
2373 Step = NewStep;
2374 SubtractStep = Subtract;
2375 return false;
2376}
2377
Alexey Bataev9c821032015-04-30 04:23:23 +00002378bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002379 // Check init-expr for canonical loop form and save loop counter
2380 // variable - #Var and its initialization value - #LB.
2381 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
2382 // var = lb
2383 // integer-type var = lb
2384 // random-access-iterator-type var = lb
2385 // pointer-type var = lb
2386 //
2387 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00002388 if (EmitDiags) {
2389 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
2390 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002391 return true;
2392 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002393 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002394 if (Expr *E = dyn_cast<Expr>(S))
2395 S = E->IgnoreParens();
2396 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2397 if (BO->getOpcode() == BO_Assign)
2398 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002399 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002400 BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002401 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
2402 if (DS->isSingleDecl()) {
2403 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
2404 if (Var->hasInit()) {
2405 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00002406 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002407 SemaRef.Diag(S->getLocStart(),
2408 diag::ext_omp_loop_not_canonical_init)
2409 << S->getSourceRange();
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002410 return SetVarAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002411 }
2412 }
2413 }
2414 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
2415 if (CE->getOperator() == OO_Equal)
2416 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002417 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
2418 CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002419
Alexey Bataev9c821032015-04-30 04:23:23 +00002420 if (EmitDiags) {
2421 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
2422 << S->getSourceRange();
2423 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002424 return true;
2425}
2426
Alexey Bataev23b69422014-06-18 07:08:49 +00002427/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002428/// variable (which may be the loop variable) if possible.
2429static const VarDecl *GetInitVarDecl(const Expr *E) {
2430 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00002431 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002432 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002433 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
2434 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00002435 if ((Ctor->isCopyOrMoveConstructor() ||
2436 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
2437 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002438 E = CE->getArg(0)->IgnoreParenImpCasts();
2439 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
2440 if (!DRE)
2441 return nullptr;
2442 return dyn_cast<VarDecl>(DRE->getDecl());
2443}
2444
2445bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
2446 // Check test-expr for canonical form, save upper-bound UB, flags for
2447 // less/greater and for strict/non-strict comparison.
2448 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2449 // var relational-op b
2450 // b relational-op var
2451 //
2452 if (!S) {
2453 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
2454 return true;
2455 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002456 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002457 SourceLocation CondLoc = S->getLocStart();
2458 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2459 if (BO->isRelationalOp()) {
2460 if (GetInitVarDecl(BO->getLHS()) == Var)
2461 return SetUB(BO->getRHS(),
2462 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
2463 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2464 BO->getSourceRange(), BO->getOperatorLoc());
2465 if (GetInitVarDecl(BO->getRHS()) == Var)
2466 return SetUB(BO->getLHS(),
2467 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
2468 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2469 BO->getSourceRange(), BO->getOperatorLoc());
2470 }
2471 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2472 if (CE->getNumArgs() == 2) {
2473 auto Op = CE->getOperator();
2474 switch (Op) {
2475 case OO_Greater:
2476 case OO_GreaterEqual:
2477 case OO_Less:
2478 case OO_LessEqual:
2479 if (GetInitVarDecl(CE->getArg(0)) == Var)
2480 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
2481 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2482 CE->getOperatorLoc());
2483 if (GetInitVarDecl(CE->getArg(1)) == Var)
2484 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
2485 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2486 CE->getOperatorLoc());
2487 break;
2488 default:
2489 break;
2490 }
2491 }
2492 }
2493 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
2494 << S->getSourceRange() << Var;
2495 return true;
2496}
2497
2498bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
2499 // RHS of canonical loop form increment can be:
2500 // var + incr
2501 // incr + var
2502 // var - incr
2503 //
2504 RHS = RHS->IgnoreParenImpCasts();
2505 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
2506 if (BO->isAdditiveOp()) {
2507 bool IsAdd = BO->getOpcode() == BO_Add;
2508 if (GetInitVarDecl(BO->getLHS()) == Var)
2509 return SetStep(BO->getRHS(), !IsAdd);
2510 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
2511 return SetStep(BO->getLHS(), false);
2512 }
2513 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
2514 bool IsAdd = CE->getOperator() == OO_Plus;
2515 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
2516 if (GetInitVarDecl(CE->getArg(0)) == Var)
2517 return SetStep(CE->getArg(1), !IsAdd);
2518 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
2519 return SetStep(CE->getArg(0), false);
2520 }
2521 }
2522 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2523 << RHS->getSourceRange() << Var;
2524 return true;
2525}
2526
2527bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
2528 // Check incr-expr for canonical loop form and return true if it
2529 // does not conform.
2530 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2531 // ++var
2532 // var++
2533 // --var
2534 // var--
2535 // var += incr
2536 // var -= incr
2537 // var = var + incr
2538 // var = incr + var
2539 // var = var - incr
2540 //
2541 if (!S) {
2542 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
2543 return true;
2544 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002545 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002546 S = S->IgnoreParens();
2547 if (auto UO = dyn_cast<UnaryOperator>(S)) {
2548 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
2549 return SetStep(
2550 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
2551 (UO->isDecrementOp() ? -1 : 1)).get(),
2552 false);
2553 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
2554 switch (BO->getOpcode()) {
2555 case BO_AddAssign:
2556 case BO_SubAssign:
2557 if (GetInitVarDecl(BO->getLHS()) == Var)
2558 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
2559 break;
2560 case BO_Assign:
2561 if (GetInitVarDecl(BO->getLHS()) == Var)
2562 return CheckIncRHS(BO->getRHS());
2563 break;
2564 default:
2565 break;
2566 }
2567 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2568 switch (CE->getOperator()) {
2569 case OO_PlusPlus:
2570 case OO_MinusMinus:
2571 if (GetInitVarDecl(CE->getArg(0)) == Var)
2572 return SetStep(
2573 SemaRef.ActOnIntegerConstant(
2574 CE->getLocStart(),
2575 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
2576 false);
2577 break;
2578 case OO_PlusEqual:
2579 case OO_MinusEqual:
2580 if (GetInitVarDecl(CE->getArg(0)) == Var)
2581 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
2582 break;
2583 case OO_Equal:
2584 if (GetInitVarDecl(CE->getArg(0)) == Var)
2585 return CheckIncRHS(CE->getArg(1));
2586 break;
2587 default:
2588 break;
2589 }
2590 }
2591 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2592 << S->getSourceRange() << Var;
2593 return true;
2594}
Alexander Musmana5f070a2014-10-01 06:03:56 +00002595
2596/// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00002597Expr *
2598OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S,
2599 const bool LimitedType) const {
Alexander Musmana5f070a2014-10-01 06:03:56 +00002600 ExprResult Diff;
2601 if (Var->getType()->isIntegerType() || Var->getType()->isPointerType() ||
2602 SemaRef.getLangOpts().CPlusPlus) {
2603 // Upper - Lower
2604 Expr *Upper = TestIsLessOp ? UB : LB;
2605 Expr *Lower = TestIsLessOp ? LB : UB;
2606
2607 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
2608
2609 if (!Diff.isUsable() && Var->getType()->getAsCXXRecordDecl()) {
2610 // BuildBinOp already emitted error, this one is to point user to upper
2611 // and lower bound, and to tell what is passed to 'operator-'.
2612 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
2613 << Upper->getSourceRange() << Lower->getSourceRange();
2614 return nullptr;
2615 }
2616 }
2617
2618 if (!Diff.isUsable())
2619 return nullptr;
2620
2621 // Upper - Lower [- 1]
2622 if (TestIsStrictOp)
2623 Diff = SemaRef.BuildBinOp(
2624 S, DefaultLoc, BO_Sub, Diff.get(),
2625 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
2626 if (!Diff.isUsable())
2627 return nullptr;
2628
2629 // Upper - Lower [- 1] + Step
2630 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(),
2631 Step->IgnoreImplicit());
2632 if (!Diff.isUsable())
2633 return nullptr;
2634
2635 // Parentheses (for dumping/debugging purposes only).
2636 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
2637 if (!Diff.isUsable())
2638 return nullptr;
2639
2640 // (Upper - Lower [- 1] + Step) / Step
2641 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(),
2642 Step->IgnoreImplicit());
2643 if (!Diff.isUsable())
2644 return nullptr;
2645
Alexander Musman174b3ca2014-10-06 11:16:29 +00002646 // OpenMP runtime requires 32-bit or 64-bit loop variables.
2647 if (LimitedType) {
2648 auto &C = SemaRef.Context;
2649 QualType Type = Diff.get()->getType();
2650 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
2651 if (NewSize != C.getTypeSize(Type)) {
2652 if (NewSize < C.getTypeSize(Type)) {
2653 assert(NewSize == 64 && "incorrect loop var size");
2654 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
2655 << InitSrcRange << ConditionSrcRange;
2656 }
2657 QualType NewType = C.getIntTypeForBitwidth(
2658 NewSize, Type->hasSignedIntegerRepresentation());
2659 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
2660 Sema::AA_Converting, true);
2661 if (!Diff.isUsable())
2662 return nullptr;
2663 }
2664 }
2665
Alexander Musmana5f070a2014-10-01 06:03:56 +00002666 return Diff.get();
2667}
2668
Alexey Bataev62dbb972015-04-22 11:59:37 +00002669Expr *OpenMPIterationSpaceChecker::BuildPreCond(Scope *S, Expr *Cond) const {
2670 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
2671 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
2672 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
2673 auto CondExpr = SemaRef.BuildBinOp(
2674 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
2675 : (TestIsStrictOp ? BO_GT : BO_GE),
2676 LB, UB);
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002677 if (CondExpr.isUsable()) {
2678 CondExpr = SemaRef.PerformImplicitConversion(
2679 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
2680 /*AllowExplicit=*/true);
2681 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00002682 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
2683 // Otherwise use original loop conditon and evaluate it in runtime.
2684 return CondExpr.isUsable() ? CondExpr.get() : Cond;
2685}
2686
Alexander Musmana5f070a2014-10-01 06:03:56 +00002687/// \brief Build reference expression to the counter be used for codegen.
2688Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const {
Alexey Bataev39f915b82015-05-08 10:41:21 +00002689 return buildDeclRefExpr(SemaRef, Var, Var->getType(), DefaultLoc);
Alexander Musmana5f070a2014-10-01 06:03:56 +00002690}
2691
2692/// \brief Build initization of the counter be used for codegen.
2693Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
2694
2695/// \brief Build step of the counter be used for codegen.
2696Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
2697
2698/// \brief Iteration space of a single for loop.
2699struct LoopIterationSpace {
Alexey Bataev62dbb972015-04-22 11:59:37 +00002700 /// \brief Condition of the loop.
2701 Expr *PreCond;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002702 /// \brief This expression calculates the number of iterations in the loop.
2703 /// It is always possible to calculate it before starting the loop.
2704 Expr *NumIterations;
2705 /// \brief The loop counter variable.
2706 Expr *CounterVar;
2707 /// \brief This is initializer for the initial value of #CounterVar.
2708 Expr *CounterInit;
2709 /// \brief This is step for the #CounterVar used to generate its update:
2710 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
2711 Expr *CounterStep;
2712 /// \brief Should step be subtracted?
2713 bool Subtract;
2714 /// \brief Source range of the loop init.
2715 SourceRange InitSrcRange;
2716 /// \brief Source range of the loop condition.
2717 SourceRange CondSrcRange;
2718 /// \brief Source range of the loop increment.
2719 SourceRange IncSrcRange;
2720};
2721
Alexey Bataev23b69422014-06-18 07:08:49 +00002722} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002723
Alexey Bataev9c821032015-04-30 04:23:23 +00002724void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
2725 assert(getLangOpts().OpenMP && "OpenMP is not active.");
2726 assert(Init && "Expected loop in canonical form.");
2727 unsigned CollapseIteration = DSAStack->getCollapseNumber();
2728 if (CollapseIteration > 0 &&
2729 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
2730 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
2731 if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
2732 DSAStack->addLoopControlVariable(ISC.GetLoopVar());
2733 }
2734 DSAStack->setCollapseNumber(CollapseIteration - 1);
2735 }
2736}
2737
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002738/// \brief Called on a for stmt to check and extract its iteration space
2739/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00002740static bool CheckOpenMPIterationSpace(
2741 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
2742 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
2743 Expr *NestedLoopCountExpr,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002744 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
2745 LoopIterationSpace &ResultIterSpace) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002746 // OpenMP [2.6, Canonical Loop Form]
2747 // for (init-expr; test-expr; incr-expr) structured-block
2748 auto For = dyn_cast_or_null<ForStmt>(S);
2749 if (!For) {
2750 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002751 << (NestedLoopCountExpr != nullptr) << getOpenMPDirectiveName(DKind)
2752 << NestedLoopCount << (CurrentNestedLoopCount > 0)
2753 << CurrentNestedLoopCount;
2754 if (NestedLoopCount > 1)
2755 SemaRef.Diag(NestedLoopCountExpr->getExprLoc(),
2756 diag::note_omp_collapse_expr)
2757 << NestedLoopCountExpr->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002758 return true;
2759 }
2760 assert(For->getBody());
2761
2762 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
2763
2764 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002765 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002766 if (ISC.CheckInit(Init)) {
2767 return true;
2768 }
2769
2770 bool HasErrors = false;
2771
2772 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002773 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002774
2775 // OpenMP [2.6, Canonical Loop Form]
2776 // Var is one of the following:
2777 // A variable of signed or unsigned integer type.
2778 // For C++, a variable of a random access iterator type.
2779 // For C, a variable of a pointer type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002780 auto VarType = Var->getType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002781 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
2782 !VarType->isPointerType() &&
2783 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
2784 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
2785 << SemaRef.getLangOpts().CPlusPlus;
2786 HasErrors = true;
2787 }
2788
Alexey Bataev4acb8592014-07-07 13:01:15 +00002789 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
2790 // Construct
2791 // The loop iteration variable(s) in the associated for-loop(s) of a for or
2792 // parallel for construct is (are) private.
2793 // The loop iteration variable in the associated for-loop of a simd construct
2794 // with just one associated for-loop is linear with a constant-linear-step
2795 // that is the increment of the associated for-loop.
2796 // Exclude loop var from the list of variables with implicitly defined data
2797 // sharing attributes.
Benjamin Kramerad8e0792014-10-10 15:32:48 +00002798 VarsWithImplicitDSA.erase(Var);
Alexey Bataev4acb8592014-07-07 13:01:15 +00002799
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002800 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
2801 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00002802 // The loop iteration variable in the associated for-loop of a simd construct
2803 // with just one associated for-loop may be listed in a linear clause with a
2804 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00002805 // The loop iteration variable(s) in the associated for-loop(s) of a for or
2806 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002807 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002808 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
2809 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
2810 // declared in the loop and it is predetermined as a private.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002811 auto PredeterminedCKind =
2812 isOpenMPSimdDirective(DKind)
2813 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
2814 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002815 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataev0c024df2015-05-12 09:02:07 +00002816 DVar.CKind != OMPC_threadprivate && DVar.CKind != PredeterminedCKind) ||
Alexander Musmanf82886e2014-09-18 05:12:34 +00002817 (isOpenMPWorksharingDirective(DKind) && !isOpenMPSimdDirective(DKind) &&
2818 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private &&
Alexey Bataev0c024df2015-05-12 09:02:07 +00002819 DVar.CKind != OMPC_lastprivate && DVar.CKind != OMPC_threadprivate)) &&
2820 ((DVar.CKind != OMPC_private && DVar.CKind != OMPC_threadprivate) ||
2821 DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002822 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00002823 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
2824 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataevf2453a02015-05-06 07:25:08 +00002825 if (DVar.RefExpr == nullptr)
2826 DVar.CKind = PredeterminedCKind;
2827 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, /*IsLoopIterVar=*/true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002828 HasErrors = true;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002829 } else if (LoopVarRefExpr != nullptr) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00002830 // Make the loop iteration variable private (for worksharing constructs),
2831 // linear (for simd directives with the only one associated loop) or
2832 // lastprivate (for simd directives with several collapsed loops).
Alexey Bataev9aba41c2014-11-14 04:08:45 +00002833 if (DVar.CKind == OMPC_unknown)
2834 DVar = DSA.hasDSA(Var, isOpenMPPrivate, MatchesAlways(),
2835 /*FromParent=*/false);
Alexey Bataev9c821032015-04-30 04:23:23 +00002836 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002837 }
2838
Alexey Bataev7ff55242014-06-19 09:13:45 +00002839 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00002840
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002841 // Check test-expr.
2842 HasErrors |= ISC.CheckCond(For->getCond());
2843
2844 // Check incr-expr.
2845 HasErrors |= ISC.CheckInc(For->getInc());
2846
Alexander Musmana5f070a2014-10-01 06:03:56 +00002847 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002848 return HasErrors;
2849
Alexander Musmana5f070a2014-10-01 06:03:56 +00002850 // Build the loop's iteration space representation.
Alexey Bataev62dbb972015-04-22 11:59:37 +00002851 ResultIterSpace.PreCond = ISC.BuildPreCond(DSA.getCurScope(), For->getCond());
Alexander Musman174b3ca2014-10-06 11:16:29 +00002852 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
2853 DSA.getCurScope(), /* LimitedType */ isOpenMPWorksharingDirective(DKind));
Alexander Musmana5f070a2014-10-01 06:03:56 +00002854 ResultIterSpace.CounterVar = ISC.BuildCounterVar();
2855 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
2856 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
2857 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
2858 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
2859 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
2860 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
2861
Alexey Bataev62dbb972015-04-22 11:59:37 +00002862 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
2863 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00002864 ResultIterSpace.CounterVar == nullptr ||
2865 ResultIterSpace.CounterInit == nullptr ||
2866 ResultIterSpace.CounterStep == nullptr);
2867
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002868 return HasErrors;
2869}
2870
Alexander Musmana5f070a2014-10-01 06:03:56 +00002871/// \brief Build 'VarRef = Start + Iter * Step'.
2872static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S,
2873 SourceLocation Loc, ExprResult VarRef,
2874 ExprResult Start, ExprResult Iter,
2875 ExprResult Step, bool Subtract) {
2876 // Add parentheses (for debugging purposes only).
2877 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
2878 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
2879 !Step.isUsable())
2880 return ExprError();
2881
2882 ExprResult Update = SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(),
2883 Step.get()->IgnoreImplicit());
2884 if (!Update.isUsable())
2885 return ExprError();
2886
2887 // Build 'VarRef = Start + Iter * Step'.
2888 Update = SemaRef.BuildBinOp(S, Loc, (Subtract ? BO_Sub : BO_Add),
2889 Start.get()->IgnoreImplicit(), Update.get());
2890 if (!Update.isUsable())
2891 return ExprError();
2892
2893 Update = SemaRef.PerformImplicitConversion(
2894 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
2895 if (!Update.isUsable())
2896 return ExprError();
2897
2898 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
2899 return Update;
2900}
2901
2902/// \brief Convert integer expression \a E to make it have at least \a Bits
2903/// bits.
2904static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
2905 Sema &SemaRef) {
2906 if (E == nullptr)
2907 return ExprError();
2908 auto &C = SemaRef.Context;
2909 QualType OldType = E->getType();
2910 unsigned HasBits = C.getTypeSize(OldType);
2911 if (HasBits >= Bits)
2912 return ExprResult(E);
2913 // OK to convert to signed, because new type has more bits than old.
2914 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
2915 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
2916 true);
2917}
2918
2919/// \brief Check if the given expression \a E is a constant integer that fits
2920/// into \a Bits bits.
2921static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
2922 if (E == nullptr)
2923 return false;
2924 llvm::APSInt Result;
2925 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
2926 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
2927 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002928}
2929
2930/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00002931/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
2932/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00002933static unsigned
2934CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *NestedLoopCountExpr,
2935 Stmt *AStmt, Sema &SemaRef, DSAStackTy &DSA,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002936 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00002937 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002938 unsigned NestedLoopCount = 1;
2939 if (NestedLoopCountExpr) {
2940 // Found 'collapse' clause - calculate collapse number.
2941 llvm::APSInt Result;
2942 if (NestedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
2943 NestedLoopCount = Result.getLimitedValue();
2944 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002945 // This is helper routine for loop directives (e.g., 'for', 'simd',
2946 // 'for simd', etc.).
Alexander Musmana5f070a2014-10-01 06:03:56 +00002947 SmallVector<LoopIterationSpace, 4> IterSpaces;
2948 IterSpaces.resize(NestedLoopCount);
2949 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002950 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00002951 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev4acb8592014-07-07 13:01:15 +00002952 NestedLoopCount, NestedLoopCountExpr,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002953 VarsWithImplicitDSA, IterSpaces[Cnt]))
Alexey Bataevabfc0692014-06-25 06:52:00 +00002954 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002955 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00002956 // OpenMP [2.8.1, simd construct, Restrictions]
2957 // All loops associated with the construct must be perfectly nested; that
2958 // is, there must be no intervening code nor any OpenMP directive between
2959 // any two loops.
2960 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002961 }
2962
Alexander Musmana5f070a2014-10-01 06:03:56 +00002963 Built.clear(/* size */ NestedLoopCount);
2964
2965 if (SemaRef.CurContext->isDependentContext())
2966 return NestedLoopCount;
2967
2968 // An example of what is generated for the following code:
2969 //
2970 // #pragma omp simd collapse(2)
2971 // for (i = 0; i < NI; ++i)
2972 // for (j = J0; j < NJ; j+=2) {
2973 // <loop body>
2974 // }
2975 //
2976 // We generate the code below.
2977 // Note: the loop body may be outlined in CodeGen.
2978 // Note: some counters may be C++ classes, operator- is used to find number of
2979 // iterations and operator+= to calculate counter value.
2980 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
2981 // or i64 is currently supported).
2982 //
2983 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
2984 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
2985 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
2986 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
2987 // // similar updates for vars in clauses (e.g. 'linear')
2988 // <loop body (using local i and j)>
2989 // }
2990 // i = NI; // assign final values of counters
2991 // j = NJ;
2992 //
2993
2994 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
2995 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00002996 // Precondition tests if there is at least one iteration (all conditions are
2997 // true).
2998 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00002999 auto N0 = IterSpaces[0].NumIterations;
3000 ExprResult LastIteration32 = WidenIterationCount(32 /* Bits */, N0, SemaRef);
3001 ExprResult LastIteration64 = WidenIterationCount(64 /* Bits */, N0, SemaRef);
3002
3003 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
3004 return NestedLoopCount;
3005
3006 auto &C = SemaRef.Context;
3007 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
3008
3009 Scope *CurScope = DSA.getCurScope();
3010 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003011 if (PreCond.isUsable()) {
3012 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
3013 PreCond.get(), IterSpaces[Cnt].PreCond);
3014 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003015 auto N = IterSpaces[Cnt].NumIterations;
3016 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
3017 if (LastIteration32.isUsable())
3018 LastIteration32 = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_Mul,
3019 LastIteration32.get(), N);
3020 if (LastIteration64.isUsable())
3021 LastIteration64 = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_Mul,
3022 LastIteration64.get(), N);
3023 }
3024
3025 // Choose either the 32-bit or 64-bit version.
3026 ExprResult LastIteration = LastIteration64;
3027 if (LastIteration32.isUsable() &&
3028 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
3029 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
3030 FitsInto(
3031 32 /* Bits */,
3032 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
3033 LastIteration64.get(), SemaRef)))
3034 LastIteration = LastIteration32;
3035
3036 if (!LastIteration.isUsable())
3037 return 0;
3038
3039 // Save the number of iterations.
3040 ExprResult NumIterations = LastIteration;
3041 {
3042 LastIteration = SemaRef.BuildBinOp(
3043 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
3044 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3045 if (!LastIteration.isUsable())
3046 return 0;
3047 }
3048
3049 // Calculate the last iteration number beforehand instead of doing this on
3050 // each iteration. Do not do this if the number of iterations may be kfold-ed.
3051 llvm::APSInt Result;
3052 bool IsConstant =
3053 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
3054 ExprResult CalcLastIteration;
3055 if (!IsConstant) {
3056 SourceLocation SaveLoc;
3057 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00003058 buildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00003059 ".omp.last.iteration");
Alexey Bataev39f915b82015-05-08 10:41:21 +00003060 ExprResult SaveRef = buildDeclRefExpr(
3061 SemaRef, SaveVar, LastIteration.get()->getType(), SaveLoc);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003062 CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign,
3063 SaveRef.get(), LastIteration.get());
3064 LastIteration = SaveRef;
3065
3066 // Prepare SaveRef + 1.
3067 NumIterations = SemaRef.BuildBinOp(
3068 CurScope, SaveLoc, BO_Add, SaveRef.get(),
3069 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3070 if (!NumIterations.isUsable())
3071 return 0;
3072 }
3073
3074 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
3075
Alexander Musmanc6388682014-12-15 07:07:06 +00003076 QualType VType = LastIteration.get()->getType();
3077 // Build variables passed into runtime, nesessary for worksharing directives.
3078 ExprResult LB, UB, IL, ST, EUB;
3079 if (isOpenMPWorksharingDirective(DKind)) {
3080 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00003081 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
3082 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003083 SemaRef.AddInitializerToDecl(
3084 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
3085 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3086
3087 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00003088 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
3089 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003090 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
3091 /*DirectInit*/ false,
3092 /*TypeMayContainAuto*/ false);
3093
3094 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
3095 // This will be used to implement clause 'lastprivate'.
3096 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00003097 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
3098 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003099 SemaRef.AddInitializerToDecl(
3100 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
3101 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3102
3103 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev39f915b82015-05-08 10:41:21 +00003104 VarDecl *STDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.stride");
3105 ST = buildDeclRefExpr(SemaRef, STDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003106 SemaRef.AddInitializerToDecl(
3107 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
3108 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3109
3110 // Build expression: UB = min(UB, LastIteration)
3111 // It is nesessary for CodeGen of directives with static scheduling.
3112 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
3113 UB.get(), LastIteration.get());
3114 ExprResult CondOp = SemaRef.ActOnConditionalOp(
3115 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
3116 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
3117 CondOp.get());
3118 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
3119 }
3120
3121 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003122 ExprResult IV;
3123 ExprResult Init;
3124 {
Alexey Bataev39f915b82015-05-08 10:41:21 +00003125 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.iv");
3126 IV = buildDeclRefExpr(SemaRef, IVDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003127 Expr *RHS = isOpenMPWorksharingDirective(DKind)
3128 ? LB.get()
3129 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
3130 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
3131 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003132 }
3133
Alexander Musmanc6388682014-12-15 07:07:06 +00003134 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003135 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00003136 ExprResult Cond =
3137 isOpenMPWorksharingDirective(DKind)
3138 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
3139 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
3140 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003141
3142 // Loop increment (IV = IV + 1)
3143 SourceLocation IncLoc;
3144 ExprResult Inc =
3145 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
3146 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
3147 if (!Inc.isUsable())
3148 return 0;
3149 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00003150 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
3151 if (!Inc.isUsable())
3152 return 0;
3153
3154 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
3155 // Used for directives with static scheduling.
3156 ExprResult NextLB, NextUB;
3157 if (isOpenMPWorksharingDirective(DKind)) {
3158 // LB + ST
3159 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
3160 if (!NextLB.isUsable())
3161 return 0;
3162 // LB = LB + ST
3163 NextLB =
3164 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
3165 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
3166 if (!NextLB.isUsable())
3167 return 0;
3168 // UB + ST
3169 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
3170 if (!NextUB.isUsable())
3171 return 0;
3172 // UB = UB + ST
3173 NextUB =
3174 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
3175 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
3176 if (!NextUB.isUsable())
3177 return 0;
3178 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003179
3180 // Build updates and final values of the loop counters.
3181 bool HasErrors = false;
3182 Built.Counters.resize(NestedLoopCount);
3183 Built.Updates.resize(NestedLoopCount);
3184 Built.Finals.resize(NestedLoopCount);
3185 {
3186 ExprResult Div;
3187 // Go from inner nested loop to outer.
3188 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
3189 LoopIterationSpace &IS = IterSpaces[Cnt];
3190 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
3191 // Build: Iter = (IV / Div) % IS.NumIters
3192 // where Div is product of previous iterations' IS.NumIters.
3193 ExprResult Iter;
3194 if (Div.isUsable()) {
3195 Iter =
3196 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
3197 } else {
3198 Iter = IV;
3199 assert((Cnt == (int)NestedLoopCount - 1) &&
3200 "unusable div expected on first iteration only");
3201 }
3202
3203 if (Cnt != 0 && Iter.isUsable())
3204 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
3205 IS.NumIterations);
3206 if (!Iter.isUsable()) {
3207 HasErrors = true;
3208 break;
3209 }
3210
Alexey Bataev39f915b82015-05-08 10:41:21 +00003211 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
3212 auto *CounterVar = buildDeclRefExpr(
3213 SemaRef, cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()),
3214 IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
3215 /*RefersToCapture=*/true);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003216 ExprResult Update =
Alexey Bataev39f915b82015-05-08 10:41:21 +00003217 BuildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003218 IS.CounterInit, Iter, IS.CounterStep, IS.Subtract);
3219 if (!Update.isUsable()) {
3220 HasErrors = true;
3221 break;
3222 }
3223
3224 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
3225 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00003226 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003227 IS.NumIterations, IS.CounterStep, IS.Subtract);
3228 if (!Final.isUsable()) {
3229 HasErrors = true;
3230 break;
3231 }
3232
3233 // Build Div for the next iteration: Div <- Div * IS.NumIters
3234 if (Cnt != 0) {
3235 if (Div.isUnset())
3236 Div = IS.NumIterations;
3237 else
3238 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
3239 IS.NumIterations);
3240
3241 // Add parentheses (for debugging purposes only).
3242 if (Div.isUsable())
3243 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
3244 if (!Div.isUsable()) {
3245 HasErrors = true;
3246 break;
3247 }
3248 }
3249 if (!Update.isUsable() || !Final.isUsable()) {
3250 HasErrors = true;
3251 break;
3252 }
3253 // Save results
3254 Built.Counters[Cnt] = IS.CounterVar;
3255 Built.Updates[Cnt] = Update.get();
3256 Built.Finals[Cnt] = Final.get();
3257 }
3258 }
3259
3260 if (HasErrors)
3261 return 0;
3262
3263 // Save results
3264 Built.IterationVarRef = IV.get();
3265 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00003266 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003267 Built.CalcLastIteration =
3268 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003269 Built.PreCond = PreCond.get();
3270 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003271 Built.Init = Init.get();
3272 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00003273 Built.LB = LB.get();
3274 Built.UB = UB.get();
3275 Built.IL = IL.get();
3276 Built.ST = ST.get();
3277 Built.EUB = EUB.get();
3278 Built.NLB = NextLB.get();
3279 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003280
Alexey Bataevabfc0692014-06-25 06:52:00 +00003281 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003282}
3283
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003284static Expr *GetCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Alexey Bataevc925aa32015-04-27 08:00:32 +00003285 auto &&CollapseFilter = [](const OMPClause *C) -> bool {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003286 return C->getClauseKind() == OMPC_collapse;
3287 };
3288 OMPExecutableDirective::filtered_clause_iterator<decltype(CollapseFilter)> I(
Alexey Bataevc925aa32015-04-27 08:00:32 +00003289 Clauses, std::move(CollapseFilter));
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003290 if (I)
3291 return cast<OMPCollapseClause>(*I)->getNumForLoops();
3292 return nullptr;
3293}
3294
Alexey Bataev4acb8592014-07-07 13:01:15 +00003295StmtResult Sema::ActOnOpenMPSimdDirective(
3296 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3297 SourceLocation EndLoc,
3298 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmanc6388682014-12-15 07:07:06 +00003299 OMPLoopDirective::HelperExprs B;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003300 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003301 unsigned NestedLoopCount =
3302 CheckOpenMPLoop(OMPD_simd, GetCollapseNumberExpr(Clauses), AStmt, *this,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003303 *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00003304 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003305 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003306
Alexander Musmana5f070a2014-10-01 06:03:56 +00003307 assert((CurContext->isDependentContext() || B.builtAll()) &&
3308 "omp simd loop exprs were not built");
3309
Alexander Musman3276a272015-03-21 10:12:56 +00003310 if (!CurContext->isDependentContext()) {
3311 // Finalize the clauses that need pre-built expressions for CodeGen.
3312 for (auto C : Clauses) {
3313 if (auto LC = dyn_cast<OMPLinearClause>(C))
3314 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3315 B.NumIterations, *this, CurScope))
3316 return StmtError();
3317 }
3318 }
3319
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003320 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003321 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
3322 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003323}
3324
Alexey Bataev4acb8592014-07-07 13:01:15 +00003325StmtResult Sema::ActOnOpenMPForDirective(
3326 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3327 SourceLocation EndLoc,
3328 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmanc6388682014-12-15 07:07:06 +00003329 OMPLoopDirective::HelperExprs B;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003330 // In presence of clause 'collapse', it will define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003331 unsigned NestedLoopCount =
3332 CheckOpenMPLoop(OMPD_for, GetCollapseNumberExpr(Clauses), AStmt, *this,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003333 *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00003334 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00003335 return StmtError();
3336
Alexander Musmana5f070a2014-10-01 06:03:56 +00003337 assert((CurContext->isDependentContext() || B.builtAll()) &&
3338 "omp for loop exprs were not built");
3339
Alexey Bataevf29276e2014-06-18 04:14:57 +00003340 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003341 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
3342 Clauses, AStmt, B);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003343}
3344
Alexander Musmanf82886e2014-09-18 05:12:34 +00003345StmtResult Sema::ActOnOpenMPForSimdDirective(
3346 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3347 SourceLocation EndLoc,
3348 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexander Musmanc6388682014-12-15 07:07:06 +00003349 OMPLoopDirective::HelperExprs B;
Alexander Musmanf82886e2014-09-18 05:12:34 +00003350 // In presence of clause 'collapse', it will define the nested loops number.
3351 unsigned NestedLoopCount =
3352 CheckOpenMPLoop(OMPD_for_simd, GetCollapseNumberExpr(Clauses), AStmt,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003353 *this, *DSAStack, VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00003354 if (NestedLoopCount == 0)
3355 return StmtError();
3356
Alexander Musmanc6388682014-12-15 07:07:06 +00003357 assert((CurContext->isDependentContext() || B.builtAll()) &&
3358 "omp for simd loop exprs were not built");
3359
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00003360 if (!CurContext->isDependentContext()) {
3361 // Finalize the clauses that need pre-built expressions for CodeGen.
3362 for (auto C : Clauses) {
3363 if (auto LC = dyn_cast<OMPLinearClause>(C))
3364 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3365 B.NumIterations, *this, CurScope))
3366 return StmtError();
3367 }
3368 }
3369
Alexander Musmanf82886e2014-09-18 05:12:34 +00003370 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003371 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
3372 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00003373}
3374
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003375StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
3376 Stmt *AStmt,
3377 SourceLocation StartLoc,
3378 SourceLocation EndLoc) {
3379 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3380 auto BaseStmt = AStmt;
3381 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
3382 BaseStmt = CS->getCapturedStmt();
3383 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
3384 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00003385 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003386 return StmtError();
3387 // All associated statements must be '#pragma omp section' except for
3388 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00003389 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003390 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
3391 if (SectionStmt)
3392 Diag(SectionStmt->getLocStart(),
3393 diag::err_omp_sections_substmt_not_section);
3394 return StmtError();
3395 }
3396 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003397 } else {
3398 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
3399 return StmtError();
3400 }
3401
3402 getCurFunction()->setHasBranchProtectedScope();
3403
3404 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses,
3405 AStmt);
3406}
3407
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003408StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
3409 SourceLocation StartLoc,
3410 SourceLocation EndLoc) {
3411 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3412
3413 getCurFunction()->setHasBranchProtectedScope();
3414
3415 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt);
3416}
3417
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003418StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
3419 Stmt *AStmt,
3420 SourceLocation StartLoc,
3421 SourceLocation EndLoc) {
Alexey Bataev74a05c92014-07-15 02:55:09 +00003422 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3423
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003424 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00003425
Alexey Bataev3255bf32015-01-19 05:20:46 +00003426 // OpenMP [2.7.3, single Construct, Restrictions]
3427 // The copyprivate clause must not be used with the nowait clause.
3428 OMPClause *Nowait = nullptr;
3429 OMPClause *Copyprivate = nullptr;
3430 for (auto *Clause : Clauses) {
3431 if (Clause->getClauseKind() == OMPC_nowait)
3432 Nowait = Clause;
3433 else if (Clause->getClauseKind() == OMPC_copyprivate)
3434 Copyprivate = Clause;
3435 if (Copyprivate && Nowait) {
3436 Diag(Copyprivate->getLocStart(),
3437 diag::err_omp_single_copyprivate_with_nowait);
3438 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
3439 return StmtError();
3440 }
3441 }
3442
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003443 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3444}
3445
Alexander Musman80c22892014-07-17 08:54:58 +00003446StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
3447 SourceLocation StartLoc,
3448 SourceLocation EndLoc) {
3449 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3450
3451 getCurFunction()->setHasBranchProtectedScope();
3452
3453 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
3454}
3455
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003456StmtResult
3457Sema::ActOnOpenMPCriticalDirective(const DeclarationNameInfo &DirName,
3458 Stmt *AStmt, SourceLocation StartLoc,
3459 SourceLocation EndLoc) {
3460 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3461
3462 getCurFunction()->setHasBranchProtectedScope();
3463
3464 return OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
3465 AStmt);
3466}
3467
Alexey Bataev4acb8592014-07-07 13:01:15 +00003468StmtResult Sema::ActOnOpenMPParallelForDirective(
3469 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3470 SourceLocation EndLoc,
3471 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
3472 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3473 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3474 // 1.2.2 OpenMP Language Terminology
3475 // Structured block - An executable statement with a single entry at the
3476 // top and a single exit at the bottom.
3477 // The point of exit cannot be a branch out of the structured block.
3478 // longjmp() and throw() must not violate the entry/exit criteria.
3479 CS->getCapturedDecl()->setNothrow();
3480
Alexander Musmanc6388682014-12-15 07:07:06 +00003481 OMPLoopDirective::HelperExprs B;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003482 // In presence of clause 'collapse', it will define the nested loops number.
3483 unsigned NestedLoopCount =
3484 CheckOpenMPLoop(OMPD_parallel_for, GetCollapseNumberExpr(Clauses), AStmt,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003485 *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003486 if (NestedLoopCount == 0)
3487 return StmtError();
3488
Alexander Musmana5f070a2014-10-01 06:03:56 +00003489 assert((CurContext->isDependentContext() || B.builtAll()) &&
3490 "omp parallel for loop exprs were not built");
3491
Alexey Bataev4acb8592014-07-07 13:01:15 +00003492 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003493 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
3494 NestedLoopCount, Clauses, AStmt, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003495}
3496
Alexander Musmane4e893b2014-09-23 09:33:00 +00003497StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
3498 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3499 SourceLocation EndLoc,
3500 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
3501 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3502 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3503 // 1.2.2 OpenMP Language Terminology
3504 // Structured block - An executable statement with a single entry at the
3505 // top and a single exit at the bottom.
3506 // The point of exit cannot be a branch out of the structured block.
3507 // longjmp() and throw() must not violate the entry/exit criteria.
3508 CS->getCapturedDecl()->setNothrow();
3509
Alexander Musmanc6388682014-12-15 07:07:06 +00003510 OMPLoopDirective::HelperExprs B;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003511 // In presence of clause 'collapse', it will define the nested loops number.
3512 unsigned NestedLoopCount =
3513 CheckOpenMPLoop(OMPD_parallel_for_simd, GetCollapseNumberExpr(Clauses),
Alexander Musmana5f070a2014-10-01 06:03:56 +00003514 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003515 if (NestedLoopCount == 0)
3516 return StmtError();
3517
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00003518 if (!CurContext->isDependentContext()) {
3519 // Finalize the clauses that need pre-built expressions for CodeGen.
3520 for (auto C : Clauses) {
3521 if (auto LC = dyn_cast<OMPLinearClause>(C))
3522 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3523 B.NumIterations, *this, CurScope))
3524 return StmtError();
3525 }
3526 }
3527
Alexander Musmane4e893b2014-09-23 09:33:00 +00003528 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003529 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00003530 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003531}
3532
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003533StmtResult
3534Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
3535 Stmt *AStmt, SourceLocation StartLoc,
3536 SourceLocation EndLoc) {
3537 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3538 auto BaseStmt = AStmt;
3539 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
3540 BaseStmt = CS->getCapturedStmt();
3541 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
3542 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00003543 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003544 return StmtError();
3545 // All associated statements must be '#pragma omp section' except for
3546 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00003547 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003548 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
3549 if (SectionStmt)
3550 Diag(SectionStmt->getLocStart(),
3551 diag::err_omp_parallel_sections_substmt_not_section);
3552 return StmtError();
3553 }
3554 }
3555 } else {
3556 Diag(AStmt->getLocStart(),
3557 diag::err_omp_parallel_sections_not_compound_stmt);
3558 return StmtError();
3559 }
3560
3561 getCurFunction()->setHasBranchProtectedScope();
3562
3563 return OMPParallelSectionsDirective::Create(Context, StartLoc, EndLoc,
3564 Clauses, AStmt);
3565}
3566
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003567StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
3568 Stmt *AStmt, SourceLocation StartLoc,
3569 SourceLocation EndLoc) {
3570 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3571 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
3572 // 1.2.2 OpenMP Language Terminology
3573 // Structured block - An executable statement with a single entry at the
3574 // top and a single exit at the bottom.
3575 // The point of exit cannot be a branch out of the structured block.
3576 // longjmp() and throw() must not violate the entry/exit criteria.
3577 CS->getCapturedDecl()->setNothrow();
3578
3579 getCurFunction()->setHasBranchProtectedScope();
3580
3581 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
3582}
3583
Alexey Bataev68446b72014-07-18 07:47:19 +00003584StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
3585 SourceLocation EndLoc) {
3586 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
3587}
3588
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003589StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
3590 SourceLocation EndLoc) {
3591 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
3592}
3593
Alexey Bataev2df347a2014-07-18 10:17:07 +00003594StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
3595 SourceLocation EndLoc) {
3596 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
3597}
3598
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003599StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
3600 SourceLocation StartLoc,
3601 SourceLocation EndLoc) {
3602 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3603
3604 getCurFunction()->setHasBranchProtectedScope();
3605
3606 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
3607}
3608
Alexey Bataev6125da92014-07-21 11:26:11 +00003609StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
3610 SourceLocation StartLoc,
3611 SourceLocation EndLoc) {
3612 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
3613 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
3614}
3615
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003616StmtResult Sema::ActOnOpenMPOrderedDirective(Stmt *AStmt,
3617 SourceLocation StartLoc,
3618 SourceLocation EndLoc) {
3619 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
3620
3621 getCurFunction()->setHasBranchProtectedScope();
3622
3623 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, AStmt);
3624}
3625
Alexey Bataev1d160b12015-03-13 12:27:31 +00003626namespace {
3627/// \brief Helper class for checking expression in 'omp atomic [update]'
3628/// construct.
3629class OpenMPAtomicUpdateChecker {
3630 /// \brief Error results for atomic update expressions.
3631 enum ExprAnalysisErrorCode {
3632 /// \brief A statement is not an expression statement.
3633 NotAnExpression,
3634 /// \brief Expression is not builtin binary or unary operation.
3635 NotABinaryOrUnaryExpression,
3636 /// \brief Unary operation is not post-/pre- increment/decrement operation.
3637 NotAnUnaryIncDecExpression,
3638 /// \brief An expression is not of scalar type.
3639 NotAScalarType,
3640 /// \brief A binary operation is not an assignment operation.
3641 NotAnAssignmentOp,
3642 /// \brief RHS part of the binary operation is not a binary expression.
3643 NotABinaryExpression,
3644 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
3645 /// expression.
3646 NotABinaryOperator,
3647 /// \brief RHS binary operation does not have reference to the updated LHS
3648 /// part.
3649 NotAnUpdateExpression,
3650 /// \brief No errors is found.
3651 NoError
3652 };
3653 /// \brief Reference to Sema.
3654 Sema &SemaRef;
3655 /// \brief A location for note diagnostics (when error is found).
3656 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003657 /// \brief 'x' lvalue part of the source atomic expression.
3658 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003659 /// \brief 'expr' rvalue part of the source atomic expression.
3660 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00003661 /// \brief Helper expression of the form
3662 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
3663 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
3664 Expr *UpdateExpr;
3665 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
3666 /// important for non-associative operations.
3667 bool IsXLHSInRHSPart;
3668 BinaryOperatorKind Op;
3669 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00003670 /// \brief true if the source expression is a postfix unary operation, false
3671 /// if it is a prefix unary operation.
3672 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003673
3674public:
3675 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00003676 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00003677 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00003678 /// \brief Check specified statement that it is suitable for 'atomic update'
3679 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00003680 /// expression. If DiagId and NoteId == 0, then only check is performed
3681 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00003682 /// \param DiagId Diagnostic which should be emitted if error is found.
3683 /// \param NoteId Diagnostic note for the main error message.
3684 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00003685 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00003686 /// \brief Return the 'x' lvalue part of the source atomic expression.
3687 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00003688 /// \brief Return the 'expr' rvalue part of the source atomic expression.
3689 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00003690 /// \brief Return the update expression used in calculation of the updated
3691 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
3692 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
3693 Expr *getUpdateExpr() const { return UpdateExpr; }
3694 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
3695 /// false otherwise.
3696 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
3697
Alexey Bataevb78ca832015-04-01 03:33:17 +00003698 /// \brief true if the source expression is a postfix unary operation, false
3699 /// if it is a prefix unary operation.
3700 bool isPostfixUpdate() const { return IsPostfixUpdate; }
3701
Alexey Bataev1d160b12015-03-13 12:27:31 +00003702private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00003703 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
3704 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00003705};
3706} // namespace
3707
3708bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
3709 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
3710 ExprAnalysisErrorCode ErrorFound = NoError;
3711 SourceLocation ErrorLoc, NoteLoc;
3712 SourceRange ErrorRange, NoteRange;
3713 // Allowed constructs are:
3714 // x = x binop expr;
3715 // x = expr binop x;
3716 if (AtomicBinOp->getOpcode() == BO_Assign) {
3717 X = AtomicBinOp->getLHS();
3718 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
3719 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
3720 if (AtomicInnerBinOp->isMultiplicativeOp() ||
3721 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
3722 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00003723 Op = AtomicInnerBinOp->getOpcode();
3724 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00003725 auto *LHS = AtomicInnerBinOp->getLHS();
3726 auto *RHS = AtomicInnerBinOp->getRHS();
3727 llvm::FoldingSetNodeID XId, LHSId, RHSId;
3728 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
3729 /*Canonical=*/true);
3730 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
3731 /*Canonical=*/true);
3732 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
3733 /*Canonical=*/true);
3734 if (XId == LHSId) {
3735 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00003736 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003737 } else if (XId == RHSId) {
3738 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00003739 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003740 } else {
3741 ErrorLoc = AtomicInnerBinOp->getExprLoc();
3742 ErrorRange = AtomicInnerBinOp->getSourceRange();
3743 NoteLoc = X->getExprLoc();
3744 NoteRange = X->getSourceRange();
3745 ErrorFound = NotAnUpdateExpression;
3746 }
3747 } else {
3748 ErrorLoc = AtomicInnerBinOp->getExprLoc();
3749 ErrorRange = AtomicInnerBinOp->getSourceRange();
3750 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
3751 NoteRange = SourceRange(NoteLoc, NoteLoc);
3752 ErrorFound = NotABinaryOperator;
3753 }
3754 } else {
3755 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
3756 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
3757 ErrorFound = NotABinaryExpression;
3758 }
3759 } else {
3760 ErrorLoc = AtomicBinOp->getExprLoc();
3761 ErrorRange = AtomicBinOp->getSourceRange();
3762 NoteLoc = AtomicBinOp->getOperatorLoc();
3763 NoteRange = SourceRange(NoteLoc, NoteLoc);
3764 ErrorFound = NotAnAssignmentOp;
3765 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00003766 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00003767 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
3768 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
3769 return true;
3770 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00003771 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00003772 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003773}
3774
3775bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
3776 unsigned NoteId) {
3777 ExprAnalysisErrorCode ErrorFound = NoError;
3778 SourceLocation ErrorLoc, NoteLoc;
3779 SourceRange ErrorRange, NoteRange;
3780 // Allowed constructs are:
3781 // x++;
3782 // x--;
3783 // ++x;
3784 // --x;
3785 // x binop= expr;
3786 // x = x binop expr;
3787 // x = expr binop x;
3788 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
3789 AtomicBody = AtomicBody->IgnoreParenImpCasts();
3790 if (AtomicBody->getType()->isScalarType() ||
3791 AtomicBody->isInstantiationDependent()) {
3792 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
3793 AtomicBody->IgnoreParenImpCasts())) {
3794 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00003795 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00003796 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00003797 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00003798 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00003799 X = AtomicCompAssignOp->getLHS();
3800 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003801 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
3802 AtomicBody->IgnoreParenImpCasts())) {
3803 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00003804 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
3805 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003806 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00003807 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
3808 // Check for Unary Operation
3809 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00003810 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00003811 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
3812 OpLoc = AtomicUnaryOp->getOperatorLoc();
3813 X = AtomicUnaryOp->getSubExpr();
3814 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
3815 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003816 } else {
3817 ErrorFound = NotAnUnaryIncDecExpression;
3818 ErrorLoc = AtomicUnaryOp->getExprLoc();
3819 ErrorRange = AtomicUnaryOp->getSourceRange();
3820 NoteLoc = AtomicUnaryOp->getOperatorLoc();
3821 NoteRange = SourceRange(NoteLoc, NoteLoc);
3822 }
3823 } else {
3824 ErrorFound = NotABinaryOrUnaryExpression;
3825 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
3826 NoteRange = ErrorRange = AtomicBody->getSourceRange();
3827 }
3828 } else {
3829 ErrorFound = NotAScalarType;
3830 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
3831 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
3832 }
3833 } else {
3834 ErrorFound = NotAnExpression;
3835 NoteLoc = ErrorLoc = S->getLocStart();
3836 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
3837 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00003838 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00003839 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
3840 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
3841 return true;
3842 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00003843 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00003844 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00003845 // Build an update expression of form 'OpaqueValueExpr(x) binop
3846 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
3847 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
3848 auto *OVEX = new (SemaRef.getASTContext())
3849 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
3850 auto *OVEExpr = new (SemaRef.getASTContext())
3851 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
3852 auto Update =
3853 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
3854 IsXLHSInRHSPart ? OVEExpr : OVEX);
3855 if (Update.isInvalid())
3856 return true;
3857 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
3858 Sema::AA_Casting);
3859 if (Update.isInvalid())
3860 return true;
3861 UpdateExpr = Update.get();
3862 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00003863 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00003864}
3865
Alexey Bataev0162e452014-07-22 10:10:35 +00003866StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
3867 Stmt *AStmt,
3868 SourceLocation StartLoc,
3869 SourceLocation EndLoc) {
3870 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003871 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00003872 // 1.2.2 OpenMP Language Terminology
3873 // Structured block - An executable statement with a single entry at the
3874 // top and a single exit at the bottom.
3875 // The point of exit cannot be a branch out of the structured block.
3876 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00003877 OpenMPClauseKind AtomicKind = OMPC_unknown;
3878 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003879 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00003880 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00003881 C->getClauseKind() == OMPC_update ||
3882 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00003883 if (AtomicKind != OMPC_unknown) {
3884 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
3885 << SourceRange(C->getLocStart(), C->getLocEnd());
3886 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
3887 << getOpenMPClauseName(AtomicKind);
3888 } else {
3889 AtomicKind = C->getClauseKind();
3890 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00003891 }
3892 }
3893 }
Alexey Bataev62cec442014-11-18 10:14:22 +00003894
Alexey Bataev459dec02014-07-24 06:46:57 +00003895 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00003896 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
3897 Body = EWC->getSubExpr();
3898
Alexey Bataev62cec442014-11-18 10:14:22 +00003899 Expr *X = nullptr;
3900 Expr *V = nullptr;
3901 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00003902 Expr *UE = nullptr;
3903 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00003904 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00003905 // OpenMP [2.12.6, atomic Construct]
3906 // In the next expressions:
3907 // * x and v (as applicable) are both l-value expressions with scalar type.
3908 // * During the execution of an atomic region, multiple syntactic
3909 // occurrences of x must designate the same storage location.
3910 // * Neither of v and expr (as applicable) may access the storage location
3911 // designated by x.
3912 // * Neither of x and expr (as applicable) may access the storage location
3913 // designated by v.
3914 // * expr is an expression with scalar type.
3915 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
3916 // * binop, binop=, ++, and -- are not overloaded operators.
3917 // * The expression x binop expr must be numerically equivalent to x binop
3918 // (expr). This requirement is satisfied if the operators in expr have
3919 // precedence greater than binop, or by using parentheses around expr or
3920 // subexpressions of expr.
3921 // * The expression expr binop x must be numerically equivalent to (expr)
3922 // binop x. This requirement is satisfied if the operators in expr have
3923 // precedence equal to or greater than binop, or by using parentheses around
3924 // expr or subexpressions of expr.
3925 // * For forms that allow multiple occurrences of x, the number of times
3926 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00003927 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00003928 enum {
3929 NotAnExpression,
3930 NotAnAssignmentOp,
3931 NotAScalarType,
3932 NotAnLValue,
3933 NoError
3934 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00003935 SourceLocation ErrorLoc, NoteLoc;
3936 SourceRange ErrorRange, NoteRange;
3937 // If clause is read:
3938 // v = x;
3939 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
3940 auto AtomicBinOp =
3941 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
3942 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
3943 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
3944 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
3945 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
3946 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
3947 if (!X->isLValue() || !V->isLValue()) {
3948 auto NotLValueExpr = X->isLValue() ? V : X;
3949 ErrorFound = NotAnLValue;
3950 ErrorLoc = AtomicBinOp->getExprLoc();
3951 ErrorRange = AtomicBinOp->getSourceRange();
3952 NoteLoc = NotLValueExpr->getExprLoc();
3953 NoteRange = NotLValueExpr->getSourceRange();
3954 }
3955 } else if (!X->isInstantiationDependent() ||
3956 !V->isInstantiationDependent()) {
3957 auto NotScalarExpr =
3958 (X->isInstantiationDependent() || X->getType()->isScalarType())
3959 ? V
3960 : X;
3961 ErrorFound = NotAScalarType;
3962 ErrorLoc = AtomicBinOp->getExprLoc();
3963 ErrorRange = AtomicBinOp->getSourceRange();
3964 NoteLoc = NotScalarExpr->getExprLoc();
3965 NoteRange = NotScalarExpr->getSourceRange();
3966 }
3967 } else {
3968 ErrorFound = NotAnAssignmentOp;
3969 ErrorLoc = AtomicBody->getExprLoc();
3970 ErrorRange = AtomicBody->getSourceRange();
3971 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
3972 : AtomicBody->getExprLoc();
3973 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
3974 : AtomicBody->getSourceRange();
3975 }
3976 } else {
3977 ErrorFound = NotAnExpression;
3978 NoteLoc = ErrorLoc = Body->getLocStart();
3979 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00003980 }
Alexey Bataev62cec442014-11-18 10:14:22 +00003981 if (ErrorFound != NoError) {
3982 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
3983 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00003984 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
3985 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00003986 return StmtError();
3987 } else if (CurContext->isDependentContext())
3988 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00003989 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00003990 enum {
3991 NotAnExpression,
3992 NotAnAssignmentOp,
3993 NotAScalarType,
3994 NotAnLValue,
3995 NoError
3996 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00003997 SourceLocation ErrorLoc, NoteLoc;
3998 SourceRange ErrorRange, NoteRange;
3999 // If clause is write:
4000 // x = expr;
4001 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
4002 auto AtomicBinOp =
4003 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
4004 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00004005 X = AtomicBinOp->getLHS();
4006 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00004007 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
4008 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
4009 if (!X->isLValue()) {
4010 ErrorFound = NotAnLValue;
4011 ErrorLoc = AtomicBinOp->getExprLoc();
4012 ErrorRange = AtomicBinOp->getSourceRange();
4013 NoteLoc = X->getExprLoc();
4014 NoteRange = X->getSourceRange();
4015 }
4016 } else if (!X->isInstantiationDependent() ||
4017 !E->isInstantiationDependent()) {
4018 auto NotScalarExpr =
4019 (X->isInstantiationDependent() || X->getType()->isScalarType())
4020 ? E
4021 : X;
4022 ErrorFound = NotAScalarType;
4023 ErrorLoc = AtomicBinOp->getExprLoc();
4024 ErrorRange = AtomicBinOp->getSourceRange();
4025 NoteLoc = NotScalarExpr->getExprLoc();
4026 NoteRange = NotScalarExpr->getSourceRange();
4027 }
4028 } else {
4029 ErrorFound = NotAnAssignmentOp;
4030 ErrorLoc = AtomicBody->getExprLoc();
4031 ErrorRange = AtomicBody->getSourceRange();
4032 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
4033 : AtomicBody->getExprLoc();
4034 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
4035 : AtomicBody->getSourceRange();
4036 }
4037 } else {
4038 ErrorFound = NotAnExpression;
4039 NoteLoc = ErrorLoc = Body->getLocStart();
4040 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00004041 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00004042 if (ErrorFound != NoError) {
4043 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
4044 << ErrorRange;
4045 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
4046 << NoteRange;
4047 return StmtError();
4048 } else if (CurContext->isDependentContext())
4049 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00004050 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004051 // If clause is update:
4052 // x++;
4053 // x--;
4054 // ++x;
4055 // --x;
4056 // x binop= expr;
4057 // x = x binop expr;
4058 // x = expr binop x;
4059 OpenMPAtomicUpdateChecker Checker(*this);
4060 if (Checker.checkStatement(
4061 Body, (AtomicKind == OMPC_update)
4062 ? diag::err_omp_atomic_update_not_expression_statement
4063 : diag::err_omp_atomic_not_expression_statement,
4064 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00004065 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00004066 if (!CurContext->isDependentContext()) {
4067 E = Checker.getExpr();
4068 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00004069 UE = Checker.getUpdateExpr();
4070 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00004071 }
Alexey Bataev459dec02014-07-24 06:46:57 +00004072 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004073 enum {
4074 NotAnAssignmentOp,
4075 NotACompoundStatement,
4076 NotTwoSubstatements,
4077 NotASpecificExpression,
4078 NoError
4079 } ErrorFound = NoError;
4080 SourceLocation ErrorLoc, NoteLoc;
4081 SourceRange ErrorRange, NoteRange;
4082 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
4083 // If clause is a capture:
4084 // v = x++;
4085 // v = x--;
4086 // v = ++x;
4087 // v = --x;
4088 // v = x binop= expr;
4089 // v = x = x binop expr;
4090 // v = x = expr binop x;
4091 auto *AtomicBinOp =
4092 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
4093 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
4094 V = AtomicBinOp->getLHS();
4095 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
4096 OpenMPAtomicUpdateChecker Checker(*this);
4097 if (Checker.checkStatement(
4098 Body, diag::err_omp_atomic_capture_not_expression_statement,
4099 diag::note_omp_atomic_update))
4100 return StmtError();
4101 E = Checker.getExpr();
4102 X = Checker.getX();
4103 UE = Checker.getUpdateExpr();
4104 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
4105 IsPostfixUpdate = Checker.isPostfixUpdate();
4106 } else {
4107 ErrorLoc = AtomicBody->getExprLoc();
4108 ErrorRange = AtomicBody->getSourceRange();
4109 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
4110 : AtomicBody->getExprLoc();
4111 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
4112 : AtomicBody->getSourceRange();
4113 ErrorFound = NotAnAssignmentOp;
4114 }
4115 if (ErrorFound != NoError) {
4116 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
4117 << ErrorRange;
4118 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
4119 return StmtError();
4120 } else if (CurContext->isDependentContext()) {
4121 UE = V = E = X = nullptr;
4122 }
4123 } else {
4124 // If clause is a capture:
4125 // { v = x; x = expr; }
4126 // { v = x; x++; }
4127 // { v = x; x--; }
4128 // { v = x; ++x; }
4129 // { v = x; --x; }
4130 // { v = x; x binop= expr; }
4131 // { v = x; x = x binop expr; }
4132 // { v = x; x = expr binop x; }
4133 // { x++; v = x; }
4134 // { x--; v = x; }
4135 // { ++x; v = x; }
4136 // { --x; v = x; }
4137 // { x binop= expr; v = x; }
4138 // { x = x binop expr; v = x; }
4139 // { x = expr binop x; v = x; }
4140 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
4141 // Check that this is { expr1; expr2; }
4142 if (CS->size() == 2) {
4143 auto *First = CS->body_front();
4144 auto *Second = CS->body_back();
4145 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
4146 First = EWC->getSubExpr()->IgnoreParenImpCasts();
4147 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
4148 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
4149 // Need to find what subexpression is 'v' and what is 'x'.
4150 OpenMPAtomicUpdateChecker Checker(*this);
4151 bool IsUpdateExprFound = !Checker.checkStatement(Second);
4152 BinaryOperator *BinOp = nullptr;
4153 if (IsUpdateExprFound) {
4154 BinOp = dyn_cast<BinaryOperator>(First);
4155 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
4156 }
4157 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
4158 // { v = x; x++; }
4159 // { v = x; x--; }
4160 // { v = x; ++x; }
4161 // { v = x; --x; }
4162 // { v = x; x binop= expr; }
4163 // { v = x; x = x binop expr; }
4164 // { v = x; x = expr binop x; }
4165 // Check that the first expression has form v = x.
4166 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
4167 llvm::FoldingSetNodeID XId, PossibleXId;
4168 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
4169 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
4170 IsUpdateExprFound = XId == PossibleXId;
4171 if (IsUpdateExprFound) {
4172 V = BinOp->getLHS();
4173 X = Checker.getX();
4174 E = Checker.getExpr();
4175 UE = Checker.getUpdateExpr();
4176 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00004177 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004178 }
4179 }
4180 if (!IsUpdateExprFound) {
4181 IsUpdateExprFound = !Checker.checkStatement(First);
4182 BinOp = nullptr;
4183 if (IsUpdateExprFound) {
4184 BinOp = dyn_cast<BinaryOperator>(Second);
4185 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
4186 }
4187 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
4188 // { x++; v = x; }
4189 // { x--; v = x; }
4190 // { ++x; v = x; }
4191 // { --x; v = x; }
4192 // { x binop= expr; v = x; }
4193 // { x = x binop expr; v = x; }
4194 // { x = expr binop x; v = x; }
4195 // Check that the second expression has form v = x.
4196 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
4197 llvm::FoldingSetNodeID XId, PossibleXId;
4198 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
4199 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
4200 IsUpdateExprFound = XId == PossibleXId;
4201 if (IsUpdateExprFound) {
4202 V = BinOp->getLHS();
4203 X = Checker.getX();
4204 E = Checker.getExpr();
4205 UE = Checker.getUpdateExpr();
4206 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00004207 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004208 }
4209 }
4210 }
4211 if (!IsUpdateExprFound) {
4212 // { v = x; x = expr; }
4213 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
4214 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
4215 ErrorFound = NotAnAssignmentOp;
4216 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
4217 : First->getLocStart();
4218 NoteRange = ErrorRange = FirstBinOp
4219 ? FirstBinOp->getSourceRange()
4220 : SourceRange(ErrorLoc, ErrorLoc);
4221 } else {
4222 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
4223 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
4224 ErrorFound = NotAnAssignmentOp;
4225 NoteLoc = ErrorLoc = SecondBinOp ? SecondBinOp->getOperatorLoc()
4226 : Second->getLocStart();
4227 NoteRange = ErrorRange = SecondBinOp
4228 ? SecondBinOp->getSourceRange()
4229 : SourceRange(ErrorLoc, ErrorLoc);
4230 } else {
4231 auto *PossibleXRHSInFirst =
4232 FirstBinOp->getRHS()->IgnoreParenImpCasts();
4233 auto *PossibleXLHSInSecond =
4234 SecondBinOp->getLHS()->IgnoreParenImpCasts();
4235 llvm::FoldingSetNodeID X1Id, X2Id;
4236 PossibleXRHSInFirst->Profile(X1Id, Context, /*Canonical=*/true);
4237 PossibleXLHSInSecond->Profile(X2Id, Context,
4238 /*Canonical=*/true);
4239 IsUpdateExprFound = X1Id == X2Id;
4240 if (IsUpdateExprFound) {
4241 V = FirstBinOp->getLHS();
4242 X = SecondBinOp->getLHS();
4243 E = SecondBinOp->getRHS();
4244 UE = nullptr;
4245 IsXLHSInRHSPart = false;
4246 IsPostfixUpdate = true;
4247 } else {
4248 ErrorFound = NotASpecificExpression;
4249 ErrorLoc = FirstBinOp->getExprLoc();
4250 ErrorRange = FirstBinOp->getSourceRange();
4251 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
4252 NoteRange = SecondBinOp->getRHS()->getSourceRange();
4253 }
4254 }
4255 }
4256 }
4257 } else {
4258 NoteLoc = ErrorLoc = Body->getLocStart();
4259 NoteRange = ErrorRange =
4260 SourceRange(Body->getLocStart(), Body->getLocStart());
4261 ErrorFound = NotTwoSubstatements;
4262 }
4263 } else {
4264 NoteLoc = ErrorLoc = Body->getLocStart();
4265 NoteRange = ErrorRange =
4266 SourceRange(Body->getLocStart(), Body->getLocStart());
4267 ErrorFound = NotACompoundStatement;
4268 }
4269 if (ErrorFound != NoError) {
4270 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
4271 << ErrorRange;
4272 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
4273 return StmtError();
4274 } else if (CurContext->isDependentContext()) {
4275 UE = V = E = X = nullptr;
4276 }
Alexey Bataev459dec02014-07-24 06:46:57 +00004277 }
Alexey Bataevdea47612014-07-23 07:46:59 +00004278 }
Alexey Bataev0162e452014-07-22 10:10:35 +00004279
4280 getCurFunction()->setHasBranchProtectedScope();
4281
Alexey Bataev62cec442014-11-18 10:14:22 +00004282 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00004283 X, V, E, UE, IsXLHSInRHSPart,
4284 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00004285}
4286
Alexey Bataev0bd520b2014-09-19 08:19:49 +00004287StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
4288 Stmt *AStmt,
4289 SourceLocation StartLoc,
4290 SourceLocation EndLoc) {
4291 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
4292
Alexey Bataev13314bf2014-10-09 04:18:56 +00004293 // OpenMP [2.16, Nesting of Regions]
4294 // If specified, a teams construct must be contained within a target
4295 // construct. That target construct must contain no statements or directives
4296 // outside of the teams construct.
4297 if (DSAStack->hasInnerTeamsRegion()) {
4298 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
4299 bool OMPTeamsFound = true;
4300 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
4301 auto I = CS->body_begin();
4302 while (I != CS->body_end()) {
4303 auto OED = dyn_cast<OMPExecutableDirective>(*I);
4304 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
4305 OMPTeamsFound = false;
4306 break;
4307 }
4308 ++I;
4309 }
4310 assert(I != CS->body_end() && "Not found statement");
4311 S = *I;
4312 }
4313 if (!OMPTeamsFound) {
4314 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
4315 Diag(DSAStack->getInnerTeamsRegionLoc(),
4316 diag::note_omp_nested_teams_construct_here);
4317 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
4318 << isa<OMPExecutableDirective>(S);
4319 return StmtError();
4320 }
4321 }
4322
Alexey Bataev0bd520b2014-09-19 08:19:49 +00004323 getCurFunction()->setHasBranchProtectedScope();
4324
4325 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4326}
4327
Michael Wong65f367f2015-07-21 13:44:28 +00004328StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
4329 Stmt *AStmt,
4330 SourceLocation StartLoc,
4331 SourceLocation EndLoc) {
4332 getCurFunction()->setHasBranchProtectedScope();
4333
4334 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
4335 AStmt);
4336}
4337
Alexey Bataev13314bf2014-10-09 04:18:56 +00004338StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
4339 Stmt *AStmt, SourceLocation StartLoc,
4340 SourceLocation EndLoc) {
4341 assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
4342 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4343 // 1.2.2 OpenMP Language Terminology
4344 // Structured block - An executable statement with a single entry at the
4345 // top and a single exit at the bottom.
4346 // The point of exit cannot be a branch out of the structured block.
4347 // longjmp() and throw() must not violate the entry/exit criteria.
4348 CS->getCapturedDecl()->setNothrow();
4349
4350 getCurFunction()->setHasBranchProtectedScope();
4351
4352 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4353}
4354
Alexey Bataev6d4ed052015-07-01 06:57:41 +00004355StmtResult
4356Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
4357 SourceLocation EndLoc,
4358 OpenMPDirectiveKind CancelRegion) {
4359 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
4360 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
4361 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
4362 << getOpenMPDirectiveName(CancelRegion);
4363 return StmtError();
4364 }
4365 if (DSAStack->isParentNowaitRegion()) {
4366 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
4367 return StmtError();
4368 }
4369 if (DSAStack->isParentOrderedRegion()) {
4370 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
4371 return StmtError();
4372 }
4373 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
4374 CancelRegion);
4375}
4376
Alexey Bataev80909872015-07-02 11:25:17 +00004377StmtResult Sema::ActOnOpenMPCancelDirective(SourceLocation StartLoc,
4378 SourceLocation EndLoc,
4379 OpenMPDirectiveKind CancelRegion) {
4380 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
4381 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
4382 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
4383 << getOpenMPDirectiveName(CancelRegion);
4384 return StmtError();
4385 }
4386 if (DSAStack->isParentNowaitRegion()) {
4387 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
4388 return StmtError();
4389 }
4390 if (DSAStack->isParentOrderedRegion()) {
4391 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
4392 return StmtError();
4393 }
4394 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, CancelRegion);
4395}
4396
Alexey Bataeved09d242014-05-28 05:53:51 +00004397OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004398 SourceLocation StartLoc,
4399 SourceLocation LParenLoc,
4400 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004401 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004402 switch (Kind) {
4403 case OMPC_if:
4404 Res = ActOnOpenMPIfClause(Expr, StartLoc, LParenLoc, EndLoc);
4405 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00004406 case OMPC_final:
4407 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
4408 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00004409 case OMPC_num_threads:
4410 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
4411 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00004412 case OMPC_safelen:
4413 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
4414 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00004415 case OMPC_collapse:
4416 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
4417 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004418 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004419 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00004420 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004421 case OMPC_private:
4422 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00004423 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004424 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00004425 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00004426 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00004427 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004428 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00004429 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004430 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00004431 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00004432 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00004433 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004434 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00004435 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004436 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00004437 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00004438 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00004439 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00004440 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00004441 case OMPC_depend:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004442 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004443 llvm_unreachable("Clause is not allowed.");
4444 }
4445 return Res;
4446}
4447
Alexey Bataeved09d242014-05-28 05:53:51 +00004448OMPClause *Sema::ActOnOpenMPIfClause(Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004449 SourceLocation LParenLoc,
4450 SourceLocation EndLoc) {
4451 Expr *ValExpr = Condition;
4452 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
4453 !Condition->isInstantiationDependent() &&
4454 !Condition->containsUnexpandedParameterPack()) {
4455 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00004456 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004457 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004458 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004459
Nikola Smiljanic01a75982014-05-29 10:55:11 +00004460 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004461 }
4462
4463 return new (Context) OMPIfClause(ValExpr, StartLoc, LParenLoc, EndLoc);
4464}
4465
Alexey Bataev3778b602014-07-17 07:32:53 +00004466OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
4467 SourceLocation StartLoc,
4468 SourceLocation LParenLoc,
4469 SourceLocation EndLoc) {
4470 Expr *ValExpr = Condition;
4471 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
4472 !Condition->isInstantiationDependent() &&
4473 !Condition->containsUnexpandedParameterPack()) {
4474 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
4475 Condition->getExprLoc(), Condition);
4476 if (Val.isInvalid())
4477 return nullptr;
4478
4479 ValExpr = Val.get();
4480 }
4481
4482 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
4483}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004484ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
4485 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00004486 if (!Op)
4487 return ExprError();
4488
4489 class IntConvertDiagnoser : public ICEConvertDiagnoser {
4490 public:
4491 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00004492 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00004493 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
4494 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004495 return S.Diag(Loc, diag::err_omp_not_integral) << T;
4496 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004497 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
4498 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004499 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
4500 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004501 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
4502 QualType T,
4503 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004504 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
4505 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004506 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
4507 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004508 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00004509 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00004510 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004511 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
4512 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004513 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
4514 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004515 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
4516 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004517 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00004518 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00004519 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004520 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
4521 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00004522 llvm_unreachable("conversion functions are permitted");
4523 }
4524 } ConvertDiagnoser;
4525 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
4526}
4527
4528OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
4529 SourceLocation StartLoc,
4530 SourceLocation LParenLoc,
4531 SourceLocation EndLoc) {
4532 Expr *ValExpr = NumThreads;
4533 if (!NumThreads->isValueDependent() && !NumThreads->isTypeDependent() &&
Alexey Bataev568a8332014-03-06 06:15:19 +00004534 !NumThreads->containsUnexpandedParameterPack()) {
4535 SourceLocation NumThreadsLoc = NumThreads->getLocStart();
4536 ExprResult Val =
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004537 PerformOpenMPImplicitIntegerConversion(NumThreadsLoc, NumThreads);
Alexey Bataev568a8332014-03-06 06:15:19 +00004538 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004539 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00004540
Nikola Smiljanic01a75982014-05-29 10:55:11 +00004541 ValExpr = Val.get();
Alexey Bataev568a8332014-03-06 06:15:19 +00004542
4543 // OpenMP [2.5, Restrictions]
4544 // The num_threads expression must evaluate to a positive integer value.
4545 llvm::APSInt Result;
Alexey Bataeved09d242014-05-28 05:53:51 +00004546 if (ValExpr->isIntegerConstantExpr(Result, Context) && Result.isSigned() &&
4547 !Result.isStrictlyPositive()) {
Alexey Bataev568a8332014-03-06 06:15:19 +00004548 Diag(NumThreadsLoc, diag::err_omp_negative_expression_in_clause)
4549 << "num_threads" << NumThreads->getSourceRange();
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004550 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00004551 }
4552 }
4553
Alexey Bataeved09d242014-05-28 05:53:51 +00004554 return new (Context)
4555 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00004556}
4557
Alexey Bataev62c87d22014-03-21 04:51:18 +00004558ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
4559 OpenMPClauseKind CKind) {
4560 if (!E)
4561 return ExprError();
4562 if (E->isValueDependent() || E->isTypeDependent() ||
4563 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00004564 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00004565 llvm::APSInt Result;
4566 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
4567 if (ICE.isInvalid())
4568 return ExprError();
4569 if (!Result.isStrictlyPositive()) {
4570 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
4571 << getOpenMPClauseName(CKind) << E->getSourceRange();
4572 return ExprError();
4573 }
Alexander Musman09184fe2014-09-30 05:29:28 +00004574 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
4575 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
4576 << E->getSourceRange();
4577 return ExprError();
4578 }
Alexey Bataev9c821032015-04-30 04:23:23 +00004579 if (CKind == OMPC_collapse) {
4580 DSAStack->setCollapseNumber(Result.getExtValue());
4581 }
Alexey Bataev62c87d22014-03-21 04:51:18 +00004582 return ICE;
4583}
4584
4585OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
4586 SourceLocation LParenLoc,
4587 SourceLocation EndLoc) {
4588 // OpenMP [2.8.1, simd construct, Description]
4589 // The parameter of the safelen clause must be a constant
4590 // positive integer expression.
4591 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
4592 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004593 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00004594 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00004595 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00004596}
4597
Alexander Musman64d33f12014-06-04 07:53:32 +00004598OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
4599 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00004600 SourceLocation LParenLoc,
4601 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00004602 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00004603 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00004604 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00004605 // The parameter of the collapse clause must be a constant
4606 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00004607 ExprResult NumForLoopsResult =
4608 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
4609 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00004610 return nullptr;
4611 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00004612 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00004613}
4614
Alexey Bataeved09d242014-05-28 05:53:51 +00004615OMPClause *Sema::ActOnOpenMPSimpleClause(
4616 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
4617 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004618 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004619 switch (Kind) {
4620 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00004621 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00004622 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
4623 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004624 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004625 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00004626 Res = ActOnOpenMPProcBindClause(
4627 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
4628 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004629 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004630 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00004631 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00004632 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00004633 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00004634 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00004635 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004636 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004637 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00004638 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00004639 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00004640 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00004641 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00004642 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004643 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00004644 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004645 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00004646 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00004647 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00004648 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004649 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00004650 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004651 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00004652 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00004653 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00004654 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00004655 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00004656 case OMPC_depend:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004657 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004658 llvm_unreachable("Clause is not allowed.");
4659 }
4660 return Res;
4661}
4662
4663OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
4664 SourceLocation KindKwLoc,
4665 SourceLocation StartLoc,
4666 SourceLocation LParenLoc,
4667 SourceLocation EndLoc) {
4668 if (Kind == OMPC_DEFAULT_unknown) {
4669 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00004670 static_assert(OMPC_DEFAULT_unknown > 0,
4671 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00004672 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00004673 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004674 Values += "'";
4675 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
4676 Values += "'";
4677 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00004678 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004679 Values += " or ";
4680 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00004681 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004682 break;
4683 default:
4684 Values += Sep;
4685 break;
4686 }
4687 }
4688 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00004689 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004690 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004691 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00004692 switch (Kind) {
4693 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00004694 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004695 break;
4696 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00004697 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00004698 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004699 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004700 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00004701 break;
4702 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004703 return new (Context)
4704 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004705}
4706
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004707OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
4708 SourceLocation KindKwLoc,
4709 SourceLocation StartLoc,
4710 SourceLocation LParenLoc,
4711 SourceLocation EndLoc) {
4712 if (Kind == OMPC_PROC_BIND_unknown) {
4713 std::string Values;
4714 std::string Sep(", ");
4715 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
4716 Values += "'";
4717 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
4718 Values += "'";
4719 switch (i) {
4720 case OMPC_PROC_BIND_unknown - 2:
4721 Values += " or ";
4722 break;
4723 case OMPC_PROC_BIND_unknown - 1:
4724 break;
4725 default:
4726 Values += Sep;
4727 break;
4728 }
4729 }
4730 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00004731 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004732 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004733 }
Alexey Bataeved09d242014-05-28 05:53:51 +00004734 return new (Context)
4735 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00004736}
4737
Alexey Bataev56dafe82014-06-20 07:16:17 +00004738OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
4739 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
4740 SourceLocation StartLoc, SourceLocation LParenLoc,
4741 SourceLocation ArgumentLoc, SourceLocation CommaLoc,
4742 SourceLocation EndLoc) {
4743 OMPClause *Res = nullptr;
4744 switch (Kind) {
4745 case OMPC_schedule:
4746 Res = ActOnOpenMPScheduleClause(
4747 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
4748 LParenLoc, ArgumentLoc, CommaLoc, EndLoc);
4749 break;
4750 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00004751 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00004752 case OMPC_num_threads:
4753 case OMPC_safelen:
4754 case OMPC_collapse:
4755 case OMPC_default:
4756 case OMPC_proc_bind:
4757 case OMPC_private:
4758 case OMPC_firstprivate:
4759 case OMPC_lastprivate:
4760 case OMPC_shared:
4761 case OMPC_reduction:
4762 case OMPC_linear:
4763 case OMPC_aligned:
4764 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00004765 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004766 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00004767 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00004768 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00004769 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00004770 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00004771 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004772 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00004773 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00004774 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00004775 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00004776 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00004777 case OMPC_depend:
Alexey Bataev56dafe82014-06-20 07:16:17 +00004778 case OMPC_unknown:
4779 llvm_unreachable("Clause is not allowed.");
4780 }
4781 return Res;
4782}
4783
4784OMPClause *Sema::ActOnOpenMPScheduleClause(
4785 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
4786 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
4787 SourceLocation EndLoc) {
4788 if (Kind == OMPC_SCHEDULE_unknown) {
4789 std::string Values;
4790 std::string Sep(", ");
4791 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) {
4792 Values += "'";
4793 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i);
4794 Values += "'";
4795 switch (i) {
4796 case OMPC_SCHEDULE_unknown - 2:
4797 Values += " or ";
4798 break;
4799 case OMPC_SCHEDULE_unknown - 1:
4800 break;
4801 default:
4802 Values += Sep;
4803 break;
4804 }
4805 }
4806 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
4807 << Values << getOpenMPClauseName(OMPC_schedule);
4808 return nullptr;
4809 }
4810 Expr *ValExpr = ChunkSize;
Alexey Bataev040d5402015-05-12 08:35:28 +00004811 Expr *HelperValExpr = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00004812 if (ChunkSize) {
4813 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
4814 !ChunkSize->isInstantiationDependent() &&
4815 !ChunkSize->containsUnexpandedParameterPack()) {
4816 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
4817 ExprResult Val =
4818 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
4819 if (Val.isInvalid())
4820 return nullptr;
4821
4822 ValExpr = Val.get();
4823
4824 // OpenMP [2.7.1, Restrictions]
4825 // chunk_size must be a loop invariant integer expression with a positive
4826 // value.
4827 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00004828 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
4829 if (Result.isSigned() && !Result.isStrictlyPositive()) {
4830 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
4831 << "schedule" << ChunkSize->getSourceRange();
4832 return nullptr;
4833 }
4834 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
4835 auto *ImpVar = buildVarDecl(*this, ChunkSize->getExprLoc(),
4836 ChunkSize->getType(), ".chunk.");
4837 auto *ImpVarRef = buildDeclRefExpr(*this, ImpVar, ChunkSize->getType(),
4838 ChunkSize->getExprLoc(),
4839 /*RefersToCapture=*/true);
4840 HelperValExpr = ImpVarRef;
Alexey Bataev56dafe82014-06-20 07:16:17 +00004841 }
4842 }
4843 }
4844
4845 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc,
Alexey Bataev040d5402015-05-12 08:35:28 +00004846 EndLoc, Kind, ValExpr, HelperValExpr);
Alexey Bataev56dafe82014-06-20 07:16:17 +00004847}
4848
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004849OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
4850 SourceLocation StartLoc,
4851 SourceLocation EndLoc) {
4852 OMPClause *Res = nullptr;
4853 switch (Kind) {
4854 case OMPC_ordered:
4855 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
4856 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00004857 case OMPC_nowait:
4858 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
4859 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00004860 case OMPC_untied:
4861 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
4862 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00004863 case OMPC_mergeable:
4864 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
4865 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004866 case OMPC_read:
4867 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
4868 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00004869 case OMPC_write:
4870 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
4871 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00004872 case OMPC_update:
4873 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
4874 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00004875 case OMPC_capture:
4876 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
4877 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00004878 case OMPC_seq_cst:
4879 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
4880 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004881 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00004882 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004883 case OMPC_num_threads:
4884 case OMPC_safelen:
4885 case OMPC_collapse:
4886 case OMPC_schedule:
4887 case OMPC_private:
4888 case OMPC_firstprivate:
4889 case OMPC_lastprivate:
4890 case OMPC_shared:
4891 case OMPC_reduction:
4892 case OMPC_linear:
4893 case OMPC_aligned:
4894 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00004895 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004896 case OMPC_default:
4897 case OMPC_proc_bind:
4898 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00004899 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00004900 case OMPC_depend:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004901 case OMPC_unknown:
4902 llvm_unreachable("Clause is not allowed.");
4903 }
4904 return Res;
4905}
4906
4907OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
4908 SourceLocation EndLoc) {
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004909 DSAStack->setOrderedRegion();
Alexey Bataev142e1fc2014-06-20 09:44:06 +00004910 return new (Context) OMPOrderedClause(StartLoc, EndLoc);
4911}
4912
Alexey Bataev236070f2014-06-20 11:19:47 +00004913OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
4914 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00004915 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00004916 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
4917}
4918
Alexey Bataev7aea99a2014-07-17 12:19:31 +00004919OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
4920 SourceLocation EndLoc) {
4921 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
4922}
4923
Alexey Bataev74ba3a52014-07-17 12:47:03 +00004924OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
4925 SourceLocation EndLoc) {
4926 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
4927}
4928
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004929OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
4930 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004931 return new (Context) OMPReadClause(StartLoc, EndLoc);
4932}
4933
Alexey Bataevdea47612014-07-23 07:46:59 +00004934OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
4935 SourceLocation EndLoc) {
4936 return new (Context) OMPWriteClause(StartLoc, EndLoc);
4937}
4938
Alexey Bataev67a4f222014-07-23 10:25:33 +00004939OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
4940 SourceLocation EndLoc) {
4941 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
4942}
4943
Alexey Bataev459dec02014-07-24 06:46:57 +00004944OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
4945 SourceLocation EndLoc) {
4946 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
4947}
4948
Alexey Bataev82bad8b2014-07-24 08:55:34 +00004949OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
4950 SourceLocation EndLoc) {
4951 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
4952}
4953
Alexey Bataevc5e02582014-06-16 07:08:35 +00004954OMPClause *Sema::ActOnOpenMPVarListClause(
4955 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
4956 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
4957 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00004958 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
4959 SourceLocation DepLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00004960 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00004961 switch (Kind) {
4962 case OMPC_private:
4963 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
4964 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00004965 case OMPC_firstprivate:
4966 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
4967 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00004968 case OMPC_lastprivate:
4969 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
4970 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00004971 case OMPC_shared:
4972 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
4973 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00004974 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00004975 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
4976 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00004977 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00004978 case OMPC_linear:
4979 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
4980 ColonLoc, EndLoc);
4981 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00004982 case OMPC_aligned:
4983 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
4984 ColonLoc, EndLoc);
4985 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00004986 case OMPC_copyin:
4987 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
4988 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00004989 case OMPC_copyprivate:
4990 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
4991 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00004992 case OMPC_flush:
4993 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
4994 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00004995 case OMPC_depend:
4996 Res = ActOnOpenMPDependClause(DepKind, DepLoc, ColonLoc, VarList, StartLoc,
4997 LParenLoc, EndLoc);
4998 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00004999 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00005000 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00005001 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00005002 case OMPC_safelen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00005003 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005004 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005005 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005006 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005007 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00005008 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005009 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005010 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005011 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005012 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00005013 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00005014 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00005015 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005016 case OMPC_seq_cst:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005017 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005018 llvm_unreachable("Clause is not allowed.");
5019 }
5020 return Res;
5021}
5022
5023OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
5024 SourceLocation StartLoc,
5025 SourceLocation LParenLoc,
5026 SourceLocation EndLoc) {
5027 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00005028 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00005029 for (auto &RefExpr : VarList) {
5030 assert(RefExpr && "NULL expr in OpenMP private clause.");
5031 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005032 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00005033 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00005034 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005035 continue;
5036 }
5037
Alexey Bataeved09d242014-05-28 05:53:51 +00005038 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005039 // OpenMP [2.1, C/C++]
5040 // A list item is a variable name.
5041 // OpenMP [2.9.3.3, Restrictions, p.1]
5042 // A variable that is part of another variable (as an array or
5043 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00005044 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005045 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005046 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005047 continue;
5048 }
5049 Decl *D = DE->getDecl();
5050 VarDecl *VD = cast<VarDecl>(D);
5051
5052 QualType Type = VD->getType();
5053 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5054 // It will be analyzed later.
5055 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00005056 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005057 continue;
5058 }
5059
5060 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
5061 // A variable that appears in a private clause must not have an incomplete
5062 // type or a reference type.
5063 if (RequireCompleteType(ELoc, Type,
5064 diag::err_omp_private_incomplete_type)) {
5065 continue;
5066 }
5067 if (Type->isReferenceType()) {
5068 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00005069 << getOpenMPClauseName(OMPC_private) << Type;
5070 bool IsDecl =
5071 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5072 Diag(VD->getLocation(),
5073 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5074 << VD;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005075 continue;
5076 }
5077
Alexey Bataev758e55e2013-09-06 18:03:48 +00005078 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
5079 // in a Construct]
5080 // Variables with the predetermined data-sharing attributes may not be
5081 // listed in data-sharing attributes clauses, except for the cases
5082 // listed below. For these exceptions only, listing a predetermined
5083 // variable in a data-sharing attribute clause is allowed and overrides
5084 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005085 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005086 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005087 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
5088 << getOpenMPClauseName(OMPC_private);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005089 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005090 continue;
5091 }
5092
Alexey Bataevccb59ec2015-05-19 08:44:56 +00005093 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00005094 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00005095 DSAStack->getCurrentDirective() == OMPD_task) {
5096 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
5097 << getOpenMPClauseName(OMPC_private) << Type
5098 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
5099 bool IsDecl =
5100 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5101 Diag(VD->getLocation(),
5102 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5103 << VD;
5104 continue;
5105 }
5106
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005107 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
5108 // A variable of class type (or array thereof) that appears in a private
5109 // clause requires an accessible, unambiguous default constructor for the
5110 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00005111 // Generate helper private variable and initialize it with the default
5112 // value. The address of the original variable is replaced by the address of
5113 // the new private variable in CodeGen. This new variable is not added to
5114 // IdResolver, so the code in the OpenMP region uses original variable for
5115 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005116 Type = Type.getUnqualifiedType();
5117 auto VDPrivate = buildVarDecl(*this, DE->getExprLoc(), Type, VD->getName());
Alexey Bataev39f915b82015-05-08 10:41:21 +00005118 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00005119 if (VDPrivate->isInvalidDecl())
5120 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005121 auto VDPrivateRefExpr = buildDeclRefExpr(
5122 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev03b340a2014-10-21 03:16:40 +00005123
Alexey Bataev758e55e2013-09-06 18:03:48 +00005124 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005125 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00005126 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005127 }
5128
Alexey Bataeved09d242014-05-28 05:53:51 +00005129 if (Vars.empty())
5130 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005131
Alexey Bataev03b340a2014-10-21 03:16:40 +00005132 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
5133 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005134}
5135
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005136namespace {
5137class DiagsUninitializedSeveretyRAII {
5138private:
5139 DiagnosticsEngine &Diags;
5140 SourceLocation SavedLoc;
5141 bool IsIgnored;
5142
5143public:
5144 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
5145 bool IsIgnored)
5146 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
5147 if (!IsIgnored) {
5148 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
5149 /*Map*/ diag::Severity::Ignored, Loc);
5150 }
5151 }
5152 ~DiagsUninitializedSeveretyRAII() {
5153 if (!IsIgnored)
5154 Diags.popMappings(SavedLoc);
5155 }
5156};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00005157}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005158
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005159OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
5160 SourceLocation StartLoc,
5161 SourceLocation LParenLoc,
5162 SourceLocation EndLoc) {
5163 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005164 SmallVector<Expr *, 8> PrivateCopies;
5165 SmallVector<Expr *, 8> Inits;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005166 bool IsImplicitClause =
5167 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
5168 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
5169
Alexey Bataeved09d242014-05-28 05:53:51 +00005170 for (auto &RefExpr : VarList) {
5171 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
5172 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005173 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00005174 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005175 PrivateCopies.push_back(nullptr);
5176 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005177 continue;
5178 }
5179
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005180 SourceLocation ELoc =
5181 IsImplicitClause ? ImplicitClauseLoc : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005182 // OpenMP [2.1, C/C++]
5183 // A list item is a variable name.
5184 // OpenMP [2.9.3.3, Restrictions, p.1]
5185 // A variable that is part of another variable (as an array or
5186 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00005187 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005188 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005189 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005190 continue;
5191 }
5192 Decl *D = DE->getDecl();
5193 VarDecl *VD = cast<VarDecl>(D);
5194
5195 QualType Type = VD->getType();
5196 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5197 // It will be analyzed later.
5198 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005199 PrivateCopies.push_back(nullptr);
5200 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005201 continue;
5202 }
5203
5204 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
5205 // A variable that appears in a private clause must not have an incomplete
5206 // type or a reference type.
5207 if (RequireCompleteType(ELoc, Type,
5208 diag::err_omp_firstprivate_incomplete_type)) {
5209 continue;
5210 }
5211 if (Type->isReferenceType()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005212 if (IsImplicitClause) {
5213 Diag(ImplicitClauseLoc,
5214 diag::err_omp_task_predetermined_firstprivate_ref_type_arg)
5215 << Type;
5216 Diag(RefExpr->getExprLoc(), diag::note_used_here);
5217 } else {
5218 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
5219 << getOpenMPClauseName(OMPC_firstprivate) << Type;
5220 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005221 bool IsDecl =
5222 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5223 Diag(VD->getLocation(),
5224 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5225 << VD;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005226 continue;
5227 }
5228
5229 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
5230 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00005231 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005232 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005233 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005234
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005235 // If an implicit firstprivate variable found it was checked already.
5236 if (!IsImplicitClause) {
5237 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005238 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005239 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
5240 // A list item that specifies a given variable may not appear in more
5241 // than one clause on the same directive, except that a variable may be
5242 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005243 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00005244 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005245 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00005246 << getOpenMPClauseName(DVar.CKind)
5247 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005248 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005249 continue;
5250 }
5251
5252 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
5253 // in a Construct]
5254 // Variables with the predetermined data-sharing attributes may not be
5255 // listed in data-sharing attributes clauses, except for the cases
5256 // listed below. For these exceptions only, listing a predetermined
5257 // variable in a data-sharing attribute clause is allowed and overrides
5258 // the variable's predetermined data-sharing attributes.
5259 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
5260 // in a Construct, C/C++, p.2]
5261 // Variables with const-qualified type having no mutable member may be
5262 // listed in a firstprivate clause, even if they are static data members.
5263 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
5264 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
5265 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00005266 << getOpenMPClauseName(DVar.CKind)
5267 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005268 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005269 continue;
5270 }
5271
Alexey Bataevf29276e2014-06-18 04:14:57 +00005272 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005273 // OpenMP [2.9.3.4, Restrictions, p.2]
5274 // A list item that is private within a parallel region must not appear
5275 // in a firstprivate clause on a worksharing construct if any of the
5276 // worksharing regions arising from the worksharing construct ever bind
5277 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00005278 if (isOpenMPWorksharingDirective(CurrDir) &&
5279 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005280 DVar = DSAStack->getImplicitDSA(VD, true);
5281 if (DVar.CKind != OMPC_shared &&
5282 (isOpenMPParallelDirective(DVar.DKind) ||
5283 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00005284 Diag(ELoc, diag::err_omp_required_access)
5285 << getOpenMPClauseName(OMPC_firstprivate)
5286 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005287 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005288 continue;
5289 }
5290 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005291 // OpenMP [2.9.3.4, Restrictions, p.3]
5292 // A list item that appears in a reduction clause of a parallel construct
5293 // must not appear in a firstprivate clause on a worksharing or task
5294 // construct if any of the worksharing or task regions arising from the
5295 // worksharing or task construct ever bind to any of the parallel regions
5296 // arising from the parallel construct.
5297 // OpenMP [2.9.3.4, Restrictions, p.4]
5298 // A list item that appears in a reduction clause in worksharing
5299 // construct must not appear in a firstprivate clause in a task construct
5300 // encountered during execution of any of the worksharing regions arising
5301 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005302 if (CurrDir == OMPD_task) {
5303 DVar =
5304 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
5305 [](OpenMPDirectiveKind K) -> bool {
5306 return isOpenMPParallelDirective(K) ||
5307 isOpenMPWorksharingDirective(K);
5308 },
5309 false);
5310 if (DVar.CKind == OMPC_reduction &&
5311 (isOpenMPParallelDirective(DVar.DKind) ||
5312 isOpenMPWorksharingDirective(DVar.DKind))) {
5313 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
5314 << getOpenMPDirectiveName(DVar.DKind);
5315 ReportOriginalDSA(*this, DSAStack, VD, DVar);
5316 continue;
5317 }
5318 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005319 }
5320
Alexey Bataevccb59ec2015-05-19 08:44:56 +00005321 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00005322 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00005323 DSAStack->getCurrentDirective() == OMPD_task) {
5324 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
5325 << getOpenMPClauseName(OMPC_firstprivate) << Type
5326 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
5327 bool IsDecl =
5328 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5329 Diag(VD->getLocation(),
5330 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5331 << VD;
5332 continue;
5333 }
5334
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005335 Type = Type.getUnqualifiedType();
5336 auto VDPrivate = buildVarDecl(*this, ELoc, Type, VD->getName());
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005337 // Generate helper private variable and initialize it with the value of the
5338 // original variable. The address of the original variable is replaced by
5339 // the address of the new private variable in the CodeGen. This new variable
5340 // is not added to IdResolver, so the code in the OpenMP region uses
5341 // original variable for proper diagnostics and variable capturing.
5342 Expr *VDInitRefExpr = nullptr;
5343 // For arrays generate initializer for single element and replace it by the
5344 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005345 if (Type->isArrayType()) {
5346 auto VDInit =
5347 buildVarDecl(*this, DE->getExprLoc(), ElemType, VD->getName());
5348 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005349 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005350 ElemType = ElemType.getUnqualifiedType();
5351 auto *VDInitTemp = buildVarDecl(*this, DE->getLocStart(), ElemType,
5352 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00005353 InitializedEntity Entity =
5354 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005355 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
5356
5357 InitializationSequence InitSeq(*this, Entity, Kind, Init);
5358 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
5359 if (Result.isInvalid())
5360 VDPrivate->setInvalidDecl();
5361 else
5362 VDPrivate->setInit(Result.getAs<Expr>());
5363 } else {
Alexey Bataev69c62a92015-04-15 04:52:20 +00005364 auto *VDInit =
Alexey Bataev39f915b82015-05-08 10:41:21 +00005365 buildVarDecl(*this, DE->getLocStart(), Type, ".firstprivate.temp");
Alexey Bataevf120c0d2015-05-19 07:46:42 +00005366 VDInitRefExpr =
5367 buildDeclRefExpr(*this, VDInit, DE->getType(), DE->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00005368 AddInitializerToDecl(VDPrivate,
5369 DefaultLvalueConversion(VDInitRefExpr).get(),
5370 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005371 }
5372 if (VDPrivate->isInvalidDecl()) {
5373 if (IsImplicitClause) {
5374 Diag(DE->getExprLoc(),
5375 diag::note_omp_task_predetermined_firstprivate_here);
5376 }
5377 continue;
5378 }
5379 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00005380 auto VDPrivateRefExpr = buildDeclRefExpr(
5381 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005382 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
5383 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005384 PrivateCopies.push_back(VDPrivateRefExpr);
5385 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005386 }
5387
Alexey Bataeved09d242014-05-28 05:53:51 +00005388 if (Vars.empty())
5389 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005390
5391 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev4a5bb772014-10-08 14:01:46 +00005392 Vars, PrivateCopies, Inits);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005393}
5394
Alexander Musman1bb328c2014-06-04 13:06:39 +00005395OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
5396 SourceLocation StartLoc,
5397 SourceLocation LParenLoc,
5398 SourceLocation EndLoc) {
5399 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00005400 SmallVector<Expr *, 8> SrcExprs;
5401 SmallVector<Expr *, 8> DstExprs;
5402 SmallVector<Expr *, 8> AssignmentOps;
Alexander Musman1bb328c2014-06-04 13:06:39 +00005403 for (auto &RefExpr : VarList) {
5404 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
5405 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
5406 // It will be analyzed later.
5407 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00005408 SrcExprs.push_back(nullptr);
5409 DstExprs.push_back(nullptr);
5410 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005411 continue;
5412 }
5413
5414 SourceLocation ELoc = RefExpr->getExprLoc();
5415 // OpenMP [2.1, C/C++]
5416 // A list item is a variable name.
5417 // OpenMP [2.14.3.5, Restrictions, p.1]
5418 // A variable that is part of another variable (as an array or structure
5419 // element) cannot appear in a lastprivate clause.
5420 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
5421 if (!DE || !isa<VarDecl>(DE->getDecl())) {
5422 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
5423 continue;
5424 }
5425 Decl *D = DE->getDecl();
5426 VarDecl *VD = cast<VarDecl>(D);
5427
5428 QualType Type = VD->getType();
5429 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5430 // It will be analyzed later.
5431 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00005432 SrcExprs.push_back(nullptr);
5433 DstExprs.push_back(nullptr);
5434 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005435 continue;
5436 }
5437
5438 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
5439 // A variable that appears in a lastprivate clause must not have an
5440 // incomplete type or a reference type.
5441 if (RequireCompleteType(ELoc, Type,
5442 diag::err_omp_lastprivate_incomplete_type)) {
5443 continue;
5444 }
5445 if (Type->isReferenceType()) {
5446 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
5447 << getOpenMPClauseName(OMPC_lastprivate) << Type;
5448 bool IsDecl =
5449 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5450 Diag(VD->getLocation(),
5451 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5452 << VD;
5453 continue;
5454 }
5455
5456 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
5457 // in a Construct]
5458 // Variables with the predetermined data-sharing attributes may not be
5459 // listed in data-sharing attributes clauses, except for the cases
5460 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005461 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005462 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
5463 DVar.CKind != OMPC_firstprivate &&
5464 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
5465 Diag(ELoc, diag::err_omp_wrong_dsa)
5466 << getOpenMPClauseName(DVar.CKind)
5467 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005468 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005469 continue;
5470 }
5471
Alexey Bataevf29276e2014-06-18 04:14:57 +00005472 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
5473 // OpenMP [2.14.3.5, Restrictions, p.2]
5474 // A list item that is private within a parallel region, or that appears in
5475 // the reduction clause of a parallel construct, must not appear in a
5476 // lastprivate clause on a worksharing construct if any of the corresponding
5477 // worksharing regions ever binds to any of the corresponding parallel
5478 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005479 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00005480 if (isOpenMPWorksharingDirective(CurrDir) &&
5481 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005482 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005483 if (DVar.CKind != OMPC_shared) {
5484 Diag(ELoc, diag::err_omp_required_access)
5485 << getOpenMPClauseName(OMPC_lastprivate)
5486 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005487 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005488 continue;
5489 }
5490 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00005491 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00005492 // A variable of class type (or array thereof) that appears in a
5493 // lastprivate clause requires an accessible, unambiguous default
5494 // constructor for the class type, unless the list item is also specified
5495 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00005496 // A variable of class type (or array thereof) that appears in a
5497 // lastprivate clause requires an accessible, unambiguous copy assignment
5498 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00005499 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00005500 auto *SrcVD = buildVarDecl(*this, DE->getLocStart(),
Alexey Bataev38e89532015-04-16 04:54:05 +00005501 Type.getUnqualifiedType(), ".lastprivate.src");
Alexey Bataev39f915b82015-05-08 10:41:21 +00005502 auto *PseudoSrcExpr = buildDeclRefExpr(
5503 *this, SrcVD, Type.getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00005504 auto *DstVD =
Alexey Bataev39f915b82015-05-08 10:41:21 +00005505 buildVarDecl(*this, DE->getLocStart(), Type, ".lastprivate.dst");
Alexey Bataev38e89532015-04-16 04:54:05 +00005506 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00005507 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00005508 // For arrays generate assignment operation for single element and replace
5509 // it by the original array element in CodeGen.
5510 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
5511 PseudoDstExpr, PseudoSrcExpr);
5512 if (AssignmentOp.isInvalid())
5513 continue;
5514 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
5515 /*DiscardedValue=*/true);
5516 if (AssignmentOp.isInvalid())
5517 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00005518
Alexey Bataev39f915b82015-05-08 10:41:21 +00005519 if (TopDVar.CKind != OMPC_firstprivate)
Alexey Bataevf29276e2014-06-18 04:14:57 +00005520 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005521 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00005522 SrcExprs.push_back(PseudoSrcExpr);
5523 DstExprs.push_back(PseudoDstExpr);
5524 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00005525 }
5526
5527 if (Vars.empty())
5528 return nullptr;
5529
5530 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00005531 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexander Musman1bb328c2014-06-04 13:06:39 +00005532}
5533
Alexey Bataev758e55e2013-09-06 18:03:48 +00005534OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
5535 SourceLocation StartLoc,
5536 SourceLocation LParenLoc,
5537 SourceLocation EndLoc) {
5538 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00005539 for (auto &RefExpr : VarList) {
5540 assert(RefExpr && "NULL expr in OpenMP shared clause.");
5541 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00005542 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00005543 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005544 continue;
5545 }
5546
Alexey Bataeved09d242014-05-28 05:53:51 +00005547 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00005548 // OpenMP [2.1, C/C++]
5549 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00005550 // OpenMP [2.14.3.2, Restrictions, p.1]
5551 // A variable that is part of another variable (as an array or structure
5552 // element) cannot appear in a shared unless it is a static data member
5553 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00005554 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005555 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00005556 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00005557 continue;
5558 }
5559 Decl *D = DE->getDecl();
5560 VarDecl *VD = cast<VarDecl>(D);
5561
5562 QualType Type = VD->getType();
5563 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
5564 // It will be analyzed later.
5565 Vars.push_back(DE);
5566 continue;
5567 }
5568
5569 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
5570 // in a Construct]
5571 // Variables with the predetermined data-sharing attributes may not be
5572 // listed in data-sharing attributes clauses, except for the cases
5573 // listed below. For these exceptions only, listing a predetermined
5574 // variable in a data-sharing attribute clause is allowed and overrides
5575 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005576 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00005577 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
5578 DVar.RefExpr) {
5579 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
5580 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005581 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005582 continue;
5583 }
5584
5585 DSAStack->addDSA(VD, DE, OMPC_shared);
5586 Vars.push_back(DE);
5587 }
5588
Alexey Bataeved09d242014-05-28 05:53:51 +00005589 if (Vars.empty())
5590 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00005591
5592 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
5593}
5594
Alexey Bataevc5e02582014-06-16 07:08:35 +00005595namespace {
5596class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
5597 DSAStackTy *Stack;
5598
5599public:
5600 bool VisitDeclRefExpr(DeclRefExpr *E) {
5601 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005602 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00005603 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
5604 return false;
5605 if (DVar.CKind != OMPC_unknown)
5606 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00005607 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005608 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005609 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00005610 return true;
5611 return false;
5612 }
5613 return false;
5614 }
5615 bool VisitStmt(Stmt *S) {
5616 for (auto Child : S->children()) {
5617 if (Child && Visit(Child))
5618 return true;
5619 }
5620 return false;
5621 }
Alexey Bataev23b69422014-06-18 07:08:49 +00005622 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00005623};
Alexey Bataev23b69422014-06-18 07:08:49 +00005624} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00005625
5626OMPClause *Sema::ActOnOpenMPReductionClause(
5627 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
5628 SourceLocation ColonLoc, SourceLocation EndLoc,
5629 CXXScopeSpec &ReductionIdScopeSpec,
5630 const DeclarationNameInfo &ReductionId) {
5631 // TODO: Allow scope specification search when 'declare reduction' is
5632 // supported.
5633 assert(ReductionIdScopeSpec.isEmpty() &&
5634 "No support for scoped reduction identifiers yet.");
5635
5636 auto DN = ReductionId.getName();
5637 auto OOK = DN.getCXXOverloadedOperator();
5638 BinaryOperatorKind BOK = BO_Comma;
5639
5640 // OpenMP [2.14.3.6, reduction clause]
5641 // C
5642 // reduction-identifier is either an identifier or one of the following
5643 // operators: +, -, *, &, |, ^, && and ||
5644 // C++
5645 // reduction-identifier is either an id-expression or one of the following
5646 // operators: +, -, *, &, |, ^, && and ||
5647 // FIXME: Only 'min' and 'max' identifiers are supported for now.
5648 switch (OOK) {
5649 case OO_Plus:
5650 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005651 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005652 break;
5653 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005654 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005655 break;
5656 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005657 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005658 break;
5659 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005660 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005661 break;
5662 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005663 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005664 break;
5665 case OO_AmpAmp:
5666 BOK = BO_LAnd;
5667 break;
5668 case OO_PipePipe:
5669 BOK = BO_LOr;
5670 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005671 case OO_New:
5672 case OO_Delete:
5673 case OO_Array_New:
5674 case OO_Array_Delete:
5675 case OO_Slash:
5676 case OO_Percent:
5677 case OO_Tilde:
5678 case OO_Exclaim:
5679 case OO_Equal:
5680 case OO_Less:
5681 case OO_Greater:
5682 case OO_LessEqual:
5683 case OO_GreaterEqual:
5684 case OO_PlusEqual:
5685 case OO_MinusEqual:
5686 case OO_StarEqual:
5687 case OO_SlashEqual:
5688 case OO_PercentEqual:
5689 case OO_CaretEqual:
5690 case OO_AmpEqual:
5691 case OO_PipeEqual:
5692 case OO_LessLess:
5693 case OO_GreaterGreater:
5694 case OO_LessLessEqual:
5695 case OO_GreaterGreaterEqual:
5696 case OO_EqualEqual:
5697 case OO_ExclaimEqual:
5698 case OO_PlusPlus:
5699 case OO_MinusMinus:
5700 case OO_Comma:
5701 case OO_ArrowStar:
5702 case OO_Arrow:
5703 case OO_Call:
5704 case OO_Subscript:
5705 case OO_Conditional:
5706 case NUM_OVERLOADED_OPERATORS:
5707 llvm_unreachable("Unexpected reduction identifier");
5708 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00005709 if (auto II = DN.getAsIdentifierInfo()) {
5710 if (II->isStr("max"))
5711 BOK = BO_GT;
5712 else if (II->isStr("min"))
5713 BOK = BO_LT;
5714 }
5715 break;
5716 }
5717 SourceRange ReductionIdRange;
5718 if (ReductionIdScopeSpec.isValid()) {
5719 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
5720 }
5721 ReductionIdRange.setEnd(ReductionId.getEndLoc());
5722 if (BOK == BO_Comma) {
5723 // Not allowed reduction identifier is found.
5724 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
5725 << ReductionIdRange;
5726 return nullptr;
5727 }
5728
5729 SmallVector<Expr *, 8> Vars;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005730 SmallVector<Expr *, 8> LHSs;
5731 SmallVector<Expr *, 8> RHSs;
5732 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005733 for (auto RefExpr : VarList) {
5734 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
5735 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
5736 // It will be analyzed later.
5737 Vars.push_back(RefExpr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005738 LHSs.push_back(nullptr);
5739 RHSs.push_back(nullptr);
5740 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00005741 continue;
5742 }
5743
5744 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
5745 RefExpr->isInstantiationDependent() ||
5746 RefExpr->containsUnexpandedParameterPack()) {
5747 // It will be analyzed later.
5748 Vars.push_back(RefExpr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005749 LHSs.push_back(nullptr);
5750 RHSs.push_back(nullptr);
5751 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00005752 continue;
5753 }
5754
5755 auto ELoc = RefExpr->getExprLoc();
5756 auto ERange = RefExpr->getSourceRange();
5757 // OpenMP [2.1, C/C++]
5758 // A list item is a variable or array section, subject to the restrictions
5759 // specified in Section 2.4 on page 42 and in each of the sections
5760 // describing clauses and directives for which a list appears.
5761 // OpenMP [2.14.3.3, Restrictions, p.1]
5762 // A variable that is part of another variable (as an array or
5763 // structure element) cannot appear in a private clause.
5764 auto DE = dyn_cast<DeclRefExpr>(RefExpr);
5765 if (!DE || !isa<VarDecl>(DE->getDecl())) {
5766 Diag(ELoc, diag::err_omp_expected_var_name) << ERange;
5767 continue;
5768 }
5769 auto D = DE->getDecl();
5770 auto VD = cast<VarDecl>(D);
5771 auto Type = VD->getType();
5772 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
5773 // A variable that appears in a private clause must not have an incomplete
5774 // type or a reference type.
5775 if (RequireCompleteType(ELoc, Type,
5776 diag::err_omp_reduction_incomplete_type))
5777 continue;
5778 // OpenMP [2.14.3.6, reduction clause, Restrictions]
5779 // Arrays may not appear in a reduction clause.
5780 if (Type.getNonReferenceType()->isArrayType()) {
5781 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
5782 bool IsDecl =
5783 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5784 Diag(VD->getLocation(),
5785 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5786 << VD;
5787 continue;
5788 }
5789 // OpenMP [2.14.3.6, reduction clause, Restrictions]
5790 // A list item that appears in a reduction clause must not be
5791 // const-qualified.
5792 if (Type.getNonReferenceType().isConstant(Context)) {
5793 Diag(ELoc, diag::err_omp_const_variable)
5794 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
5795 bool IsDecl =
5796 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5797 Diag(VD->getLocation(),
5798 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5799 << VD;
5800 continue;
5801 }
5802 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
5803 // If a list-item is a reference type then it must bind to the same object
5804 // for all threads of the team.
5805 VarDecl *VDDef = VD->getDefinition();
5806 if (Type->isReferenceType() && VDDef) {
5807 DSARefChecker Check(DSAStack);
5808 if (Check.Visit(VDDef->getInit())) {
5809 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
5810 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
5811 continue;
5812 }
5813 }
5814 // OpenMP [2.14.3.6, reduction clause, Restrictions]
5815 // The type of a list item that appears in a reduction clause must be valid
5816 // for the reduction-identifier. For a max or min reduction in C, the type
5817 // of the list item must be an allowed arithmetic data type: char, int,
5818 // float, double, or _Bool, possibly modified with long, short, signed, or
5819 // unsigned. For a max or min reduction in C++, the type of the list item
5820 // must be an allowed arithmetic data type: char, wchar_t, int, float,
5821 // double, or bool, possibly modified with long, short, signed, or unsigned.
5822 if ((BOK == BO_GT || BOK == BO_LT) &&
5823 !(Type->isScalarType() ||
5824 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
5825 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
5826 << getLangOpts().CPlusPlus;
5827 bool IsDecl =
5828 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5829 Diag(VD->getLocation(),
5830 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5831 << VD;
5832 continue;
5833 }
5834 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
5835 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
5836 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
5837 bool IsDecl =
5838 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
5839 Diag(VD->getLocation(),
5840 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
5841 << VD;
5842 continue;
5843 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00005844 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
5845 // in a Construct]
5846 // Variables with the predetermined data-sharing attributes may not be
5847 // listed in data-sharing attributes clauses, except for the cases
5848 // listed below. For these exceptions only, listing a predetermined
5849 // variable in a data-sharing attribute clause is allowed and overrides
5850 // the variable's predetermined data-sharing attributes.
5851 // OpenMP [2.14.3.6, Restrictions, p.3]
5852 // Any number of reduction clauses can be specified on the directive,
5853 // but a list item can appear only once in the reduction clauses for that
5854 // directive.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005855 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00005856 if (DVar.CKind == OMPC_reduction) {
5857 Diag(ELoc, diag::err_omp_once_referenced)
5858 << getOpenMPClauseName(OMPC_reduction);
5859 if (DVar.RefExpr) {
5860 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
5861 }
5862 } else if (DVar.CKind != OMPC_unknown) {
5863 Diag(ELoc, diag::err_omp_wrong_dsa)
5864 << getOpenMPClauseName(DVar.CKind)
5865 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005866 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00005867 continue;
5868 }
5869
5870 // OpenMP [2.14.3.6, Restrictions, p.1]
5871 // A list item that appears in a reduction clause of a worksharing
5872 // construct must be shared in the parallel regions to which any of the
5873 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf29276e2014-06-18 04:14:57 +00005874 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataev549210e2014-06-24 04:39:47 +00005875 if (isOpenMPWorksharingDirective(CurrDir) &&
5876 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00005877 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005878 if (DVar.CKind != OMPC_shared) {
5879 Diag(ELoc, diag::err_omp_required_access)
5880 << getOpenMPClauseName(OMPC_reduction)
5881 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00005882 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00005883 continue;
5884 }
5885 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005886 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00005887 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs");
5888 auto *RHSVD = buildVarDecl(*this, ELoc, Type, VD->getName());
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005889 // Add initializer for private variable.
5890 Expr *Init = nullptr;
5891 switch (BOK) {
5892 case BO_Add:
5893 case BO_Xor:
5894 case BO_Or:
5895 case BO_LOr:
5896 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
5897 if (Type->isScalarType() || Type->isAnyComplexType()) {
5898 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00005899 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005900 break;
5901 case BO_Mul:
5902 case BO_LAnd:
5903 if (Type->isScalarType() || Type->isAnyComplexType()) {
5904 // '*' and '&&' reduction ops - initializer is '1'.
5905 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
5906 }
5907 break;
5908 case BO_And: {
5909 // '&' reduction op - initializer is '~0'.
5910 QualType OrigType = Type;
5911 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) {
5912 Type = ComplexTy->getElementType();
5913 }
5914 if (Type->isRealFloatingType()) {
5915 llvm::APFloat InitValue =
5916 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
5917 /*isIEEE=*/true);
5918 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
5919 Type, ELoc);
5920 } else if (Type->isScalarType()) {
5921 auto Size = Context.getTypeSize(Type);
5922 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
5923 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
5924 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
5925 }
5926 if (Init && OrigType->isAnyComplexType()) {
5927 // Init = 0xFFFF + 0xFFFFi;
5928 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
5929 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
5930 }
5931 Type = OrigType;
5932 break;
5933 }
5934 case BO_LT:
5935 case BO_GT: {
5936 // 'min' reduction op - initializer is 'Largest representable number in
5937 // the reduction list item type'.
5938 // 'max' reduction op - initializer is 'Least representable number in
5939 // the reduction list item type'.
5940 if (Type->isIntegerType() || Type->isPointerType()) {
5941 bool IsSigned = Type->hasSignedIntegerRepresentation();
5942 auto Size = Context.getTypeSize(Type);
5943 QualType IntTy =
5944 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
5945 llvm::APInt InitValue =
5946 (BOK != BO_LT)
5947 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
5948 : llvm::APInt::getMinValue(Size)
5949 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
5950 : llvm::APInt::getMaxValue(Size);
5951 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
5952 if (Type->isPointerType()) {
5953 // Cast to pointer type.
5954 auto CastExpr = BuildCStyleCastExpr(
5955 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
5956 SourceLocation(), Init);
5957 if (CastExpr.isInvalid())
5958 continue;
5959 Init = CastExpr.get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00005960 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00005961 } else if (Type->isRealFloatingType()) {
5962 llvm::APFloat InitValue = llvm::APFloat::getLargest(
5963 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
5964 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
5965 Type, ELoc);
5966 }
5967 break;
5968 }
5969 case BO_PtrMemD:
5970 case BO_PtrMemI:
5971 case BO_MulAssign:
5972 case BO_Div:
5973 case BO_Rem:
5974 case BO_Sub:
5975 case BO_Shl:
5976 case BO_Shr:
5977 case BO_LE:
5978 case BO_GE:
5979 case BO_EQ:
5980 case BO_NE:
5981 case BO_AndAssign:
5982 case BO_XorAssign:
5983 case BO_OrAssign:
5984 case BO_Assign:
5985 case BO_AddAssign:
5986 case BO_SubAssign:
5987 case BO_DivAssign:
5988 case BO_RemAssign:
5989 case BO_ShlAssign:
5990 case BO_ShrAssign:
5991 case BO_Comma:
5992 llvm_unreachable("Unexpected reduction operation");
5993 }
5994 if (Init) {
5995 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
5996 /*TypeMayContainAuto=*/false);
5997 } else {
5998 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
5999 }
6000 if (!RHSVD->hasInit()) {
6001 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
6002 << ReductionIdRange;
6003 bool IsDecl =
6004 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6005 Diag(VD->getLocation(),
6006 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6007 << VD;
6008 continue;
6009 }
Alexey Bataev39f915b82015-05-08 10:41:21 +00006010 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
6011 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006012 ExprResult ReductionOp =
6013 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
6014 LHSDRE, RHSDRE);
6015 if (ReductionOp.isUsable()) {
Alexey Bataev69a47792015-05-07 03:54:03 +00006016 if (BOK != BO_LT && BOK != BO_GT) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006017 ReductionOp =
6018 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
6019 BO_Assign, LHSDRE, ReductionOp.get());
6020 } else {
6021 auto *ConditionalOp = new (Context) ConditionalOperator(
6022 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
6023 RHSDRE, Type, VK_LValue, OK_Ordinary);
6024 ReductionOp =
6025 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
6026 BO_Assign, LHSDRE, ConditionalOp);
6027 }
6028 if (ReductionOp.isUsable()) {
6029 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00006030 }
6031 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006032 if (ReductionOp.isInvalid())
6033 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006034
6035 DSAStack->addDSA(VD, DE, OMPC_reduction);
6036 Vars.push_back(DE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006037 LHSs.push_back(LHSDRE);
6038 RHSs.push_back(RHSDRE);
6039 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00006040 }
6041
6042 if (Vars.empty())
6043 return nullptr;
6044
6045 return OMPReductionClause::Create(
6046 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006047 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, LHSs,
6048 RHSs, ReductionOps);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006049}
6050
Alexander Musman8dba6642014-04-22 13:09:42 +00006051OMPClause *Sema::ActOnOpenMPLinearClause(ArrayRef<Expr *> VarList, Expr *Step,
6052 SourceLocation StartLoc,
6053 SourceLocation LParenLoc,
6054 SourceLocation ColonLoc,
6055 SourceLocation EndLoc) {
6056 SmallVector<Expr *, 8> Vars;
Alexander Musman3276a272015-03-21 10:12:56 +00006057 SmallVector<Expr *, 8> Inits;
Alexey Bataeved09d242014-05-28 05:53:51 +00006058 for (auto &RefExpr : VarList) {
6059 assert(RefExpr && "NULL expr in OpenMP linear clause.");
6060 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00006061 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006062 Vars.push_back(RefExpr);
Alexander Musman3276a272015-03-21 10:12:56 +00006063 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00006064 continue;
6065 }
6066
6067 // OpenMP [2.14.3.7, linear clause]
6068 // A list item that appears in a linear clause is subject to the private
6069 // clause semantics described in Section 2.14.3.3 on page 159 except as
6070 // noted. In addition, the value of the new list item on each iteration
6071 // of the associated loop(s) corresponds to the value of the original
6072 // list item before entering the construct plus the logical number of
6073 // the iteration times linear-step.
6074
Alexey Bataeved09d242014-05-28 05:53:51 +00006075 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00006076 // OpenMP [2.1, C/C++]
6077 // A list item is a variable name.
6078 // OpenMP [2.14.3.3, Restrictions, p.1]
6079 // A variable that is part of another variable (as an array or
6080 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00006081 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00006082 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006083 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00006084 continue;
6085 }
6086
6087 VarDecl *VD = cast<VarDecl>(DE->getDecl());
6088
6089 // OpenMP [2.14.3.7, linear clause]
6090 // A list-item cannot appear in more than one linear clause.
6091 // A list-item that appears in a linear clause cannot appear in any
6092 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006093 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00006094 if (DVar.RefExpr) {
6095 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
6096 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006097 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00006098 continue;
6099 }
6100
6101 QualType QType = VD->getType();
6102 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
6103 // It will be analyzed later.
6104 Vars.push_back(DE);
Alexander Musman3276a272015-03-21 10:12:56 +00006105 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00006106 continue;
6107 }
6108
6109 // A variable must not have an incomplete type or a reference type.
6110 if (RequireCompleteType(ELoc, QType,
6111 diag::err_omp_linear_incomplete_type)) {
6112 continue;
6113 }
6114 if (QType->isReferenceType()) {
6115 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
6116 << getOpenMPClauseName(OMPC_linear) << QType;
6117 bool IsDecl =
6118 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6119 Diag(VD->getLocation(),
6120 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6121 << VD;
6122 continue;
6123 }
6124
6125 // A list item must not be const-qualified.
6126 if (QType.isConstant(Context)) {
6127 Diag(ELoc, diag::err_omp_const_variable)
6128 << getOpenMPClauseName(OMPC_linear);
6129 bool IsDecl =
6130 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6131 Diag(VD->getLocation(),
6132 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6133 << VD;
6134 continue;
6135 }
6136
6137 // A list item must be of integral or pointer type.
6138 QType = QType.getUnqualifiedType().getCanonicalType();
6139 const Type *Ty = QType.getTypePtrOrNull();
6140 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
6141 !Ty->isPointerType())) {
6142 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
6143 bool IsDecl =
6144 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6145 Diag(VD->getLocation(),
6146 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6147 << VD;
6148 continue;
6149 }
6150
Alexander Musman3276a272015-03-21 10:12:56 +00006151 // Build var to save initial value.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006152 VarDecl *Init = buildVarDecl(*this, ELoc, QType, ".linear.start");
Alexander Musman3276a272015-03-21 10:12:56 +00006153 AddInitializerToDecl(Init, DefaultLvalueConversion(DE).get(),
6154 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006155 auto InitRef = buildDeclRefExpr(
6156 *this, Init, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman8dba6642014-04-22 13:09:42 +00006157 DSAStack->addDSA(VD, DE, OMPC_linear);
6158 Vars.push_back(DE);
Alexander Musman3276a272015-03-21 10:12:56 +00006159 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00006160 }
6161
6162 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006163 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00006164
6165 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00006166 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00006167 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
6168 !Step->isInstantiationDependent() &&
6169 !Step->containsUnexpandedParameterPack()) {
6170 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00006171 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00006172 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006173 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00006174 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00006175
Alexander Musman3276a272015-03-21 10:12:56 +00006176 // Build var to save the step value.
6177 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006178 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00006179 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006180 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00006181 ExprResult CalcStep =
6182 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
6183
Alexander Musman8dba6642014-04-22 13:09:42 +00006184 // Warn about zero linear step (it would be probably better specified as
6185 // making corresponding variables 'const').
6186 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00006187 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
6188 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00006189 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
6190 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00006191 if (!IsConstant && CalcStep.isUsable()) {
6192 // Calculate the step beforehand instead of doing this on each iteration.
6193 // (This is not used if the number of iterations may be kfold-ed).
6194 CalcStepExpr = CalcStep.get();
6195 }
Alexander Musman8dba6642014-04-22 13:09:42 +00006196 }
6197
6198 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, ColonLoc, EndLoc,
Alexander Musman3276a272015-03-21 10:12:56 +00006199 Vars, Inits, StepExpr, CalcStepExpr);
6200}
6201
6202static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
6203 Expr *NumIterations, Sema &SemaRef,
6204 Scope *S) {
6205 // Walk the vars and build update/final expressions for the CodeGen.
6206 SmallVector<Expr *, 8> Updates;
6207 SmallVector<Expr *, 8> Finals;
6208 Expr *Step = Clause.getStep();
6209 Expr *CalcStep = Clause.getCalcStep();
6210 // OpenMP [2.14.3.7, linear clause]
6211 // If linear-step is not specified it is assumed to be 1.
6212 if (Step == nullptr)
6213 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
6214 else if (CalcStep)
6215 Step = cast<BinaryOperator>(CalcStep)->getLHS();
6216 bool HasErrors = false;
6217 auto CurInit = Clause.inits().begin();
6218 for (auto &RefExpr : Clause.varlists()) {
6219 Expr *InitExpr = *CurInit;
6220
6221 // Build privatized reference to the current linear var.
6222 auto DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00006223 auto PrivateRef =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006224 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
6225 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
6226 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00006227
6228 // Build update: Var = InitExpr + IV * Step
6229 ExprResult Update =
6230 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), PrivateRef,
6231 InitExpr, IV, Step, /* Subtract */ false);
6232 Update = SemaRef.ActOnFinishFullExpr(Update.get());
6233
6234 // Build final: Var = InitExpr + NumIterations * Step
6235 ExprResult Final =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006236 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), PrivateRef,
6237 InitExpr, NumIterations, Step, /* Subtract */ false);
Alexander Musman3276a272015-03-21 10:12:56 +00006238 Final = SemaRef.ActOnFinishFullExpr(Final.get());
6239 if (!Update.isUsable() || !Final.isUsable()) {
6240 Updates.push_back(nullptr);
6241 Finals.push_back(nullptr);
6242 HasErrors = true;
6243 } else {
6244 Updates.push_back(Update.get());
6245 Finals.push_back(Final.get());
6246 }
6247 ++CurInit;
6248 }
6249 Clause.setUpdates(Updates);
6250 Clause.setFinals(Finals);
6251 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00006252}
6253
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006254OMPClause *Sema::ActOnOpenMPAlignedClause(
6255 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
6256 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
6257
6258 SmallVector<Expr *, 8> Vars;
6259 for (auto &RefExpr : VarList) {
6260 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
6261 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6262 // It will be analyzed later.
6263 Vars.push_back(RefExpr);
6264 continue;
6265 }
6266
6267 SourceLocation ELoc = RefExpr->getExprLoc();
6268 // OpenMP [2.1, C/C++]
6269 // A list item is a variable name.
6270 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
6271 if (!DE || !isa<VarDecl>(DE->getDecl())) {
6272 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
6273 continue;
6274 }
6275
6276 VarDecl *VD = cast<VarDecl>(DE->getDecl());
6277
6278 // OpenMP [2.8.1, simd construct, Restrictions]
6279 // The type of list items appearing in the aligned clause must be
6280 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006281 QualType QType = VD->getType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006282 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006283 const Type *Ty = QType.getTypePtrOrNull();
6284 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
6285 !Ty->isPointerType())) {
6286 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
6287 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
6288 bool IsDecl =
6289 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6290 Diag(VD->getLocation(),
6291 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6292 << VD;
6293 continue;
6294 }
6295
6296 // OpenMP [2.8.1, simd construct, Restrictions]
6297 // A list-item cannot appear in more than one aligned clause.
6298 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
6299 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
6300 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
6301 << getOpenMPClauseName(OMPC_aligned);
6302 continue;
6303 }
6304
6305 Vars.push_back(DE);
6306 }
6307
6308 // OpenMP [2.8.1, simd construct, Description]
6309 // The parameter of the aligned clause, alignment, must be a constant
6310 // positive integer expression.
6311 // If no optional parameter is specified, implementation-defined default
6312 // alignments for SIMD instructions on the target platforms are assumed.
6313 if (Alignment != nullptr) {
6314 ExprResult AlignResult =
6315 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
6316 if (AlignResult.isInvalid())
6317 return nullptr;
6318 Alignment = AlignResult.get();
6319 }
6320 if (Vars.empty())
6321 return nullptr;
6322
6323 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
6324 EndLoc, Vars, Alignment);
6325}
6326
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006327OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
6328 SourceLocation StartLoc,
6329 SourceLocation LParenLoc,
6330 SourceLocation EndLoc) {
6331 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006332 SmallVector<Expr *, 8> SrcExprs;
6333 SmallVector<Expr *, 8> DstExprs;
6334 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00006335 for (auto &RefExpr : VarList) {
6336 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
6337 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006338 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006339 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006340 SrcExprs.push_back(nullptr);
6341 DstExprs.push_back(nullptr);
6342 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006343 continue;
6344 }
6345
Alexey Bataeved09d242014-05-28 05:53:51 +00006346 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006347 // OpenMP [2.1, C/C++]
6348 // A list item is a variable name.
6349 // OpenMP [2.14.4.1, Restrictions, p.1]
6350 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00006351 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006352 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006353 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006354 continue;
6355 }
6356
6357 Decl *D = DE->getDecl();
6358 VarDecl *VD = cast<VarDecl>(D);
6359
6360 QualType Type = VD->getType();
6361 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6362 // It will be analyzed later.
6363 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006364 SrcExprs.push_back(nullptr);
6365 DstExprs.push_back(nullptr);
6366 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006367 continue;
6368 }
6369
6370 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
6371 // A list item that appears in a copyin clause must be threadprivate.
6372 if (!DSAStack->isThreadPrivate(VD)) {
6373 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00006374 << getOpenMPClauseName(OMPC_copyin)
6375 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006376 continue;
6377 }
6378
6379 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
6380 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00006381 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006382 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006383 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00006384 auto *SrcVD = buildVarDecl(*this, DE->getLocStart(),
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006385 ElemType.getUnqualifiedType(), ".copyin.src");
Alexey Bataev39f915b82015-05-08 10:41:21 +00006386 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006387 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
6388 auto *DstVD =
6389 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst");
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006390 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006391 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006392 // For arrays generate assignment operation for single element and replace
6393 // it by the original array element in CodeGen.
6394 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
6395 PseudoDstExpr, PseudoSrcExpr);
6396 if (AssignmentOp.isInvalid())
6397 continue;
6398 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
6399 /*DiscardedValue=*/true);
6400 if (AssignmentOp.isInvalid())
6401 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006402
6403 DSAStack->addDSA(VD, DE, OMPC_copyin);
6404 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006405 SrcExprs.push_back(PseudoSrcExpr);
6406 DstExprs.push_back(PseudoDstExpr);
6407 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006408 }
6409
Alexey Bataeved09d242014-05-28 05:53:51 +00006410 if (Vars.empty())
6411 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006412
Alexey Bataevf56f98c2015-04-16 05:39:01 +00006413 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
6414 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006415}
6416
Alexey Bataevbae9a792014-06-27 10:37:06 +00006417OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
6418 SourceLocation StartLoc,
6419 SourceLocation LParenLoc,
6420 SourceLocation EndLoc) {
6421 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00006422 SmallVector<Expr *, 8> SrcExprs;
6423 SmallVector<Expr *, 8> DstExprs;
6424 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00006425 for (auto &RefExpr : VarList) {
6426 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
6427 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6428 // It will be analyzed later.
6429 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00006430 SrcExprs.push_back(nullptr);
6431 DstExprs.push_back(nullptr);
6432 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00006433 continue;
6434 }
6435
6436 SourceLocation ELoc = RefExpr->getExprLoc();
6437 // OpenMP [2.1, C/C++]
6438 // A list item is a variable name.
6439 // OpenMP [2.14.4.1, Restrictions, p.1]
6440 // A list item that appears in a copyin clause must be threadprivate.
6441 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
6442 if (!DE || !isa<VarDecl>(DE->getDecl())) {
6443 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
6444 continue;
6445 }
6446
6447 Decl *D = DE->getDecl();
6448 VarDecl *VD = cast<VarDecl>(D);
6449
6450 QualType Type = VD->getType();
6451 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6452 // It will be analyzed later.
6453 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00006454 SrcExprs.push_back(nullptr);
6455 DstExprs.push_back(nullptr);
6456 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00006457 continue;
6458 }
6459
6460 // OpenMP [2.14.4.2, Restrictions, p.2]
6461 // A list item that appears in a copyprivate clause may not appear in a
6462 // private or firstprivate clause on the single construct.
6463 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006464 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00006465 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
6466 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00006467 Diag(ELoc, diag::err_omp_wrong_dsa)
6468 << getOpenMPClauseName(DVar.CKind)
6469 << getOpenMPClauseName(OMPC_copyprivate);
6470 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6471 continue;
6472 }
6473
6474 // OpenMP [2.11.4.2, Restrictions, p.1]
6475 // All list items that appear in a copyprivate clause must be either
6476 // threadprivate or private in the enclosing context.
6477 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006478 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00006479 if (DVar.CKind == OMPC_shared) {
6480 Diag(ELoc, diag::err_omp_required_access)
6481 << getOpenMPClauseName(OMPC_copyprivate)
6482 << "threadprivate or private in the enclosing context";
6483 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6484 continue;
6485 }
6486 }
6487 }
6488
Alexey Bataev7a3e5852015-05-19 08:19:24 +00006489 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00006490 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00006491 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006492 << getOpenMPClauseName(OMPC_copyprivate) << Type
6493 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00006494 bool IsDecl =
6495 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6496 Diag(VD->getLocation(),
6497 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6498 << VD;
6499 continue;
6500 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006501
Alexey Bataevbae9a792014-06-27 10:37:06 +00006502 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
6503 // A variable of class type (or array thereof) that appears in a
6504 // copyin clause requires an accessible, unambiguous copy assignment
6505 // operator for the class type.
Alexey Bataev420d45b2015-04-14 05:11:24 +00006506 Type = Context.getBaseElementType(Type).getUnqualifiedType();
6507 auto *SrcVD =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006508 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.src");
Alexey Bataev420d45b2015-04-14 05:11:24 +00006509 auto *PseudoSrcExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006510 buildDeclRefExpr(*this, SrcVD, Type, DE->getExprLoc());
Alexey Bataev420d45b2015-04-14 05:11:24 +00006511 auto *DstVD =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006512 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.dst");
Alexey Bataev420d45b2015-04-14 05:11:24 +00006513 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006514 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataeva63048e2015-03-23 06:18:07 +00006515 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
6516 PseudoDstExpr, PseudoSrcExpr);
6517 if (AssignmentOp.isInvalid())
6518 continue;
6519 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
6520 /*DiscardedValue=*/true);
6521 if (AssignmentOp.isInvalid())
6522 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00006523
6524 // No need to mark vars as copyprivate, they are already threadprivate or
6525 // implicitly private.
6526 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00006527 SrcExprs.push_back(PseudoSrcExpr);
6528 DstExprs.push_back(PseudoDstExpr);
6529 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +00006530 }
6531
6532 if (Vars.empty())
6533 return nullptr;
6534
Alexey Bataeva63048e2015-03-23 06:18:07 +00006535 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
6536 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00006537}
6538
Alexey Bataev6125da92014-07-21 11:26:11 +00006539OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
6540 SourceLocation StartLoc,
6541 SourceLocation LParenLoc,
6542 SourceLocation EndLoc) {
6543 if (VarList.empty())
6544 return nullptr;
6545
6546 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
6547}
Alexey Bataevdea47612014-07-23 07:46:59 +00006548
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006549OMPClause *
6550Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
6551 SourceLocation DepLoc, SourceLocation ColonLoc,
6552 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
6553 SourceLocation LParenLoc, SourceLocation EndLoc) {
6554 if (DepKind == OMPC_DEPEND_unknown) {
6555 std::string Values;
6556 std::string Sep(", ");
6557 for (unsigned i = 0; i < OMPC_DEPEND_unknown; ++i) {
6558 Values += "'";
6559 Values += getOpenMPSimpleClauseTypeName(OMPC_depend, i);
6560 Values += "'";
6561 switch (i) {
6562 case OMPC_DEPEND_unknown - 2:
6563 Values += " or ";
6564 break;
6565 case OMPC_DEPEND_unknown - 1:
6566 break;
6567 default:
6568 Values += Sep;
6569 break;
6570 }
6571 }
6572 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
6573 << Values << getOpenMPClauseName(OMPC_depend);
6574 return nullptr;
6575 }
6576 SmallVector<Expr *, 8> Vars;
6577 for (auto &RefExpr : VarList) {
6578 assert(RefExpr && "NULL expr in OpenMP shared clause.");
6579 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6580 // It will be analyzed later.
6581 Vars.push_back(RefExpr);
6582 continue;
6583 }
6584
6585 SourceLocation ELoc = RefExpr->getExprLoc();
6586 // OpenMP [2.11.1.1, Restrictions, p.3]
6587 // A variable that is part of another variable (such as a field of a
6588 // structure) but is not an array element or an array section cannot appear
6589 // in a depend clause.
6590 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
6591 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
6592 ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
6593 if (!RefExpr->IgnoreParenImpCasts()->isLValue() || (!ASE && !DE) ||
6594 (DE && !isa<VarDecl>(DE->getDecl())) ||
6595 (ASE && !ASE->getBase()->getType()->isAnyPointerType() &&
6596 !ASE->getBase()->getType()->isArrayType())) {
6597 Diag(ELoc, diag::err_omp_expected_var_name_or_array_item)
6598 << RefExpr->getSourceRange();
6599 continue;
6600 }
6601
6602 Vars.push_back(RefExpr->IgnoreParenImpCasts());
6603 }
6604
6605 if (Vars.empty())
6606 return nullptr;
6607
6608 return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
6609 DepLoc, ColonLoc, Vars);
6610}