blob: 90aaeaa8afc29b1e564637e631dd26df1cc8d177 [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
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000568 // in a Construct, C/C++, predetermined, p.4]
569 // Static data members are shared.
570 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
571 // in a Construct, C/C++, predetermined, p.7]
572 // Variables with static storage duration that are declared in a scope
573 // inside the construct are shared.
574 if (D->isStaticDataMember()) {
575 DSAVarData DVarTemp =
576 hasDSA(D, isOpenMPPrivate, MatchesAlways(), FromParent);
577 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
Alexey Bataevec3da872014-01-31 05:15:34 +0000578 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000579
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000580 DVar.CKind = OMPC_shared;
581 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000582 }
583
584 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +0000585 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
586 Type = SemaRef.getASTContext().getBaseElementType(Type);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000587 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
588 // in a Construct, C/C++, predetermined, p.6]
589 // Variables with const qualified type having no mutable member are
590 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000591 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000592 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000593 if (IsConstant &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000594 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000595 // Variables with const-qualified type having no mutable member may be
596 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000597 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
598 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000599 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
600 return DVar;
601
Alexey Bataev758e55e2013-09-06 18:03:48 +0000602 DVar.CKind = OMPC_shared;
603 return DVar;
604 }
605
Alexey Bataev758e55e2013-09-06 18:03:48 +0000606 // Explicitly specified attributes and local variables with predetermined
607 // attributes.
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000608 auto StartI = std::next(Stack.rbegin());
609 auto EndI = std::prev(Stack.rend());
610 if (FromParent && StartI != EndI) {
611 StartI = std::next(StartI);
612 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000613 auto I = std::prev(StartI);
614 if (I->SharingMap.count(D)) {
615 DVar.RefExpr = I->SharingMap[D].RefExpr;
616 DVar.CKind = I->SharingMap[D].Attributes;
617 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000618 }
619
620 return DVar;
621}
622
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000623DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D, bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000624 D = D->getCanonicalDecl();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000625 auto StartI = Stack.rbegin();
626 auto EndI = std::prev(Stack.rend());
627 if (FromParent && StartI != EndI) {
628 StartI = std::next(StartI);
629 }
630 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000631}
632
Alexey Bataevf29276e2014-06-18 04:14:57 +0000633template <class ClausesPredicate, class DirectivesPredicate>
634DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000635 DirectivesPredicate DPred,
636 bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000637 D = D->getCanonicalDecl();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000638 auto StartI = std::next(Stack.rbegin());
639 auto EndI = std::prev(Stack.rend());
640 if (FromParent && StartI != EndI) {
641 StartI = std::next(StartI);
642 }
643 for (auto I = StartI, EE = EndI; I != EE; ++I) {
644 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000645 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000646 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000647 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000648 return DVar;
649 }
650 return DSAVarData();
651}
652
Alexey Bataevf29276e2014-06-18 04:14:57 +0000653template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000654DSAStackTy::DSAVarData
655DSAStackTy::hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
656 DirectivesPredicate DPred, bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000657 D = D->getCanonicalDecl();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000658 auto StartI = std::next(Stack.rbegin());
659 auto EndI = std::prev(Stack.rend());
660 if (FromParent && StartI != EndI) {
661 StartI = std::next(StartI);
662 }
663 for (auto I = StartI, EE = EndI; I != EE; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000664 if (!DPred(I->Directive))
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000665 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000666 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000667 if (CPred(DVar.CKind))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000668 return DVar;
669 return DSAVarData();
670 }
671 return DSAVarData();
672}
673
Alexey Bataevaac108a2015-06-23 04:51:00 +0000674bool DSAStackTy::hasExplicitDSA(
675 VarDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
676 unsigned Level) {
677 if (CPred(ClauseKindMode))
678 return true;
679 if (isClauseParsingMode())
680 ++Level;
681 D = D->getCanonicalDecl();
682 auto StartI = Stack.rbegin();
683 auto EndI = std::prev(Stack.rend());
NAKAMURA Takumi0332eda2015-06-23 10:01:20 +0000684 if (std::distance(StartI, EndI) <= (int)Level)
Alexey Bataevaac108a2015-06-23 04:51:00 +0000685 return false;
686 std::advance(StartI, Level);
687 return (StartI->SharingMap.count(D) > 0) && StartI->SharingMap[D].RefExpr &&
688 CPred(StartI->SharingMap[D].Attributes);
689}
690
Samuel Antao4be30e92015-10-02 17:14:03 +0000691bool DSAStackTy::hasExplicitDirective(
692 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
693 unsigned Level) {
694 if (isClauseParsingMode())
695 ++Level;
696 auto StartI = Stack.rbegin();
697 auto EndI = std::prev(Stack.rend());
698 if (std::distance(StartI, EndI) <= (int)Level)
699 return false;
700 std::advance(StartI, Level);
701 return DPred(StartI->Directive);
702}
703
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000704template <class NamedDirectivesPredicate>
705bool DSAStackTy::hasDirective(NamedDirectivesPredicate DPred, bool FromParent) {
706 auto StartI = std::next(Stack.rbegin());
707 auto EndI = std::prev(Stack.rend());
708 if (FromParent && StartI != EndI) {
709 StartI = std::next(StartI);
710 }
711 for (auto I = StartI, EE = EndI; I != EE; ++I) {
712 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
713 return true;
714 }
715 return false;
716}
717
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000718OpenMPDirectiveKind DSAStackTy::getDirectiveForScope(const Scope *S) const {
719 for (auto I = Stack.rbegin(), EE = Stack.rend(); I != EE; ++I)
720 if (I->CurScope == S)
721 return I->Directive;
722 return OMPD_unknown;
723}
724
Alexey Bataev758e55e2013-09-06 18:03:48 +0000725void Sema::InitDataSharingAttributesStack() {
726 VarDataSharingAttributesStack = new DSAStackTy(*this);
727}
728
729#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
730
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000731bool Sema::IsOpenMPCapturedByRef(VarDecl *VD,
732 const CapturedRegionScopeInfo *RSI) {
733 assert(LangOpts.OpenMP && "OpenMP is not allowed");
734
735 auto &Ctx = getASTContext();
736 bool IsByRef = true;
737
738 // Find the directive that is associated with the provided scope.
739 auto DKind = DSAStack->getDirectiveForScope(RSI->TheScope);
740 auto Ty = VD->getType();
741
742 if (isOpenMPTargetDirective(DKind)) {
743 // This table summarizes how a given variable should be passed to the device
744 // given its type and the clauses where it appears. This table is based on
745 // the description in OpenMP 4.5 [2.10.4, target Construct] and
746 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
747 //
748 // =========================================================================
749 // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
750 // | |(tofrom:scalar)| | pvt | | | |
751 // =========================================================================
752 // | scl | | | | - | | bycopy|
753 // | scl | | - | x | - | - | bycopy|
754 // | scl | | x | - | - | - | null |
755 // | scl | x | | | - | | byref |
756 // | scl | x | - | x | - | - | bycopy|
757 // | scl | x | x | - | - | - | null |
758 // | scl | | - | - | - | x | byref |
759 // | scl | x | - | - | - | x | byref |
760 //
761 // | agg | n.a. | | | - | | byref |
762 // | agg | n.a. | - | x | - | - | byref |
763 // | agg | n.a. | x | - | - | - | null |
764 // | agg | n.a. | - | - | - | x | byref |
765 // | agg | n.a. | - | - | - | x[] | byref |
766 //
767 // | ptr | n.a. | | | - | | bycopy|
768 // | ptr | n.a. | - | x | - | - | bycopy|
769 // | ptr | n.a. | x | - | - | - | null |
770 // | ptr | n.a. | - | - | - | x | byref |
771 // | ptr | n.a. | - | - | - | x[] | bycopy|
772 // | ptr | n.a. | - | - | x | | bycopy|
773 // | ptr | n.a. | - | - | x | x | bycopy|
774 // | ptr | n.a. | - | - | x | x[] | bycopy|
775 // =========================================================================
776 // Legend:
777 // scl - scalar
778 // ptr - pointer
779 // agg - aggregate
780 // x - applies
781 // - - invalid in this combination
782 // [] - mapped with an array section
783 // byref - should be mapped by reference
784 // byval - should be mapped by value
785 // null - initialize a local variable to null on the device
786 //
787 // Observations:
788 // - All scalar declarations that show up in a map clause have to be passed
789 // by reference, because they may have been mapped in the enclosing data
790 // environment.
791 // - If the scalar value does not fit the size of uintptr, it has to be
792 // passed by reference, regardless the result in the table above.
793 // - For pointers mapped by value that have either an implicit map or an
794 // array section, the runtime library may pass the NULL value to the
795 // device instead of the value passed to it by the compiler.
796
797 // FIXME: Right now, only implicit maps are implemented. Properly mapping
798 // values requires having the map, private, and firstprivate clauses SEMA
799 // and parsing in place, which we don't yet.
800
801 if (Ty->isReferenceType())
802 Ty = Ty->castAs<ReferenceType>()->getPointeeType();
803 IsByRef = !Ty->isScalarType();
804 }
805
806 // When passing data by value, we need to make sure it fits the uintptr size
807 // and alignment, because the runtime library only deals with uintptr types.
808 // If it does not fit the uintptr size, we need to pass the data by reference
809 // instead.
810 if (!IsByRef &&
811 (Ctx.getTypeSizeInChars(Ty) >
812 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
813 Ctx.getDeclAlign(VD) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType())))
814 IsByRef = true;
815
816 return IsByRef;
817}
818
Alexey Bataevf841bd92014-12-16 07:00:22 +0000819bool Sema::IsOpenMPCapturedVar(VarDecl *VD) {
820 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000821 VD = VD->getCanonicalDecl();
Samuel Antao4be30e92015-10-02 17:14:03 +0000822
823 // If we are attempting to capture a global variable in a directive with
824 // 'target' we return true so that this global is also mapped to the device.
825 //
826 // FIXME: If the declaration is enclosed in a 'declare target' directive,
827 // then it should not be captured. Therefore, an extra check has to be
828 // inserted here once support for 'declare target' is added.
829 //
830 if (!VD->hasLocalStorage()) {
831 if (DSAStack->getCurrentDirective() == OMPD_target &&
832 !DSAStack->isClauseParsingMode()) {
833 return true;
834 }
835 if (DSAStack->getCurScope() &&
836 DSAStack->hasDirective(
837 [](OpenMPDirectiveKind K, const DeclarationNameInfo &DNI,
838 SourceLocation Loc) -> bool {
839 return isOpenMPTargetDirective(K);
840 },
841 false)) {
842 return true;
843 }
844 }
845
Alexey Bataev48977c32015-08-04 08:10:48 +0000846 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
847 (!DSAStack->isClauseParsingMode() ||
848 DSAStack->getParentDirective() != OMPD_unknown)) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000849 if (DSAStack->isLoopControlVariable(VD) ||
850 (VD->hasLocalStorage() &&
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000851 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
852 DSAStack->isForceVarCapturing())
Alexey Bataev9c821032015-04-30 04:23:23 +0000853 return true;
Alexey Bataevaac108a2015-06-23 04:51:00 +0000854 auto DVarPrivate = DSAStack->getTopDSA(VD, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000855 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
856 return true;
857 DVarPrivate = DSAStack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(),
Alexey Bataevaac108a2015-06-23 04:51:00 +0000858 DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000859 return DVarPrivate.CKind != OMPC_unknown;
860 }
861 return false;
862}
863
Alexey Bataevaac108a2015-06-23 04:51:00 +0000864bool Sema::isOpenMPPrivateVar(VarDecl *VD, unsigned Level) {
865 assert(LangOpts.OpenMP && "OpenMP is not allowed");
866 return DSAStack->hasExplicitDSA(
867 VD, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level);
868}
869
Samuel Antao4be30e92015-10-02 17:14:03 +0000870bool Sema::isOpenMPTargetCapturedVar(VarDecl *VD, unsigned Level) {
871 assert(LangOpts.OpenMP && "OpenMP is not allowed");
872 // Return true if the current level is no longer enclosed in a target region.
873
874 return !VD->hasLocalStorage() &&
875 DSAStack->hasExplicitDirective(isOpenMPTargetDirective, Level);
876}
877
Alexey Bataeved09d242014-05-28 05:53:51 +0000878void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000879
880void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
881 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000882 Scope *CurScope, SourceLocation Loc) {
883 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000884 PushExpressionEvaluationContext(PotentiallyEvaluated);
885}
886
Alexey Bataevaac108a2015-06-23 04:51:00 +0000887void Sema::StartOpenMPClause(OpenMPClauseKind K) {
888 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000889}
890
Alexey Bataevaac108a2015-06-23 04:51:00 +0000891void Sema::EndOpenMPClause() {
892 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000893}
894
Alexey Bataev758e55e2013-09-06 18:03:48 +0000895void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000896 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
897 // A variable of class type (or array thereof) that appears in a lastprivate
898 // clause requires an accessible, unambiguous default constructor for the
899 // class type, unless the list item is also specified in a firstprivate
900 // clause.
901 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000902 for (auto *C : D->clauses()) {
903 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
904 SmallVector<Expr *, 8> PrivateCopies;
905 for (auto *DE : Clause->varlists()) {
906 if (DE->isValueDependent() || DE->isTypeDependent()) {
907 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000908 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +0000909 }
910 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(DE)->getDecl());
Alexey Bataevbd9fec12015-08-18 06:47:21 +0000911 QualType Type = VD->getType().getNonReferenceType();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000912 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000913 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000914 // Generate helper private variable and initialize it with the
915 // default value. The address of the original variable is replaced
916 // by the address of the new private variable in CodeGen. This new
917 // variable is not added to IdResolver, so the code in the OpenMP
918 // region uses original variable for proper diagnostics.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000919 auto *VDPrivate = buildVarDecl(
920 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
921 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +0000922 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
923 if (VDPrivate->isInvalidDecl())
924 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +0000925 PrivateCopies.push_back(buildDeclRefExpr(
926 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +0000927 } else {
928 // The variable is also a firstprivate, so initialization sequence
929 // for private copy is generated already.
930 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000931 }
932 }
Alexey Bataev38e89532015-04-16 04:54:05 +0000933 // Set initializers to private copies if no errors were found.
934 if (PrivateCopies.size() == Clause->varlist_size()) {
935 Clause->setPrivateCopies(PrivateCopies);
936 }
Alexey Bataevf29276e2014-06-18 04:14:57 +0000937 }
938 }
939 }
940
Alexey Bataev758e55e2013-09-06 18:03:48 +0000941 DSAStack->pop();
942 DiscardCleanupsInEvaluationContext();
943 PopExpressionEvaluationContext();
944}
945
Alexander Musman3276a272015-03-21 10:12:56 +0000946static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
947 Expr *NumIterations, Sema &SemaRef,
948 Scope *S);
949
Alexey Bataeva769e072013-03-22 06:34:35 +0000950namespace {
951
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000952class VarDeclFilterCCC : public CorrectionCandidateCallback {
953private:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000954 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +0000955
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000956public:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000957 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +0000958 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000959 NamedDecl *ND = Candidate.getCorrectionDecl();
960 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
961 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000962 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
963 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +0000964 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000965 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000966 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000967};
Alexey Bataeved09d242014-05-28 05:53:51 +0000968} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000969
970ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
971 CXXScopeSpec &ScopeSpec,
972 const DeclarationNameInfo &Id) {
973 LookupResult Lookup(*this, Id, LookupOrdinaryName);
974 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
975
976 if (Lookup.isAmbiguous())
977 return ExprError();
978
979 VarDecl *VD;
980 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +0000981 if (TypoCorrection Corrected = CorrectTypo(
982 Id, LookupOrdinaryName, CurScope, nullptr,
983 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +0000984 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000985 PDiag(Lookup.empty()
986 ? diag::err_undeclared_var_use_suggest
987 : diag::err_omp_expected_var_arg_suggest)
988 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +0000989 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000990 } else {
Richard Smithf9b15102013-08-17 00:46:16 +0000991 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
992 : diag::err_omp_expected_var_arg)
993 << Id.getName();
994 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000995 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000996 } else {
997 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000998 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000999 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1000 return ExprError();
1001 }
1002 }
1003 Lookup.suppressDiagnostics();
1004
1005 // OpenMP [2.9.2, Syntax, C/C++]
1006 // Variables must be file-scope, namespace-scope, or static block-scope.
1007 if (!VD->hasGlobalStorage()) {
1008 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001009 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1010 bool IsDecl =
1011 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001012 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001013 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1014 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001015 return ExprError();
1016 }
1017
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001018 VarDecl *CanonicalVD = VD->getCanonicalDecl();
1019 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001020 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1021 // A threadprivate directive for file-scope variables must appear outside
1022 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001023 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1024 !getCurLexicalContext()->isTranslationUnit()) {
1025 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001026 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1027 bool IsDecl =
1028 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1029 Diag(VD->getLocation(),
1030 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1031 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001032 return ExprError();
1033 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001034 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1035 // A threadprivate directive for static class member variables must appear
1036 // in the class definition, in the same scope in which the member
1037 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001038 if (CanonicalVD->isStaticDataMember() &&
1039 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1040 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001041 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1042 bool IsDecl =
1043 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1044 Diag(VD->getLocation(),
1045 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1046 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001047 return ExprError();
1048 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001049 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1050 // A threadprivate directive for namespace-scope variables must appear
1051 // outside any definition or declaration other than the namespace
1052 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001053 if (CanonicalVD->getDeclContext()->isNamespace() &&
1054 (!getCurLexicalContext()->isFileContext() ||
1055 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
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.6]
1066 // A threadprivate directive for static block-scope variables must appear
1067 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001068 if (CanonicalVD->isStaticLocal() && CurScope &&
1069 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001070 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001071 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1072 bool IsDecl =
1073 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1074 Diag(VD->getLocation(),
1075 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1076 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001077 return ExprError();
1078 }
1079
1080 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1081 // A threadprivate directive must lexically precede all references to any
1082 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001083 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001084 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +00001085 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001086 return ExprError();
1087 }
1088
1089 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00001090 ExprResult DE = buildDeclRefExpr(*this, VD, ExprType, Id.getLoc());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001091 return DE;
1092}
1093
Alexey Bataeved09d242014-05-28 05:53:51 +00001094Sema::DeclGroupPtrTy
1095Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1096 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001097 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001098 CurContext->addDecl(D);
1099 return DeclGroupPtrTy::make(DeclGroupRef(D));
1100 }
1101 return DeclGroupPtrTy();
1102}
1103
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001104namespace {
1105class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
1106 Sema &SemaRef;
1107
1108public:
1109 bool VisitDeclRefExpr(const DeclRefExpr *E) {
1110 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
1111 if (VD->hasLocalStorage()) {
1112 SemaRef.Diag(E->getLocStart(),
1113 diag::err_omp_local_var_in_threadprivate_init)
1114 << E->getSourceRange();
1115 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1116 << VD << VD->getSourceRange();
1117 return true;
1118 }
1119 }
1120 return false;
1121 }
1122 bool VisitStmt(const Stmt *S) {
1123 for (auto Child : S->children()) {
1124 if (Child && Visit(Child))
1125 return true;
1126 }
1127 return false;
1128 }
Alexey Bataev23b69422014-06-18 07:08:49 +00001129 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001130};
1131} // namespace
1132
Alexey Bataeved09d242014-05-28 05:53:51 +00001133OMPThreadPrivateDecl *
1134Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001135 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001136 for (auto &RefExpr : VarList) {
1137 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001138 VarDecl *VD = cast<VarDecl>(DE->getDecl());
1139 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +00001140
Alexey Bataevf56f98c2015-04-16 05:39:01 +00001141 QualType QType = VD->getType();
1142 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1143 // It will be analyzed later.
1144 Vars.push_back(DE);
1145 continue;
1146 }
1147
Alexey Bataeva769e072013-03-22 06:34:35 +00001148 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1149 // A threadprivate variable must not have an incomplete type.
1150 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001151 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001152 continue;
1153 }
1154
1155 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1156 // A threadprivate variable must not have a reference type.
1157 if (VD->getType()->isReferenceType()) {
1158 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001159 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
1160 bool IsDecl =
1161 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1162 Diag(VD->getLocation(),
1163 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1164 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001165 continue;
1166 }
1167
Samuel Antaof8b50122015-07-13 22:54:53 +00001168 // Check if this is a TLS variable. If TLS is not being supported, produce
1169 // the corresponding diagnostic.
1170 if ((VD->getTLSKind() != VarDecl::TLS_None &&
1171 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1172 getLangOpts().OpenMPUseTLS &&
1173 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +00001174 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
1175 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +00001176 Diag(ILoc, diag::err_omp_var_thread_local)
1177 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +00001178 bool IsDecl =
1179 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1180 Diag(VD->getLocation(),
1181 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1182 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001183 continue;
1184 }
1185
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001186 // Check if initial value of threadprivate variable reference variable with
1187 // local storage (it is not supported by runtime).
1188 if (auto Init = VD->getAnyInitializer()) {
1189 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001190 if (Checker.Visit(Init))
1191 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001192 }
1193
Alexey Bataeved09d242014-05-28 05:53:51 +00001194 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00001195 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00001196 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1197 Context, SourceRange(Loc, Loc)));
1198 if (auto *ML = Context.getASTMutationListener())
1199 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00001200 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001201 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001202 if (!Vars.empty()) {
1203 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1204 Vars);
1205 D->setAccess(AS_public);
1206 }
1207 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00001208}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001209
Alexey Bataev7ff55242014-06-19 09:13:45 +00001210static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
1211 const VarDecl *VD, DSAStackTy::DSAVarData DVar,
1212 bool IsLoopIterVar = false) {
1213 if (DVar.RefExpr) {
1214 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1215 << getOpenMPClauseName(DVar.CKind);
1216 return;
1217 }
1218 enum {
1219 PDSA_StaticMemberShared,
1220 PDSA_StaticLocalVarShared,
1221 PDSA_LoopIterVarPrivate,
1222 PDSA_LoopIterVarLinear,
1223 PDSA_LoopIterVarLastprivate,
1224 PDSA_ConstVarShared,
1225 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001226 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001227 PDSA_LocalVarPrivate,
1228 PDSA_Implicit
1229 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001230 bool ReportHint = false;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001231 auto ReportLoc = VD->getLocation();
Alexey Bataev7ff55242014-06-19 09:13:45 +00001232 if (IsLoopIterVar) {
1233 if (DVar.CKind == OMPC_private)
1234 Reason = PDSA_LoopIterVarPrivate;
1235 else if (DVar.CKind == OMPC_lastprivate)
1236 Reason = PDSA_LoopIterVarLastprivate;
1237 else
1238 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001239 } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) {
1240 Reason = PDSA_TaskVarFirstprivate;
1241 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001242 } else if (VD->isStaticLocal())
1243 Reason = PDSA_StaticLocalVarShared;
1244 else if (VD->isStaticDataMember())
1245 Reason = PDSA_StaticMemberShared;
1246 else if (VD->isFileVarDecl())
1247 Reason = PDSA_GlobalVarShared;
1248 else if (VD->getType().isConstant(SemaRef.getASTContext()))
1249 Reason = PDSA_ConstVarShared;
Alexey Bataevbae9a792014-06-27 10:37:06 +00001250 else if (VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00001251 ReportHint = true;
1252 Reason = PDSA_LocalVarPrivate;
1253 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00001254 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001255 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00001256 << Reason << ReportHint
1257 << getOpenMPDirectiveName(Stack->getCurrentDirective());
1258 } else if (DVar.ImplicitDSALoc.isValid()) {
1259 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1260 << getOpenMPClauseName(DVar.CKind);
1261 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00001262}
1263
Alexey Bataev758e55e2013-09-06 18:03:48 +00001264namespace {
1265class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1266 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001267 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001268 bool ErrorFound;
1269 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001270 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev4acb8592014-07-07 13:01:15 +00001271 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +00001272
Alexey Bataev758e55e2013-09-06 18:03:48 +00001273public:
1274 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001275 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001276 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +00001277 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1278 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001279
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001280 auto DVar = Stack->getTopDSA(VD, false);
1281 // Check if the variable has explicit DSA set and stop analysis if it so.
1282 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001283
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001284 auto ELoc = E->getExprLoc();
1285 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001286 // The default(none) clause requires that each variable that is referenced
1287 // in the construct, and does not have a predetermined data-sharing
1288 // attribute, must have its data-sharing attribute explicitly determined
1289 // by being listed in a data-sharing attribute clause.
1290 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001291 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001292 VarsWithInheritedDSA.count(VD) == 0) {
1293 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001294 return;
1295 }
1296
1297 // OpenMP [2.9.3.6, Restrictions, p.2]
1298 // A list item that appears in a reduction clause of the innermost
1299 // enclosing worksharing or parallel construct may not be accessed in an
1300 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +00001301 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001302 [](OpenMPDirectiveKind K) -> bool {
1303 return isOpenMPParallelDirective(K) ||
Alexey Bataev13314bf2014-10-09 04:18:56 +00001304 isOpenMPWorksharingDirective(K) ||
1305 isOpenMPTeamsDirective(K);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001306 },
1307 false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001308 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
1309 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001310 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1311 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001312 return;
1313 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001314
1315 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001316 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001317 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001318 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001319 }
1320 }
1321 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001322 for (auto *C : S->clauses()) {
1323 // Skip analysis of arguments of implicitly defined firstprivate clause
1324 // for task directives.
1325 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1326 for (auto *CC : C->children()) {
1327 if (CC)
1328 Visit(CC);
1329 }
1330 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001331 }
1332 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001333 for (auto *C : S->children()) {
1334 if (C && !isa<OMPExecutableDirective>(C))
1335 Visit(C);
1336 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001337 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001338
1339 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001340 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001341 llvm::DenseMap<VarDecl *, Expr *> &getVarsWithInheritedDSA() {
1342 return VarsWithInheritedDSA;
1343 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001344
Alexey Bataev7ff55242014-06-19 09:13:45 +00001345 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1346 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00001347};
Alexey Bataeved09d242014-05-28 05:53:51 +00001348} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001349
Alexey Bataevbae9a792014-06-27 10:37:06 +00001350void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001351 switch (DKind) {
1352 case OMPD_parallel: {
1353 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001354 QualType KmpInt32PtrTy =
1355 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001356 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001357 std::make_pair(".global_tid.", KmpInt32PtrTy),
1358 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1359 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001360 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001361 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1362 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001363 break;
1364 }
1365 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001366 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001367 std::make_pair(StringRef(), QualType()) // __context with shared vars
1368 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001369 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1370 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001371 break;
1372 }
1373 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001374 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001375 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 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001381 case OMPD_for_simd: {
1382 Sema::CapturedParamNameType Params[] = {
1383 std::make_pair(StringRef(), QualType()) // __context with shared vars
1384 };
1385 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1386 Params);
1387 break;
1388 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001389 case OMPD_sections: {
1390 Sema::CapturedParamNameType Params[] = {
1391 std::make_pair(StringRef(), QualType()) // __context with shared vars
1392 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001393 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1394 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001395 break;
1396 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001397 case OMPD_section: {
1398 Sema::CapturedParamNameType Params[] = {
1399 std::make_pair(StringRef(), QualType()) // __context with shared vars
1400 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001401 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1402 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001403 break;
1404 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001405 case OMPD_single: {
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 Bataevd1e40fb2014-06-26 12:05:45 +00001411 break;
1412 }
Alexander Musman80c22892014-07-17 08:54:58 +00001413 case OMPD_master: {
1414 Sema::CapturedParamNameType Params[] = {
1415 std::make_pair(StringRef(), QualType()) // __context with shared vars
1416 };
1417 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1418 Params);
1419 break;
1420 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001421 case OMPD_critical: {
1422 Sema::CapturedParamNameType Params[] = {
1423 std::make_pair(StringRef(), QualType()) // __context with shared vars
1424 };
1425 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1426 Params);
1427 break;
1428 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001429 case OMPD_parallel_for: {
1430 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001431 QualType KmpInt32PtrTy =
1432 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev4acb8592014-07-07 13:01:15 +00001433 Sema::CapturedParamNameType Params[] = {
1434 std::make_pair(".global_tid.", KmpInt32PtrTy),
1435 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1436 std::make_pair(StringRef(), QualType()) // __context with shared vars
1437 };
1438 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1439 Params);
1440 break;
1441 }
Alexander Musmane4e893b2014-09-23 09:33:00 +00001442 case OMPD_parallel_for_simd: {
1443 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001444 QualType KmpInt32PtrTy =
1445 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexander Musmane4e893b2014-09-23 09:33:00 +00001446 Sema::CapturedParamNameType Params[] = {
1447 std::make_pair(".global_tid.", KmpInt32PtrTy),
1448 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1449 std::make_pair(StringRef(), QualType()) // __context with shared vars
1450 };
1451 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1452 Params);
1453 break;
1454 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001455 case OMPD_parallel_sections: {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001456 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001457 QualType KmpInt32PtrTy =
1458 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001459 Sema::CapturedParamNameType Params[] = {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001460 std::make_pair(".global_tid.", KmpInt32PtrTy),
1461 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001462 std::make_pair(StringRef(), QualType()) // __context with shared vars
1463 };
1464 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1465 Params);
1466 break;
1467 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001468 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001469 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001470 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1471 FunctionProtoType::ExtProtoInfo EPI;
1472 EPI.Variadic = true;
1473 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001474 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001475 std::make_pair(".global_tid.", KmpInt32Ty),
1476 std::make_pair(".part_id.", KmpInt32Ty),
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001477 std::make_pair(".privates.",
1478 Context.VoidPtrTy.withConst().withRestrict()),
1479 std::make_pair(
1480 ".copy_fn.",
1481 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001482 std::make_pair(StringRef(), QualType()) // __context with shared vars
1483 };
1484 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1485 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001486 // Mark this captured region as inlined, because we don't use outlined
1487 // function directly.
1488 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1489 AlwaysInlineAttr::CreateImplicit(
1490 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001491 break;
1492 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001493 case OMPD_ordered: {
1494 Sema::CapturedParamNameType Params[] = {
1495 std::make_pair(StringRef(), QualType()) // __context with shared vars
1496 };
1497 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1498 Params);
1499 break;
1500 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001501 case OMPD_atomic: {
1502 Sema::CapturedParamNameType Params[] = {
1503 std::make_pair(StringRef(), QualType()) // __context with shared vars
1504 };
1505 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1506 Params);
1507 break;
1508 }
Michael Wong65f367f2015-07-21 13:44:28 +00001509 case OMPD_target_data:
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001510 case OMPD_target: {
1511 Sema::CapturedParamNameType Params[] = {
1512 std::make_pair(StringRef(), QualType()) // __context with shared vars
1513 };
1514 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1515 Params);
1516 break;
1517 }
Alexey Bataev13314bf2014-10-09 04:18:56 +00001518 case OMPD_teams: {
1519 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001520 QualType KmpInt32PtrTy =
1521 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev13314bf2014-10-09 04:18:56 +00001522 Sema::CapturedParamNameType Params[] = {
1523 std::make_pair(".global_tid.", KmpInt32PtrTy),
1524 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1525 std::make_pair(StringRef(), QualType()) // __context with shared vars
1526 };
1527 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1528 Params);
1529 break;
1530 }
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001531 case OMPD_taskgroup: {
1532 Sema::CapturedParamNameType Params[] = {
1533 std::make_pair(StringRef(), QualType()) // __context with shared vars
1534 };
1535 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1536 Params);
1537 break;
1538 }
Alexey Bataev49f6e782015-12-01 04:18:41 +00001539 case OMPD_taskloop: {
1540 Sema::CapturedParamNameType Params[] = {
1541 std::make_pair(StringRef(), QualType()) // __context with shared vars
1542 };
1543 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1544 Params);
1545 break;
1546 }
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001547 case OMPD_taskloop_simd: {
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 Bataev9959db52014-05-06 10:08:46 +00001555 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001556 case OMPD_taskyield:
1557 case OMPD_barrier:
1558 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001559 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001560 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00001561 case OMPD_flush:
Alexey Bataev9959db52014-05-06 10:08:46 +00001562 llvm_unreachable("OpenMP Directive is not allowed");
1563 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001564 llvm_unreachable("Unknown OpenMP directive");
1565 }
1566}
1567
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001568StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1569 ArrayRef<OMPClause *> Clauses) {
1570 if (!S.isUsable()) {
1571 ActOnCapturedRegionError();
1572 return StmtError();
1573 }
Alexey Bataev040d5402015-05-12 08:35:28 +00001574 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001575 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001576 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001577 Clause->getClauseKind() == OMPC_copyprivate ||
1578 (getLangOpts().OpenMPUseTLS &&
1579 getASTContext().getTargetInfo().isTLSSupported() &&
1580 Clause->getClauseKind() == OMPC_copyin)) {
1581 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001582 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001583 for (auto *VarRef : Clause->children()) {
1584 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001585 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001586 }
1587 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001588 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev040d5402015-05-12 08:35:28 +00001589 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
1590 Clause->getClauseKind() == OMPC_schedule) {
1591 // Mark all variables in private list clauses as used in inner region.
1592 // Required for proper codegen of combined directives.
1593 // TODO: add processing for other clauses.
1594 if (auto *E = cast_or_null<Expr>(
1595 cast<OMPScheduleClause>(Clause)->getHelperChunkSize())) {
1596 MarkDeclarationsReferencedInExpr(E);
1597 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001598 }
1599 }
1600 return ActOnCapturedRegionEnd(S.get());
1601}
1602
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001603static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1604 OpenMPDirectiveKind CurrentRegion,
1605 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001606 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001607 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001608 // Allowed nesting of constructs
1609 // +------------------+-----------------+------------------------------------+
1610 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1611 // +------------------+-----------------+------------------------------------+
1612 // | parallel | parallel | * |
1613 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001614 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001615 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001616 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001617 // | parallel | simd | * |
1618 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001619 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001620 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001621 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001622 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001623 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001624 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001625 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001626 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001627 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001628 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001629 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001630 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001631 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001632 // | parallel | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001633 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001634 // | parallel | cancellation | |
1635 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001636 // | parallel | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001637 // | parallel | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001638 // | parallel | taskloop simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001639 // +------------------+-----------------+------------------------------------+
1640 // | for | parallel | * |
1641 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001642 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001643 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001644 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001645 // | for | simd | * |
1646 // | for | sections | + |
1647 // | for | section | + |
1648 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001649 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001650 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001651 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001652 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001653 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001654 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001655 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001656 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001657 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001658 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001659 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001660 // | for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001661 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001662 // | for | cancellation | |
1663 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001664 // | for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001665 // | for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001666 // | for | taskloop simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001667 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001668 // | master | parallel | * |
1669 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001670 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001671 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001672 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001673 // | master | simd | * |
1674 // | master | sections | + |
1675 // | master | section | + |
1676 // | master | single | + |
1677 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001678 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001679 // | master |parallel sections| * |
1680 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001681 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001682 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001683 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001684 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001685 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001686 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001687 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001688 // | master | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001689 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001690 // | master | cancellation | |
1691 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001692 // | master | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001693 // | master | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001694 // | master | taskloop simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001695 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001696 // | critical | parallel | * |
1697 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001698 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001699 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001700 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001701 // | critical | simd | * |
1702 // | critical | sections | + |
1703 // | critical | section | + |
1704 // | critical | single | + |
1705 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001706 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001707 // | critical |parallel sections| * |
1708 // | critical | task | * |
1709 // | critical | taskyield | * |
1710 // | critical | barrier | + |
1711 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001712 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001713 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001714 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001715 // | critical | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001716 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001717 // | critical | cancellation | |
1718 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001719 // | critical | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001720 // | critical | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001721 // | critical | taskloop simd | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001722 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001723 // | simd | parallel | |
1724 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001725 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00001726 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001727 // | simd | critical | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001728 // | simd | simd | |
1729 // | simd | sections | |
1730 // | simd | section | |
1731 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001732 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001733 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001734 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001735 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00001736 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001737 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001738 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001739 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00001740 // | simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001741 // | simd | ordered | + (with simd clause) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001742 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001743 // | simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001744 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001745 // | simd | cancellation | |
1746 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001747 // | simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001748 // | simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001749 // | simd | taskloop simd | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001750 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00001751 // | for simd | parallel | |
1752 // | for simd | for | |
1753 // | for simd | for simd | |
1754 // | for simd | master | |
1755 // | for simd | critical | |
1756 // | for simd | simd | |
1757 // | for simd | sections | |
1758 // | for simd | section | |
1759 // | for simd | single | |
1760 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001761 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001762 // | for simd |parallel sections| |
1763 // | for simd | task | |
1764 // | for simd | taskyield | |
1765 // | for simd | barrier | |
1766 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001767 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001768 // | for simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001769 // | for simd | ordered | + (with simd clause) |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001770 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001771 // | for simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001772 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001773 // | for simd | cancellation | |
1774 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001775 // | for simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001776 // | for simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001777 // | for simd | taskloop simd | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001778 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00001779 // | parallel for simd| parallel | |
1780 // | parallel for simd| for | |
1781 // | parallel for simd| for simd | |
1782 // | parallel for simd| master | |
1783 // | parallel for simd| critical | |
1784 // | parallel for simd| simd | |
1785 // | parallel for simd| sections | |
1786 // | parallel for simd| section | |
1787 // | parallel for simd| single | |
1788 // | parallel for simd| parallel for | |
1789 // | parallel for simd|parallel for simd| |
1790 // | parallel for simd|parallel sections| |
1791 // | parallel for simd| task | |
1792 // | parallel for simd| taskyield | |
1793 // | parallel for simd| barrier | |
1794 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001795 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001796 // | parallel for simd| flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001797 // | parallel for simd| ordered | + (with simd clause) |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001798 // | parallel for simd| atomic | |
1799 // | parallel for simd| target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001800 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001801 // | parallel for simd| cancellation | |
1802 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001803 // | parallel for simd| cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001804 // | parallel for simd| taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001805 // | parallel for simd| taskloop simd | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001806 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001807 // | sections | parallel | * |
1808 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001809 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001810 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001811 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001812 // | sections | simd | * |
1813 // | sections | sections | + |
1814 // | sections | section | * |
1815 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001816 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001817 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001818 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001819 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001820 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001821 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001822 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001823 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001824 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001825 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001826 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001827 // | sections | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001828 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001829 // | sections | cancellation | |
1830 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001831 // | sections | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001832 // | sections | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001833 // | sections | taskloop simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001834 // +------------------+-----------------+------------------------------------+
1835 // | section | parallel | * |
1836 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001837 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001838 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001839 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001840 // | section | simd | * |
1841 // | section | sections | + |
1842 // | section | section | + |
1843 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001844 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001845 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001846 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001847 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001848 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001849 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001850 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001851 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001852 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001853 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001854 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001855 // | section | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001856 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001857 // | section | cancellation | |
1858 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001859 // | section | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001860 // | section | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001861 // | section | taskloop simd | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001862 // +------------------+-----------------+------------------------------------+
1863 // | single | parallel | * |
1864 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001865 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001866 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001867 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001868 // | single | simd | * |
1869 // | single | sections | + |
1870 // | single | section | + |
1871 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001872 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001873 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001874 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001875 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001876 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001877 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001878 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001879 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001880 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001881 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001882 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001883 // | single | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001884 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001885 // | single | cancellation | |
1886 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001887 // | single | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001888 // | single | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001889 // | single | taskloop simd | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001890 // +------------------+-----------------+------------------------------------+
1891 // | parallel for | parallel | * |
1892 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001893 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001894 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001895 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001896 // | parallel for | simd | * |
1897 // | parallel for | sections | + |
1898 // | parallel for | section | + |
1899 // | parallel for | single | + |
1900 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001901 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001902 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001903 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001904 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001905 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001906 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001907 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001908 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001909 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001910 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001911 // | parallel for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001912 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001913 // | parallel for | cancellation | |
1914 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001915 // | parallel for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001916 // | parallel for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001917 // | parallel for | taskloop simd | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001918 // +------------------+-----------------+------------------------------------+
1919 // | parallel sections| parallel | * |
1920 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001921 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001922 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001923 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001924 // | parallel sections| simd | * |
1925 // | parallel sections| sections | + |
1926 // | parallel sections| section | * |
1927 // | parallel sections| single | + |
1928 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001929 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001930 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001931 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001932 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001933 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001934 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001935 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001936 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001937 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001938 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001939 // | parallel sections| target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001940 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001941 // | parallel sections| cancellation | |
1942 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001943 // | parallel sections| cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001944 // | parallel sections| taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001945 // | parallel sections| taskloop simd | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001946 // +------------------+-----------------+------------------------------------+
1947 // | task | parallel | * |
1948 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001949 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001950 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001951 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001952 // | task | simd | * |
1953 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001954 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001955 // | task | single | + |
1956 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001957 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001958 // | task |parallel sections| * |
1959 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001960 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001961 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001962 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001963 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001964 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001965 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001966 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001967 // | task | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001968 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001969 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00001970 // | | point | ! |
1971 // | task | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001972 // | task | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001973 // | task | taskloop simd | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001974 // +------------------+-----------------+------------------------------------+
1975 // | ordered | parallel | * |
1976 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001977 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001978 // | ordered | master | * |
1979 // | ordered | critical | * |
1980 // | ordered | simd | * |
1981 // | ordered | sections | + |
1982 // | ordered | section | + |
1983 // | ordered | single | + |
1984 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001985 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001986 // | ordered |parallel sections| * |
1987 // | ordered | task | * |
1988 // | ordered | taskyield | * |
1989 // | ordered | barrier | + |
1990 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001991 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001992 // | ordered | flush | * |
1993 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001994 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001995 // | ordered | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001996 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001997 // | ordered | cancellation | |
1998 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001999 // | ordered | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002000 // | ordered | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002001 // | ordered | taskloop simd | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002002 // +------------------+-----------------+------------------------------------+
2003 // | atomic | parallel | |
2004 // | atomic | for | |
2005 // | atomic | for simd | |
2006 // | atomic | master | |
2007 // | atomic | critical | |
2008 // | atomic | simd | |
2009 // | atomic | sections | |
2010 // | atomic | section | |
2011 // | atomic | single | |
2012 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002013 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002014 // | atomic |parallel sections| |
2015 // | atomic | task | |
2016 // | atomic | taskyield | |
2017 // | atomic | barrier | |
2018 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002019 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002020 // | atomic | flush | |
2021 // | atomic | ordered | |
2022 // | atomic | atomic | |
2023 // | atomic | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002024 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002025 // | atomic | cancellation | |
2026 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002027 // | atomic | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002028 // | atomic | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002029 // | atomic | taskloop simd | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002030 // +------------------+-----------------+------------------------------------+
2031 // | target | parallel | * |
2032 // | target | for | * |
2033 // | target | for simd | * |
2034 // | target | master | * |
2035 // | target | critical | * |
2036 // | target | simd | * |
2037 // | target | sections | * |
2038 // | target | section | * |
2039 // | target | single | * |
2040 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002041 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002042 // | target |parallel sections| * |
2043 // | target | task | * |
2044 // | target | taskyield | * |
2045 // | target | barrier | * |
2046 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002047 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002048 // | target | flush | * |
2049 // | target | ordered | * |
2050 // | target | atomic | * |
2051 // | target | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002052 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002053 // | target | cancellation | |
2054 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002055 // | target | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002056 // | target | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002057 // | target | taskloop simd | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002058 // +------------------+-----------------+------------------------------------+
2059 // | teams | parallel | * |
2060 // | teams | for | + |
2061 // | teams | for simd | + |
2062 // | teams | master | + |
2063 // | teams | critical | + |
2064 // | teams | simd | + |
2065 // | teams | sections | + |
2066 // | teams | section | + |
2067 // | teams | single | + |
2068 // | teams | parallel for | * |
2069 // | teams |parallel for simd| * |
2070 // | teams |parallel sections| * |
2071 // | teams | task | + |
2072 // | teams | taskyield | + |
2073 // | teams | barrier | + |
2074 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00002075 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002076 // | teams | flush | + |
2077 // | teams | ordered | + |
2078 // | teams | atomic | + |
2079 // | teams | target | + |
2080 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002081 // | teams | cancellation | |
2082 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002083 // | teams | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002084 // | teams | taskloop | + |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002085 // | teams | taskloop simd | + |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002086 // +------------------+-----------------+------------------------------------+
2087 // | taskloop | parallel | * |
2088 // | taskloop | for | + |
2089 // | taskloop | for simd | + |
2090 // | taskloop | master | + |
2091 // | taskloop | critical | * |
2092 // | taskloop | simd | * |
2093 // | taskloop | sections | + |
2094 // | taskloop | section | + |
2095 // | taskloop | single | + |
2096 // | taskloop | parallel for | * |
2097 // | taskloop |parallel for simd| * |
2098 // | taskloop |parallel sections| * |
2099 // | taskloop | task | * |
2100 // | taskloop | taskyield | * |
2101 // | taskloop | barrier | + |
2102 // | taskloop | taskwait | * |
2103 // | taskloop | taskgroup | * |
2104 // | taskloop | flush | * |
2105 // | taskloop | ordered | + |
2106 // | taskloop | atomic | * |
2107 // | taskloop | target | * |
2108 // | taskloop | teams | + |
2109 // | taskloop | cancellation | |
2110 // | | point | |
2111 // | taskloop | cancel | |
2112 // | taskloop | taskloop | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002113 // +------------------+-----------------+------------------------------------+
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002114 // | taskloop simd | parallel | |
2115 // | taskloop simd | for | |
2116 // | taskloop simd | for simd | |
2117 // | taskloop simd | master | |
2118 // | taskloop simd | critical | |
2119 // | taskloop simd | simd | |
2120 // | taskloop simd | sections | |
2121 // | taskloop simd | section | |
2122 // | taskloop simd | single | |
2123 // | taskloop simd | parallel for | |
2124 // | taskloop simd |parallel for simd| |
2125 // | taskloop simd |parallel sections| |
2126 // | taskloop simd | task | |
2127 // | taskloop simd | taskyield | |
2128 // | taskloop simd | barrier | |
2129 // | taskloop simd | taskwait | |
2130 // | taskloop simd | taskgroup | |
2131 // | taskloop simd | flush | |
2132 // | taskloop simd | ordered | + (with simd clause) |
2133 // | taskloop simd | atomic | |
2134 // | taskloop simd | target | |
2135 // | taskloop simd | teams | |
2136 // | taskloop simd | cancellation | |
2137 // | | point | |
2138 // | taskloop simd | cancel | |
2139 // | taskloop simd | taskloop | |
2140 // | taskloop simd | taskloop simd | |
2141 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00002142 if (Stack->getCurScope()) {
2143 auto ParentRegion = Stack->getParentDirective();
2144 bool NestingProhibited = false;
2145 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002146 enum {
2147 NoRecommend,
2148 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00002149 ShouldBeInOrderedRegion,
NAKAMURA Takumi2d5c6dd2015-12-09 04:35:57 +00002150 ShouldBeInTargetRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002151 } Recommend = NoRecommend;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002152 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002153 // OpenMP [2.16, Nesting of Regions]
2154 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002155 // OpenMP [2.8.1,simd Construct, Restrictions]
2156 // An ordered construct with the simd clause is the only OpenMP construct
2157 // that can appear in the simd region.
Alexey Bataev549210e2014-06-24 04:39:47 +00002158 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
2159 return true;
2160 }
Alexey Bataev0162e452014-07-22 10:10:35 +00002161 if (ParentRegion == OMPD_atomic) {
2162 // OpenMP [2.16, Nesting of Regions]
2163 // OpenMP constructs may not be nested inside an atomic region.
2164 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
2165 return true;
2166 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002167 if (CurrentRegion == OMPD_section) {
2168 // OpenMP [2.7.2, sections Construct, Restrictions]
2169 // Orphaned section directives are prohibited. That is, the section
2170 // directives must appear within the sections construct and must not be
2171 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002172 if (ParentRegion != OMPD_sections &&
2173 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002174 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
2175 << (ParentRegion != OMPD_unknown)
2176 << getOpenMPDirectiveName(ParentRegion);
2177 return true;
2178 }
2179 return false;
2180 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002181 // Allow some constructs to be orphaned (they could be used in functions,
2182 // called from OpenMP regions with the required preconditions).
2183 if (ParentRegion == OMPD_unknown)
2184 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00002185 if (CurrentRegion == OMPD_cancellation_point ||
2186 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002187 // OpenMP [2.16, Nesting of Regions]
2188 // A cancellation point construct for which construct-type-clause is
2189 // taskgroup must be nested inside a task construct. A cancellation
2190 // point construct for which construct-type-clause is not taskgroup must
2191 // be closely nested inside an OpenMP construct that matches the type
2192 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00002193 // A cancel construct for which construct-type-clause is taskgroup must be
2194 // nested inside a task construct. A cancel construct for which
2195 // construct-type-clause is not taskgroup must be closely nested inside an
2196 // OpenMP construct that matches the type specified in
2197 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002198 NestingProhibited =
2199 !((CancelRegion == OMPD_parallel && ParentRegion == OMPD_parallel) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00002200 (CancelRegion == OMPD_for &&
2201 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002202 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
2203 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00002204 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
2205 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002206 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00002207 // OpenMP [2.16, Nesting of Regions]
2208 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002209 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00002210 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002211 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002212 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002213 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
2214 // OpenMP [2.16, Nesting of Regions]
2215 // A critical region may not be nested (closely or otherwise) inside a
2216 // critical region with the same name. Note that this restriction is not
2217 // sufficient to prevent deadlock.
2218 SourceLocation PreviousCriticalLoc;
2219 bool DeadLock =
2220 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
2221 OpenMPDirectiveKind K,
2222 const DeclarationNameInfo &DNI,
2223 SourceLocation Loc)
2224 ->bool {
2225 if (K == OMPD_critical &&
2226 DNI.getName() == CurrentName.getName()) {
2227 PreviousCriticalLoc = Loc;
2228 return true;
2229 } else
2230 return false;
2231 },
2232 false /* skip top directive */);
2233 if (DeadLock) {
2234 SemaRef.Diag(StartLoc,
2235 diag::err_omp_prohibited_region_critical_same_name)
2236 << CurrentName.getName();
2237 if (PreviousCriticalLoc.isValid())
2238 SemaRef.Diag(PreviousCriticalLoc,
2239 diag::note_omp_previous_critical_region);
2240 return true;
2241 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002242 } else if (CurrentRegion == OMPD_barrier) {
2243 // OpenMP [2.16, Nesting of Regions]
2244 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002245 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002246 NestingProhibited =
2247 isOpenMPWorksharingDirective(ParentRegion) ||
2248 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002249 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002250 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musman80c22892014-07-17 08:54:58 +00002251 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00002252 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002253 // OpenMP [2.16, Nesting of Regions]
2254 // A worksharing region may not be closely nested inside a worksharing,
2255 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002256 NestingProhibited =
Alexander Musmanf82886e2014-09-18 05:12:34 +00002257 isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002258 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002259 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002260 isOpenMPTaskLoopDirective(ParentRegion);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002261 Recommend = ShouldBeInParallelRegion;
2262 } else if (CurrentRegion == OMPD_ordered) {
2263 // OpenMP [2.16, Nesting of Regions]
2264 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00002265 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002266 // An ordered region must be closely nested inside a loop region (or
2267 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002268 // OpenMP [2.8.1,simd Construct, Restrictions]
2269 // An ordered construct with the simd clause is the only OpenMP construct
2270 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002271 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00002272 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002273 isOpenMPTaskLoopDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002274 !(isOpenMPSimdDirective(ParentRegion) ||
2275 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002276 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002277 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
2278 // OpenMP [2.16, Nesting of Regions]
2279 // If specified, a teams construct must be contained within a target
2280 // construct.
2281 NestingProhibited = ParentRegion != OMPD_target;
2282 Recommend = ShouldBeInTargetRegion;
2283 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
2284 }
2285 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
2286 // OpenMP [2.16, Nesting of Regions]
2287 // distribute, parallel, parallel sections, parallel workshare, and the
2288 // parallel loop and parallel loop SIMD constructs are the only OpenMP
2289 // constructs that can be closely nested in the teams region.
NAKAMURA Takumi2d5c6dd2015-12-09 04:35:57 +00002290 // TODO: add distribute directive.
2291 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00002292 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002293 }
2294 if (NestingProhibited) {
2295 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002296 << CloseNesting << getOpenMPDirectiveName(ParentRegion) << Recommend
2297 << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00002298 return true;
2299 }
2300 }
2301 return false;
2302}
2303
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002304static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
2305 ArrayRef<OMPClause *> Clauses,
2306 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
2307 bool ErrorFound = false;
2308 unsigned NamedModifiersNumber = 0;
2309 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
2310 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00002311 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002312 for (const auto *C : Clauses) {
2313 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
2314 // At most one if clause without a directive-name-modifier can appear on
2315 // the directive.
2316 OpenMPDirectiveKind CurNM = IC->getNameModifier();
2317 if (FoundNameModifiers[CurNM]) {
2318 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
2319 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
2320 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
2321 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002322 } else if (CurNM != OMPD_unknown) {
2323 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002324 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002325 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002326 FoundNameModifiers[CurNM] = IC;
2327 if (CurNM == OMPD_unknown)
2328 continue;
2329 // Check if the specified name modifier is allowed for the current
2330 // directive.
2331 // At most one if clause with the particular directive-name-modifier can
2332 // appear on the directive.
2333 bool MatchFound = false;
2334 for (auto NM : AllowedNameModifiers) {
2335 if (CurNM == NM) {
2336 MatchFound = true;
2337 break;
2338 }
2339 }
2340 if (!MatchFound) {
2341 S.Diag(IC->getNameModifierLoc(),
2342 diag::err_omp_wrong_if_directive_name_modifier)
2343 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
2344 ErrorFound = true;
2345 }
2346 }
2347 }
2348 // If any if clause on the directive includes a directive-name-modifier then
2349 // all if clauses on the directive must include a directive-name-modifier.
2350 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
2351 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
2352 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
2353 diag::err_omp_no_more_if_clause);
2354 } else {
2355 std::string Values;
2356 std::string Sep(", ");
2357 unsigned AllowedCnt = 0;
2358 unsigned TotalAllowedNum =
2359 AllowedNameModifiers.size() - NamedModifiersNumber;
2360 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
2361 ++Cnt) {
2362 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
2363 if (!FoundNameModifiers[NM]) {
2364 Values += "'";
2365 Values += getOpenMPDirectiveName(NM);
2366 Values += "'";
2367 if (AllowedCnt + 2 == TotalAllowedNum)
2368 Values += " or ";
2369 else if (AllowedCnt + 1 != TotalAllowedNum)
2370 Values += Sep;
2371 ++AllowedCnt;
2372 }
2373 }
2374 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
2375 diag::err_omp_unnamed_if_clause)
2376 << (TotalAllowedNum > 1) << Values;
2377 }
Alexey Bataevecb156a2015-09-15 17:23:56 +00002378 for (auto Loc : NameModifierLoc) {
2379 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
2380 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002381 ErrorFound = true;
2382 }
2383 return ErrorFound;
2384}
2385
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002386StmtResult Sema::ActOnOpenMPExecutableDirective(
2387 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
2388 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
2389 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002390 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002391 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
2392 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00002393 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002394
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002395 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev68446b72014-07-18 07:47:19 +00002396 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002397 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00002398 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00002399 if (AStmt) {
2400 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
2401
2402 // Check default data sharing attributes for referenced variables.
2403 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
2404 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
2405 if (DSAChecker.isErrorFound())
2406 return StmtError();
2407 // Generate list of implicitly defined firstprivate variables.
2408 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00002409
2410 if (!DSAChecker.getImplicitFirstprivate().empty()) {
2411 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
2412 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
2413 SourceLocation(), SourceLocation())) {
2414 ClausesWithImplicit.push_back(Implicit);
2415 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
2416 DSAChecker.getImplicitFirstprivate().size();
2417 } else
2418 ErrorFound = true;
2419 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002420 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002421
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002422 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002423 switch (Kind) {
2424 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00002425 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
2426 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002427 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002428 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002429 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002430 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2431 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002432 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002433 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002434 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2435 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002436 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00002437 case OMPD_for_simd:
2438 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2439 EndLoc, VarsWithInheritedDSA);
2440 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002441 case OMPD_sections:
2442 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
2443 EndLoc);
2444 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002445 case OMPD_section:
2446 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00002447 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002448 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
2449 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002450 case OMPD_single:
2451 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
2452 EndLoc);
2453 break;
Alexander Musman80c22892014-07-17 08:54:58 +00002454 case OMPD_master:
2455 assert(ClausesWithImplicit.empty() &&
2456 "No clauses are allowed for 'omp master' directive");
2457 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
2458 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002459 case OMPD_critical:
2460 assert(ClausesWithImplicit.empty() &&
2461 "No clauses are allowed for 'omp critical' directive");
2462 Res = ActOnOpenMPCriticalDirective(DirName, AStmt, StartLoc, EndLoc);
2463 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002464 case OMPD_parallel_for:
2465 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
2466 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002467 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00002468 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00002469 case OMPD_parallel_for_simd:
2470 Res = ActOnOpenMPParallelForSimdDirective(
2471 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002472 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00002473 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002474 case OMPD_parallel_sections:
2475 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
2476 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002477 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002478 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002479 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002480 Res =
2481 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002482 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002483 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00002484 case OMPD_taskyield:
2485 assert(ClausesWithImplicit.empty() &&
2486 "No clauses are allowed for 'omp taskyield' directive");
2487 assert(AStmt == nullptr &&
2488 "No associated statement allowed for 'omp taskyield' directive");
2489 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
2490 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002491 case OMPD_barrier:
2492 assert(ClausesWithImplicit.empty() &&
2493 "No clauses are allowed for 'omp barrier' directive");
2494 assert(AStmt == nullptr &&
2495 "No associated statement allowed for 'omp barrier' directive");
2496 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
2497 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00002498 case OMPD_taskwait:
2499 assert(ClausesWithImplicit.empty() &&
2500 "No clauses are allowed for 'omp taskwait' directive");
2501 assert(AStmt == nullptr &&
2502 "No associated statement allowed for 'omp taskwait' directive");
2503 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
2504 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002505 case OMPD_taskgroup:
2506 assert(ClausesWithImplicit.empty() &&
2507 "No clauses are allowed for 'omp taskgroup' directive");
2508 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
2509 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00002510 case OMPD_flush:
2511 assert(AStmt == nullptr &&
2512 "No associated statement allowed for 'omp flush' directive");
2513 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
2514 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002515 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00002516 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
2517 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002518 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00002519 case OMPD_atomic:
2520 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
2521 EndLoc);
2522 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002523 case OMPD_teams:
2524 Res =
2525 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
2526 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002527 case OMPD_target:
2528 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
2529 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002530 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002531 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002532 case OMPD_cancellation_point:
2533 assert(ClausesWithImplicit.empty() &&
2534 "No clauses are allowed for 'omp cancellation point' directive");
2535 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
2536 "cancellation point' directive");
2537 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
2538 break;
Alexey Bataev80909872015-07-02 11:25:17 +00002539 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00002540 assert(AStmt == nullptr &&
2541 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00002542 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
2543 CancelRegion);
2544 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00002545 break;
Michael Wong65f367f2015-07-21 13:44:28 +00002546 case OMPD_target_data:
2547 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
2548 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002549 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00002550 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00002551 case OMPD_taskloop:
2552 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
2553 EndLoc, VarsWithInheritedDSA);
2554 AllowedNameModifiers.push_back(OMPD_taskloop);
2555 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002556 case OMPD_taskloop_simd:
2557 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2558 EndLoc, VarsWithInheritedDSA);
2559 AllowedNameModifiers.push_back(OMPD_taskloop);
2560 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002561 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002562 llvm_unreachable("OpenMP Directive is not allowed");
2563 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002564 llvm_unreachable("Unknown OpenMP directive");
2565 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002566
Alexey Bataev4acb8592014-07-07 13:01:15 +00002567 for (auto P : VarsWithInheritedDSA) {
2568 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
2569 << P.first << P.second->getSourceRange();
2570 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002571 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
2572
2573 if (!AllowedNameModifiers.empty())
2574 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
2575 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002576
Alexey Bataeved09d242014-05-28 05:53:51 +00002577 if (ErrorFound)
2578 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002579 return Res;
2580}
2581
2582StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
2583 Stmt *AStmt,
2584 SourceLocation StartLoc,
2585 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002586 if (!AStmt)
2587 return StmtError();
2588
Alexey Bataev9959db52014-05-06 10:08:46 +00002589 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
2590 // 1.2.2 OpenMP Language Terminology
2591 // Structured block - An executable statement with a single entry at the
2592 // top and a single exit at the bottom.
2593 // The point of exit cannot be a branch out of the structured block.
2594 // longjmp() and throw() must not violate the entry/exit criteria.
2595 CS->getCapturedDecl()->setNothrow();
2596
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002597 getCurFunction()->setHasBranchProtectedScope();
2598
Alexey Bataev25e5b442015-09-15 12:52:43 +00002599 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
2600 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002601}
2602
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002603namespace {
2604/// \brief Helper class for checking canonical form of the OpenMP loops and
2605/// extracting iteration space of each loop in the loop nest, that will be used
2606/// for IR generation.
2607class OpenMPIterationSpaceChecker {
2608 /// \brief Reference to Sema.
2609 Sema &SemaRef;
2610 /// \brief A location for diagnostics (when there is no some better location).
2611 SourceLocation DefaultLoc;
2612 /// \brief A location for diagnostics (when increment is not compatible).
2613 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002614 /// \brief A source location for referring to loop init later.
2615 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002616 /// \brief A source location for referring to condition later.
2617 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002618 /// \brief A source location for referring to increment later.
2619 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002620 /// \brief Loop variable.
2621 VarDecl *Var;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002622 /// \brief Reference to loop variable.
2623 DeclRefExpr *VarRef;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002624 /// \brief Lower bound (initializer for the var).
2625 Expr *LB;
2626 /// \brief Upper bound.
2627 Expr *UB;
2628 /// \brief Loop step (increment).
2629 Expr *Step;
2630 /// \brief This flag is true when condition is one of:
2631 /// Var < UB
2632 /// Var <= UB
2633 /// UB > Var
2634 /// UB >= Var
2635 bool TestIsLessOp;
2636 /// \brief This flag is true when condition is strict ( < or > ).
2637 bool TestIsStrictOp;
2638 /// \brief This flag is true when step is subtracted on each iteration.
2639 bool SubtractStep;
2640
2641public:
2642 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
2643 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
Alexander Musmana5f070a2014-10-01 06:03:56 +00002644 InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()),
2645 IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002646 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
2647 TestIsStrictOp(false), SubtractStep(false) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002648 /// \brief Check init-expr for canonical loop form and save loop counter
2649 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00002650 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002651 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
2652 /// for less/greater and for strict/non-strict comparison.
2653 bool CheckCond(Expr *S);
2654 /// \brief Check incr-expr for canonical loop form and return true if it
2655 /// does not conform, otherwise save loop step (#Step).
2656 bool CheckInc(Expr *S);
2657 /// \brief Return the loop counter variable.
2658 VarDecl *GetLoopVar() const { return Var; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002659 /// \brief Return the reference expression to loop counter variable.
2660 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002661 /// \brief Source range of the loop init.
2662 SourceRange GetInitSrcRange() const { return InitSrcRange; }
2663 /// \brief Source range of the loop condition.
2664 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
2665 /// \brief Source range of the loop increment.
2666 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
2667 /// \brief True if the step should be subtracted.
2668 bool ShouldSubtractStep() const { return SubtractStep; }
2669 /// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00002670 Expr *BuildNumIterations(Scope *S, const bool LimitedType) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00002671 /// \brief Build the precondition expression for the loops.
2672 Expr *BuildPreCond(Scope *S, Expr *Cond) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002673 /// \brief Build reference expression to the counter be used for codegen.
2674 Expr *BuildCounterVar() const;
Alexey Bataeva8899172015-08-06 12:30:57 +00002675 /// \brief Build reference expression to the private counter be used for
2676 /// codegen.
2677 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002678 /// \brief Build initization of the counter be used for codegen.
2679 Expr *BuildCounterInit() const;
2680 /// \brief Build step of the counter be used for codegen.
2681 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002682 /// \brief Return true if any expression is dependent.
2683 bool Dependent() const;
2684
2685private:
2686 /// \brief Check the right-hand side of an assignment in the increment
2687 /// expression.
2688 bool CheckIncRHS(Expr *RHS);
2689 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002690 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002691 /// \brief Helper to set upper bound.
Craig Toppere335f252015-10-04 04:53:55 +00002692 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00002693 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002694 /// \brief Helper to set loop increment.
2695 bool SetStep(Expr *NewStep, bool Subtract);
2696};
2697
2698bool OpenMPIterationSpaceChecker::Dependent() const {
2699 if (!Var) {
2700 assert(!LB && !UB && !Step);
2701 return false;
2702 }
2703 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
2704 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
2705}
2706
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002707template <typename T>
2708static T *getExprAsWritten(T *E) {
2709 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
2710 E = ExprTemp->getSubExpr();
2711
2712 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
2713 E = MTE->GetTemporaryExpr();
2714
2715 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
2716 E = Binder->getSubExpr();
2717
2718 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
2719 E = ICE->getSubExprAsWritten();
2720 return E->IgnoreParens();
2721}
2722
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002723bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
2724 DeclRefExpr *NewVarRefExpr,
2725 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002726 // State consistency checking to ensure correct usage.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002727 assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
2728 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002729 if (!NewVar || !NewLB)
2730 return true;
2731 Var = NewVar;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002732 VarRef = NewVarRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002733 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
2734 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00002735 if ((Ctor->isCopyOrMoveConstructor() ||
2736 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
2737 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002738 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002739 LB = NewLB;
2740 return false;
2741}
2742
2743bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00002744 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002745 // State consistency checking to ensure correct usage.
2746 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
2747 !TestIsLessOp && !TestIsStrictOp);
2748 if (!NewUB)
2749 return true;
2750 UB = NewUB;
2751 TestIsLessOp = LessOp;
2752 TestIsStrictOp = StrictOp;
2753 ConditionSrcRange = SR;
2754 ConditionLoc = SL;
2755 return false;
2756}
2757
2758bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
2759 // State consistency checking to ensure correct usage.
2760 assert(Var != nullptr && LB != nullptr && Step == nullptr);
2761 if (!NewStep)
2762 return true;
2763 if (!NewStep->isValueDependent()) {
2764 // Check that the step is integer expression.
2765 SourceLocation StepLoc = NewStep->getLocStart();
2766 ExprResult Val =
2767 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
2768 if (Val.isInvalid())
2769 return true;
2770 NewStep = Val.get();
2771
2772 // OpenMP [2.6, Canonical Loop Form, Restrictions]
2773 // If test-expr is of form var relational-op b and relational-op is < or
2774 // <= then incr-expr must cause var to increase on each iteration of the
2775 // loop. If test-expr is of form var relational-op b and relational-op is
2776 // > or >= then incr-expr must cause var to decrease on each iteration of
2777 // the loop.
2778 // If test-expr is of form b relational-op var and relational-op is < or
2779 // <= then incr-expr must cause var to decrease on each iteration of the
2780 // loop. If test-expr is of form b relational-op var and relational-op is
2781 // > or >= then incr-expr must cause var to increase on each iteration of
2782 // the loop.
2783 llvm::APSInt Result;
2784 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
2785 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
2786 bool IsConstNeg =
2787 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00002788 bool IsConstPos =
2789 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002790 bool IsConstZero = IsConstant && !Result.getBoolValue();
2791 if (UB && (IsConstZero ||
2792 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00002793 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002794 SemaRef.Diag(NewStep->getExprLoc(),
2795 diag::err_omp_loop_incr_not_compatible)
2796 << Var << TestIsLessOp << NewStep->getSourceRange();
2797 SemaRef.Diag(ConditionLoc,
2798 diag::note_omp_loop_cond_requres_compatible_incr)
2799 << TestIsLessOp << ConditionSrcRange;
2800 return true;
2801 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002802 if (TestIsLessOp == Subtract) {
2803 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
2804 NewStep).get();
2805 Subtract = !Subtract;
2806 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002807 }
2808
2809 Step = NewStep;
2810 SubtractStep = Subtract;
2811 return false;
2812}
2813
Alexey Bataev9c821032015-04-30 04:23:23 +00002814bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002815 // Check init-expr for canonical loop form and save loop counter
2816 // variable - #Var and its initialization value - #LB.
2817 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
2818 // var = lb
2819 // integer-type var = lb
2820 // random-access-iterator-type var = lb
2821 // pointer-type var = lb
2822 //
2823 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00002824 if (EmitDiags) {
2825 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
2826 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002827 return true;
2828 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002829 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002830 if (Expr *E = dyn_cast<Expr>(S))
2831 S = E->IgnoreParens();
2832 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2833 if (BO->getOpcode() == BO_Assign)
2834 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002835 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002836 BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002837 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
2838 if (DS->isSingleDecl()) {
2839 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00002840 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002841 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00002842 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002843 SemaRef.Diag(S->getLocStart(),
2844 diag::ext_omp_loop_not_canonical_init)
2845 << S->getSourceRange();
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002846 return SetVarAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002847 }
2848 }
2849 }
2850 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
2851 if (CE->getOperator() == OO_Equal)
2852 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002853 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
2854 CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002855
Alexey Bataev9c821032015-04-30 04:23:23 +00002856 if (EmitDiags) {
2857 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
2858 << S->getSourceRange();
2859 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002860 return true;
2861}
2862
Alexey Bataev23b69422014-06-18 07:08:49 +00002863/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002864/// variable (which may be the loop variable) if possible.
2865static const VarDecl *GetInitVarDecl(const Expr *E) {
2866 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00002867 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002868 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002869 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
2870 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00002871 if ((Ctor->isCopyOrMoveConstructor() ||
2872 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
2873 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002874 E = CE->getArg(0)->IgnoreParenImpCasts();
2875 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
2876 if (!DRE)
2877 return nullptr;
2878 return dyn_cast<VarDecl>(DRE->getDecl());
2879}
2880
2881bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
2882 // Check test-expr for canonical form, save upper-bound UB, flags for
2883 // less/greater and for strict/non-strict comparison.
2884 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2885 // var relational-op b
2886 // b relational-op var
2887 //
2888 if (!S) {
2889 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
2890 return true;
2891 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002892 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002893 SourceLocation CondLoc = S->getLocStart();
2894 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2895 if (BO->isRelationalOp()) {
2896 if (GetInitVarDecl(BO->getLHS()) == Var)
2897 return SetUB(BO->getRHS(),
2898 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
2899 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2900 BO->getSourceRange(), BO->getOperatorLoc());
2901 if (GetInitVarDecl(BO->getRHS()) == Var)
2902 return SetUB(BO->getLHS(),
2903 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
2904 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2905 BO->getSourceRange(), BO->getOperatorLoc());
2906 }
2907 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2908 if (CE->getNumArgs() == 2) {
2909 auto Op = CE->getOperator();
2910 switch (Op) {
2911 case OO_Greater:
2912 case OO_GreaterEqual:
2913 case OO_Less:
2914 case OO_LessEqual:
2915 if (GetInitVarDecl(CE->getArg(0)) == Var)
2916 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
2917 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2918 CE->getOperatorLoc());
2919 if (GetInitVarDecl(CE->getArg(1)) == Var)
2920 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
2921 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2922 CE->getOperatorLoc());
2923 break;
2924 default:
2925 break;
2926 }
2927 }
2928 }
2929 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
2930 << S->getSourceRange() << Var;
2931 return true;
2932}
2933
2934bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
2935 // RHS of canonical loop form increment can be:
2936 // var + incr
2937 // incr + var
2938 // var - incr
2939 //
2940 RHS = RHS->IgnoreParenImpCasts();
2941 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
2942 if (BO->isAdditiveOp()) {
2943 bool IsAdd = BO->getOpcode() == BO_Add;
2944 if (GetInitVarDecl(BO->getLHS()) == Var)
2945 return SetStep(BO->getRHS(), !IsAdd);
2946 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
2947 return SetStep(BO->getLHS(), false);
2948 }
2949 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
2950 bool IsAdd = CE->getOperator() == OO_Plus;
2951 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
2952 if (GetInitVarDecl(CE->getArg(0)) == Var)
2953 return SetStep(CE->getArg(1), !IsAdd);
2954 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
2955 return SetStep(CE->getArg(0), false);
2956 }
2957 }
2958 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2959 << RHS->getSourceRange() << Var;
2960 return true;
2961}
2962
2963bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
2964 // Check incr-expr for canonical loop form and return true if it
2965 // does not conform.
2966 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2967 // ++var
2968 // var++
2969 // --var
2970 // var--
2971 // var += incr
2972 // var -= incr
2973 // var = var + incr
2974 // var = incr + var
2975 // var = var - incr
2976 //
2977 if (!S) {
2978 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
2979 return true;
2980 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002981 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002982 S = S->IgnoreParens();
2983 if (auto UO = dyn_cast<UnaryOperator>(S)) {
2984 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
2985 return SetStep(
2986 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
2987 (UO->isDecrementOp() ? -1 : 1)).get(),
2988 false);
2989 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
2990 switch (BO->getOpcode()) {
2991 case BO_AddAssign:
2992 case BO_SubAssign:
2993 if (GetInitVarDecl(BO->getLHS()) == Var)
2994 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
2995 break;
2996 case BO_Assign:
2997 if (GetInitVarDecl(BO->getLHS()) == Var)
2998 return CheckIncRHS(BO->getRHS());
2999 break;
3000 default:
3001 break;
3002 }
3003 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3004 switch (CE->getOperator()) {
3005 case OO_PlusPlus:
3006 case OO_MinusMinus:
3007 if (GetInitVarDecl(CE->getArg(0)) == Var)
3008 return SetStep(
3009 SemaRef.ActOnIntegerConstant(
3010 CE->getLocStart(),
3011 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
3012 false);
3013 break;
3014 case OO_PlusEqual:
3015 case OO_MinusEqual:
3016 if (GetInitVarDecl(CE->getArg(0)) == Var)
3017 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
3018 break;
3019 case OO_Equal:
3020 if (GetInitVarDecl(CE->getArg(0)) == Var)
3021 return CheckIncRHS(CE->getArg(1));
3022 break;
3023 default:
3024 break;
3025 }
3026 }
3027 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3028 << S->getSourceRange() << Var;
3029 return true;
3030}
Alexander Musmana5f070a2014-10-01 06:03:56 +00003031
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003032namespace {
3033// Transform variables declared in GNU statement expressions to new ones to
3034// avoid crash on codegen.
3035class TransformToNewDefs : public TreeTransform<TransformToNewDefs> {
3036 typedef TreeTransform<TransformToNewDefs> BaseTransform;
3037
3038public:
3039 TransformToNewDefs(Sema &SemaRef) : BaseTransform(SemaRef) {}
3040
3041 Decl *TransformDefinition(SourceLocation Loc, Decl *D) {
3042 if (auto *VD = cast<VarDecl>(D))
3043 if (!isa<ParmVarDecl>(D) && !isa<VarTemplateSpecializationDecl>(D) &&
3044 !isa<ImplicitParamDecl>(D)) {
3045 auto *NewVD = VarDecl::Create(
3046 SemaRef.Context, VD->getDeclContext(), VD->getLocStart(),
3047 VD->getLocation(), VD->getIdentifier(), VD->getType(),
3048 VD->getTypeSourceInfo(), VD->getStorageClass());
3049 NewVD->setTSCSpec(VD->getTSCSpec());
3050 NewVD->setInit(VD->getInit());
3051 NewVD->setInitStyle(VD->getInitStyle());
3052 NewVD->setExceptionVariable(VD->isExceptionVariable());
3053 NewVD->setNRVOVariable(VD->isNRVOVariable());
3054 NewVD->setCXXForRangeDecl(VD->isInExternCXXContext());
3055 NewVD->setConstexpr(VD->isConstexpr());
3056 NewVD->setInitCapture(VD->isInitCapture());
3057 NewVD->setPreviousDeclInSameBlockScope(
3058 VD->isPreviousDeclInSameBlockScope());
3059 VD->getDeclContext()->addHiddenDecl(NewVD);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003060 if (VD->hasAttrs())
3061 NewVD->setAttrs(VD->getAttrs());
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003062 transformedLocalDecl(VD, NewVD);
3063 return NewVD;
3064 }
3065 return BaseTransform::TransformDefinition(Loc, D);
3066 }
3067
3068 ExprResult TransformDeclRefExpr(DeclRefExpr *E) {
3069 if (auto *NewD = TransformDecl(E->getExprLoc(), E->getDecl()))
3070 if (E->getDecl() != NewD) {
3071 NewD->setReferenced();
3072 NewD->markUsed(SemaRef.Context);
3073 return DeclRefExpr::Create(
3074 SemaRef.Context, E->getQualifierLoc(), E->getTemplateKeywordLoc(),
3075 cast<ValueDecl>(NewD), E->refersToEnclosingVariableOrCapture(),
3076 E->getNameInfo(), E->getType(), E->getValueKind());
3077 }
3078 return BaseTransform::TransformDeclRefExpr(E);
3079 }
3080};
3081}
3082
Alexander Musmana5f070a2014-10-01 06:03:56 +00003083/// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00003084Expr *
3085OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S,
3086 const bool LimitedType) const {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003087 TransformToNewDefs Transform(SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003088 ExprResult Diff;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003089 auto VarType = Var->getType().getNonReferenceType();
3090 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003091 SemaRef.getLangOpts().CPlusPlus) {
3092 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003093 auto *UBExpr = TestIsLessOp ? UB : LB;
3094 auto *LBExpr = TestIsLessOp ? LB : UB;
3095 Expr *Upper = Transform.TransformExpr(UBExpr).get();
3096 Expr *Lower = Transform.TransformExpr(LBExpr).get();
3097 if (!Upper || !Lower)
3098 return nullptr;
3099 Upper = SemaRef.PerformImplicitConversion(Upper, UBExpr->getType(),
3100 Sema::AA_Converting,
3101 /*AllowExplicit=*/true)
3102 .get();
3103 Lower = SemaRef.PerformImplicitConversion(Lower, LBExpr->getType(),
3104 Sema::AA_Converting,
3105 /*AllowExplicit=*/true)
3106 .get();
3107 if (!Upper || !Lower)
3108 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003109
3110 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
3111
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003112 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00003113 // BuildBinOp already emitted error, this one is to point user to upper
3114 // and lower bound, and to tell what is passed to 'operator-'.
3115 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
3116 << Upper->getSourceRange() << Lower->getSourceRange();
3117 return nullptr;
3118 }
3119 }
3120
3121 if (!Diff.isUsable())
3122 return nullptr;
3123
3124 // Upper - Lower [- 1]
3125 if (TestIsStrictOp)
3126 Diff = SemaRef.BuildBinOp(
3127 S, DefaultLoc, BO_Sub, Diff.get(),
3128 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3129 if (!Diff.isUsable())
3130 return nullptr;
3131
3132 // Upper - Lower [- 1] + Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003133 auto NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
3134 if (NewStep.isInvalid())
3135 return nullptr;
3136 NewStep = SemaRef.PerformImplicitConversion(
3137 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
3138 /*AllowExplicit=*/true);
3139 if (NewStep.isInvalid())
3140 return nullptr;
3141 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003142 if (!Diff.isUsable())
3143 return nullptr;
3144
3145 // Parentheses (for dumping/debugging purposes only).
3146 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
3147 if (!Diff.isUsable())
3148 return nullptr;
3149
3150 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003151 NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
3152 if (NewStep.isInvalid())
3153 return nullptr;
3154 NewStep = SemaRef.PerformImplicitConversion(
3155 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
3156 /*AllowExplicit=*/true);
3157 if (NewStep.isInvalid())
3158 return nullptr;
3159 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003160 if (!Diff.isUsable())
3161 return nullptr;
3162
Alexander Musman174b3ca2014-10-06 11:16:29 +00003163 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003164 QualType Type = Diff.get()->getType();
3165 auto &C = SemaRef.Context;
3166 bool UseVarType = VarType->hasIntegerRepresentation() &&
3167 C.getTypeSize(Type) > C.getTypeSize(VarType);
3168 if (!Type->isIntegerType() || UseVarType) {
3169 unsigned NewSize =
3170 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
3171 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
3172 : Type->hasSignedIntegerRepresentation();
3173 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
3174 Diff = SemaRef.PerformImplicitConversion(
3175 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
3176 if (!Diff.isUsable())
3177 return nullptr;
3178 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00003179 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00003180 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
3181 if (NewSize != C.getTypeSize(Type)) {
3182 if (NewSize < C.getTypeSize(Type)) {
3183 assert(NewSize == 64 && "incorrect loop var size");
3184 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
3185 << InitSrcRange << ConditionSrcRange;
3186 }
3187 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003188 NewSize, Type->hasSignedIntegerRepresentation() ||
3189 C.getTypeSize(Type) < NewSize);
Alexander Musman174b3ca2014-10-06 11:16:29 +00003190 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
3191 Sema::AA_Converting, true);
3192 if (!Diff.isUsable())
3193 return nullptr;
3194 }
3195 }
3196
Alexander Musmana5f070a2014-10-01 06:03:56 +00003197 return Diff.get();
3198}
3199
Alexey Bataev62dbb972015-04-22 11:59:37 +00003200Expr *OpenMPIterationSpaceChecker::BuildPreCond(Scope *S, Expr *Cond) const {
3201 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
3202 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
3203 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003204 TransformToNewDefs Transform(SemaRef);
3205
3206 auto NewLB = Transform.TransformExpr(LB);
3207 auto NewUB = Transform.TransformExpr(UB);
3208 if (NewLB.isInvalid() || NewUB.isInvalid())
3209 return Cond;
3210 NewLB = SemaRef.PerformImplicitConversion(NewLB.get(), LB->getType(),
3211 Sema::AA_Converting,
3212 /*AllowExplicit=*/true);
3213 NewUB = SemaRef.PerformImplicitConversion(NewUB.get(), UB->getType(),
3214 Sema::AA_Converting,
3215 /*AllowExplicit=*/true);
3216 if (NewLB.isInvalid() || NewUB.isInvalid())
3217 return Cond;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003218 auto CondExpr = SemaRef.BuildBinOp(
3219 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
3220 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003221 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003222 if (CondExpr.isUsable()) {
3223 CondExpr = SemaRef.PerformImplicitConversion(
3224 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
3225 /*AllowExplicit=*/true);
3226 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00003227 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
3228 // Otherwise use original loop conditon and evaluate it in runtime.
3229 return CondExpr.isUsable() ? CondExpr.get() : Cond;
3230}
3231
Alexander Musmana5f070a2014-10-01 06:03:56 +00003232/// \brief Build reference expression to the counter be used for codegen.
3233Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const {
Alexey Bataeva8899172015-08-06 12:30:57 +00003234 return buildDeclRefExpr(SemaRef, Var, Var->getType().getNonReferenceType(),
3235 DefaultLoc);
3236}
3237
3238Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
3239 if (Var && !Var->isInvalidDecl()) {
3240 auto Type = Var->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003241 auto *PrivateVar =
3242 buildVarDecl(SemaRef, DefaultLoc, Type, Var->getName(),
3243 Var->hasAttrs() ? &Var->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00003244 if (PrivateVar->isInvalidDecl())
3245 return nullptr;
3246 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
3247 }
3248 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003249}
3250
3251/// \brief Build initization of the counter be used for codegen.
3252Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
3253
3254/// \brief Build step of the counter be used for codegen.
3255Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
3256
3257/// \brief Iteration space of a single for loop.
3258struct LoopIterationSpace {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003259 /// \brief Condition of the loop.
3260 Expr *PreCond;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003261 /// \brief This expression calculates the number of iterations in the loop.
3262 /// It is always possible to calculate it before starting the loop.
3263 Expr *NumIterations;
3264 /// \brief The loop counter variable.
3265 Expr *CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00003266 /// \brief Private loop counter variable.
3267 Expr *PrivateCounterVar;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003268 /// \brief This is initializer for the initial value of #CounterVar.
3269 Expr *CounterInit;
3270 /// \brief This is step for the #CounterVar used to generate its update:
3271 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
3272 Expr *CounterStep;
3273 /// \brief Should step be subtracted?
3274 bool Subtract;
3275 /// \brief Source range of the loop init.
3276 SourceRange InitSrcRange;
3277 /// \brief Source range of the loop condition.
3278 SourceRange CondSrcRange;
3279 /// \brief Source range of the loop increment.
3280 SourceRange IncSrcRange;
3281};
3282
Alexey Bataev23b69422014-06-18 07:08:49 +00003283} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003284
Alexey Bataev9c821032015-04-30 04:23:23 +00003285void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
3286 assert(getLangOpts().OpenMP && "OpenMP is not active.");
3287 assert(Init && "Expected loop in canonical form.");
3288 unsigned CollapseIteration = DSAStack->getCollapseNumber();
3289 if (CollapseIteration > 0 &&
3290 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
3291 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
3292 if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
3293 DSAStack->addLoopControlVariable(ISC.GetLoopVar());
3294 }
3295 DSAStack->setCollapseNumber(CollapseIteration - 1);
3296 }
3297}
3298
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003299/// \brief Called on a for stmt to check and extract its iteration space
3300/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00003301static bool CheckOpenMPIterationSpace(
3302 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
3303 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003304 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003305 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
3306 LoopIterationSpace &ResultIterSpace) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003307 // OpenMP [2.6, Canonical Loop Form]
3308 // for (init-expr; test-expr; incr-expr) structured-block
3309 auto For = dyn_cast_or_null<ForStmt>(S);
3310 if (!For) {
3311 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003312 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
3313 << getOpenMPDirectiveName(DKind) << NestedLoopCount
3314 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
3315 if (NestedLoopCount > 1) {
3316 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
3317 SemaRef.Diag(DSA.getConstructLoc(),
3318 diag::note_omp_collapse_ordered_expr)
3319 << 2 << CollapseLoopCountExpr->getSourceRange()
3320 << OrderedLoopCountExpr->getSourceRange();
3321 else if (CollapseLoopCountExpr)
3322 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
3323 diag::note_omp_collapse_ordered_expr)
3324 << 0 << CollapseLoopCountExpr->getSourceRange();
3325 else
3326 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
3327 diag::note_omp_collapse_ordered_expr)
3328 << 1 << OrderedLoopCountExpr->getSourceRange();
3329 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003330 return true;
3331 }
3332 assert(For->getBody());
3333
3334 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
3335
3336 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003337 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003338 if (ISC.CheckInit(Init)) {
3339 return true;
3340 }
3341
3342 bool HasErrors = false;
3343
3344 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003345 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003346
3347 // OpenMP [2.6, Canonical Loop Form]
3348 // Var is one of the following:
3349 // A variable of signed or unsigned integer type.
3350 // For C++, a variable of a random access iterator type.
3351 // For C, a variable of a pointer type.
Alexey Bataeva8899172015-08-06 12:30:57 +00003352 auto VarType = Var->getType().getNonReferenceType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003353 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
3354 !VarType->isPointerType() &&
3355 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
3356 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
3357 << SemaRef.getLangOpts().CPlusPlus;
3358 HasErrors = true;
3359 }
3360
Alexey Bataev4acb8592014-07-07 13:01:15 +00003361 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
3362 // Construct
3363 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3364 // parallel for construct is (are) private.
3365 // The loop iteration variable in the associated for-loop of a simd construct
3366 // with just one associated for-loop is linear with a constant-linear-step
3367 // that is the increment of the associated for-loop.
3368 // Exclude loop var from the list of variables with implicitly defined data
3369 // sharing attributes.
Benjamin Kramerad8e0792014-10-10 15:32:48 +00003370 VarsWithImplicitDSA.erase(Var);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003371
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003372 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
3373 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00003374 // The loop iteration variable in the associated for-loop of a simd construct
3375 // with just one associated for-loop may be listed in a linear clause with a
3376 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00003377 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3378 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003379 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003380 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
3381 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
3382 // declared in the loop and it is predetermined as a private.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003383 auto PredeterminedCKind =
3384 isOpenMPSimdDirective(DKind)
3385 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
3386 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003387 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataev0c024df2015-05-12 09:02:07 +00003388 DVar.CKind != OMPC_threadprivate && DVar.CKind != PredeterminedCKind) ||
NAKAMURA Takumi2d5c6dd2015-12-09 04:35:57 +00003389 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop) &&
Alexey Bataev49f6e782015-12-01 04:18:41 +00003390 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
3391 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate &&
3392 DVar.CKind != OMPC_threadprivate)) &&
Alexey Bataev0c024df2015-05-12 09:02:07 +00003393 ((DVar.CKind != OMPC_private && DVar.CKind != OMPC_threadprivate) ||
3394 DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003395 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00003396 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
3397 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataevf2453a02015-05-06 07:25:08 +00003398 if (DVar.RefExpr == nullptr)
3399 DVar.CKind = PredeterminedCKind;
3400 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, /*IsLoopIterVar=*/true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003401 HasErrors = true;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003402 } else if (LoopVarRefExpr != nullptr) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00003403 // Make the loop iteration variable private (for worksharing constructs),
3404 // linear (for simd directives with the only one associated loop) or
Alexey Bataev10e775f2015-07-30 11:36:16 +00003405 // lastprivate (for simd directives with several collapsed or ordered
3406 // loops).
Alexey Bataev9aba41c2014-11-14 04:08:45 +00003407 if (DVar.CKind == OMPC_unknown)
3408 DVar = DSA.hasDSA(Var, isOpenMPPrivate, MatchesAlways(),
3409 /*FromParent=*/false);
Alexey Bataev9c821032015-04-30 04:23:23 +00003410 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003411 }
3412
Alexey Bataev7ff55242014-06-19 09:13:45 +00003413 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00003414
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003415 // Check test-expr.
3416 HasErrors |= ISC.CheckCond(For->getCond());
3417
3418 // Check incr-expr.
3419 HasErrors |= ISC.CheckInc(For->getInc());
3420
Alexander Musmana5f070a2014-10-01 06:03:56 +00003421 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003422 return HasErrors;
3423
Alexander Musmana5f070a2014-10-01 06:03:56 +00003424 // Build the loop's iteration space representation.
Alexey Bataev62dbb972015-04-22 11:59:37 +00003425 ResultIterSpace.PreCond = ISC.BuildPreCond(DSA.getCurScope(), For->getCond());
Alexander Musman174b3ca2014-10-06 11:16:29 +00003426 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003427 DSA.getCurScope(), (isOpenMPWorksharingDirective(DKind) ||
NAKAMURA Takumi2d5c6dd2015-12-09 04:35:57 +00003428 isOpenMPTaskLoopDirective(DKind)));
Alexander Musmana5f070a2014-10-01 06:03:56 +00003429 ResultIterSpace.CounterVar = ISC.BuildCounterVar();
Alexey Bataeva8899172015-08-06 12:30:57 +00003430 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003431 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
3432 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
3433 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
3434 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
3435 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
3436 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
3437
Alexey Bataev62dbb972015-04-22 11:59:37 +00003438 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
3439 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003440 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00003441 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003442 ResultIterSpace.CounterInit == nullptr ||
3443 ResultIterSpace.CounterStep == nullptr);
3444
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003445 return HasErrors;
3446}
3447
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003448/// \brief Build 'VarRef = Start.
3449static ExprResult BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc,
3450 ExprResult VarRef, ExprResult Start) {
3451 TransformToNewDefs Transform(SemaRef);
3452 // Build 'VarRef = Start.
3453 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
3454 if (NewStart.isInvalid())
3455 return ExprError();
3456 NewStart = SemaRef.PerformImplicitConversion(
3457 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
3458 Sema::AA_Converting,
3459 /*AllowExplicit=*/true);
3460 if (NewStart.isInvalid())
3461 return ExprError();
3462 NewStart = SemaRef.PerformImplicitConversion(
3463 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
3464 /*AllowExplicit=*/true);
3465 if (!NewStart.isUsable())
3466 return ExprError();
3467
3468 auto Init =
3469 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
3470 return Init;
3471}
3472
Alexander Musmana5f070a2014-10-01 06:03:56 +00003473/// \brief Build 'VarRef = Start + Iter * Step'.
3474static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S,
3475 SourceLocation Loc, ExprResult VarRef,
3476 ExprResult Start, ExprResult Iter,
3477 ExprResult Step, bool Subtract) {
3478 // Add parentheses (for debugging purposes only).
3479 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
3480 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
3481 !Step.isUsable())
3482 return ExprError();
3483
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003484 TransformToNewDefs Transform(SemaRef);
3485 auto NewStep = Transform.TransformExpr(Step.get()->IgnoreImplicit());
3486 if (NewStep.isInvalid())
3487 return ExprError();
3488 NewStep = SemaRef.PerformImplicitConversion(
3489 NewStep.get(), Step.get()->IgnoreImplicit()->getType(),
3490 Sema::AA_Converting,
3491 /*AllowExplicit=*/true);
3492 if (NewStep.isInvalid())
3493 return ExprError();
3494 ExprResult Update =
3495 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003496 if (!Update.isUsable())
3497 return ExprError();
3498
3499 // Build 'VarRef = Start + Iter * Step'.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003500 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
3501 if (NewStart.isInvalid())
3502 return ExprError();
3503 NewStart = SemaRef.PerformImplicitConversion(
3504 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
3505 Sema::AA_Converting,
3506 /*AllowExplicit=*/true);
3507 if (NewStart.isInvalid())
3508 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003509 Update = SemaRef.BuildBinOp(S, Loc, (Subtract ? BO_Sub : BO_Add),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003510 NewStart.get(), Update.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003511 if (!Update.isUsable())
3512 return ExprError();
3513
3514 Update = SemaRef.PerformImplicitConversion(
3515 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
3516 if (!Update.isUsable())
3517 return ExprError();
3518
3519 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
3520 return Update;
3521}
3522
3523/// \brief Convert integer expression \a E to make it have at least \a Bits
3524/// bits.
3525static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
3526 Sema &SemaRef) {
3527 if (E == nullptr)
3528 return ExprError();
3529 auto &C = SemaRef.Context;
3530 QualType OldType = E->getType();
3531 unsigned HasBits = C.getTypeSize(OldType);
3532 if (HasBits >= Bits)
3533 return ExprResult(E);
3534 // OK to convert to signed, because new type has more bits than old.
3535 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
3536 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
3537 true);
3538}
3539
3540/// \brief Check if the given expression \a E is a constant integer that fits
3541/// into \a Bits bits.
3542static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
3543 if (E == nullptr)
3544 return false;
3545 llvm::APSInt Result;
3546 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
3547 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
3548 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003549}
3550
3551/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00003552/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
3553/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003554static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00003555CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
3556 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
3557 DSAStackTy &DSA,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003558 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00003559 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003560 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003561 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003562 // Found 'collapse' clause - calculate collapse number.
3563 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003564 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00003565 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00003566 }
3567 if (OrderedLoopCountExpr) {
3568 // Found 'ordered' clause - calculate collapse number.
3569 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00003570 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
3571 if (Result.getLimitedValue() < NestedLoopCount) {
3572 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
3573 diag::err_omp_wrong_ordered_loop_count)
3574 << OrderedLoopCountExpr->getSourceRange();
3575 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
3576 diag::note_collapse_loop_count)
3577 << CollapseLoopCountExpr->getSourceRange();
3578 }
3579 NestedLoopCount = Result.getLimitedValue();
3580 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003581 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003582 // This is helper routine for loop directives (e.g., 'for', 'simd',
3583 // 'for simd', etc.).
Alexander Musmana5f070a2014-10-01 06:03:56 +00003584 SmallVector<LoopIterationSpace, 4> IterSpaces;
3585 IterSpaces.resize(NestedLoopCount);
3586 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003587 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003588 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003589 NestedLoopCount, CollapseLoopCountExpr,
3590 OrderedLoopCountExpr, VarsWithImplicitDSA,
3591 IterSpaces[Cnt]))
Alexey Bataevabfc0692014-06-25 06:52:00 +00003592 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003593 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003594 // OpenMP [2.8.1, simd construct, Restrictions]
3595 // All loops associated with the construct must be perfectly nested; that
3596 // is, there must be no intervening code nor any OpenMP directive between
3597 // any two loops.
3598 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003599 }
3600
Alexander Musmana5f070a2014-10-01 06:03:56 +00003601 Built.clear(/* size */ NestedLoopCount);
3602
3603 if (SemaRef.CurContext->isDependentContext())
3604 return NestedLoopCount;
3605
3606 // An example of what is generated for the following code:
3607 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00003608 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00003609 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003610 // for (k = 0; k < NK; ++k)
3611 // for (j = J0; j < NJ; j+=2) {
3612 // <loop body>
3613 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003614 //
3615 // We generate the code below.
3616 // Note: the loop body may be outlined in CodeGen.
3617 // Note: some counters may be C++ classes, operator- is used to find number of
3618 // iterations and operator+= to calculate counter value.
3619 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
3620 // or i64 is currently supported).
3621 //
3622 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
3623 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
3624 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
3625 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
3626 // // similar updates for vars in clauses (e.g. 'linear')
3627 // <loop body (using local i and j)>
3628 // }
3629 // i = NI; // assign final values of counters
3630 // j = NJ;
3631 //
3632
3633 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
3634 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00003635 // Precondition tests if there is at least one iteration (all conditions are
3636 // true).
3637 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003638 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003639 ExprResult LastIteration32 = WidenIterationCount(
3640 32 /* Bits */, SemaRef.PerformImplicitConversion(
3641 N0->IgnoreImpCasts(), N0->getType(),
3642 Sema::AA_Converting, /*AllowExplicit=*/true)
3643 .get(),
3644 SemaRef);
3645 ExprResult LastIteration64 = WidenIterationCount(
3646 64 /* Bits */, SemaRef.PerformImplicitConversion(
3647 N0->IgnoreImpCasts(), N0->getType(),
3648 Sema::AA_Converting, /*AllowExplicit=*/true)
3649 .get(),
3650 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003651
3652 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
3653 return NestedLoopCount;
3654
3655 auto &C = SemaRef.Context;
3656 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
3657
3658 Scope *CurScope = DSA.getCurScope();
3659 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003660 if (PreCond.isUsable()) {
3661 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
3662 PreCond.get(), IterSpaces[Cnt].PreCond);
3663 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003664 auto N = IterSpaces[Cnt].NumIterations;
3665 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
3666 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003667 LastIteration32 = SemaRef.BuildBinOp(
3668 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
3669 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
3670 Sema::AA_Converting,
3671 /*AllowExplicit=*/true)
3672 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003673 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003674 LastIteration64 = SemaRef.BuildBinOp(
3675 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
3676 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
3677 Sema::AA_Converting,
3678 /*AllowExplicit=*/true)
3679 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003680 }
3681
3682 // Choose either the 32-bit or 64-bit version.
3683 ExprResult LastIteration = LastIteration64;
3684 if (LastIteration32.isUsable() &&
3685 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
3686 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
3687 FitsInto(
3688 32 /* Bits */,
3689 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
3690 LastIteration64.get(), SemaRef)))
3691 LastIteration = LastIteration32;
3692
3693 if (!LastIteration.isUsable())
3694 return 0;
3695
3696 // Save the number of iterations.
3697 ExprResult NumIterations = LastIteration;
3698 {
3699 LastIteration = SemaRef.BuildBinOp(
3700 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
3701 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3702 if (!LastIteration.isUsable())
3703 return 0;
3704 }
3705
3706 // Calculate the last iteration number beforehand instead of doing this on
3707 // each iteration. Do not do this if the number of iterations may be kfold-ed.
3708 llvm::APSInt Result;
3709 bool IsConstant =
3710 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
3711 ExprResult CalcLastIteration;
3712 if (!IsConstant) {
3713 SourceLocation SaveLoc;
3714 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00003715 buildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00003716 ".omp.last.iteration");
Alexey Bataev39f915b82015-05-08 10:41:21 +00003717 ExprResult SaveRef = buildDeclRefExpr(
3718 SemaRef, SaveVar, LastIteration.get()->getType(), SaveLoc);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003719 CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign,
3720 SaveRef.get(), LastIteration.get());
3721 LastIteration = SaveRef;
3722
3723 // Prepare SaveRef + 1.
3724 NumIterations = SemaRef.BuildBinOp(
3725 CurScope, SaveLoc, BO_Add, SaveRef.get(),
3726 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3727 if (!NumIterations.isUsable())
3728 return 0;
3729 }
3730
3731 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
3732
Alexander Musmanc6388682014-12-15 07:07:06 +00003733 QualType VType = LastIteration.get()->getType();
3734 // Build variables passed into runtime, nesessary for worksharing directives.
3735 ExprResult LB, UB, IL, ST, EUB;
NAKAMURA Takumi2d5c6dd2015-12-09 04:35:57 +00003736 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00003737 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00003738 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
3739 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003740 SemaRef.AddInitializerToDecl(
3741 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
3742 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3743
3744 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00003745 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
3746 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003747 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
3748 /*DirectInit*/ false,
3749 /*TypeMayContainAuto*/ false);
3750
3751 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
3752 // This will be used to implement clause 'lastprivate'.
3753 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00003754 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
3755 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003756 SemaRef.AddInitializerToDecl(
3757 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
3758 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3759
3760 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev39f915b82015-05-08 10:41:21 +00003761 VarDecl *STDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.stride");
3762 ST = buildDeclRefExpr(SemaRef, STDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003763 SemaRef.AddInitializerToDecl(
3764 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
3765 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3766
3767 // Build expression: UB = min(UB, LastIteration)
3768 // It is nesessary for CodeGen of directives with static scheduling.
3769 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
3770 UB.get(), LastIteration.get());
3771 ExprResult CondOp = SemaRef.ActOnConditionalOp(
3772 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
3773 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
3774 CondOp.get());
3775 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
3776 }
3777
3778 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003779 ExprResult IV;
3780 ExprResult Init;
3781 {
Alexey Bataev39f915b82015-05-08 10:41:21 +00003782 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.iv");
3783 IV = buildDeclRefExpr(SemaRef, IVDecl, VType, InitLoc);
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003784 Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
NAKAMURA Takumi2d5c6dd2015-12-09 04:35:57 +00003785 isOpenMPTaskLoopDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00003786 ? LB.get()
3787 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
3788 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
3789 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003790 }
3791
Alexander Musmanc6388682014-12-15 07:07:06 +00003792 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003793 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00003794 ExprResult Cond =
NAKAMURA Takumi2d5c6dd2015-12-09 04:35:57 +00003795 (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00003796 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
3797 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
3798 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003799
3800 // Loop increment (IV = IV + 1)
3801 SourceLocation IncLoc;
3802 ExprResult Inc =
3803 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
3804 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
3805 if (!Inc.isUsable())
3806 return 0;
3807 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00003808 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
3809 if (!Inc.isUsable())
3810 return 0;
3811
3812 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
3813 // Used for directives with static scheduling.
3814 ExprResult NextLB, NextUB;
NAKAMURA Takumi2d5c6dd2015-12-09 04:35:57 +00003815 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00003816 // LB + ST
3817 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
3818 if (!NextLB.isUsable())
3819 return 0;
3820 // LB = LB + ST
3821 NextLB =
3822 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
3823 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
3824 if (!NextLB.isUsable())
3825 return 0;
3826 // UB + ST
3827 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
3828 if (!NextUB.isUsable())
3829 return 0;
3830 // UB = UB + ST
3831 NextUB =
3832 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
3833 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
3834 if (!NextUB.isUsable())
3835 return 0;
3836 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003837
3838 // Build updates and final values of the loop counters.
3839 bool HasErrors = false;
3840 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003841 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003842 Built.Updates.resize(NestedLoopCount);
3843 Built.Finals.resize(NestedLoopCount);
3844 {
3845 ExprResult Div;
3846 // Go from inner nested loop to outer.
3847 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
3848 LoopIterationSpace &IS = IterSpaces[Cnt];
3849 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
3850 // Build: Iter = (IV / Div) % IS.NumIters
3851 // where Div is product of previous iterations' IS.NumIters.
3852 ExprResult Iter;
3853 if (Div.isUsable()) {
3854 Iter =
3855 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
3856 } else {
3857 Iter = IV;
3858 assert((Cnt == (int)NestedLoopCount - 1) &&
3859 "unusable div expected on first iteration only");
3860 }
3861
3862 if (Cnt != 0 && Iter.isUsable())
3863 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
3864 IS.NumIterations);
3865 if (!Iter.isUsable()) {
3866 HasErrors = true;
3867 break;
3868 }
3869
Alexey Bataev39f915b82015-05-08 10:41:21 +00003870 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
3871 auto *CounterVar = buildDeclRefExpr(
3872 SemaRef, cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()),
3873 IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
3874 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003875 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
3876 IS.CounterInit);
3877 if (!Init.isUsable()) {
3878 HasErrors = true;
3879 break;
3880 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003881 ExprResult Update =
Alexey Bataev39f915b82015-05-08 10:41:21 +00003882 BuildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003883 IS.CounterInit, Iter, IS.CounterStep, IS.Subtract);
3884 if (!Update.isUsable()) {
3885 HasErrors = true;
3886 break;
3887 }
3888
3889 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
3890 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00003891 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003892 IS.NumIterations, IS.CounterStep, IS.Subtract);
3893 if (!Final.isUsable()) {
3894 HasErrors = true;
3895 break;
3896 }
3897
3898 // Build Div for the next iteration: Div <- Div * IS.NumIters
3899 if (Cnt != 0) {
3900 if (Div.isUnset())
3901 Div = IS.NumIterations;
3902 else
3903 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
3904 IS.NumIterations);
3905
3906 // Add parentheses (for debugging purposes only).
3907 if (Div.isUsable())
3908 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
3909 if (!Div.isUsable()) {
3910 HasErrors = true;
3911 break;
3912 }
3913 }
3914 if (!Update.isUsable() || !Final.isUsable()) {
3915 HasErrors = true;
3916 break;
3917 }
3918 // Save results
3919 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00003920 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003921 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003922 Built.Updates[Cnt] = Update.get();
3923 Built.Finals[Cnt] = Final.get();
3924 }
3925 }
3926
3927 if (HasErrors)
3928 return 0;
3929
3930 // Save results
3931 Built.IterationVarRef = IV.get();
3932 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00003933 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003934 Built.CalcLastIteration =
3935 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003936 Built.PreCond = PreCond.get();
3937 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003938 Built.Init = Init.get();
3939 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00003940 Built.LB = LB.get();
3941 Built.UB = UB.get();
3942 Built.IL = IL.get();
3943 Built.ST = ST.get();
3944 Built.EUB = EUB.get();
3945 Built.NLB = NextLB.get();
3946 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003947
Alexey Bataevabfc0692014-06-25 06:52:00 +00003948 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003949}
3950
Alexey Bataev10e775f2015-07-30 11:36:16 +00003951static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00003952 auto CollapseClauses =
3953 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
3954 if (CollapseClauses.begin() != CollapseClauses.end())
3955 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003956 return nullptr;
3957}
3958
Alexey Bataev10e775f2015-07-30 11:36:16 +00003959static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00003960 auto OrderedClauses =
3961 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
3962 if (OrderedClauses.begin() != OrderedClauses.end())
3963 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00003964 return nullptr;
3965}
3966
Alexey Bataev66b15b52015-08-21 11:14:16 +00003967static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen,
3968 const Expr *Safelen) {
3969 llvm::APSInt SimdlenRes, SafelenRes;
3970 if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() ||
3971 Simdlen->isInstantiationDependent() ||
3972 Simdlen->containsUnexpandedParameterPack())
3973 return false;
3974 if (Safelen->isValueDependent() || Safelen->isTypeDependent() ||
3975 Safelen->isInstantiationDependent() ||
3976 Safelen->containsUnexpandedParameterPack())
3977 return false;
3978 Simdlen->EvaluateAsInt(SimdlenRes, S.Context);
3979 Safelen->EvaluateAsInt(SafelenRes, S.Context);
3980 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
3981 // If both simdlen and safelen clauses are specified, the value of the simdlen
3982 // parameter must be less than or equal to the value of the safelen parameter.
3983 if (SimdlenRes > SafelenRes) {
3984 S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values)
3985 << Simdlen->getSourceRange() << Safelen->getSourceRange();
3986 return true;
3987 }
3988 return false;
3989}
3990
Alexey Bataev4acb8592014-07-07 13:01:15 +00003991StmtResult Sema::ActOnOpenMPSimdDirective(
3992 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3993 SourceLocation EndLoc,
3994 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003995 if (!AStmt)
3996 return StmtError();
3997
3998 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00003999 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004000 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4001 // define the nested loops number.
4002 unsigned NestedLoopCount = CheckOpenMPLoop(
4003 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4004 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004005 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004006 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004007
Alexander Musmana5f070a2014-10-01 06:03:56 +00004008 assert((CurContext->isDependentContext() || B.builtAll()) &&
4009 "omp simd loop exprs were not built");
4010
Alexander Musman3276a272015-03-21 10:12:56 +00004011 if (!CurContext->isDependentContext()) {
4012 // Finalize the clauses that need pre-built expressions for CodeGen.
4013 for (auto C : Clauses) {
4014 if (auto LC = dyn_cast<OMPLinearClause>(C))
4015 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4016 B.NumIterations, *this, CurScope))
4017 return StmtError();
4018 }
4019 }
4020
Alexey Bataev66b15b52015-08-21 11:14:16 +00004021 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4022 // If both simdlen and safelen clauses are specified, the value of the simdlen
4023 // parameter must be less than or equal to the value of the safelen parameter.
4024 OMPSafelenClause *Safelen = nullptr;
4025 OMPSimdlenClause *Simdlen = nullptr;
4026 for (auto *Clause : Clauses) {
4027 if (Clause->getClauseKind() == OMPC_safelen)
4028 Safelen = cast<OMPSafelenClause>(Clause);
4029 else if (Clause->getClauseKind() == OMPC_simdlen)
4030 Simdlen = cast<OMPSimdlenClause>(Clause);
4031 if (Safelen && Simdlen)
4032 break;
4033 }
4034 if (Simdlen && Safelen &&
4035 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4036 Safelen->getSafelen()))
4037 return StmtError();
4038
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004039 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004040 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4041 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004042}
4043
Alexey Bataev4acb8592014-07-07 13:01:15 +00004044StmtResult Sema::ActOnOpenMPForDirective(
4045 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4046 SourceLocation EndLoc,
4047 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004048 if (!AStmt)
4049 return StmtError();
4050
4051 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004052 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004053 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4054 // define the nested loops number.
4055 unsigned NestedLoopCount = CheckOpenMPLoop(
4056 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4057 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004058 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00004059 return StmtError();
4060
Alexander Musmana5f070a2014-10-01 06:03:56 +00004061 assert((CurContext->isDependentContext() || B.builtAll()) &&
4062 "omp for loop exprs were not built");
4063
Alexey Bataev54acd402015-08-04 11:18:19 +00004064 if (!CurContext->isDependentContext()) {
4065 // Finalize the clauses that need pre-built expressions for CodeGen.
4066 for (auto C : Clauses) {
4067 if (auto LC = dyn_cast<OMPLinearClause>(C))
4068 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4069 B.NumIterations, *this, CurScope))
4070 return StmtError();
4071 }
4072 }
4073
Alexey Bataevf29276e2014-06-18 04:14:57 +00004074 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004075 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004076 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00004077}
4078
Alexander Musmanf82886e2014-09-18 05:12:34 +00004079StmtResult Sema::ActOnOpenMPForSimdDirective(
4080 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4081 SourceLocation EndLoc,
4082 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004083 if (!AStmt)
4084 return StmtError();
4085
4086 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004087 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004088 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4089 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00004090 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004091 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
4092 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4093 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004094 if (NestedLoopCount == 0)
4095 return StmtError();
4096
Alexander Musmanc6388682014-12-15 07:07:06 +00004097 assert((CurContext->isDependentContext() || B.builtAll()) &&
4098 "omp for simd loop exprs were not built");
4099
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00004100 if (!CurContext->isDependentContext()) {
4101 // Finalize the clauses that need pre-built expressions for CodeGen.
4102 for (auto C : Clauses) {
4103 if (auto LC = dyn_cast<OMPLinearClause>(C))
4104 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4105 B.NumIterations, *this, CurScope))
4106 return StmtError();
4107 }
4108 }
4109
Alexey Bataev66b15b52015-08-21 11:14:16 +00004110 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4111 // If both simdlen and safelen clauses are specified, the value of the simdlen
4112 // parameter must be less than or equal to the value of the safelen parameter.
4113 OMPSafelenClause *Safelen = nullptr;
4114 OMPSimdlenClause *Simdlen = nullptr;
4115 for (auto *Clause : Clauses) {
4116 if (Clause->getClauseKind() == OMPC_safelen)
4117 Safelen = cast<OMPSafelenClause>(Clause);
4118 else if (Clause->getClauseKind() == OMPC_simdlen)
4119 Simdlen = cast<OMPSimdlenClause>(Clause);
4120 if (Safelen && Simdlen)
4121 break;
4122 }
4123 if (Simdlen && Safelen &&
4124 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4125 Safelen->getSafelen()))
4126 return StmtError();
4127
Alexander Musmanf82886e2014-09-18 05:12:34 +00004128 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004129 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4130 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004131}
4132
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004133StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
4134 Stmt *AStmt,
4135 SourceLocation StartLoc,
4136 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004137 if (!AStmt)
4138 return StmtError();
4139
4140 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004141 auto BaseStmt = AStmt;
4142 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4143 BaseStmt = CS->getCapturedStmt();
4144 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4145 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004146 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004147 return StmtError();
4148 // All associated statements must be '#pragma omp section' except for
4149 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004150 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004151 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4152 if (SectionStmt)
4153 Diag(SectionStmt->getLocStart(),
4154 diag::err_omp_sections_substmt_not_section);
4155 return StmtError();
4156 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004157 cast<OMPSectionDirective>(SectionStmt)
4158 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004159 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004160 } else {
4161 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
4162 return StmtError();
4163 }
4164
4165 getCurFunction()->setHasBranchProtectedScope();
4166
Alexey Bataev25e5b442015-09-15 12:52:43 +00004167 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4168 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004169}
4170
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004171StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
4172 SourceLocation StartLoc,
4173 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004174 if (!AStmt)
4175 return StmtError();
4176
4177 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004178
4179 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00004180 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004181
Alexey Bataev25e5b442015-09-15 12:52:43 +00004182 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
4183 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004184}
4185
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004186StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
4187 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 Bataev74a05c92014-07-15 02:55:09 +00004194
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004195 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00004196
Alexey Bataev3255bf32015-01-19 05:20:46 +00004197 // OpenMP [2.7.3, single Construct, Restrictions]
4198 // The copyprivate clause must not be used with the nowait clause.
4199 OMPClause *Nowait = nullptr;
4200 OMPClause *Copyprivate = nullptr;
4201 for (auto *Clause : Clauses) {
4202 if (Clause->getClauseKind() == OMPC_nowait)
4203 Nowait = Clause;
4204 else if (Clause->getClauseKind() == OMPC_copyprivate)
4205 Copyprivate = Clause;
4206 if (Copyprivate && Nowait) {
4207 Diag(Copyprivate->getLocStart(),
4208 diag::err_omp_single_copyprivate_with_nowait);
4209 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
4210 return StmtError();
4211 }
4212 }
4213
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004214 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4215}
4216
Alexander Musman80c22892014-07-17 08:54:58 +00004217StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
4218 SourceLocation StartLoc,
4219 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004220 if (!AStmt)
4221 return StmtError();
4222
4223 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00004224
4225 getCurFunction()->setHasBranchProtectedScope();
4226
4227 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
4228}
4229
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004230StmtResult
4231Sema::ActOnOpenMPCriticalDirective(const DeclarationNameInfo &DirName,
4232 Stmt *AStmt, SourceLocation StartLoc,
4233 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004234 if (!AStmt)
4235 return StmtError();
4236
4237 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004238
4239 getCurFunction()->setHasBranchProtectedScope();
4240
4241 return OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
4242 AStmt);
4243}
4244
Alexey Bataev4acb8592014-07-07 13:01:15 +00004245StmtResult Sema::ActOnOpenMPParallelForDirective(
4246 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4247 SourceLocation EndLoc,
4248 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004249 if (!AStmt)
4250 return StmtError();
4251
Alexey Bataev4acb8592014-07-07 13:01:15 +00004252 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4253 // 1.2.2 OpenMP Language Terminology
4254 // Structured block - An executable statement with a single entry at the
4255 // top and a single exit at the bottom.
4256 // The point of exit cannot be a branch out of the structured block.
4257 // longjmp() and throw() must not violate the entry/exit criteria.
4258 CS->getCapturedDecl()->setNothrow();
4259
Alexander Musmanc6388682014-12-15 07:07:06 +00004260 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004261 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4262 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004263 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004264 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
4265 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4266 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00004267 if (NestedLoopCount == 0)
4268 return StmtError();
4269
Alexander Musmana5f070a2014-10-01 06:03:56 +00004270 assert((CurContext->isDependentContext() || B.builtAll()) &&
4271 "omp parallel for loop exprs were not built");
4272
Alexey Bataev54acd402015-08-04 11:18:19 +00004273 if (!CurContext->isDependentContext()) {
4274 // Finalize the clauses that need pre-built expressions for CodeGen.
4275 for (auto C : Clauses) {
4276 if (auto LC = dyn_cast<OMPLinearClause>(C))
4277 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4278 B.NumIterations, *this, CurScope))
4279 return StmtError();
4280 }
4281 }
4282
Alexey Bataev4acb8592014-07-07 13:01:15 +00004283 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004284 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004285 NestedLoopCount, Clauses, AStmt, B,
4286 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00004287}
4288
Alexander Musmane4e893b2014-09-23 09:33:00 +00004289StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
4290 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4291 SourceLocation EndLoc,
4292 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004293 if (!AStmt)
4294 return StmtError();
4295
Alexander Musmane4e893b2014-09-23 09:33:00 +00004296 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4297 // 1.2.2 OpenMP Language Terminology
4298 // Structured block - An executable statement with a single entry at the
4299 // top and a single exit at the bottom.
4300 // The point of exit cannot be a branch out of the structured block.
4301 // longjmp() and throw() must not violate the entry/exit criteria.
4302 CS->getCapturedDecl()->setNothrow();
4303
Alexander Musmanc6388682014-12-15 07:07:06 +00004304 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004305 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4306 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00004307 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004308 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
4309 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4310 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004311 if (NestedLoopCount == 0)
4312 return StmtError();
4313
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00004314 if (!CurContext->isDependentContext()) {
4315 // Finalize the clauses that need pre-built expressions for CodeGen.
4316 for (auto C : Clauses) {
4317 if (auto LC = dyn_cast<OMPLinearClause>(C))
4318 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4319 B.NumIterations, *this, CurScope))
4320 return StmtError();
4321 }
4322 }
4323
Alexey Bataev66b15b52015-08-21 11:14:16 +00004324 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4325 // If both simdlen and safelen clauses are specified, the value of the simdlen
4326 // parameter must be less than or equal to the value of the safelen parameter.
4327 OMPSafelenClause *Safelen = nullptr;
4328 OMPSimdlenClause *Simdlen = nullptr;
4329 for (auto *Clause : Clauses) {
4330 if (Clause->getClauseKind() == OMPC_safelen)
4331 Safelen = cast<OMPSafelenClause>(Clause);
4332 else if (Clause->getClauseKind() == OMPC_simdlen)
4333 Simdlen = cast<OMPSimdlenClause>(Clause);
4334 if (Safelen && Simdlen)
4335 break;
4336 }
4337 if (Simdlen && Safelen &&
4338 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4339 Safelen->getSafelen()))
4340 return StmtError();
4341
Alexander Musmane4e893b2014-09-23 09:33:00 +00004342 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004343 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00004344 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004345}
4346
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004347StmtResult
4348Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
4349 Stmt *AStmt, SourceLocation StartLoc,
4350 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004351 if (!AStmt)
4352 return StmtError();
4353
4354 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004355 auto BaseStmt = AStmt;
4356 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4357 BaseStmt = CS->getCapturedStmt();
4358 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4359 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004360 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004361 return StmtError();
4362 // All associated statements must be '#pragma omp section' except for
4363 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004364 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004365 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4366 if (SectionStmt)
4367 Diag(SectionStmt->getLocStart(),
4368 diag::err_omp_parallel_sections_substmt_not_section);
4369 return StmtError();
4370 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004371 cast<OMPSectionDirective>(SectionStmt)
4372 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004373 }
4374 } else {
4375 Diag(AStmt->getLocStart(),
4376 diag::err_omp_parallel_sections_not_compound_stmt);
4377 return StmtError();
4378 }
4379
4380 getCurFunction()->setHasBranchProtectedScope();
4381
Alexey Bataev25e5b442015-09-15 12:52:43 +00004382 return OMPParallelSectionsDirective::Create(
4383 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004384}
4385
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004386StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
4387 Stmt *AStmt, SourceLocation StartLoc,
4388 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004389 if (!AStmt)
4390 return StmtError();
4391
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004392 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4393 // 1.2.2 OpenMP Language Terminology
4394 // Structured block - An executable statement with a single entry at the
4395 // top and a single exit at the bottom.
4396 // The point of exit cannot be a branch out of the structured block.
4397 // longjmp() and throw() must not violate the entry/exit criteria.
4398 CS->getCapturedDecl()->setNothrow();
4399
4400 getCurFunction()->setHasBranchProtectedScope();
4401
Alexey Bataev25e5b442015-09-15 12:52:43 +00004402 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4403 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004404}
4405
Alexey Bataev68446b72014-07-18 07:47:19 +00004406StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
4407 SourceLocation EndLoc) {
4408 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
4409}
4410
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00004411StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
4412 SourceLocation EndLoc) {
4413 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
4414}
4415
Alexey Bataev2df347a2014-07-18 10:17:07 +00004416StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
4417 SourceLocation EndLoc) {
4418 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
4419}
4420
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00004421StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
4422 SourceLocation StartLoc,
4423 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004424 if (!AStmt)
4425 return StmtError();
4426
4427 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00004428
4429 getCurFunction()->setHasBranchProtectedScope();
4430
4431 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
4432}
4433
Alexey Bataev6125da92014-07-21 11:26:11 +00004434StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
4435 SourceLocation StartLoc,
4436 SourceLocation EndLoc) {
4437 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
4438 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
4439}
4440
Alexey Bataev346265e2015-09-25 10:37:12 +00004441StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
4442 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004443 SourceLocation StartLoc,
4444 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004445 if (!AStmt)
4446 return StmtError();
4447
4448 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004449
4450 getCurFunction()->setHasBranchProtectedScope();
4451
Alexey Bataev346265e2015-09-25 10:37:12 +00004452 OMPThreadsClause *TC = nullptr;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004453 OMPSIMDClause *SC = nullptr;
Alexey Bataev346265e2015-09-25 10:37:12 +00004454 for (auto *C: Clauses) {
4455 if (C->getClauseKind() == OMPC_threads)
4456 TC = cast<OMPThreadsClause>(C);
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004457 else if (C->getClauseKind() == OMPC_simd)
4458 SC = cast<OMPSIMDClause>(C);
Alexey Bataev346265e2015-09-25 10:37:12 +00004459 }
4460
4461 // TODO: this must happen only if 'threads' clause specified or if no clauses
4462 // is specified.
4463 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
4464 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
4465 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param) << (TC != nullptr);
4466 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
4467 return StmtError();
4468 }
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004469 if (!SC && isOpenMPSimdDirective(DSAStack->getParentDirective())) {
4470 // OpenMP [2.8.1,simd Construct, Restrictions]
4471 // An ordered construct with the simd clause is the only OpenMP construct
4472 // that can appear in the simd region.
4473 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
4474 return StmtError();
4475 }
Alexey Bataev346265e2015-09-25 10:37:12 +00004476
4477 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004478}
4479
Alexey Bataev1d160b12015-03-13 12:27:31 +00004480namespace {
4481/// \brief Helper class for checking expression in 'omp atomic [update]'
4482/// construct.
4483class OpenMPAtomicUpdateChecker {
4484 /// \brief Error results for atomic update expressions.
4485 enum ExprAnalysisErrorCode {
4486 /// \brief A statement is not an expression statement.
4487 NotAnExpression,
4488 /// \brief Expression is not builtin binary or unary operation.
4489 NotABinaryOrUnaryExpression,
4490 /// \brief Unary operation is not post-/pre- increment/decrement operation.
4491 NotAnUnaryIncDecExpression,
4492 /// \brief An expression is not of scalar type.
4493 NotAScalarType,
4494 /// \brief A binary operation is not an assignment operation.
4495 NotAnAssignmentOp,
4496 /// \brief RHS part of the binary operation is not a binary expression.
4497 NotABinaryExpression,
4498 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
4499 /// expression.
4500 NotABinaryOperator,
4501 /// \brief RHS binary operation does not have reference to the updated LHS
4502 /// part.
4503 NotAnUpdateExpression,
4504 /// \brief No errors is found.
4505 NoError
4506 };
4507 /// \brief Reference to Sema.
4508 Sema &SemaRef;
4509 /// \brief A location for note diagnostics (when error is found).
4510 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004511 /// \brief 'x' lvalue part of the source atomic expression.
4512 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004513 /// \brief 'expr' rvalue part of the source atomic expression.
4514 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004515 /// \brief Helper expression of the form
4516 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
4517 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
4518 Expr *UpdateExpr;
4519 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
4520 /// important for non-associative operations.
4521 bool IsXLHSInRHSPart;
4522 BinaryOperatorKind Op;
4523 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004524 /// \brief true if the source expression is a postfix unary operation, false
4525 /// if it is a prefix unary operation.
4526 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004527
4528public:
4529 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00004530 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00004531 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00004532 /// \brief Check specified statement that it is suitable for 'atomic update'
4533 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00004534 /// expression. If DiagId and NoteId == 0, then only check is performed
4535 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00004536 /// \param DiagId Diagnostic which should be emitted if error is found.
4537 /// \param NoteId Diagnostic note for the main error message.
4538 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00004539 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00004540 /// \brief Return the 'x' lvalue part of the source atomic expression.
4541 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00004542 /// \brief Return the 'expr' rvalue part of the source atomic expression.
4543 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00004544 /// \brief Return the update expression used in calculation of the updated
4545 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
4546 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
4547 Expr *getUpdateExpr() const { return UpdateExpr; }
4548 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
4549 /// false otherwise.
4550 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
4551
Alexey Bataevb78ca832015-04-01 03:33:17 +00004552 /// \brief true if the source expression is a postfix unary operation, false
4553 /// if it is a prefix unary operation.
4554 bool isPostfixUpdate() const { return IsPostfixUpdate; }
4555
Alexey Bataev1d160b12015-03-13 12:27:31 +00004556private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00004557 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
4558 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00004559};
4560} // namespace
4561
4562bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
4563 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
4564 ExprAnalysisErrorCode ErrorFound = NoError;
4565 SourceLocation ErrorLoc, NoteLoc;
4566 SourceRange ErrorRange, NoteRange;
4567 // Allowed constructs are:
4568 // x = x binop expr;
4569 // x = expr binop x;
4570 if (AtomicBinOp->getOpcode() == BO_Assign) {
4571 X = AtomicBinOp->getLHS();
4572 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
4573 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
4574 if (AtomicInnerBinOp->isMultiplicativeOp() ||
4575 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
4576 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00004577 Op = AtomicInnerBinOp->getOpcode();
4578 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00004579 auto *LHS = AtomicInnerBinOp->getLHS();
4580 auto *RHS = AtomicInnerBinOp->getRHS();
4581 llvm::FoldingSetNodeID XId, LHSId, RHSId;
4582 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
4583 /*Canonical=*/true);
4584 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
4585 /*Canonical=*/true);
4586 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
4587 /*Canonical=*/true);
4588 if (XId == LHSId) {
4589 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004590 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004591 } else if (XId == RHSId) {
4592 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004593 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004594 } else {
4595 ErrorLoc = AtomicInnerBinOp->getExprLoc();
4596 ErrorRange = AtomicInnerBinOp->getSourceRange();
4597 NoteLoc = X->getExprLoc();
4598 NoteRange = X->getSourceRange();
4599 ErrorFound = NotAnUpdateExpression;
4600 }
4601 } else {
4602 ErrorLoc = AtomicInnerBinOp->getExprLoc();
4603 ErrorRange = AtomicInnerBinOp->getSourceRange();
4604 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
4605 NoteRange = SourceRange(NoteLoc, NoteLoc);
4606 ErrorFound = NotABinaryOperator;
4607 }
4608 } else {
4609 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
4610 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
4611 ErrorFound = NotABinaryExpression;
4612 }
4613 } else {
4614 ErrorLoc = AtomicBinOp->getExprLoc();
4615 ErrorRange = AtomicBinOp->getSourceRange();
4616 NoteLoc = AtomicBinOp->getOperatorLoc();
4617 NoteRange = SourceRange(NoteLoc, NoteLoc);
4618 ErrorFound = NotAnAssignmentOp;
4619 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00004620 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004621 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
4622 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
4623 return true;
4624 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00004625 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00004626 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004627}
4628
4629bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
4630 unsigned NoteId) {
4631 ExprAnalysisErrorCode ErrorFound = NoError;
4632 SourceLocation ErrorLoc, NoteLoc;
4633 SourceRange ErrorRange, NoteRange;
4634 // Allowed constructs are:
4635 // x++;
4636 // x--;
4637 // ++x;
4638 // --x;
4639 // x binop= expr;
4640 // x = x binop expr;
4641 // x = expr binop x;
4642 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
4643 AtomicBody = AtomicBody->IgnoreParenImpCasts();
4644 if (AtomicBody->getType()->isScalarType() ||
4645 AtomicBody->isInstantiationDependent()) {
4646 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
4647 AtomicBody->IgnoreParenImpCasts())) {
4648 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00004649 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00004650 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00004651 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00004652 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00004653 X = AtomicCompAssignOp->getLHS();
4654 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004655 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
4656 AtomicBody->IgnoreParenImpCasts())) {
4657 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00004658 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
4659 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004660 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00004661 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
4662 // Check for Unary Operation
4663 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004664 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00004665 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
4666 OpLoc = AtomicUnaryOp->getOperatorLoc();
4667 X = AtomicUnaryOp->getSubExpr();
4668 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
4669 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004670 } else {
4671 ErrorFound = NotAnUnaryIncDecExpression;
4672 ErrorLoc = AtomicUnaryOp->getExprLoc();
4673 ErrorRange = AtomicUnaryOp->getSourceRange();
4674 NoteLoc = AtomicUnaryOp->getOperatorLoc();
4675 NoteRange = SourceRange(NoteLoc, NoteLoc);
4676 }
Alexey Bataev5a195472015-09-04 12:55:50 +00004677 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004678 ErrorFound = NotABinaryOrUnaryExpression;
4679 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
4680 NoteRange = ErrorRange = AtomicBody->getSourceRange();
4681 }
4682 } else {
4683 ErrorFound = NotAScalarType;
4684 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
4685 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
4686 }
4687 } else {
4688 ErrorFound = NotAnExpression;
4689 NoteLoc = ErrorLoc = S->getLocStart();
4690 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
4691 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00004692 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004693 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
4694 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
4695 return true;
4696 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00004697 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00004698 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00004699 // Build an update expression of form 'OpaqueValueExpr(x) binop
4700 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
4701 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
4702 auto *OVEX = new (SemaRef.getASTContext())
4703 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
4704 auto *OVEExpr = new (SemaRef.getASTContext())
4705 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
4706 auto Update =
4707 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
4708 IsXLHSInRHSPart ? OVEExpr : OVEX);
4709 if (Update.isInvalid())
4710 return true;
4711 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
4712 Sema::AA_Casting);
4713 if (Update.isInvalid())
4714 return true;
4715 UpdateExpr = Update.get();
4716 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00004717 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004718}
4719
Alexey Bataev0162e452014-07-22 10:10:35 +00004720StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
4721 Stmt *AStmt,
4722 SourceLocation StartLoc,
4723 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004724 if (!AStmt)
4725 return StmtError();
4726
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004727 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00004728 // 1.2.2 OpenMP Language Terminology
4729 // Structured block - An executable statement with a single entry at the
4730 // top and a single exit at the bottom.
4731 // The point of exit cannot be a branch out of the structured block.
4732 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00004733 OpenMPClauseKind AtomicKind = OMPC_unknown;
4734 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004735 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00004736 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00004737 C->getClauseKind() == OMPC_update ||
4738 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00004739 if (AtomicKind != OMPC_unknown) {
4740 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
4741 << SourceRange(C->getLocStart(), C->getLocEnd());
4742 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
4743 << getOpenMPClauseName(AtomicKind);
4744 } else {
4745 AtomicKind = C->getClauseKind();
4746 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004747 }
4748 }
4749 }
Alexey Bataev62cec442014-11-18 10:14:22 +00004750
Alexey Bataev459dec02014-07-24 06:46:57 +00004751 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00004752 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
4753 Body = EWC->getSubExpr();
4754
Alexey Bataev62cec442014-11-18 10:14:22 +00004755 Expr *X = nullptr;
4756 Expr *V = nullptr;
4757 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004758 Expr *UE = nullptr;
4759 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004760 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00004761 // OpenMP [2.12.6, atomic Construct]
4762 // In the next expressions:
4763 // * x and v (as applicable) are both l-value expressions with scalar type.
4764 // * During the execution of an atomic region, multiple syntactic
4765 // occurrences of x must designate the same storage location.
4766 // * Neither of v and expr (as applicable) may access the storage location
4767 // designated by x.
4768 // * Neither of x and expr (as applicable) may access the storage location
4769 // designated by v.
4770 // * expr is an expression with scalar type.
4771 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
4772 // * binop, binop=, ++, and -- are not overloaded operators.
4773 // * The expression x binop expr must be numerically equivalent to x binop
4774 // (expr). This requirement is satisfied if the operators in expr have
4775 // precedence greater than binop, or by using parentheses around expr or
4776 // subexpressions of expr.
4777 // * The expression expr binop x must be numerically equivalent to (expr)
4778 // binop x. This requirement is satisfied if the operators in expr have
4779 // precedence equal to or greater than binop, or by using parentheses around
4780 // expr or subexpressions of expr.
4781 // * For forms that allow multiple occurrences of x, the number of times
4782 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00004783 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004784 enum {
4785 NotAnExpression,
4786 NotAnAssignmentOp,
4787 NotAScalarType,
4788 NotAnLValue,
4789 NoError
4790 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00004791 SourceLocation ErrorLoc, NoteLoc;
4792 SourceRange ErrorRange, NoteRange;
4793 // If clause is read:
4794 // v = x;
4795 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
4796 auto AtomicBinOp =
4797 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
4798 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
4799 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
4800 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
4801 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
4802 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
4803 if (!X->isLValue() || !V->isLValue()) {
4804 auto NotLValueExpr = X->isLValue() ? V : X;
4805 ErrorFound = NotAnLValue;
4806 ErrorLoc = AtomicBinOp->getExprLoc();
4807 ErrorRange = AtomicBinOp->getSourceRange();
4808 NoteLoc = NotLValueExpr->getExprLoc();
4809 NoteRange = NotLValueExpr->getSourceRange();
4810 }
4811 } else if (!X->isInstantiationDependent() ||
4812 !V->isInstantiationDependent()) {
4813 auto NotScalarExpr =
4814 (X->isInstantiationDependent() || X->getType()->isScalarType())
4815 ? V
4816 : X;
4817 ErrorFound = NotAScalarType;
4818 ErrorLoc = AtomicBinOp->getExprLoc();
4819 ErrorRange = AtomicBinOp->getSourceRange();
4820 NoteLoc = NotScalarExpr->getExprLoc();
4821 NoteRange = NotScalarExpr->getSourceRange();
4822 }
Alexey Bataev5a195472015-09-04 12:55:50 +00004823 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00004824 ErrorFound = NotAnAssignmentOp;
4825 ErrorLoc = AtomicBody->getExprLoc();
4826 ErrorRange = AtomicBody->getSourceRange();
4827 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
4828 : AtomicBody->getExprLoc();
4829 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
4830 : AtomicBody->getSourceRange();
4831 }
4832 } else {
4833 ErrorFound = NotAnExpression;
4834 NoteLoc = ErrorLoc = Body->getLocStart();
4835 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00004836 }
Alexey Bataev62cec442014-11-18 10:14:22 +00004837 if (ErrorFound != NoError) {
4838 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
4839 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00004840 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
4841 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00004842 return StmtError();
4843 } else if (CurContext->isDependentContext())
4844 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00004845 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004846 enum {
4847 NotAnExpression,
4848 NotAnAssignmentOp,
4849 NotAScalarType,
4850 NotAnLValue,
4851 NoError
4852 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00004853 SourceLocation ErrorLoc, NoteLoc;
4854 SourceRange ErrorRange, NoteRange;
4855 // If clause is write:
4856 // x = expr;
4857 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
4858 auto AtomicBinOp =
4859 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
4860 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00004861 X = AtomicBinOp->getLHS();
4862 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00004863 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
4864 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
4865 if (!X->isLValue()) {
4866 ErrorFound = NotAnLValue;
4867 ErrorLoc = AtomicBinOp->getExprLoc();
4868 ErrorRange = AtomicBinOp->getSourceRange();
4869 NoteLoc = X->getExprLoc();
4870 NoteRange = X->getSourceRange();
4871 }
4872 } else if (!X->isInstantiationDependent() ||
4873 !E->isInstantiationDependent()) {
4874 auto NotScalarExpr =
4875 (X->isInstantiationDependent() || X->getType()->isScalarType())
4876 ? E
4877 : X;
4878 ErrorFound = NotAScalarType;
4879 ErrorLoc = AtomicBinOp->getExprLoc();
4880 ErrorRange = AtomicBinOp->getSourceRange();
4881 NoteLoc = NotScalarExpr->getExprLoc();
4882 NoteRange = NotScalarExpr->getSourceRange();
4883 }
Alexey Bataev5a195472015-09-04 12:55:50 +00004884 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00004885 ErrorFound = NotAnAssignmentOp;
4886 ErrorLoc = AtomicBody->getExprLoc();
4887 ErrorRange = AtomicBody->getSourceRange();
4888 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
4889 : AtomicBody->getExprLoc();
4890 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
4891 : AtomicBody->getSourceRange();
4892 }
4893 } else {
4894 ErrorFound = NotAnExpression;
4895 NoteLoc = ErrorLoc = Body->getLocStart();
4896 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00004897 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00004898 if (ErrorFound != NoError) {
4899 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
4900 << ErrorRange;
4901 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
4902 << NoteRange;
4903 return StmtError();
4904 } else if (CurContext->isDependentContext())
4905 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00004906 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004907 // If clause is update:
4908 // x++;
4909 // x--;
4910 // ++x;
4911 // --x;
4912 // x binop= expr;
4913 // x = x binop expr;
4914 // x = expr binop x;
4915 OpenMPAtomicUpdateChecker Checker(*this);
4916 if (Checker.checkStatement(
4917 Body, (AtomicKind == OMPC_update)
4918 ? diag::err_omp_atomic_update_not_expression_statement
4919 : diag::err_omp_atomic_not_expression_statement,
4920 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00004921 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00004922 if (!CurContext->isDependentContext()) {
4923 E = Checker.getExpr();
4924 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00004925 UE = Checker.getUpdateExpr();
4926 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00004927 }
Alexey Bataev459dec02014-07-24 06:46:57 +00004928 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004929 enum {
4930 NotAnAssignmentOp,
4931 NotACompoundStatement,
4932 NotTwoSubstatements,
4933 NotASpecificExpression,
4934 NoError
4935 } ErrorFound = NoError;
4936 SourceLocation ErrorLoc, NoteLoc;
4937 SourceRange ErrorRange, NoteRange;
4938 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
4939 // If clause is a capture:
4940 // v = x++;
4941 // v = x--;
4942 // v = ++x;
4943 // v = --x;
4944 // v = x binop= expr;
4945 // v = x = x binop expr;
4946 // v = x = expr binop x;
4947 auto *AtomicBinOp =
4948 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
4949 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
4950 V = AtomicBinOp->getLHS();
4951 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
4952 OpenMPAtomicUpdateChecker Checker(*this);
4953 if (Checker.checkStatement(
4954 Body, diag::err_omp_atomic_capture_not_expression_statement,
4955 diag::note_omp_atomic_update))
4956 return StmtError();
4957 E = Checker.getExpr();
4958 X = Checker.getX();
4959 UE = Checker.getUpdateExpr();
4960 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
4961 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00004962 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004963 ErrorLoc = AtomicBody->getExprLoc();
4964 ErrorRange = AtomicBody->getSourceRange();
4965 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
4966 : AtomicBody->getExprLoc();
4967 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
4968 : AtomicBody->getSourceRange();
4969 ErrorFound = NotAnAssignmentOp;
4970 }
4971 if (ErrorFound != NoError) {
4972 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
4973 << ErrorRange;
4974 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
4975 return StmtError();
4976 } else if (CurContext->isDependentContext()) {
4977 UE = V = E = X = nullptr;
4978 }
4979 } else {
4980 // If clause is a capture:
4981 // { v = x; x = expr; }
4982 // { v = x; x++; }
4983 // { v = x; x--; }
4984 // { v = x; ++x; }
4985 // { v = x; --x; }
4986 // { v = x; x binop= expr; }
4987 // { v = x; x = x binop expr; }
4988 // { v = x; x = expr binop x; }
4989 // { x++; v = x; }
4990 // { x--; v = x; }
4991 // { ++x; v = x; }
4992 // { --x; v = x; }
4993 // { x binop= expr; v = x; }
4994 // { x = x binop expr; v = x; }
4995 // { x = expr binop x; v = x; }
4996 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
4997 // Check that this is { expr1; expr2; }
4998 if (CS->size() == 2) {
4999 auto *First = CS->body_front();
5000 auto *Second = CS->body_back();
5001 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
5002 First = EWC->getSubExpr()->IgnoreParenImpCasts();
5003 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
5004 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
5005 // Need to find what subexpression is 'v' and what is 'x'.
5006 OpenMPAtomicUpdateChecker Checker(*this);
5007 bool IsUpdateExprFound = !Checker.checkStatement(Second);
5008 BinaryOperator *BinOp = nullptr;
5009 if (IsUpdateExprFound) {
5010 BinOp = dyn_cast<BinaryOperator>(First);
5011 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5012 }
5013 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5014 // { v = x; x++; }
5015 // { v = x; x--; }
5016 // { v = x; ++x; }
5017 // { v = x; --x; }
5018 // { v = x; x binop= expr; }
5019 // { v = x; x = x binop expr; }
5020 // { v = x; x = expr binop x; }
5021 // Check that the first expression has form v = x.
5022 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5023 llvm::FoldingSetNodeID XId, PossibleXId;
5024 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5025 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5026 IsUpdateExprFound = XId == PossibleXId;
5027 if (IsUpdateExprFound) {
5028 V = BinOp->getLHS();
5029 X = Checker.getX();
5030 E = Checker.getExpr();
5031 UE = Checker.getUpdateExpr();
5032 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005033 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005034 }
5035 }
5036 if (!IsUpdateExprFound) {
5037 IsUpdateExprFound = !Checker.checkStatement(First);
5038 BinOp = nullptr;
5039 if (IsUpdateExprFound) {
5040 BinOp = dyn_cast<BinaryOperator>(Second);
5041 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5042 }
5043 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5044 // { x++; v = x; }
5045 // { x--; v = x; }
5046 // { ++x; v = x; }
5047 // { --x; v = x; }
5048 // { x binop= expr; v = x; }
5049 // { x = x binop expr; v = x; }
5050 // { x = expr binop x; v = x; }
5051 // Check that the second expression has form v = x.
5052 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5053 llvm::FoldingSetNodeID XId, PossibleXId;
5054 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5055 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5056 IsUpdateExprFound = XId == PossibleXId;
5057 if (IsUpdateExprFound) {
5058 V = BinOp->getLHS();
5059 X = Checker.getX();
5060 E = Checker.getExpr();
5061 UE = Checker.getUpdateExpr();
5062 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005063 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005064 }
5065 }
5066 }
5067 if (!IsUpdateExprFound) {
5068 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00005069 auto *FirstExpr = dyn_cast<Expr>(First);
5070 auto *SecondExpr = dyn_cast<Expr>(Second);
5071 if (!FirstExpr || !SecondExpr ||
5072 !(FirstExpr->isInstantiationDependent() ||
5073 SecondExpr->isInstantiationDependent())) {
5074 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
5075 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005076 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00005077 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
5078 : First->getLocStart();
5079 NoteRange = ErrorRange = FirstBinOp
5080 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00005081 : SourceRange(ErrorLoc, ErrorLoc);
5082 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005083 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
5084 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
5085 ErrorFound = NotAnAssignmentOp;
5086 NoteLoc = ErrorLoc = SecondBinOp
5087 ? SecondBinOp->getOperatorLoc()
5088 : Second->getLocStart();
5089 NoteRange = ErrorRange =
5090 SecondBinOp ? SecondBinOp->getSourceRange()
5091 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00005092 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005093 auto *PossibleXRHSInFirst =
5094 FirstBinOp->getRHS()->IgnoreParenImpCasts();
5095 auto *PossibleXLHSInSecond =
5096 SecondBinOp->getLHS()->IgnoreParenImpCasts();
5097 llvm::FoldingSetNodeID X1Id, X2Id;
5098 PossibleXRHSInFirst->Profile(X1Id, Context,
5099 /*Canonical=*/true);
5100 PossibleXLHSInSecond->Profile(X2Id, Context,
5101 /*Canonical=*/true);
5102 IsUpdateExprFound = X1Id == X2Id;
5103 if (IsUpdateExprFound) {
5104 V = FirstBinOp->getLHS();
5105 X = SecondBinOp->getLHS();
5106 E = SecondBinOp->getRHS();
5107 UE = nullptr;
5108 IsXLHSInRHSPart = false;
5109 IsPostfixUpdate = true;
5110 } else {
5111 ErrorFound = NotASpecificExpression;
5112 ErrorLoc = FirstBinOp->getExprLoc();
5113 ErrorRange = FirstBinOp->getSourceRange();
5114 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
5115 NoteRange = SecondBinOp->getRHS()->getSourceRange();
5116 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005117 }
5118 }
5119 }
5120 }
5121 } else {
5122 NoteLoc = ErrorLoc = Body->getLocStart();
5123 NoteRange = ErrorRange =
5124 SourceRange(Body->getLocStart(), Body->getLocStart());
5125 ErrorFound = NotTwoSubstatements;
5126 }
5127 } else {
5128 NoteLoc = ErrorLoc = Body->getLocStart();
5129 NoteRange = ErrorRange =
5130 SourceRange(Body->getLocStart(), Body->getLocStart());
5131 ErrorFound = NotACompoundStatement;
5132 }
5133 if (ErrorFound != NoError) {
5134 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
5135 << ErrorRange;
5136 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5137 return StmtError();
5138 } else if (CurContext->isDependentContext()) {
5139 UE = V = E = X = nullptr;
5140 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005141 }
Alexey Bataevdea47612014-07-23 07:46:59 +00005142 }
Alexey Bataev0162e452014-07-22 10:10:35 +00005143
5144 getCurFunction()->setHasBranchProtectedScope();
5145
Alexey Bataev62cec442014-11-18 10:14:22 +00005146 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00005147 X, V, E, UE, IsXLHSInRHSPart,
5148 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00005149}
5150
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005151StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
5152 Stmt *AStmt,
5153 SourceLocation StartLoc,
5154 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005155 if (!AStmt)
5156 return StmtError();
5157
Samuel Antao4af1b7b2015-12-02 17:44:43 +00005158 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5159 // 1.2.2 OpenMP Language Terminology
5160 // Structured block - An executable statement with a single entry at the
5161 // top and a single exit at the bottom.
5162 // The point of exit cannot be a branch out of the structured block.
5163 // longjmp() and throw() must not violate the entry/exit criteria.
5164 CS->getCapturedDecl()->setNothrow();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005165
Alexey Bataev13314bf2014-10-09 04:18:56 +00005166 // OpenMP [2.16, Nesting of Regions]
5167 // If specified, a teams construct must be contained within a target
5168 // construct. That target construct must contain no statements or directives
5169 // outside of the teams construct.
5170 if (DSAStack->hasInnerTeamsRegion()) {
5171 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
5172 bool OMPTeamsFound = true;
5173 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
5174 auto I = CS->body_begin();
5175 while (I != CS->body_end()) {
5176 auto OED = dyn_cast<OMPExecutableDirective>(*I);
5177 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
5178 OMPTeamsFound = false;
5179 break;
5180 }
5181 ++I;
5182 }
5183 assert(I != CS->body_end() && "Not found statement");
5184 S = *I;
5185 }
5186 if (!OMPTeamsFound) {
5187 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
5188 Diag(DSAStack->getInnerTeamsRegionLoc(),
5189 diag::note_omp_nested_teams_construct_here);
5190 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
5191 << isa<OMPExecutableDirective>(S);
5192 return StmtError();
5193 }
5194 }
5195
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005196 getCurFunction()->setHasBranchProtectedScope();
5197
5198 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5199}
5200
Michael Wong65f367f2015-07-21 13:44:28 +00005201StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
5202 Stmt *AStmt,
5203 SourceLocation StartLoc,
5204 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005205 if (!AStmt)
5206 return StmtError();
5207
5208 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5209
Michael Wong65f367f2015-07-21 13:44:28 +00005210 getCurFunction()->setHasBranchProtectedScope();
5211
5212 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
5213 AStmt);
5214}
5215
Alexey Bataev13314bf2014-10-09 04:18:56 +00005216StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
5217 Stmt *AStmt, SourceLocation StartLoc,
5218 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005219 if (!AStmt)
5220 return StmtError();
5221
Alexey Bataev13314bf2014-10-09 04:18:56 +00005222 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5223 // 1.2.2 OpenMP Language Terminology
5224 // Structured block - An executable statement with a single entry at the
5225 // top and a single exit at the bottom.
5226 // The point of exit cannot be a branch out of the structured block.
5227 // longjmp() and throw() must not violate the entry/exit criteria.
5228 CS->getCapturedDecl()->setNothrow();
5229
5230 getCurFunction()->setHasBranchProtectedScope();
5231
5232 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5233}
5234
Alexey Bataev6d4ed052015-07-01 06:57:41 +00005235StmtResult
5236Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
5237 SourceLocation EndLoc,
5238 OpenMPDirectiveKind CancelRegion) {
5239 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
5240 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
5241 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
5242 << getOpenMPDirectiveName(CancelRegion);
5243 return StmtError();
5244 }
5245 if (DSAStack->isParentNowaitRegion()) {
5246 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
5247 return StmtError();
5248 }
5249 if (DSAStack->isParentOrderedRegion()) {
5250 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
5251 return StmtError();
5252 }
5253 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
5254 CancelRegion);
5255}
5256
Alexey Bataev87933c72015-09-18 08:07:34 +00005257StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
5258 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00005259 SourceLocation EndLoc,
5260 OpenMPDirectiveKind CancelRegion) {
5261 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
5262 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
5263 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
5264 << getOpenMPDirectiveName(CancelRegion);
5265 return StmtError();
5266 }
5267 if (DSAStack->isParentNowaitRegion()) {
5268 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
5269 return StmtError();
5270 }
5271 if (DSAStack->isParentOrderedRegion()) {
5272 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
5273 return StmtError();
5274 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005275 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00005276 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
5277 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00005278}
5279
Alexey Bataev382967a2015-12-08 12:06:20 +00005280static bool checkGrainsizeNumTasksClauses(Sema &S,
5281 ArrayRef<OMPClause *> Clauses) {
5282 OMPClause *PrevClause = nullptr;
5283 bool ErrorFound = false;
5284 for (auto *C : Clauses) {
5285 if (C->getClauseKind() == OMPC_grainsize ||
5286 C->getClauseKind() == OMPC_num_tasks) {
5287 if (!PrevClause)
5288 PrevClause = C;
5289 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
5290 S.Diag(C->getLocStart(),
5291 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
5292 << getOpenMPClauseName(C->getClauseKind())
5293 << getOpenMPClauseName(PrevClause->getClauseKind());
5294 S.Diag(PrevClause->getLocStart(),
5295 diag::note_omp_previous_grainsize_num_tasks)
5296 << getOpenMPClauseName(PrevClause->getClauseKind());
5297 ErrorFound = true;
5298 }
5299 }
5300 }
5301 return ErrorFound;
5302}
5303
Alexey Bataev49f6e782015-12-01 04:18:41 +00005304StmtResult Sema::ActOnOpenMPTaskLoopDirective(
5305 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5306 SourceLocation EndLoc,
5307 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
5308 if (!AStmt)
5309 return StmtError();
5310
5311 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5312 OMPLoopDirective::HelperExprs B;
5313 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5314 // define the nested loops number.
5315 unsigned NestedLoopCount =
5316 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005317 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00005318 VarsWithImplicitDSA, B);
5319 if (NestedLoopCount == 0)
5320 return StmtError();
5321
5322 assert((CurContext->isDependentContext() || B.builtAll()) &&
5323 "omp for loop exprs were not built");
5324
Alexey Bataev382967a2015-12-08 12:06:20 +00005325 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
5326 // The grainsize clause and num_tasks clause are mutually exclusive and may
5327 // not appear on the same taskloop directive.
5328 if (checkGrainsizeNumTasksClauses(*this, Clauses))
5329 return StmtError();
5330
Alexey Bataev49f6e782015-12-01 04:18:41 +00005331 getCurFunction()->setHasBranchProtectedScope();
5332 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
5333 NestedLoopCount, Clauses, AStmt, B);
5334}
5335
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005336StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
5337 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5338 SourceLocation EndLoc,
5339 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
5340 if (!AStmt)
5341 return StmtError();
5342
5343 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5344 OMPLoopDirective::HelperExprs B;
5345 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5346 // define the nested loops number.
5347 unsigned NestedLoopCount =
5348 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
5349 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
5350 VarsWithImplicitDSA, B);
5351 if (NestedLoopCount == 0)
5352 return StmtError();
5353
5354 assert((CurContext->isDependentContext() || B.builtAll()) &&
5355 "omp for loop exprs were not built");
5356
Alexey Bataev382967a2015-12-08 12:06:20 +00005357 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
5358 // The grainsize clause and num_tasks clause are mutually exclusive and may
5359 // not appear on the same taskloop directive.
5360 if (checkGrainsizeNumTasksClauses(*this, Clauses))
5361 return StmtError();
5362
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005363 getCurFunction()->setHasBranchProtectedScope();
5364 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
5365 NestedLoopCount, Clauses, AStmt, B);
5366}
5367
Alexey Bataeved09d242014-05-28 05:53:51 +00005368OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005369 SourceLocation StartLoc,
5370 SourceLocation LParenLoc,
5371 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005372 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005373 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00005374 case OMPC_final:
5375 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
5376 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00005377 case OMPC_num_threads:
5378 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
5379 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00005380 case OMPC_safelen:
5381 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
5382 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00005383 case OMPC_simdlen:
5384 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
5385 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00005386 case OMPC_collapse:
5387 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
5388 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005389 case OMPC_ordered:
5390 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
5391 break;
Michael Wonge710d542015-08-07 16:16:36 +00005392 case OMPC_device:
5393 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
5394 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00005395 case OMPC_num_teams:
5396 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
5397 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005398 case OMPC_thread_limit:
5399 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
5400 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00005401 case OMPC_priority:
5402 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
5403 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00005404 case OMPC_grainsize:
5405 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
5406 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00005407 case OMPC_num_tasks:
5408 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
5409 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005410 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005411 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005412 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005413 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005414 case OMPC_private:
5415 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00005416 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005417 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00005418 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00005419 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00005420 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005421 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005422 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00005423 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005424 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005425 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005426 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00005427 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005428 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00005429 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00005430 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00005431 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005432 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005433 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00005434 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005435 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00005436 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00005437 case OMPC_nogroup:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005438 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005439 llvm_unreachable("Clause is not allowed.");
5440 }
5441 return Res;
5442}
5443
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005444OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
5445 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005446 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005447 SourceLocation NameModifierLoc,
5448 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005449 SourceLocation EndLoc) {
5450 Expr *ValExpr = Condition;
5451 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
5452 !Condition->isInstantiationDependent() &&
5453 !Condition->containsUnexpandedParameterPack()) {
5454 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00005455 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005456 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005457 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005458
Nikola Smiljanic01a75982014-05-29 10:55:11 +00005459 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005460 }
5461
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005462 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
5463 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005464}
5465
Alexey Bataev3778b602014-07-17 07:32:53 +00005466OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
5467 SourceLocation StartLoc,
5468 SourceLocation LParenLoc,
5469 SourceLocation EndLoc) {
5470 Expr *ValExpr = Condition;
5471 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
5472 !Condition->isInstantiationDependent() &&
5473 !Condition->containsUnexpandedParameterPack()) {
5474 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
5475 Condition->getExprLoc(), Condition);
5476 if (Val.isInvalid())
5477 return nullptr;
5478
5479 ValExpr = Val.get();
5480 }
5481
5482 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
5483}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005484ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
5485 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00005486 if (!Op)
5487 return ExprError();
5488
5489 class IntConvertDiagnoser : public ICEConvertDiagnoser {
5490 public:
5491 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00005492 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00005493 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
5494 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005495 return S.Diag(Loc, diag::err_omp_not_integral) << T;
5496 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005497 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
5498 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005499 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
5500 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005501 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
5502 QualType T,
5503 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005504 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
5505 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005506 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
5507 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005508 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00005509 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00005510 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005511 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
5512 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005513 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
5514 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005515 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
5516 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005517 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00005518 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00005519 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005520 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
5521 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005522 llvm_unreachable("conversion functions are permitted");
5523 }
5524 } ConvertDiagnoser;
5525 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
5526}
5527
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005528static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00005529 OpenMPClauseKind CKind,
5530 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005531 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
5532 !ValExpr->isInstantiationDependent()) {
5533 SourceLocation Loc = ValExpr->getExprLoc();
5534 ExprResult Value =
5535 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
5536 if (Value.isInvalid())
5537 return false;
5538
5539 ValExpr = Value.get();
5540 // The expression must evaluate to a non-negative integer value.
5541 llvm::APSInt Result;
5542 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00005543 Result.isSigned() &&
5544 !((!StrictlyPositive && Result.isNonNegative()) ||
5545 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005546 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00005547 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
5548 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005549 return false;
5550 }
5551 }
5552 return true;
5553}
5554
Alexey Bataev568a8332014-03-06 06:15:19 +00005555OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
5556 SourceLocation StartLoc,
5557 SourceLocation LParenLoc,
5558 SourceLocation EndLoc) {
5559 Expr *ValExpr = NumThreads;
Alexey Bataev568a8332014-03-06 06:15:19 +00005560
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005561 // OpenMP [2.5, Restrictions]
5562 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00005563 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
5564 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005565 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00005566
Alexey Bataeved09d242014-05-28 05:53:51 +00005567 return new (Context)
5568 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00005569}
5570
Alexey Bataev62c87d22014-03-21 04:51:18 +00005571ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
5572 OpenMPClauseKind CKind) {
5573 if (!E)
5574 return ExprError();
5575 if (E->isValueDependent() || E->isTypeDependent() ||
5576 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00005577 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00005578 llvm::APSInt Result;
5579 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
5580 if (ICE.isInvalid())
5581 return ExprError();
5582 if (!Result.isStrictlyPositive()) {
5583 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00005584 << getOpenMPClauseName(CKind) << 1 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00005585 return ExprError();
5586 }
Alexander Musman09184fe2014-09-30 05:29:28 +00005587 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
5588 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
5589 << E->getSourceRange();
5590 return ExprError();
5591 }
Alexey Bataev7b6bc882015-11-26 07:50:39 +00005592 if (CKind == OMPC_collapse)
5593 DSAStack->setCollapseNumber(Result.getExtValue());
5594 else if (CKind == OMPC_ordered)
5595 DSAStack->setCollapseNumber(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00005596 return ICE;
5597}
5598
5599OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
5600 SourceLocation LParenLoc,
5601 SourceLocation EndLoc) {
5602 // OpenMP [2.8.1, simd construct, Description]
5603 // The parameter of the safelen clause must be a constant
5604 // positive integer expression.
5605 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
5606 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005607 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00005608 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00005609 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00005610}
5611
Alexey Bataev66b15b52015-08-21 11:14:16 +00005612OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
5613 SourceLocation LParenLoc,
5614 SourceLocation EndLoc) {
5615 // OpenMP [2.8.1, simd construct, Description]
5616 // The parameter of the simdlen clause must be a constant
5617 // positive integer expression.
5618 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
5619 if (Simdlen.isInvalid())
5620 return nullptr;
5621 return new (Context)
5622 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
5623}
5624
Alexander Musman64d33f12014-06-04 07:53:32 +00005625OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
5626 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00005627 SourceLocation LParenLoc,
5628 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00005629 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00005630 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00005631 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00005632 // The parameter of the collapse clause must be a constant
5633 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00005634 ExprResult NumForLoopsResult =
5635 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
5636 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00005637 return nullptr;
5638 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00005639 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00005640}
5641
Alexey Bataev10e775f2015-07-30 11:36:16 +00005642OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
5643 SourceLocation EndLoc,
5644 SourceLocation LParenLoc,
5645 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00005646 // OpenMP [2.7.1, loop construct, Description]
5647 // OpenMP [2.8.1, simd construct, Description]
5648 // OpenMP [2.9.6, distribute construct, Description]
5649 // The parameter of the ordered clause must be a constant
5650 // positive integer expression if any.
5651 if (NumForLoops && LParenLoc.isValid()) {
5652 ExprResult NumForLoopsResult =
5653 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
5654 if (NumForLoopsResult.isInvalid())
5655 return nullptr;
5656 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00005657 } else
5658 NumForLoops = nullptr;
5659 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00005660 return new (Context)
5661 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
5662}
5663
Alexey Bataeved09d242014-05-28 05:53:51 +00005664OMPClause *Sema::ActOnOpenMPSimpleClause(
5665 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
5666 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005667 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005668 switch (Kind) {
5669 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00005670 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00005671 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
5672 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005673 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005674 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00005675 Res = ActOnOpenMPProcBindClause(
5676 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
5677 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005678 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005679 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00005680 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00005681 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00005682 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00005683 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00005684 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005685 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005686 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005687 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00005688 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00005689 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00005690 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00005691 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00005692 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005693 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005694 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005695 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00005696 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005697 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005698 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005699 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00005700 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005701 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00005702 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00005703 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00005704 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005705 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005706 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00005707 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00005708 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005709 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00005710 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00005711 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005712 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00005713 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00005714 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00005715 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00005716 case OMPC_num_tasks:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005717 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005718 llvm_unreachable("Clause is not allowed.");
5719 }
5720 return Res;
5721}
5722
5723OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
5724 SourceLocation KindKwLoc,
5725 SourceLocation StartLoc,
5726 SourceLocation LParenLoc,
5727 SourceLocation EndLoc) {
5728 if (Kind == OMPC_DEFAULT_unknown) {
5729 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005730 static_assert(OMPC_DEFAULT_unknown > 0,
5731 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00005732 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005733 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005734 Values += "'";
5735 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
5736 Values += "'";
5737 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005738 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005739 Values += " or ";
5740 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005741 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005742 break;
5743 default:
5744 Values += Sep;
5745 break;
5746 }
5747 }
5748 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00005749 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005750 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005751 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00005752 switch (Kind) {
5753 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005754 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005755 break;
5756 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005757 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005758 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005759 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005760 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00005761 break;
5762 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005763 return new (Context)
5764 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005765}
5766
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005767OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
5768 SourceLocation KindKwLoc,
5769 SourceLocation StartLoc,
5770 SourceLocation LParenLoc,
5771 SourceLocation EndLoc) {
5772 if (Kind == OMPC_PROC_BIND_unknown) {
5773 std::string Values;
5774 std::string Sep(", ");
5775 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
5776 Values += "'";
5777 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
5778 Values += "'";
5779 switch (i) {
5780 case OMPC_PROC_BIND_unknown - 2:
5781 Values += " or ";
5782 break;
5783 case OMPC_PROC_BIND_unknown - 1:
5784 break;
5785 default:
5786 Values += Sep;
5787 break;
5788 }
5789 }
5790 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00005791 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005792 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005793 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005794 return new (Context)
5795 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005796}
5797
Alexey Bataev56dafe82014-06-20 07:16:17 +00005798OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
5799 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
5800 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005801 SourceLocation ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00005802 SourceLocation EndLoc) {
5803 OMPClause *Res = nullptr;
5804 switch (Kind) {
5805 case OMPC_schedule:
5806 Res = ActOnOpenMPScheduleClause(
5807 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005808 LParenLoc, ArgumentLoc, DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00005809 break;
5810 case OMPC_if:
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005811 Res =
5812 ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument), Expr,
5813 StartLoc, LParenLoc, ArgumentLoc, DelimLoc, EndLoc);
5814 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00005815 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005816 case OMPC_num_threads:
5817 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00005818 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005819 case OMPC_collapse:
5820 case OMPC_default:
5821 case OMPC_proc_bind:
5822 case OMPC_private:
5823 case OMPC_firstprivate:
5824 case OMPC_lastprivate:
5825 case OMPC_shared:
5826 case OMPC_reduction:
5827 case OMPC_linear:
5828 case OMPC_aligned:
5829 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005830 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005831 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00005832 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005833 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005834 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005835 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00005836 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005837 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00005838 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00005839 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00005840 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005841 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005842 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00005843 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00005844 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005845 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00005846 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00005847 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005848 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00005849 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00005850 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00005851 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00005852 case OMPC_num_tasks:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005853 case OMPC_unknown:
5854 llvm_unreachable("Clause is not allowed.");
5855 }
5856 return Res;
5857}
5858
5859OMPClause *Sema::ActOnOpenMPScheduleClause(
5860 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
5861 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
5862 SourceLocation EndLoc) {
5863 if (Kind == OMPC_SCHEDULE_unknown) {
5864 std::string Values;
5865 std::string Sep(", ");
5866 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) {
5867 Values += "'";
5868 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i);
5869 Values += "'";
5870 switch (i) {
5871 case OMPC_SCHEDULE_unknown - 2:
5872 Values += " or ";
5873 break;
5874 case OMPC_SCHEDULE_unknown - 1:
5875 break;
5876 default:
5877 Values += Sep;
5878 break;
5879 }
5880 }
5881 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
5882 << Values << getOpenMPClauseName(OMPC_schedule);
5883 return nullptr;
5884 }
5885 Expr *ValExpr = ChunkSize;
Alexey Bataev040d5402015-05-12 08:35:28 +00005886 Expr *HelperValExpr = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00005887 if (ChunkSize) {
5888 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
5889 !ChunkSize->isInstantiationDependent() &&
5890 !ChunkSize->containsUnexpandedParameterPack()) {
5891 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
5892 ExprResult Val =
5893 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
5894 if (Val.isInvalid())
5895 return nullptr;
5896
5897 ValExpr = Val.get();
5898
5899 // OpenMP [2.7.1, Restrictions]
5900 // chunk_size must be a loop invariant integer expression with a positive
5901 // value.
5902 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00005903 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
5904 if (Result.isSigned() && !Result.isStrictlyPositive()) {
5905 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00005906 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00005907 return nullptr;
5908 }
5909 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
5910 auto *ImpVar = buildVarDecl(*this, ChunkSize->getExprLoc(),
5911 ChunkSize->getType(), ".chunk.");
5912 auto *ImpVarRef = buildDeclRefExpr(*this, ImpVar, ChunkSize->getType(),
5913 ChunkSize->getExprLoc(),
5914 /*RefersToCapture=*/true);
5915 HelperValExpr = ImpVarRef;
Alexey Bataev56dafe82014-06-20 07:16:17 +00005916 }
5917 }
5918 }
5919
5920 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc,
Alexey Bataev040d5402015-05-12 08:35:28 +00005921 EndLoc, Kind, ValExpr, HelperValExpr);
Alexey Bataev56dafe82014-06-20 07:16:17 +00005922}
5923
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005924OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
5925 SourceLocation StartLoc,
5926 SourceLocation EndLoc) {
5927 OMPClause *Res = nullptr;
5928 switch (Kind) {
5929 case OMPC_ordered:
5930 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
5931 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00005932 case OMPC_nowait:
5933 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
5934 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005935 case OMPC_untied:
5936 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
5937 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005938 case OMPC_mergeable:
5939 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
5940 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005941 case OMPC_read:
5942 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
5943 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00005944 case OMPC_write:
5945 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
5946 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00005947 case OMPC_update:
5948 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
5949 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00005950 case OMPC_capture:
5951 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
5952 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005953 case OMPC_seq_cst:
5954 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
5955 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00005956 case OMPC_threads:
5957 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
5958 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005959 case OMPC_simd:
5960 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
5961 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00005962 case OMPC_nogroup:
5963 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
5964 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005965 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00005966 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005967 case OMPC_num_threads:
5968 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00005969 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005970 case OMPC_collapse:
5971 case OMPC_schedule:
5972 case OMPC_private:
5973 case OMPC_firstprivate:
5974 case OMPC_lastprivate:
5975 case OMPC_shared:
5976 case OMPC_reduction:
5977 case OMPC_linear:
5978 case OMPC_aligned:
5979 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005980 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005981 case OMPC_default:
5982 case OMPC_proc_bind:
5983 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00005984 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005985 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00005986 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00005987 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00005988 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005989 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00005990 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00005991 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00005992 case OMPC_num_tasks:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005993 case OMPC_unknown:
5994 llvm_unreachable("Clause is not allowed.");
5995 }
5996 return Res;
5997}
5998
Alexey Bataev236070f2014-06-20 11:19:47 +00005999OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
6000 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006001 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00006002 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
6003}
6004
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006005OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
6006 SourceLocation EndLoc) {
6007 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
6008}
6009
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006010OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
6011 SourceLocation EndLoc) {
6012 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
6013}
6014
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006015OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
6016 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006017 return new (Context) OMPReadClause(StartLoc, EndLoc);
6018}
6019
Alexey Bataevdea47612014-07-23 07:46:59 +00006020OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
6021 SourceLocation EndLoc) {
6022 return new (Context) OMPWriteClause(StartLoc, EndLoc);
6023}
6024
Alexey Bataev67a4f222014-07-23 10:25:33 +00006025OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
6026 SourceLocation EndLoc) {
6027 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
6028}
6029
Alexey Bataev459dec02014-07-24 06:46:57 +00006030OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
6031 SourceLocation EndLoc) {
6032 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
6033}
6034
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006035OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
6036 SourceLocation EndLoc) {
6037 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
6038}
6039
Alexey Bataev346265e2015-09-25 10:37:12 +00006040OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
6041 SourceLocation EndLoc) {
6042 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
6043}
6044
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006045OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
6046 SourceLocation EndLoc) {
6047 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
6048}
6049
Alexey Bataevb825de12015-12-07 10:51:44 +00006050OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
6051 SourceLocation EndLoc) {
6052 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
6053}
6054
Alexey Bataevc5e02582014-06-16 07:08:35 +00006055OMPClause *Sema::ActOnOpenMPVarListClause(
6056 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
6057 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
6058 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006059 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Kelvin Li0bff7af2015-11-23 05:32:03 +00006060 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
6061 OpenMPMapClauseKind MapType, SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006062 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006063 switch (Kind) {
6064 case OMPC_private:
6065 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6066 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006067 case OMPC_firstprivate:
6068 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6069 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00006070 case OMPC_lastprivate:
6071 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6072 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00006073 case OMPC_shared:
6074 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
6075 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006076 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00006077 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
6078 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006079 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00006080 case OMPC_linear:
6081 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00006082 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00006083 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006084 case OMPC_aligned:
6085 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
6086 ColonLoc, EndLoc);
6087 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006088 case OMPC_copyin:
6089 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
6090 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00006091 case OMPC_copyprivate:
6092 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6093 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00006094 case OMPC_flush:
6095 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
6096 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006097 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006098 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
6099 StartLoc, LParenLoc, EndLoc);
6100 break;
6101 case OMPC_map:
6102 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, DepLinMapLoc, ColonLoc,
6103 VarList, StartLoc, LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006104 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006105 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006106 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00006107 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00006108 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006109 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00006110 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006111 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006112 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006113 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006114 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006115 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006116 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006117 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006118 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006119 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006120 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006121 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006122 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006123 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00006124 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006125 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006126 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006127 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006128 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006129 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006130 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006131 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006132 case OMPC_num_tasks:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006133 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006134 llvm_unreachable("Clause is not allowed.");
6135 }
6136 return Res;
6137}
6138
6139OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
6140 SourceLocation StartLoc,
6141 SourceLocation LParenLoc,
6142 SourceLocation EndLoc) {
6143 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00006144 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00006145 for (auto &RefExpr : VarList) {
6146 assert(RefExpr && "NULL expr in OpenMP private clause.");
6147 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006148 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006149 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006150 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006151 continue;
6152 }
6153
Alexey Bataeved09d242014-05-28 05:53:51 +00006154 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006155 // OpenMP [2.1, C/C++]
6156 // A list item is a variable name.
6157 // OpenMP [2.9.3.3, Restrictions, p.1]
6158 // A variable that is part of another variable (as an array or
6159 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00006160 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006161 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006162 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006163 continue;
6164 }
6165 Decl *D = DE->getDecl();
6166 VarDecl *VD = cast<VarDecl>(D);
6167
6168 QualType Type = VD->getType();
6169 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6170 // It will be analyzed later.
6171 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006172 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006173 continue;
6174 }
6175
6176 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
6177 // A variable that appears in a private clause must not have an incomplete
6178 // type or a reference type.
6179 if (RequireCompleteType(ELoc, Type,
6180 diag::err_omp_private_incomplete_type)) {
6181 continue;
6182 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006183 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006184
Alexey Bataev758e55e2013-09-06 18:03:48 +00006185 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6186 // in a Construct]
6187 // Variables with the predetermined data-sharing attributes may not be
6188 // listed in data-sharing attributes clauses, except for the cases
6189 // listed below. For these exceptions only, listing a predetermined
6190 // variable in a data-sharing attribute clause is allowed and overrides
6191 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006192 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006193 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006194 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
6195 << getOpenMPClauseName(OMPC_private);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006196 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006197 continue;
6198 }
6199
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006200 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00006201 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006202 DSAStack->getCurrentDirective() == OMPD_task) {
6203 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
6204 << getOpenMPClauseName(OMPC_private) << Type
6205 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
6206 bool IsDecl =
6207 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6208 Diag(VD->getLocation(),
6209 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6210 << VD;
6211 continue;
6212 }
6213
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006214 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
6215 // A variable of class type (or array thereof) that appears in a private
6216 // clause requires an accessible, unambiguous default constructor for the
6217 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00006218 // Generate helper private variable and initialize it with the default
6219 // value. The address of the original variable is replaced by the address of
6220 // the new private variable in CodeGen. This new variable is not added to
6221 // IdResolver, so the code in the OpenMP region uses original variable for
6222 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006223 Type = Type.getUnqualifiedType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006224 auto VDPrivate = buildVarDecl(*this, DE->getExprLoc(), Type, VD->getName(),
6225 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00006226 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006227 if (VDPrivate->isInvalidDecl())
6228 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006229 auto VDPrivateRefExpr = buildDeclRefExpr(
6230 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev03b340a2014-10-21 03:16:40 +00006231
Alexey Bataev758e55e2013-09-06 18:03:48 +00006232 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006233 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006234 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006235 }
6236
Alexey Bataeved09d242014-05-28 05:53:51 +00006237 if (Vars.empty())
6238 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006239
Alexey Bataev03b340a2014-10-21 03:16:40 +00006240 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
6241 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006242}
6243
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006244namespace {
6245class DiagsUninitializedSeveretyRAII {
6246private:
6247 DiagnosticsEngine &Diags;
6248 SourceLocation SavedLoc;
6249 bool IsIgnored;
6250
6251public:
6252 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
6253 bool IsIgnored)
6254 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
6255 if (!IsIgnored) {
6256 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
6257 /*Map*/ diag::Severity::Ignored, Loc);
6258 }
6259 }
6260 ~DiagsUninitializedSeveretyRAII() {
6261 if (!IsIgnored)
6262 Diags.popMappings(SavedLoc);
6263 }
6264};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00006265}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006266
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006267OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
6268 SourceLocation StartLoc,
6269 SourceLocation LParenLoc,
6270 SourceLocation EndLoc) {
6271 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006272 SmallVector<Expr *, 8> PrivateCopies;
6273 SmallVector<Expr *, 8> Inits;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006274 bool IsImplicitClause =
6275 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
6276 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
6277
Alexey Bataeved09d242014-05-28 05:53:51 +00006278 for (auto &RefExpr : VarList) {
6279 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
6280 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006281 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006282 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006283 PrivateCopies.push_back(nullptr);
6284 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006285 continue;
6286 }
6287
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006288 SourceLocation ELoc =
6289 IsImplicitClause ? ImplicitClauseLoc : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006290 // OpenMP [2.1, C/C++]
6291 // A list item is a variable name.
6292 // OpenMP [2.9.3.3, Restrictions, p.1]
6293 // A variable that is part of another variable (as an array or
6294 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00006295 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006296 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006297 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006298 continue;
6299 }
6300 Decl *D = DE->getDecl();
6301 VarDecl *VD = cast<VarDecl>(D);
6302
6303 QualType Type = VD->getType();
6304 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6305 // It will be analyzed later.
6306 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006307 PrivateCopies.push_back(nullptr);
6308 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006309 continue;
6310 }
6311
6312 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
6313 // A variable that appears in a private clause must not have an incomplete
6314 // type or a reference type.
6315 if (RequireCompleteType(ELoc, Type,
6316 diag::err_omp_firstprivate_incomplete_type)) {
6317 continue;
6318 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006319 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006320
6321 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
6322 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00006323 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006324 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006325 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006326
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006327 // If an implicit firstprivate variable found it was checked already.
6328 if (!IsImplicitClause) {
6329 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006330 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006331 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
6332 // A list item that specifies a given variable may not appear in more
6333 // than one clause on the same directive, except that a variable may be
6334 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006335 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00006336 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006337 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00006338 << getOpenMPClauseName(DVar.CKind)
6339 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006340 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006341 continue;
6342 }
6343
6344 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6345 // in a Construct]
6346 // Variables with the predetermined data-sharing attributes may not be
6347 // listed in data-sharing attributes clauses, except for the cases
6348 // listed below. For these exceptions only, listing a predetermined
6349 // variable in a data-sharing attribute clause is allowed and overrides
6350 // the variable's predetermined data-sharing attributes.
6351 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6352 // in a Construct, C/C++, p.2]
6353 // Variables with const-qualified type having no mutable member may be
6354 // listed in a firstprivate clause, even if they are static data members.
6355 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
6356 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
6357 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00006358 << getOpenMPClauseName(DVar.CKind)
6359 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006360 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006361 continue;
6362 }
6363
Alexey Bataevf29276e2014-06-18 04:14:57 +00006364 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006365 // OpenMP [2.9.3.4, Restrictions, p.2]
6366 // A list item that is private within a parallel region must not appear
6367 // in a firstprivate clause on a worksharing construct if any of the
6368 // worksharing regions arising from the worksharing construct ever bind
6369 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00006370 if (isOpenMPWorksharingDirective(CurrDir) &&
6371 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006372 DVar = DSAStack->getImplicitDSA(VD, true);
6373 if (DVar.CKind != OMPC_shared &&
6374 (isOpenMPParallelDirective(DVar.DKind) ||
6375 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00006376 Diag(ELoc, diag::err_omp_required_access)
6377 << getOpenMPClauseName(OMPC_firstprivate)
6378 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006379 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006380 continue;
6381 }
6382 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006383 // OpenMP [2.9.3.4, Restrictions, p.3]
6384 // A list item that appears in a reduction clause of a parallel construct
6385 // must not appear in a firstprivate clause on a worksharing or task
6386 // construct if any of the worksharing or task regions arising from the
6387 // worksharing or task construct ever bind to any of the parallel regions
6388 // arising from the parallel construct.
6389 // OpenMP [2.9.3.4, Restrictions, p.4]
6390 // A list item that appears in a reduction clause in worksharing
6391 // construct must not appear in a firstprivate clause in a task construct
6392 // encountered during execution of any of the worksharing regions arising
6393 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006394 if (CurrDir == OMPD_task) {
6395 DVar =
6396 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
6397 [](OpenMPDirectiveKind K) -> bool {
6398 return isOpenMPParallelDirective(K) ||
6399 isOpenMPWorksharingDirective(K);
6400 },
6401 false);
6402 if (DVar.CKind == OMPC_reduction &&
6403 (isOpenMPParallelDirective(DVar.DKind) ||
6404 isOpenMPWorksharingDirective(DVar.DKind))) {
6405 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
6406 << getOpenMPDirectiveName(DVar.DKind);
6407 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6408 continue;
6409 }
6410 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006411 }
6412
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006413 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00006414 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006415 DSAStack->getCurrentDirective() == OMPD_task) {
6416 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
6417 << getOpenMPClauseName(OMPC_firstprivate) << Type
6418 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
6419 bool IsDecl =
6420 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6421 Diag(VD->getLocation(),
6422 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6423 << VD;
6424 continue;
6425 }
6426
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006427 Type = Type.getUnqualifiedType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006428 auto VDPrivate = buildVarDecl(*this, ELoc, Type, VD->getName(),
6429 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006430 // Generate helper private variable and initialize it with the value of the
6431 // original variable. The address of the original variable is replaced by
6432 // the address of the new private variable in the CodeGen. This new variable
6433 // is not added to IdResolver, so the code in the OpenMP region uses
6434 // original variable for proper diagnostics and variable capturing.
6435 Expr *VDInitRefExpr = nullptr;
6436 // For arrays generate initializer for single element and replace it by the
6437 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006438 if (Type->isArrayType()) {
6439 auto VDInit =
6440 buildVarDecl(*this, DE->getExprLoc(), ElemType, VD->getName());
6441 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006442 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006443 ElemType = ElemType.getUnqualifiedType();
6444 auto *VDInitTemp = buildVarDecl(*this, DE->getLocStart(), ElemType,
6445 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00006446 InitializedEntity Entity =
6447 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006448 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
6449
6450 InitializationSequence InitSeq(*this, Entity, Kind, Init);
6451 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
6452 if (Result.isInvalid())
6453 VDPrivate->setInvalidDecl();
6454 else
6455 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006456 // Remove temp variable declaration.
6457 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006458 } else {
Alexey Bataev69c62a92015-04-15 04:52:20 +00006459 auto *VDInit =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006460 buildVarDecl(*this, DE->getLocStart(), Type, ".firstprivate.temp");
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006461 VDInitRefExpr =
6462 buildDeclRefExpr(*this, VDInit, DE->getType(), DE->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00006463 AddInitializerToDecl(VDPrivate,
6464 DefaultLvalueConversion(VDInitRefExpr).get(),
6465 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006466 }
6467 if (VDPrivate->isInvalidDecl()) {
6468 if (IsImplicitClause) {
6469 Diag(DE->getExprLoc(),
6470 diag::note_omp_task_predetermined_firstprivate_here);
6471 }
6472 continue;
6473 }
6474 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00006475 auto VDPrivateRefExpr = buildDeclRefExpr(
6476 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006477 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
6478 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006479 PrivateCopies.push_back(VDPrivateRefExpr);
6480 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006481 }
6482
Alexey Bataeved09d242014-05-28 05:53:51 +00006483 if (Vars.empty())
6484 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006485
6486 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006487 Vars, PrivateCopies, Inits);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006488}
6489
Alexander Musman1bb328c2014-06-04 13:06:39 +00006490OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
6491 SourceLocation StartLoc,
6492 SourceLocation LParenLoc,
6493 SourceLocation EndLoc) {
6494 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00006495 SmallVector<Expr *, 8> SrcExprs;
6496 SmallVector<Expr *, 8> DstExprs;
6497 SmallVector<Expr *, 8> AssignmentOps;
Alexander Musman1bb328c2014-06-04 13:06:39 +00006498 for (auto &RefExpr : VarList) {
6499 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
6500 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6501 // It will be analyzed later.
6502 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00006503 SrcExprs.push_back(nullptr);
6504 DstExprs.push_back(nullptr);
6505 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006506 continue;
6507 }
6508
6509 SourceLocation ELoc = RefExpr->getExprLoc();
6510 // OpenMP [2.1, C/C++]
6511 // A list item is a variable name.
6512 // OpenMP [2.14.3.5, Restrictions, p.1]
6513 // A variable that is part of another variable (as an array or structure
6514 // element) cannot appear in a lastprivate clause.
6515 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
6516 if (!DE || !isa<VarDecl>(DE->getDecl())) {
6517 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
6518 continue;
6519 }
6520 Decl *D = DE->getDecl();
6521 VarDecl *VD = cast<VarDecl>(D);
6522
6523 QualType Type = VD->getType();
6524 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6525 // It will be analyzed later.
6526 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00006527 SrcExprs.push_back(nullptr);
6528 DstExprs.push_back(nullptr);
6529 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006530 continue;
6531 }
6532
6533 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
6534 // A variable that appears in a lastprivate clause must not have an
6535 // incomplete type or a reference type.
6536 if (RequireCompleteType(ELoc, Type,
6537 diag::err_omp_lastprivate_incomplete_type)) {
6538 continue;
6539 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006540 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00006541
6542 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
6543 // in a Construct]
6544 // Variables with the predetermined data-sharing attributes may not be
6545 // listed in data-sharing attributes clauses, except for the cases
6546 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006547 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006548 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
6549 DVar.CKind != OMPC_firstprivate &&
6550 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
6551 Diag(ELoc, diag::err_omp_wrong_dsa)
6552 << getOpenMPClauseName(DVar.CKind)
6553 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006554 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006555 continue;
6556 }
6557
Alexey Bataevf29276e2014-06-18 04:14:57 +00006558 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
6559 // OpenMP [2.14.3.5, Restrictions, p.2]
6560 // A list item that is private within a parallel region, or that appears in
6561 // the reduction clause of a parallel construct, must not appear in a
6562 // lastprivate clause on a worksharing construct if any of the corresponding
6563 // worksharing regions ever binds to any of the corresponding parallel
6564 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00006565 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00006566 if (isOpenMPWorksharingDirective(CurrDir) &&
6567 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006568 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006569 if (DVar.CKind != OMPC_shared) {
6570 Diag(ELoc, diag::err_omp_required_access)
6571 << getOpenMPClauseName(OMPC_lastprivate)
6572 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006573 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006574 continue;
6575 }
6576 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00006577 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00006578 // A variable of class type (or array thereof) that appears in a
6579 // lastprivate clause requires an accessible, unambiguous default
6580 // constructor for the class type, unless the list item is also specified
6581 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00006582 // A variable of class type (or array thereof) that appears in a
6583 // lastprivate clause requires an accessible, unambiguous copy assignment
6584 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00006585 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00006586 auto *SrcVD = buildVarDecl(*this, DE->getLocStart(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006587 Type.getUnqualifiedType(), ".lastprivate.src",
6588 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00006589 auto *PseudoSrcExpr = buildDeclRefExpr(
6590 *this, SrcVD, Type.getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00006591 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006592 buildVarDecl(*this, DE->getLocStart(), Type, ".lastprivate.dst",
6593 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00006594 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006595 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00006596 // For arrays generate assignment operation for single element and replace
6597 // it by the original array element in CodeGen.
6598 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
6599 PseudoDstExpr, PseudoSrcExpr);
6600 if (AssignmentOp.isInvalid())
6601 continue;
6602 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
6603 /*DiscardedValue=*/true);
6604 if (AssignmentOp.isInvalid())
6605 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00006606
Alexey Bataev39f915b82015-05-08 10:41:21 +00006607 if (TopDVar.CKind != OMPC_firstprivate)
Alexey Bataevf29276e2014-06-18 04:14:57 +00006608 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006609 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00006610 SrcExprs.push_back(PseudoSrcExpr);
6611 DstExprs.push_back(PseudoDstExpr);
6612 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00006613 }
6614
6615 if (Vars.empty())
6616 return nullptr;
6617
6618 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00006619 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006620}
6621
Alexey Bataev758e55e2013-09-06 18:03:48 +00006622OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
6623 SourceLocation StartLoc,
6624 SourceLocation LParenLoc,
6625 SourceLocation EndLoc) {
6626 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00006627 for (auto &RefExpr : VarList) {
6628 assert(RefExpr && "NULL expr in OpenMP shared clause.");
6629 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00006630 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006631 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006632 continue;
6633 }
6634
Alexey Bataeved09d242014-05-28 05:53:51 +00006635 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00006636 // OpenMP [2.1, C/C++]
6637 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00006638 // OpenMP [2.14.3.2, Restrictions, p.1]
6639 // A variable that is part of another variable (as an array or structure
6640 // element) cannot appear in a shared unless it is a static data member
6641 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00006642 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006643 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006644 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00006645 continue;
6646 }
6647 Decl *D = DE->getDecl();
6648 VarDecl *VD = cast<VarDecl>(D);
6649
6650 QualType Type = VD->getType();
6651 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6652 // It will be analyzed later.
6653 Vars.push_back(DE);
6654 continue;
6655 }
6656
6657 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6658 // in a Construct]
6659 // Variables with the predetermined data-sharing attributes may not be
6660 // listed in data-sharing attributes clauses, except for the cases
6661 // listed below. For these exceptions only, listing a predetermined
6662 // variable in a data-sharing attribute clause is allowed and overrides
6663 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006664 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00006665 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
6666 DVar.RefExpr) {
6667 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
6668 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006669 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006670 continue;
6671 }
6672
6673 DSAStack->addDSA(VD, DE, OMPC_shared);
6674 Vars.push_back(DE);
6675 }
6676
Alexey Bataeved09d242014-05-28 05:53:51 +00006677 if (Vars.empty())
6678 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00006679
6680 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
6681}
6682
Alexey Bataevc5e02582014-06-16 07:08:35 +00006683namespace {
6684class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
6685 DSAStackTy *Stack;
6686
6687public:
6688 bool VisitDeclRefExpr(DeclRefExpr *E) {
6689 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006690 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006691 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
6692 return false;
6693 if (DVar.CKind != OMPC_unknown)
6694 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00006695 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006696 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006697 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00006698 return true;
6699 return false;
6700 }
6701 return false;
6702 }
6703 bool VisitStmt(Stmt *S) {
6704 for (auto Child : S->children()) {
6705 if (Child && Visit(Child))
6706 return true;
6707 }
6708 return false;
6709 }
Alexey Bataev23b69422014-06-18 07:08:49 +00006710 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00006711};
Alexey Bataev23b69422014-06-18 07:08:49 +00006712} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00006713
6714OMPClause *Sema::ActOnOpenMPReductionClause(
6715 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
6716 SourceLocation ColonLoc, SourceLocation EndLoc,
6717 CXXScopeSpec &ReductionIdScopeSpec,
6718 const DeclarationNameInfo &ReductionId) {
6719 // TODO: Allow scope specification search when 'declare reduction' is
6720 // supported.
6721 assert(ReductionIdScopeSpec.isEmpty() &&
6722 "No support for scoped reduction identifiers yet.");
6723
6724 auto DN = ReductionId.getName();
6725 auto OOK = DN.getCXXOverloadedOperator();
6726 BinaryOperatorKind BOK = BO_Comma;
6727
6728 // OpenMP [2.14.3.6, reduction clause]
6729 // C
6730 // reduction-identifier is either an identifier or one of the following
6731 // operators: +, -, *, &, |, ^, && and ||
6732 // C++
6733 // reduction-identifier is either an id-expression or one of the following
6734 // operators: +, -, *, &, |, ^, && and ||
6735 // FIXME: Only 'min' and 'max' identifiers are supported for now.
6736 switch (OOK) {
6737 case OO_Plus:
6738 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006739 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006740 break;
6741 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006742 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006743 break;
6744 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006745 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006746 break;
6747 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006748 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006749 break;
6750 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006751 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006752 break;
6753 case OO_AmpAmp:
6754 BOK = BO_LAnd;
6755 break;
6756 case OO_PipePipe:
6757 BOK = BO_LOr;
6758 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006759 case OO_New:
6760 case OO_Delete:
6761 case OO_Array_New:
6762 case OO_Array_Delete:
6763 case OO_Slash:
6764 case OO_Percent:
6765 case OO_Tilde:
6766 case OO_Exclaim:
6767 case OO_Equal:
6768 case OO_Less:
6769 case OO_Greater:
6770 case OO_LessEqual:
6771 case OO_GreaterEqual:
6772 case OO_PlusEqual:
6773 case OO_MinusEqual:
6774 case OO_StarEqual:
6775 case OO_SlashEqual:
6776 case OO_PercentEqual:
6777 case OO_CaretEqual:
6778 case OO_AmpEqual:
6779 case OO_PipeEqual:
6780 case OO_LessLess:
6781 case OO_GreaterGreater:
6782 case OO_LessLessEqual:
6783 case OO_GreaterGreaterEqual:
6784 case OO_EqualEqual:
6785 case OO_ExclaimEqual:
6786 case OO_PlusPlus:
6787 case OO_MinusMinus:
6788 case OO_Comma:
6789 case OO_ArrowStar:
6790 case OO_Arrow:
6791 case OO_Call:
6792 case OO_Subscript:
6793 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +00006794 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006795 case NUM_OVERLOADED_OPERATORS:
6796 llvm_unreachable("Unexpected reduction identifier");
6797 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006798 if (auto II = DN.getAsIdentifierInfo()) {
6799 if (II->isStr("max"))
6800 BOK = BO_GT;
6801 else if (II->isStr("min"))
6802 BOK = BO_LT;
6803 }
6804 break;
6805 }
6806 SourceRange ReductionIdRange;
6807 if (ReductionIdScopeSpec.isValid()) {
6808 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
6809 }
6810 ReductionIdRange.setEnd(ReductionId.getEndLoc());
6811 if (BOK == BO_Comma) {
6812 // Not allowed reduction identifier is found.
6813 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
6814 << ReductionIdRange;
6815 return nullptr;
6816 }
6817
6818 SmallVector<Expr *, 8> Vars;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006819 SmallVector<Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006820 SmallVector<Expr *, 8> LHSs;
6821 SmallVector<Expr *, 8> RHSs;
6822 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006823 for (auto RefExpr : VarList) {
6824 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
6825 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6826 // It will be analyzed later.
6827 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006828 Privates.push_back(nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006829 LHSs.push_back(nullptr);
6830 RHSs.push_back(nullptr);
6831 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006832 continue;
6833 }
6834
6835 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
6836 RefExpr->isInstantiationDependent() ||
6837 RefExpr->containsUnexpandedParameterPack()) {
6838 // It will be analyzed later.
6839 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006840 Privates.push_back(nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006841 LHSs.push_back(nullptr);
6842 RHSs.push_back(nullptr);
6843 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006844 continue;
6845 }
6846
6847 auto ELoc = RefExpr->getExprLoc();
6848 auto ERange = RefExpr->getSourceRange();
6849 // OpenMP [2.1, C/C++]
6850 // A list item is a variable or array section, subject to the restrictions
6851 // specified in Section 2.4 on page 42 and in each of the sections
6852 // describing clauses and directives for which a list appears.
6853 // OpenMP [2.14.3.3, Restrictions, p.1]
6854 // A variable that is part of another variable (as an array or
6855 // structure element) cannot appear in a private clause.
Alexey Bataeva1764212015-09-30 09:22:36 +00006856 auto *DE = dyn_cast<DeclRefExpr>(RefExpr);
6857 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr);
6858 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr);
6859 if (!ASE && !OASE && (!DE || !isa<VarDecl>(DE->getDecl()))) {
6860 Diag(ELoc, diag::err_omp_expected_var_name_or_array_item) << ERange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006861 continue;
6862 }
Alexey Bataeva1764212015-09-30 09:22:36 +00006863 QualType Type;
6864 VarDecl *VD = nullptr;
6865 if (DE) {
6866 auto D = DE->getDecl();
6867 VD = cast<VarDecl>(D);
6868 Type = VD->getType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006869 } else if (ASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00006870 Type = ASE->getType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006871 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
6872 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
6873 Base = TempASE->getBase()->IgnoreParenImpCasts();
6874 DE = dyn_cast<DeclRefExpr>(Base);
6875 if (DE)
6876 VD = dyn_cast<VarDecl>(DE->getDecl());
6877 if (!VD) {
6878 Diag(Base->getExprLoc(), diag::err_omp_expected_base_var_name)
6879 << 0 << Base->getSourceRange();
6880 continue;
6881 }
6882 } else if (OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00006883 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
6884 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
6885 Type = ATy->getElementType();
6886 else
6887 Type = BaseType->getPointeeType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006888 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
6889 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
6890 Base = TempOASE->getBase()->IgnoreParenImpCasts();
6891 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
6892 Base = TempASE->getBase()->IgnoreParenImpCasts();
6893 DE = dyn_cast<DeclRefExpr>(Base);
6894 if (DE)
6895 VD = dyn_cast<VarDecl>(DE->getDecl());
6896 if (!VD) {
6897 Diag(Base->getExprLoc(), diag::err_omp_expected_base_var_name)
6898 << 1 << Base->getSourceRange();
6899 continue;
6900 }
Alexey Bataeva1764212015-09-30 09:22:36 +00006901 }
6902
Alexey Bataevc5e02582014-06-16 07:08:35 +00006903 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
6904 // A variable that appears in a private clause must not have an incomplete
6905 // type or a reference type.
6906 if (RequireCompleteType(ELoc, Type,
6907 diag::err_omp_reduction_incomplete_type))
6908 continue;
6909 // OpenMP [2.14.3.6, reduction clause, Restrictions]
6910 // Arrays may not appear in a reduction clause.
6911 if (Type.getNonReferenceType()->isArrayType()) {
6912 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006913 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00006914 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
6915 VarDecl::DeclarationOnly;
6916 Diag(VD->getLocation(),
6917 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6918 << VD;
6919 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00006920 continue;
6921 }
6922 // OpenMP [2.14.3.6, reduction clause, Restrictions]
6923 // A list item that appears in a reduction clause must not be
6924 // const-qualified.
6925 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataeva1764212015-09-30 09:22:36 +00006926 Diag(ELoc, diag::err_omp_const_reduction_list_item)
Alexey Bataevc5e02582014-06-16 07:08:35 +00006927 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006928 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00006929 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
6930 VarDecl::DeclarationOnly;
6931 Diag(VD->getLocation(),
6932 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6933 << VD;
6934 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00006935 continue;
6936 }
6937 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
6938 // If a list-item is a reference type then it must bind to the same object
6939 // for all threads of the team.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006940 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00006941 VarDecl *VDDef = VD->getDefinition();
6942 if (Type->isReferenceType() && VDDef) {
6943 DSARefChecker Check(DSAStack);
6944 if (Check.Visit(VDDef->getInit())) {
6945 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
6946 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
6947 continue;
6948 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00006949 }
6950 }
6951 // OpenMP [2.14.3.6, reduction clause, Restrictions]
6952 // The type of a list item that appears in a reduction clause must be valid
6953 // for the reduction-identifier. For a max or min reduction in C, the type
6954 // of the list item must be an allowed arithmetic data type: char, int,
6955 // float, double, or _Bool, possibly modified with long, short, signed, or
6956 // unsigned. For a max or min reduction in C++, the type of the list item
6957 // must be an allowed arithmetic data type: char, wchar_t, int, float,
6958 // double, or bool, possibly modified with long, short, signed, or unsigned.
6959 if ((BOK == BO_GT || BOK == BO_LT) &&
6960 !(Type->isScalarType() ||
6961 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
6962 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
6963 << getLangOpts().CPlusPlus;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006964 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00006965 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
6966 VarDecl::DeclarationOnly;
6967 Diag(VD->getLocation(),
6968 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6969 << VD;
6970 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00006971 continue;
6972 }
6973 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
6974 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
6975 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006976 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00006977 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
6978 VarDecl::DeclarationOnly;
6979 Diag(VD->getLocation(),
6980 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6981 << VD;
6982 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00006983 continue;
6984 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00006985 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
6986 // in a Construct]
6987 // Variables with the predetermined data-sharing attributes may not be
6988 // listed in data-sharing attributes clauses, except for the cases
6989 // listed below. For these exceptions only, listing a predetermined
6990 // variable in a data-sharing attribute clause is allowed and overrides
6991 // the variable's predetermined data-sharing attributes.
6992 // OpenMP [2.14.3.6, Restrictions, p.3]
6993 // Any number of reduction clauses can be specified on the directive,
6994 // but a list item can appear only once in the reduction clauses for that
6995 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +00006996 DSAStackTy::DSAVarData DVar;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006997 DVar = DSAStack->getTopDSA(VD, false);
6998 if (DVar.CKind == OMPC_reduction) {
6999 Diag(ELoc, diag::err_omp_once_referenced)
7000 << getOpenMPClauseName(OMPC_reduction);
7001 if (DVar.RefExpr) {
7002 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007003 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007004 } else if (DVar.CKind != OMPC_unknown) {
7005 Diag(ELoc, diag::err_omp_wrong_dsa)
7006 << getOpenMPClauseName(DVar.CKind)
7007 << getOpenMPClauseName(OMPC_reduction);
7008 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7009 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007010 }
7011
7012 // OpenMP [2.14.3.6, Restrictions, p.1]
7013 // A list item that appears in a reduction clause of a worksharing
7014 // construct must be shared in the parallel regions to which any of the
7015 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007016 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
7017 if (isOpenMPWorksharingDirective(CurrDir) &&
7018 !isOpenMPParallelDirective(CurrDir)) {
7019 DVar = DSAStack->getImplicitDSA(VD, true);
7020 if (DVar.CKind != OMPC_shared) {
7021 Diag(ELoc, diag::err_omp_required_access)
7022 << getOpenMPClauseName(OMPC_reduction)
7023 << getOpenMPClauseName(OMPC_shared);
7024 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7025 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +00007026 }
7027 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007028
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007029 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007030 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
7031 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
7032 auto *RHSVD = buildVarDecl(*this, ELoc, Type, VD->getName(),
7033 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
7034 auto PrivateTy = Type;
7035 if (OASE) {
7036 // For array sections only:
7037 // Create pseudo array type for private copy. The size for this array will
7038 // be generated during codegen.
7039 // For array subscripts or single variables Private Ty is the same as Type
7040 // (type of the variable or single array element).
7041 PrivateTy = Context.getVariableArrayType(
7042 Type, new (Context) OpaqueValueExpr(SourceLocation(),
7043 Context.getSizeType(), VK_RValue),
7044 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
7045 }
7046 // Private copy.
7047 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, VD->getName(),
7048 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007049 // Add initializer for private variable.
7050 Expr *Init = nullptr;
7051 switch (BOK) {
7052 case BO_Add:
7053 case BO_Xor:
7054 case BO_Or:
7055 case BO_LOr:
7056 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
7057 if (Type->isScalarType() || Type->isAnyComplexType()) {
7058 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00007059 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007060 break;
7061 case BO_Mul:
7062 case BO_LAnd:
7063 if (Type->isScalarType() || Type->isAnyComplexType()) {
7064 // '*' and '&&' reduction ops - initializer is '1'.
7065 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
7066 }
7067 break;
7068 case BO_And: {
7069 // '&' reduction op - initializer is '~0'.
7070 QualType OrigType = Type;
7071 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) {
7072 Type = ComplexTy->getElementType();
7073 }
7074 if (Type->isRealFloatingType()) {
7075 llvm::APFloat InitValue =
7076 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
7077 /*isIEEE=*/true);
7078 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
7079 Type, ELoc);
7080 } else if (Type->isScalarType()) {
7081 auto Size = Context.getTypeSize(Type);
7082 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
7083 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
7084 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
7085 }
7086 if (Init && OrigType->isAnyComplexType()) {
7087 // Init = 0xFFFF + 0xFFFFi;
7088 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
7089 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
7090 }
7091 Type = OrigType;
7092 break;
7093 }
7094 case BO_LT:
7095 case BO_GT: {
7096 // 'min' reduction op - initializer is 'Largest representable number in
7097 // the reduction list item type'.
7098 // 'max' reduction op - initializer is 'Least representable number in
7099 // the reduction list item type'.
7100 if (Type->isIntegerType() || Type->isPointerType()) {
7101 bool IsSigned = Type->hasSignedIntegerRepresentation();
7102 auto Size = Context.getTypeSize(Type);
7103 QualType IntTy =
7104 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
7105 llvm::APInt InitValue =
7106 (BOK != BO_LT)
7107 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
7108 : llvm::APInt::getMinValue(Size)
7109 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
7110 : llvm::APInt::getMaxValue(Size);
7111 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
7112 if (Type->isPointerType()) {
7113 // Cast to pointer type.
7114 auto CastExpr = BuildCStyleCastExpr(
7115 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
7116 SourceLocation(), Init);
7117 if (CastExpr.isInvalid())
7118 continue;
7119 Init = CastExpr.get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00007120 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007121 } else if (Type->isRealFloatingType()) {
7122 llvm::APFloat InitValue = llvm::APFloat::getLargest(
7123 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
7124 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
7125 Type, ELoc);
7126 }
7127 break;
7128 }
7129 case BO_PtrMemD:
7130 case BO_PtrMemI:
7131 case BO_MulAssign:
7132 case BO_Div:
7133 case BO_Rem:
7134 case BO_Sub:
7135 case BO_Shl:
7136 case BO_Shr:
7137 case BO_LE:
7138 case BO_GE:
7139 case BO_EQ:
7140 case BO_NE:
7141 case BO_AndAssign:
7142 case BO_XorAssign:
7143 case BO_OrAssign:
7144 case BO_Assign:
7145 case BO_AddAssign:
7146 case BO_SubAssign:
7147 case BO_DivAssign:
7148 case BO_RemAssign:
7149 case BO_ShlAssign:
7150 case BO_ShrAssign:
7151 case BO_Comma:
7152 llvm_unreachable("Unexpected reduction operation");
7153 }
7154 if (Init) {
7155 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
7156 /*TypeMayContainAuto=*/false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007157 } else
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007158 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007159 if (!RHSVD->hasInit()) {
7160 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
7161 << ReductionIdRange;
Alexey Bataeva1764212015-09-30 09:22:36 +00007162 if (VD) {
7163 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7164 VarDecl::DeclarationOnly;
7165 Diag(VD->getLocation(),
7166 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7167 << VD;
7168 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007169 continue;
7170 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007171 // Store initializer for single element in private copy. Will be used during
7172 // codegen.
7173 PrivateVD->setInit(RHSVD->getInit());
7174 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataev39f915b82015-05-08 10:41:21 +00007175 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
7176 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007177 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007178 ExprResult ReductionOp =
7179 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
7180 LHSDRE, RHSDRE);
7181 if (ReductionOp.isUsable()) {
Alexey Bataev69a47792015-05-07 03:54:03 +00007182 if (BOK != BO_LT && BOK != BO_GT) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007183 ReductionOp =
7184 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
7185 BO_Assign, LHSDRE, ReductionOp.get());
7186 } else {
7187 auto *ConditionalOp = new (Context) ConditionalOperator(
7188 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
7189 RHSDRE, Type, VK_LValue, OK_Ordinary);
7190 ReductionOp =
7191 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
7192 BO_Assign, LHSDRE, ConditionalOp);
7193 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007194 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00007195 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007196 if (ReductionOp.isInvalid())
7197 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007198
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007199 DSAStack->addDSA(VD, DE, OMPC_reduction);
Alexey Bataeva1764212015-09-30 09:22:36 +00007200 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007201 Privates.push_back(PrivateDRE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007202 LHSs.push_back(LHSDRE);
7203 RHSs.push_back(RHSDRE);
7204 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00007205 }
7206
7207 if (Vars.empty())
7208 return nullptr;
7209
7210 return OMPReductionClause::Create(
7211 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007212 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
7213 LHSs, RHSs, ReductionOps);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007214}
7215
Alexey Bataev182227b2015-08-20 10:54:39 +00007216OMPClause *Sema::ActOnOpenMPLinearClause(
7217 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
7218 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
7219 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00007220 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007221 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00007222 SmallVector<Expr *, 8> Inits;
Alexey Bataev182227b2015-08-20 10:54:39 +00007223 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
7224 LinKind == OMPC_LINEAR_unknown) {
7225 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
7226 LinKind = OMPC_LINEAR_val;
7227 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007228 for (auto &RefExpr : VarList) {
7229 assert(RefExpr && "NULL expr in OpenMP linear clause.");
7230 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00007231 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007232 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007233 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00007234 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00007235 continue;
7236 }
7237
7238 // OpenMP [2.14.3.7, linear clause]
7239 // A list item that appears in a linear clause is subject to the private
7240 // clause semantics described in Section 2.14.3.3 on page 159 except as
7241 // noted. In addition, the value of the new list item on each iteration
7242 // of the associated loop(s) corresponds to the value of the original
7243 // list item before entering the construct plus the logical number of
7244 // the iteration times linear-step.
7245
Alexey Bataeved09d242014-05-28 05:53:51 +00007246 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00007247 // OpenMP [2.1, C/C++]
7248 // A list item is a variable name.
7249 // OpenMP [2.14.3.3, Restrictions, p.1]
7250 // A variable that is part of another variable (as an array or
7251 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00007252 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00007253 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00007254 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00007255 continue;
7256 }
7257
7258 VarDecl *VD = cast<VarDecl>(DE->getDecl());
7259
7260 // OpenMP [2.14.3.7, linear clause]
7261 // A list-item cannot appear in more than one linear clause.
7262 // A list-item that appears in a linear clause cannot appear in any
7263 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007264 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00007265 if (DVar.RefExpr) {
7266 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7267 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007268 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00007269 continue;
7270 }
7271
7272 QualType QType = VD->getType();
7273 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
7274 // It will be analyzed later.
7275 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007276 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00007277 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00007278 continue;
7279 }
7280
7281 // A variable must not have an incomplete type or a reference type.
7282 if (RequireCompleteType(ELoc, QType,
7283 diag::err_omp_linear_incomplete_type)) {
7284 continue;
7285 }
Alexey Bataev1185e192015-08-20 12:15:57 +00007286 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
7287 !QType->isReferenceType()) {
7288 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
7289 << QType << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
7290 continue;
7291 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007292 QType = QType.getNonReferenceType();
Alexander Musman8dba6642014-04-22 13:09:42 +00007293
7294 // A list item must not be const-qualified.
7295 if (QType.isConstant(Context)) {
7296 Diag(ELoc, diag::err_omp_const_variable)
7297 << getOpenMPClauseName(OMPC_linear);
7298 bool IsDecl =
7299 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7300 Diag(VD->getLocation(),
7301 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7302 << VD;
7303 continue;
7304 }
7305
7306 // A list item must be of integral or pointer type.
7307 QType = QType.getUnqualifiedType().getCanonicalType();
7308 const Type *Ty = QType.getTypePtrOrNull();
7309 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
7310 !Ty->isPointerType())) {
7311 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
7312 bool IsDecl =
7313 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7314 Diag(VD->getLocation(),
7315 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7316 << VD;
7317 continue;
7318 }
7319
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007320 // Build private copy of original var.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007321 auto *Private = buildVarDecl(*this, ELoc, QType, VD->getName(),
7322 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007323 auto *PrivateRef = buildDeclRefExpr(
7324 *this, Private, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman3276a272015-03-21 10:12:56 +00007325 // Build var to save initial value.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007326 VarDecl *Init = buildVarDecl(*this, ELoc, QType, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00007327 Expr *InitExpr;
7328 if (LinKind == OMPC_LINEAR_uval)
7329 InitExpr = VD->getInit();
7330 else
7331 InitExpr = DE;
7332 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexander Musman3276a272015-03-21 10:12:56 +00007333 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007334 auto InitRef = buildDeclRefExpr(
7335 *this, Init, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman8dba6642014-04-22 13:09:42 +00007336 DSAStack->addDSA(VD, DE, OMPC_linear);
7337 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007338 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00007339 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00007340 }
7341
7342 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007343 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00007344
7345 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00007346 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00007347 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
7348 !Step->isInstantiationDependent() &&
7349 !Step->containsUnexpandedParameterPack()) {
7350 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00007351 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00007352 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007353 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00007354 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00007355
Alexander Musman3276a272015-03-21 10:12:56 +00007356 // Build var to save the step value.
7357 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007358 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00007359 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007360 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00007361 ExprResult CalcStep =
7362 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007363 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00007364
Alexander Musman8dba6642014-04-22 13:09:42 +00007365 // Warn about zero linear step (it would be probably better specified as
7366 // making corresponding variables 'const').
7367 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00007368 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
7369 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00007370 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
7371 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00007372 if (!IsConstant && CalcStep.isUsable()) {
7373 // Calculate the step beforehand instead of doing this on each iteration.
7374 // (This is not used if the number of iterations may be kfold-ed).
7375 CalcStepExpr = CalcStep.get();
7376 }
Alexander Musman8dba6642014-04-22 13:09:42 +00007377 }
7378
Alexey Bataev182227b2015-08-20 10:54:39 +00007379 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
7380 ColonLoc, EndLoc, Vars, Privates, Inits,
7381 StepExpr, CalcStepExpr);
Alexander Musman3276a272015-03-21 10:12:56 +00007382}
7383
7384static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
7385 Expr *NumIterations, Sema &SemaRef,
7386 Scope *S) {
7387 // Walk the vars and build update/final expressions for the CodeGen.
7388 SmallVector<Expr *, 8> Updates;
7389 SmallVector<Expr *, 8> Finals;
7390 Expr *Step = Clause.getStep();
7391 Expr *CalcStep = Clause.getCalcStep();
7392 // OpenMP [2.14.3.7, linear clause]
7393 // If linear-step is not specified it is assumed to be 1.
7394 if (Step == nullptr)
7395 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
7396 else if (CalcStep)
7397 Step = cast<BinaryOperator>(CalcStep)->getLHS();
7398 bool HasErrors = false;
7399 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007400 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00007401 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00007402 for (auto &RefExpr : Clause.varlists()) {
7403 Expr *InitExpr = *CurInit;
7404
7405 // Build privatized reference to the current linear var.
7406 auto DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00007407 Expr *CapturedRef;
7408 if (LinKind == OMPC_LINEAR_uval)
7409 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
7410 else
7411 CapturedRef =
7412 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
7413 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
7414 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00007415
7416 // Build update: Var = InitExpr + IV * Step
7417 ExprResult Update =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007418 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
Alexander Musman3276a272015-03-21 10:12:56 +00007419 InitExpr, IV, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007420 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
7421 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00007422
7423 // Build final: Var = InitExpr + NumIterations * Step
7424 ExprResult Final =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007425 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
Alexey Bataev39f915b82015-05-08 10:41:21 +00007426 InitExpr, NumIterations, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007427 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
7428 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00007429 if (!Update.isUsable() || !Final.isUsable()) {
7430 Updates.push_back(nullptr);
7431 Finals.push_back(nullptr);
7432 HasErrors = true;
7433 } else {
7434 Updates.push_back(Update.get());
7435 Finals.push_back(Final.get());
7436 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007437 ++CurInit, ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00007438 }
7439 Clause.setUpdates(Updates);
7440 Clause.setFinals(Finals);
7441 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00007442}
7443
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007444OMPClause *Sema::ActOnOpenMPAlignedClause(
7445 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
7446 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
7447
7448 SmallVector<Expr *, 8> Vars;
7449 for (auto &RefExpr : VarList) {
7450 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
7451 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7452 // It will be analyzed later.
7453 Vars.push_back(RefExpr);
7454 continue;
7455 }
7456
7457 SourceLocation ELoc = RefExpr->getExprLoc();
7458 // OpenMP [2.1, C/C++]
7459 // A list item is a variable name.
7460 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
7461 if (!DE || !isa<VarDecl>(DE->getDecl())) {
7462 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
7463 continue;
7464 }
7465
7466 VarDecl *VD = cast<VarDecl>(DE->getDecl());
7467
7468 // OpenMP [2.8.1, simd construct, Restrictions]
7469 // The type of list items appearing in the aligned clause must be
7470 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007471 QualType QType = VD->getType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007472 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007473 const Type *Ty = QType.getTypePtrOrNull();
7474 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
7475 !Ty->isPointerType())) {
7476 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
7477 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
7478 bool IsDecl =
7479 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7480 Diag(VD->getLocation(),
7481 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7482 << VD;
7483 continue;
7484 }
7485
7486 // OpenMP [2.8.1, simd construct, Restrictions]
7487 // A list-item cannot appear in more than one aligned clause.
7488 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
7489 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
7490 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
7491 << getOpenMPClauseName(OMPC_aligned);
7492 continue;
7493 }
7494
7495 Vars.push_back(DE);
7496 }
7497
7498 // OpenMP [2.8.1, simd construct, Description]
7499 // The parameter of the aligned clause, alignment, must be a constant
7500 // positive integer expression.
7501 // If no optional parameter is specified, implementation-defined default
7502 // alignments for SIMD instructions on the target platforms are assumed.
7503 if (Alignment != nullptr) {
7504 ExprResult AlignResult =
7505 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
7506 if (AlignResult.isInvalid())
7507 return nullptr;
7508 Alignment = AlignResult.get();
7509 }
7510 if (Vars.empty())
7511 return nullptr;
7512
7513 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
7514 EndLoc, Vars, Alignment);
7515}
7516
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007517OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
7518 SourceLocation StartLoc,
7519 SourceLocation LParenLoc,
7520 SourceLocation EndLoc) {
7521 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007522 SmallVector<Expr *, 8> SrcExprs;
7523 SmallVector<Expr *, 8> DstExprs;
7524 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00007525 for (auto &RefExpr : VarList) {
7526 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
7527 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007528 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007529 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007530 SrcExprs.push_back(nullptr);
7531 DstExprs.push_back(nullptr);
7532 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007533 continue;
7534 }
7535
Alexey Bataeved09d242014-05-28 05:53:51 +00007536 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007537 // OpenMP [2.1, C/C++]
7538 // A list item is a variable name.
7539 // OpenMP [2.14.4.1, Restrictions, p.1]
7540 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00007541 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007542 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00007543 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007544 continue;
7545 }
7546
7547 Decl *D = DE->getDecl();
7548 VarDecl *VD = cast<VarDecl>(D);
7549
7550 QualType Type = VD->getType();
7551 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
7552 // It will be analyzed later.
7553 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007554 SrcExprs.push_back(nullptr);
7555 DstExprs.push_back(nullptr);
7556 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007557 continue;
7558 }
7559
7560 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
7561 // A list item that appears in a copyin clause must be threadprivate.
7562 if (!DSAStack->isThreadPrivate(VD)) {
7563 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00007564 << getOpenMPClauseName(OMPC_copyin)
7565 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007566 continue;
7567 }
7568
7569 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
7570 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00007571 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007572 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007573 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007574 auto *SrcVD =
7575 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
7576 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007577 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007578 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
7579 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007580 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
7581 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007582 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007583 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007584 // For arrays generate assignment operation for single element and replace
7585 // it by the original array element in CodeGen.
7586 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
7587 PseudoDstExpr, PseudoSrcExpr);
7588 if (AssignmentOp.isInvalid())
7589 continue;
7590 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
7591 /*DiscardedValue=*/true);
7592 if (AssignmentOp.isInvalid())
7593 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007594
7595 DSAStack->addDSA(VD, DE, OMPC_copyin);
7596 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007597 SrcExprs.push_back(PseudoSrcExpr);
7598 DstExprs.push_back(PseudoDstExpr);
7599 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007600 }
7601
Alexey Bataeved09d242014-05-28 05:53:51 +00007602 if (Vars.empty())
7603 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007604
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007605 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
7606 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007607}
7608
Alexey Bataevbae9a792014-06-27 10:37:06 +00007609OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
7610 SourceLocation StartLoc,
7611 SourceLocation LParenLoc,
7612 SourceLocation EndLoc) {
7613 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00007614 SmallVector<Expr *, 8> SrcExprs;
7615 SmallVector<Expr *, 8> DstExprs;
7616 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00007617 for (auto &RefExpr : VarList) {
7618 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
7619 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7620 // It will be analyzed later.
7621 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00007622 SrcExprs.push_back(nullptr);
7623 DstExprs.push_back(nullptr);
7624 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00007625 continue;
7626 }
7627
7628 SourceLocation ELoc = RefExpr->getExprLoc();
7629 // OpenMP [2.1, C/C++]
7630 // A list item is a variable name.
7631 // OpenMP [2.14.4.1, Restrictions, p.1]
7632 // A list item that appears in a copyin clause must be threadprivate.
7633 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
7634 if (!DE || !isa<VarDecl>(DE->getDecl())) {
7635 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
7636 continue;
7637 }
7638
7639 Decl *D = DE->getDecl();
7640 VarDecl *VD = cast<VarDecl>(D);
7641
7642 QualType Type = VD->getType();
7643 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
7644 // It will be analyzed later.
7645 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00007646 SrcExprs.push_back(nullptr);
7647 DstExprs.push_back(nullptr);
7648 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00007649 continue;
7650 }
7651
7652 // OpenMP [2.14.4.2, Restrictions, p.2]
7653 // A list item that appears in a copyprivate clause may not appear in a
7654 // private or firstprivate clause on the single construct.
7655 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007656 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00007657 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
7658 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00007659 Diag(ELoc, diag::err_omp_wrong_dsa)
7660 << getOpenMPClauseName(DVar.CKind)
7661 << getOpenMPClauseName(OMPC_copyprivate);
7662 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7663 continue;
7664 }
7665
7666 // OpenMP [2.11.4.2, Restrictions, p.1]
7667 // All list items that appear in a copyprivate clause must be either
7668 // threadprivate or private in the enclosing context.
7669 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007670 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00007671 if (DVar.CKind == OMPC_shared) {
7672 Diag(ELoc, diag::err_omp_required_access)
7673 << getOpenMPClauseName(OMPC_copyprivate)
7674 << "threadprivate or private in the enclosing context";
7675 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7676 continue;
7677 }
7678 }
7679 }
7680
Alexey Bataev7a3e5852015-05-19 08:19:24 +00007681 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007682 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00007683 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007684 << getOpenMPClauseName(OMPC_copyprivate) << Type
7685 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00007686 bool IsDecl =
7687 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7688 Diag(VD->getLocation(),
7689 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7690 << VD;
7691 continue;
7692 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007693
Alexey Bataevbae9a792014-06-27 10:37:06 +00007694 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
7695 // A variable of class type (or array thereof) that appears in a
7696 // copyin clause requires an accessible, unambiguous copy assignment
7697 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007698 Type = Context.getBaseElementType(Type.getNonReferenceType())
7699 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +00007700 auto *SrcVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007701 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.src",
7702 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00007703 auto *PseudoSrcExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007704 buildDeclRefExpr(*this, SrcVD, Type, DE->getExprLoc());
Alexey Bataev420d45b2015-04-14 05:11:24 +00007705 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007706 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.dst",
7707 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00007708 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007709 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataeva63048e2015-03-23 06:18:07 +00007710 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
7711 PseudoDstExpr, PseudoSrcExpr);
7712 if (AssignmentOp.isInvalid())
7713 continue;
7714 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
7715 /*DiscardedValue=*/true);
7716 if (AssignmentOp.isInvalid())
7717 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00007718
7719 // No need to mark vars as copyprivate, they are already threadprivate or
7720 // implicitly private.
7721 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00007722 SrcExprs.push_back(PseudoSrcExpr);
7723 DstExprs.push_back(PseudoDstExpr);
7724 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +00007725 }
7726
7727 if (Vars.empty())
7728 return nullptr;
7729
Alexey Bataeva63048e2015-03-23 06:18:07 +00007730 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
7731 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00007732}
7733
Alexey Bataev6125da92014-07-21 11:26:11 +00007734OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
7735 SourceLocation StartLoc,
7736 SourceLocation LParenLoc,
7737 SourceLocation EndLoc) {
7738 if (VarList.empty())
7739 return nullptr;
7740
7741 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
7742}
Alexey Bataevdea47612014-07-23 07:46:59 +00007743
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007744OMPClause *
7745Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
7746 SourceLocation DepLoc, SourceLocation ColonLoc,
7747 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
7748 SourceLocation LParenLoc, SourceLocation EndLoc) {
7749 if (DepKind == OMPC_DEPEND_unknown) {
7750 std::string Values;
7751 std::string Sep(", ");
7752 for (unsigned i = 0; i < OMPC_DEPEND_unknown; ++i) {
7753 Values += "'";
7754 Values += getOpenMPSimpleClauseTypeName(OMPC_depend, i);
7755 Values += "'";
7756 switch (i) {
7757 case OMPC_DEPEND_unknown - 2:
7758 Values += " or ";
7759 break;
7760 case OMPC_DEPEND_unknown - 1:
7761 break;
7762 default:
7763 Values += Sep;
7764 break;
7765 }
7766 }
7767 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
7768 << Values << getOpenMPClauseName(OMPC_depend);
7769 return nullptr;
7770 }
7771 SmallVector<Expr *, 8> Vars;
7772 for (auto &RefExpr : VarList) {
7773 assert(RefExpr && "NULL expr in OpenMP shared clause.");
7774 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7775 // It will be analyzed later.
7776 Vars.push_back(RefExpr);
7777 continue;
7778 }
7779
7780 SourceLocation ELoc = RefExpr->getExprLoc();
7781 // OpenMP [2.11.1.1, Restrictions, p.3]
7782 // A variable that is part of another variable (such as a field of a
7783 // structure) but is not an array element or an array section cannot appear
7784 // in a depend clause.
7785 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
Alexey Bataev1a3320e2015-08-25 14:24:04 +00007786 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
7787 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
7788 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
7789 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
7790 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007791 (ASE && !ASE->getBase()->getType()->isAnyPointerType() &&
7792 !ASE->getBase()->getType()->isArrayType())) {
7793 Diag(ELoc, diag::err_omp_expected_var_name_or_array_item)
7794 << RefExpr->getSourceRange();
7795 continue;
7796 }
7797
7798 Vars.push_back(RefExpr->IgnoreParenImpCasts());
7799 }
7800
7801 if (Vars.empty())
7802 return nullptr;
7803
7804 return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
7805 DepLoc, ColonLoc, Vars);
7806}
Michael Wonge710d542015-08-07 16:16:36 +00007807
7808OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
7809 SourceLocation LParenLoc,
7810 SourceLocation EndLoc) {
7811 Expr *ValExpr = Device;
Michael Wonge710d542015-08-07 16:16:36 +00007812
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007813 // OpenMP [2.9.1, Restrictions]
7814 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00007815 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
7816 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007817 return nullptr;
7818
Michael Wonge710d542015-08-07 16:16:36 +00007819 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
7820}
Kelvin Li0bff7af2015-11-23 05:32:03 +00007821
7822static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
7823 DSAStackTy *Stack, CXXRecordDecl *RD) {
7824 if (!RD || RD->isInvalidDecl())
7825 return true;
7826
7827 auto QTy = SemaRef.Context.getRecordType(RD);
7828 if (RD->isDynamicClass()) {
7829 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
7830 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
7831 return false;
7832 }
7833 auto *DC = RD;
7834 bool IsCorrect = true;
7835 for (auto *I : DC->decls()) {
7836 if (I) {
7837 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
7838 if (MD->isStatic()) {
7839 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
7840 SemaRef.Diag(MD->getLocation(),
7841 diag::note_omp_static_member_in_target);
7842 IsCorrect = false;
7843 }
7844 } else if (auto *VD = dyn_cast<VarDecl>(I)) {
7845 if (VD->isStaticDataMember()) {
7846 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
7847 SemaRef.Diag(VD->getLocation(),
7848 diag::note_omp_static_member_in_target);
7849 IsCorrect = false;
7850 }
7851 }
7852 }
7853 }
7854
7855 for (auto &I : RD->bases()) {
7856 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
7857 I.getType()->getAsCXXRecordDecl()))
7858 IsCorrect = false;
7859 }
7860 return IsCorrect;
7861}
7862
7863static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
7864 DSAStackTy *Stack, QualType QTy) {
7865 NamedDecl *ND;
7866 if (QTy->isIncompleteType(&ND)) {
7867 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
7868 return false;
7869 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
7870 if (!RD->isInvalidDecl() &&
7871 !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
7872 return false;
7873 }
7874 return true;
7875}
7876
7877OMPClause *Sema::ActOnOpenMPMapClause(
7878 OpenMPMapClauseKind MapTypeModifier, OpenMPMapClauseKind MapType,
7879 SourceLocation MapLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VarList,
7880 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
7881 SmallVector<Expr *, 4> Vars;
7882
7883 for (auto &RE : VarList) {
7884 assert(RE && "Null expr in omp map");
7885 if (isa<DependentScopeDeclRefExpr>(RE)) {
7886 // It will be analyzed later.
7887 Vars.push_back(RE);
7888 continue;
7889 }
7890 SourceLocation ELoc = RE->getExprLoc();
7891
7892 // OpenMP [2.14.5, Restrictions]
7893 // A variable that is part of another variable (such as field of a
7894 // structure) but is not an array element or an array section cannot appear
7895 // in a map clause.
7896 auto *VE = RE->IgnoreParenLValueCasts();
7897
7898 if (VE->isValueDependent() || VE->isTypeDependent() ||
7899 VE->isInstantiationDependent() ||
7900 VE->containsUnexpandedParameterPack()) {
7901 // It will be analyzed later.
7902 Vars.push_back(RE);
7903 continue;
7904 }
7905
7906 auto *SimpleExpr = RE->IgnoreParenCasts();
7907 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
7908 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
7909 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
7910
7911 if (!RE->IgnoreParenImpCasts()->isLValue() ||
7912 (!OASE && !ASE && !DE) ||
7913 (DE && !isa<VarDecl>(DE->getDecl())) ||
7914 (ASE && !ASE->getBase()->getType()->isAnyPointerType() &&
7915 !ASE->getBase()->getType()->isArrayType())) {
7916 Diag(ELoc, diag::err_omp_expected_var_name_or_array_item)
7917 << RE->getSourceRange();
7918 continue;
7919 }
7920
7921 Decl *D = nullptr;
7922 if (DE) {
7923 D = DE->getDecl();
7924 } else if (ASE) {
7925 auto *B = ASE->getBase()->IgnoreParenCasts();
7926 D = dyn_cast<DeclRefExpr>(B)->getDecl();
7927 } else if (OASE) {
7928 auto *B = OASE->getBase();
7929 D = dyn_cast<DeclRefExpr>(B)->getDecl();
7930 }
7931 assert(D && "Null decl on map clause.");
7932 auto *VD = cast<VarDecl>(D);
7933
7934 // OpenMP [2.14.5, Restrictions, p.8]
7935 // threadprivate variables cannot appear in a map clause.
7936 if (DSAStack->isThreadPrivate(VD)) {
7937 auto DVar = DSAStack->getTopDSA(VD, false);
7938 Diag(ELoc, diag::err_omp_threadprivate_in_map);
7939 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7940 continue;
7941 }
7942
7943 // OpenMP [2.14.5, Restrictions, p.2]
7944 // At most one list item can be an array item derived from a given variable
7945 // in map clauses of the same construct.
7946 // OpenMP [2.14.5, Restrictions, p.3]
7947 // List items of map clauses in the same construct must not share original
7948 // storage.
7949 // OpenMP [2.14.5, Restrictions, C/C++, p.2]
7950 // A variable for which the type is pointer, reference to array, or
7951 // reference to pointer and an array section derived from that variable
7952 // must not appear as list items of map clauses of the same construct.
7953 DSAStackTy::MapInfo MI = DSAStack->IsMappedInCurrentRegion(VD);
7954 if (MI.RefExpr) {
7955 Diag(ELoc, diag::err_omp_map_shared_storage) << ELoc;
7956 Diag(MI.RefExpr->getExprLoc(), diag::note_used_here)
7957 << MI.RefExpr->getSourceRange();
7958 continue;
7959 }
7960
7961 // OpenMP [2.14.5, Restrictions, C/C++, p.3,4]
7962 // A variable for which the type is pointer, reference to array, or
7963 // reference to pointer must not appear as a list item if the enclosing
7964 // device data environment already contains an array section derived from
7965 // that variable.
7966 // An array section derived from a variable for which the type is pointer,
7967 // reference to array, or reference to pointer must not appear as a list
7968 // item if the enclosing device data environment already contains that
7969 // variable.
7970 QualType Type = VD->getType();
7971 MI = DSAStack->getMapInfoForVar(VD);
7972 if (MI.RefExpr && (isa<DeclRefExpr>(MI.RefExpr->IgnoreParenLValueCasts()) !=
7973 isa<DeclRefExpr>(VE)) &&
7974 (Type->isPointerType() || Type->isReferenceType())) {
7975 Diag(ELoc, diag::err_omp_map_shared_storage) << ELoc;
7976 Diag(MI.RefExpr->getExprLoc(), diag::note_used_here)
7977 << MI.RefExpr->getSourceRange();
7978 continue;
7979 }
7980
7981 // OpenMP [2.14.5, Restrictions, C/C++, p.7]
7982 // A list item must have a mappable type.
7983 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), *this,
7984 DSAStack, Type))
7985 continue;
7986
7987 Vars.push_back(RE);
7988 MI.RefExpr = RE;
7989 DSAStack->addMapInfoForVar(VD, MI);
7990 }
7991 if (Vars.empty())
7992 return nullptr;
7993
7994 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
7995 MapTypeModifier, MapType, MapLoc);
7996}
Kelvin Li099bb8c2015-11-24 20:50:12 +00007997
7998OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
7999 SourceLocation StartLoc,
8000 SourceLocation LParenLoc,
8001 SourceLocation EndLoc) {
8002 Expr *ValExpr = NumTeams;
Kelvin Li099bb8c2015-11-24 20:50:12 +00008003
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008004 // OpenMP [teams Constrcut, Restrictions]
8005 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00008006 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
8007 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008008 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +00008009
8010 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8011}
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008012
8013OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
8014 SourceLocation StartLoc,
8015 SourceLocation LParenLoc,
8016 SourceLocation EndLoc) {
8017 Expr *ValExpr = ThreadLimit;
8018
8019 // OpenMP [teams Constrcut, Restrictions]
8020 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00008021 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
8022 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008023 return nullptr;
8024
8025 return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc,
8026 EndLoc);
8027}
Alexey Bataeva0569352015-12-01 10:17:31 +00008028
8029OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
8030 SourceLocation StartLoc,
8031 SourceLocation LParenLoc,
8032 SourceLocation EndLoc) {
8033 Expr *ValExpr = Priority;
8034
8035 // OpenMP [2.9.1, task Constrcut]
8036 // The priority-value is a non-negative numerical scalar expression.
8037 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
8038 /*StrictlyPositive=*/false))
8039 return nullptr;
8040
8041 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8042}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008043
8044OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
8045 SourceLocation StartLoc,
8046 SourceLocation LParenLoc,
8047 SourceLocation EndLoc) {
8048 Expr *ValExpr = Grainsize;
8049
8050 // OpenMP [2.9.2, taskloop Constrcut]
8051 // The parameter of the grainsize clause must be a positive integer
8052 // expression.
8053 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
8054 /*StrictlyPositive=*/true))
8055 return nullptr;
8056
8057 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8058}
Alexey Bataev382967a2015-12-08 12:06:20 +00008059
8060OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
8061 SourceLocation StartLoc,
8062 SourceLocation LParenLoc,
8063 SourceLocation EndLoc) {
8064 Expr *ValExpr = NumTasks;
8065
8066 // OpenMP [2.9.2, taskloop Constrcut]
8067 // The parameter of the num_tasks clause must be a positive integer
8068 // expression.
8069 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
8070 /*StrictlyPositive=*/true))
8071 return nullptr;
8072
8073 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8074}
8075