blob: f9efe8e997c8af7328e144bfb3acfce1bc3a5ace [file] [log] [blame]
Alexey Bataev9959db52014-05-06 10:08:46 +00001//===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===//
Alexey Bataeva769e072013-03-22 06:34:35 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10/// \brief This file implements semantic analysis for OpenMP directives and
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000011/// clauses.
Alexey Bataeva769e072013-03-22 06:34:35 +000012///
13//===----------------------------------------------------------------------===//
14
Alexey Bataevb08f89f2015-08-14 12:25:37 +000015#include "TreeTransform.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000016#include "clang/AST/ASTContext.h"
Alexey Bataev97720002014-11-11 04:05:39 +000017#include "clang/AST/ASTMutationListener.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000018#include "clang/AST/Decl.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000019#include "clang/AST/DeclCXX.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000020#include "clang/AST/DeclOpenMP.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000021#include "clang/AST/StmtCXX.h"
22#include "clang/AST/StmtOpenMP.h"
23#include "clang/AST/StmtVisitor.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000024#include "clang/Basic/OpenMPKinds.h"
Samuel Antaof8b50122015-07-13 22:54:53 +000025#include "clang/Basic/TargetInfo.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000026#include "clang/Lex/Preprocessor.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000027#include "clang/Sema/Initialization.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000028#include "clang/Sema/Lookup.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000029#include "clang/Sema/Scope.h"
30#include "clang/Sema/ScopeInfo.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000031#include "clang/Sema/SemaInternal.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000032using namespace clang;
33
Alexey Bataev758e55e2013-09-06 18:03:48 +000034//===----------------------------------------------------------------------===//
35// Stack of data-sharing attributes for variables
36//===----------------------------------------------------------------------===//
37
38namespace {
39/// \brief Default data sharing attributes, which can be applied to directive.
40enum DefaultDataSharingAttributes {
Alexey Bataeved09d242014-05-28 05:53:51 +000041 DSA_unspecified = 0, /// \brief Data sharing attribute not specified.
42 DSA_none = 1 << 0, /// \brief Default data sharing attribute 'none'.
43 DSA_shared = 1 << 1 /// \brief Default data sharing attribute 'shared'.
Alexey Bataev758e55e2013-09-06 18:03:48 +000044};
Alexey Bataev7ff55242014-06-19 09:13:45 +000045
Alexey Bataevf29276e2014-06-18 04:14:57 +000046template <class T> struct MatchesAny {
Alexey Bataev23b69422014-06-18 07:08:49 +000047 explicit MatchesAny(ArrayRef<T> Arr) : Arr(std::move(Arr)) {}
Alexey Bataevf29276e2014-06-18 04:14:57 +000048 bool operator()(T Kind) {
49 for (auto KindEl : Arr)
50 if (KindEl == Kind)
51 return true;
52 return false;
53 }
54
55private:
56 ArrayRef<T> Arr;
57};
Alexey Bataev23b69422014-06-18 07:08:49 +000058struct MatchesAlways {
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +000059 MatchesAlways() {}
Alexey Bataev7ff55242014-06-19 09:13:45 +000060 template <class T> bool operator()(T) { return true; }
Alexey Bataevf29276e2014-06-18 04:14:57 +000061};
62
63typedef MatchesAny<OpenMPClauseKind> MatchesAnyClause;
64typedef MatchesAny<OpenMPDirectiveKind> MatchesAnyDirective;
Alexey Bataev758e55e2013-09-06 18:03:48 +000065
66/// \brief Stack for tracking declarations used in OpenMP directives and
67/// clauses and their data-sharing attributes.
68class DSAStackTy {
69public:
70 struct DSAVarData {
71 OpenMPDirectiveKind DKind;
72 OpenMPClauseKind CKind;
73 DeclRefExpr *RefExpr;
Alexey Bataevbae9a792014-06-27 10:37:06 +000074 SourceLocation ImplicitDSALoc;
75 DSAVarData()
76 : DKind(OMPD_unknown), CKind(OMPC_unknown), RefExpr(nullptr),
77 ImplicitDSALoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000078 };
Alexey Bataeved09d242014-05-28 05:53:51 +000079
Kelvin Li0bff7af2015-11-23 05:32:03 +000080public:
81 struct MapInfo {
82 Expr *RefExpr;
83 };
84
Alexey Bataev758e55e2013-09-06 18:03:48 +000085private:
86 struct DSAInfo {
87 OpenMPClauseKind Attributes;
88 DeclRefExpr *RefExpr;
89 };
90 typedef llvm::SmallDenseMap<VarDecl *, DSAInfo, 64> DeclSAMapTy;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000091 typedef llvm::SmallDenseMap<VarDecl *, DeclRefExpr *, 64> AlignedMapTy;
Alexey Bataev9c821032015-04-30 04:23:23 +000092 typedef llvm::DenseSet<VarDecl *> LoopControlVariablesSetTy;
Kelvin Li0bff7af2015-11-23 05:32:03 +000093 typedef llvm::SmallDenseMap<VarDecl *, MapInfo, 64> MappedDeclsTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +000094
95 struct SharingMapTy {
96 DeclSAMapTy SharingMap;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000097 AlignedMapTy AlignedMap;
Kelvin Li0bff7af2015-11-23 05:32:03 +000098 MappedDeclsTy MappedDecls;
Alexey Bataev9c821032015-04-30 04:23:23 +000099 LoopControlVariablesSetTy LCVSet;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000100 DefaultDataSharingAttributes DefaultAttr;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000101 SourceLocation DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000102 OpenMPDirectiveKind Directive;
103 DeclarationNameInfo DirectiveName;
104 Scope *CurScope;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000105 SourceLocation ConstructLoc;
Alexey Bataev346265e2015-09-25 10:37:12 +0000106 /// \brief first argument (Expr *) contains optional argument of the
107 /// 'ordered' clause, the second one is true if the regions has 'ordered'
108 /// clause, false otherwise.
109 llvm::PointerIntPair<Expr *, 1, bool> OrderedRegion;
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000110 bool NowaitRegion;
Alexey Bataev25e5b442015-09-15 12:52:43 +0000111 bool CancelRegion;
Alexey Bataev9c821032015-04-30 04:23:23 +0000112 unsigned CollapseNumber;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000113 SourceLocation InnerTeamsRegionLoc;
Alexey Bataeved09d242014-05-28 05:53:51 +0000114 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000115 Scope *CurScope, SourceLocation Loc)
Alexey Bataev9c821032015-04-30 04:23:23 +0000116 : SharingMap(), AlignedMap(), LCVSet(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000117 Directive(DKind), DirectiveName(std::move(Name)), CurScope(CurScope),
Alexey Bataev346265e2015-09-25 10:37:12 +0000118 ConstructLoc(Loc), OrderedRegion(), NowaitRegion(false),
Alexey Bataev25e5b442015-09-15 12:52:43 +0000119 CancelRegion(false), CollapseNumber(1), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000120 SharingMapTy()
Alexey Bataev9c821032015-04-30 04:23:23 +0000121 : SharingMap(), AlignedMap(), LCVSet(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000122 Directive(OMPD_unknown), DirectiveName(), CurScope(nullptr),
Alexey Bataev346265e2015-09-25 10:37:12 +0000123 ConstructLoc(), OrderedRegion(), NowaitRegion(false),
Alexey Bataev25e5b442015-09-15 12:52:43 +0000124 CancelRegion(false), CollapseNumber(1), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000125 };
126
127 typedef SmallVector<SharingMapTy, 64> StackTy;
128
129 /// \brief Stack of used declaration and their data-sharing attributes.
130 StackTy Stack;
Alexey Bataev39f915b82015-05-08 10:41:21 +0000131 /// \brief true, if check for DSA must be from parent directive, false, if
132 /// from current directive.
Alexey Bataevaac108a2015-06-23 04:51:00 +0000133 OpenMPClauseKind ClauseKindMode;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000134 Sema &SemaRef;
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000135 bool ForceCapturing;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000136
137 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
138
139 DSAVarData getDSA(StackTy::reverse_iterator Iter, VarDecl *D);
Alexey Bataevec3da872014-01-31 05:15:34 +0000140
141 /// \brief Checks if the variable is a local for OpenMP region.
142 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
Alexey Bataeved09d242014-05-28 05:53:51 +0000143
Alexey Bataev758e55e2013-09-06 18:03:48 +0000144public:
Alexey Bataevaac108a2015-06-23 04:51:00 +0000145 explicit DSAStackTy(Sema &S)
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000146 : Stack(1), ClauseKindMode(OMPC_unknown), SemaRef(S),
147 ForceCapturing(false) {}
Alexey Bataev39f915b82015-05-08 10:41:21 +0000148
Alexey Bataevaac108a2015-06-23 04:51:00 +0000149 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
150 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000151
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000152 bool isForceVarCapturing() const { return ForceCapturing; }
153 void setForceVarCapturing(bool V) { ForceCapturing = V; }
154
Alexey Bataev758e55e2013-09-06 18:03:48 +0000155 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000156 Scope *CurScope, SourceLocation Loc) {
157 Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc));
158 Stack.back().DefaultAttrLoc = Loc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000159 }
160
161 void pop() {
162 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
163 Stack.pop_back();
164 }
165
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000166 /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000167 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000168 /// for diagnostics.
169 DeclRefExpr *addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE);
170
Alexey Bataev9c821032015-04-30 04:23:23 +0000171 /// \brief Register specified variable as loop control variable.
172 void addLoopControlVariable(VarDecl *D);
173 /// \brief Check if the specified variable is a loop control variable for
174 /// current region.
175 bool isLoopControlVariable(VarDecl *D);
176
Alexey Bataev758e55e2013-09-06 18:03:48 +0000177 /// \brief Adds explicit data sharing attribute to the specified declaration.
178 void addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A);
179
Alexey Bataev758e55e2013-09-06 18:03:48 +0000180 /// \brief Returns data sharing attributes from top of the stack for the
181 /// specified declaration.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000182 DSAVarData getTopDSA(VarDecl *D, bool FromParent);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000183 /// \brief Returns data-sharing attributes for the specified declaration.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000184 DSAVarData getImplicitDSA(VarDecl *D, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000185 /// \brief Checks if the specified variables has data-sharing attributes which
186 /// match specified \a CPred predicate in any directive which matches \a DPred
187 /// predicate.
188 template <class ClausesPredicate, class DirectivesPredicate>
189 DSAVarData hasDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000190 DirectivesPredicate DPred, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000191 /// \brief Checks if the specified variables has data-sharing attributes which
192 /// match specified \a CPred predicate in any innermost directive which
193 /// matches \a DPred predicate.
194 template <class ClausesPredicate, class DirectivesPredicate>
195 DSAVarData hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000196 DirectivesPredicate DPred,
197 bool FromParent);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000198 /// \brief Checks if the specified variables has explicit data-sharing
199 /// attributes which match specified \a CPred predicate at the specified
200 /// OpenMP region.
201 bool hasExplicitDSA(VarDecl *D,
202 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
203 unsigned Level);
Samuel Antao4be30e92015-10-02 17:14:03 +0000204
205 /// \brief Returns true if the directive at level \Level matches in the
206 /// specified \a DPred predicate.
207 bool hasExplicitDirective(
208 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
209 unsigned Level);
210
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000211 /// \brief Finds a directive which matches specified \a DPred predicate.
212 template <class NamedDirectivesPredicate>
213 bool hasDirective(NamedDirectivesPredicate DPred, bool FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000214
Alexey Bataev758e55e2013-09-06 18:03:48 +0000215 /// \brief Returns currently analyzed directive.
216 OpenMPDirectiveKind getCurrentDirective() const {
217 return Stack.back().Directive;
218 }
Alexey Bataev549210e2014-06-24 04:39:47 +0000219 /// \brief Returns parent directive.
220 OpenMPDirectiveKind getParentDirective() const {
221 if (Stack.size() > 2)
222 return Stack[Stack.size() - 2].Directive;
223 return OMPD_unknown;
224 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000225 /// \brief Return the directive associated with the provided scope.
226 OpenMPDirectiveKind getDirectiveForScope(const Scope *S) const;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000227
228 /// \brief Set default data sharing attribute to none.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000229 void setDefaultDSANone(SourceLocation Loc) {
230 Stack.back().DefaultAttr = DSA_none;
231 Stack.back().DefaultAttrLoc = Loc;
232 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000233 /// \brief Set default data sharing attribute to shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000234 void setDefaultDSAShared(SourceLocation Loc) {
235 Stack.back().DefaultAttr = DSA_shared;
236 Stack.back().DefaultAttrLoc = Loc;
237 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000238
239 DefaultDataSharingAttributes getDefaultDSA() const {
240 return Stack.back().DefaultAttr;
241 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000242 SourceLocation getDefaultDSALocation() const {
243 return Stack.back().DefaultAttrLoc;
244 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000245
Alexey Bataevf29276e2014-06-18 04:14:57 +0000246 /// \brief Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000247 bool isThreadPrivate(VarDecl *D) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000248 DSAVarData DVar = getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000249 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000250 }
251
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000252 /// \brief Marks current region as ordered (it has an 'ordered' clause).
Alexey Bataev346265e2015-09-25 10:37:12 +0000253 void setOrderedRegion(bool IsOrdered, Expr *Param) {
254 Stack.back().OrderedRegion.setInt(IsOrdered);
255 Stack.back().OrderedRegion.setPointer(Param);
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000256 }
257 /// \brief Returns true, if parent region is ordered (has associated
258 /// 'ordered' clause), false - otherwise.
259 bool isParentOrderedRegion() const {
260 if (Stack.size() > 2)
Alexey Bataev346265e2015-09-25 10:37:12 +0000261 return Stack[Stack.size() - 2].OrderedRegion.getInt();
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000262 return false;
263 }
Alexey Bataev346265e2015-09-25 10:37:12 +0000264 /// \brief Returns optional parameter for the ordered region.
265 Expr *getParentOrderedRegionParam() const {
266 if (Stack.size() > 2)
267 return Stack[Stack.size() - 2].OrderedRegion.getPointer();
268 return nullptr;
269 }
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000270 /// \brief Marks current region as nowait (it has a 'nowait' clause).
271 void setNowaitRegion(bool IsNowait = true) {
272 Stack.back().NowaitRegion = IsNowait;
273 }
274 /// \brief Returns true, if parent region is nowait (has associated
275 /// 'nowait' clause), false - otherwise.
276 bool isParentNowaitRegion() const {
277 if (Stack.size() > 2)
278 return Stack[Stack.size() - 2].NowaitRegion;
279 return false;
280 }
Alexey Bataev25e5b442015-09-15 12:52:43 +0000281 /// \brief Marks parent region as cancel region.
282 void setParentCancelRegion(bool Cancel = true) {
283 if (Stack.size() > 2)
284 Stack[Stack.size() - 2].CancelRegion =
285 Stack[Stack.size() - 2].CancelRegion || Cancel;
286 }
287 /// \brief Return true if current region has inner cancel construct.
288 bool isCancelRegion() const {
289 return Stack.back().CancelRegion;
290 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000291
Alexey Bataev9c821032015-04-30 04:23:23 +0000292 /// \brief Set collapse value for the region.
293 void setCollapseNumber(unsigned Val) { Stack.back().CollapseNumber = Val; }
294 /// \brief Return collapse value for region.
295 unsigned getCollapseNumber() const {
296 return Stack.back().CollapseNumber;
297 }
298
Alexey Bataev13314bf2014-10-09 04:18:56 +0000299 /// \brief Marks current target region as one with closely nested teams
300 /// region.
301 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
302 if (Stack.size() > 2)
303 Stack[Stack.size() - 2].InnerTeamsRegionLoc = TeamsRegionLoc;
304 }
305 /// \brief Returns true, if current region has closely nested teams region.
306 bool hasInnerTeamsRegion() const {
307 return getInnerTeamsRegionLoc().isValid();
308 }
309 /// \brief Returns location of the nested teams region (if any).
310 SourceLocation getInnerTeamsRegionLoc() const {
311 if (Stack.size() > 1)
312 return Stack.back().InnerTeamsRegionLoc;
313 return SourceLocation();
314 }
315
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000316 Scope *getCurScope() const { return Stack.back().CurScope; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000317 Scope *getCurScope() { return Stack.back().CurScope; }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000318 SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000319
320 MapInfo getMapInfoForVar(VarDecl *VD) {
321 MapInfo VarMI = {0};
322 for (auto Cnt = Stack.size() - 1; Cnt > 0; --Cnt) {
323 if (Stack[Cnt].MappedDecls.count(VD)) {
324 VarMI = Stack[Cnt].MappedDecls[VD];
325 break;
326 }
327 }
328 return VarMI;
329 }
330
331 void addMapInfoForVar(VarDecl *VD, MapInfo MI) {
332 if (Stack.size() > 1) {
333 Stack.back().MappedDecls[VD] = MI;
334 }
335 }
336
337 MapInfo IsMappedInCurrentRegion(VarDecl *VD) {
338 assert(Stack.size() > 1 && "Target level is 0");
339 MapInfo VarMI = {0};
340 if (Stack.size() > 1 && Stack.back().MappedDecls.count(VD)) {
341 VarMI = Stack.back().MappedDecls[VD];
342 }
343 return VarMI;
344 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000345};
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000346bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
347 return isOpenMPParallelDirective(DKind) || DKind == OMPD_task ||
Alexey Bataev49f6e782015-12-01 04:18:41 +0000348 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000349 isOpenMPTaskLoopDirective(DKind);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000350}
Alexey Bataeved09d242014-05-28 05:53:51 +0000351} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000352
353DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter,
354 VarDecl *D) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000355 D = D->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000356 DSAVarData DVar;
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000357 if (Iter == std::prev(Stack.rend())) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000358 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
359 // in a region but not in construct]
360 // File-scope or namespace-scope variables referenced in called routines
361 // in the region are shared unless they appear in a threadprivate
362 // directive.
Alexey Bataev8b9cb982014-07-24 02:33:58 +0000363 if (!D->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D))
Alexey Bataev750a58b2014-03-18 12:19:12 +0000364 DVar.CKind = OMPC_shared;
365
366 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
367 // in a region but not in construct]
368 // Variables with static storage duration that are declared in called
369 // routines in the region are shared.
370 if (D->hasGlobalStorage())
371 DVar.CKind = OMPC_shared;
372
Alexey Bataev758e55e2013-09-06 18:03:48 +0000373 return DVar;
374 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000375
Alexey Bataev758e55e2013-09-06 18:03:48 +0000376 DVar.DKind = Iter->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +0000377 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
378 // in a Construct, C/C++, predetermined, p.1]
379 // Variables with automatic storage duration that are declared in a scope
380 // inside the construct are private.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000381 if (isOpenMPLocal(D, Iter) && D->isLocalVarDecl() &&
382 (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) {
383 DVar.CKind = OMPC_private;
384 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000385 }
386
Alexey Bataev758e55e2013-09-06 18:03:48 +0000387 // Explicitly specified attributes and local variables with predetermined
388 // attributes.
389 if (Iter->SharingMap.count(D)) {
390 DVar.RefExpr = Iter->SharingMap[D].RefExpr;
391 DVar.CKind = Iter->SharingMap[D].Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000392 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000393 return DVar;
394 }
395
396 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
397 // in a Construct, C/C++, implicitly determined, p.1]
398 // In a parallel or task construct, the data-sharing attributes of these
399 // variables are determined by the default clause, if present.
400 switch (Iter->DefaultAttr) {
401 case DSA_shared:
402 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000403 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000404 return DVar;
405 case DSA_none:
406 return DVar;
407 case DSA_unspecified:
408 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
409 // in a Construct, implicitly determined, p.2]
410 // In a parallel construct, if no default clause is present, these
411 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000412 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000413 if (isOpenMPParallelDirective(DVar.DKind) ||
414 isOpenMPTeamsDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000415 DVar.CKind = OMPC_shared;
416 return DVar;
417 }
418
419 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
420 // in a Construct, implicitly determined, p.4]
421 // In a task construct, if no default clause is present, a variable that in
422 // the enclosing context is determined to be shared by all implicit tasks
423 // bound to the current team is shared.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000424 if (DVar.DKind == OMPD_task) {
425 DSAVarData DVarTemp;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000426 for (StackTy::reverse_iterator I = std::next(Iter), EE = Stack.rend();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000427 I != EE; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000428 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
429 // Referenced
Alexey Bataev758e55e2013-09-06 18:03:48 +0000430 // in a Construct, implicitly determined, p.6]
431 // In a task construct, if no default clause is present, a variable
432 // whose data-sharing attribute is not determined by the rules above is
433 // firstprivate.
434 DVarTemp = getDSA(I, D);
435 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000436 DVar.RefExpr = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000437 DVar.DKind = OMPD_task;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000438 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000439 return DVar;
440 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000441 if (isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000442 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000443 }
444 DVar.DKind = OMPD_task;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000445 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000446 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000447 return DVar;
448 }
449 }
450 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
451 // in a Construct, implicitly determined, p.3]
452 // For constructs other than task, if no default clause is present, these
453 // variables inherit their data-sharing attributes from the enclosing
454 // context.
Benjamin Kramer167e9992014-03-02 12:20:24 +0000455 return getDSA(std::next(Iter), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000456}
457
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000458DeclRefExpr *DSAStackTy::addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE) {
459 assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000460 D = D->getCanonicalDecl();
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000461 auto It = Stack.back().AlignedMap.find(D);
462 if (It == Stack.back().AlignedMap.end()) {
463 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
464 Stack.back().AlignedMap[D] = NewDE;
465 return nullptr;
466 } else {
467 assert(It->second && "Unexpected nullptr expr in the aligned map");
468 return It->second;
469 }
470 return nullptr;
471}
472
Alexey Bataev9c821032015-04-30 04:23:23 +0000473void DSAStackTy::addLoopControlVariable(VarDecl *D) {
474 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
475 D = D->getCanonicalDecl();
476 Stack.back().LCVSet.insert(D);
477}
478
479bool DSAStackTy::isLoopControlVariable(VarDecl *D) {
480 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
481 D = D->getCanonicalDecl();
482 return Stack.back().LCVSet.count(D) > 0;
483}
484
Alexey Bataev758e55e2013-09-06 18:03:48 +0000485void DSAStackTy::addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000486 D = D->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000487 if (A == OMPC_threadprivate) {
488 Stack[0].SharingMap[D].Attributes = A;
489 Stack[0].SharingMap[D].RefExpr = E;
490 } else {
491 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
492 Stack.back().SharingMap[D].Attributes = A;
493 Stack.back().SharingMap[D].RefExpr = E;
494 }
495}
496
Alexey Bataeved09d242014-05-28 05:53:51 +0000497bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000498 D = D->getCanonicalDecl();
Alexey Bataevec3da872014-01-31 05:15:34 +0000499 if (Stack.size() > 2) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000500 reverse_iterator I = Iter, E = std::prev(Stack.rend());
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000501 Scope *TopScope = nullptr;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000502 while (I != E && !isParallelOrTaskRegion(I->Directive)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000503 ++I;
504 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000505 if (I == E)
506 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000507 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000508 Scope *CurScope = getCurScope();
509 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000510 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000511 }
512 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000513 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000514 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000515}
516
Alexey Bataev39f915b82015-05-08 10:41:21 +0000517/// \brief Build a variable declaration for OpenMP loop iteration variable.
518static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000519 StringRef Name, const AttrVec *Attrs = nullptr) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000520 DeclContext *DC = SemaRef.CurContext;
521 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
522 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
523 VarDecl *Decl =
524 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000525 if (Attrs) {
526 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
527 I != E; ++I)
528 Decl->addAttr(*I);
529 }
Alexey Bataev39f915b82015-05-08 10:41:21 +0000530 Decl->setImplicit();
531 return Decl;
532}
533
534static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
535 SourceLocation Loc,
536 bool RefersToCapture = false) {
537 D->setReferenced();
538 D->markUsed(S.Context);
539 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
540 SourceLocation(), D, RefersToCapture, Loc, Ty,
541 VK_LValue);
542}
543
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000544DSAStackTy::DSAVarData DSAStackTy::getTopDSA(VarDecl *D, bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000545 D = D->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000546 DSAVarData DVar;
547
548 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
549 // in a Construct, C/C++, predetermined, p.1]
550 // Variables appearing in threadprivate directives are threadprivate.
Samuel Antaof8b50122015-07-13 22:54:53 +0000551 if ((D->getTLSKind() != VarDecl::TLS_None &&
552 !(D->hasAttr<OMPThreadPrivateDeclAttr>() &&
553 SemaRef.getLangOpts().OpenMPUseTLS &&
554 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +0000555 (D->getStorageClass() == SC_Register && D->hasAttr<AsmLabelAttr>() &&
556 !D->isLocalVarDecl())) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000557 addDSA(D, buildDeclRefExpr(SemaRef, D, D->getType().getNonReferenceType(),
558 D->getLocation()),
Alexey Bataevf2453a02015-05-06 07:25:08 +0000559 OMPC_threadprivate);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000560 }
561 if (Stack[0].SharingMap.count(D)) {
562 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
563 DVar.CKind = OMPC_threadprivate;
564 return DVar;
565 }
566
567 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
568 // in a Construct, C/C++, predetermined, p.1]
569 // Variables with automatic storage duration that are declared in a scope
570 // inside the construct are private.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000571 OpenMPDirectiveKind Kind =
572 FromParent ? getParentDirective() : getCurrentDirective();
573 auto StartI = std::next(Stack.rbegin());
574 auto EndI = std::prev(Stack.rend());
575 if (FromParent && StartI != EndI) {
576 StartI = std::next(StartI);
577 }
578 if (!isParallelOrTaskRegion(Kind)) {
Alexey Bataev8b9cb982014-07-24 02:33:58 +0000579 if (isOpenMPLocal(D, StartI) &&
580 ((D->isLocalVarDecl() && (D->getStorageClass() == SC_Auto ||
581 D->getStorageClass() == SC_None)) ||
582 isa<ParmVarDecl>(D))) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000583 DVar.CKind = OMPC_private;
584 return DVar;
Alexander Musman8dba6642014-04-22 13:09:42 +0000585 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000586
Alexey Bataev24b04aa2015-01-16 07:11:33 +0000587 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
588 // in a Construct, C/C++, predetermined, p.4]
589 // Static data members are shared.
Alexey Bataev24b04aa2015-01-16 07:11:33 +0000590 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
591 // in a Construct, C/C++, predetermined, p.7]
592 // Variables with static storage duration that are declared in a scope
593 // inside the construct are shared.
Kelvin Li4eea8c62015-09-15 18:56:58 +0000594 if (D->isStaticDataMember()) {
Alexey Bataev42971a32015-01-20 07:03:46 +0000595 DSAVarData DVarTemp =
596 hasDSA(D, isOpenMPPrivate, MatchesAlways(), FromParent);
597 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
598 return DVar;
599
Alexey Bataev24b04aa2015-01-16 07:11:33 +0000600 DVar.CKind = OMPC_shared;
601 return DVar;
602 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000603 }
604
605 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +0000606 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
607 Type = SemaRef.getASTContext().getBaseElementType(Type);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000608 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
609 // in a Construct, C/C++, predetermined, p.6]
610 // Variables with const qualified type having no mutable member are
611 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000612 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000613 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000614 if (IsConstant &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000615 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000616 // Variables with const-qualified type having no mutable member may be
617 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000618 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
619 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000620 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
621 return DVar;
622
Alexey Bataev758e55e2013-09-06 18:03:48 +0000623 DVar.CKind = OMPC_shared;
624 return DVar;
625 }
626
Alexey Bataev758e55e2013-09-06 18:03:48 +0000627 // Explicitly specified attributes and local variables with predetermined
628 // attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000629 auto I = std::prev(StartI);
630 if (I->SharingMap.count(D)) {
631 DVar.RefExpr = I->SharingMap[D].RefExpr;
632 DVar.CKind = I->SharingMap[D].Attributes;
633 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000634 }
635
636 return DVar;
637}
638
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000639DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D, bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000640 D = D->getCanonicalDecl();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000641 auto StartI = Stack.rbegin();
642 auto EndI = std::prev(Stack.rend());
643 if (FromParent && StartI != EndI) {
644 StartI = std::next(StartI);
645 }
646 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000647}
648
Alexey Bataevf29276e2014-06-18 04:14:57 +0000649template <class ClausesPredicate, class DirectivesPredicate>
650DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000651 DirectivesPredicate DPred,
652 bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000653 D = D->getCanonicalDecl();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000654 auto StartI = std::next(Stack.rbegin());
655 auto EndI = std::prev(Stack.rend());
656 if (FromParent && StartI != EndI) {
657 StartI = std::next(StartI);
658 }
659 for (auto I = StartI, EE = EndI; I != EE; ++I) {
660 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000661 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000662 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000663 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000664 return DVar;
665 }
666 return DSAVarData();
667}
668
Alexey Bataevf29276e2014-06-18 04:14:57 +0000669template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000670DSAStackTy::DSAVarData
671DSAStackTy::hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
672 DirectivesPredicate DPred, bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000673 D = D->getCanonicalDecl();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000674 auto StartI = std::next(Stack.rbegin());
675 auto EndI = std::prev(Stack.rend());
676 if (FromParent && StartI != EndI) {
677 StartI = std::next(StartI);
678 }
679 for (auto I = StartI, EE = EndI; I != EE; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000680 if (!DPred(I->Directive))
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000681 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000682 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000683 if (CPred(DVar.CKind))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000684 return DVar;
685 return DSAVarData();
686 }
687 return DSAVarData();
688}
689
Alexey Bataevaac108a2015-06-23 04:51:00 +0000690bool DSAStackTy::hasExplicitDSA(
691 VarDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
692 unsigned Level) {
693 if (CPred(ClauseKindMode))
694 return true;
695 if (isClauseParsingMode())
696 ++Level;
697 D = D->getCanonicalDecl();
698 auto StartI = Stack.rbegin();
699 auto EndI = std::prev(Stack.rend());
NAKAMURA Takumi0332eda2015-06-23 10:01:20 +0000700 if (std::distance(StartI, EndI) <= (int)Level)
Alexey Bataevaac108a2015-06-23 04:51:00 +0000701 return false;
702 std::advance(StartI, Level);
703 return (StartI->SharingMap.count(D) > 0) && StartI->SharingMap[D].RefExpr &&
704 CPred(StartI->SharingMap[D].Attributes);
705}
706
Samuel Antao4be30e92015-10-02 17:14:03 +0000707bool DSAStackTy::hasExplicitDirective(
708 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
709 unsigned Level) {
710 if (isClauseParsingMode())
711 ++Level;
712 auto StartI = Stack.rbegin();
713 auto EndI = std::prev(Stack.rend());
714 if (std::distance(StartI, EndI) <= (int)Level)
715 return false;
716 std::advance(StartI, Level);
717 return DPred(StartI->Directive);
718}
719
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000720template <class NamedDirectivesPredicate>
721bool DSAStackTy::hasDirective(NamedDirectivesPredicate DPred, bool FromParent) {
722 auto StartI = std::next(Stack.rbegin());
723 auto EndI = std::prev(Stack.rend());
724 if (FromParent && StartI != EndI) {
725 StartI = std::next(StartI);
726 }
727 for (auto I = StartI, EE = EndI; I != EE; ++I) {
728 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
729 return true;
730 }
731 return false;
732}
733
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000734OpenMPDirectiveKind DSAStackTy::getDirectiveForScope(const Scope *S) const {
735 for (auto I = Stack.rbegin(), EE = Stack.rend(); I != EE; ++I)
736 if (I->CurScope == S)
737 return I->Directive;
738 return OMPD_unknown;
739}
740
Alexey Bataev758e55e2013-09-06 18:03:48 +0000741void Sema::InitDataSharingAttributesStack() {
742 VarDataSharingAttributesStack = new DSAStackTy(*this);
743}
744
745#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
746
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000747bool Sema::IsOpenMPCapturedByRef(VarDecl *VD,
748 const CapturedRegionScopeInfo *RSI) {
749 assert(LangOpts.OpenMP && "OpenMP is not allowed");
750
751 auto &Ctx = getASTContext();
752 bool IsByRef = true;
753
754 // Find the directive that is associated with the provided scope.
755 auto DKind = DSAStack->getDirectiveForScope(RSI->TheScope);
756 auto Ty = VD->getType();
757
758 if (isOpenMPTargetDirective(DKind)) {
759 // This table summarizes how a given variable should be passed to the device
760 // given its type and the clauses where it appears. This table is based on
761 // the description in OpenMP 4.5 [2.10.4, target Construct] and
762 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
763 //
764 // =========================================================================
765 // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
766 // | |(tofrom:scalar)| | pvt | | | |
767 // =========================================================================
768 // | scl | | | | - | | bycopy|
769 // | scl | | - | x | - | - | bycopy|
770 // | scl | | x | - | - | - | null |
771 // | scl | x | | | - | | byref |
772 // | scl | x | - | x | - | - | bycopy|
773 // | scl | x | x | - | - | - | null |
774 // | scl | | - | - | - | x | byref |
775 // | scl | x | - | - | - | x | byref |
776 //
777 // | agg | n.a. | | | - | | byref |
778 // | agg | n.a. | - | x | - | - | byref |
779 // | agg | n.a. | x | - | - | - | null |
780 // | agg | n.a. | - | - | - | x | byref |
781 // | agg | n.a. | - | - | - | x[] | byref |
782 //
783 // | ptr | n.a. | | | - | | bycopy|
784 // | ptr | n.a. | - | x | - | - | bycopy|
785 // | ptr | n.a. | x | - | - | - | null |
786 // | ptr | n.a. | - | - | - | x | byref |
787 // | ptr | n.a. | - | - | - | x[] | bycopy|
788 // | ptr | n.a. | - | - | x | | bycopy|
789 // | ptr | n.a. | - | - | x | x | bycopy|
790 // | ptr | n.a. | - | - | x | x[] | bycopy|
791 // =========================================================================
792 // Legend:
793 // scl - scalar
794 // ptr - pointer
795 // agg - aggregate
796 // x - applies
797 // - - invalid in this combination
798 // [] - mapped with an array section
799 // byref - should be mapped by reference
800 // byval - should be mapped by value
801 // null - initialize a local variable to null on the device
802 //
803 // Observations:
804 // - All scalar declarations that show up in a map clause have to be passed
805 // by reference, because they may have been mapped in the enclosing data
806 // environment.
807 // - If the scalar value does not fit the size of uintptr, it has to be
808 // passed by reference, regardless the result in the table above.
809 // - For pointers mapped by value that have either an implicit map or an
810 // array section, the runtime library may pass the NULL value to the
811 // device instead of the value passed to it by the compiler.
812
813 // FIXME: Right now, only implicit maps are implemented. Properly mapping
814 // values requires having the map, private, and firstprivate clauses SEMA
815 // and parsing in place, which we don't yet.
816
817 if (Ty->isReferenceType())
818 Ty = Ty->castAs<ReferenceType>()->getPointeeType();
819 IsByRef = !Ty->isScalarType();
820 }
821
822 // When passing data by value, we need to make sure it fits the uintptr size
823 // and alignment, because the runtime library only deals with uintptr types.
824 // If it does not fit the uintptr size, we need to pass the data by reference
825 // instead.
826 if (!IsByRef &&
827 (Ctx.getTypeSizeInChars(Ty) >
828 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
829 Ctx.getDeclAlign(VD) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType())))
830 IsByRef = true;
831
832 return IsByRef;
833}
834
Alexey Bataevf841bd92014-12-16 07:00:22 +0000835bool Sema::IsOpenMPCapturedVar(VarDecl *VD) {
836 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000837 VD = VD->getCanonicalDecl();
Samuel Antao4be30e92015-10-02 17:14:03 +0000838
839 // If we are attempting to capture a global variable in a directive with
840 // 'target' we return true so that this global is also mapped to the device.
841 //
842 // FIXME: If the declaration is enclosed in a 'declare target' directive,
843 // then it should not be captured. Therefore, an extra check has to be
844 // inserted here once support for 'declare target' is added.
845 //
846 if (!VD->hasLocalStorage()) {
847 if (DSAStack->getCurrentDirective() == OMPD_target &&
848 !DSAStack->isClauseParsingMode()) {
849 return true;
850 }
851 if (DSAStack->getCurScope() &&
852 DSAStack->hasDirective(
853 [](OpenMPDirectiveKind K, const DeclarationNameInfo &DNI,
854 SourceLocation Loc) -> bool {
855 return isOpenMPTargetDirective(K);
856 },
857 false)) {
858 return true;
859 }
860 }
861
Alexey Bataev48977c32015-08-04 08:10:48 +0000862 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
863 (!DSAStack->isClauseParsingMode() ||
864 DSAStack->getParentDirective() != OMPD_unknown)) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000865 if (DSAStack->isLoopControlVariable(VD) ||
866 (VD->hasLocalStorage() &&
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000867 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
868 DSAStack->isForceVarCapturing())
Alexey Bataev9c821032015-04-30 04:23:23 +0000869 return true;
Alexey Bataevaac108a2015-06-23 04:51:00 +0000870 auto DVarPrivate = DSAStack->getTopDSA(VD, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000871 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
872 return true;
873 DVarPrivate = DSAStack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(),
Alexey Bataevaac108a2015-06-23 04:51:00 +0000874 DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000875 return DVarPrivate.CKind != OMPC_unknown;
876 }
877 return false;
878}
879
Alexey Bataevaac108a2015-06-23 04:51:00 +0000880bool Sema::isOpenMPPrivateVar(VarDecl *VD, unsigned Level) {
881 assert(LangOpts.OpenMP && "OpenMP is not allowed");
882 return DSAStack->hasExplicitDSA(
883 VD, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level);
884}
885
Samuel Antao4be30e92015-10-02 17:14:03 +0000886bool Sema::isOpenMPTargetCapturedVar(VarDecl *VD, unsigned Level) {
887 assert(LangOpts.OpenMP && "OpenMP is not allowed");
888 // Return true if the current level is no longer enclosed in a target region.
889
890 return !VD->hasLocalStorage() &&
891 DSAStack->hasExplicitDirective(isOpenMPTargetDirective, Level);
892}
893
Alexey Bataeved09d242014-05-28 05:53:51 +0000894void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000895
896void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
897 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000898 Scope *CurScope, SourceLocation Loc) {
899 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000900 PushExpressionEvaluationContext(PotentiallyEvaluated);
901}
902
Alexey Bataevaac108a2015-06-23 04:51:00 +0000903void Sema::StartOpenMPClause(OpenMPClauseKind K) {
904 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000905}
906
Alexey Bataevaac108a2015-06-23 04:51:00 +0000907void Sema::EndOpenMPClause() {
908 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000909}
910
Alexey Bataev758e55e2013-09-06 18:03:48 +0000911void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000912 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
913 // A variable of class type (or array thereof) that appears in a lastprivate
914 // clause requires an accessible, unambiguous default constructor for the
915 // class type, unless the list item is also specified in a firstprivate
916 // clause.
917 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000918 for (auto *C : D->clauses()) {
919 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
920 SmallVector<Expr *, 8> PrivateCopies;
921 for (auto *DE : Clause->varlists()) {
922 if (DE->isValueDependent() || DE->isTypeDependent()) {
923 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000924 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +0000925 }
926 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(DE)->getDecl());
Alexey Bataevbd9fec12015-08-18 06:47:21 +0000927 QualType Type = VD->getType().getNonReferenceType();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000928 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000929 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000930 // Generate helper private variable and initialize it with the
931 // default value. The address of the original variable is replaced
932 // by the address of the new private variable in CodeGen. This new
933 // variable is not added to IdResolver, so the code in the OpenMP
934 // region uses original variable for proper diagnostics.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000935 auto *VDPrivate = buildVarDecl(
936 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
937 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +0000938 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
939 if (VDPrivate->isInvalidDecl())
940 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +0000941 PrivateCopies.push_back(buildDeclRefExpr(
942 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +0000943 } else {
944 // The variable is also a firstprivate, so initialization sequence
945 // for private copy is generated already.
946 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000947 }
948 }
Alexey Bataev38e89532015-04-16 04:54:05 +0000949 // Set initializers to private copies if no errors were found.
950 if (PrivateCopies.size() == Clause->varlist_size()) {
951 Clause->setPrivateCopies(PrivateCopies);
952 }
Alexey Bataevf29276e2014-06-18 04:14:57 +0000953 }
954 }
955 }
956
Alexey Bataev758e55e2013-09-06 18:03:48 +0000957 DSAStack->pop();
958 DiscardCleanupsInEvaluationContext();
959 PopExpressionEvaluationContext();
960}
961
Alexander Musman3276a272015-03-21 10:12:56 +0000962static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
963 Expr *NumIterations, Sema &SemaRef,
964 Scope *S);
965
Alexey Bataeva769e072013-03-22 06:34:35 +0000966namespace {
967
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000968class VarDeclFilterCCC : public CorrectionCandidateCallback {
969private:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000970 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +0000971
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000972public:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000973 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +0000974 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000975 NamedDecl *ND = Candidate.getCorrectionDecl();
976 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
977 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000978 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
979 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +0000980 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000981 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000982 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000983};
Alexey Bataeved09d242014-05-28 05:53:51 +0000984} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000985
986ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
987 CXXScopeSpec &ScopeSpec,
988 const DeclarationNameInfo &Id) {
989 LookupResult Lookup(*this, Id, LookupOrdinaryName);
990 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
991
992 if (Lookup.isAmbiguous())
993 return ExprError();
994
995 VarDecl *VD;
996 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +0000997 if (TypoCorrection Corrected = CorrectTypo(
998 Id, LookupOrdinaryName, CurScope, nullptr,
999 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +00001000 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001001 PDiag(Lookup.empty()
1002 ? diag::err_undeclared_var_use_suggest
1003 : diag::err_omp_expected_var_arg_suggest)
1004 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +00001005 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001006 } else {
Richard Smithf9b15102013-08-17 00:46:16 +00001007 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
1008 : diag::err_omp_expected_var_arg)
1009 << Id.getName();
1010 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001011 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001012 } else {
1013 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001014 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001015 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1016 return ExprError();
1017 }
1018 }
1019 Lookup.suppressDiagnostics();
1020
1021 // OpenMP [2.9.2, Syntax, C/C++]
1022 // Variables must be file-scope, namespace-scope, or static block-scope.
1023 if (!VD->hasGlobalStorage()) {
1024 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001025 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1026 bool IsDecl =
1027 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001028 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001029 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1030 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001031 return ExprError();
1032 }
1033
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001034 VarDecl *CanonicalVD = VD->getCanonicalDecl();
1035 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001036 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1037 // A threadprivate directive for file-scope variables must appear outside
1038 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001039 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1040 !getCurLexicalContext()->isTranslationUnit()) {
1041 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001042 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1043 bool IsDecl =
1044 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1045 Diag(VD->getLocation(),
1046 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1047 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001048 return ExprError();
1049 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001050 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1051 // A threadprivate directive for static class member variables must appear
1052 // in the class definition, in the same scope in which the member
1053 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001054 if (CanonicalVD->isStaticDataMember() &&
1055 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1056 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001057 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1058 bool IsDecl =
1059 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1060 Diag(VD->getLocation(),
1061 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1062 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001063 return ExprError();
1064 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001065 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1066 // A threadprivate directive for namespace-scope variables must appear
1067 // outside any definition or declaration other than the namespace
1068 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001069 if (CanonicalVD->getDeclContext()->isNamespace() &&
1070 (!getCurLexicalContext()->isFileContext() ||
1071 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
1072 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001073 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1074 bool IsDecl =
1075 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1076 Diag(VD->getLocation(),
1077 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1078 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001079 return ExprError();
1080 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001081 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
1082 // A threadprivate directive for static block-scope variables must appear
1083 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001084 if (CanonicalVD->isStaticLocal() && CurScope &&
1085 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001086 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001087 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1088 bool IsDecl =
1089 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1090 Diag(VD->getLocation(),
1091 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1092 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001093 return ExprError();
1094 }
1095
1096 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1097 // A threadprivate directive must lexically precede all references to any
1098 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001099 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001100 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +00001101 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001102 return ExprError();
1103 }
1104
1105 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00001106 ExprResult DE = buildDeclRefExpr(*this, VD, ExprType, Id.getLoc());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001107 return DE;
1108}
1109
Alexey Bataeved09d242014-05-28 05:53:51 +00001110Sema::DeclGroupPtrTy
1111Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1112 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001113 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001114 CurContext->addDecl(D);
1115 return DeclGroupPtrTy::make(DeclGroupRef(D));
1116 }
1117 return DeclGroupPtrTy();
1118}
1119
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001120namespace {
1121class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
1122 Sema &SemaRef;
1123
1124public:
1125 bool VisitDeclRefExpr(const DeclRefExpr *E) {
1126 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
1127 if (VD->hasLocalStorage()) {
1128 SemaRef.Diag(E->getLocStart(),
1129 diag::err_omp_local_var_in_threadprivate_init)
1130 << E->getSourceRange();
1131 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1132 << VD << VD->getSourceRange();
1133 return true;
1134 }
1135 }
1136 return false;
1137 }
1138 bool VisitStmt(const Stmt *S) {
1139 for (auto Child : S->children()) {
1140 if (Child && Visit(Child))
1141 return true;
1142 }
1143 return false;
1144 }
Alexey Bataev23b69422014-06-18 07:08:49 +00001145 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001146};
1147} // namespace
1148
Alexey Bataeved09d242014-05-28 05:53:51 +00001149OMPThreadPrivateDecl *
1150Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001151 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001152 for (auto &RefExpr : VarList) {
1153 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001154 VarDecl *VD = cast<VarDecl>(DE->getDecl());
1155 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +00001156
Alexey Bataevf56f98c2015-04-16 05:39:01 +00001157 QualType QType = VD->getType();
1158 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1159 // It will be analyzed later.
1160 Vars.push_back(DE);
1161 continue;
1162 }
1163
Alexey Bataeva769e072013-03-22 06:34:35 +00001164 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1165 // A threadprivate variable must not have an incomplete type.
1166 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001167 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001168 continue;
1169 }
1170
1171 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1172 // A threadprivate variable must not have a reference type.
1173 if (VD->getType()->isReferenceType()) {
1174 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001175 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
1176 bool IsDecl =
1177 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1178 Diag(VD->getLocation(),
1179 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1180 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001181 continue;
1182 }
1183
Samuel Antaof8b50122015-07-13 22:54:53 +00001184 // Check if this is a TLS variable. If TLS is not being supported, produce
1185 // the corresponding diagnostic.
1186 if ((VD->getTLSKind() != VarDecl::TLS_None &&
1187 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1188 getLangOpts().OpenMPUseTLS &&
1189 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +00001190 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
1191 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +00001192 Diag(ILoc, diag::err_omp_var_thread_local)
1193 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +00001194 bool IsDecl =
1195 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1196 Diag(VD->getLocation(),
1197 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1198 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001199 continue;
1200 }
1201
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001202 // Check if initial value of threadprivate variable reference variable with
1203 // local storage (it is not supported by runtime).
1204 if (auto Init = VD->getAnyInitializer()) {
1205 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001206 if (Checker.Visit(Init))
1207 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001208 }
1209
Alexey Bataeved09d242014-05-28 05:53:51 +00001210 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00001211 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00001212 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1213 Context, SourceRange(Loc, Loc)));
1214 if (auto *ML = Context.getASTMutationListener())
1215 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00001216 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001217 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001218 if (!Vars.empty()) {
1219 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1220 Vars);
1221 D->setAccess(AS_public);
1222 }
1223 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00001224}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001225
Alexey Bataev7ff55242014-06-19 09:13:45 +00001226static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
1227 const VarDecl *VD, DSAStackTy::DSAVarData DVar,
1228 bool IsLoopIterVar = false) {
1229 if (DVar.RefExpr) {
1230 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1231 << getOpenMPClauseName(DVar.CKind);
1232 return;
1233 }
1234 enum {
1235 PDSA_StaticMemberShared,
1236 PDSA_StaticLocalVarShared,
1237 PDSA_LoopIterVarPrivate,
1238 PDSA_LoopIterVarLinear,
1239 PDSA_LoopIterVarLastprivate,
1240 PDSA_ConstVarShared,
1241 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001242 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001243 PDSA_LocalVarPrivate,
1244 PDSA_Implicit
1245 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001246 bool ReportHint = false;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001247 auto ReportLoc = VD->getLocation();
Alexey Bataev7ff55242014-06-19 09:13:45 +00001248 if (IsLoopIterVar) {
1249 if (DVar.CKind == OMPC_private)
1250 Reason = PDSA_LoopIterVarPrivate;
1251 else if (DVar.CKind == OMPC_lastprivate)
1252 Reason = PDSA_LoopIterVarLastprivate;
1253 else
1254 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001255 } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) {
1256 Reason = PDSA_TaskVarFirstprivate;
1257 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001258 } else if (VD->isStaticLocal())
1259 Reason = PDSA_StaticLocalVarShared;
1260 else if (VD->isStaticDataMember())
1261 Reason = PDSA_StaticMemberShared;
1262 else if (VD->isFileVarDecl())
1263 Reason = PDSA_GlobalVarShared;
1264 else if (VD->getType().isConstant(SemaRef.getASTContext()))
1265 Reason = PDSA_ConstVarShared;
Alexey Bataevbae9a792014-06-27 10:37:06 +00001266 else if (VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00001267 ReportHint = true;
1268 Reason = PDSA_LocalVarPrivate;
1269 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00001270 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001271 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00001272 << Reason << ReportHint
1273 << getOpenMPDirectiveName(Stack->getCurrentDirective());
1274 } else if (DVar.ImplicitDSALoc.isValid()) {
1275 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1276 << getOpenMPClauseName(DVar.CKind);
1277 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00001278}
1279
Alexey Bataev758e55e2013-09-06 18:03:48 +00001280namespace {
1281class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1282 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001283 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001284 bool ErrorFound;
1285 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001286 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev4acb8592014-07-07 13:01:15 +00001287 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +00001288
Alexey Bataev758e55e2013-09-06 18:03:48 +00001289public:
1290 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001291 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001292 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +00001293 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1294 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001295
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001296 auto DVar = Stack->getTopDSA(VD, false);
1297 // Check if the variable has explicit DSA set and stop analysis if it so.
1298 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001299
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001300 auto ELoc = E->getExprLoc();
1301 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001302 // The default(none) clause requires that each variable that is referenced
1303 // in the construct, and does not have a predetermined data-sharing
1304 // attribute, must have its data-sharing attribute explicitly determined
1305 // by being listed in a data-sharing attribute clause.
1306 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001307 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001308 VarsWithInheritedDSA.count(VD) == 0) {
1309 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001310 return;
1311 }
1312
1313 // OpenMP [2.9.3.6, Restrictions, p.2]
1314 // A list item that appears in a reduction clause of the innermost
1315 // enclosing worksharing or parallel construct may not be accessed in an
1316 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +00001317 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001318 [](OpenMPDirectiveKind K) -> bool {
1319 return isOpenMPParallelDirective(K) ||
Alexey Bataev13314bf2014-10-09 04:18:56 +00001320 isOpenMPWorksharingDirective(K) ||
1321 isOpenMPTeamsDirective(K);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001322 },
1323 false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001324 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
1325 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001326 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1327 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001328 return;
1329 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001330
1331 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001332 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001333 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001334 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001335 }
1336 }
1337 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001338 for (auto *C : S->clauses()) {
1339 // Skip analysis of arguments of implicitly defined firstprivate clause
1340 // for task directives.
1341 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1342 for (auto *CC : C->children()) {
1343 if (CC)
1344 Visit(CC);
1345 }
1346 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001347 }
1348 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001349 for (auto *C : S->children()) {
1350 if (C && !isa<OMPExecutableDirective>(C))
1351 Visit(C);
1352 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001353 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001354
1355 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001356 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001357 llvm::DenseMap<VarDecl *, Expr *> &getVarsWithInheritedDSA() {
1358 return VarsWithInheritedDSA;
1359 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001360
Alexey Bataev7ff55242014-06-19 09:13:45 +00001361 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1362 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00001363};
Alexey Bataeved09d242014-05-28 05:53:51 +00001364} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001365
Alexey Bataevbae9a792014-06-27 10:37:06 +00001366void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001367 switch (DKind) {
1368 case OMPD_parallel: {
1369 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001370 QualType KmpInt32PtrTy =
1371 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001372 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001373 std::make_pair(".global_tid.", KmpInt32PtrTy),
1374 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1375 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001376 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001377 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1378 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001379 break;
1380 }
1381 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001382 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001383 std::make_pair(StringRef(), QualType()) // __context with shared vars
1384 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001385 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1386 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001387 break;
1388 }
1389 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001390 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001391 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001392 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001393 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1394 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001395 break;
1396 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001397 case OMPD_for_simd: {
1398 Sema::CapturedParamNameType Params[] = {
1399 std::make_pair(StringRef(), QualType()) // __context with shared vars
1400 };
1401 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1402 Params);
1403 break;
1404 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001405 case OMPD_sections: {
1406 Sema::CapturedParamNameType Params[] = {
1407 std::make_pair(StringRef(), QualType()) // __context with shared vars
1408 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001409 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1410 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001411 break;
1412 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001413 case OMPD_section: {
1414 Sema::CapturedParamNameType Params[] = {
1415 std::make_pair(StringRef(), QualType()) // __context with shared vars
1416 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001417 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1418 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001419 break;
1420 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001421 case OMPD_single: {
1422 Sema::CapturedParamNameType Params[] = {
1423 std::make_pair(StringRef(), QualType()) // __context with shared vars
1424 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001425 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1426 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001427 break;
1428 }
Alexander Musman80c22892014-07-17 08:54:58 +00001429 case OMPD_master: {
1430 Sema::CapturedParamNameType Params[] = {
1431 std::make_pair(StringRef(), QualType()) // __context with shared vars
1432 };
1433 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1434 Params);
1435 break;
1436 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001437 case OMPD_critical: {
1438 Sema::CapturedParamNameType Params[] = {
1439 std::make_pair(StringRef(), QualType()) // __context with shared vars
1440 };
1441 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1442 Params);
1443 break;
1444 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001445 case OMPD_parallel_for: {
1446 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001447 QualType KmpInt32PtrTy =
1448 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev4acb8592014-07-07 13:01:15 +00001449 Sema::CapturedParamNameType Params[] = {
1450 std::make_pair(".global_tid.", KmpInt32PtrTy),
1451 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1452 std::make_pair(StringRef(), QualType()) // __context with shared vars
1453 };
1454 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1455 Params);
1456 break;
1457 }
Alexander Musmane4e893b2014-09-23 09:33:00 +00001458 case OMPD_parallel_for_simd: {
1459 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001460 QualType KmpInt32PtrTy =
1461 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexander Musmane4e893b2014-09-23 09:33:00 +00001462 Sema::CapturedParamNameType Params[] = {
1463 std::make_pair(".global_tid.", KmpInt32PtrTy),
1464 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1465 std::make_pair(StringRef(), QualType()) // __context with shared vars
1466 };
1467 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1468 Params);
1469 break;
1470 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001471 case OMPD_parallel_sections: {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001472 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001473 QualType KmpInt32PtrTy =
1474 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001475 Sema::CapturedParamNameType Params[] = {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001476 std::make_pair(".global_tid.", KmpInt32PtrTy),
1477 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001478 std::make_pair(StringRef(), QualType()) // __context with shared vars
1479 };
1480 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1481 Params);
1482 break;
1483 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001484 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001485 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001486 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1487 FunctionProtoType::ExtProtoInfo EPI;
1488 EPI.Variadic = true;
1489 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001490 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001491 std::make_pair(".global_tid.", KmpInt32Ty),
1492 std::make_pair(".part_id.", KmpInt32Ty),
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001493 std::make_pair(".privates.",
1494 Context.VoidPtrTy.withConst().withRestrict()),
1495 std::make_pair(
1496 ".copy_fn.",
1497 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001498 std::make_pair(StringRef(), QualType()) // __context with shared vars
1499 };
1500 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1501 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001502 // Mark this captured region as inlined, because we don't use outlined
1503 // function directly.
1504 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1505 AlwaysInlineAttr::CreateImplicit(
1506 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001507 break;
1508 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001509 case OMPD_ordered: {
1510 Sema::CapturedParamNameType Params[] = {
1511 std::make_pair(StringRef(), QualType()) // __context with shared vars
1512 };
1513 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1514 Params);
1515 break;
1516 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001517 case OMPD_atomic: {
1518 Sema::CapturedParamNameType Params[] = {
1519 std::make_pair(StringRef(), QualType()) // __context with shared vars
1520 };
1521 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1522 Params);
1523 break;
1524 }
Michael Wong65f367f2015-07-21 13:44:28 +00001525 case OMPD_target_data:
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001526 case OMPD_target: {
1527 Sema::CapturedParamNameType Params[] = {
1528 std::make_pair(StringRef(), QualType()) // __context with shared vars
1529 };
1530 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1531 Params);
1532 break;
1533 }
Alexey Bataev13314bf2014-10-09 04:18:56 +00001534 case OMPD_teams: {
1535 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001536 QualType KmpInt32PtrTy =
1537 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev13314bf2014-10-09 04:18:56 +00001538 Sema::CapturedParamNameType Params[] = {
1539 std::make_pair(".global_tid.", KmpInt32PtrTy),
1540 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1541 std::make_pair(StringRef(), QualType()) // __context with shared vars
1542 };
1543 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1544 Params);
1545 break;
1546 }
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001547 case OMPD_taskgroup: {
1548 Sema::CapturedParamNameType Params[] = {
1549 std::make_pair(StringRef(), QualType()) // __context with shared vars
1550 };
1551 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1552 Params);
1553 break;
1554 }
Alexey Bataev49f6e782015-12-01 04:18:41 +00001555 case OMPD_taskloop: {
1556 Sema::CapturedParamNameType Params[] = {
1557 std::make_pair(StringRef(), QualType()) // __context with shared vars
1558 };
1559 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1560 Params);
1561 break;
1562 }
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001563 case OMPD_taskloop_simd: {
1564 Sema::CapturedParamNameType Params[] = {
1565 std::make_pair(StringRef(), QualType()) // __context with shared vars
1566 };
1567 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1568 Params);
1569 break;
1570 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001571 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001572 case OMPD_taskyield:
1573 case OMPD_barrier:
1574 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001575 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001576 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00001577 case OMPD_flush:
Alexey Bataev9959db52014-05-06 10:08:46 +00001578 llvm_unreachable("OpenMP Directive is not allowed");
1579 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001580 llvm_unreachable("Unknown OpenMP directive");
1581 }
1582}
1583
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001584StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1585 ArrayRef<OMPClause *> Clauses) {
1586 if (!S.isUsable()) {
1587 ActOnCapturedRegionError();
1588 return StmtError();
1589 }
Alexey Bataev040d5402015-05-12 08:35:28 +00001590 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001591 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001592 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001593 Clause->getClauseKind() == OMPC_copyprivate ||
1594 (getLangOpts().OpenMPUseTLS &&
1595 getASTContext().getTargetInfo().isTLSSupported() &&
1596 Clause->getClauseKind() == OMPC_copyin)) {
1597 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001598 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001599 for (auto *VarRef : Clause->children()) {
1600 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001601 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001602 }
1603 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001604 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev040d5402015-05-12 08:35:28 +00001605 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
1606 Clause->getClauseKind() == OMPC_schedule) {
1607 // Mark all variables in private list clauses as used in inner region.
1608 // Required for proper codegen of combined directives.
1609 // TODO: add processing for other clauses.
1610 if (auto *E = cast_or_null<Expr>(
1611 cast<OMPScheduleClause>(Clause)->getHelperChunkSize())) {
1612 MarkDeclarationsReferencedInExpr(E);
1613 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001614 }
1615 }
1616 return ActOnCapturedRegionEnd(S.get());
1617}
1618
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001619static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1620 OpenMPDirectiveKind CurrentRegion,
1621 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001622 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001623 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001624 // Allowed nesting of constructs
1625 // +------------------+-----------------+------------------------------------+
1626 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1627 // +------------------+-----------------+------------------------------------+
1628 // | parallel | parallel | * |
1629 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001630 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001631 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001632 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001633 // | parallel | simd | * |
1634 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001635 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001636 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001637 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001638 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001639 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001640 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001641 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001642 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001643 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001644 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001645 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001646 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001647 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001648 // | parallel | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001649 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001650 // | parallel | cancellation | |
1651 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001652 // | parallel | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001653 // | parallel | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001654 // | parallel | taskloop simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001655 // +------------------+-----------------+------------------------------------+
1656 // | for | parallel | * |
1657 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001658 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001659 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001660 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001661 // | for | simd | * |
1662 // | for | sections | + |
1663 // | for | section | + |
1664 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001665 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001666 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001667 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001668 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001669 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001670 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001671 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001672 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001673 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001674 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001675 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001676 // | for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001677 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001678 // | for | cancellation | |
1679 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001680 // | for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001681 // | for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001682 // | for | taskloop simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001683 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001684 // | master | parallel | * |
1685 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001686 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001687 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001688 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001689 // | master | simd | * |
1690 // | master | sections | + |
1691 // | master | section | + |
1692 // | master | single | + |
1693 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001694 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001695 // | master |parallel sections| * |
1696 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001697 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001698 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001699 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001700 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001701 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001702 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001703 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001704 // | master | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001705 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001706 // | master | cancellation | |
1707 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001708 // | master | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001709 // | master | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001710 // | master | taskloop simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001711 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001712 // | critical | parallel | * |
1713 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001714 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001715 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001716 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001717 // | critical | simd | * |
1718 // | critical | sections | + |
1719 // | critical | section | + |
1720 // | critical | single | + |
1721 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001722 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001723 // | critical |parallel sections| * |
1724 // | critical | task | * |
1725 // | critical | taskyield | * |
1726 // | critical | barrier | + |
1727 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001728 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001729 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001730 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001731 // | critical | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001732 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001733 // | critical | cancellation | |
1734 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001735 // | critical | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001736 // | critical | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001737 // | critical | taskloop simd | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001738 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001739 // | simd | parallel | |
1740 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001741 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00001742 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001743 // | simd | critical | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001744 // | simd | simd | |
1745 // | simd | sections | |
1746 // | simd | section | |
1747 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001748 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001749 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001750 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001751 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00001752 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001753 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001754 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001755 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00001756 // | simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001757 // | simd | ordered | + (with simd clause) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001758 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001759 // | simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001760 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001761 // | simd | cancellation | |
1762 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001763 // | simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001764 // | simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001765 // | simd | taskloop simd | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001766 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00001767 // | for simd | parallel | |
1768 // | for simd | for | |
1769 // | for simd | for simd | |
1770 // | for simd | master | |
1771 // | for simd | critical | |
1772 // | for simd | simd | |
1773 // | for simd | sections | |
1774 // | for simd | section | |
1775 // | for simd | single | |
1776 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001777 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001778 // | for simd |parallel sections| |
1779 // | for simd | task | |
1780 // | for simd | taskyield | |
1781 // | for simd | barrier | |
1782 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001783 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001784 // | for simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001785 // | for simd | ordered | + (with simd clause) |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001786 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001787 // | for simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001788 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001789 // | for simd | cancellation | |
1790 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001791 // | for simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001792 // | for simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001793 // | for simd | taskloop simd | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001794 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00001795 // | parallel for simd| parallel | |
1796 // | parallel for simd| for | |
1797 // | parallel for simd| for simd | |
1798 // | parallel for simd| master | |
1799 // | parallel for simd| critical | |
1800 // | parallel for simd| simd | |
1801 // | parallel for simd| sections | |
1802 // | parallel for simd| section | |
1803 // | parallel for simd| single | |
1804 // | parallel for simd| parallel for | |
1805 // | parallel for simd|parallel for simd| |
1806 // | parallel for simd|parallel sections| |
1807 // | parallel for simd| task | |
1808 // | parallel for simd| taskyield | |
1809 // | parallel for simd| barrier | |
1810 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001811 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001812 // | parallel for simd| flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001813 // | parallel for simd| ordered | + (with simd clause) |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001814 // | parallel for simd| atomic | |
1815 // | parallel for simd| target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001816 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001817 // | parallel for simd| cancellation | |
1818 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001819 // | parallel for simd| cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001820 // | parallel for simd| taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001821 // | parallel for simd| taskloop simd | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001822 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001823 // | sections | parallel | * |
1824 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001825 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001826 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001827 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001828 // | sections | simd | * |
1829 // | sections | sections | + |
1830 // | sections | section | * |
1831 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001832 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001833 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001834 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001835 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001836 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001837 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001838 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001839 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001840 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001841 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001842 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001843 // | sections | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001844 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001845 // | sections | cancellation | |
1846 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001847 // | sections | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001848 // | sections | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001849 // | sections | taskloop simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001850 // +------------------+-----------------+------------------------------------+
1851 // | section | parallel | * |
1852 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001853 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001854 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001855 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001856 // | section | simd | * |
1857 // | section | sections | + |
1858 // | section | section | + |
1859 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001860 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001861 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001862 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001863 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001864 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001865 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001866 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001867 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001868 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001869 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001870 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001871 // | section | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001872 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001873 // | section | cancellation | |
1874 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001875 // | section | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001876 // | section | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001877 // | section | taskloop simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001878 // +------------------+-----------------+------------------------------------+
1879 // | single | parallel | * |
1880 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001881 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001882 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001883 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001884 // | single | simd | * |
1885 // | single | sections | + |
1886 // | single | section | + |
1887 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001888 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001889 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001890 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001891 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001892 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001893 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001894 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001895 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001896 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001897 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001898 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001899 // | single | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001900 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001901 // | single | cancellation | |
1902 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001903 // | single | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001904 // | single | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001905 // | single | taskloop simd | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001906 // +------------------+-----------------+------------------------------------+
1907 // | parallel for | parallel | * |
1908 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001909 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001910 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001911 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001912 // | parallel for | simd | * |
1913 // | parallel for | sections | + |
1914 // | parallel for | section | + |
1915 // | parallel for | single | + |
1916 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001917 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001918 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001919 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001920 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001921 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001922 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001923 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001924 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001925 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001926 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001927 // | parallel for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001928 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001929 // | parallel for | cancellation | |
1930 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001931 // | parallel for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001932 // | parallel for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001933 // | parallel for | taskloop simd | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001934 // +------------------+-----------------+------------------------------------+
1935 // | parallel sections| parallel | * |
1936 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001937 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001938 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001939 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001940 // | parallel sections| simd | * |
1941 // | parallel sections| sections | + |
1942 // | parallel sections| section | * |
1943 // | parallel sections| single | + |
1944 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001945 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001946 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001947 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001948 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001949 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001950 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001951 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001952 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001953 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001954 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001955 // | parallel sections| target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001956 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001957 // | parallel sections| cancellation | |
1958 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001959 // | parallel sections| cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001960 // | parallel sections| taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001961 // | parallel sections| taskloop simd | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001962 // +------------------+-----------------+------------------------------------+
1963 // | task | parallel | * |
1964 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001965 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001966 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001967 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001968 // | task | simd | * |
1969 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001970 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001971 // | task | single | + |
1972 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001973 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001974 // | task |parallel sections| * |
1975 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001976 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001977 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001978 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001979 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001980 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001981 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001982 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001983 // | task | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001984 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001985 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00001986 // | | point | ! |
1987 // | task | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001988 // | task | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001989 // | task | taskloop simd | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001990 // +------------------+-----------------+------------------------------------+
1991 // | ordered | parallel | * |
1992 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001993 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001994 // | ordered | master | * |
1995 // | ordered | critical | * |
1996 // | ordered | simd | * |
1997 // | ordered | sections | + |
1998 // | ordered | section | + |
1999 // | ordered | single | + |
2000 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002001 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002002 // | ordered |parallel sections| * |
2003 // | ordered | task | * |
2004 // | ordered | taskyield | * |
2005 // | ordered | barrier | + |
2006 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002007 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002008 // | ordered | flush | * |
2009 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002010 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002011 // | ordered | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002012 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002013 // | ordered | cancellation | |
2014 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002015 // | ordered | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002016 // | ordered | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002017 // | ordered | taskloop simd | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002018 // +------------------+-----------------+------------------------------------+
2019 // | atomic | parallel | |
2020 // | atomic | for | |
2021 // | atomic | for simd | |
2022 // | atomic | master | |
2023 // | atomic | critical | |
2024 // | atomic | simd | |
2025 // | atomic | sections | |
2026 // | atomic | section | |
2027 // | atomic | single | |
2028 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002029 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002030 // | atomic |parallel sections| |
2031 // | atomic | task | |
2032 // | atomic | taskyield | |
2033 // | atomic | barrier | |
2034 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002035 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002036 // | atomic | flush | |
2037 // | atomic | ordered | |
2038 // | atomic | atomic | |
2039 // | atomic | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002040 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002041 // | atomic | cancellation | |
2042 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002043 // | atomic | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002044 // | atomic | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002045 // | atomic | taskloop simd | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002046 // +------------------+-----------------+------------------------------------+
2047 // | target | parallel | * |
2048 // | target | for | * |
2049 // | target | for simd | * |
2050 // | target | master | * |
2051 // | target | critical | * |
2052 // | target | simd | * |
2053 // | target | sections | * |
2054 // | target | section | * |
2055 // | target | single | * |
2056 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002057 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002058 // | target |parallel sections| * |
2059 // | target | task | * |
2060 // | target | taskyield | * |
2061 // | target | barrier | * |
2062 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002063 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002064 // | target | flush | * |
2065 // | target | ordered | * |
2066 // | target | atomic | * |
2067 // | target | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002068 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002069 // | target | cancellation | |
2070 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002071 // | target | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002072 // | target | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002073 // | target | taskloop simd | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002074 // +------------------+-----------------+------------------------------------+
2075 // | teams | parallel | * |
2076 // | teams | for | + |
2077 // | teams | for simd | + |
2078 // | teams | master | + |
2079 // | teams | critical | + |
2080 // | teams | simd | + |
2081 // | teams | sections | + |
2082 // | teams | section | + |
2083 // | teams | single | + |
2084 // | teams | parallel for | * |
2085 // | teams |parallel for simd| * |
2086 // | teams |parallel sections| * |
2087 // | teams | task | + |
2088 // | teams | taskyield | + |
2089 // | teams | barrier | + |
2090 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00002091 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002092 // | teams | flush | + |
2093 // | teams | ordered | + |
2094 // | teams | atomic | + |
2095 // | teams | target | + |
2096 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002097 // | teams | cancellation | |
2098 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002099 // | teams | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002100 // | teams | taskloop | + |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002101 // | teams | taskloop simd | + |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002102 // +------------------+-----------------+------------------------------------+
2103 // | taskloop | parallel | * |
2104 // | taskloop | for | + |
2105 // | taskloop | for simd | + |
2106 // | taskloop | master | + |
2107 // | taskloop | critical | * |
2108 // | taskloop | simd | * |
2109 // | taskloop | sections | + |
2110 // | taskloop | section | + |
2111 // | taskloop | single | + |
2112 // | taskloop | parallel for | * |
2113 // | taskloop |parallel for simd| * |
2114 // | taskloop |parallel sections| * |
2115 // | taskloop | task | * |
2116 // | taskloop | taskyield | * |
2117 // | taskloop | barrier | + |
2118 // | taskloop | taskwait | * |
2119 // | taskloop | taskgroup | * |
2120 // | taskloop | flush | * |
2121 // | taskloop | ordered | + |
2122 // | taskloop | atomic | * |
2123 // | taskloop | target | * |
2124 // | taskloop | teams | + |
2125 // | taskloop | cancellation | |
2126 // | | point | |
2127 // | taskloop | cancel | |
2128 // | taskloop | taskloop | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002129 // +------------------+-----------------+------------------------------------+
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002130 // | taskloop simd | parallel | |
2131 // | taskloop simd | for | |
2132 // | taskloop simd | for simd | |
2133 // | taskloop simd | master | |
2134 // | taskloop simd | critical | |
2135 // | taskloop simd | simd | |
2136 // | taskloop simd | sections | |
2137 // | taskloop simd | section | |
2138 // | taskloop simd | single | |
2139 // | taskloop simd | parallel for | |
2140 // | taskloop simd |parallel for simd| |
2141 // | taskloop simd |parallel sections| |
2142 // | taskloop simd | task | |
2143 // | taskloop simd | taskyield | |
2144 // | taskloop simd | barrier | |
2145 // | taskloop simd | taskwait | |
2146 // | taskloop simd | taskgroup | |
2147 // | taskloop simd | flush | |
2148 // | taskloop simd | ordered | + (with simd clause) |
2149 // | taskloop simd | atomic | |
2150 // | taskloop simd | target | |
2151 // | taskloop simd | teams | |
2152 // | taskloop simd | cancellation | |
2153 // | | point | |
2154 // | taskloop simd | cancel | |
2155 // | taskloop simd | taskloop | |
2156 // | taskloop simd | taskloop simd | |
2157 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00002158 if (Stack->getCurScope()) {
2159 auto ParentRegion = Stack->getParentDirective();
2160 bool NestingProhibited = false;
2161 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002162 enum {
2163 NoRecommend,
2164 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00002165 ShouldBeInOrderedRegion,
2166 ShouldBeInTargetRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002167 } Recommend = NoRecommend;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002168 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002169 // OpenMP [2.16, Nesting of Regions]
2170 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002171 // OpenMP [2.8.1,simd Construct, Restrictions]
2172 // An ordered construct with the simd clause is the only OpenMP construct
2173 // that can appear in the simd region.
Alexey Bataev549210e2014-06-24 04:39:47 +00002174 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
2175 return true;
2176 }
Alexey Bataev0162e452014-07-22 10:10:35 +00002177 if (ParentRegion == OMPD_atomic) {
2178 // OpenMP [2.16, Nesting of Regions]
2179 // OpenMP constructs may not be nested inside an atomic region.
2180 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
2181 return true;
2182 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002183 if (CurrentRegion == OMPD_section) {
2184 // OpenMP [2.7.2, sections Construct, Restrictions]
2185 // Orphaned section directives are prohibited. That is, the section
2186 // directives must appear within the sections construct and must not be
2187 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002188 if (ParentRegion != OMPD_sections &&
2189 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002190 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
2191 << (ParentRegion != OMPD_unknown)
2192 << getOpenMPDirectiveName(ParentRegion);
2193 return true;
2194 }
2195 return false;
2196 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002197 // Allow some constructs to be orphaned (they could be used in functions,
2198 // called from OpenMP regions with the required preconditions).
2199 if (ParentRegion == OMPD_unknown)
2200 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00002201 if (CurrentRegion == OMPD_cancellation_point ||
2202 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002203 // OpenMP [2.16, Nesting of Regions]
2204 // A cancellation point construct for which construct-type-clause is
2205 // taskgroup must be nested inside a task construct. A cancellation
2206 // point construct for which construct-type-clause is not taskgroup must
2207 // be closely nested inside an OpenMP construct that matches the type
2208 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00002209 // A cancel construct for which construct-type-clause is taskgroup must be
2210 // nested inside a task construct. A cancel construct for which
2211 // construct-type-clause is not taskgroup must be closely nested inside an
2212 // OpenMP construct that matches the type specified in
2213 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002214 NestingProhibited =
2215 !((CancelRegion == OMPD_parallel && ParentRegion == OMPD_parallel) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00002216 (CancelRegion == OMPD_for &&
2217 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002218 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
2219 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00002220 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
2221 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002222 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00002223 // OpenMP [2.16, Nesting of Regions]
2224 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002225 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00002226 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002227 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002228 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002229 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
2230 // OpenMP [2.16, Nesting of Regions]
2231 // A critical region may not be nested (closely or otherwise) inside a
2232 // critical region with the same name. Note that this restriction is not
2233 // sufficient to prevent deadlock.
2234 SourceLocation PreviousCriticalLoc;
2235 bool DeadLock =
2236 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
2237 OpenMPDirectiveKind K,
2238 const DeclarationNameInfo &DNI,
2239 SourceLocation Loc)
2240 ->bool {
2241 if (K == OMPD_critical &&
2242 DNI.getName() == CurrentName.getName()) {
2243 PreviousCriticalLoc = Loc;
2244 return true;
2245 } else
2246 return false;
2247 },
2248 false /* skip top directive */);
2249 if (DeadLock) {
2250 SemaRef.Diag(StartLoc,
2251 diag::err_omp_prohibited_region_critical_same_name)
2252 << CurrentName.getName();
2253 if (PreviousCriticalLoc.isValid())
2254 SemaRef.Diag(PreviousCriticalLoc,
2255 diag::note_omp_previous_critical_region);
2256 return true;
2257 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002258 } else if (CurrentRegion == OMPD_barrier) {
2259 // OpenMP [2.16, Nesting of Regions]
2260 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002261 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002262 NestingProhibited =
2263 isOpenMPWorksharingDirective(ParentRegion) ||
2264 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002265 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002266 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musman80c22892014-07-17 08:54:58 +00002267 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00002268 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002269 // OpenMP [2.16, Nesting of Regions]
2270 // A worksharing region may not be closely nested inside a worksharing,
2271 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002272 NestingProhibited =
Alexander Musmanf82886e2014-09-18 05:12:34 +00002273 isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002274 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002275 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002276 isOpenMPTaskLoopDirective(ParentRegion);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002277 Recommend = ShouldBeInParallelRegion;
2278 } else if (CurrentRegion == OMPD_ordered) {
2279 // OpenMP [2.16, Nesting of Regions]
2280 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00002281 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002282 // An ordered region must be closely nested inside a loop region (or
2283 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002284 // OpenMP [2.8.1,simd Construct, Restrictions]
2285 // An ordered construct with the simd clause is the only OpenMP construct
2286 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002287 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00002288 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002289 isOpenMPTaskLoopDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002290 !(isOpenMPSimdDirective(ParentRegion) ||
2291 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002292 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002293 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
2294 // OpenMP [2.16, Nesting of Regions]
2295 // If specified, a teams construct must be contained within a target
2296 // construct.
2297 NestingProhibited = ParentRegion != OMPD_target;
2298 Recommend = ShouldBeInTargetRegion;
2299 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
2300 }
2301 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
2302 // OpenMP [2.16, Nesting of Regions]
2303 // distribute, parallel, parallel sections, parallel workshare, and the
2304 // parallel loop and parallel loop SIMD constructs are the only OpenMP
2305 // constructs that can be closely nested in the teams region.
2306 // TODO: add distribute directive.
2307 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion);
2308 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002309 }
2310 if (NestingProhibited) {
2311 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002312 << CloseNesting << getOpenMPDirectiveName(ParentRegion) << Recommend
2313 << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00002314 return true;
2315 }
2316 }
2317 return false;
2318}
2319
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002320static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
2321 ArrayRef<OMPClause *> Clauses,
2322 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
2323 bool ErrorFound = false;
2324 unsigned NamedModifiersNumber = 0;
2325 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
2326 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00002327 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002328 for (const auto *C : Clauses) {
2329 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
2330 // At most one if clause without a directive-name-modifier can appear on
2331 // the directive.
2332 OpenMPDirectiveKind CurNM = IC->getNameModifier();
2333 if (FoundNameModifiers[CurNM]) {
2334 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
2335 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
2336 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
2337 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002338 } else if (CurNM != OMPD_unknown) {
2339 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002340 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002341 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002342 FoundNameModifiers[CurNM] = IC;
2343 if (CurNM == OMPD_unknown)
2344 continue;
2345 // Check if the specified name modifier is allowed for the current
2346 // directive.
2347 // At most one if clause with the particular directive-name-modifier can
2348 // appear on the directive.
2349 bool MatchFound = false;
2350 for (auto NM : AllowedNameModifiers) {
2351 if (CurNM == NM) {
2352 MatchFound = true;
2353 break;
2354 }
2355 }
2356 if (!MatchFound) {
2357 S.Diag(IC->getNameModifierLoc(),
2358 diag::err_omp_wrong_if_directive_name_modifier)
2359 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
2360 ErrorFound = true;
2361 }
2362 }
2363 }
2364 // If any if clause on the directive includes a directive-name-modifier then
2365 // all if clauses on the directive must include a directive-name-modifier.
2366 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
2367 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
2368 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
2369 diag::err_omp_no_more_if_clause);
2370 } else {
2371 std::string Values;
2372 std::string Sep(", ");
2373 unsigned AllowedCnt = 0;
2374 unsigned TotalAllowedNum =
2375 AllowedNameModifiers.size() - NamedModifiersNumber;
2376 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
2377 ++Cnt) {
2378 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
2379 if (!FoundNameModifiers[NM]) {
2380 Values += "'";
2381 Values += getOpenMPDirectiveName(NM);
2382 Values += "'";
2383 if (AllowedCnt + 2 == TotalAllowedNum)
2384 Values += " or ";
2385 else if (AllowedCnt + 1 != TotalAllowedNum)
2386 Values += Sep;
2387 ++AllowedCnt;
2388 }
2389 }
2390 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
2391 diag::err_omp_unnamed_if_clause)
2392 << (TotalAllowedNum > 1) << Values;
2393 }
Alexey Bataevecb156a2015-09-15 17:23:56 +00002394 for (auto Loc : NameModifierLoc) {
2395 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
2396 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002397 ErrorFound = true;
2398 }
2399 return ErrorFound;
2400}
2401
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002402StmtResult Sema::ActOnOpenMPExecutableDirective(
2403 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
2404 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
2405 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002406 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002407 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
2408 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00002409 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002410
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002411 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev68446b72014-07-18 07:47:19 +00002412 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002413 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00002414 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00002415 if (AStmt) {
2416 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
2417
2418 // Check default data sharing attributes for referenced variables.
2419 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
2420 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
2421 if (DSAChecker.isErrorFound())
2422 return StmtError();
2423 // Generate list of implicitly defined firstprivate variables.
2424 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00002425
2426 if (!DSAChecker.getImplicitFirstprivate().empty()) {
2427 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
2428 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
2429 SourceLocation(), SourceLocation())) {
2430 ClausesWithImplicit.push_back(Implicit);
2431 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
2432 DSAChecker.getImplicitFirstprivate().size();
2433 } else
2434 ErrorFound = true;
2435 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002436 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002437
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002438 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002439 switch (Kind) {
2440 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00002441 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
2442 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002443 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002444 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002445 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002446 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2447 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002448 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002449 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002450 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2451 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002452 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00002453 case OMPD_for_simd:
2454 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2455 EndLoc, VarsWithInheritedDSA);
2456 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002457 case OMPD_sections:
2458 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
2459 EndLoc);
2460 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002461 case OMPD_section:
2462 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00002463 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002464 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
2465 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002466 case OMPD_single:
2467 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
2468 EndLoc);
2469 break;
Alexander Musman80c22892014-07-17 08:54:58 +00002470 case OMPD_master:
2471 assert(ClausesWithImplicit.empty() &&
2472 "No clauses are allowed for 'omp master' directive");
2473 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
2474 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002475 case OMPD_critical:
2476 assert(ClausesWithImplicit.empty() &&
2477 "No clauses are allowed for 'omp critical' directive");
2478 Res = ActOnOpenMPCriticalDirective(DirName, AStmt, StartLoc, EndLoc);
2479 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002480 case OMPD_parallel_for:
2481 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
2482 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002483 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00002484 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00002485 case OMPD_parallel_for_simd:
2486 Res = ActOnOpenMPParallelForSimdDirective(
2487 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002488 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00002489 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002490 case OMPD_parallel_sections:
2491 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
2492 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002493 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002494 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002495 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002496 Res =
2497 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002498 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002499 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00002500 case OMPD_taskyield:
2501 assert(ClausesWithImplicit.empty() &&
2502 "No clauses are allowed for 'omp taskyield' directive");
2503 assert(AStmt == nullptr &&
2504 "No associated statement allowed for 'omp taskyield' directive");
2505 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
2506 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002507 case OMPD_barrier:
2508 assert(ClausesWithImplicit.empty() &&
2509 "No clauses are allowed for 'omp barrier' directive");
2510 assert(AStmt == nullptr &&
2511 "No associated statement allowed for 'omp barrier' directive");
2512 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
2513 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00002514 case OMPD_taskwait:
2515 assert(ClausesWithImplicit.empty() &&
2516 "No clauses are allowed for 'omp taskwait' directive");
2517 assert(AStmt == nullptr &&
2518 "No associated statement allowed for 'omp taskwait' directive");
2519 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
2520 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002521 case OMPD_taskgroup:
2522 assert(ClausesWithImplicit.empty() &&
2523 "No clauses are allowed for 'omp taskgroup' directive");
2524 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
2525 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00002526 case OMPD_flush:
2527 assert(AStmt == nullptr &&
2528 "No associated statement allowed for 'omp flush' directive");
2529 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
2530 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002531 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00002532 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
2533 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002534 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00002535 case OMPD_atomic:
2536 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
2537 EndLoc);
2538 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002539 case OMPD_teams:
2540 Res =
2541 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
2542 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002543 case OMPD_target:
2544 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
2545 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002546 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002547 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002548 case OMPD_cancellation_point:
2549 assert(ClausesWithImplicit.empty() &&
2550 "No clauses are allowed for 'omp cancellation point' directive");
2551 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
2552 "cancellation point' directive");
2553 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
2554 break;
Alexey Bataev80909872015-07-02 11:25:17 +00002555 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00002556 assert(AStmt == nullptr &&
2557 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00002558 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
2559 CancelRegion);
2560 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00002561 break;
Michael Wong65f367f2015-07-21 13:44:28 +00002562 case OMPD_target_data:
2563 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
2564 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002565 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00002566 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00002567 case OMPD_taskloop:
2568 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
2569 EndLoc, VarsWithInheritedDSA);
2570 AllowedNameModifiers.push_back(OMPD_taskloop);
2571 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002572 case OMPD_taskloop_simd:
2573 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2574 EndLoc, VarsWithInheritedDSA);
2575 AllowedNameModifiers.push_back(OMPD_taskloop);
2576 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002577 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002578 llvm_unreachable("OpenMP Directive is not allowed");
2579 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002580 llvm_unreachable("Unknown OpenMP directive");
2581 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002582
Alexey Bataev4acb8592014-07-07 13:01:15 +00002583 for (auto P : VarsWithInheritedDSA) {
2584 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
2585 << P.first << P.second->getSourceRange();
2586 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002587 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
2588
2589 if (!AllowedNameModifiers.empty())
2590 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
2591 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002592
Alexey Bataeved09d242014-05-28 05:53:51 +00002593 if (ErrorFound)
2594 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002595 return Res;
2596}
2597
2598StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
2599 Stmt *AStmt,
2600 SourceLocation StartLoc,
2601 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002602 if (!AStmt)
2603 return StmtError();
2604
Alexey Bataev9959db52014-05-06 10:08:46 +00002605 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
2606 // 1.2.2 OpenMP Language Terminology
2607 // Structured block - An executable statement with a single entry at the
2608 // top and a single exit at the bottom.
2609 // The point of exit cannot be a branch out of the structured block.
2610 // longjmp() and throw() must not violate the entry/exit criteria.
2611 CS->getCapturedDecl()->setNothrow();
2612
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002613 getCurFunction()->setHasBranchProtectedScope();
2614
Alexey Bataev25e5b442015-09-15 12:52:43 +00002615 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
2616 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002617}
2618
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002619namespace {
2620/// \brief Helper class for checking canonical form of the OpenMP loops and
2621/// extracting iteration space of each loop in the loop nest, that will be used
2622/// for IR generation.
2623class OpenMPIterationSpaceChecker {
2624 /// \brief Reference to Sema.
2625 Sema &SemaRef;
2626 /// \brief A location for diagnostics (when there is no some better location).
2627 SourceLocation DefaultLoc;
2628 /// \brief A location for diagnostics (when increment is not compatible).
2629 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002630 /// \brief A source location for referring to loop init later.
2631 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002632 /// \brief A source location for referring to condition later.
2633 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002634 /// \brief A source location for referring to increment later.
2635 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002636 /// \brief Loop variable.
2637 VarDecl *Var;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002638 /// \brief Reference to loop variable.
2639 DeclRefExpr *VarRef;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002640 /// \brief Lower bound (initializer for the var).
2641 Expr *LB;
2642 /// \brief Upper bound.
2643 Expr *UB;
2644 /// \brief Loop step (increment).
2645 Expr *Step;
2646 /// \brief This flag is true when condition is one of:
2647 /// Var < UB
2648 /// Var <= UB
2649 /// UB > Var
2650 /// UB >= Var
2651 bool TestIsLessOp;
2652 /// \brief This flag is true when condition is strict ( < or > ).
2653 bool TestIsStrictOp;
2654 /// \brief This flag is true when step is subtracted on each iteration.
2655 bool SubtractStep;
2656
2657public:
2658 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
2659 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
Alexander Musmana5f070a2014-10-01 06:03:56 +00002660 InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()),
2661 IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002662 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
2663 TestIsStrictOp(false), SubtractStep(false) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002664 /// \brief Check init-expr for canonical loop form and save loop counter
2665 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00002666 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002667 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
2668 /// for less/greater and for strict/non-strict comparison.
2669 bool CheckCond(Expr *S);
2670 /// \brief Check incr-expr for canonical loop form and return true if it
2671 /// does not conform, otherwise save loop step (#Step).
2672 bool CheckInc(Expr *S);
2673 /// \brief Return the loop counter variable.
2674 VarDecl *GetLoopVar() const { return Var; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002675 /// \brief Return the reference expression to loop counter variable.
2676 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002677 /// \brief Source range of the loop init.
2678 SourceRange GetInitSrcRange() const { return InitSrcRange; }
2679 /// \brief Source range of the loop condition.
2680 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
2681 /// \brief Source range of the loop increment.
2682 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
2683 /// \brief True if the step should be subtracted.
2684 bool ShouldSubtractStep() const { return SubtractStep; }
2685 /// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00002686 Expr *BuildNumIterations(Scope *S, const bool LimitedType) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00002687 /// \brief Build the precondition expression for the loops.
2688 Expr *BuildPreCond(Scope *S, Expr *Cond) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002689 /// \brief Build reference expression to the counter be used for codegen.
2690 Expr *BuildCounterVar() const;
Alexey Bataeva8899172015-08-06 12:30:57 +00002691 /// \brief Build reference expression to the private counter be used for
2692 /// codegen.
2693 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002694 /// \brief Build initization of the counter be used for codegen.
2695 Expr *BuildCounterInit() const;
2696 /// \brief Build step of the counter be used for codegen.
2697 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002698 /// \brief Return true if any expression is dependent.
2699 bool Dependent() const;
2700
2701private:
2702 /// \brief Check the right-hand side of an assignment in the increment
2703 /// expression.
2704 bool CheckIncRHS(Expr *RHS);
2705 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002706 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002707 /// \brief Helper to set upper bound.
Craig Toppere335f252015-10-04 04:53:55 +00002708 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00002709 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002710 /// \brief Helper to set loop increment.
2711 bool SetStep(Expr *NewStep, bool Subtract);
2712};
2713
2714bool OpenMPIterationSpaceChecker::Dependent() const {
2715 if (!Var) {
2716 assert(!LB && !UB && !Step);
2717 return false;
2718 }
2719 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
2720 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
2721}
2722
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002723template <typename T>
2724static T *getExprAsWritten(T *E) {
2725 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
2726 E = ExprTemp->getSubExpr();
2727
2728 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
2729 E = MTE->GetTemporaryExpr();
2730
2731 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
2732 E = Binder->getSubExpr();
2733
2734 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
2735 E = ICE->getSubExprAsWritten();
2736 return E->IgnoreParens();
2737}
2738
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002739bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
2740 DeclRefExpr *NewVarRefExpr,
2741 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002742 // State consistency checking to ensure correct usage.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002743 assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
2744 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002745 if (!NewVar || !NewLB)
2746 return true;
2747 Var = NewVar;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002748 VarRef = NewVarRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002749 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
2750 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00002751 if ((Ctor->isCopyOrMoveConstructor() ||
2752 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
2753 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002754 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002755 LB = NewLB;
2756 return false;
2757}
2758
2759bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00002760 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002761 // State consistency checking to ensure correct usage.
2762 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
2763 !TestIsLessOp && !TestIsStrictOp);
2764 if (!NewUB)
2765 return true;
2766 UB = NewUB;
2767 TestIsLessOp = LessOp;
2768 TestIsStrictOp = StrictOp;
2769 ConditionSrcRange = SR;
2770 ConditionLoc = SL;
2771 return false;
2772}
2773
2774bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
2775 // State consistency checking to ensure correct usage.
2776 assert(Var != nullptr && LB != nullptr && Step == nullptr);
2777 if (!NewStep)
2778 return true;
2779 if (!NewStep->isValueDependent()) {
2780 // Check that the step is integer expression.
2781 SourceLocation StepLoc = NewStep->getLocStart();
2782 ExprResult Val =
2783 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
2784 if (Val.isInvalid())
2785 return true;
2786 NewStep = Val.get();
2787
2788 // OpenMP [2.6, Canonical Loop Form, Restrictions]
2789 // If test-expr is of form var relational-op b and relational-op is < or
2790 // <= then incr-expr must cause var to increase on each iteration of the
2791 // loop. If test-expr is of form var relational-op b and relational-op is
2792 // > or >= then incr-expr must cause var to decrease on each iteration of
2793 // the loop.
2794 // If test-expr is of form b relational-op var and relational-op is < or
2795 // <= then incr-expr must cause var to decrease on each iteration of the
2796 // loop. If test-expr is of form b relational-op var and relational-op is
2797 // > or >= then incr-expr must cause var to increase on each iteration of
2798 // the loop.
2799 llvm::APSInt Result;
2800 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
2801 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
2802 bool IsConstNeg =
2803 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00002804 bool IsConstPos =
2805 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002806 bool IsConstZero = IsConstant && !Result.getBoolValue();
2807 if (UB && (IsConstZero ||
2808 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00002809 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002810 SemaRef.Diag(NewStep->getExprLoc(),
2811 diag::err_omp_loop_incr_not_compatible)
2812 << Var << TestIsLessOp << NewStep->getSourceRange();
2813 SemaRef.Diag(ConditionLoc,
2814 diag::note_omp_loop_cond_requres_compatible_incr)
2815 << TestIsLessOp << ConditionSrcRange;
2816 return true;
2817 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002818 if (TestIsLessOp == Subtract) {
2819 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
2820 NewStep).get();
2821 Subtract = !Subtract;
2822 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002823 }
2824
2825 Step = NewStep;
2826 SubtractStep = Subtract;
2827 return false;
2828}
2829
Alexey Bataev9c821032015-04-30 04:23:23 +00002830bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002831 // Check init-expr for canonical loop form and save loop counter
2832 // variable - #Var and its initialization value - #LB.
2833 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
2834 // var = lb
2835 // integer-type var = lb
2836 // random-access-iterator-type var = lb
2837 // pointer-type var = lb
2838 //
2839 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00002840 if (EmitDiags) {
2841 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
2842 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002843 return true;
2844 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002845 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002846 if (Expr *E = dyn_cast<Expr>(S))
2847 S = E->IgnoreParens();
2848 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2849 if (BO->getOpcode() == BO_Assign)
2850 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002851 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002852 BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002853 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
2854 if (DS->isSingleDecl()) {
2855 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00002856 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002857 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00002858 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002859 SemaRef.Diag(S->getLocStart(),
2860 diag::ext_omp_loop_not_canonical_init)
2861 << S->getSourceRange();
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002862 return SetVarAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002863 }
2864 }
2865 }
2866 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
2867 if (CE->getOperator() == OO_Equal)
2868 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002869 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
2870 CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002871
Alexey Bataev9c821032015-04-30 04:23:23 +00002872 if (EmitDiags) {
2873 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
2874 << S->getSourceRange();
2875 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002876 return true;
2877}
2878
Alexey Bataev23b69422014-06-18 07:08:49 +00002879/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002880/// variable (which may be the loop variable) if possible.
2881static const VarDecl *GetInitVarDecl(const Expr *E) {
2882 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00002883 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002884 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002885 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
2886 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00002887 if ((Ctor->isCopyOrMoveConstructor() ||
2888 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
2889 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002890 E = CE->getArg(0)->IgnoreParenImpCasts();
2891 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
2892 if (!DRE)
2893 return nullptr;
2894 return dyn_cast<VarDecl>(DRE->getDecl());
2895}
2896
2897bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
2898 // Check test-expr for canonical form, save upper-bound UB, flags for
2899 // less/greater and for strict/non-strict comparison.
2900 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2901 // var relational-op b
2902 // b relational-op var
2903 //
2904 if (!S) {
2905 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
2906 return true;
2907 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002908 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002909 SourceLocation CondLoc = S->getLocStart();
2910 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2911 if (BO->isRelationalOp()) {
2912 if (GetInitVarDecl(BO->getLHS()) == Var)
2913 return SetUB(BO->getRHS(),
2914 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
2915 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2916 BO->getSourceRange(), BO->getOperatorLoc());
2917 if (GetInitVarDecl(BO->getRHS()) == Var)
2918 return SetUB(BO->getLHS(),
2919 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
2920 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2921 BO->getSourceRange(), BO->getOperatorLoc());
2922 }
2923 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2924 if (CE->getNumArgs() == 2) {
2925 auto Op = CE->getOperator();
2926 switch (Op) {
2927 case OO_Greater:
2928 case OO_GreaterEqual:
2929 case OO_Less:
2930 case OO_LessEqual:
2931 if (GetInitVarDecl(CE->getArg(0)) == Var)
2932 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
2933 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2934 CE->getOperatorLoc());
2935 if (GetInitVarDecl(CE->getArg(1)) == Var)
2936 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
2937 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2938 CE->getOperatorLoc());
2939 break;
2940 default:
2941 break;
2942 }
2943 }
2944 }
2945 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
2946 << S->getSourceRange() << Var;
2947 return true;
2948}
2949
2950bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
2951 // RHS of canonical loop form increment can be:
2952 // var + incr
2953 // incr + var
2954 // var - incr
2955 //
2956 RHS = RHS->IgnoreParenImpCasts();
2957 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
2958 if (BO->isAdditiveOp()) {
2959 bool IsAdd = BO->getOpcode() == BO_Add;
2960 if (GetInitVarDecl(BO->getLHS()) == Var)
2961 return SetStep(BO->getRHS(), !IsAdd);
2962 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
2963 return SetStep(BO->getLHS(), false);
2964 }
2965 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
2966 bool IsAdd = CE->getOperator() == OO_Plus;
2967 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
2968 if (GetInitVarDecl(CE->getArg(0)) == Var)
2969 return SetStep(CE->getArg(1), !IsAdd);
2970 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
2971 return SetStep(CE->getArg(0), false);
2972 }
2973 }
2974 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2975 << RHS->getSourceRange() << Var;
2976 return true;
2977}
2978
2979bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
2980 // Check incr-expr for canonical loop form and return true if it
2981 // does not conform.
2982 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2983 // ++var
2984 // var++
2985 // --var
2986 // var--
2987 // var += incr
2988 // var -= incr
2989 // var = var + incr
2990 // var = incr + var
2991 // var = var - incr
2992 //
2993 if (!S) {
2994 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
2995 return true;
2996 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002997 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002998 S = S->IgnoreParens();
2999 if (auto UO = dyn_cast<UnaryOperator>(S)) {
3000 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
3001 return SetStep(
3002 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
3003 (UO->isDecrementOp() ? -1 : 1)).get(),
3004 false);
3005 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
3006 switch (BO->getOpcode()) {
3007 case BO_AddAssign:
3008 case BO_SubAssign:
3009 if (GetInitVarDecl(BO->getLHS()) == Var)
3010 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
3011 break;
3012 case BO_Assign:
3013 if (GetInitVarDecl(BO->getLHS()) == Var)
3014 return CheckIncRHS(BO->getRHS());
3015 break;
3016 default:
3017 break;
3018 }
3019 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3020 switch (CE->getOperator()) {
3021 case OO_PlusPlus:
3022 case OO_MinusMinus:
3023 if (GetInitVarDecl(CE->getArg(0)) == Var)
3024 return SetStep(
3025 SemaRef.ActOnIntegerConstant(
3026 CE->getLocStart(),
3027 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
3028 false);
3029 break;
3030 case OO_PlusEqual:
3031 case OO_MinusEqual:
3032 if (GetInitVarDecl(CE->getArg(0)) == Var)
3033 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
3034 break;
3035 case OO_Equal:
3036 if (GetInitVarDecl(CE->getArg(0)) == Var)
3037 return CheckIncRHS(CE->getArg(1));
3038 break;
3039 default:
3040 break;
3041 }
3042 }
3043 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3044 << S->getSourceRange() << Var;
3045 return true;
3046}
Alexander Musmana5f070a2014-10-01 06:03:56 +00003047
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003048namespace {
3049// Transform variables declared in GNU statement expressions to new ones to
3050// avoid crash on codegen.
3051class TransformToNewDefs : public TreeTransform<TransformToNewDefs> {
3052 typedef TreeTransform<TransformToNewDefs> BaseTransform;
3053
3054public:
3055 TransformToNewDefs(Sema &SemaRef) : BaseTransform(SemaRef) {}
3056
3057 Decl *TransformDefinition(SourceLocation Loc, Decl *D) {
3058 if (auto *VD = cast<VarDecl>(D))
3059 if (!isa<ParmVarDecl>(D) && !isa<VarTemplateSpecializationDecl>(D) &&
3060 !isa<ImplicitParamDecl>(D)) {
3061 auto *NewVD = VarDecl::Create(
3062 SemaRef.Context, VD->getDeclContext(), VD->getLocStart(),
3063 VD->getLocation(), VD->getIdentifier(), VD->getType(),
3064 VD->getTypeSourceInfo(), VD->getStorageClass());
3065 NewVD->setTSCSpec(VD->getTSCSpec());
3066 NewVD->setInit(VD->getInit());
3067 NewVD->setInitStyle(VD->getInitStyle());
3068 NewVD->setExceptionVariable(VD->isExceptionVariable());
3069 NewVD->setNRVOVariable(VD->isNRVOVariable());
3070 NewVD->setCXXForRangeDecl(VD->isInExternCXXContext());
3071 NewVD->setConstexpr(VD->isConstexpr());
3072 NewVD->setInitCapture(VD->isInitCapture());
3073 NewVD->setPreviousDeclInSameBlockScope(
3074 VD->isPreviousDeclInSameBlockScope());
3075 VD->getDeclContext()->addHiddenDecl(NewVD);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003076 if (VD->hasAttrs())
3077 NewVD->setAttrs(VD->getAttrs());
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003078 transformedLocalDecl(VD, NewVD);
3079 return NewVD;
3080 }
3081 return BaseTransform::TransformDefinition(Loc, D);
3082 }
3083
3084 ExprResult TransformDeclRefExpr(DeclRefExpr *E) {
3085 if (auto *NewD = TransformDecl(E->getExprLoc(), E->getDecl()))
3086 if (E->getDecl() != NewD) {
3087 NewD->setReferenced();
3088 NewD->markUsed(SemaRef.Context);
3089 return DeclRefExpr::Create(
3090 SemaRef.Context, E->getQualifierLoc(), E->getTemplateKeywordLoc(),
3091 cast<ValueDecl>(NewD), E->refersToEnclosingVariableOrCapture(),
3092 E->getNameInfo(), E->getType(), E->getValueKind());
3093 }
3094 return BaseTransform::TransformDeclRefExpr(E);
3095 }
3096};
3097}
3098
Alexander Musmana5f070a2014-10-01 06:03:56 +00003099/// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00003100Expr *
3101OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S,
3102 const bool LimitedType) const {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003103 TransformToNewDefs Transform(SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003104 ExprResult Diff;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003105 auto VarType = Var->getType().getNonReferenceType();
3106 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003107 SemaRef.getLangOpts().CPlusPlus) {
3108 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003109 auto *UBExpr = TestIsLessOp ? UB : LB;
3110 auto *LBExpr = TestIsLessOp ? LB : UB;
3111 Expr *Upper = Transform.TransformExpr(UBExpr).get();
3112 Expr *Lower = Transform.TransformExpr(LBExpr).get();
3113 if (!Upper || !Lower)
3114 return nullptr;
3115 Upper = SemaRef.PerformImplicitConversion(Upper, UBExpr->getType(),
3116 Sema::AA_Converting,
3117 /*AllowExplicit=*/true)
3118 .get();
3119 Lower = SemaRef.PerformImplicitConversion(Lower, LBExpr->getType(),
3120 Sema::AA_Converting,
3121 /*AllowExplicit=*/true)
3122 .get();
3123 if (!Upper || !Lower)
3124 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003125
3126 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
3127
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003128 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00003129 // BuildBinOp already emitted error, this one is to point user to upper
3130 // and lower bound, and to tell what is passed to 'operator-'.
3131 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
3132 << Upper->getSourceRange() << Lower->getSourceRange();
3133 return nullptr;
3134 }
3135 }
3136
3137 if (!Diff.isUsable())
3138 return nullptr;
3139
3140 // Upper - Lower [- 1]
3141 if (TestIsStrictOp)
3142 Diff = SemaRef.BuildBinOp(
3143 S, DefaultLoc, BO_Sub, Diff.get(),
3144 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3145 if (!Diff.isUsable())
3146 return nullptr;
3147
3148 // Upper - Lower [- 1] + Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003149 auto NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
3150 if (NewStep.isInvalid())
3151 return nullptr;
3152 NewStep = SemaRef.PerformImplicitConversion(
3153 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
3154 /*AllowExplicit=*/true);
3155 if (NewStep.isInvalid())
3156 return nullptr;
3157 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003158 if (!Diff.isUsable())
3159 return nullptr;
3160
3161 // Parentheses (for dumping/debugging purposes only).
3162 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
3163 if (!Diff.isUsable())
3164 return nullptr;
3165
3166 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003167 NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
3168 if (NewStep.isInvalid())
3169 return nullptr;
3170 NewStep = SemaRef.PerformImplicitConversion(
3171 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
3172 /*AllowExplicit=*/true);
3173 if (NewStep.isInvalid())
3174 return nullptr;
3175 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003176 if (!Diff.isUsable())
3177 return nullptr;
3178
Alexander Musman174b3ca2014-10-06 11:16:29 +00003179 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003180 QualType Type = Diff.get()->getType();
3181 auto &C = SemaRef.Context;
3182 bool UseVarType = VarType->hasIntegerRepresentation() &&
3183 C.getTypeSize(Type) > C.getTypeSize(VarType);
3184 if (!Type->isIntegerType() || UseVarType) {
3185 unsigned NewSize =
3186 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
3187 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
3188 : Type->hasSignedIntegerRepresentation();
3189 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
3190 Diff = SemaRef.PerformImplicitConversion(
3191 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
3192 if (!Diff.isUsable())
3193 return nullptr;
3194 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00003195 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00003196 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
3197 if (NewSize != C.getTypeSize(Type)) {
3198 if (NewSize < C.getTypeSize(Type)) {
3199 assert(NewSize == 64 && "incorrect loop var size");
3200 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
3201 << InitSrcRange << ConditionSrcRange;
3202 }
3203 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003204 NewSize, Type->hasSignedIntegerRepresentation() ||
3205 C.getTypeSize(Type) < NewSize);
Alexander Musman174b3ca2014-10-06 11:16:29 +00003206 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
3207 Sema::AA_Converting, true);
3208 if (!Diff.isUsable())
3209 return nullptr;
3210 }
3211 }
3212
Alexander Musmana5f070a2014-10-01 06:03:56 +00003213 return Diff.get();
3214}
3215
Alexey Bataev62dbb972015-04-22 11:59:37 +00003216Expr *OpenMPIterationSpaceChecker::BuildPreCond(Scope *S, Expr *Cond) const {
3217 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
3218 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
3219 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003220 TransformToNewDefs Transform(SemaRef);
3221
3222 auto NewLB = Transform.TransformExpr(LB);
3223 auto NewUB = Transform.TransformExpr(UB);
3224 if (NewLB.isInvalid() || NewUB.isInvalid())
3225 return Cond;
3226 NewLB = SemaRef.PerformImplicitConversion(NewLB.get(), LB->getType(),
3227 Sema::AA_Converting,
3228 /*AllowExplicit=*/true);
3229 NewUB = SemaRef.PerformImplicitConversion(NewUB.get(), UB->getType(),
3230 Sema::AA_Converting,
3231 /*AllowExplicit=*/true);
3232 if (NewLB.isInvalid() || NewUB.isInvalid())
3233 return Cond;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003234 auto CondExpr = SemaRef.BuildBinOp(
3235 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
3236 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003237 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003238 if (CondExpr.isUsable()) {
3239 CondExpr = SemaRef.PerformImplicitConversion(
3240 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
3241 /*AllowExplicit=*/true);
3242 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00003243 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
3244 // Otherwise use original loop conditon and evaluate it in runtime.
3245 return CondExpr.isUsable() ? CondExpr.get() : Cond;
3246}
3247
Alexander Musmana5f070a2014-10-01 06:03:56 +00003248/// \brief Build reference expression to the counter be used for codegen.
3249Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const {
Alexey Bataeva8899172015-08-06 12:30:57 +00003250 return buildDeclRefExpr(SemaRef, Var, Var->getType().getNonReferenceType(),
3251 DefaultLoc);
3252}
3253
3254Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
3255 if (Var && !Var->isInvalidDecl()) {
3256 auto Type = Var->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003257 auto *PrivateVar =
3258 buildVarDecl(SemaRef, DefaultLoc, Type, Var->getName(),
3259 Var->hasAttrs() ? &Var->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00003260 if (PrivateVar->isInvalidDecl())
3261 return nullptr;
3262 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
3263 }
3264 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003265}
3266
3267/// \brief Build initization of the counter be used for codegen.
3268Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
3269
3270/// \brief Build step of the counter be used for codegen.
3271Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
3272
3273/// \brief Iteration space of a single for loop.
3274struct LoopIterationSpace {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003275 /// \brief Condition of the loop.
3276 Expr *PreCond;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003277 /// \brief This expression calculates the number of iterations in the loop.
3278 /// It is always possible to calculate it before starting the loop.
3279 Expr *NumIterations;
3280 /// \brief The loop counter variable.
3281 Expr *CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00003282 /// \brief Private loop counter variable.
3283 Expr *PrivateCounterVar;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003284 /// \brief This is initializer for the initial value of #CounterVar.
3285 Expr *CounterInit;
3286 /// \brief This is step for the #CounterVar used to generate its update:
3287 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
3288 Expr *CounterStep;
3289 /// \brief Should step be subtracted?
3290 bool Subtract;
3291 /// \brief Source range of the loop init.
3292 SourceRange InitSrcRange;
3293 /// \brief Source range of the loop condition.
3294 SourceRange CondSrcRange;
3295 /// \brief Source range of the loop increment.
3296 SourceRange IncSrcRange;
3297};
3298
Alexey Bataev23b69422014-06-18 07:08:49 +00003299} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003300
Alexey Bataev9c821032015-04-30 04:23:23 +00003301void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
3302 assert(getLangOpts().OpenMP && "OpenMP is not active.");
3303 assert(Init && "Expected loop in canonical form.");
3304 unsigned CollapseIteration = DSAStack->getCollapseNumber();
3305 if (CollapseIteration > 0 &&
3306 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
3307 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
3308 if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
3309 DSAStack->addLoopControlVariable(ISC.GetLoopVar());
3310 }
3311 DSAStack->setCollapseNumber(CollapseIteration - 1);
3312 }
3313}
3314
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003315/// \brief Called on a for stmt to check and extract its iteration space
3316/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00003317static bool CheckOpenMPIterationSpace(
3318 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
3319 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003320 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003321 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
3322 LoopIterationSpace &ResultIterSpace) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003323 // OpenMP [2.6, Canonical Loop Form]
3324 // for (init-expr; test-expr; incr-expr) structured-block
3325 auto For = dyn_cast_or_null<ForStmt>(S);
3326 if (!For) {
3327 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003328 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
3329 << getOpenMPDirectiveName(DKind) << NestedLoopCount
3330 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
3331 if (NestedLoopCount > 1) {
3332 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
3333 SemaRef.Diag(DSA.getConstructLoc(),
3334 diag::note_omp_collapse_ordered_expr)
3335 << 2 << CollapseLoopCountExpr->getSourceRange()
3336 << OrderedLoopCountExpr->getSourceRange();
3337 else if (CollapseLoopCountExpr)
3338 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
3339 diag::note_omp_collapse_ordered_expr)
3340 << 0 << CollapseLoopCountExpr->getSourceRange();
3341 else
3342 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
3343 diag::note_omp_collapse_ordered_expr)
3344 << 1 << OrderedLoopCountExpr->getSourceRange();
3345 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003346 return true;
3347 }
3348 assert(For->getBody());
3349
3350 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
3351
3352 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003353 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003354 if (ISC.CheckInit(Init)) {
3355 return true;
3356 }
3357
3358 bool HasErrors = false;
3359
3360 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003361 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003362
3363 // OpenMP [2.6, Canonical Loop Form]
3364 // Var is one of the following:
3365 // A variable of signed or unsigned integer type.
3366 // For C++, a variable of a random access iterator type.
3367 // For C, a variable of a pointer type.
Alexey Bataeva8899172015-08-06 12:30:57 +00003368 auto VarType = Var->getType().getNonReferenceType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003369 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
3370 !VarType->isPointerType() &&
3371 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
3372 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
3373 << SemaRef.getLangOpts().CPlusPlus;
3374 HasErrors = true;
3375 }
3376
Alexey Bataev4acb8592014-07-07 13:01:15 +00003377 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
3378 // Construct
3379 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3380 // parallel for construct is (are) private.
3381 // The loop iteration variable in the associated for-loop of a simd construct
3382 // with just one associated for-loop is linear with a constant-linear-step
3383 // that is the increment of the associated for-loop.
3384 // Exclude loop var from the list of variables with implicitly defined data
3385 // sharing attributes.
Benjamin Kramerad8e0792014-10-10 15:32:48 +00003386 VarsWithImplicitDSA.erase(Var);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003387
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003388 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
3389 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00003390 // The loop iteration variable in the associated for-loop of a simd construct
3391 // with just one associated for-loop may be listed in a linear clause with a
3392 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00003393 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3394 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003395 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003396 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
3397 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
3398 // declared in the loop and it is predetermined as a private.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003399 auto PredeterminedCKind =
3400 isOpenMPSimdDirective(DKind)
3401 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
3402 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003403 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataev0c024df2015-05-12 09:02:07 +00003404 DVar.CKind != OMPC_threadprivate && DVar.CKind != PredeterminedCKind) ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00003405 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop) &&
3406 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
3407 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate &&
3408 DVar.CKind != OMPC_threadprivate)) &&
Alexey Bataev0c024df2015-05-12 09:02:07 +00003409 ((DVar.CKind != OMPC_private && DVar.CKind != OMPC_threadprivate) ||
3410 DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003411 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00003412 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
3413 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataevf2453a02015-05-06 07:25:08 +00003414 if (DVar.RefExpr == nullptr)
3415 DVar.CKind = PredeterminedCKind;
3416 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, /*IsLoopIterVar=*/true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003417 HasErrors = true;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003418 } else if (LoopVarRefExpr != nullptr) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00003419 // Make the loop iteration variable private (for worksharing constructs),
3420 // linear (for simd directives with the only one associated loop) or
Alexey Bataev10e775f2015-07-30 11:36:16 +00003421 // lastprivate (for simd directives with several collapsed or ordered
3422 // loops).
Alexey Bataev9aba41c2014-11-14 04:08:45 +00003423 if (DVar.CKind == OMPC_unknown)
3424 DVar = DSA.hasDSA(Var, isOpenMPPrivate, MatchesAlways(),
3425 /*FromParent=*/false);
Alexey Bataev9c821032015-04-30 04:23:23 +00003426 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003427 }
3428
Alexey Bataev7ff55242014-06-19 09:13:45 +00003429 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00003430
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003431 // Check test-expr.
3432 HasErrors |= ISC.CheckCond(For->getCond());
3433
3434 // Check incr-expr.
3435 HasErrors |= ISC.CheckInc(For->getInc());
3436
Alexander Musmana5f070a2014-10-01 06:03:56 +00003437 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003438 return HasErrors;
3439
Alexander Musmana5f070a2014-10-01 06:03:56 +00003440 // Build the loop's iteration space representation.
Alexey Bataev62dbb972015-04-22 11:59:37 +00003441 ResultIterSpace.PreCond = ISC.BuildPreCond(DSA.getCurScope(), For->getCond());
Alexander Musman174b3ca2014-10-06 11:16:29 +00003442 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003443 DSA.getCurScope(), (isOpenMPWorksharingDirective(DKind) ||
3444 isOpenMPTaskLoopDirective(DKind)));
Alexander Musmana5f070a2014-10-01 06:03:56 +00003445 ResultIterSpace.CounterVar = ISC.BuildCounterVar();
Alexey Bataeva8899172015-08-06 12:30:57 +00003446 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003447 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
3448 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
3449 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
3450 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
3451 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
3452 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
3453
Alexey Bataev62dbb972015-04-22 11:59:37 +00003454 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
3455 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003456 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00003457 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003458 ResultIterSpace.CounterInit == nullptr ||
3459 ResultIterSpace.CounterStep == nullptr);
3460
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003461 return HasErrors;
3462}
3463
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003464/// \brief Build 'VarRef = Start.
3465static ExprResult BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc,
3466 ExprResult VarRef, ExprResult Start) {
3467 TransformToNewDefs Transform(SemaRef);
3468 // Build 'VarRef = Start.
3469 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
3470 if (NewStart.isInvalid())
3471 return ExprError();
3472 NewStart = SemaRef.PerformImplicitConversion(
3473 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
3474 Sema::AA_Converting,
3475 /*AllowExplicit=*/true);
3476 if (NewStart.isInvalid())
3477 return ExprError();
3478 NewStart = SemaRef.PerformImplicitConversion(
3479 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
3480 /*AllowExplicit=*/true);
3481 if (!NewStart.isUsable())
3482 return ExprError();
3483
3484 auto Init =
3485 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
3486 return Init;
3487}
3488
Alexander Musmana5f070a2014-10-01 06:03:56 +00003489/// \brief Build 'VarRef = Start + Iter * Step'.
3490static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S,
3491 SourceLocation Loc, ExprResult VarRef,
3492 ExprResult Start, ExprResult Iter,
3493 ExprResult Step, bool Subtract) {
3494 // Add parentheses (for debugging purposes only).
3495 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
3496 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
3497 !Step.isUsable())
3498 return ExprError();
3499
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003500 TransformToNewDefs Transform(SemaRef);
3501 auto NewStep = Transform.TransformExpr(Step.get()->IgnoreImplicit());
3502 if (NewStep.isInvalid())
3503 return ExprError();
3504 NewStep = SemaRef.PerformImplicitConversion(
3505 NewStep.get(), Step.get()->IgnoreImplicit()->getType(),
3506 Sema::AA_Converting,
3507 /*AllowExplicit=*/true);
3508 if (NewStep.isInvalid())
3509 return ExprError();
3510 ExprResult Update =
3511 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003512 if (!Update.isUsable())
3513 return ExprError();
3514
3515 // Build 'VarRef = Start + Iter * Step'.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003516 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
3517 if (NewStart.isInvalid())
3518 return ExprError();
3519 NewStart = SemaRef.PerformImplicitConversion(
3520 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
3521 Sema::AA_Converting,
3522 /*AllowExplicit=*/true);
3523 if (NewStart.isInvalid())
3524 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003525 Update = SemaRef.BuildBinOp(S, Loc, (Subtract ? BO_Sub : BO_Add),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003526 NewStart.get(), Update.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003527 if (!Update.isUsable())
3528 return ExprError();
3529
3530 Update = SemaRef.PerformImplicitConversion(
3531 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
3532 if (!Update.isUsable())
3533 return ExprError();
3534
3535 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
3536 return Update;
3537}
3538
3539/// \brief Convert integer expression \a E to make it have at least \a Bits
3540/// bits.
3541static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
3542 Sema &SemaRef) {
3543 if (E == nullptr)
3544 return ExprError();
3545 auto &C = SemaRef.Context;
3546 QualType OldType = E->getType();
3547 unsigned HasBits = C.getTypeSize(OldType);
3548 if (HasBits >= Bits)
3549 return ExprResult(E);
3550 // OK to convert to signed, because new type has more bits than old.
3551 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
3552 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
3553 true);
3554}
3555
3556/// \brief Check if the given expression \a E is a constant integer that fits
3557/// into \a Bits bits.
3558static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
3559 if (E == nullptr)
3560 return false;
3561 llvm::APSInt Result;
3562 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
3563 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
3564 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003565}
3566
3567/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00003568/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
3569/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003570static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00003571CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
3572 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
3573 DSAStackTy &DSA,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003574 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00003575 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003576 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003577 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003578 // Found 'collapse' clause - calculate collapse number.
3579 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003580 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00003581 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00003582 }
3583 if (OrderedLoopCountExpr) {
3584 // Found 'ordered' clause - calculate collapse number.
3585 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00003586 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
3587 if (Result.getLimitedValue() < NestedLoopCount) {
3588 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
3589 diag::err_omp_wrong_ordered_loop_count)
3590 << OrderedLoopCountExpr->getSourceRange();
3591 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
3592 diag::note_collapse_loop_count)
3593 << CollapseLoopCountExpr->getSourceRange();
3594 }
3595 NestedLoopCount = Result.getLimitedValue();
3596 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003597 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003598 // This is helper routine for loop directives (e.g., 'for', 'simd',
3599 // 'for simd', etc.).
Alexander Musmana5f070a2014-10-01 06:03:56 +00003600 SmallVector<LoopIterationSpace, 4> IterSpaces;
3601 IterSpaces.resize(NestedLoopCount);
3602 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003603 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003604 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003605 NestedLoopCount, CollapseLoopCountExpr,
3606 OrderedLoopCountExpr, VarsWithImplicitDSA,
3607 IterSpaces[Cnt]))
Alexey Bataevabfc0692014-06-25 06:52:00 +00003608 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003609 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003610 // OpenMP [2.8.1, simd construct, Restrictions]
3611 // All loops associated with the construct must be perfectly nested; that
3612 // is, there must be no intervening code nor any OpenMP directive between
3613 // any two loops.
3614 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003615 }
3616
Alexander Musmana5f070a2014-10-01 06:03:56 +00003617 Built.clear(/* size */ NestedLoopCount);
3618
3619 if (SemaRef.CurContext->isDependentContext())
3620 return NestedLoopCount;
3621
3622 // An example of what is generated for the following code:
3623 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00003624 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00003625 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003626 // for (k = 0; k < NK; ++k)
3627 // for (j = J0; j < NJ; j+=2) {
3628 // <loop body>
3629 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003630 //
3631 // We generate the code below.
3632 // Note: the loop body may be outlined in CodeGen.
3633 // Note: some counters may be C++ classes, operator- is used to find number of
3634 // iterations and operator+= to calculate counter value.
3635 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
3636 // or i64 is currently supported).
3637 //
3638 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
3639 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
3640 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
3641 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
3642 // // similar updates for vars in clauses (e.g. 'linear')
3643 // <loop body (using local i and j)>
3644 // }
3645 // i = NI; // assign final values of counters
3646 // j = NJ;
3647 //
3648
3649 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
3650 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00003651 // Precondition tests if there is at least one iteration (all conditions are
3652 // true).
3653 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003654 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003655 ExprResult LastIteration32 = WidenIterationCount(
3656 32 /* Bits */, SemaRef.PerformImplicitConversion(
3657 N0->IgnoreImpCasts(), N0->getType(),
3658 Sema::AA_Converting, /*AllowExplicit=*/true)
3659 .get(),
3660 SemaRef);
3661 ExprResult LastIteration64 = WidenIterationCount(
3662 64 /* Bits */, SemaRef.PerformImplicitConversion(
3663 N0->IgnoreImpCasts(), N0->getType(),
3664 Sema::AA_Converting, /*AllowExplicit=*/true)
3665 .get(),
3666 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003667
3668 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
3669 return NestedLoopCount;
3670
3671 auto &C = SemaRef.Context;
3672 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
3673
3674 Scope *CurScope = DSA.getCurScope();
3675 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003676 if (PreCond.isUsable()) {
3677 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
3678 PreCond.get(), IterSpaces[Cnt].PreCond);
3679 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003680 auto N = IterSpaces[Cnt].NumIterations;
3681 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
3682 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003683 LastIteration32 = SemaRef.BuildBinOp(
3684 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
3685 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
3686 Sema::AA_Converting,
3687 /*AllowExplicit=*/true)
3688 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003689 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003690 LastIteration64 = SemaRef.BuildBinOp(
3691 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
3692 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
3693 Sema::AA_Converting,
3694 /*AllowExplicit=*/true)
3695 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003696 }
3697
3698 // Choose either the 32-bit or 64-bit version.
3699 ExprResult LastIteration = LastIteration64;
3700 if (LastIteration32.isUsable() &&
3701 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
3702 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
3703 FitsInto(
3704 32 /* Bits */,
3705 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
3706 LastIteration64.get(), SemaRef)))
3707 LastIteration = LastIteration32;
3708
3709 if (!LastIteration.isUsable())
3710 return 0;
3711
3712 // Save the number of iterations.
3713 ExprResult NumIterations = LastIteration;
3714 {
3715 LastIteration = SemaRef.BuildBinOp(
3716 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
3717 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3718 if (!LastIteration.isUsable())
3719 return 0;
3720 }
3721
3722 // Calculate the last iteration number beforehand instead of doing this on
3723 // each iteration. Do not do this if the number of iterations may be kfold-ed.
3724 llvm::APSInt Result;
3725 bool IsConstant =
3726 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
3727 ExprResult CalcLastIteration;
3728 if (!IsConstant) {
3729 SourceLocation SaveLoc;
3730 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00003731 buildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00003732 ".omp.last.iteration");
Alexey Bataev39f915b82015-05-08 10:41:21 +00003733 ExprResult SaveRef = buildDeclRefExpr(
3734 SemaRef, SaveVar, LastIteration.get()->getType(), SaveLoc);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003735 CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign,
3736 SaveRef.get(), LastIteration.get());
3737 LastIteration = SaveRef;
3738
3739 // Prepare SaveRef + 1.
3740 NumIterations = SemaRef.BuildBinOp(
3741 CurScope, SaveLoc, BO_Add, SaveRef.get(),
3742 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3743 if (!NumIterations.isUsable())
3744 return 0;
3745 }
3746
3747 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
3748
Alexander Musmanc6388682014-12-15 07:07:06 +00003749 QualType VType = LastIteration.get()->getType();
3750 // Build variables passed into runtime, nesessary for worksharing directives.
3751 ExprResult LB, UB, IL, ST, EUB;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003752 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00003753 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00003754 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
3755 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003756 SemaRef.AddInitializerToDecl(
3757 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
3758 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3759
3760 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00003761 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
3762 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003763 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
3764 /*DirectInit*/ false,
3765 /*TypeMayContainAuto*/ false);
3766
3767 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
3768 // This will be used to implement clause 'lastprivate'.
3769 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00003770 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
3771 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003772 SemaRef.AddInitializerToDecl(
3773 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
3774 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3775
3776 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev39f915b82015-05-08 10:41:21 +00003777 VarDecl *STDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.stride");
3778 ST = buildDeclRefExpr(SemaRef, STDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003779 SemaRef.AddInitializerToDecl(
3780 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
3781 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3782
3783 // Build expression: UB = min(UB, LastIteration)
3784 // It is nesessary for CodeGen of directives with static scheduling.
3785 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
3786 UB.get(), LastIteration.get());
3787 ExprResult CondOp = SemaRef.ActOnConditionalOp(
3788 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
3789 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
3790 CondOp.get());
3791 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
3792 }
3793
3794 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003795 ExprResult IV;
3796 ExprResult Init;
3797 {
Alexey Bataev39f915b82015-05-08 10:41:21 +00003798 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.iv");
3799 IV = buildDeclRefExpr(SemaRef, IVDecl, VType, InitLoc);
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003800 Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
3801 isOpenMPTaskLoopDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00003802 ? LB.get()
3803 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
3804 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
3805 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003806 }
3807
Alexander Musmanc6388682014-12-15 07:07:06 +00003808 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003809 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00003810 ExprResult Cond =
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003811 (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00003812 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
3813 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
3814 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003815
3816 // Loop increment (IV = IV + 1)
3817 SourceLocation IncLoc;
3818 ExprResult Inc =
3819 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
3820 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
3821 if (!Inc.isUsable())
3822 return 0;
3823 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00003824 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
3825 if (!Inc.isUsable())
3826 return 0;
3827
3828 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
3829 // Used for directives with static scheduling.
3830 ExprResult NextLB, NextUB;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003831 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00003832 // LB + ST
3833 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
3834 if (!NextLB.isUsable())
3835 return 0;
3836 // LB = LB + ST
3837 NextLB =
3838 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
3839 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
3840 if (!NextLB.isUsable())
3841 return 0;
3842 // UB + ST
3843 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
3844 if (!NextUB.isUsable())
3845 return 0;
3846 // UB = UB + ST
3847 NextUB =
3848 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
3849 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
3850 if (!NextUB.isUsable())
3851 return 0;
3852 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003853
3854 // Build updates and final values of the loop counters.
3855 bool HasErrors = false;
3856 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003857 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003858 Built.Updates.resize(NestedLoopCount);
3859 Built.Finals.resize(NestedLoopCount);
3860 {
3861 ExprResult Div;
3862 // Go from inner nested loop to outer.
3863 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
3864 LoopIterationSpace &IS = IterSpaces[Cnt];
3865 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
3866 // Build: Iter = (IV / Div) % IS.NumIters
3867 // where Div is product of previous iterations' IS.NumIters.
3868 ExprResult Iter;
3869 if (Div.isUsable()) {
3870 Iter =
3871 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
3872 } else {
3873 Iter = IV;
3874 assert((Cnt == (int)NestedLoopCount - 1) &&
3875 "unusable div expected on first iteration only");
3876 }
3877
3878 if (Cnt != 0 && Iter.isUsable())
3879 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
3880 IS.NumIterations);
3881 if (!Iter.isUsable()) {
3882 HasErrors = true;
3883 break;
3884 }
3885
Alexey Bataev39f915b82015-05-08 10:41:21 +00003886 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
3887 auto *CounterVar = buildDeclRefExpr(
3888 SemaRef, cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()),
3889 IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
3890 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003891 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
3892 IS.CounterInit);
3893 if (!Init.isUsable()) {
3894 HasErrors = true;
3895 break;
3896 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003897 ExprResult Update =
Alexey Bataev39f915b82015-05-08 10:41:21 +00003898 BuildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003899 IS.CounterInit, Iter, IS.CounterStep, IS.Subtract);
3900 if (!Update.isUsable()) {
3901 HasErrors = true;
3902 break;
3903 }
3904
3905 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
3906 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00003907 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003908 IS.NumIterations, IS.CounterStep, IS.Subtract);
3909 if (!Final.isUsable()) {
3910 HasErrors = true;
3911 break;
3912 }
3913
3914 // Build Div for the next iteration: Div <- Div * IS.NumIters
3915 if (Cnt != 0) {
3916 if (Div.isUnset())
3917 Div = IS.NumIterations;
3918 else
3919 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
3920 IS.NumIterations);
3921
3922 // Add parentheses (for debugging purposes only).
3923 if (Div.isUsable())
3924 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
3925 if (!Div.isUsable()) {
3926 HasErrors = true;
3927 break;
3928 }
3929 }
3930 if (!Update.isUsable() || !Final.isUsable()) {
3931 HasErrors = true;
3932 break;
3933 }
3934 // Save results
3935 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00003936 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003937 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003938 Built.Updates[Cnt] = Update.get();
3939 Built.Finals[Cnt] = Final.get();
3940 }
3941 }
3942
3943 if (HasErrors)
3944 return 0;
3945
3946 // Save results
3947 Built.IterationVarRef = IV.get();
3948 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00003949 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003950 Built.CalcLastIteration =
3951 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003952 Built.PreCond = PreCond.get();
3953 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003954 Built.Init = Init.get();
3955 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00003956 Built.LB = LB.get();
3957 Built.UB = UB.get();
3958 Built.IL = IL.get();
3959 Built.ST = ST.get();
3960 Built.EUB = EUB.get();
3961 Built.NLB = NextLB.get();
3962 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003963
Alexey Bataevabfc0692014-06-25 06:52:00 +00003964 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003965}
3966
Alexey Bataev10e775f2015-07-30 11:36:16 +00003967static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00003968 auto CollapseClauses =
3969 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
3970 if (CollapseClauses.begin() != CollapseClauses.end())
3971 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003972 return nullptr;
3973}
3974
Alexey Bataev10e775f2015-07-30 11:36:16 +00003975static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00003976 auto OrderedClauses =
3977 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
3978 if (OrderedClauses.begin() != OrderedClauses.end())
3979 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00003980 return nullptr;
3981}
3982
Alexey Bataev66b15b52015-08-21 11:14:16 +00003983static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen,
3984 const Expr *Safelen) {
3985 llvm::APSInt SimdlenRes, SafelenRes;
3986 if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() ||
3987 Simdlen->isInstantiationDependent() ||
3988 Simdlen->containsUnexpandedParameterPack())
3989 return false;
3990 if (Safelen->isValueDependent() || Safelen->isTypeDependent() ||
3991 Safelen->isInstantiationDependent() ||
3992 Safelen->containsUnexpandedParameterPack())
3993 return false;
3994 Simdlen->EvaluateAsInt(SimdlenRes, S.Context);
3995 Safelen->EvaluateAsInt(SafelenRes, S.Context);
3996 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
3997 // If both simdlen and safelen clauses are specified, the value of the simdlen
3998 // parameter must be less than or equal to the value of the safelen parameter.
3999 if (SimdlenRes > SafelenRes) {
4000 S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values)
4001 << Simdlen->getSourceRange() << Safelen->getSourceRange();
4002 return true;
4003 }
4004 return false;
4005}
4006
Alexey Bataev4acb8592014-07-07 13:01:15 +00004007StmtResult Sema::ActOnOpenMPSimdDirective(
4008 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4009 SourceLocation EndLoc,
4010 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004011 if (!AStmt)
4012 return StmtError();
4013
4014 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004015 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004016 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4017 // define the nested loops number.
4018 unsigned NestedLoopCount = CheckOpenMPLoop(
4019 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4020 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004021 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004022 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004023
Alexander Musmana5f070a2014-10-01 06:03:56 +00004024 assert((CurContext->isDependentContext() || B.builtAll()) &&
4025 "omp simd loop exprs were not built");
4026
Alexander Musman3276a272015-03-21 10:12:56 +00004027 if (!CurContext->isDependentContext()) {
4028 // Finalize the clauses that need pre-built expressions for CodeGen.
4029 for (auto C : Clauses) {
4030 if (auto LC = dyn_cast<OMPLinearClause>(C))
4031 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4032 B.NumIterations, *this, CurScope))
4033 return StmtError();
4034 }
4035 }
4036
Alexey Bataev66b15b52015-08-21 11:14:16 +00004037 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4038 // If both simdlen and safelen clauses are specified, the value of the simdlen
4039 // parameter must be less than or equal to the value of the safelen parameter.
4040 OMPSafelenClause *Safelen = nullptr;
4041 OMPSimdlenClause *Simdlen = nullptr;
4042 for (auto *Clause : Clauses) {
4043 if (Clause->getClauseKind() == OMPC_safelen)
4044 Safelen = cast<OMPSafelenClause>(Clause);
4045 else if (Clause->getClauseKind() == OMPC_simdlen)
4046 Simdlen = cast<OMPSimdlenClause>(Clause);
4047 if (Safelen && Simdlen)
4048 break;
4049 }
4050 if (Simdlen && Safelen &&
4051 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4052 Safelen->getSafelen()))
4053 return StmtError();
4054
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004055 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004056 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4057 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004058}
4059
Alexey Bataev4acb8592014-07-07 13:01:15 +00004060StmtResult Sema::ActOnOpenMPForDirective(
4061 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4062 SourceLocation EndLoc,
4063 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004064 if (!AStmt)
4065 return StmtError();
4066
4067 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004068 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004069 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4070 // define the nested loops number.
4071 unsigned NestedLoopCount = CheckOpenMPLoop(
4072 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4073 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004074 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00004075 return StmtError();
4076
Alexander Musmana5f070a2014-10-01 06:03:56 +00004077 assert((CurContext->isDependentContext() || B.builtAll()) &&
4078 "omp for loop exprs were not built");
4079
Alexey Bataev54acd402015-08-04 11:18:19 +00004080 if (!CurContext->isDependentContext()) {
4081 // Finalize the clauses that need pre-built expressions for CodeGen.
4082 for (auto C : Clauses) {
4083 if (auto LC = dyn_cast<OMPLinearClause>(C))
4084 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4085 B.NumIterations, *this, CurScope))
4086 return StmtError();
4087 }
4088 }
4089
Alexey Bataevf29276e2014-06-18 04:14:57 +00004090 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004091 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004092 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00004093}
4094
Alexander Musmanf82886e2014-09-18 05:12:34 +00004095StmtResult Sema::ActOnOpenMPForSimdDirective(
4096 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4097 SourceLocation EndLoc,
4098 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004099 if (!AStmt)
4100 return StmtError();
4101
4102 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004103 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004104 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4105 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00004106 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004107 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
4108 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4109 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004110 if (NestedLoopCount == 0)
4111 return StmtError();
4112
Alexander Musmanc6388682014-12-15 07:07:06 +00004113 assert((CurContext->isDependentContext() || B.builtAll()) &&
4114 "omp for simd loop exprs were not built");
4115
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00004116 if (!CurContext->isDependentContext()) {
4117 // Finalize the clauses that need pre-built expressions for CodeGen.
4118 for (auto C : Clauses) {
4119 if (auto LC = dyn_cast<OMPLinearClause>(C))
4120 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4121 B.NumIterations, *this, CurScope))
4122 return StmtError();
4123 }
4124 }
4125
Alexey Bataev66b15b52015-08-21 11:14:16 +00004126 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4127 // If both simdlen and safelen clauses are specified, the value of the simdlen
4128 // parameter must be less than or equal to the value of the safelen parameter.
4129 OMPSafelenClause *Safelen = nullptr;
4130 OMPSimdlenClause *Simdlen = nullptr;
4131 for (auto *Clause : Clauses) {
4132 if (Clause->getClauseKind() == OMPC_safelen)
4133 Safelen = cast<OMPSafelenClause>(Clause);
4134 else if (Clause->getClauseKind() == OMPC_simdlen)
4135 Simdlen = cast<OMPSimdlenClause>(Clause);
4136 if (Safelen && Simdlen)
4137 break;
4138 }
4139 if (Simdlen && Safelen &&
4140 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4141 Safelen->getSafelen()))
4142 return StmtError();
4143
Alexander Musmanf82886e2014-09-18 05:12:34 +00004144 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004145 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4146 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004147}
4148
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004149StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
4150 Stmt *AStmt,
4151 SourceLocation StartLoc,
4152 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004153 if (!AStmt)
4154 return StmtError();
4155
4156 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004157 auto BaseStmt = AStmt;
4158 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4159 BaseStmt = CS->getCapturedStmt();
4160 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4161 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004162 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004163 return StmtError();
4164 // All associated statements must be '#pragma omp section' except for
4165 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004166 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004167 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4168 if (SectionStmt)
4169 Diag(SectionStmt->getLocStart(),
4170 diag::err_omp_sections_substmt_not_section);
4171 return StmtError();
4172 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004173 cast<OMPSectionDirective>(SectionStmt)
4174 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004175 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004176 } else {
4177 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
4178 return StmtError();
4179 }
4180
4181 getCurFunction()->setHasBranchProtectedScope();
4182
Alexey Bataev25e5b442015-09-15 12:52:43 +00004183 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4184 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004185}
4186
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004187StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
4188 SourceLocation StartLoc,
4189 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004190 if (!AStmt)
4191 return StmtError();
4192
4193 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004194
4195 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00004196 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004197
Alexey Bataev25e5b442015-09-15 12:52:43 +00004198 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
4199 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004200}
4201
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004202StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
4203 Stmt *AStmt,
4204 SourceLocation StartLoc,
4205 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004206 if (!AStmt)
4207 return StmtError();
4208
4209 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00004210
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004211 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00004212
Alexey Bataev3255bf32015-01-19 05:20:46 +00004213 // OpenMP [2.7.3, single Construct, Restrictions]
4214 // The copyprivate clause must not be used with the nowait clause.
4215 OMPClause *Nowait = nullptr;
4216 OMPClause *Copyprivate = nullptr;
4217 for (auto *Clause : Clauses) {
4218 if (Clause->getClauseKind() == OMPC_nowait)
4219 Nowait = Clause;
4220 else if (Clause->getClauseKind() == OMPC_copyprivate)
4221 Copyprivate = Clause;
4222 if (Copyprivate && Nowait) {
4223 Diag(Copyprivate->getLocStart(),
4224 diag::err_omp_single_copyprivate_with_nowait);
4225 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
4226 return StmtError();
4227 }
4228 }
4229
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004230 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4231}
4232
Alexander Musman80c22892014-07-17 08:54:58 +00004233StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
4234 SourceLocation StartLoc,
4235 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004236 if (!AStmt)
4237 return StmtError();
4238
4239 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00004240
4241 getCurFunction()->setHasBranchProtectedScope();
4242
4243 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
4244}
4245
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004246StmtResult
4247Sema::ActOnOpenMPCriticalDirective(const DeclarationNameInfo &DirName,
4248 Stmt *AStmt, SourceLocation StartLoc,
4249 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004250 if (!AStmt)
4251 return StmtError();
4252
4253 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004254
4255 getCurFunction()->setHasBranchProtectedScope();
4256
4257 return OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
4258 AStmt);
4259}
4260
Alexey Bataev4acb8592014-07-07 13:01:15 +00004261StmtResult Sema::ActOnOpenMPParallelForDirective(
4262 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4263 SourceLocation EndLoc,
4264 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004265 if (!AStmt)
4266 return StmtError();
4267
Alexey Bataev4acb8592014-07-07 13:01:15 +00004268 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4269 // 1.2.2 OpenMP Language Terminology
4270 // Structured block - An executable statement with a single entry at the
4271 // top and a single exit at the bottom.
4272 // The point of exit cannot be a branch out of the structured block.
4273 // longjmp() and throw() must not violate the entry/exit criteria.
4274 CS->getCapturedDecl()->setNothrow();
4275
Alexander Musmanc6388682014-12-15 07:07:06 +00004276 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004277 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4278 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004279 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004280 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
4281 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4282 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00004283 if (NestedLoopCount == 0)
4284 return StmtError();
4285
Alexander Musmana5f070a2014-10-01 06:03:56 +00004286 assert((CurContext->isDependentContext() || B.builtAll()) &&
4287 "omp parallel for loop exprs were not built");
4288
Alexey Bataev54acd402015-08-04 11:18:19 +00004289 if (!CurContext->isDependentContext()) {
4290 // Finalize the clauses that need pre-built expressions for CodeGen.
4291 for (auto C : Clauses) {
4292 if (auto LC = dyn_cast<OMPLinearClause>(C))
4293 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4294 B.NumIterations, *this, CurScope))
4295 return StmtError();
4296 }
4297 }
4298
Alexey Bataev4acb8592014-07-07 13:01:15 +00004299 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004300 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004301 NestedLoopCount, Clauses, AStmt, B,
4302 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00004303}
4304
Alexander Musmane4e893b2014-09-23 09:33:00 +00004305StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
4306 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4307 SourceLocation EndLoc,
4308 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004309 if (!AStmt)
4310 return StmtError();
4311
Alexander Musmane4e893b2014-09-23 09:33:00 +00004312 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4313 // 1.2.2 OpenMP Language Terminology
4314 // Structured block - An executable statement with a single entry at the
4315 // top and a single exit at the bottom.
4316 // The point of exit cannot be a branch out of the structured block.
4317 // longjmp() and throw() must not violate the entry/exit criteria.
4318 CS->getCapturedDecl()->setNothrow();
4319
Alexander Musmanc6388682014-12-15 07:07:06 +00004320 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004321 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4322 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00004323 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004324 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
4325 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4326 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004327 if (NestedLoopCount == 0)
4328 return StmtError();
4329
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00004330 if (!CurContext->isDependentContext()) {
4331 // Finalize the clauses that need pre-built expressions for CodeGen.
4332 for (auto C : Clauses) {
4333 if (auto LC = dyn_cast<OMPLinearClause>(C))
4334 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4335 B.NumIterations, *this, CurScope))
4336 return StmtError();
4337 }
4338 }
4339
Alexey Bataev66b15b52015-08-21 11:14:16 +00004340 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4341 // If both simdlen and safelen clauses are specified, the value of the simdlen
4342 // parameter must be less than or equal to the value of the safelen parameter.
4343 OMPSafelenClause *Safelen = nullptr;
4344 OMPSimdlenClause *Simdlen = nullptr;
4345 for (auto *Clause : Clauses) {
4346 if (Clause->getClauseKind() == OMPC_safelen)
4347 Safelen = cast<OMPSafelenClause>(Clause);
4348 else if (Clause->getClauseKind() == OMPC_simdlen)
4349 Simdlen = cast<OMPSimdlenClause>(Clause);
4350 if (Safelen && Simdlen)
4351 break;
4352 }
4353 if (Simdlen && Safelen &&
4354 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4355 Safelen->getSafelen()))
4356 return StmtError();
4357
Alexander Musmane4e893b2014-09-23 09:33:00 +00004358 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004359 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00004360 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004361}
4362
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004363StmtResult
4364Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
4365 Stmt *AStmt, SourceLocation StartLoc,
4366 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004367 if (!AStmt)
4368 return StmtError();
4369
4370 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004371 auto BaseStmt = AStmt;
4372 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4373 BaseStmt = CS->getCapturedStmt();
4374 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4375 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004376 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004377 return StmtError();
4378 // All associated statements must be '#pragma omp section' except for
4379 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004380 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004381 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4382 if (SectionStmt)
4383 Diag(SectionStmt->getLocStart(),
4384 diag::err_omp_parallel_sections_substmt_not_section);
4385 return StmtError();
4386 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004387 cast<OMPSectionDirective>(SectionStmt)
4388 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004389 }
4390 } else {
4391 Diag(AStmt->getLocStart(),
4392 diag::err_omp_parallel_sections_not_compound_stmt);
4393 return StmtError();
4394 }
4395
4396 getCurFunction()->setHasBranchProtectedScope();
4397
Alexey Bataev25e5b442015-09-15 12:52:43 +00004398 return OMPParallelSectionsDirective::Create(
4399 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004400}
4401
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004402StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
4403 Stmt *AStmt, SourceLocation StartLoc,
4404 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004405 if (!AStmt)
4406 return StmtError();
4407
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004408 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4409 // 1.2.2 OpenMP Language Terminology
4410 // Structured block - An executable statement with a single entry at the
4411 // top and a single exit at the bottom.
4412 // The point of exit cannot be a branch out of the structured block.
4413 // longjmp() and throw() must not violate the entry/exit criteria.
4414 CS->getCapturedDecl()->setNothrow();
4415
4416 getCurFunction()->setHasBranchProtectedScope();
4417
Alexey Bataev25e5b442015-09-15 12:52:43 +00004418 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4419 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004420}
4421
Alexey Bataev68446b72014-07-18 07:47:19 +00004422StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
4423 SourceLocation EndLoc) {
4424 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
4425}
4426
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00004427StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
4428 SourceLocation EndLoc) {
4429 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
4430}
4431
Alexey Bataev2df347a2014-07-18 10:17:07 +00004432StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
4433 SourceLocation EndLoc) {
4434 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
4435}
4436
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00004437StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
4438 SourceLocation StartLoc,
4439 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004440 if (!AStmt)
4441 return StmtError();
4442
4443 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00004444
4445 getCurFunction()->setHasBranchProtectedScope();
4446
4447 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
4448}
4449
Alexey Bataev6125da92014-07-21 11:26:11 +00004450StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
4451 SourceLocation StartLoc,
4452 SourceLocation EndLoc) {
4453 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
4454 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
4455}
4456
Alexey Bataev346265e2015-09-25 10:37:12 +00004457StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
4458 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004459 SourceLocation StartLoc,
4460 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004461 if (!AStmt)
4462 return StmtError();
4463
4464 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004465
4466 getCurFunction()->setHasBranchProtectedScope();
4467
Alexey Bataev346265e2015-09-25 10:37:12 +00004468 OMPThreadsClause *TC = nullptr;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004469 OMPSIMDClause *SC = nullptr;
Alexey Bataev346265e2015-09-25 10:37:12 +00004470 for (auto *C: Clauses) {
4471 if (C->getClauseKind() == OMPC_threads)
4472 TC = cast<OMPThreadsClause>(C);
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004473 else if (C->getClauseKind() == OMPC_simd)
4474 SC = cast<OMPSIMDClause>(C);
Alexey Bataev346265e2015-09-25 10:37:12 +00004475 }
4476
4477 // TODO: this must happen only if 'threads' clause specified or if no clauses
4478 // is specified.
4479 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
4480 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
4481 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param) << (TC != nullptr);
4482 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
4483 return StmtError();
4484 }
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004485 if (!SC && isOpenMPSimdDirective(DSAStack->getParentDirective())) {
4486 // OpenMP [2.8.1,simd Construct, Restrictions]
4487 // An ordered construct with the simd clause is the only OpenMP construct
4488 // that can appear in the simd region.
4489 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
4490 return StmtError();
4491 }
Alexey Bataev346265e2015-09-25 10:37:12 +00004492
4493 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004494}
4495
Alexey Bataev1d160b12015-03-13 12:27:31 +00004496namespace {
4497/// \brief Helper class for checking expression in 'omp atomic [update]'
4498/// construct.
4499class OpenMPAtomicUpdateChecker {
4500 /// \brief Error results for atomic update expressions.
4501 enum ExprAnalysisErrorCode {
4502 /// \brief A statement is not an expression statement.
4503 NotAnExpression,
4504 /// \brief Expression is not builtin binary or unary operation.
4505 NotABinaryOrUnaryExpression,
4506 /// \brief Unary operation is not post-/pre- increment/decrement operation.
4507 NotAnUnaryIncDecExpression,
4508 /// \brief An expression is not of scalar type.
4509 NotAScalarType,
4510 /// \brief A binary operation is not an assignment operation.
4511 NotAnAssignmentOp,
4512 /// \brief RHS part of the binary operation is not a binary expression.
4513 NotABinaryExpression,
4514 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
4515 /// expression.
4516 NotABinaryOperator,
4517 /// \brief RHS binary operation does not have reference to the updated LHS
4518 /// part.
4519 NotAnUpdateExpression,
4520 /// \brief No errors is found.
4521 NoError
4522 };
4523 /// \brief Reference to Sema.
4524 Sema &SemaRef;
4525 /// \brief A location for note diagnostics (when error is found).
4526 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004527 /// \brief 'x' lvalue part of the source atomic expression.
4528 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004529 /// \brief 'expr' rvalue part of the source atomic expression.
4530 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004531 /// \brief Helper expression of the form
4532 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
4533 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
4534 Expr *UpdateExpr;
4535 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
4536 /// important for non-associative operations.
4537 bool IsXLHSInRHSPart;
4538 BinaryOperatorKind Op;
4539 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004540 /// \brief true if the source expression is a postfix unary operation, false
4541 /// if it is a prefix unary operation.
4542 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004543
4544public:
4545 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00004546 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00004547 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00004548 /// \brief Check specified statement that it is suitable for 'atomic update'
4549 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00004550 /// expression. If DiagId and NoteId == 0, then only check is performed
4551 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00004552 /// \param DiagId Diagnostic which should be emitted if error is found.
4553 /// \param NoteId Diagnostic note for the main error message.
4554 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00004555 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00004556 /// \brief Return the 'x' lvalue part of the source atomic expression.
4557 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00004558 /// \brief Return the 'expr' rvalue part of the source atomic expression.
4559 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00004560 /// \brief Return the update expression used in calculation of the updated
4561 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
4562 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
4563 Expr *getUpdateExpr() const { return UpdateExpr; }
4564 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
4565 /// false otherwise.
4566 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
4567
Alexey Bataevb78ca832015-04-01 03:33:17 +00004568 /// \brief true if the source expression is a postfix unary operation, false
4569 /// if it is a prefix unary operation.
4570 bool isPostfixUpdate() const { return IsPostfixUpdate; }
4571
Alexey Bataev1d160b12015-03-13 12:27:31 +00004572private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00004573 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
4574 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00004575};
4576} // namespace
4577
4578bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
4579 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
4580 ExprAnalysisErrorCode ErrorFound = NoError;
4581 SourceLocation ErrorLoc, NoteLoc;
4582 SourceRange ErrorRange, NoteRange;
4583 // Allowed constructs are:
4584 // x = x binop expr;
4585 // x = expr binop x;
4586 if (AtomicBinOp->getOpcode() == BO_Assign) {
4587 X = AtomicBinOp->getLHS();
4588 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
4589 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
4590 if (AtomicInnerBinOp->isMultiplicativeOp() ||
4591 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
4592 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00004593 Op = AtomicInnerBinOp->getOpcode();
4594 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00004595 auto *LHS = AtomicInnerBinOp->getLHS();
4596 auto *RHS = AtomicInnerBinOp->getRHS();
4597 llvm::FoldingSetNodeID XId, LHSId, RHSId;
4598 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
4599 /*Canonical=*/true);
4600 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
4601 /*Canonical=*/true);
4602 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
4603 /*Canonical=*/true);
4604 if (XId == LHSId) {
4605 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004606 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004607 } else if (XId == RHSId) {
4608 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004609 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004610 } else {
4611 ErrorLoc = AtomicInnerBinOp->getExprLoc();
4612 ErrorRange = AtomicInnerBinOp->getSourceRange();
4613 NoteLoc = X->getExprLoc();
4614 NoteRange = X->getSourceRange();
4615 ErrorFound = NotAnUpdateExpression;
4616 }
4617 } else {
4618 ErrorLoc = AtomicInnerBinOp->getExprLoc();
4619 ErrorRange = AtomicInnerBinOp->getSourceRange();
4620 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
4621 NoteRange = SourceRange(NoteLoc, NoteLoc);
4622 ErrorFound = NotABinaryOperator;
4623 }
4624 } else {
4625 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
4626 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
4627 ErrorFound = NotABinaryExpression;
4628 }
4629 } else {
4630 ErrorLoc = AtomicBinOp->getExprLoc();
4631 ErrorRange = AtomicBinOp->getSourceRange();
4632 NoteLoc = AtomicBinOp->getOperatorLoc();
4633 NoteRange = SourceRange(NoteLoc, NoteLoc);
4634 ErrorFound = NotAnAssignmentOp;
4635 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00004636 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004637 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
4638 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
4639 return true;
4640 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00004641 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00004642 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004643}
4644
4645bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
4646 unsigned NoteId) {
4647 ExprAnalysisErrorCode ErrorFound = NoError;
4648 SourceLocation ErrorLoc, NoteLoc;
4649 SourceRange ErrorRange, NoteRange;
4650 // Allowed constructs are:
4651 // x++;
4652 // x--;
4653 // ++x;
4654 // --x;
4655 // x binop= expr;
4656 // x = x binop expr;
4657 // x = expr binop x;
4658 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
4659 AtomicBody = AtomicBody->IgnoreParenImpCasts();
4660 if (AtomicBody->getType()->isScalarType() ||
4661 AtomicBody->isInstantiationDependent()) {
4662 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
4663 AtomicBody->IgnoreParenImpCasts())) {
4664 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00004665 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00004666 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00004667 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00004668 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00004669 X = AtomicCompAssignOp->getLHS();
4670 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004671 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
4672 AtomicBody->IgnoreParenImpCasts())) {
4673 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00004674 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
4675 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004676 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00004677 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
4678 // Check for Unary Operation
4679 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004680 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00004681 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
4682 OpLoc = AtomicUnaryOp->getOperatorLoc();
4683 X = AtomicUnaryOp->getSubExpr();
4684 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
4685 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004686 } else {
4687 ErrorFound = NotAnUnaryIncDecExpression;
4688 ErrorLoc = AtomicUnaryOp->getExprLoc();
4689 ErrorRange = AtomicUnaryOp->getSourceRange();
4690 NoteLoc = AtomicUnaryOp->getOperatorLoc();
4691 NoteRange = SourceRange(NoteLoc, NoteLoc);
4692 }
Alexey Bataev5a195472015-09-04 12:55:50 +00004693 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004694 ErrorFound = NotABinaryOrUnaryExpression;
4695 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
4696 NoteRange = ErrorRange = AtomicBody->getSourceRange();
4697 }
4698 } else {
4699 ErrorFound = NotAScalarType;
4700 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
4701 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
4702 }
4703 } else {
4704 ErrorFound = NotAnExpression;
4705 NoteLoc = ErrorLoc = S->getLocStart();
4706 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
4707 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00004708 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004709 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
4710 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
4711 return true;
4712 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00004713 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00004714 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00004715 // Build an update expression of form 'OpaqueValueExpr(x) binop
4716 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
4717 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
4718 auto *OVEX = new (SemaRef.getASTContext())
4719 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
4720 auto *OVEExpr = new (SemaRef.getASTContext())
4721 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
4722 auto Update =
4723 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
4724 IsXLHSInRHSPart ? OVEExpr : OVEX);
4725 if (Update.isInvalid())
4726 return true;
4727 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
4728 Sema::AA_Casting);
4729 if (Update.isInvalid())
4730 return true;
4731 UpdateExpr = Update.get();
4732 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00004733 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004734}
4735
Alexey Bataev0162e452014-07-22 10:10:35 +00004736StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
4737 Stmt *AStmt,
4738 SourceLocation StartLoc,
4739 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004740 if (!AStmt)
4741 return StmtError();
4742
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004743 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00004744 // 1.2.2 OpenMP Language Terminology
4745 // Structured block - An executable statement with a single entry at the
4746 // top and a single exit at the bottom.
4747 // The point of exit cannot be a branch out of the structured block.
4748 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00004749 OpenMPClauseKind AtomicKind = OMPC_unknown;
4750 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004751 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00004752 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00004753 C->getClauseKind() == OMPC_update ||
4754 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00004755 if (AtomicKind != OMPC_unknown) {
4756 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
4757 << SourceRange(C->getLocStart(), C->getLocEnd());
4758 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
4759 << getOpenMPClauseName(AtomicKind);
4760 } else {
4761 AtomicKind = C->getClauseKind();
4762 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004763 }
4764 }
4765 }
Alexey Bataev62cec442014-11-18 10:14:22 +00004766
Alexey Bataev459dec02014-07-24 06:46:57 +00004767 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00004768 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
4769 Body = EWC->getSubExpr();
4770
Alexey Bataev62cec442014-11-18 10:14:22 +00004771 Expr *X = nullptr;
4772 Expr *V = nullptr;
4773 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004774 Expr *UE = nullptr;
4775 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004776 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00004777 // OpenMP [2.12.6, atomic Construct]
4778 // In the next expressions:
4779 // * x and v (as applicable) are both l-value expressions with scalar type.
4780 // * During the execution of an atomic region, multiple syntactic
4781 // occurrences of x must designate the same storage location.
4782 // * Neither of v and expr (as applicable) may access the storage location
4783 // designated by x.
4784 // * Neither of x and expr (as applicable) may access the storage location
4785 // designated by v.
4786 // * expr is an expression with scalar type.
4787 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
4788 // * binop, binop=, ++, and -- are not overloaded operators.
4789 // * The expression x binop expr must be numerically equivalent to x binop
4790 // (expr). This requirement is satisfied if the operators in expr have
4791 // precedence greater than binop, or by using parentheses around expr or
4792 // subexpressions of expr.
4793 // * The expression expr binop x must be numerically equivalent to (expr)
4794 // binop x. This requirement is satisfied if the operators in expr have
4795 // precedence equal to or greater than binop, or by using parentheses around
4796 // expr or subexpressions of expr.
4797 // * For forms that allow multiple occurrences of x, the number of times
4798 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00004799 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004800 enum {
4801 NotAnExpression,
4802 NotAnAssignmentOp,
4803 NotAScalarType,
4804 NotAnLValue,
4805 NoError
4806 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00004807 SourceLocation ErrorLoc, NoteLoc;
4808 SourceRange ErrorRange, NoteRange;
4809 // If clause is read:
4810 // v = x;
4811 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
4812 auto AtomicBinOp =
4813 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
4814 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
4815 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
4816 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
4817 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
4818 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
4819 if (!X->isLValue() || !V->isLValue()) {
4820 auto NotLValueExpr = X->isLValue() ? V : X;
4821 ErrorFound = NotAnLValue;
4822 ErrorLoc = AtomicBinOp->getExprLoc();
4823 ErrorRange = AtomicBinOp->getSourceRange();
4824 NoteLoc = NotLValueExpr->getExprLoc();
4825 NoteRange = NotLValueExpr->getSourceRange();
4826 }
4827 } else if (!X->isInstantiationDependent() ||
4828 !V->isInstantiationDependent()) {
4829 auto NotScalarExpr =
4830 (X->isInstantiationDependent() || X->getType()->isScalarType())
4831 ? V
4832 : X;
4833 ErrorFound = NotAScalarType;
4834 ErrorLoc = AtomicBinOp->getExprLoc();
4835 ErrorRange = AtomicBinOp->getSourceRange();
4836 NoteLoc = NotScalarExpr->getExprLoc();
4837 NoteRange = NotScalarExpr->getSourceRange();
4838 }
Alexey Bataev5a195472015-09-04 12:55:50 +00004839 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00004840 ErrorFound = NotAnAssignmentOp;
4841 ErrorLoc = AtomicBody->getExprLoc();
4842 ErrorRange = AtomicBody->getSourceRange();
4843 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
4844 : AtomicBody->getExprLoc();
4845 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
4846 : AtomicBody->getSourceRange();
4847 }
4848 } else {
4849 ErrorFound = NotAnExpression;
4850 NoteLoc = ErrorLoc = Body->getLocStart();
4851 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00004852 }
Alexey Bataev62cec442014-11-18 10:14:22 +00004853 if (ErrorFound != NoError) {
4854 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
4855 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00004856 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
4857 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00004858 return StmtError();
4859 } else if (CurContext->isDependentContext())
4860 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00004861 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004862 enum {
4863 NotAnExpression,
4864 NotAnAssignmentOp,
4865 NotAScalarType,
4866 NotAnLValue,
4867 NoError
4868 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00004869 SourceLocation ErrorLoc, NoteLoc;
4870 SourceRange ErrorRange, NoteRange;
4871 // If clause is write:
4872 // x = expr;
4873 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
4874 auto AtomicBinOp =
4875 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
4876 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00004877 X = AtomicBinOp->getLHS();
4878 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00004879 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
4880 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
4881 if (!X->isLValue()) {
4882 ErrorFound = NotAnLValue;
4883 ErrorLoc = AtomicBinOp->getExprLoc();
4884 ErrorRange = AtomicBinOp->getSourceRange();
4885 NoteLoc = X->getExprLoc();
4886 NoteRange = X->getSourceRange();
4887 }
4888 } else if (!X->isInstantiationDependent() ||
4889 !E->isInstantiationDependent()) {
4890 auto NotScalarExpr =
4891 (X->isInstantiationDependent() || X->getType()->isScalarType())
4892 ? E
4893 : X;
4894 ErrorFound = NotAScalarType;
4895 ErrorLoc = AtomicBinOp->getExprLoc();
4896 ErrorRange = AtomicBinOp->getSourceRange();
4897 NoteLoc = NotScalarExpr->getExprLoc();
4898 NoteRange = NotScalarExpr->getSourceRange();
4899 }
Alexey Bataev5a195472015-09-04 12:55:50 +00004900 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00004901 ErrorFound = NotAnAssignmentOp;
4902 ErrorLoc = AtomicBody->getExprLoc();
4903 ErrorRange = AtomicBody->getSourceRange();
4904 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
4905 : AtomicBody->getExprLoc();
4906 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
4907 : AtomicBody->getSourceRange();
4908 }
4909 } else {
4910 ErrorFound = NotAnExpression;
4911 NoteLoc = ErrorLoc = Body->getLocStart();
4912 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00004913 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00004914 if (ErrorFound != NoError) {
4915 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
4916 << ErrorRange;
4917 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
4918 << NoteRange;
4919 return StmtError();
4920 } else if (CurContext->isDependentContext())
4921 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00004922 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004923 // If clause is update:
4924 // x++;
4925 // x--;
4926 // ++x;
4927 // --x;
4928 // x binop= expr;
4929 // x = x binop expr;
4930 // x = expr binop x;
4931 OpenMPAtomicUpdateChecker Checker(*this);
4932 if (Checker.checkStatement(
4933 Body, (AtomicKind == OMPC_update)
4934 ? diag::err_omp_atomic_update_not_expression_statement
4935 : diag::err_omp_atomic_not_expression_statement,
4936 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00004937 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00004938 if (!CurContext->isDependentContext()) {
4939 E = Checker.getExpr();
4940 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00004941 UE = Checker.getUpdateExpr();
4942 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00004943 }
Alexey Bataev459dec02014-07-24 06:46:57 +00004944 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004945 enum {
4946 NotAnAssignmentOp,
4947 NotACompoundStatement,
4948 NotTwoSubstatements,
4949 NotASpecificExpression,
4950 NoError
4951 } ErrorFound = NoError;
4952 SourceLocation ErrorLoc, NoteLoc;
4953 SourceRange ErrorRange, NoteRange;
4954 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
4955 // If clause is a capture:
4956 // v = x++;
4957 // v = x--;
4958 // v = ++x;
4959 // v = --x;
4960 // v = x binop= expr;
4961 // v = x = x binop expr;
4962 // v = x = expr binop x;
4963 auto *AtomicBinOp =
4964 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
4965 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
4966 V = AtomicBinOp->getLHS();
4967 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
4968 OpenMPAtomicUpdateChecker Checker(*this);
4969 if (Checker.checkStatement(
4970 Body, diag::err_omp_atomic_capture_not_expression_statement,
4971 diag::note_omp_atomic_update))
4972 return StmtError();
4973 E = Checker.getExpr();
4974 X = Checker.getX();
4975 UE = Checker.getUpdateExpr();
4976 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
4977 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00004978 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004979 ErrorLoc = AtomicBody->getExprLoc();
4980 ErrorRange = AtomicBody->getSourceRange();
4981 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
4982 : AtomicBody->getExprLoc();
4983 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
4984 : AtomicBody->getSourceRange();
4985 ErrorFound = NotAnAssignmentOp;
4986 }
4987 if (ErrorFound != NoError) {
4988 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
4989 << ErrorRange;
4990 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
4991 return StmtError();
4992 } else if (CurContext->isDependentContext()) {
4993 UE = V = E = X = nullptr;
4994 }
4995 } else {
4996 // If clause is a capture:
4997 // { v = x; x = expr; }
4998 // { v = x; x++; }
4999 // { v = x; x--; }
5000 // { v = x; ++x; }
5001 // { v = x; --x; }
5002 // { v = x; x binop= expr; }
5003 // { v = x; x = x binop expr; }
5004 // { v = x; x = expr binop x; }
5005 // { x++; v = x; }
5006 // { x--; v = x; }
5007 // { ++x; v = x; }
5008 // { --x; v = x; }
5009 // { x binop= expr; v = x; }
5010 // { x = x binop expr; v = x; }
5011 // { x = expr binop x; v = x; }
5012 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
5013 // Check that this is { expr1; expr2; }
5014 if (CS->size() == 2) {
5015 auto *First = CS->body_front();
5016 auto *Second = CS->body_back();
5017 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
5018 First = EWC->getSubExpr()->IgnoreParenImpCasts();
5019 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
5020 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
5021 // Need to find what subexpression is 'v' and what is 'x'.
5022 OpenMPAtomicUpdateChecker Checker(*this);
5023 bool IsUpdateExprFound = !Checker.checkStatement(Second);
5024 BinaryOperator *BinOp = nullptr;
5025 if (IsUpdateExprFound) {
5026 BinOp = dyn_cast<BinaryOperator>(First);
5027 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5028 }
5029 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5030 // { v = x; x++; }
5031 // { v = x; x--; }
5032 // { v = x; ++x; }
5033 // { v = x; --x; }
5034 // { v = x; x binop= expr; }
5035 // { v = x; x = x binop expr; }
5036 // { v = x; x = expr binop x; }
5037 // Check that the first expression has form v = x.
5038 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5039 llvm::FoldingSetNodeID XId, PossibleXId;
5040 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5041 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5042 IsUpdateExprFound = XId == PossibleXId;
5043 if (IsUpdateExprFound) {
5044 V = BinOp->getLHS();
5045 X = Checker.getX();
5046 E = Checker.getExpr();
5047 UE = Checker.getUpdateExpr();
5048 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005049 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005050 }
5051 }
5052 if (!IsUpdateExprFound) {
5053 IsUpdateExprFound = !Checker.checkStatement(First);
5054 BinOp = nullptr;
5055 if (IsUpdateExprFound) {
5056 BinOp = dyn_cast<BinaryOperator>(Second);
5057 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5058 }
5059 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5060 // { x++; v = x; }
5061 // { x--; v = x; }
5062 // { ++x; v = x; }
5063 // { --x; v = x; }
5064 // { x binop= expr; v = x; }
5065 // { x = x binop expr; v = x; }
5066 // { x = expr binop x; v = x; }
5067 // Check that the second expression has form v = x.
5068 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5069 llvm::FoldingSetNodeID XId, PossibleXId;
5070 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5071 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5072 IsUpdateExprFound = XId == PossibleXId;
5073 if (IsUpdateExprFound) {
5074 V = BinOp->getLHS();
5075 X = Checker.getX();
5076 E = Checker.getExpr();
5077 UE = Checker.getUpdateExpr();
5078 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005079 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005080 }
5081 }
5082 }
5083 if (!IsUpdateExprFound) {
5084 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00005085 auto *FirstExpr = dyn_cast<Expr>(First);
5086 auto *SecondExpr = dyn_cast<Expr>(Second);
5087 if (!FirstExpr || !SecondExpr ||
5088 !(FirstExpr->isInstantiationDependent() ||
5089 SecondExpr->isInstantiationDependent())) {
5090 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
5091 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005092 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00005093 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
5094 : First->getLocStart();
5095 NoteRange = ErrorRange = FirstBinOp
5096 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00005097 : SourceRange(ErrorLoc, ErrorLoc);
5098 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005099 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
5100 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
5101 ErrorFound = NotAnAssignmentOp;
5102 NoteLoc = ErrorLoc = SecondBinOp
5103 ? SecondBinOp->getOperatorLoc()
5104 : Second->getLocStart();
5105 NoteRange = ErrorRange =
5106 SecondBinOp ? SecondBinOp->getSourceRange()
5107 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00005108 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005109 auto *PossibleXRHSInFirst =
5110 FirstBinOp->getRHS()->IgnoreParenImpCasts();
5111 auto *PossibleXLHSInSecond =
5112 SecondBinOp->getLHS()->IgnoreParenImpCasts();
5113 llvm::FoldingSetNodeID X1Id, X2Id;
5114 PossibleXRHSInFirst->Profile(X1Id, Context,
5115 /*Canonical=*/true);
5116 PossibleXLHSInSecond->Profile(X2Id, Context,
5117 /*Canonical=*/true);
5118 IsUpdateExprFound = X1Id == X2Id;
5119 if (IsUpdateExprFound) {
5120 V = FirstBinOp->getLHS();
5121 X = SecondBinOp->getLHS();
5122 E = SecondBinOp->getRHS();
5123 UE = nullptr;
5124 IsXLHSInRHSPart = false;
5125 IsPostfixUpdate = true;
5126 } else {
5127 ErrorFound = NotASpecificExpression;
5128 ErrorLoc = FirstBinOp->getExprLoc();
5129 ErrorRange = FirstBinOp->getSourceRange();
5130 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
5131 NoteRange = SecondBinOp->getRHS()->getSourceRange();
5132 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005133 }
5134 }
5135 }
5136 }
5137 } else {
5138 NoteLoc = ErrorLoc = Body->getLocStart();
5139 NoteRange = ErrorRange =
5140 SourceRange(Body->getLocStart(), Body->getLocStart());
5141 ErrorFound = NotTwoSubstatements;
5142 }
5143 } else {
5144 NoteLoc = ErrorLoc = Body->getLocStart();
5145 NoteRange = ErrorRange =
5146 SourceRange(Body->getLocStart(), Body->getLocStart());
5147 ErrorFound = NotACompoundStatement;
5148 }
5149 if (ErrorFound != NoError) {
5150 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
5151 << ErrorRange;
5152 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5153 return StmtError();
5154 } else if (CurContext->isDependentContext()) {
5155 UE = V = E = X = nullptr;
5156 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005157 }
Alexey Bataevdea47612014-07-23 07:46:59 +00005158 }
Alexey Bataev0162e452014-07-22 10:10:35 +00005159
5160 getCurFunction()->setHasBranchProtectedScope();
5161
Alexey Bataev62cec442014-11-18 10:14:22 +00005162 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00005163 X, V, E, UE, IsXLHSInRHSPart,
5164 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00005165}
5166
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005167StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
5168 Stmt *AStmt,
5169 SourceLocation StartLoc,
5170 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005171 if (!AStmt)
5172 return StmtError();
5173
Samuel Antao4af1b7b2015-12-02 17:44:43 +00005174 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5175 // 1.2.2 OpenMP Language Terminology
5176 // Structured block - An executable statement with a single entry at the
5177 // top and a single exit at the bottom.
5178 // The point of exit cannot be a branch out of the structured block.
5179 // longjmp() and throw() must not violate the entry/exit criteria.
5180 CS->getCapturedDecl()->setNothrow();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005181
Alexey Bataev13314bf2014-10-09 04:18:56 +00005182 // OpenMP [2.16, Nesting of Regions]
5183 // If specified, a teams construct must be contained within a target
5184 // construct. That target construct must contain no statements or directives
5185 // outside of the teams construct.
5186 if (DSAStack->hasInnerTeamsRegion()) {
5187 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
5188 bool OMPTeamsFound = true;
5189 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
5190 auto I = CS->body_begin();
5191 while (I != CS->body_end()) {
5192 auto OED = dyn_cast<OMPExecutableDirective>(*I);
5193 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
5194 OMPTeamsFound = false;
5195 break;
5196 }
5197 ++I;
5198 }
5199 assert(I != CS->body_end() && "Not found statement");
5200 S = *I;
5201 }
5202 if (!OMPTeamsFound) {
5203 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
5204 Diag(DSAStack->getInnerTeamsRegionLoc(),
5205 diag::note_omp_nested_teams_construct_here);
5206 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
5207 << isa<OMPExecutableDirective>(S);
5208 return StmtError();
5209 }
5210 }
5211
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005212 getCurFunction()->setHasBranchProtectedScope();
5213
5214 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5215}
5216
Michael Wong65f367f2015-07-21 13:44:28 +00005217StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
5218 Stmt *AStmt,
5219 SourceLocation StartLoc,
5220 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005221 if (!AStmt)
5222 return StmtError();
5223
5224 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5225
Michael Wong65f367f2015-07-21 13:44:28 +00005226 getCurFunction()->setHasBranchProtectedScope();
5227
5228 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
5229 AStmt);
5230}
5231
Alexey Bataev13314bf2014-10-09 04:18:56 +00005232StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
5233 Stmt *AStmt, SourceLocation StartLoc,
5234 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005235 if (!AStmt)
5236 return StmtError();
5237
Alexey Bataev13314bf2014-10-09 04:18:56 +00005238 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5239 // 1.2.2 OpenMP Language Terminology
5240 // Structured block - An executable statement with a single entry at the
5241 // top and a single exit at the bottom.
5242 // The point of exit cannot be a branch out of the structured block.
5243 // longjmp() and throw() must not violate the entry/exit criteria.
5244 CS->getCapturedDecl()->setNothrow();
5245
5246 getCurFunction()->setHasBranchProtectedScope();
5247
5248 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5249}
5250
Alexey Bataev6d4ed052015-07-01 06:57:41 +00005251StmtResult
5252Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
5253 SourceLocation EndLoc,
5254 OpenMPDirectiveKind CancelRegion) {
5255 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
5256 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
5257 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
5258 << getOpenMPDirectiveName(CancelRegion);
5259 return StmtError();
5260 }
5261 if (DSAStack->isParentNowaitRegion()) {
5262 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
5263 return StmtError();
5264 }
5265 if (DSAStack->isParentOrderedRegion()) {
5266 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
5267 return StmtError();
5268 }
5269 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
5270 CancelRegion);
5271}
5272
Alexey Bataev87933c72015-09-18 08:07:34 +00005273StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
5274 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00005275 SourceLocation EndLoc,
5276 OpenMPDirectiveKind CancelRegion) {
5277 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
5278 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
5279 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
5280 << getOpenMPDirectiveName(CancelRegion);
5281 return StmtError();
5282 }
5283 if (DSAStack->isParentNowaitRegion()) {
5284 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
5285 return StmtError();
5286 }
5287 if (DSAStack->isParentOrderedRegion()) {
5288 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
5289 return StmtError();
5290 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005291 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00005292 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
5293 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00005294}
5295
Alexey Bataev49f6e782015-12-01 04:18:41 +00005296StmtResult Sema::ActOnOpenMPTaskLoopDirective(
5297 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5298 SourceLocation EndLoc,
5299 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
5300 if (!AStmt)
5301 return StmtError();
5302
5303 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5304 OMPLoopDirective::HelperExprs B;
5305 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5306 // define the nested loops number.
5307 unsigned NestedLoopCount =
5308 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005309 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00005310 VarsWithImplicitDSA, B);
5311 if (NestedLoopCount == 0)
5312 return StmtError();
5313
5314 assert((CurContext->isDependentContext() || B.builtAll()) &&
5315 "omp for loop exprs were not built");
5316
5317 getCurFunction()->setHasBranchProtectedScope();
5318 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
5319 NestedLoopCount, Clauses, AStmt, B);
5320}
5321
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005322StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
5323 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5324 SourceLocation EndLoc,
5325 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
5326 if (!AStmt)
5327 return StmtError();
5328
5329 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5330 OMPLoopDirective::HelperExprs B;
5331 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5332 // define the nested loops number.
5333 unsigned NestedLoopCount =
5334 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
5335 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
5336 VarsWithImplicitDSA, B);
5337 if (NestedLoopCount == 0)
5338 return StmtError();
5339
5340 assert((CurContext->isDependentContext() || B.builtAll()) &&
5341 "omp for loop exprs were not built");
5342
5343 getCurFunction()->setHasBranchProtectedScope();
5344 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
5345 NestedLoopCount, Clauses, AStmt, B);
5346}
5347
Alexey Bataeved09d242014-05-28 05:53:51 +00005348OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005349 SourceLocation StartLoc,
5350 SourceLocation LParenLoc,
5351 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005352 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005353 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00005354 case OMPC_final:
5355 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
5356 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00005357 case OMPC_num_threads:
5358 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
5359 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00005360 case OMPC_safelen:
5361 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
5362 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00005363 case OMPC_simdlen:
5364 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
5365 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00005366 case OMPC_collapse:
5367 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
5368 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005369 case OMPC_ordered:
5370 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
5371 break;
Michael Wonge710d542015-08-07 16:16:36 +00005372 case OMPC_device:
5373 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
5374 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00005375 case OMPC_num_teams:
5376 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
5377 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005378 case OMPC_thread_limit:
5379 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
5380 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00005381 case OMPC_priority:
5382 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
5383 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00005384 case OMPC_grainsize:
5385 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
5386 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005387 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005388 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005389 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005390 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005391 case OMPC_private:
5392 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00005393 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005394 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00005395 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00005396 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00005397 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005398 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005399 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00005400 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005401 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005402 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005403 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00005404 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005405 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00005406 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00005407 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00005408 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005409 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005410 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00005411 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005412 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00005413 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00005414 case OMPC_nogroup:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005415 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005416 llvm_unreachable("Clause is not allowed.");
5417 }
5418 return Res;
5419}
5420
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005421OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
5422 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005423 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005424 SourceLocation NameModifierLoc,
5425 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005426 SourceLocation EndLoc) {
5427 Expr *ValExpr = Condition;
5428 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
5429 !Condition->isInstantiationDependent() &&
5430 !Condition->containsUnexpandedParameterPack()) {
5431 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00005432 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005433 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005434 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005435
Nikola Smiljanic01a75982014-05-29 10:55:11 +00005436 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005437 }
5438
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005439 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
5440 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005441}
5442
Alexey Bataev3778b602014-07-17 07:32:53 +00005443OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
5444 SourceLocation StartLoc,
5445 SourceLocation LParenLoc,
5446 SourceLocation EndLoc) {
5447 Expr *ValExpr = Condition;
5448 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
5449 !Condition->isInstantiationDependent() &&
5450 !Condition->containsUnexpandedParameterPack()) {
5451 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
5452 Condition->getExprLoc(), Condition);
5453 if (Val.isInvalid())
5454 return nullptr;
5455
5456 ValExpr = Val.get();
5457 }
5458
5459 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
5460}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005461ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
5462 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00005463 if (!Op)
5464 return ExprError();
5465
5466 class IntConvertDiagnoser : public ICEConvertDiagnoser {
5467 public:
5468 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00005469 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00005470 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
5471 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005472 return S.Diag(Loc, diag::err_omp_not_integral) << T;
5473 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005474 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
5475 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005476 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
5477 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005478 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
5479 QualType T,
5480 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005481 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
5482 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005483 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
5484 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005485 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00005486 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00005487 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005488 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
5489 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005490 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
5491 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005492 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
5493 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005494 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00005495 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00005496 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005497 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
5498 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005499 llvm_unreachable("conversion functions are permitted");
5500 }
5501 } ConvertDiagnoser;
5502 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
5503}
5504
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005505static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00005506 OpenMPClauseKind CKind,
5507 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005508 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
5509 !ValExpr->isInstantiationDependent()) {
5510 SourceLocation Loc = ValExpr->getExprLoc();
5511 ExprResult Value =
5512 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
5513 if (Value.isInvalid())
5514 return false;
5515
5516 ValExpr = Value.get();
5517 // The expression must evaluate to a non-negative integer value.
5518 llvm::APSInt Result;
5519 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00005520 Result.isSigned() &&
5521 !((!StrictlyPositive && Result.isNonNegative()) ||
5522 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005523 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00005524 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
5525 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005526 return false;
5527 }
5528 }
5529 return true;
5530}
5531
Alexey Bataev568a8332014-03-06 06:15:19 +00005532OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
5533 SourceLocation StartLoc,
5534 SourceLocation LParenLoc,
5535 SourceLocation EndLoc) {
5536 Expr *ValExpr = NumThreads;
Alexey Bataev568a8332014-03-06 06:15:19 +00005537
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005538 // OpenMP [2.5, Restrictions]
5539 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00005540 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
5541 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005542 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00005543
Alexey Bataeved09d242014-05-28 05:53:51 +00005544 return new (Context)
5545 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00005546}
5547
Alexey Bataev62c87d22014-03-21 04:51:18 +00005548ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
5549 OpenMPClauseKind CKind) {
5550 if (!E)
5551 return ExprError();
5552 if (E->isValueDependent() || E->isTypeDependent() ||
5553 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00005554 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00005555 llvm::APSInt Result;
5556 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
5557 if (ICE.isInvalid())
5558 return ExprError();
5559 if (!Result.isStrictlyPositive()) {
5560 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00005561 << getOpenMPClauseName(CKind) << 1 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00005562 return ExprError();
5563 }
Alexander Musman09184fe2014-09-30 05:29:28 +00005564 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
5565 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
5566 << E->getSourceRange();
5567 return ExprError();
5568 }
Alexey Bataev7b6bc882015-11-26 07:50:39 +00005569 if (CKind == OMPC_collapse)
5570 DSAStack->setCollapseNumber(Result.getExtValue());
5571 else if (CKind == OMPC_ordered)
5572 DSAStack->setCollapseNumber(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00005573 return ICE;
5574}
5575
5576OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
5577 SourceLocation LParenLoc,
5578 SourceLocation EndLoc) {
5579 // OpenMP [2.8.1, simd construct, Description]
5580 // The parameter of the safelen clause must be a constant
5581 // positive integer expression.
5582 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
5583 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005584 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00005585 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00005586 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00005587}
5588
Alexey Bataev66b15b52015-08-21 11:14:16 +00005589OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
5590 SourceLocation LParenLoc,
5591 SourceLocation EndLoc) {
5592 // OpenMP [2.8.1, simd construct, Description]
5593 // The parameter of the simdlen clause must be a constant
5594 // positive integer expression.
5595 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
5596 if (Simdlen.isInvalid())
5597 return nullptr;
5598 return new (Context)
5599 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
5600}
5601
Alexander Musman64d33f12014-06-04 07:53:32 +00005602OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
5603 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00005604 SourceLocation LParenLoc,
5605 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00005606 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00005607 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00005608 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00005609 // The parameter of the collapse clause must be a constant
5610 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00005611 ExprResult NumForLoopsResult =
5612 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
5613 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00005614 return nullptr;
5615 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00005616 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00005617}
5618
Alexey Bataev10e775f2015-07-30 11:36:16 +00005619OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
5620 SourceLocation EndLoc,
5621 SourceLocation LParenLoc,
5622 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00005623 // OpenMP [2.7.1, loop construct, Description]
5624 // OpenMP [2.8.1, simd construct, Description]
5625 // OpenMP [2.9.6, distribute construct, Description]
5626 // The parameter of the ordered clause must be a constant
5627 // positive integer expression if any.
5628 if (NumForLoops && LParenLoc.isValid()) {
5629 ExprResult NumForLoopsResult =
5630 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
5631 if (NumForLoopsResult.isInvalid())
5632 return nullptr;
5633 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00005634 } else
5635 NumForLoops = nullptr;
5636 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00005637 return new (Context)
5638 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
5639}
5640
Alexey Bataeved09d242014-05-28 05:53:51 +00005641OMPClause *Sema::ActOnOpenMPSimpleClause(
5642 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
5643 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005644 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005645 switch (Kind) {
5646 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00005647 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00005648 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
5649 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005650 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005651 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00005652 Res = ActOnOpenMPProcBindClause(
5653 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
5654 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005655 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005656 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00005657 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00005658 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00005659 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00005660 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00005661 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005662 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005663 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005664 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00005665 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00005666 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00005667 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00005668 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00005669 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005670 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005671 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005672 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00005673 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005674 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005675 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005676 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00005677 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005678 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00005679 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00005680 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00005681 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005682 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005683 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00005684 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00005685 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005686 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00005687 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00005688 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005689 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00005690 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00005691 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00005692 case OMPC_nogroup:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005693 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005694 llvm_unreachable("Clause is not allowed.");
5695 }
5696 return Res;
5697}
5698
5699OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
5700 SourceLocation KindKwLoc,
5701 SourceLocation StartLoc,
5702 SourceLocation LParenLoc,
5703 SourceLocation EndLoc) {
5704 if (Kind == OMPC_DEFAULT_unknown) {
5705 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005706 static_assert(OMPC_DEFAULT_unknown > 0,
5707 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00005708 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005709 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005710 Values += "'";
5711 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
5712 Values += "'";
5713 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005714 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005715 Values += " or ";
5716 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005717 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005718 break;
5719 default:
5720 Values += Sep;
5721 break;
5722 }
5723 }
5724 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00005725 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005726 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005727 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00005728 switch (Kind) {
5729 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005730 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005731 break;
5732 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005733 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005734 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005735 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005736 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00005737 break;
5738 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005739 return new (Context)
5740 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005741}
5742
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005743OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
5744 SourceLocation KindKwLoc,
5745 SourceLocation StartLoc,
5746 SourceLocation LParenLoc,
5747 SourceLocation EndLoc) {
5748 if (Kind == OMPC_PROC_BIND_unknown) {
5749 std::string Values;
5750 std::string Sep(", ");
5751 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
5752 Values += "'";
5753 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
5754 Values += "'";
5755 switch (i) {
5756 case OMPC_PROC_BIND_unknown - 2:
5757 Values += " or ";
5758 break;
5759 case OMPC_PROC_BIND_unknown - 1:
5760 break;
5761 default:
5762 Values += Sep;
5763 break;
5764 }
5765 }
5766 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00005767 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005768 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005769 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005770 return new (Context)
5771 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005772}
5773
Alexey Bataev56dafe82014-06-20 07:16:17 +00005774OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
5775 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
5776 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005777 SourceLocation ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00005778 SourceLocation EndLoc) {
5779 OMPClause *Res = nullptr;
5780 switch (Kind) {
5781 case OMPC_schedule:
5782 Res = ActOnOpenMPScheduleClause(
5783 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005784 LParenLoc, ArgumentLoc, DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00005785 break;
5786 case OMPC_if:
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005787 Res =
5788 ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument), Expr,
5789 StartLoc, LParenLoc, ArgumentLoc, DelimLoc, EndLoc);
5790 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00005791 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005792 case OMPC_num_threads:
5793 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00005794 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005795 case OMPC_collapse:
5796 case OMPC_default:
5797 case OMPC_proc_bind:
5798 case OMPC_private:
5799 case OMPC_firstprivate:
5800 case OMPC_lastprivate:
5801 case OMPC_shared:
5802 case OMPC_reduction:
5803 case OMPC_linear:
5804 case OMPC_aligned:
5805 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005806 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005807 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00005808 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005809 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005810 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005811 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00005812 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005813 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00005814 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00005815 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00005816 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005817 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005818 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00005819 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00005820 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005821 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00005822 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00005823 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005824 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00005825 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00005826 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00005827 case OMPC_nogroup:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005828 case OMPC_unknown:
5829 llvm_unreachable("Clause is not allowed.");
5830 }
5831 return Res;
5832}
5833
5834OMPClause *Sema::ActOnOpenMPScheduleClause(
5835 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
5836 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
5837 SourceLocation EndLoc) {
5838 if (Kind == OMPC_SCHEDULE_unknown) {
5839 std::string Values;
5840 std::string Sep(", ");
5841 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) {
5842 Values += "'";
5843 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i);
5844 Values += "'";
5845 switch (i) {
5846 case OMPC_SCHEDULE_unknown - 2:
5847 Values += " or ";
5848 break;
5849 case OMPC_SCHEDULE_unknown - 1:
5850 break;
5851 default:
5852 Values += Sep;
5853 break;
5854 }
5855 }
5856 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
5857 << Values << getOpenMPClauseName(OMPC_schedule);
5858 return nullptr;
5859 }
5860 Expr *ValExpr = ChunkSize;
Alexey Bataev040d5402015-05-12 08:35:28 +00005861 Expr *HelperValExpr = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00005862 if (ChunkSize) {
5863 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
5864 !ChunkSize->isInstantiationDependent() &&
5865 !ChunkSize->containsUnexpandedParameterPack()) {
5866 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
5867 ExprResult Val =
5868 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
5869 if (Val.isInvalid())
5870 return nullptr;
5871
5872 ValExpr = Val.get();
5873
5874 // OpenMP [2.7.1, Restrictions]
5875 // chunk_size must be a loop invariant integer expression with a positive
5876 // value.
5877 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00005878 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
5879 if (Result.isSigned() && !Result.isStrictlyPositive()) {
5880 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00005881 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00005882 return nullptr;
5883 }
5884 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
5885 auto *ImpVar = buildVarDecl(*this, ChunkSize->getExprLoc(),
5886 ChunkSize->getType(), ".chunk.");
5887 auto *ImpVarRef = buildDeclRefExpr(*this, ImpVar, ChunkSize->getType(),
5888 ChunkSize->getExprLoc(),
5889 /*RefersToCapture=*/true);
5890 HelperValExpr = ImpVarRef;
Alexey Bataev56dafe82014-06-20 07:16:17 +00005891 }
5892 }
5893 }
5894
5895 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc,
Alexey Bataev040d5402015-05-12 08:35:28 +00005896 EndLoc, Kind, ValExpr, HelperValExpr);
Alexey Bataev56dafe82014-06-20 07:16:17 +00005897}
5898
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005899OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
5900 SourceLocation StartLoc,
5901 SourceLocation EndLoc) {
5902 OMPClause *Res = nullptr;
5903 switch (Kind) {
5904 case OMPC_ordered:
5905 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
5906 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00005907 case OMPC_nowait:
5908 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
5909 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005910 case OMPC_untied:
5911 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
5912 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005913 case OMPC_mergeable:
5914 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
5915 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005916 case OMPC_read:
5917 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
5918 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00005919 case OMPC_write:
5920 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
5921 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00005922 case OMPC_update:
5923 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
5924 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00005925 case OMPC_capture:
5926 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
5927 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005928 case OMPC_seq_cst:
5929 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
5930 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00005931 case OMPC_threads:
5932 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
5933 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005934 case OMPC_simd:
5935 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
5936 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00005937 case OMPC_nogroup:
5938 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
5939 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005940 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00005941 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005942 case OMPC_num_threads:
5943 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00005944 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005945 case OMPC_collapse:
5946 case OMPC_schedule:
5947 case OMPC_private:
5948 case OMPC_firstprivate:
5949 case OMPC_lastprivate:
5950 case OMPC_shared:
5951 case OMPC_reduction:
5952 case OMPC_linear:
5953 case OMPC_aligned:
5954 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005955 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005956 case OMPC_default:
5957 case OMPC_proc_bind:
5958 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00005959 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005960 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00005961 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00005962 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00005963 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005964 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00005965 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00005966 case OMPC_grainsize:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005967 case OMPC_unknown:
5968 llvm_unreachable("Clause is not allowed.");
5969 }
5970 return Res;
5971}
5972
Alexey Bataev236070f2014-06-20 11:19:47 +00005973OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
5974 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00005975 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00005976 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
5977}
5978
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005979OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
5980 SourceLocation EndLoc) {
5981 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
5982}
5983
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005984OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
5985 SourceLocation EndLoc) {
5986 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
5987}
5988
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005989OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
5990 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005991 return new (Context) OMPReadClause(StartLoc, EndLoc);
5992}
5993
Alexey Bataevdea47612014-07-23 07:46:59 +00005994OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
5995 SourceLocation EndLoc) {
5996 return new (Context) OMPWriteClause(StartLoc, EndLoc);
5997}
5998
Alexey Bataev67a4f222014-07-23 10:25:33 +00005999OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
6000 SourceLocation EndLoc) {
6001 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
6002}
6003
Alexey Bataev459dec02014-07-24 06:46:57 +00006004OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
6005 SourceLocation EndLoc) {
6006 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
6007}
6008
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006009OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
6010 SourceLocation EndLoc) {
6011 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
6012}
6013
Alexey Bataev346265e2015-09-25 10:37:12 +00006014OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
6015 SourceLocation EndLoc) {
6016 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
6017}
6018
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006019OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
6020 SourceLocation EndLoc) {
6021 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
6022}
6023
Alexey Bataevb825de12015-12-07 10:51:44 +00006024OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
6025 SourceLocation EndLoc) {
6026 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
6027}
6028
Alexey Bataevc5e02582014-06-16 07:08:35 +00006029OMPClause *Sema::ActOnOpenMPVarListClause(
6030 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
6031 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
6032 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006033 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Kelvin Li0bff7af2015-11-23 05:32:03 +00006034 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
6035 OpenMPMapClauseKind MapType, SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006036 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006037 switch (Kind) {
6038 case OMPC_private:
6039 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6040 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006041 case OMPC_firstprivate:
6042 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6043 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00006044 case OMPC_lastprivate:
6045 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6046 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00006047 case OMPC_shared:
6048 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
6049 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006050 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00006051 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
6052 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006053 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00006054 case OMPC_linear:
6055 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00006056 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00006057 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006058 case OMPC_aligned:
6059 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
6060 ColonLoc, EndLoc);
6061 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006062 case OMPC_copyin:
6063 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
6064 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00006065 case OMPC_copyprivate:
6066 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6067 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00006068 case OMPC_flush:
6069 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
6070 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006071 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006072 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
6073 StartLoc, LParenLoc, EndLoc);
6074 break;
6075 case OMPC_map:
6076 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, DepLinMapLoc, ColonLoc,
6077 VarList, StartLoc, LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006078 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006079 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006080 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00006081 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00006082 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006083 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00006084 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006085 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006086 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006087 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006088 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006089 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006090 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006091 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006092 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006093 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006094 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006095 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006096 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006097 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00006098 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006099 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006100 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006101 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006102 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006103 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006104 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006105 case OMPC_nogroup:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006106 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006107 llvm_unreachable("Clause is not allowed.");
6108 }
6109 return Res;
6110}
6111
6112OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
6113 SourceLocation StartLoc,
6114 SourceLocation LParenLoc,
6115 SourceLocation EndLoc) {
6116 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00006117 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00006118 for (auto &RefExpr : VarList) {
6119 assert(RefExpr && "NULL expr in OpenMP private clause.");
6120 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006121 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006122 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006123 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006124 continue;
6125 }
6126
Alexey Bataeved09d242014-05-28 05:53:51 +00006127 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006128 // OpenMP [2.1, C/C++]
6129 // A list item is a variable name.
6130 // OpenMP [2.9.3.3, Restrictions, p.1]
6131 // A variable that is part of another variable (as an array or
6132 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00006133 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006134 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006135 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006136 continue;
6137 }
6138 Decl *D = DE->getDecl();
6139 VarDecl *VD = cast<VarDecl>(D);
6140
6141 QualType Type = VD->getType();
6142 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6143 // It will be analyzed later.
6144 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006145 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006146 continue;
6147 }
6148
6149 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
6150 // A variable that appears in a private clause must not have an incomplete
6151 // type or a reference type.
6152 if (RequireCompleteType(ELoc, Type,
6153 diag::err_omp_private_incomplete_type)) {
6154 continue;
6155 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006156 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006157
Alexey Bataev758e55e2013-09-06 18:03:48 +00006158 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6159 // in a Construct]
6160 // Variables with the predetermined data-sharing attributes may not be
6161 // listed in data-sharing attributes clauses, except for the cases
6162 // listed below. For these exceptions only, listing a predetermined
6163 // variable in a data-sharing attribute clause is allowed and overrides
6164 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006165 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006166 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006167 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
6168 << getOpenMPClauseName(OMPC_private);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006169 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006170 continue;
6171 }
6172
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006173 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00006174 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006175 DSAStack->getCurrentDirective() == OMPD_task) {
6176 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
6177 << getOpenMPClauseName(OMPC_private) << Type
6178 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
6179 bool IsDecl =
6180 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6181 Diag(VD->getLocation(),
6182 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6183 << VD;
6184 continue;
6185 }
6186
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006187 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
6188 // A variable of class type (or array thereof) that appears in a private
6189 // clause requires an accessible, unambiguous default constructor for the
6190 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00006191 // Generate helper private variable and initialize it with the default
6192 // value. The address of the original variable is replaced by the address of
6193 // the new private variable in CodeGen. This new variable is not added to
6194 // IdResolver, so the code in the OpenMP region uses original variable for
6195 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006196 Type = Type.getUnqualifiedType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006197 auto VDPrivate = buildVarDecl(*this, DE->getExprLoc(), Type, VD->getName(),
6198 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00006199 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006200 if (VDPrivate->isInvalidDecl())
6201 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006202 auto VDPrivateRefExpr = buildDeclRefExpr(
6203 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev03b340a2014-10-21 03:16:40 +00006204
Alexey Bataev758e55e2013-09-06 18:03:48 +00006205 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006206 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006207 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006208 }
6209
Alexey Bataeved09d242014-05-28 05:53:51 +00006210 if (Vars.empty())
6211 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006212
Alexey Bataev03b340a2014-10-21 03:16:40 +00006213 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
6214 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006215}
6216
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006217namespace {
6218class DiagsUninitializedSeveretyRAII {
6219private:
6220 DiagnosticsEngine &Diags;
6221 SourceLocation SavedLoc;
6222 bool IsIgnored;
6223
6224public:
6225 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
6226 bool IsIgnored)
6227 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
6228 if (!IsIgnored) {
6229 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
6230 /*Map*/ diag::Severity::Ignored, Loc);
6231 }
6232 }
6233 ~DiagsUninitializedSeveretyRAII() {
6234 if (!IsIgnored)
6235 Diags.popMappings(SavedLoc);
6236 }
6237};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00006238}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006239
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006240OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
6241 SourceLocation StartLoc,
6242 SourceLocation LParenLoc,
6243 SourceLocation EndLoc) {
6244 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006245 SmallVector<Expr *, 8> PrivateCopies;
6246 SmallVector<Expr *, 8> Inits;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006247 bool IsImplicitClause =
6248 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
6249 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
6250
Alexey Bataeved09d242014-05-28 05:53:51 +00006251 for (auto &RefExpr : VarList) {
6252 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
6253 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006254 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006255 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006256 PrivateCopies.push_back(nullptr);
6257 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006258 continue;
6259 }
6260
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006261 SourceLocation ELoc =
6262 IsImplicitClause ? ImplicitClauseLoc : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006263 // OpenMP [2.1, C/C++]
6264 // A list item is a variable name.
6265 // OpenMP [2.9.3.3, Restrictions, p.1]
6266 // A variable that is part of another variable (as an array or
6267 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00006268 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006269 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006270 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006271 continue;
6272 }
6273 Decl *D = DE->getDecl();
6274 VarDecl *VD = cast<VarDecl>(D);
6275
6276 QualType Type = VD->getType();
6277 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6278 // It will be analyzed later.
6279 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006280 PrivateCopies.push_back(nullptr);
6281 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006282 continue;
6283 }
6284
6285 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
6286 // A variable that appears in a private clause must not have an incomplete
6287 // type or a reference type.
6288 if (RequireCompleteType(ELoc, Type,
6289 diag::err_omp_firstprivate_incomplete_type)) {
6290 continue;
6291 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006292 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006293
6294 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
6295 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00006296 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006297 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006298 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006299
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006300 // If an implicit firstprivate variable found it was checked already.
6301 if (!IsImplicitClause) {
6302 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006303 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006304 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
6305 // A list item that specifies a given variable may not appear in more
6306 // than one clause on the same directive, except that a variable may be
6307 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006308 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00006309 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006310 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00006311 << getOpenMPClauseName(DVar.CKind)
6312 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006313 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006314 continue;
6315 }
6316
6317 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6318 // in a Construct]
6319 // Variables with the predetermined data-sharing attributes may not be
6320 // listed in data-sharing attributes clauses, except for the cases
6321 // listed below. For these exceptions only, listing a predetermined
6322 // variable in a data-sharing attribute clause is allowed and overrides
6323 // the variable's predetermined data-sharing attributes.
6324 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6325 // in a Construct, C/C++, p.2]
6326 // Variables with const-qualified type having no mutable member may be
6327 // listed in a firstprivate clause, even if they are static data members.
6328 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
6329 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
6330 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00006331 << getOpenMPClauseName(DVar.CKind)
6332 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006333 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006334 continue;
6335 }
6336
Alexey Bataevf29276e2014-06-18 04:14:57 +00006337 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006338 // OpenMP [2.9.3.4, Restrictions, p.2]
6339 // A list item that is private within a parallel region must not appear
6340 // in a firstprivate clause on a worksharing construct if any of the
6341 // worksharing regions arising from the worksharing construct ever bind
6342 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00006343 if (isOpenMPWorksharingDirective(CurrDir) &&
6344 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006345 DVar = DSAStack->getImplicitDSA(VD, true);
6346 if (DVar.CKind != OMPC_shared &&
6347 (isOpenMPParallelDirective(DVar.DKind) ||
6348 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00006349 Diag(ELoc, diag::err_omp_required_access)
6350 << getOpenMPClauseName(OMPC_firstprivate)
6351 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006352 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006353 continue;
6354 }
6355 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006356 // OpenMP [2.9.3.4, Restrictions, p.3]
6357 // A list item that appears in a reduction clause of a parallel construct
6358 // must not appear in a firstprivate clause on a worksharing or task
6359 // construct if any of the worksharing or task regions arising from the
6360 // worksharing or task construct ever bind to any of the parallel regions
6361 // arising from the parallel construct.
6362 // OpenMP [2.9.3.4, Restrictions, p.4]
6363 // A list item that appears in a reduction clause in worksharing
6364 // construct must not appear in a firstprivate clause in a task construct
6365 // encountered during execution of any of the worksharing regions arising
6366 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006367 if (CurrDir == OMPD_task) {
6368 DVar =
6369 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
6370 [](OpenMPDirectiveKind K) -> bool {
6371 return isOpenMPParallelDirective(K) ||
6372 isOpenMPWorksharingDirective(K);
6373 },
6374 false);
6375 if (DVar.CKind == OMPC_reduction &&
6376 (isOpenMPParallelDirective(DVar.DKind) ||
6377 isOpenMPWorksharingDirective(DVar.DKind))) {
6378 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
6379 << getOpenMPDirectiveName(DVar.DKind);
6380 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6381 continue;
6382 }
6383 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006384 }
6385
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006386 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00006387 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006388 DSAStack->getCurrentDirective() == OMPD_task) {
6389 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
6390 << getOpenMPClauseName(OMPC_firstprivate) << Type
6391 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
6392 bool IsDecl =
6393 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6394 Diag(VD->getLocation(),
6395 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6396 << VD;
6397 continue;
6398 }
6399
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006400 Type = Type.getUnqualifiedType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006401 auto VDPrivate = buildVarDecl(*this, ELoc, Type, VD->getName(),
6402 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006403 // Generate helper private variable and initialize it with the value of the
6404 // original variable. The address of the original variable is replaced by
6405 // the address of the new private variable in the CodeGen. This new variable
6406 // is not added to IdResolver, so the code in the OpenMP region uses
6407 // original variable for proper diagnostics and variable capturing.
6408 Expr *VDInitRefExpr = nullptr;
6409 // For arrays generate initializer for single element and replace it by the
6410 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006411 if (Type->isArrayType()) {
6412 auto VDInit =
6413 buildVarDecl(*this, DE->getExprLoc(), ElemType, VD->getName());
6414 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006415 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006416 ElemType = ElemType.getUnqualifiedType();
6417 auto *VDInitTemp = buildVarDecl(*this, DE->getLocStart(), ElemType,
6418 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00006419 InitializedEntity Entity =
6420 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006421 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
6422
6423 InitializationSequence InitSeq(*this, Entity, Kind, Init);
6424 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
6425 if (Result.isInvalid())
6426 VDPrivate->setInvalidDecl();
6427 else
6428 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006429 // Remove temp variable declaration.
6430 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006431 } else {
Alexey Bataev69c62a92015-04-15 04:52:20 +00006432 auto *VDInit =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006433 buildVarDecl(*this, DE->getLocStart(), Type, ".firstprivate.temp");
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006434 VDInitRefExpr =
6435 buildDeclRefExpr(*this, VDInit, DE->getType(), DE->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00006436 AddInitializerToDecl(VDPrivate,
6437 DefaultLvalueConversion(VDInitRefExpr).get(),
6438 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006439 }
6440 if (VDPrivate->isInvalidDecl()) {
6441 if (IsImplicitClause) {
6442 Diag(DE->getExprLoc(),
6443 diag::note_omp_task_predetermined_firstprivate_here);
6444 }
6445 continue;
6446 }
6447 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00006448 auto VDPrivateRefExpr = buildDeclRefExpr(
6449 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006450 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
6451 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006452 PrivateCopies.push_back(VDPrivateRefExpr);
6453 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006454 }
6455
Alexey Bataeved09d242014-05-28 05:53:51 +00006456 if (Vars.empty())
6457 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006458
6459 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006460 Vars, PrivateCopies, Inits);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006461}
6462
Alexander Musman1bb328c2014-06-04 13:06:39 +00006463OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
6464 SourceLocation StartLoc,
6465 SourceLocation LParenLoc,
6466 SourceLocation EndLoc) {
6467 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00006468 SmallVector<Expr *, 8> SrcExprs;
6469 SmallVector<Expr *, 8> DstExprs;
6470 SmallVector<Expr *, 8> AssignmentOps;
Alexander Musman1bb328c2014-06-04 13:06:39 +00006471 for (auto &RefExpr : VarList) {
6472 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
6473 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6474 // It will be analyzed later.
6475 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00006476 SrcExprs.push_back(nullptr);
6477 DstExprs.push_back(nullptr);
6478 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006479 continue;
6480 }
6481
6482 SourceLocation ELoc = RefExpr->getExprLoc();
6483 // OpenMP [2.1, C/C++]
6484 // A list item is a variable name.
6485 // OpenMP [2.14.3.5, Restrictions, p.1]
6486 // A variable that is part of another variable (as an array or structure
6487 // element) cannot appear in a lastprivate clause.
6488 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
6489 if (!DE || !isa<VarDecl>(DE->getDecl())) {
6490 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
6491 continue;
6492 }
6493 Decl *D = DE->getDecl();
6494 VarDecl *VD = cast<VarDecl>(D);
6495
6496 QualType Type = VD->getType();
6497 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6498 // It will be analyzed later.
6499 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00006500 SrcExprs.push_back(nullptr);
6501 DstExprs.push_back(nullptr);
6502 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006503 continue;
6504 }
6505
6506 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
6507 // A variable that appears in a lastprivate clause must not have an
6508 // incomplete type or a reference type.
6509 if (RequireCompleteType(ELoc, Type,
6510 diag::err_omp_lastprivate_incomplete_type)) {
6511 continue;
6512 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006513 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00006514
6515 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
6516 // in a Construct]
6517 // Variables with the predetermined data-sharing attributes may not be
6518 // listed in data-sharing attributes clauses, except for the cases
6519 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006520 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006521 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
6522 DVar.CKind != OMPC_firstprivate &&
6523 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
6524 Diag(ELoc, diag::err_omp_wrong_dsa)
6525 << getOpenMPClauseName(DVar.CKind)
6526 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006527 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006528 continue;
6529 }
6530
Alexey Bataevf29276e2014-06-18 04:14:57 +00006531 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
6532 // OpenMP [2.14.3.5, Restrictions, p.2]
6533 // A list item that is private within a parallel region, or that appears in
6534 // the reduction clause of a parallel construct, must not appear in a
6535 // lastprivate clause on a worksharing construct if any of the corresponding
6536 // worksharing regions ever binds to any of the corresponding parallel
6537 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00006538 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00006539 if (isOpenMPWorksharingDirective(CurrDir) &&
6540 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006541 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006542 if (DVar.CKind != OMPC_shared) {
6543 Diag(ELoc, diag::err_omp_required_access)
6544 << getOpenMPClauseName(OMPC_lastprivate)
6545 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006546 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006547 continue;
6548 }
6549 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00006550 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00006551 // A variable of class type (or array thereof) that appears in a
6552 // lastprivate clause requires an accessible, unambiguous default
6553 // constructor for the class type, unless the list item is also specified
6554 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00006555 // A variable of class type (or array thereof) that appears in a
6556 // lastprivate clause requires an accessible, unambiguous copy assignment
6557 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00006558 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00006559 auto *SrcVD = buildVarDecl(*this, DE->getLocStart(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006560 Type.getUnqualifiedType(), ".lastprivate.src",
6561 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00006562 auto *PseudoSrcExpr = buildDeclRefExpr(
6563 *this, SrcVD, Type.getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00006564 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006565 buildVarDecl(*this, DE->getLocStart(), Type, ".lastprivate.dst",
6566 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00006567 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006568 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00006569 // For arrays generate assignment operation for single element and replace
6570 // it by the original array element in CodeGen.
6571 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
6572 PseudoDstExpr, PseudoSrcExpr);
6573 if (AssignmentOp.isInvalid())
6574 continue;
6575 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
6576 /*DiscardedValue=*/true);
6577 if (AssignmentOp.isInvalid())
6578 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00006579
Alexey Bataev39f915b82015-05-08 10:41:21 +00006580 if (TopDVar.CKind != OMPC_firstprivate)
Alexey Bataevf29276e2014-06-18 04:14:57 +00006581 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006582 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00006583 SrcExprs.push_back(PseudoSrcExpr);
6584 DstExprs.push_back(PseudoDstExpr);
6585 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00006586 }
6587
6588 if (Vars.empty())
6589 return nullptr;
6590
6591 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00006592 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006593}
6594
Alexey Bataev758e55e2013-09-06 18:03:48 +00006595OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
6596 SourceLocation StartLoc,
6597 SourceLocation LParenLoc,
6598 SourceLocation EndLoc) {
6599 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00006600 for (auto &RefExpr : VarList) {
6601 assert(RefExpr && "NULL expr in OpenMP shared clause.");
6602 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00006603 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006604 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006605 continue;
6606 }
6607
Alexey Bataeved09d242014-05-28 05:53:51 +00006608 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00006609 // OpenMP [2.1, C/C++]
6610 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00006611 // OpenMP [2.14.3.2, Restrictions, p.1]
6612 // A variable that is part of another variable (as an array or structure
6613 // element) cannot appear in a shared unless it is a static data member
6614 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00006615 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006616 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006617 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00006618 continue;
6619 }
6620 Decl *D = DE->getDecl();
6621 VarDecl *VD = cast<VarDecl>(D);
6622
6623 QualType Type = VD->getType();
6624 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6625 // It will be analyzed later.
6626 Vars.push_back(DE);
6627 continue;
6628 }
6629
6630 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6631 // in a Construct]
6632 // Variables with the predetermined data-sharing attributes may not be
6633 // listed in data-sharing attributes clauses, except for the cases
6634 // listed below. For these exceptions only, listing a predetermined
6635 // variable in a data-sharing attribute clause is allowed and overrides
6636 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006637 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00006638 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
6639 DVar.RefExpr) {
6640 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
6641 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006642 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006643 continue;
6644 }
6645
6646 DSAStack->addDSA(VD, DE, OMPC_shared);
6647 Vars.push_back(DE);
6648 }
6649
Alexey Bataeved09d242014-05-28 05:53:51 +00006650 if (Vars.empty())
6651 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00006652
6653 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
6654}
6655
Alexey Bataevc5e02582014-06-16 07:08:35 +00006656namespace {
6657class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
6658 DSAStackTy *Stack;
6659
6660public:
6661 bool VisitDeclRefExpr(DeclRefExpr *E) {
6662 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006663 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006664 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
6665 return false;
6666 if (DVar.CKind != OMPC_unknown)
6667 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00006668 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006669 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006670 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00006671 return true;
6672 return false;
6673 }
6674 return false;
6675 }
6676 bool VisitStmt(Stmt *S) {
6677 for (auto Child : S->children()) {
6678 if (Child && Visit(Child))
6679 return true;
6680 }
6681 return false;
6682 }
Alexey Bataev23b69422014-06-18 07:08:49 +00006683 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00006684};
Alexey Bataev23b69422014-06-18 07:08:49 +00006685} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00006686
6687OMPClause *Sema::ActOnOpenMPReductionClause(
6688 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
6689 SourceLocation ColonLoc, SourceLocation EndLoc,
6690 CXXScopeSpec &ReductionIdScopeSpec,
6691 const DeclarationNameInfo &ReductionId) {
6692 // TODO: Allow scope specification search when 'declare reduction' is
6693 // supported.
6694 assert(ReductionIdScopeSpec.isEmpty() &&
6695 "No support for scoped reduction identifiers yet.");
6696
6697 auto DN = ReductionId.getName();
6698 auto OOK = DN.getCXXOverloadedOperator();
6699 BinaryOperatorKind BOK = BO_Comma;
6700
6701 // OpenMP [2.14.3.6, reduction clause]
6702 // C
6703 // reduction-identifier is either an identifier or one of the following
6704 // operators: +, -, *, &, |, ^, && and ||
6705 // C++
6706 // reduction-identifier is either an id-expression or one of the following
6707 // operators: +, -, *, &, |, ^, && and ||
6708 // FIXME: Only 'min' and 'max' identifiers are supported for now.
6709 switch (OOK) {
6710 case OO_Plus:
6711 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006712 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006713 break;
6714 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006715 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006716 break;
6717 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006718 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006719 break;
6720 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006721 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006722 break;
6723 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006724 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006725 break;
6726 case OO_AmpAmp:
6727 BOK = BO_LAnd;
6728 break;
6729 case OO_PipePipe:
6730 BOK = BO_LOr;
6731 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006732 case OO_New:
6733 case OO_Delete:
6734 case OO_Array_New:
6735 case OO_Array_Delete:
6736 case OO_Slash:
6737 case OO_Percent:
6738 case OO_Tilde:
6739 case OO_Exclaim:
6740 case OO_Equal:
6741 case OO_Less:
6742 case OO_Greater:
6743 case OO_LessEqual:
6744 case OO_GreaterEqual:
6745 case OO_PlusEqual:
6746 case OO_MinusEqual:
6747 case OO_StarEqual:
6748 case OO_SlashEqual:
6749 case OO_PercentEqual:
6750 case OO_CaretEqual:
6751 case OO_AmpEqual:
6752 case OO_PipeEqual:
6753 case OO_LessLess:
6754 case OO_GreaterGreater:
6755 case OO_LessLessEqual:
6756 case OO_GreaterGreaterEqual:
6757 case OO_EqualEqual:
6758 case OO_ExclaimEqual:
6759 case OO_PlusPlus:
6760 case OO_MinusMinus:
6761 case OO_Comma:
6762 case OO_ArrowStar:
6763 case OO_Arrow:
6764 case OO_Call:
6765 case OO_Subscript:
6766 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +00006767 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006768 case NUM_OVERLOADED_OPERATORS:
6769 llvm_unreachable("Unexpected reduction identifier");
6770 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006771 if (auto II = DN.getAsIdentifierInfo()) {
6772 if (II->isStr("max"))
6773 BOK = BO_GT;
6774 else if (II->isStr("min"))
6775 BOK = BO_LT;
6776 }
6777 break;
6778 }
6779 SourceRange ReductionIdRange;
6780 if (ReductionIdScopeSpec.isValid()) {
6781 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
6782 }
6783 ReductionIdRange.setEnd(ReductionId.getEndLoc());
6784 if (BOK == BO_Comma) {
6785 // Not allowed reduction identifier is found.
6786 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
6787 << ReductionIdRange;
6788 return nullptr;
6789 }
6790
6791 SmallVector<Expr *, 8> Vars;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006792 SmallVector<Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006793 SmallVector<Expr *, 8> LHSs;
6794 SmallVector<Expr *, 8> RHSs;
6795 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006796 for (auto RefExpr : VarList) {
6797 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
6798 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6799 // It will be analyzed later.
6800 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006801 Privates.push_back(nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006802 LHSs.push_back(nullptr);
6803 RHSs.push_back(nullptr);
6804 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006805 continue;
6806 }
6807
6808 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
6809 RefExpr->isInstantiationDependent() ||
6810 RefExpr->containsUnexpandedParameterPack()) {
6811 // It will be analyzed later.
6812 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006813 Privates.push_back(nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006814 LHSs.push_back(nullptr);
6815 RHSs.push_back(nullptr);
6816 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006817 continue;
6818 }
6819
6820 auto ELoc = RefExpr->getExprLoc();
6821 auto ERange = RefExpr->getSourceRange();
6822 // OpenMP [2.1, C/C++]
6823 // A list item is a variable or array section, subject to the restrictions
6824 // specified in Section 2.4 on page 42 and in each of the sections
6825 // describing clauses and directives for which a list appears.
6826 // OpenMP [2.14.3.3, Restrictions, p.1]
6827 // A variable that is part of another variable (as an array or
6828 // structure element) cannot appear in a private clause.
Alexey Bataeva1764212015-09-30 09:22:36 +00006829 auto *DE = dyn_cast<DeclRefExpr>(RefExpr);
6830 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr);
6831 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr);
6832 if (!ASE && !OASE && (!DE || !isa<VarDecl>(DE->getDecl()))) {
6833 Diag(ELoc, diag::err_omp_expected_var_name_or_array_item) << ERange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006834 continue;
6835 }
Alexey Bataeva1764212015-09-30 09:22:36 +00006836 QualType Type;
6837 VarDecl *VD = nullptr;
6838 if (DE) {
6839 auto D = DE->getDecl();
6840 VD = cast<VarDecl>(D);
6841 Type = VD->getType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006842 } else if (ASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00006843 Type = ASE->getType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006844 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
6845 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
6846 Base = TempASE->getBase()->IgnoreParenImpCasts();
6847 DE = dyn_cast<DeclRefExpr>(Base);
6848 if (DE)
6849 VD = dyn_cast<VarDecl>(DE->getDecl());
6850 if (!VD) {
6851 Diag(Base->getExprLoc(), diag::err_omp_expected_base_var_name)
6852 << 0 << Base->getSourceRange();
6853 continue;
6854 }
6855 } else if (OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00006856 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
6857 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
6858 Type = ATy->getElementType();
6859 else
6860 Type = BaseType->getPointeeType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006861 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
6862 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
6863 Base = TempOASE->getBase()->IgnoreParenImpCasts();
6864 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
6865 Base = TempASE->getBase()->IgnoreParenImpCasts();
6866 DE = dyn_cast<DeclRefExpr>(Base);
6867 if (DE)
6868 VD = dyn_cast<VarDecl>(DE->getDecl());
6869 if (!VD) {
6870 Diag(Base->getExprLoc(), diag::err_omp_expected_base_var_name)
6871 << 1 << Base->getSourceRange();
6872 continue;
6873 }
Alexey Bataeva1764212015-09-30 09:22:36 +00006874 }
6875
Alexey Bataevc5e02582014-06-16 07:08:35 +00006876 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
6877 // A variable that appears in a private clause must not have an incomplete
6878 // type or a reference type.
6879 if (RequireCompleteType(ELoc, Type,
6880 diag::err_omp_reduction_incomplete_type))
6881 continue;
6882 // OpenMP [2.14.3.6, reduction clause, Restrictions]
6883 // Arrays may not appear in a reduction clause.
6884 if (Type.getNonReferenceType()->isArrayType()) {
6885 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006886 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00006887 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
6888 VarDecl::DeclarationOnly;
6889 Diag(VD->getLocation(),
6890 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6891 << VD;
6892 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00006893 continue;
6894 }
6895 // OpenMP [2.14.3.6, reduction clause, Restrictions]
6896 // A list item that appears in a reduction clause must not be
6897 // const-qualified.
6898 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataeva1764212015-09-30 09:22:36 +00006899 Diag(ELoc, diag::err_omp_const_reduction_list_item)
Alexey Bataevc5e02582014-06-16 07:08:35 +00006900 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006901 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00006902 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
6903 VarDecl::DeclarationOnly;
6904 Diag(VD->getLocation(),
6905 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6906 << VD;
6907 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00006908 continue;
6909 }
6910 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
6911 // If a list-item is a reference type then it must bind to the same object
6912 // for all threads of the team.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006913 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00006914 VarDecl *VDDef = VD->getDefinition();
6915 if (Type->isReferenceType() && VDDef) {
6916 DSARefChecker Check(DSAStack);
6917 if (Check.Visit(VDDef->getInit())) {
6918 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
6919 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
6920 continue;
6921 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00006922 }
6923 }
6924 // OpenMP [2.14.3.6, reduction clause, Restrictions]
6925 // The type of a list item that appears in a reduction clause must be valid
6926 // for the reduction-identifier. For a max or min reduction in C, the type
6927 // of the list item must be an allowed arithmetic data type: char, int,
6928 // float, double, or _Bool, possibly modified with long, short, signed, or
6929 // unsigned. For a max or min reduction in C++, the type of the list item
6930 // must be an allowed arithmetic data type: char, wchar_t, int, float,
6931 // double, or bool, possibly modified with long, short, signed, or unsigned.
6932 if ((BOK == BO_GT || BOK == BO_LT) &&
6933 !(Type->isScalarType() ||
6934 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
6935 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
6936 << getLangOpts().CPlusPlus;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006937 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00006938 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
6939 VarDecl::DeclarationOnly;
6940 Diag(VD->getLocation(),
6941 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6942 << VD;
6943 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00006944 continue;
6945 }
6946 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
6947 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
6948 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006949 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00006950 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
6951 VarDecl::DeclarationOnly;
6952 Diag(VD->getLocation(),
6953 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6954 << VD;
6955 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00006956 continue;
6957 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00006958 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
6959 // in a Construct]
6960 // Variables with the predetermined data-sharing attributes may not be
6961 // listed in data-sharing attributes clauses, except for the cases
6962 // listed below. For these exceptions only, listing a predetermined
6963 // variable in a data-sharing attribute clause is allowed and overrides
6964 // the variable's predetermined data-sharing attributes.
6965 // OpenMP [2.14.3.6, Restrictions, p.3]
6966 // Any number of reduction clauses can be specified on the directive,
6967 // but a list item can appear only once in the reduction clauses for that
6968 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +00006969 DSAStackTy::DSAVarData DVar;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006970 DVar = DSAStack->getTopDSA(VD, false);
6971 if (DVar.CKind == OMPC_reduction) {
6972 Diag(ELoc, diag::err_omp_once_referenced)
6973 << getOpenMPClauseName(OMPC_reduction);
6974 if (DVar.RefExpr) {
6975 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006976 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006977 } else if (DVar.CKind != OMPC_unknown) {
6978 Diag(ELoc, diag::err_omp_wrong_dsa)
6979 << getOpenMPClauseName(DVar.CKind)
6980 << getOpenMPClauseName(OMPC_reduction);
6981 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6982 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006983 }
6984
6985 // OpenMP [2.14.3.6, Restrictions, p.1]
6986 // A list item that appears in a reduction clause of a worksharing
6987 // construct must be shared in the parallel regions to which any of the
6988 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006989 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
6990 if (isOpenMPWorksharingDirective(CurrDir) &&
6991 !isOpenMPParallelDirective(CurrDir)) {
6992 DVar = DSAStack->getImplicitDSA(VD, true);
6993 if (DVar.CKind != OMPC_shared) {
6994 Diag(ELoc, diag::err_omp_required_access)
6995 << getOpenMPClauseName(OMPC_reduction)
6996 << getOpenMPClauseName(OMPC_shared);
6997 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6998 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +00006999 }
7000 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007001
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007002 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007003 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
7004 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
7005 auto *RHSVD = buildVarDecl(*this, ELoc, Type, VD->getName(),
7006 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
7007 auto PrivateTy = Type;
7008 if (OASE) {
7009 // For array sections only:
7010 // Create pseudo array type for private copy. The size for this array will
7011 // be generated during codegen.
7012 // For array subscripts or single variables Private Ty is the same as Type
7013 // (type of the variable or single array element).
7014 PrivateTy = Context.getVariableArrayType(
7015 Type, new (Context) OpaqueValueExpr(SourceLocation(),
7016 Context.getSizeType(), VK_RValue),
7017 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
7018 }
7019 // Private copy.
7020 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, VD->getName(),
7021 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007022 // Add initializer for private variable.
7023 Expr *Init = nullptr;
7024 switch (BOK) {
7025 case BO_Add:
7026 case BO_Xor:
7027 case BO_Or:
7028 case BO_LOr:
7029 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
7030 if (Type->isScalarType() || Type->isAnyComplexType()) {
7031 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00007032 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007033 break;
7034 case BO_Mul:
7035 case BO_LAnd:
7036 if (Type->isScalarType() || Type->isAnyComplexType()) {
7037 // '*' and '&&' reduction ops - initializer is '1'.
7038 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
7039 }
7040 break;
7041 case BO_And: {
7042 // '&' reduction op - initializer is '~0'.
7043 QualType OrigType = Type;
7044 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) {
7045 Type = ComplexTy->getElementType();
7046 }
7047 if (Type->isRealFloatingType()) {
7048 llvm::APFloat InitValue =
7049 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
7050 /*isIEEE=*/true);
7051 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
7052 Type, ELoc);
7053 } else if (Type->isScalarType()) {
7054 auto Size = Context.getTypeSize(Type);
7055 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
7056 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
7057 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
7058 }
7059 if (Init && OrigType->isAnyComplexType()) {
7060 // Init = 0xFFFF + 0xFFFFi;
7061 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
7062 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
7063 }
7064 Type = OrigType;
7065 break;
7066 }
7067 case BO_LT:
7068 case BO_GT: {
7069 // 'min' reduction op - initializer is 'Largest representable number in
7070 // the reduction list item type'.
7071 // 'max' reduction op - initializer is 'Least representable number in
7072 // the reduction list item type'.
7073 if (Type->isIntegerType() || Type->isPointerType()) {
7074 bool IsSigned = Type->hasSignedIntegerRepresentation();
7075 auto Size = Context.getTypeSize(Type);
7076 QualType IntTy =
7077 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
7078 llvm::APInt InitValue =
7079 (BOK != BO_LT)
7080 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
7081 : llvm::APInt::getMinValue(Size)
7082 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
7083 : llvm::APInt::getMaxValue(Size);
7084 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
7085 if (Type->isPointerType()) {
7086 // Cast to pointer type.
7087 auto CastExpr = BuildCStyleCastExpr(
7088 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
7089 SourceLocation(), Init);
7090 if (CastExpr.isInvalid())
7091 continue;
7092 Init = CastExpr.get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00007093 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007094 } else if (Type->isRealFloatingType()) {
7095 llvm::APFloat InitValue = llvm::APFloat::getLargest(
7096 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
7097 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
7098 Type, ELoc);
7099 }
7100 break;
7101 }
7102 case BO_PtrMemD:
7103 case BO_PtrMemI:
7104 case BO_MulAssign:
7105 case BO_Div:
7106 case BO_Rem:
7107 case BO_Sub:
7108 case BO_Shl:
7109 case BO_Shr:
7110 case BO_LE:
7111 case BO_GE:
7112 case BO_EQ:
7113 case BO_NE:
7114 case BO_AndAssign:
7115 case BO_XorAssign:
7116 case BO_OrAssign:
7117 case BO_Assign:
7118 case BO_AddAssign:
7119 case BO_SubAssign:
7120 case BO_DivAssign:
7121 case BO_RemAssign:
7122 case BO_ShlAssign:
7123 case BO_ShrAssign:
7124 case BO_Comma:
7125 llvm_unreachable("Unexpected reduction operation");
7126 }
7127 if (Init) {
7128 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
7129 /*TypeMayContainAuto=*/false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007130 } else
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007131 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007132 if (!RHSVD->hasInit()) {
7133 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
7134 << ReductionIdRange;
Alexey Bataeva1764212015-09-30 09:22:36 +00007135 if (VD) {
7136 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7137 VarDecl::DeclarationOnly;
7138 Diag(VD->getLocation(),
7139 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7140 << VD;
7141 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007142 continue;
7143 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007144 // Store initializer for single element in private copy. Will be used during
7145 // codegen.
7146 PrivateVD->setInit(RHSVD->getInit());
7147 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataev39f915b82015-05-08 10:41:21 +00007148 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
7149 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007150 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007151 ExprResult ReductionOp =
7152 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
7153 LHSDRE, RHSDRE);
7154 if (ReductionOp.isUsable()) {
Alexey Bataev69a47792015-05-07 03:54:03 +00007155 if (BOK != BO_LT && BOK != BO_GT) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007156 ReductionOp =
7157 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
7158 BO_Assign, LHSDRE, ReductionOp.get());
7159 } else {
7160 auto *ConditionalOp = new (Context) ConditionalOperator(
7161 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
7162 RHSDRE, Type, VK_LValue, OK_Ordinary);
7163 ReductionOp =
7164 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
7165 BO_Assign, LHSDRE, ConditionalOp);
7166 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007167 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00007168 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007169 if (ReductionOp.isInvalid())
7170 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007171
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007172 DSAStack->addDSA(VD, DE, OMPC_reduction);
Alexey Bataeva1764212015-09-30 09:22:36 +00007173 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007174 Privates.push_back(PrivateDRE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007175 LHSs.push_back(LHSDRE);
7176 RHSs.push_back(RHSDRE);
7177 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00007178 }
7179
7180 if (Vars.empty())
7181 return nullptr;
7182
7183 return OMPReductionClause::Create(
7184 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007185 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
7186 LHSs, RHSs, ReductionOps);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007187}
7188
Alexey Bataev182227b2015-08-20 10:54:39 +00007189OMPClause *Sema::ActOnOpenMPLinearClause(
7190 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
7191 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
7192 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00007193 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007194 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00007195 SmallVector<Expr *, 8> Inits;
Alexey Bataev182227b2015-08-20 10:54:39 +00007196 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
7197 LinKind == OMPC_LINEAR_unknown) {
7198 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
7199 LinKind = OMPC_LINEAR_val;
7200 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007201 for (auto &RefExpr : VarList) {
7202 assert(RefExpr && "NULL expr in OpenMP linear clause.");
7203 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00007204 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007205 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007206 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00007207 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00007208 continue;
7209 }
7210
7211 // OpenMP [2.14.3.7, linear clause]
7212 // A list item that appears in a linear clause is subject to the private
7213 // clause semantics described in Section 2.14.3.3 on page 159 except as
7214 // noted. In addition, the value of the new list item on each iteration
7215 // of the associated loop(s) corresponds to the value of the original
7216 // list item before entering the construct plus the logical number of
7217 // the iteration times linear-step.
7218
Alexey Bataeved09d242014-05-28 05:53:51 +00007219 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00007220 // OpenMP [2.1, C/C++]
7221 // A list item is a variable name.
7222 // OpenMP [2.14.3.3, Restrictions, p.1]
7223 // A variable that is part of another variable (as an array or
7224 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00007225 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00007226 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00007227 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00007228 continue;
7229 }
7230
7231 VarDecl *VD = cast<VarDecl>(DE->getDecl());
7232
7233 // OpenMP [2.14.3.7, linear clause]
7234 // A list-item cannot appear in more than one linear clause.
7235 // A list-item that appears in a linear clause cannot appear in any
7236 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007237 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00007238 if (DVar.RefExpr) {
7239 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7240 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007241 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00007242 continue;
7243 }
7244
7245 QualType QType = VD->getType();
7246 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
7247 // It will be analyzed later.
7248 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007249 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00007250 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00007251 continue;
7252 }
7253
7254 // A variable must not have an incomplete type or a reference type.
7255 if (RequireCompleteType(ELoc, QType,
7256 diag::err_omp_linear_incomplete_type)) {
7257 continue;
7258 }
Alexey Bataev1185e192015-08-20 12:15:57 +00007259 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
7260 !QType->isReferenceType()) {
7261 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
7262 << QType << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
7263 continue;
7264 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007265 QType = QType.getNonReferenceType();
Alexander Musman8dba6642014-04-22 13:09:42 +00007266
7267 // A list item must not be const-qualified.
7268 if (QType.isConstant(Context)) {
7269 Diag(ELoc, diag::err_omp_const_variable)
7270 << getOpenMPClauseName(OMPC_linear);
7271 bool IsDecl =
7272 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7273 Diag(VD->getLocation(),
7274 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7275 << VD;
7276 continue;
7277 }
7278
7279 // A list item must be of integral or pointer type.
7280 QType = QType.getUnqualifiedType().getCanonicalType();
7281 const Type *Ty = QType.getTypePtrOrNull();
7282 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
7283 !Ty->isPointerType())) {
7284 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
7285 bool IsDecl =
7286 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7287 Diag(VD->getLocation(),
7288 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7289 << VD;
7290 continue;
7291 }
7292
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007293 // Build private copy of original var.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007294 auto *Private = buildVarDecl(*this, ELoc, QType, VD->getName(),
7295 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007296 auto *PrivateRef = buildDeclRefExpr(
7297 *this, Private, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman3276a272015-03-21 10:12:56 +00007298 // Build var to save initial value.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007299 VarDecl *Init = buildVarDecl(*this, ELoc, QType, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00007300 Expr *InitExpr;
7301 if (LinKind == OMPC_LINEAR_uval)
7302 InitExpr = VD->getInit();
7303 else
7304 InitExpr = DE;
7305 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexander Musman3276a272015-03-21 10:12:56 +00007306 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007307 auto InitRef = buildDeclRefExpr(
7308 *this, Init, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman8dba6642014-04-22 13:09:42 +00007309 DSAStack->addDSA(VD, DE, OMPC_linear);
7310 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007311 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00007312 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00007313 }
7314
7315 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007316 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00007317
7318 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00007319 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00007320 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
7321 !Step->isInstantiationDependent() &&
7322 !Step->containsUnexpandedParameterPack()) {
7323 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00007324 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00007325 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007326 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00007327 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00007328
Alexander Musman3276a272015-03-21 10:12:56 +00007329 // Build var to save the step value.
7330 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007331 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00007332 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007333 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00007334 ExprResult CalcStep =
7335 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007336 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00007337
Alexander Musman8dba6642014-04-22 13:09:42 +00007338 // Warn about zero linear step (it would be probably better specified as
7339 // making corresponding variables 'const').
7340 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00007341 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
7342 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00007343 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
7344 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00007345 if (!IsConstant && CalcStep.isUsable()) {
7346 // Calculate the step beforehand instead of doing this on each iteration.
7347 // (This is not used if the number of iterations may be kfold-ed).
7348 CalcStepExpr = CalcStep.get();
7349 }
Alexander Musman8dba6642014-04-22 13:09:42 +00007350 }
7351
Alexey Bataev182227b2015-08-20 10:54:39 +00007352 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
7353 ColonLoc, EndLoc, Vars, Privates, Inits,
7354 StepExpr, CalcStepExpr);
Alexander Musman3276a272015-03-21 10:12:56 +00007355}
7356
7357static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
7358 Expr *NumIterations, Sema &SemaRef,
7359 Scope *S) {
7360 // Walk the vars and build update/final expressions for the CodeGen.
7361 SmallVector<Expr *, 8> Updates;
7362 SmallVector<Expr *, 8> Finals;
7363 Expr *Step = Clause.getStep();
7364 Expr *CalcStep = Clause.getCalcStep();
7365 // OpenMP [2.14.3.7, linear clause]
7366 // If linear-step is not specified it is assumed to be 1.
7367 if (Step == nullptr)
7368 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
7369 else if (CalcStep)
7370 Step = cast<BinaryOperator>(CalcStep)->getLHS();
7371 bool HasErrors = false;
7372 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007373 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00007374 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00007375 for (auto &RefExpr : Clause.varlists()) {
7376 Expr *InitExpr = *CurInit;
7377
7378 // Build privatized reference to the current linear var.
7379 auto DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00007380 Expr *CapturedRef;
7381 if (LinKind == OMPC_LINEAR_uval)
7382 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
7383 else
7384 CapturedRef =
7385 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
7386 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
7387 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00007388
7389 // Build update: Var = InitExpr + IV * Step
7390 ExprResult Update =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007391 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
Alexander Musman3276a272015-03-21 10:12:56 +00007392 InitExpr, IV, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007393 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
7394 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00007395
7396 // Build final: Var = InitExpr + NumIterations * Step
7397 ExprResult Final =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007398 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
Alexey Bataev39f915b82015-05-08 10:41:21 +00007399 InitExpr, NumIterations, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007400 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
7401 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00007402 if (!Update.isUsable() || !Final.isUsable()) {
7403 Updates.push_back(nullptr);
7404 Finals.push_back(nullptr);
7405 HasErrors = true;
7406 } else {
7407 Updates.push_back(Update.get());
7408 Finals.push_back(Final.get());
7409 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007410 ++CurInit, ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00007411 }
7412 Clause.setUpdates(Updates);
7413 Clause.setFinals(Finals);
7414 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00007415}
7416
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007417OMPClause *Sema::ActOnOpenMPAlignedClause(
7418 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
7419 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
7420
7421 SmallVector<Expr *, 8> Vars;
7422 for (auto &RefExpr : VarList) {
7423 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
7424 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7425 // It will be analyzed later.
7426 Vars.push_back(RefExpr);
7427 continue;
7428 }
7429
7430 SourceLocation ELoc = RefExpr->getExprLoc();
7431 // OpenMP [2.1, C/C++]
7432 // A list item is a variable name.
7433 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
7434 if (!DE || !isa<VarDecl>(DE->getDecl())) {
7435 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
7436 continue;
7437 }
7438
7439 VarDecl *VD = cast<VarDecl>(DE->getDecl());
7440
7441 // OpenMP [2.8.1, simd construct, Restrictions]
7442 // The type of list items appearing in the aligned clause must be
7443 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007444 QualType QType = VD->getType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007445 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007446 const Type *Ty = QType.getTypePtrOrNull();
7447 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
7448 !Ty->isPointerType())) {
7449 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
7450 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
7451 bool IsDecl =
7452 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7453 Diag(VD->getLocation(),
7454 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7455 << VD;
7456 continue;
7457 }
7458
7459 // OpenMP [2.8.1, simd construct, Restrictions]
7460 // A list-item cannot appear in more than one aligned clause.
7461 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
7462 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
7463 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
7464 << getOpenMPClauseName(OMPC_aligned);
7465 continue;
7466 }
7467
7468 Vars.push_back(DE);
7469 }
7470
7471 // OpenMP [2.8.1, simd construct, Description]
7472 // The parameter of the aligned clause, alignment, must be a constant
7473 // positive integer expression.
7474 // If no optional parameter is specified, implementation-defined default
7475 // alignments for SIMD instructions on the target platforms are assumed.
7476 if (Alignment != nullptr) {
7477 ExprResult AlignResult =
7478 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
7479 if (AlignResult.isInvalid())
7480 return nullptr;
7481 Alignment = AlignResult.get();
7482 }
7483 if (Vars.empty())
7484 return nullptr;
7485
7486 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
7487 EndLoc, Vars, Alignment);
7488}
7489
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007490OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
7491 SourceLocation StartLoc,
7492 SourceLocation LParenLoc,
7493 SourceLocation EndLoc) {
7494 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007495 SmallVector<Expr *, 8> SrcExprs;
7496 SmallVector<Expr *, 8> DstExprs;
7497 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00007498 for (auto &RefExpr : VarList) {
7499 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
7500 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007501 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007502 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007503 SrcExprs.push_back(nullptr);
7504 DstExprs.push_back(nullptr);
7505 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007506 continue;
7507 }
7508
Alexey Bataeved09d242014-05-28 05:53:51 +00007509 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007510 // OpenMP [2.1, C/C++]
7511 // A list item is a variable name.
7512 // OpenMP [2.14.4.1, Restrictions, p.1]
7513 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00007514 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007515 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00007516 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007517 continue;
7518 }
7519
7520 Decl *D = DE->getDecl();
7521 VarDecl *VD = cast<VarDecl>(D);
7522
7523 QualType Type = VD->getType();
7524 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
7525 // It will be analyzed later.
7526 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007527 SrcExprs.push_back(nullptr);
7528 DstExprs.push_back(nullptr);
7529 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007530 continue;
7531 }
7532
7533 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
7534 // A list item that appears in a copyin clause must be threadprivate.
7535 if (!DSAStack->isThreadPrivate(VD)) {
7536 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00007537 << getOpenMPClauseName(OMPC_copyin)
7538 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007539 continue;
7540 }
7541
7542 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
7543 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00007544 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007545 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007546 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007547 auto *SrcVD =
7548 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
7549 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007550 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007551 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
7552 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007553 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
7554 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007555 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007556 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007557 // For arrays generate assignment operation for single element and replace
7558 // it by the original array element in CodeGen.
7559 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
7560 PseudoDstExpr, PseudoSrcExpr);
7561 if (AssignmentOp.isInvalid())
7562 continue;
7563 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
7564 /*DiscardedValue=*/true);
7565 if (AssignmentOp.isInvalid())
7566 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007567
7568 DSAStack->addDSA(VD, DE, OMPC_copyin);
7569 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007570 SrcExprs.push_back(PseudoSrcExpr);
7571 DstExprs.push_back(PseudoDstExpr);
7572 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007573 }
7574
Alexey Bataeved09d242014-05-28 05:53:51 +00007575 if (Vars.empty())
7576 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007577
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007578 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
7579 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007580}
7581
Alexey Bataevbae9a792014-06-27 10:37:06 +00007582OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
7583 SourceLocation StartLoc,
7584 SourceLocation LParenLoc,
7585 SourceLocation EndLoc) {
7586 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00007587 SmallVector<Expr *, 8> SrcExprs;
7588 SmallVector<Expr *, 8> DstExprs;
7589 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00007590 for (auto &RefExpr : VarList) {
7591 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
7592 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7593 // It will be analyzed later.
7594 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00007595 SrcExprs.push_back(nullptr);
7596 DstExprs.push_back(nullptr);
7597 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00007598 continue;
7599 }
7600
7601 SourceLocation ELoc = RefExpr->getExprLoc();
7602 // OpenMP [2.1, C/C++]
7603 // A list item is a variable name.
7604 // OpenMP [2.14.4.1, Restrictions, p.1]
7605 // A list item that appears in a copyin clause must be threadprivate.
7606 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
7607 if (!DE || !isa<VarDecl>(DE->getDecl())) {
7608 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
7609 continue;
7610 }
7611
7612 Decl *D = DE->getDecl();
7613 VarDecl *VD = cast<VarDecl>(D);
7614
7615 QualType Type = VD->getType();
7616 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
7617 // It will be analyzed later.
7618 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00007619 SrcExprs.push_back(nullptr);
7620 DstExprs.push_back(nullptr);
7621 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00007622 continue;
7623 }
7624
7625 // OpenMP [2.14.4.2, Restrictions, p.2]
7626 // A list item that appears in a copyprivate clause may not appear in a
7627 // private or firstprivate clause on the single construct.
7628 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007629 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00007630 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
7631 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00007632 Diag(ELoc, diag::err_omp_wrong_dsa)
7633 << getOpenMPClauseName(DVar.CKind)
7634 << getOpenMPClauseName(OMPC_copyprivate);
7635 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7636 continue;
7637 }
7638
7639 // OpenMP [2.11.4.2, Restrictions, p.1]
7640 // All list items that appear in a copyprivate clause must be either
7641 // threadprivate or private in the enclosing context.
7642 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007643 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00007644 if (DVar.CKind == OMPC_shared) {
7645 Diag(ELoc, diag::err_omp_required_access)
7646 << getOpenMPClauseName(OMPC_copyprivate)
7647 << "threadprivate or private in the enclosing context";
7648 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7649 continue;
7650 }
7651 }
7652 }
7653
Alexey Bataev7a3e5852015-05-19 08:19:24 +00007654 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007655 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00007656 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007657 << getOpenMPClauseName(OMPC_copyprivate) << Type
7658 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00007659 bool IsDecl =
7660 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7661 Diag(VD->getLocation(),
7662 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7663 << VD;
7664 continue;
7665 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007666
Alexey Bataevbae9a792014-06-27 10:37:06 +00007667 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
7668 // A variable of class type (or array thereof) that appears in a
7669 // copyin clause requires an accessible, unambiguous copy assignment
7670 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007671 Type = Context.getBaseElementType(Type.getNonReferenceType())
7672 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +00007673 auto *SrcVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007674 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.src",
7675 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00007676 auto *PseudoSrcExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007677 buildDeclRefExpr(*this, SrcVD, Type, DE->getExprLoc());
Alexey Bataev420d45b2015-04-14 05:11:24 +00007678 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007679 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.dst",
7680 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00007681 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007682 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataeva63048e2015-03-23 06:18:07 +00007683 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
7684 PseudoDstExpr, PseudoSrcExpr);
7685 if (AssignmentOp.isInvalid())
7686 continue;
7687 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
7688 /*DiscardedValue=*/true);
7689 if (AssignmentOp.isInvalid())
7690 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00007691
7692 // No need to mark vars as copyprivate, they are already threadprivate or
7693 // implicitly private.
7694 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00007695 SrcExprs.push_back(PseudoSrcExpr);
7696 DstExprs.push_back(PseudoDstExpr);
7697 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +00007698 }
7699
7700 if (Vars.empty())
7701 return nullptr;
7702
Alexey Bataeva63048e2015-03-23 06:18:07 +00007703 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
7704 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00007705}
7706
Alexey Bataev6125da92014-07-21 11:26:11 +00007707OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
7708 SourceLocation StartLoc,
7709 SourceLocation LParenLoc,
7710 SourceLocation EndLoc) {
7711 if (VarList.empty())
7712 return nullptr;
7713
7714 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
7715}
Alexey Bataevdea47612014-07-23 07:46:59 +00007716
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007717OMPClause *
7718Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
7719 SourceLocation DepLoc, SourceLocation ColonLoc,
7720 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
7721 SourceLocation LParenLoc, SourceLocation EndLoc) {
7722 if (DepKind == OMPC_DEPEND_unknown) {
7723 std::string Values;
7724 std::string Sep(", ");
7725 for (unsigned i = 0; i < OMPC_DEPEND_unknown; ++i) {
7726 Values += "'";
7727 Values += getOpenMPSimpleClauseTypeName(OMPC_depend, i);
7728 Values += "'";
7729 switch (i) {
7730 case OMPC_DEPEND_unknown - 2:
7731 Values += " or ";
7732 break;
7733 case OMPC_DEPEND_unknown - 1:
7734 break;
7735 default:
7736 Values += Sep;
7737 break;
7738 }
7739 }
7740 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
7741 << Values << getOpenMPClauseName(OMPC_depend);
7742 return nullptr;
7743 }
7744 SmallVector<Expr *, 8> Vars;
7745 for (auto &RefExpr : VarList) {
7746 assert(RefExpr && "NULL expr in OpenMP shared clause.");
7747 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7748 // It will be analyzed later.
7749 Vars.push_back(RefExpr);
7750 continue;
7751 }
7752
7753 SourceLocation ELoc = RefExpr->getExprLoc();
7754 // OpenMP [2.11.1.1, Restrictions, p.3]
7755 // A variable that is part of another variable (such as a field of a
7756 // structure) but is not an array element or an array section cannot appear
7757 // in a depend clause.
7758 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
Alexey Bataev1a3320e2015-08-25 14:24:04 +00007759 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
7760 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
7761 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
7762 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
7763 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007764 (ASE && !ASE->getBase()->getType()->isAnyPointerType() &&
7765 !ASE->getBase()->getType()->isArrayType())) {
7766 Diag(ELoc, diag::err_omp_expected_var_name_or_array_item)
7767 << RefExpr->getSourceRange();
7768 continue;
7769 }
7770
7771 Vars.push_back(RefExpr->IgnoreParenImpCasts());
7772 }
7773
7774 if (Vars.empty())
7775 return nullptr;
7776
7777 return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
7778 DepLoc, ColonLoc, Vars);
7779}
Michael Wonge710d542015-08-07 16:16:36 +00007780
7781OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
7782 SourceLocation LParenLoc,
7783 SourceLocation EndLoc) {
7784 Expr *ValExpr = Device;
Michael Wonge710d542015-08-07 16:16:36 +00007785
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007786 // OpenMP [2.9.1, Restrictions]
7787 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00007788 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
7789 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007790 return nullptr;
7791
Michael Wonge710d542015-08-07 16:16:36 +00007792 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
7793}
Kelvin Li0bff7af2015-11-23 05:32:03 +00007794
7795static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
7796 DSAStackTy *Stack, CXXRecordDecl *RD) {
7797 if (!RD || RD->isInvalidDecl())
7798 return true;
7799
7800 auto QTy = SemaRef.Context.getRecordType(RD);
7801 if (RD->isDynamicClass()) {
7802 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
7803 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
7804 return false;
7805 }
7806 auto *DC = RD;
7807 bool IsCorrect = true;
7808 for (auto *I : DC->decls()) {
7809 if (I) {
7810 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
7811 if (MD->isStatic()) {
7812 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
7813 SemaRef.Diag(MD->getLocation(),
7814 diag::note_omp_static_member_in_target);
7815 IsCorrect = false;
7816 }
7817 } else if (auto *VD = dyn_cast<VarDecl>(I)) {
7818 if (VD->isStaticDataMember()) {
7819 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
7820 SemaRef.Diag(VD->getLocation(),
7821 diag::note_omp_static_member_in_target);
7822 IsCorrect = false;
7823 }
7824 }
7825 }
7826 }
7827
7828 for (auto &I : RD->bases()) {
7829 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
7830 I.getType()->getAsCXXRecordDecl()))
7831 IsCorrect = false;
7832 }
7833 return IsCorrect;
7834}
7835
7836static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
7837 DSAStackTy *Stack, QualType QTy) {
7838 NamedDecl *ND;
7839 if (QTy->isIncompleteType(&ND)) {
7840 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
7841 return false;
7842 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
7843 if (!RD->isInvalidDecl() &&
7844 !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
7845 return false;
7846 }
7847 return true;
7848}
7849
7850OMPClause *Sema::ActOnOpenMPMapClause(
7851 OpenMPMapClauseKind MapTypeModifier, OpenMPMapClauseKind MapType,
7852 SourceLocation MapLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VarList,
7853 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
7854 SmallVector<Expr *, 4> Vars;
7855
7856 for (auto &RE : VarList) {
7857 assert(RE && "Null expr in omp map");
7858 if (isa<DependentScopeDeclRefExpr>(RE)) {
7859 // It will be analyzed later.
7860 Vars.push_back(RE);
7861 continue;
7862 }
7863 SourceLocation ELoc = RE->getExprLoc();
7864
7865 // OpenMP [2.14.5, Restrictions]
7866 // A variable that is part of another variable (such as field of a
7867 // structure) but is not an array element or an array section cannot appear
7868 // in a map clause.
7869 auto *VE = RE->IgnoreParenLValueCasts();
7870
7871 if (VE->isValueDependent() || VE->isTypeDependent() ||
7872 VE->isInstantiationDependent() ||
7873 VE->containsUnexpandedParameterPack()) {
7874 // It will be analyzed later.
7875 Vars.push_back(RE);
7876 continue;
7877 }
7878
7879 auto *SimpleExpr = RE->IgnoreParenCasts();
7880 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
7881 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
7882 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
7883
7884 if (!RE->IgnoreParenImpCasts()->isLValue() ||
7885 (!OASE && !ASE && !DE) ||
7886 (DE && !isa<VarDecl>(DE->getDecl())) ||
7887 (ASE && !ASE->getBase()->getType()->isAnyPointerType() &&
7888 !ASE->getBase()->getType()->isArrayType())) {
7889 Diag(ELoc, diag::err_omp_expected_var_name_or_array_item)
7890 << RE->getSourceRange();
7891 continue;
7892 }
7893
7894 Decl *D = nullptr;
7895 if (DE) {
7896 D = DE->getDecl();
7897 } else if (ASE) {
7898 auto *B = ASE->getBase()->IgnoreParenCasts();
7899 D = dyn_cast<DeclRefExpr>(B)->getDecl();
7900 } else if (OASE) {
7901 auto *B = OASE->getBase();
7902 D = dyn_cast<DeclRefExpr>(B)->getDecl();
7903 }
7904 assert(D && "Null decl on map clause.");
7905 auto *VD = cast<VarDecl>(D);
7906
7907 // OpenMP [2.14.5, Restrictions, p.8]
7908 // threadprivate variables cannot appear in a map clause.
7909 if (DSAStack->isThreadPrivate(VD)) {
7910 auto DVar = DSAStack->getTopDSA(VD, false);
7911 Diag(ELoc, diag::err_omp_threadprivate_in_map);
7912 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7913 continue;
7914 }
7915
7916 // OpenMP [2.14.5, Restrictions, p.2]
7917 // At most one list item can be an array item derived from a given variable
7918 // in map clauses of the same construct.
7919 // OpenMP [2.14.5, Restrictions, p.3]
7920 // List items of map clauses in the same construct must not share original
7921 // storage.
7922 // OpenMP [2.14.5, Restrictions, C/C++, p.2]
7923 // A variable for which the type is pointer, reference to array, or
7924 // reference to pointer and an array section derived from that variable
7925 // must not appear as list items of map clauses of the same construct.
7926 DSAStackTy::MapInfo MI = DSAStack->IsMappedInCurrentRegion(VD);
7927 if (MI.RefExpr) {
7928 Diag(ELoc, diag::err_omp_map_shared_storage) << ELoc;
7929 Diag(MI.RefExpr->getExprLoc(), diag::note_used_here)
7930 << MI.RefExpr->getSourceRange();
7931 continue;
7932 }
7933
7934 // OpenMP [2.14.5, Restrictions, C/C++, p.3,4]
7935 // A variable for which the type is pointer, reference to array, or
7936 // reference to pointer must not appear as a list item if the enclosing
7937 // device data environment already contains an array section derived from
7938 // that variable.
7939 // An array section derived from a variable for which the type is pointer,
7940 // reference to array, or reference to pointer must not appear as a list
7941 // item if the enclosing device data environment already contains that
7942 // variable.
7943 QualType Type = VD->getType();
7944 MI = DSAStack->getMapInfoForVar(VD);
7945 if (MI.RefExpr && (isa<DeclRefExpr>(MI.RefExpr->IgnoreParenLValueCasts()) !=
7946 isa<DeclRefExpr>(VE)) &&
7947 (Type->isPointerType() || Type->isReferenceType())) {
7948 Diag(ELoc, diag::err_omp_map_shared_storage) << ELoc;
7949 Diag(MI.RefExpr->getExprLoc(), diag::note_used_here)
7950 << MI.RefExpr->getSourceRange();
7951 continue;
7952 }
7953
7954 // OpenMP [2.14.5, Restrictions, C/C++, p.7]
7955 // A list item must have a mappable type.
7956 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), *this,
7957 DSAStack, Type))
7958 continue;
7959
7960 Vars.push_back(RE);
7961 MI.RefExpr = RE;
7962 DSAStack->addMapInfoForVar(VD, MI);
7963 }
7964 if (Vars.empty())
7965 return nullptr;
7966
7967 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
7968 MapTypeModifier, MapType, MapLoc);
7969}
Kelvin Li099bb8c2015-11-24 20:50:12 +00007970
7971OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
7972 SourceLocation StartLoc,
7973 SourceLocation LParenLoc,
7974 SourceLocation EndLoc) {
7975 Expr *ValExpr = NumTeams;
Kelvin Li099bb8c2015-11-24 20:50:12 +00007976
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007977 // OpenMP [teams Constrcut, Restrictions]
7978 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00007979 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
7980 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007981 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +00007982
7983 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
7984}
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007985
7986OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
7987 SourceLocation StartLoc,
7988 SourceLocation LParenLoc,
7989 SourceLocation EndLoc) {
7990 Expr *ValExpr = ThreadLimit;
7991
7992 // OpenMP [teams Constrcut, Restrictions]
7993 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00007994 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
7995 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007996 return nullptr;
7997
7998 return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc,
7999 EndLoc);
8000}
Alexey Bataeva0569352015-12-01 10:17:31 +00008001
8002OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
8003 SourceLocation StartLoc,
8004 SourceLocation LParenLoc,
8005 SourceLocation EndLoc) {
8006 Expr *ValExpr = Priority;
8007
8008 // OpenMP [2.9.1, task Constrcut]
8009 // The priority-value is a non-negative numerical scalar expression.
8010 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
8011 /*StrictlyPositive=*/false))
8012 return nullptr;
8013
8014 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8015}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008016
8017OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
8018 SourceLocation StartLoc,
8019 SourceLocation LParenLoc,
8020 SourceLocation EndLoc) {
8021 Expr *ValExpr = Grainsize;
8022
8023 // OpenMP [2.9.2, taskloop Constrcut]
8024 // The parameter of the grainsize clause must be a positive integer
8025 // expression.
8026 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
8027 /*StrictlyPositive=*/true))
8028 return nullptr;
8029
8030 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8031}