blob: a458969c7e252a5289cd58eb127a77a90e0f3a48 [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 ||
349 DKind == OMPD_taskloop;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000350}
Alexey Bataeved09d242014-05-28 05:53:51 +0000351} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000352
353DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter,
354 VarDecl *D) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000355 D = D->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000356 DSAVarData DVar;
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000357 if (Iter == std::prev(Stack.rend())) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000358 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
359 // in a region but not in construct]
360 // File-scope or namespace-scope variables referenced in called routines
361 // in the region are shared unless they appear in a threadprivate
362 // directive.
Alexey Bataev8b9cb982014-07-24 02:33:58 +0000363 if (!D->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D))
Alexey Bataev750a58b2014-03-18 12:19:12 +0000364 DVar.CKind = OMPC_shared;
365
366 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
367 // in a region but not in construct]
368 // Variables with static storage duration that are declared in called
369 // routines in the region are shared.
370 if (D->hasGlobalStorage())
371 DVar.CKind = OMPC_shared;
372
Alexey Bataev758e55e2013-09-06 18:03:48 +0000373 return DVar;
374 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000375
Alexey Bataev758e55e2013-09-06 18:03:48 +0000376 DVar.DKind = Iter->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +0000377 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
378 // in a Construct, C/C++, predetermined, p.1]
379 // Variables with automatic storage duration that are declared in a scope
380 // inside the construct are private.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000381 if (isOpenMPLocal(D, Iter) && D->isLocalVarDecl() &&
382 (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) {
383 DVar.CKind = OMPC_private;
384 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000385 }
386
Alexey Bataev758e55e2013-09-06 18:03:48 +0000387 // Explicitly specified attributes and local variables with predetermined
388 // attributes.
389 if (Iter->SharingMap.count(D)) {
390 DVar.RefExpr = Iter->SharingMap[D].RefExpr;
391 DVar.CKind = Iter->SharingMap[D].Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000392 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000393 return DVar;
394 }
395
396 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
397 // in a Construct, C/C++, implicitly determined, p.1]
398 // In a parallel or task construct, the data-sharing attributes of these
399 // variables are determined by the default clause, if present.
400 switch (Iter->DefaultAttr) {
401 case DSA_shared:
402 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000403 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000404 return DVar;
405 case DSA_none:
406 return DVar;
407 case DSA_unspecified:
408 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
409 // in a Construct, implicitly determined, p.2]
410 // In a parallel construct, if no default clause is present, these
411 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000412 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000413 if (isOpenMPParallelDirective(DVar.DKind) ||
414 isOpenMPTeamsDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000415 DVar.CKind = OMPC_shared;
416 return DVar;
417 }
418
419 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
420 // in a Construct, implicitly determined, p.4]
421 // In a task construct, if no default clause is present, a variable that in
422 // the enclosing context is determined to be shared by all implicit tasks
423 // bound to the current team is shared.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000424 if (DVar.DKind == OMPD_task) {
425 DSAVarData DVarTemp;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000426 for (StackTy::reverse_iterator I = std::next(Iter), EE = Stack.rend();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000427 I != EE; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000428 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
429 // Referenced
Alexey Bataev758e55e2013-09-06 18:03:48 +0000430 // in a Construct, implicitly determined, p.6]
431 // In a task construct, if no default clause is present, a variable
432 // whose data-sharing attribute is not determined by the rules above is
433 // firstprivate.
434 DVarTemp = getDSA(I, D);
435 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000436 DVar.RefExpr = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000437 DVar.DKind = OMPD_task;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000438 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000439 return DVar;
440 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000441 if (isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000442 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000443 }
444 DVar.DKind = OMPD_task;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000445 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000446 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000447 return DVar;
448 }
449 }
450 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
451 // in a Construct, implicitly determined, p.3]
452 // For constructs other than task, if no default clause is present, these
453 // variables inherit their data-sharing attributes from the enclosing
454 // context.
Benjamin Kramer167e9992014-03-02 12:20:24 +0000455 return getDSA(std::next(Iter), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000456}
457
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000458DeclRefExpr *DSAStackTy::addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE) {
459 assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000460 D = D->getCanonicalDecl();
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000461 auto It = Stack.back().AlignedMap.find(D);
462 if (It == Stack.back().AlignedMap.end()) {
463 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
464 Stack.back().AlignedMap[D] = NewDE;
465 return nullptr;
466 } else {
467 assert(It->second && "Unexpected nullptr expr in the aligned map");
468 return It->second;
469 }
470 return nullptr;
471}
472
Alexey Bataev9c821032015-04-30 04:23:23 +0000473void DSAStackTy::addLoopControlVariable(VarDecl *D) {
474 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
475 D = D->getCanonicalDecl();
476 Stack.back().LCVSet.insert(D);
477}
478
479bool DSAStackTy::isLoopControlVariable(VarDecl *D) {
480 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
481 D = D->getCanonicalDecl();
482 return Stack.back().LCVSet.count(D) > 0;
483}
484
Alexey Bataev758e55e2013-09-06 18:03:48 +0000485void DSAStackTy::addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000486 D = D->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000487 if (A == OMPC_threadprivate) {
488 Stack[0].SharingMap[D].Attributes = A;
489 Stack[0].SharingMap[D].RefExpr = E;
490 } else {
491 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
492 Stack.back().SharingMap[D].Attributes = A;
493 Stack.back().SharingMap[D].RefExpr = E;
494 }
495}
496
Alexey Bataeved09d242014-05-28 05:53:51 +0000497bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000498 D = D->getCanonicalDecl();
Alexey Bataevec3da872014-01-31 05:15:34 +0000499 if (Stack.size() > 2) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000500 reverse_iterator I = Iter, E = std::prev(Stack.rend());
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000501 Scope *TopScope = nullptr;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000502 while (I != E && !isParallelOrTaskRegion(I->Directive)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000503 ++I;
504 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000505 if (I == E)
506 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000507 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000508 Scope *CurScope = getCurScope();
509 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000510 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000511 }
512 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000513 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000514 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000515}
516
Alexey Bataev39f915b82015-05-08 10:41:21 +0000517/// \brief Build a variable declaration for OpenMP loop iteration variable.
518static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000519 StringRef Name, const AttrVec *Attrs = nullptr) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000520 DeclContext *DC = SemaRef.CurContext;
521 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
522 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
523 VarDecl *Decl =
524 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000525 if (Attrs) {
526 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
527 I != E; ++I)
528 Decl->addAttr(*I);
529 }
Alexey Bataev39f915b82015-05-08 10:41:21 +0000530 Decl->setImplicit();
531 return Decl;
532}
533
534static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
535 SourceLocation Loc,
536 bool RefersToCapture = false) {
537 D->setReferenced();
538 D->markUsed(S.Context);
539 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
540 SourceLocation(), D, RefersToCapture, Loc, Ty,
541 VK_LValue);
542}
543
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000544DSAStackTy::DSAVarData DSAStackTy::getTopDSA(VarDecl *D, bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000545 D = D->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000546 DSAVarData DVar;
547
548 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
549 // in a Construct, C/C++, predetermined, p.1]
550 // Variables appearing in threadprivate directives are threadprivate.
Samuel Antaof8b50122015-07-13 22:54:53 +0000551 if ((D->getTLSKind() != VarDecl::TLS_None &&
552 !(D->hasAttr<OMPThreadPrivateDeclAttr>() &&
553 SemaRef.getLangOpts().OpenMPUseTLS &&
554 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +0000555 (D->getStorageClass() == SC_Register && D->hasAttr<AsmLabelAttr>() &&
556 !D->isLocalVarDecl())) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000557 addDSA(D, buildDeclRefExpr(SemaRef, D, D->getType().getNonReferenceType(),
558 D->getLocation()),
Alexey Bataevf2453a02015-05-06 07:25:08 +0000559 OMPC_threadprivate);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000560 }
561 if (Stack[0].SharingMap.count(D)) {
562 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
563 DVar.CKind = OMPC_threadprivate;
564 return DVar;
565 }
566
567 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
568 // in a Construct, C/C++, predetermined, p.1]
569 // Variables with automatic storage duration that are declared in a scope
570 // inside the construct are private.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000571 OpenMPDirectiveKind Kind =
572 FromParent ? getParentDirective() : getCurrentDirective();
573 auto StartI = std::next(Stack.rbegin());
574 auto EndI = std::prev(Stack.rend());
575 if (FromParent && StartI != EndI) {
576 StartI = std::next(StartI);
577 }
578 if (!isParallelOrTaskRegion(Kind)) {
Alexey Bataev8b9cb982014-07-24 02:33:58 +0000579 if (isOpenMPLocal(D, StartI) &&
580 ((D->isLocalVarDecl() && (D->getStorageClass() == SC_Auto ||
581 D->getStorageClass() == SC_None)) ||
582 isa<ParmVarDecl>(D))) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000583 DVar.CKind = OMPC_private;
584 return DVar;
Alexander Musman8dba6642014-04-22 13:09:42 +0000585 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000586
Alexey Bataev24b04aa2015-01-16 07:11:33 +0000587 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
588 // in a Construct, C/C++, predetermined, p.4]
589 // Static data members are shared.
Alexey Bataev24b04aa2015-01-16 07:11:33 +0000590 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
591 // in a Construct, C/C++, predetermined, p.7]
592 // Variables with static storage duration that are declared in a scope
593 // inside the construct are shared.
Kelvin Li4eea8c62015-09-15 18:56:58 +0000594 if (D->isStaticDataMember()) {
Alexey Bataev42971a32015-01-20 07:03:46 +0000595 DSAVarData DVarTemp =
596 hasDSA(D, isOpenMPPrivate, MatchesAlways(), FromParent);
597 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
598 return DVar;
599
Alexey Bataev24b04aa2015-01-16 07:11:33 +0000600 DVar.CKind = OMPC_shared;
601 return DVar;
602 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000603 }
604
605 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +0000606 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
607 Type = SemaRef.getASTContext().getBaseElementType(Type);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000608 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
609 // in a Construct, C/C++, predetermined, p.6]
610 // Variables with const qualified type having no mutable member are
611 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000612 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000613 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000614 if (IsConstant &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000615 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000616 // Variables with const-qualified type having no mutable member may be
617 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000618 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
619 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000620 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
621 return DVar;
622
Alexey Bataev758e55e2013-09-06 18:03:48 +0000623 DVar.CKind = OMPC_shared;
624 return DVar;
625 }
626
Alexey Bataev758e55e2013-09-06 18:03:48 +0000627 // Explicitly specified attributes and local variables with predetermined
628 // attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000629 auto I = std::prev(StartI);
630 if (I->SharingMap.count(D)) {
631 DVar.RefExpr = I->SharingMap[D].RefExpr;
632 DVar.CKind = I->SharingMap[D].Attributes;
633 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000634 }
635
636 return DVar;
637}
638
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000639DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D, bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000640 D = D->getCanonicalDecl();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000641 auto StartI = Stack.rbegin();
642 auto EndI = std::prev(Stack.rend());
643 if (FromParent && StartI != EndI) {
644 StartI = std::next(StartI);
645 }
646 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000647}
648
Alexey Bataevf29276e2014-06-18 04:14:57 +0000649template <class ClausesPredicate, class DirectivesPredicate>
650DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000651 DirectivesPredicate DPred,
652 bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000653 D = D->getCanonicalDecl();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000654 auto StartI = std::next(Stack.rbegin());
655 auto EndI = std::prev(Stack.rend());
656 if (FromParent && StartI != EndI) {
657 StartI = std::next(StartI);
658 }
659 for (auto I = StartI, EE = EndI; I != EE; ++I) {
660 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000661 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000662 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000663 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000664 return DVar;
665 }
666 return DSAVarData();
667}
668
Alexey Bataevf29276e2014-06-18 04:14:57 +0000669template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000670DSAStackTy::DSAVarData
671DSAStackTy::hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
672 DirectivesPredicate DPred, bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000673 D = D->getCanonicalDecl();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000674 auto StartI = std::next(Stack.rbegin());
675 auto EndI = std::prev(Stack.rend());
676 if (FromParent && StartI != EndI) {
677 StartI = std::next(StartI);
678 }
679 for (auto I = StartI, EE = EndI; I != EE; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000680 if (!DPred(I->Directive))
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000681 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000682 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000683 if (CPred(DVar.CKind))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000684 return DVar;
685 return DSAVarData();
686 }
687 return DSAVarData();
688}
689
Alexey Bataevaac108a2015-06-23 04:51:00 +0000690bool DSAStackTy::hasExplicitDSA(
691 VarDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
692 unsigned Level) {
693 if (CPred(ClauseKindMode))
694 return true;
695 if (isClauseParsingMode())
696 ++Level;
697 D = D->getCanonicalDecl();
698 auto StartI = Stack.rbegin();
699 auto EndI = std::prev(Stack.rend());
NAKAMURA Takumi0332eda2015-06-23 10:01:20 +0000700 if (std::distance(StartI, EndI) <= (int)Level)
Alexey Bataevaac108a2015-06-23 04:51:00 +0000701 return false;
702 std::advance(StartI, Level);
703 return (StartI->SharingMap.count(D) > 0) && StartI->SharingMap[D].RefExpr &&
704 CPred(StartI->SharingMap[D].Attributes);
705}
706
Samuel Antao4be30e92015-10-02 17:14:03 +0000707bool DSAStackTy::hasExplicitDirective(
708 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
709 unsigned Level) {
710 if (isClauseParsingMode())
711 ++Level;
712 auto StartI = Stack.rbegin();
713 auto EndI = std::prev(Stack.rend());
714 if (std::distance(StartI, EndI) <= (int)Level)
715 return false;
716 std::advance(StartI, Level);
717 return DPred(StartI->Directive);
718}
719
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000720template <class NamedDirectivesPredicate>
721bool DSAStackTy::hasDirective(NamedDirectivesPredicate DPred, bool FromParent) {
722 auto StartI = std::next(Stack.rbegin());
723 auto EndI = std::prev(Stack.rend());
724 if (FromParent && StartI != EndI) {
725 StartI = std::next(StartI);
726 }
727 for (auto I = StartI, EE = EndI; I != EE; ++I) {
728 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
729 return true;
730 }
731 return false;
732}
733
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000734OpenMPDirectiveKind DSAStackTy::getDirectiveForScope(const Scope *S) const {
735 for (auto I = Stack.rbegin(), EE = Stack.rend(); I != EE; ++I)
736 if (I->CurScope == S)
737 return I->Directive;
738 return OMPD_unknown;
739}
740
Alexey Bataev758e55e2013-09-06 18:03:48 +0000741void Sema::InitDataSharingAttributesStack() {
742 VarDataSharingAttributesStack = new DSAStackTy(*this);
743}
744
745#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
746
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000747bool Sema::IsOpenMPCapturedByRef(VarDecl *VD,
748 const CapturedRegionScopeInfo *RSI) {
749 assert(LangOpts.OpenMP && "OpenMP is not allowed");
750
751 auto &Ctx = getASTContext();
752 bool IsByRef = true;
753
754 // Find the directive that is associated with the provided scope.
755 auto DKind = DSAStack->getDirectiveForScope(RSI->TheScope);
756 auto Ty = VD->getType();
757
758 if (isOpenMPTargetDirective(DKind)) {
759 // This table summarizes how a given variable should be passed to the device
760 // given its type and the clauses where it appears. This table is based on
761 // the description in OpenMP 4.5 [2.10.4, target Construct] and
762 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
763 //
764 // =========================================================================
765 // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
766 // | |(tofrom:scalar)| | pvt | | | |
767 // =========================================================================
768 // | scl | | | | - | | bycopy|
769 // | scl | | - | x | - | - | bycopy|
770 // | scl | | x | - | - | - | null |
771 // | scl | x | | | - | | byref |
772 // | scl | x | - | x | - | - | bycopy|
773 // | scl | x | x | - | - | - | null |
774 // | scl | | - | - | - | x | byref |
775 // | scl | x | - | - | - | x | byref |
776 //
777 // | agg | n.a. | | | - | | byref |
778 // | agg | n.a. | - | x | - | - | byref |
779 // | agg | n.a. | x | - | - | - | null |
780 // | agg | n.a. | - | - | - | x | byref |
781 // | agg | n.a. | - | - | - | x[] | byref |
782 //
783 // | ptr | n.a. | | | - | | bycopy|
784 // | ptr | n.a. | - | x | - | - | bycopy|
785 // | ptr | n.a. | x | - | - | - | null |
786 // | ptr | n.a. | - | - | - | x | byref |
787 // | ptr | n.a. | - | - | - | x[] | bycopy|
788 // | ptr | n.a. | - | - | x | | bycopy|
789 // | ptr | n.a. | - | - | x | x | bycopy|
790 // | ptr | n.a. | - | - | x | x[] | bycopy|
791 // =========================================================================
792 // Legend:
793 // scl - scalar
794 // ptr - pointer
795 // agg - aggregate
796 // x - applies
797 // - - invalid in this combination
798 // [] - mapped with an array section
799 // byref - should be mapped by reference
800 // byval - should be mapped by value
801 // null - initialize a local variable to null on the device
802 //
803 // Observations:
804 // - All scalar declarations that show up in a map clause have to be passed
805 // by reference, because they may have been mapped in the enclosing data
806 // environment.
807 // - If the scalar value does not fit the size of uintptr, it has to be
808 // passed by reference, regardless the result in the table above.
809 // - For pointers mapped by value that have either an implicit map or an
810 // array section, the runtime library may pass the NULL value to the
811 // device instead of the value passed to it by the compiler.
812
813 // FIXME: Right now, only implicit maps are implemented. Properly mapping
814 // values requires having the map, private, and firstprivate clauses SEMA
815 // and parsing in place, which we don't yet.
816
817 if (Ty->isReferenceType())
818 Ty = Ty->castAs<ReferenceType>()->getPointeeType();
819 IsByRef = !Ty->isScalarType();
820 }
821
822 // When passing data by value, we need to make sure it fits the uintptr size
823 // and alignment, because the runtime library only deals with uintptr types.
824 // If it does not fit the uintptr size, we need to pass the data by reference
825 // instead.
826 if (!IsByRef &&
827 (Ctx.getTypeSizeInChars(Ty) >
828 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
829 Ctx.getDeclAlign(VD) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType())))
830 IsByRef = true;
831
832 return IsByRef;
833}
834
Alexey Bataevf841bd92014-12-16 07:00:22 +0000835bool Sema::IsOpenMPCapturedVar(VarDecl *VD) {
836 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000837 VD = VD->getCanonicalDecl();
Samuel Antao4be30e92015-10-02 17:14:03 +0000838
839 // If we are attempting to capture a global variable in a directive with
840 // 'target' we return true so that this global is also mapped to the device.
841 //
842 // FIXME: If the declaration is enclosed in a 'declare target' directive,
843 // then it should not be captured. Therefore, an extra check has to be
844 // inserted here once support for 'declare target' is added.
845 //
846 if (!VD->hasLocalStorage()) {
847 if (DSAStack->getCurrentDirective() == OMPD_target &&
848 !DSAStack->isClauseParsingMode()) {
849 return true;
850 }
851 if (DSAStack->getCurScope() &&
852 DSAStack->hasDirective(
853 [](OpenMPDirectiveKind K, const DeclarationNameInfo &DNI,
854 SourceLocation Loc) -> bool {
855 return isOpenMPTargetDirective(K);
856 },
857 false)) {
858 return true;
859 }
860 }
861
Alexey Bataev48977c32015-08-04 08:10:48 +0000862 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
863 (!DSAStack->isClauseParsingMode() ||
864 DSAStack->getParentDirective() != OMPD_unknown)) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000865 if (DSAStack->isLoopControlVariable(VD) ||
866 (VD->hasLocalStorage() &&
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000867 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
868 DSAStack->isForceVarCapturing())
Alexey Bataev9c821032015-04-30 04:23:23 +0000869 return true;
Alexey Bataevaac108a2015-06-23 04:51:00 +0000870 auto DVarPrivate = DSAStack->getTopDSA(VD, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000871 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
872 return true;
873 DVarPrivate = DSAStack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(),
Alexey Bataevaac108a2015-06-23 04:51:00 +0000874 DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000875 return DVarPrivate.CKind != OMPC_unknown;
876 }
877 return false;
878}
879
Alexey Bataevaac108a2015-06-23 04:51:00 +0000880bool Sema::isOpenMPPrivateVar(VarDecl *VD, unsigned Level) {
881 assert(LangOpts.OpenMP && "OpenMP is not allowed");
882 return DSAStack->hasExplicitDSA(
883 VD, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level);
884}
885
Samuel Antao4be30e92015-10-02 17:14:03 +0000886bool Sema::isOpenMPTargetCapturedVar(VarDecl *VD, unsigned Level) {
887 assert(LangOpts.OpenMP && "OpenMP is not allowed");
888 // Return true if the current level is no longer enclosed in a target region.
889
890 return !VD->hasLocalStorage() &&
891 DSAStack->hasExplicitDirective(isOpenMPTargetDirective, Level);
892}
893
Alexey Bataeved09d242014-05-28 05:53:51 +0000894void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000895
896void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
897 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000898 Scope *CurScope, SourceLocation Loc) {
899 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000900 PushExpressionEvaluationContext(PotentiallyEvaluated);
901}
902
Alexey Bataevaac108a2015-06-23 04:51:00 +0000903void Sema::StartOpenMPClause(OpenMPClauseKind K) {
904 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000905}
906
Alexey Bataevaac108a2015-06-23 04:51:00 +0000907void Sema::EndOpenMPClause() {
908 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000909}
910
Alexey Bataev758e55e2013-09-06 18:03:48 +0000911void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000912 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
913 // A variable of class type (or array thereof) that appears in a lastprivate
914 // clause requires an accessible, unambiguous default constructor for the
915 // class type, unless the list item is also specified in a firstprivate
916 // clause.
917 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000918 for (auto *C : D->clauses()) {
919 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
920 SmallVector<Expr *, 8> PrivateCopies;
921 for (auto *DE : Clause->varlists()) {
922 if (DE->isValueDependent() || DE->isTypeDependent()) {
923 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000924 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +0000925 }
926 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(DE)->getDecl());
Alexey Bataevbd9fec12015-08-18 06:47:21 +0000927 QualType Type = VD->getType().getNonReferenceType();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000928 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000929 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000930 // Generate helper private variable and initialize it with the
931 // default value. The address of the original variable is replaced
932 // by the address of the new private variable in CodeGen. This new
933 // variable is not added to IdResolver, so the code in the OpenMP
934 // region uses original variable for proper diagnostics.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000935 auto *VDPrivate = buildVarDecl(
936 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
937 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +0000938 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
939 if (VDPrivate->isInvalidDecl())
940 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +0000941 PrivateCopies.push_back(buildDeclRefExpr(
942 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +0000943 } else {
944 // The variable is also a firstprivate, so initialization sequence
945 // for private copy is generated already.
946 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000947 }
948 }
Alexey Bataev38e89532015-04-16 04:54:05 +0000949 // Set initializers to private copies if no errors were found.
950 if (PrivateCopies.size() == Clause->varlist_size()) {
951 Clause->setPrivateCopies(PrivateCopies);
952 }
Alexey Bataevf29276e2014-06-18 04:14:57 +0000953 }
954 }
955 }
956
Alexey Bataev758e55e2013-09-06 18:03:48 +0000957 DSAStack->pop();
958 DiscardCleanupsInEvaluationContext();
959 PopExpressionEvaluationContext();
960}
961
Alexander Musman3276a272015-03-21 10:12:56 +0000962static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
963 Expr *NumIterations, Sema &SemaRef,
964 Scope *S);
965
Alexey Bataeva769e072013-03-22 06:34:35 +0000966namespace {
967
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000968class VarDeclFilterCCC : public CorrectionCandidateCallback {
969private:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000970 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +0000971
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000972public:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000973 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +0000974 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000975 NamedDecl *ND = Candidate.getCorrectionDecl();
976 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
977 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000978 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
979 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +0000980 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000981 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000982 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000983};
Alexey Bataeved09d242014-05-28 05:53:51 +0000984} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000985
986ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
987 CXXScopeSpec &ScopeSpec,
988 const DeclarationNameInfo &Id) {
989 LookupResult Lookup(*this, Id, LookupOrdinaryName);
990 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
991
992 if (Lookup.isAmbiguous())
993 return ExprError();
994
995 VarDecl *VD;
996 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +0000997 if (TypoCorrection Corrected = CorrectTypo(
998 Id, LookupOrdinaryName, CurScope, nullptr,
999 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +00001000 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001001 PDiag(Lookup.empty()
1002 ? diag::err_undeclared_var_use_suggest
1003 : diag::err_omp_expected_var_arg_suggest)
1004 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +00001005 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001006 } else {
Richard Smithf9b15102013-08-17 00:46:16 +00001007 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
1008 : diag::err_omp_expected_var_arg)
1009 << Id.getName();
1010 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001011 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001012 } else {
1013 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001014 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001015 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1016 return ExprError();
1017 }
1018 }
1019 Lookup.suppressDiagnostics();
1020
1021 // OpenMP [2.9.2, Syntax, C/C++]
1022 // Variables must be file-scope, namespace-scope, or static block-scope.
1023 if (!VD->hasGlobalStorage()) {
1024 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001025 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1026 bool IsDecl =
1027 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001028 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001029 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1030 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001031 return ExprError();
1032 }
1033
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001034 VarDecl *CanonicalVD = VD->getCanonicalDecl();
1035 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001036 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1037 // A threadprivate directive for file-scope variables must appear outside
1038 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001039 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1040 !getCurLexicalContext()->isTranslationUnit()) {
1041 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001042 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1043 bool IsDecl =
1044 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1045 Diag(VD->getLocation(),
1046 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1047 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001048 return ExprError();
1049 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001050 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1051 // A threadprivate directive for static class member variables must appear
1052 // in the class definition, in the same scope in which the member
1053 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001054 if (CanonicalVD->isStaticDataMember() &&
1055 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1056 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001057 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1058 bool IsDecl =
1059 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1060 Diag(VD->getLocation(),
1061 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1062 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001063 return ExprError();
1064 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001065 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1066 // A threadprivate directive for namespace-scope variables must appear
1067 // outside any definition or declaration other than the namespace
1068 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001069 if (CanonicalVD->getDeclContext()->isNamespace() &&
1070 (!getCurLexicalContext()->isFileContext() ||
1071 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
1072 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001073 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1074 bool IsDecl =
1075 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1076 Diag(VD->getLocation(),
1077 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1078 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001079 return ExprError();
1080 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001081 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
1082 // A threadprivate directive for static block-scope variables must appear
1083 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001084 if (CanonicalVD->isStaticLocal() && CurScope &&
1085 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001086 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001087 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1088 bool IsDecl =
1089 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1090 Diag(VD->getLocation(),
1091 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1092 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001093 return ExprError();
1094 }
1095
1096 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1097 // A threadprivate directive must lexically precede all references to any
1098 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001099 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001100 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +00001101 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001102 return ExprError();
1103 }
1104
1105 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00001106 ExprResult DE = buildDeclRefExpr(*this, VD, ExprType, Id.getLoc());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001107 return DE;
1108}
1109
Alexey Bataeved09d242014-05-28 05:53:51 +00001110Sema::DeclGroupPtrTy
1111Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1112 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001113 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001114 CurContext->addDecl(D);
1115 return DeclGroupPtrTy::make(DeclGroupRef(D));
1116 }
1117 return DeclGroupPtrTy();
1118}
1119
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001120namespace {
1121class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
1122 Sema &SemaRef;
1123
1124public:
1125 bool VisitDeclRefExpr(const DeclRefExpr *E) {
1126 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
1127 if (VD->hasLocalStorage()) {
1128 SemaRef.Diag(E->getLocStart(),
1129 diag::err_omp_local_var_in_threadprivate_init)
1130 << E->getSourceRange();
1131 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1132 << VD << VD->getSourceRange();
1133 return true;
1134 }
1135 }
1136 return false;
1137 }
1138 bool VisitStmt(const Stmt *S) {
1139 for (auto Child : S->children()) {
1140 if (Child && Visit(Child))
1141 return true;
1142 }
1143 return false;
1144 }
Alexey Bataev23b69422014-06-18 07:08:49 +00001145 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001146};
1147} // namespace
1148
Alexey Bataeved09d242014-05-28 05:53:51 +00001149OMPThreadPrivateDecl *
1150Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001151 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001152 for (auto &RefExpr : VarList) {
1153 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001154 VarDecl *VD = cast<VarDecl>(DE->getDecl());
1155 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +00001156
Alexey Bataevf56f98c2015-04-16 05:39:01 +00001157 QualType QType = VD->getType();
1158 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1159 // It will be analyzed later.
1160 Vars.push_back(DE);
1161 continue;
1162 }
1163
Alexey Bataeva769e072013-03-22 06:34:35 +00001164 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1165 // A threadprivate variable must not have an incomplete type.
1166 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001167 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001168 continue;
1169 }
1170
1171 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1172 // A threadprivate variable must not have a reference type.
1173 if (VD->getType()->isReferenceType()) {
1174 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001175 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
1176 bool IsDecl =
1177 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1178 Diag(VD->getLocation(),
1179 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1180 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001181 continue;
1182 }
1183
Samuel Antaof8b50122015-07-13 22:54:53 +00001184 // Check if this is a TLS variable. If TLS is not being supported, produce
1185 // the corresponding diagnostic.
1186 if ((VD->getTLSKind() != VarDecl::TLS_None &&
1187 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1188 getLangOpts().OpenMPUseTLS &&
1189 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +00001190 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
1191 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +00001192 Diag(ILoc, diag::err_omp_var_thread_local)
1193 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +00001194 bool IsDecl =
1195 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1196 Diag(VD->getLocation(),
1197 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1198 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001199 continue;
1200 }
1201
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001202 // Check if initial value of threadprivate variable reference variable with
1203 // local storage (it is not supported by runtime).
1204 if (auto Init = VD->getAnyInitializer()) {
1205 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001206 if (Checker.Visit(Init))
1207 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001208 }
1209
Alexey Bataeved09d242014-05-28 05:53:51 +00001210 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00001211 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00001212 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1213 Context, SourceRange(Loc, Loc)));
1214 if (auto *ML = Context.getASTMutationListener())
1215 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00001216 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001217 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001218 if (!Vars.empty()) {
1219 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1220 Vars);
1221 D->setAccess(AS_public);
1222 }
1223 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00001224}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001225
Alexey Bataev7ff55242014-06-19 09:13:45 +00001226static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
1227 const VarDecl *VD, DSAStackTy::DSAVarData DVar,
1228 bool IsLoopIterVar = false) {
1229 if (DVar.RefExpr) {
1230 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1231 << getOpenMPClauseName(DVar.CKind);
1232 return;
1233 }
1234 enum {
1235 PDSA_StaticMemberShared,
1236 PDSA_StaticLocalVarShared,
1237 PDSA_LoopIterVarPrivate,
1238 PDSA_LoopIterVarLinear,
1239 PDSA_LoopIterVarLastprivate,
1240 PDSA_ConstVarShared,
1241 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001242 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001243 PDSA_LocalVarPrivate,
1244 PDSA_Implicit
1245 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001246 bool ReportHint = false;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001247 auto ReportLoc = VD->getLocation();
Alexey Bataev7ff55242014-06-19 09:13:45 +00001248 if (IsLoopIterVar) {
1249 if (DVar.CKind == OMPC_private)
1250 Reason = PDSA_LoopIterVarPrivate;
1251 else if (DVar.CKind == OMPC_lastprivate)
1252 Reason = PDSA_LoopIterVarLastprivate;
1253 else
1254 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001255 } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) {
1256 Reason = PDSA_TaskVarFirstprivate;
1257 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001258 } else if (VD->isStaticLocal())
1259 Reason = PDSA_StaticLocalVarShared;
1260 else if (VD->isStaticDataMember())
1261 Reason = PDSA_StaticMemberShared;
1262 else if (VD->isFileVarDecl())
1263 Reason = PDSA_GlobalVarShared;
1264 else if (VD->getType().isConstant(SemaRef.getASTContext()))
1265 Reason = PDSA_ConstVarShared;
Alexey Bataevbae9a792014-06-27 10:37:06 +00001266 else if (VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00001267 ReportHint = true;
1268 Reason = PDSA_LocalVarPrivate;
1269 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00001270 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001271 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00001272 << Reason << ReportHint
1273 << getOpenMPDirectiveName(Stack->getCurrentDirective());
1274 } else if (DVar.ImplicitDSALoc.isValid()) {
1275 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1276 << getOpenMPClauseName(DVar.CKind);
1277 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00001278}
1279
Alexey Bataev758e55e2013-09-06 18:03:48 +00001280namespace {
1281class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1282 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001283 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001284 bool ErrorFound;
1285 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001286 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev4acb8592014-07-07 13:01:15 +00001287 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +00001288
Alexey Bataev758e55e2013-09-06 18:03:48 +00001289public:
1290 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001291 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001292 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +00001293 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1294 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001295
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001296 auto DVar = Stack->getTopDSA(VD, false);
1297 // Check if the variable has explicit DSA set and stop analysis if it so.
1298 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001299
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001300 auto ELoc = E->getExprLoc();
1301 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001302 // The default(none) clause requires that each variable that is referenced
1303 // in the construct, and does not have a predetermined data-sharing
1304 // attribute, must have its data-sharing attribute explicitly determined
1305 // by being listed in a data-sharing attribute clause.
1306 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001307 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001308 VarsWithInheritedDSA.count(VD) == 0) {
1309 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001310 return;
1311 }
1312
1313 // OpenMP [2.9.3.6, Restrictions, p.2]
1314 // A list item that appears in a reduction clause of the innermost
1315 // enclosing worksharing or parallel construct may not be accessed in an
1316 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +00001317 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001318 [](OpenMPDirectiveKind K) -> bool {
1319 return isOpenMPParallelDirective(K) ||
Alexey Bataev13314bf2014-10-09 04:18:56 +00001320 isOpenMPWorksharingDirective(K) ||
1321 isOpenMPTeamsDirective(K);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001322 },
1323 false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001324 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
1325 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001326 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1327 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001328 return;
1329 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001330
1331 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001332 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001333 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001334 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001335 }
1336 }
1337 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001338 for (auto *C : S->clauses()) {
1339 // Skip analysis of arguments of implicitly defined firstprivate clause
1340 // for task directives.
1341 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1342 for (auto *CC : C->children()) {
1343 if (CC)
1344 Visit(CC);
1345 }
1346 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001347 }
1348 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001349 for (auto *C : S->children()) {
1350 if (C && !isa<OMPExecutableDirective>(C))
1351 Visit(C);
1352 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001353 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001354
1355 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001356 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001357 llvm::DenseMap<VarDecl *, Expr *> &getVarsWithInheritedDSA() {
1358 return VarsWithInheritedDSA;
1359 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001360
Alexey Bataev7ff55242014-06-19 09:13:45 +00001361 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1362 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00001363};
Alexey Bataeved09d242014-05-28 05:53:51 +00001364} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001365
Alexey Bataevbae9a792014-06-27 10:37:06 +00001366void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001367 switch (DKind) {
1368 case OMPD_parallel: {
1369 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001370 QualType KmpInt32PtrTy =
1371 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001372 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001373 std::make_pair(".global_tid.", KmpInt32PtrTy),
1374 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1375 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001376 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001377 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1378 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001379 break;
1380 }
1381 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001382 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001383 std::make_pair(StringRef(), QualType()) // __context with shared vars
1384 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001385 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1386 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001387 break;
1388 }
1389 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001390 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001391 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001392 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001393 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1394 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001395 break;
1396 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001397 case OMPD_for_simd: {
1398 Sema::CapturedParamNameType Params[] = {
1399 std::make_pair(StringRef(), QualType()) // __context with shared vars
1400 };
1401 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1402 Params);
1403 break;
1404 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001405 case OMPD_sections: {
1406 Sema::CapturedParamNameType Params[] = {
1407 std::make_pair(StringRef(), QualType()) // __context with shared vars
1408 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001409 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1410 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001411 break;
1412 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001413 case OMPD_section: {
1414 Sema::CapturedParamNameType Params[] = {
1415 std::make_pair(StringRef(), QualType()) // __context with shared vars
1416 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001417 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1418 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001419 break;
1420 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001421 case OMPD_single: {
1422 Sema::CapturedParamNameType Params[] = {
1423 std::make_pair(StringRef(), QualType()) // __context with shared vars
1424 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001425 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1426 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001427 break;
1428 }
Alexander Musman80c22892014-07-17 08:54:58 +00001429 case OMPD_master: {
1430 Sema::CapturedParamNameType Params[] = {
1431 std::make_pair(StringRef(), QualType()) // __context with shared vars
1432 };
1433 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1434 Params);
1435 break;
1436 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001437 case OMPD_critical: {
1438 Sema::CapturedParamNameType Params[] = {
1439 std::make_pair(StringRef(), QualType()) // __context with shared vars
1440 };
1441 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1442 Params);
1443 break;
1444 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001445 case OMPD_parallel_for: {
1446 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001447 QualType KmpInt32PtrTy =
1448 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev4acb8592014-07-07 13:01:15 +00001449 Sema::CapturedParamNameType Params[] = {
1450 std::make_pair(".global_tid.", KmpInt32PtrTy),
1451 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1452 std::make_pair(StringRef(), QualType()) // __context with shared vars
1453 };
1454 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1455 Params);
1456 break;
1457 }
Alexander Musmane4e893b2014-09-23 09:33:00 +00001458 case OMPD_parallel_for_simd: {
1459 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001460 QualType KmpInt32PtrTy =
1461 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexander Musmane4e893b2014-09-23 09:33:00 +00001462 Sema::CapturedParamNameType Params[] = {
1463 std::make_pair(".global_tid.", KmpInt32PtrTy),
1464 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1465 std::make_pair(StringRef(), QualType()) // __context with shared vars
1466 };
1467 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1468 Params);
1469 break;
1470 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001471 case OMPD_parallel_sections: {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001472 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001473 QualType KmpInt32PtrTy =
1474 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001475 Sema::CapturedParamNameType Params[] = {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001476 std::make_pair(".global_tid.", KmpInt32PtrTy),
1477 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001478 std::make_pair(StringRef(), QualType()) // __context with shared vars
1479 };
1480 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1481 Params);
1482 break;
1483 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001484 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001485 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001486 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1487 FunctionProtoType::ExtProtoInfo EPI;
1488 EPI.Variadic = true;
1489 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001490 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001491 std::make_pair(".global_tid.", KmpInt32Ty),
1492 std::make_pair(".part_id.", KmpInt32Ty),
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001493 std::make_pair(".privates.",
1494 Context.VoidPtrTy.withConst().withRestrict()),
1495 std::make_pair(
1496 ".copy_fn.",
1497 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001498 std::make_pair(StringRef(), QualType()) // __context with shared vars
1499 };
1500 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1501 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001502 // Mark this captured region as inlined, because we don't use outlined
1503 // function directly.
1504 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1505 AlwaysInlineAttr::CreateImplicit(
1506 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001507 break;
1508 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001509 case OMPD_ordered: {
1510 Sema::CapturedParamNameType Params[] = {
1511 std::make_pair(StringRef(), QualType()) // __context with shared vars
1512 };
1513 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1514 Params);
1515 break;
1516 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001517 case OMPD_atomic: {
1518 Sema::CapturedParamNameType Params[] = {
1519 std::make_pair(StringRef(), QualType()) // __context with shared vars
1520 };
1521 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1522 Params);
1523 break;
1524 }
Michael Wong65f367f2015-07-21 13:44:28 +00001525 case OMPD_target_data:
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001526 case OMPD_target: {
1527 Sema::CapturedParamNameType Params[] = {
1528 std::make_pair(StringRef(), QualType()) // __context with shared vars
1529 };
1530 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1531 Params);
1532 break;
1533 }
Alexey Bataev13314bf2014-10-09 04:18:56 +00001534 case OMPD_teams: {
1535 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001536 QualType KmpInt32PtrTy =
1537 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev13314bf2014-10-09 04:18:56 +00001538 Sema::CapturedParamNameType Params[] = {
1539 std::make_pair(".global_tid.", KmpInt32PtrTy),
1540 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1541 std::make_pair(StringRef(), QualType()) // __context with shared vars
1542 };
1543 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1544 Params);
1545 break;
1546 }
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001547 case OMPD_taskgroup: {
1548 Sema::CapturedParamNameType Params[] = {
1549 std::make_pair(StringRef(), QualType()) // __context with shared vars
1550 };
1551 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1552 Params);
1553 break;
1554 }
Alexey Bataev49f6e782015-12-01 04:18:41 +00001555 case OMPD_taskloop: {
1556 Sema::CapturedParamNameType Params[] = {
1557 std::make_pair(StringRef(), QualType()) // __context with shared vars
1558 };
1559 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1560 Params);
1561 break;
1562 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001563 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001564 case OMPD_taskyield:
1565 case OMPD_barrier:
1566 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001567 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001568 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00001569 case OMPD_flush:
Alexey Bataev9959db52014-05-06 10:08:46 +00001570 llvm_unreachable("OpenMP Directive is not allowed");
1571 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001572 llvm_unreachable("Unknown OpenMP directive");
1573 }
1574}
1575
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001576StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1577 ArrayRef<OMPClause *> Clauses) {
1578 if (!S.isUsable()) {
1579 ActOnCapturedRegionError();
1580 return StmtError();
1581 }
Alexey Bataev040d5402015-05-12 08:35:28 +00001582 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001583 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001584 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001585 Clause->getClauseKind() == OMPC_copyprivate ||
1586 (getLangOpts().OpenMPUseTLS &&
1587 getASTContext().getTargetInfo().isTLSSupported() &&
1588 Clause->getClauseKind() == OMPC_copyin)) {
1589 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001590 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001591 for (auto *VarRef : Clause->children()) {
1592 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001593 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001594 }
1595 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001596 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev040d5402015-05-12 08:35:28 +00001597 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
1598 Clause->getClauseKind() == OMPC_schedule) {
1599 // Mark all variables in private list clauses as used in inner region.
1600 // Required for proper codegen of combined directives.
1601 // TODO: add processing for other clauses.
1602 if (auto *E = cast_or_null<Expr>(
1603 cast<OMPScheduleClause>(Clause)->getHelperChunkSize())) {
1604 MarkDeclarationsReferencedInExpr(E);
1605 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001606 }
1607 }
1608 return ActOnCapturedRegionEnd(S.get());
1609}
1610
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001611static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1612 OpenMPDirectiveKind CurrentRegion,
1613 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001614 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001615 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001616 // Allowed nesting of constructs
1617 // +------------------+-----------------+------------------------------------+
1618 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1619 // +------------------+-----------------+------------------------------------+
1620 // | parallel | parallel | * |
1621 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001622 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001623 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001624 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001625 // | parallel | simd | * |
1626 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001627 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001628 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001629 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001630 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001631 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001632 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001633 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001634 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001635 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001636 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001637 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001638 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001639 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001640 // | parallel | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001641 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001642 // | parallel | cancellation | |
1643 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001644 // | parallel | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001645 // | parallel | taskloop | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001646 // +------------------+-----------------+------------------------------------+
1647 // | for | parallel | * |
1648 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001649 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001650 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001651 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001652 // | for | simd | * |
1653 // | for | sections | + |
1654 // | for | section | + |
1655 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001656 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001657 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001658 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001659 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001660 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001661 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001662 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001663 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001664 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001665 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001666 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001667 // | for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001668 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001669 // | for | cancellation | |
1670 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001671 // | for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001672 // | for | taskloop | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001673 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001674 // | master | parallel | * |
1675 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001676 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001677 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001678 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001679 // | master | simd | * |
1680 // | master | sections | + |
1681 // | master | section | + |
1682 // | master | single | + |
1683 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001684 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001685 // | master |parallel sections| * |
1686 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001687 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001688 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001689 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001690 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001691 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001692 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001693 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001694 // | master | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001695 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001696 // | master | cancellation | |
1697 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001698 // | master | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001699 // | master | taskloop | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001700 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001701 // | critical | parallel | * |
1702 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001703 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001704 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001705 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001706 // | critical | simd | * |
1707 // | critical | sections | + |
1708 // | critical | section | + |
1709 // | critical | single | + |
1710 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001711 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001712 // | critical |parallel sections| * |
1713 // | critical | task | * |
1714 // | critical | taskyield | * |
1715 // | critical | barrier | + |
1716 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001717 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001718 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001719 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001720 // | critical | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001721 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001722 // | critical | cancellation | |
1723 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001724 // | critical | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001725 // | critical | taskloop | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001726 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001727 // | simd | parallel | |
1728 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001729 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00001730 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001731 // | simd | critical | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001732 // | simd | simd | |
1733 // | simd | sections | |
1734 // | simd | section | |
1735 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001736 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001737 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001738 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001739 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00001740 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001741 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001742 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001743 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00001744 // | simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001745 // | simd | ordered | + (with simd clause) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001746 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001747 // | simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001748 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001749 // | simd | cancellation | |
1750 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001751 // | simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001752 // | simd | taskloop | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001753 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00001754 // | for simd | parallel | |
1755 // | for simd | for | |
1756 // | for simd | for simd | |
1757 // | for simd | master | |
1758 // | for simd | critical | |
1759 // | for simd | simd | |
1760 // | for simd | sections | |
1761 // | for simd | section | |
1762 // | for simd | single | |
1763 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001764 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001765 // | for simd |parallel sections| |
1766 // | for simd | task | |
1767 // | for simd | taskyield | |
1768 // | for simd | barrier | |
1769 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001770 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001771 // | for simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001772 // | for simd | ordered | + (with simd clause) |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001773 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001774 // | for simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001775 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001776 // | for simd | cancellation | |
1777 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001778 // | for simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001779 // | for simd | taskloop | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001780 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00001781 // | parallel for simd| parallel | |
1782 // | parallel for simd| for | |
1783 // | parallel for simd| for simd | |
1784 // | parallel for simd| master | |
1785 // | parallel for simd| critical | |
1786 // | parallel for simd| simd | |
1787 // | parallel for simd| sections | |
1788 // | parallel for simd| section | |
1789 // | parallel for simd| single | |
1790 // | parallel for simd| parallel for | |
1791 // | parallel for simd|parallel for simd| |
1792 // | parallel for simd|parallel sections| |
1793 // | parallel for simd| task | |
1794 // | parallel for simd| taskyield | |
1795 // | parallel for simd| barrier | |
1796 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001797 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001798 // | parallel for simd| flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001799 // | parallel for simd| ordered | + (with simd clause) |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001800 // | parallel for simd| atomic | |
1801 // | parallel for simd| target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001802 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001803 // | parallel for simd| cancellation | |
1804 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001805 // | parallel for simd| cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001806 // | parallel for simd| taskloop | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001807 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001808 // | sections | parallel | * |
1809 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001810 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001811 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001812 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001813 // | sections | simd | * |
1814 // | sections | sections | + |
1815 // | sections | section | * |
1816 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001817 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001818 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001819 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001820 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001821 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001822 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001823 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001824 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001825 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001826 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001827 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001828 // | sections | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001829 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001830 // | sections | cancellation | |
1831 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001832 // | sections | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001833 // | sections | taskloop | * |
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 Bataev18eb25e2014-06-30 10:22:46 +00001861 // +------------------+-----------------+------------------------------------+
1862 // | single | parallel | * |
1863 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001864 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001865 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001866 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001867 // | single | simd | * |
1868 // | single | sections | + |
1869 // | single | section | + |
1870 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001871 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001872 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001873 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001874 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001875 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001876 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001877 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001878 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001879 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001880 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001881 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001882 // | single | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001883 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001884 // | single | cancellation | |
1885 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001886 // | single | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001887 // | single | taskloop | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001888 // +------------------+-----------------+------------------------------------+
1889 // | parallel for | parallel | * |
1890 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001891 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001892 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001893 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001894 // | parallel for | simd | * |
1895 // | parallel for | sections | + |
1896 // | parallel for | section | + |
1897 // | parallel for | single | + |
1898 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001899 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001900 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001901 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001902 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001903 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001904 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001905 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001906 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001907 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001908 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001909 // | parallel for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001910 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001911 // | parallel for | cancellation | |
1912 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001913 // | parallel for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001914 // | parallel for | taskloop | * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001915 // +------------------+-----------------+------------------------------------+
1916 // | parallel sections| parallel | * |
1917 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001918 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001919 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001920 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001921 // | parallel sections| simd | * |
1922 // | parallel sections| sections | + |
1923 // | parallel sections| section | * |
1924 // | parallel sections| single | + |
1925 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001926 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001927 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001928 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001929 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001930 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001931 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001932 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001933 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001934 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001935 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001936 // | parallel sections| target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001937 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001938 // | parallel sections| cancellation | |
1939 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001940 // | parallel sections| cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001941 // | parallel sections| taskloop | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001942 // +------------------+-----------------+------------------------------------+
1943 // | task | parallel | * |
1944 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001945 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001946 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001947 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001948 // | task | simd | * |
1949 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001950 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001951 // | task | single | + |
1952 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001953 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001954 // | task |parallel sections| * |
1955 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001956 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001957 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001958 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001959 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001960 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001961 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001962 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001963 // | task | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001964 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001965 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00001966 // | | point | ! |
1967 // | task | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001968 // | task | taskloop | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001969 // +------------------+-----------------+------------------------------------+
1970 // | ordered | parallel | * |
1971 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001972 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001973 // | ordered | master | * |
1974 // | ordered | critical | * |
1975 // | ordered | simd | * |
1976 // | ordered | sections | + |
1977 // | ordered | section | + |
1978 // | ordered | single | + |
1979 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001980 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001981 // | ordered |parallel sections| * |
1982 // | ordered | task | * |
1983 // | ordered | taskyield | * |
1984 // | ordered | barrier | + |
1985 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001986 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001987 // | ordered | flush | * |
1988 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001989 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001990 // | ordered | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001991 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001992 // | ordered | cancellation | |
1993 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001994 // | ordered | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001995 // | ordered | taskloop | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001996 // +------------------+-----------------+------------------------------------+
1997 // | atomic | parallel | |
1998 // | atomic | for | |
1999 // | atomic | for simd | |
2000 // | atomic | master | |
2001 // | atomic | critical | |
2002 // | atomic | simd | |
2003 // | atomic | sections | |
2004 // | atomic | section | |
2005 // | atomic | single | |
2006 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002007 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002008 // | atomic |parallel sections| |
2009 // | atomic | task | |
2010 // | atomic | taskyield | |
2011 // | atomic | barrier | |
2012 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002013 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002014 // | atomic | flush | |
2015 // | atomic | ordered | |
2016 // | atomic | atomic | |
2017 // | atomic | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002018 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002019 // | atomic | cancellation | |
2020 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002021 // | atomic | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002022 // | atomic | taskloop | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002023 // +------------------+-----------------+------------------------------------+
2024 // | target | parallel | * |
2025 // | target | for | * |
2026 // | target | for simd | * |
2027 // | target | master | * |
2028 // | target | critical | * |
2029 // | target | simd | * |
2030 // | target | sections | * |
2031 // | target | section | * |
2032 // | target | single | * |
2033 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002034 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002035 // | target |parallel sections| * |
2036 // | target | task | * |
2037 // | target | taskyield | * |
2038 // | target | barrier | * |
2039 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002040 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002041 // | target | flush | * |
2042 // | target | ordered | * |
2043 // | target | atomic | * |
2044 // | target | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002045 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002046 // | target | cancellation | |
2047 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002048 // | target | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002049 // | target | taskloop | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002050 // +------------------+-----------------+------------------------------------+
2051 // | teams | parallel | * |
2052 // | teams | for | + |
2053 // | teams | for simd | + |
2054 // | teams | master | + |
2055 // | teams | critical | + |
2056 // | teams | simd | + |
2057 // | teams | sections | + |
2058 // | teams | section | + |
2059 // | teams | single | + |
2060 // | teams | parallel for | * |
2061 // | teams |parallel for simd| * |
2062 // | teams |parallel sections| * |
2063 // | teams | task | + |
2064 // | teams | taskyield | + |
2065 // | teams | barrier | + |
2066 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00002067 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002068 // | teams | flush | + |
2069 // | teams | ordered | + |
2070 // | teams | atomic | + |
2071 // | teams | target | + |
2072 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002073 // | teams | cancellation | |
2074 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002075 // | teams | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002076 // | teams | taskloop | + |
2077 // +------------------+-----------------+------------------------------------+
2078 // | taskloop | parallel | * |
2079 // | taskloop | for | + |
2080 // | taskloop | for simd | + |
2081 // | taskloop | master | + |
2082 // | taskloop | critical | * |
2083 // | taskloop | simd | * |
2084 // | taskloop | sections | + |
2085 // | taskloop | section | + |
2086 // | taskloop | single | + |
2087 // | taskloop | parallel for | * |
2088 // | taskloop |parallel for simd| * |
2089 // | taskloop |parallel sections| * |
2090 // | taskloop | task | * |
2091 // | taskloop | taskyield | * |
2092 // | taskloop | barrier | + |
2093 // | taskloop | taskwait | * |
2094 // | taskloop | taskgroup | * |
2095 // | taskloop | flush | * |
2096 // | taskloop | ordered | + |
2097 // | taskloop | atomic | * |
2098 // | taskloop | target | * |
2099 // | taskloop | teams | + |
2100 // | taskloop | cancellation | |
2101 // | | point | |
2102 // | taskloop | cancel | |
2103 // | taskloop | taskloop | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002104 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00002105 if (Stack->getCurScope()) {
2106 auto ParentRegion = Stack->getParentDirective();
2107 bool NestingProhibited = false;
2108 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002109 enum {
2110 NoRecommend,
2111 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00002112 ShouldBeInOrderedRegion,
2113 ShouldBeInTargetRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002114 } Recommend = NoRecommend;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002115 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002116 // OpenMP [2.16, Nesting of Regions]
2117 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002118 // OpenMP [2.8.1,simd Construct, Restrictions]
2119 // An ordered construct with the simd clause is the only OpenMP construct
2120 // that can appear in the simd region.
Alexey Bataev549210e2014-06-24 04:39:47 +00002121 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
2122 return true;
2123 }
Alexey Bataev0162e452014-07-22 10:10:35 +00002124 if (ParentRegion == OMPD_atomic) {
2125 // OpenMP [2.16, Nesting of Regions]
2126 // OpenMP constructs may not be nested inside an atomic region.
2127 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
2128 return true;
2129 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002130 if (CurrentRegion == OMPD_section) {
2131 // OpenMP [2.7.2, sections Construct, Restrictions]
2132 // Orphaned section directives are prohibited. That is, the section
2133 // directives must appear within the sections construct and must not be
2134 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002135 if (ParentRegion != OMPD_sections &&
2136 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002137 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
2138 << (ParentRegion != OMPD_unknown)
2139 << getOpenMPDirectiveName(ParentRegion);
2140 return true;
2141 }
2142 return false;
2143 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002144 // Allow some constructs to be orphaned (they could be used in functions,
2145 // called from OpenMP regions with the required preconditions).
2146 if (ParentRegion == OMPD_unknown)
2147 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00002148 if (CurrentRegion == OMPD_cancellation_point ||
2149 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002150 // OpenMP [2.16, Nesting of Regions]
2151 // A cancellation point construct for which construct-type-clause is
2152 // taskgroup must be nested inside a task construct. A cancellation
2153 // point construct for which construct-type-clause is not taskgroup must
2154 // be closely nested inside an OpenMP construct that matches the type
2155 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00002156 // A cancel construct for which construct-type-clause is taskgroup must be
2157 // nested inside a task construct. A cancel construct for which
2158 // construct-type-clause is not taskgroup must be closely nested inside an
2159 // OpenMP construct that matches the type specified in
2160 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002161 NestingProhibited =
2162 !((CancelRegion == OMPD_parallel && ParentRegion == OMPD_parallel) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00002163 (CancelRegion == OMPD_for &&
2164 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002165 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
2166 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00002167 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
2168 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002169 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00002170 // OpenMP [2.16, Nesting of Regions]
2171 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002172 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00002173 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002174 ParentRegion == OMPD_task ||
2175 ParentRegion == OMPD_taskloop;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002176 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
2177 // OpenMP [2.16, Nesting of Regions]
2178 // A critical region may not be nested (closely or otherwise) inside a
2179 // critical region with the same name. Note that this restriction is not
2180 // sufficient to prevent deadlock.
2181 SourceLocation PreviousCriticalLoc;
2182 bool DeadLock =
2183 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
2184 OpenMPDirectiveKind K,
2185 const DeclarationNameInfo &DNI,
2186 SourceLocation Loc)
2187 ->bool {
2188 if (K == OMPD_critical &&
2189 DNI.getName() == CurrentName.getName()) {
2190 PreviousCriticalLoc = Loc;
2191 return true;
2192 } else
2193 return false;
2194 },
2195 false /* skip top directive */);
2196 if (DeadLock) {
2197 SemaRef.Diag(StartLoc,
2198 diag::err_omp_prohibited_region_critical_same_name)
2199 << CurrentName.getName();
2200 if (PreviousCriticalLoc.isValid())
2201 SemaRef.Diag(PreviousCriticalLoc,
2202 diag::note_omp_previous_critical_region);
2203 return true;
2204 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002205 } else if (CurrentRegion == OMPD_barrier) {
2206 // OpenMP [2.16, Nesting of Regions]
2207 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002208 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002209 NestingProhibited =
2210 isOpenMPWorksharingDirective(ParentRegion) ||
2211 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002212 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
2213 ParentRegion == OMPD_taskloop;
Alexander Musman80c22892014-07-17 08:54:58 +00002214 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00002215 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002216 // OpenMP [2.16, Nesting of Regions]
2217 // A worksharing region may not be closely nested inside a worksharing,
2218 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002219 NestingProhibited =
Alexander Musmanf82886e2014-09-18 05:12:34 +00002220 isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002221 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002222 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
2223 ParentRegion == OMPD_taskloop;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002224 Recommend = ShouldBeInParallelRegion;
2225 } else if (CurrentRegion == OMPD_ordered) {
2226 // OpenMP [2.16, Nesting of Regions]
2227 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00002228 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002229 // An ordered region must be closely nested inside a loop region (or
2230 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002231 // OpenMP [2.8.1,simd Construct, Restrictions]
2232 // An ordered construct with the simd clause is the only OpenMP construct
2233 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002234 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00002235 ParentRegion == OMPD_task ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002236 ParentRegion == OMPD_taskloop ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002237 !(isOpenMPSimdDirective(ParentRegion) ||
2238 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002239 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002240 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
2241 // OpenMP [2.16, Nesting of Regions]
2242 // If specified, a teams construct must be contained within a target
2243 // construct.
2244 NestingProhibited = ParentRegion != OMPD_target;
2245 Recommend = ShouldBeInTargetRegion;
2246 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
2247 }
2248 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
2249 // OpenMP [2.16, Nesting of Regions]
2250 // distribute, parallel, parallel sections, parallel workshare, and the
2251 // parallel loop and parallel loop SIMD constructs are the only OpenMP
2252 // constructs that can be closely nested in the teams region.
2253 // TODO: add distribute directive.
2254 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion);
2255 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002256 }
2257 if (NestingProhibited) {
2258 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002259 << CloseNesting << getOpenMPDirectiveName(ParentRegion) << Recommend
2260 << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00002261 return true;
2262 }
2263 }
2264 return false;
2265}
2266
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002267static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
2268 ArrayRef<OMPClause *> Clauses,
2269 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
2270 bool ErrorFound = false;
2271 unsigned NamedModifiersNumber = 0;
2272 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
2273 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00002274 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002275 for (const auto *C : Clauses) {
2276 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
2277 // At most one if clause without a directive-name-modifier can appear on
2278 // the directive.
2279 OpenMPDirectiveKind CurNM = IC->getNameModifier();
2280 if (FoundNameModifiers[CurNM]) {
2281 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
2282 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
2283 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
2284 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002285 } else if (CurNM != OMPD_unknown) {
2286 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002287 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002288 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002289 FoundNameModifiers[CurNM] = IC;
2290 if (CurNM == OMPD_unknown)
2291 continue;
2292 // Check if the specified name modifier is allowed for the current
2293 // directive.
2294 // At most one if clause with the particular directive-name-modifier can
2295 // appear on the directive.
2296 bool MatchFound = false;
2297 for (auto NM : AllowedNameModifiers) {
2298 if (CurNM == NM) {
2299 MatchFound = true;
2300 break;
2301 }
2302 }
2303 if (!MatchFound) {
2304 S.Diag(IC->getNameModifierLoc(),
2305 diag::err_omp_wrong_if_directive_name_modifier)
2306 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
2307 ErrorFound = true;
2308 }
2309 }
2310 }
2311 // If any if clause on the directive includes a directive-name-modifier then
2312 // all if clauses on the directive must include a directive-name-modifier.
2313 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
2314 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
2315 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
2316 diag::err_omp_no_more_if_clause);
2317 } else {
2318 std::string Values;
2319 std::string Sep(", ");
2320 unsigned AllowedCnt = 0;
2321 unsigned TotalAllowedNum =
2322 AllowedNameModifiers.size() - NamedModifiersNumber;
2323 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
2324 ++Cnt) {
2325 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
2326 if (!FoundNameModifiers[NM]) {
2327 Values += "'";
2328 Values += getOpenMPDirectiveName(NM);
2329 Values += "'";
2330 if (AllowedCnt + 2 == TotalAllowedNum)
2331 Values += " or ";
2332 else if (AllowedCnt + 1 != TotalAllowedNum)
2333 Values += Sep;
2334 ++AllowedCnt;
2335 }
2336 }
2337 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
2338 diag::err_omp_unnamed_if_clause)
2339 << (TotalAllowedNum > 1) << Values;
2340 }
Alexey Bataevecb156a2015-09-15 17:23:56 +00002341 for (auto Loc : NameModifierLoc) {
2342 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
2343 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002344 ErrorFound = true;
2345 }
2346 return ErrorFound;
2347}
2348
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002349StmtResult Sema::ActOnOpenMPExecutableDirective(
2350 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
2351 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
2352 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002353 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002354 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
2355 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00002356 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002357
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002358 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev68446b72014-07-18 07:47:19 +00002359 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002360 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00002361 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00002362 if (AStmt) {
2363 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
2364
2365 // Check default data sharing attributes for referenced variables.
2366 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
2367 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
2368 if (DSAChecker.isErrorFound())
2369 return StmtError();
2370 // Generate list of implicitly defined firstprivate variables.
2371 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00002372
2373 if (!DSAChecker.getImplicitFirstprivate().empty()) {
2374 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
2375 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
2376 SourceLocation(), SourceLocation())) {
2377 ClausesWithImplicit.push_back(Implicit);
2378 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
2379 DSAChecker.getImplicitFirstprivate().size();
2380 } else
2381 ErrorFound = true;
2382 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002383 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002384
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002385 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002386 switch (Kind) {
2387 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00002388 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
2389 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002390 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002391 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002392 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002393 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2394 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002395 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002396 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002397 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2398 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002399 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00002400 case OMPD_for_simd:
2401 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2402 EndLoc, VarsWithInheritedDSA);
2403 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002404 case OMPD_sections:
2405 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
2406 EndLoc);
2407 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002408 case OMPD_section:
2409 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00002410 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002411 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
2412 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002413 case OMPD_single:
2414 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
2415 EndLoc);
2416 break;
Alexander Musman80c22892014-07-17 08:54:58 +00002417 case OMPD_master:
2418 assert(ClausesWithImplicit.empty() &&
2419 "No clauses are allowed for 'omp master' directive");
2420 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
2421 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002422 case OMPD_critical:
2423 assert(ClausesWithImplicit.empty() &&
2424 "No clauses are allowed for 'omp critical' directive");
2425 Res = ActOnOpenMPCriticalDirective(DirName, AStmt, StartLoc, EndLoc);
2426 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002427 case OMPD_parallel_for:
2428 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
2429 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002430 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00002431 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00002432 case OMPD_parallel_for_simd:
2433 Res = ActOnOpenMPParallelForSimdDirective(
2434 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002435 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00002436 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002437 case OMPD_parallel_sections:
2438 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
2439 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002440 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002441 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002442 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002443 Res =
2444 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002445 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002446 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00002447 case OMPD_taskyield:
2448 assert(ClausesWithImplicit.empty() &&
2449 "No clauses are allowed for 'omp taskyield' directive");
2450 assert(AStmt == nullptr &&
2451 "No associated statement allowed for 'omp taskyield' directive");
2452 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
2453 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002454 case OMPD_barrier:
2455 assert(ClausesWithImplicit.empty() &&
2456 "No clauses are allowed for 'omp barrier' directive");
2457 assert(AStmt == nullptr &&
2458 "No associated statement allowed for 'omp barrier' directive");
2459 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
2460 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00002461 case OMPD_taskwait:
2462 assert(ClausesWithImplicit.empty() &&
2463 "No clauses are allowed for 'omp taskwait' directive");
2464 assert(AStmt == nullptr &&
2465 "No associated statement allowed for 'omp taskwait' directive");
2466 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
2467 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002468 case OMPD_taskgroup:
2469 assert(ClausesWithImplicit.empty() &&
2470 "No clauses are allowed for 'omp taskgroup' directive");
2471 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
2472 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00002473 case OMPD_flush:
2474 assert(AStmt == nullptr &&
2475 "No associated statement allowed for 'omp flush' directive");
2476 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
2477 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002478 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00002479 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
2480 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002481 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00002482 case OMPD_atomic:
2483 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
2484 EndLoc);
2485 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002486 case OMPD_teams:
2487 Res =
2488 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
2489 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002490 case OMPD_target:
2491 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
2492 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002493 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002494 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002495 case OMPD_cancellation_point:
2496 assert(ClausesWithImplicit.empty() &&
2497 "No clauses are allowed for 'omp cancellation point' directive");
2498 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
2499 "cancellation point' directive");
2500 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
2501 break;
Alexey Bataev80909872015-07-02 11:25:17 +00002502 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00002503 assert(AStmt == nullptr &&
2504 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00002505 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
2506 CancelRegion);
2507 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00002508 break;
Michael Wong65f367f2015-07-21 13:44:28 +00002509 case OMPD_target_data:
2510 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
2511 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002512 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00002513 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00002514 case OMPD_taskloop:
2515 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
2516 EndLoc, VarsWithInheritedDSA);
2517 AllowedNameModifiers.push_back(OMPD_taskloop);
2518 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002519 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002520 llvm_unreachable("OpenMP Directive is not allowed");
2521 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002522 llvm_unreachable("Unknown OpenMP directive");
2523 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002524
Alexey Bataev4acb8592014-07-07 13:01:15 +00002525 for (auto P : VarsWithInheritedDSA) {
2526 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
2527 << P.first << P.second->getSourceRange();
2528 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002529 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
2530
2531 if (!AllowedNameModifiers.empty())
2532 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
2533 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002534
Alexey Bataeved09d242014-05-28 05:53:51 +00002535 if (ErrorFound)
2536 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002537 return Res;
2538}
2539
2540StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
2541 Stmt *AStmt,
2542 SourceLocation StartLoc,
2543 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002544 if (!AStmt)
2545 return StmtError();
2546
Alexey Bataev9959db52014-05-06 10:08:46 +00002547 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
2548 // 1.2.2 OpenMP Language Terminology
2549 // Structured block - An executable statement with a single entry at the
2550 // top and a single exit at the bottom.
2551 // The point of exit cannot be a branch out of the structured block.
2552 // longjmp() and throw() must not violate the entry/exit criteria.
2553 CS->getCapturedDecl()->setNothrow();
2554
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002555 getCurFunction()->setHasBranchProtectedScope();
2556
Alexey Bataev25e5b442015-09-15 12:52:43 +00002557 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
2558 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002559}
2560
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002561namespace {
2562/// \brief Helper class for checking canonical form of the OpenMP loops and
2563/// extracting iteration space of each loop in the loop nest, that will be used
2564/// for IR generation.
2565class OpenMPIterationSpaceChecker {
2566 /// \brief Reference to Sema.
2567 Sema &SemaRef;
2568 /// \brief A location for diagnostics (when there is no some better location).
2569 SourceLocation DefaultLoc;
2570 /// \brief A location for diagnostics (when increment is not compatible).
2571 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002572 /// \brief A source location for referring to loop init later.
2573 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002574 /// \brief A source location for referring to condition later.
2575 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002576 /// \brief A source location for referring to increment later.
2577 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002578 /// \brief Loop variable.
2579 VarDecl *Var;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002580 /// \brief Reference to loop variable.
2581 DeclRefExpr *VarRef;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002582 /// \brief Lower bound (initializer for the var).
2583 Expr *LB;
2584 /// \brief Upper bound.
2585 Expr *UB;
2586 /// \brief Loop step (increment).
2587 Expr *Step;
2588 /// \brief This flag is true when condition is one of:
2589 /// Var < UB
2590 /// Var <= UB
2591 /// UB > Var
2592 /// UB >= Var
2593 bool TestIsLessOp;
2594 /// \brief This flag is true when condition is strict ( < or > ).
2595 bool TestIsStrictOp;
2596 /// \brief This flag is true when step is subtracted on each iteration.
2597 bool SubtractStep;
2598
2599public:
2600 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
2601 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
Alexander Musmana5f070a2014-10-01 06:03:56 +00002602 InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()),
2603 IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002604 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
2605 TestIsStrictOp(false), SubtractStep(false) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002606 /// \brief Check init-expr for canonical loop form and save loop counter
2607 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00002608 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002609 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
2610 /// for less/greater and for strict/non-strict comparison.
2611 bool CheckCond(Expr *S);
2612 /// \brief Check incr-expr for canonical loop form and return true if it
2613 /// does not conform, otherwise save loop step (#Step).
2614 bool CheckInc(Expr *S);
2615 /// \brief Return the loop counter variable.
2616 VarDecl *GetLoopVar() const { return Var; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002617 /// \brief Return the reference expression to loop counter variable.
2618 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002619 /// \brief Source range of the loop init.
2620 SourceRange GetInitSrcRange() const { return InitSrcRange; }
2621 /// \brief Source range of the loop condition.
2622 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
2623 /// \brief Source range of the loop increment.
2624 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
2625 /// \brief True if the step should be subtracted.
2626 bool ShouldSubtractStep() const { return SubtractStep; }
2627 /// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00002628 Expr *BuildNumIterations(Scope *S, const bool LimitedType) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00002629 /// \brief Build the precondition expression for the loops.
2630 Expr *BuildPreCond(Scope *S, Expr *Cond) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002631 /// \brief Build reference expression to the counter be used for codegen.
2632 Expr *BuildCounterVar() const;
Alexey Bataeva8899172015-08-06 12:30:57 +00002633 /// \brief Build reference expression to the private counter be used for
2634 /// codegen.
2635 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002636 /// \brief Build initization of the counter be used for codegen.
2637 Expr *BuildCounterInit() const;
2638 /// \brief Build step of the counter be used for codegen.
2639 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002640 /// \brief Return true if any expression is dependent.
2641 bool Dependent() const;
2642
2643private:
2644 /// \brief Check the right-hand side of an assignment in the increment
2645 /// expression.
2646 bool CheckIncRHS(Expr *RHS);
2647 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002648 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002649 /// \brief Helper to set upper bound.
Craig Toppere335f252015-10-04 04:53:55 +00002650 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00002651 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002652 /// \brief Helper to set loop increment.
2653 bool SetStep(Expr *NewStep, bool Subtract);
2654};
2655
2656bool OpenMPIterationSpaceChecker::Dependent() const {
2657 if (!Var) {
2658 assert(!LB && !UB && !Step);
2659 return false;
2660 }
2661 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
2662 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
2663}
2664
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002665template <typename T>
2666static T *getExprAsWritten(T *E) {
2667 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
2668 E = ExprTemp->getSubExpr();
2669
2670 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
2671 E = MTE->GetTemporaryExpr();
2672
2673 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
2674 E = Binder->getSubExpr();
2675
2676 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
2677 E = ICE->getSubExprAsWritten();
2678 return E->IgnoreParens();
2679}
2680
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002681bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
2682 DeclRefExpr *NewVarRefExpr,
2683 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002684 // State consistency checking to ensure correct usage.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002685 assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
2686 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002687 if (!NewVar || !NewLB)
2688 return true;
2689 Var = NewVar;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002690 VarRef = NewVarRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002691 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
2692 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00002693 if ((Ctor->isCopyOrMoveConstructor() ||
2694 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
2695 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002696 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002697 LB = NewLB;
2698 return false;
2699}
2700
2701bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00002702 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002703 // State consistency checking to ensure correct usage.
2704 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
2705 !TestIsLessOp && !TestIsStrictOp);
2706 if (!NewUB)
2707 return true;
2708 UB = NewUB;
2709 TestIsLessOp = LessOp;
2710 TestIsStrictOp = StrictOp;
2711 ConditionSrcRange = SR;
2712 ConditionLoc = SL;
2713 return false;
2714}
2715
2716bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
2717 // State consistency checking to ensure correct usage.
2718 assert(Var != nullptr && LB != nullptr && Step == nullptr);
2719 if (!NewStep)
2720 return true;
2721 if (!NewStep->isValueDependent()) {
2722 // Check that the step is integer expression.
2723 SourceLocation StepLoc = NewStep->getLocStart();
2724 ExprResult Val =
2725 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
2726 if (Val.isInvalid())
2727 return true;
2728 NewStep = Val.get();
2729
2730 // OpenMP [2.6, Canonical Loop Form, Restrictions]
2731 // If test-expr is of form var relational-op b and relational-op is < or
2732 // <= then incr-expr must cause var to increase on each iteration of the
2733 // loop. If test-expr is of form var relational-op b and relational-op is
2734 // > or >= then incr-expr must cause var to decrease on each iteration of
2735 // the loop.
2736 // If test-expr is of form b relational-op var and relational-op is < or
2737 // <= then incr-expr must cause var to decrease on each iteration of the
2738 // loop. If test-expr is of form b relational-op var and relational-op is
2739 // > or >= then incr-expr must cause var to increase on each iteration of
2740 // the loop.
2741 llvm::APSInt Result;
2742 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
2743 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
2744 bool IsConstNeg =
2745 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00002746 bool IsConstPos =
2747 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002748 bool IsConstZero = IsConstant && !Result.getBoolValue();
2749 if (UB && (IsConstZero ||
2750 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00002751 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002752 SemaRef.Diag(NewStep->getExprLoc(),
2753 diag::err_omp_loop_incr_not_compatible)
2754 << Var << TestIsLessOp << NewStep->getSourceRange();
2755 SemaRef.Diag(ConditionLoc,
2756 diag::note_omp_loop_cond_requres_compatible_incr)
2757 << TestIsLessOp << ConditionSrcRange;
2758 return true;
2759 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002760 if (TestIsLessOp == Subtract) {
2761 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
2762 NewStep).get();
2763 Subtract = !Subtract;
2764 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002765 }
2766
2767 Step = NewStep;
2768 SubtractStep = Subtract;
2769 return false;
2770}
2771
Alexey Bataev9c821032015-04-30 04:23:23 +00002772bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002773 // Check init-expr for canonical loop form and save loop counter
2774 // variable - #Var and its initialization value - #LB.
2775 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
2776 // var = lb
2777 // integer-type var = lb
2778 // random-access-iterator-type var = lb
2779 // pointer-type var = lb
2780 //
2781 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00002782 if (EmitDiags) {
2783 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
2784 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002785 return true;
2786 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002787 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002788 if (Expr *E = dyn_cast<Expr>(S))
2789 S = E->IgnoreParens();
2790 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2791 if (BO->getOpcode() == BO_Assign)
2792 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002793 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002794 BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002795 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
2796 if (DS->isSingleDecl()) {
2797 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00002798 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002799 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00002800 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002801 SemaRef.Diag(S->getLocStart(),
2802 diag::ext_omp_loop_not_canonical_init)
2803 << S->getSourceRange();
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002804 return SetVarAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002805 }
2806 }
2807 }
2808 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
2809 if (CE->getOperator() == OO_Equal)
2810 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002811 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
2812 CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002813
Alexey Bataev9c821032015-04-30 04:23:23 +00002814 if (EmitDiags) {
2815 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
2816 << S->getSourceRange();
2817 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002818 return true;
2819}
2820
Alexey Bataev23b69422014-06-18 07:08:49 +00002821/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002822/// variable (which may be the loop variable) if possible.
2823static const VarDecl *GetInitVarDecl(const Expr *E) {
2824 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00002825 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002826 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002827 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
2828 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00002829 if ((Ctor->isCopyOrMoveConstructor() ||
2830 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
2831 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002832 E = CE->getArg(0)->IgnoreParenImpCasts();
2833 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
2834 if (!DRE)
2835 return nullptr;
2836 return dyn_cast<VarDecl>(DRE->getDecl());
2837}
2838
2839bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
2840 // Check test-expr for canonical form, save upper-bound UB, flags for
2841 // less/greater and for strict/non-strict comparison.
2842 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2843 // var relational-op b
2844 // b relational-op var
2845 //
2846 if (!S) {
2847 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
2848 return true;
2849 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002850 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002851 SourceLocation CondLoc = S->getLocStart();
2852 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2853 if (BO->isRelationalOp()) {
2854 if (GetInitVarDecl(BO->getLHS()) == Var)
2855 return SetUB(BO->getRHS(),
2856 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
2857 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2858 BO->getSourceRange(), BO->getOperatorLoc());
2859 if (GetInitVarDecl(BO->getRHS()) == Var)
2860 return SetUB(BO->getLHS(),
2861 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
2862 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2863 BO->getSourceRange(), BO->getOperatorLoc());
2864 }
2865 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2866 if (CE->getNumArgs() == 2) {
2867 auto Op = CE->getOperator();
2868 switch (Op) {
2869 case OO_Greater:
2870 case OO_GreaterEqual:
2871 case OO_Less:
2872 case OO_LessEqual:
2873 if (GetInitVarDecl(CE->getArg(0)) == Var)
2874 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
2875 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2876 CE->getOperatorLoc());
2877 if (GetInitVarDecl(CE->getArg(1)) == Var)
2878 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
2879 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2880 CE->getOperatorLoc());
2881 break;
2882 default:
2883 break;
2884 }
2885 }
2886 }
2887 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
2888 << S->getSourceRange() << Var;
2889 return true;
2890}
2891
2892bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
2893 // RHS of canonical loop form increment can be:
2894 // var + incr
2895 // incr + var
2896 // var - incr
2897 //
2898 RHS = RHS->IgnoreParenImpCasts();
2899 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
2900 if (BO->isAdditiveOp()) {
2901 bool IsAdd = BO->getOpcode() == BO_Add;
2902 if (GetInitVarDecl(BO->getLHS()) == Var)
2903 return SetStep(BO->getRHS(), !IsAdd);
2904 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
2905 return SetStep(BO->getLHS(), false);
2906 }
2907 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
2908 bool IsAdd = CE->getOperator() == OO_Plus;
2909 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
2910 if (GetInitVarDecl(CE->getArg(0)) == Var)
2911 return SetStep(CE->getArg(1), !IsAdd);
2912 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
2913 return SetStep(CE->getArg(0), false);
2914 }
2915 }
2916 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2917 << RHS->getSourceRange() << Var;
2918 return true;
2919}
2920
2921bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
2922 // Check incr-expr for canonical loop form and return true if it
2923 // does not conform.
2924 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2925 // ++var
2926 // var++
2927 // --var
2928 // var--
2929 // var += incr
2930 // var -= incr
2931 // var = var + incr
2932 // var = incr + var
2933 // var = var - incr
2934 //
2935 if (!S) {
2936 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
2937 return true;
2938 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002939 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002940 S = S->IgnoreParens();
2941 if (auto UO = dyn_cast<UnaryOperator>(S)) {
2942 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
2943 return SetStep(
2944 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
2945 (UO->isDecrementOp() ? -1 : 1)).get(),
2946 false);
2947 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
2948 switch (BO->getOpcode()) {
2949 case BO_AddAssign:
2950 case BO_SubAssign:
2951 if (GetInitVarDecl(BO->getLHS()) == Var)
2952 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
2953 break;
2954 case BO_Assign:
2955 if (GetInitVarDecl(BO->getLHS()) == Var)
2956 return CheckIncRHS(BO->getRHS());
2957 break;
2958 default:
2959 break;
2960 }
2961 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2962 switch (CE->getOperator()) {
2963 case OO_PlusPlus:
2964 case OO_MinusMinus:
2965 if (GetInitVarDecl(CE->getArg(0)) == Var)
2966 return SetStep(
2967 SemaRef.ActOnIntegerConstant(
2968 CE->getLocStart(),
2969 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
2970 false);
2971 break;
2972 case OO_PlusEqual:
2973 case OO_MinusEqual:
2974 if (GetInitVarDecl(CE->getArg(0)) == Var)
2975 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
2976 break;
2977 case OO_Equal:
2978 if (GetInitVarDecl(CE->getArg(0)) == Var)
2979 return CheckIncRHS(CE->getArg(1));
2980 break;
2981 default:
2982 break;
2983 }
2984 }
2985 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
2986 << S->getSourceRange() << Var;
2987 return true;
2988}
Alexander Musmana5f070a2014-10-01 06:03:56 +00002989
Alexey Bataevb08f89f2015-08-14 12:25:37 +00002990namespace {
2991// Transform variables declared in GNU statement expressions to new ones to
2992// avoid crash on codegen.
2993class TransformToNewDefs : public TreeTransform<TransformToNewDefs> {
2994 typedef TreeTransform<TransformToNewDefs> BaseTransform;
2995
2996public:
2997 TransformToNewDefs(Sema &SemaRef) : BaseTransform(SemaRef) {}
2998
2999 Decl *TransformDefinition(SourceLocation Loc, Decl *D) {
3000 if (auto *VD = cast<VarDecl>(D))
3001 if (!isa<ParmVarDecl>(D) && !isa<VarTemplateSpecializationDecl>(D) &&
3002 !isa<ImplicitParamDecl>(D)) {
3003 auto *NewVD = VarDecl::Create(
3004 SemaRef.Context, VD->getDeclContext(), VD->getLocStart(),
3005 VD->getLocation(), VD->getIdentifier(), VD->getType(),
3006 VD->getTypeSourceInfo(), VD->getStorageClass());
3007 NewVD->setTSCSpec(VD->getTSCSpec());
3008 NewVD->setInit(VD->getInit());
3009 NewVD->setInitStyle(VD->getInitStyle());
3010 NewVD->setExceptionVariable(VD->isExceptionVariable());
3011 NewVD->setNRVOVariable(VD->isNRVOVariable());
3012 NewVD->setCXXForRangeDecl(VD->isInExternCXXContext());
3013 NewVD->setConstexpr(VD->isConstexpr());
3014 NewVD->setInitCapture(VD->isInitCapture());
3015 NewVD->setPreviousDeclInSameBlockScope(
3016 VD->isPreviousDeclInSameBlockScope());
3017 VD->getDeclContext()->addHiddenDecl(NewVD);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003018 if (VD->hasAttrs())
3019 NewVD->setAttrs(VD->getAttrs());
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003020 transformedLocalDecl(VD, NewVD);
3021 return NewVD;
3022 }
3023 return BaseTransform::TransformDefinition(Loc, D);
3024 }
3025
3026 ExprResult TransformDeclRefExpr(DeclRefExpr *E) {
3027 if (auto *NewD = TransformDecl(E->getExprLoc(), E->getDecl()))
3028 if (E->getDecl() != NewD) {
3029 NewD->setReferenced();
3030 NewD->markUsed(SemaRef.Context);
3031 return DeclRefExpr::Create(
3032 SemaRef.Context, E->getQualifierLoc(), E->getTemplateKeywordLoc(),
3033 cast<ValueDecl>(NewD), E->refersToEnclosingVariableOrCapture(),
3034 E->getNameInfo(), E->getType(), E->getValueKind());
3035 }
3036 return BaseTransform::TransformDeclRefExpr(E);
3037 }
3038};
3039}
3040
Alexander Musmana5f070a2014-10-01 06:03:56 +00003041/// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00003042Expr *
3043OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S,
3044 const bool LimitedType) const {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003045 TransformToNewDefs Transform(SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003046 ExprResult Diff;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003047 auto VarType = Var->getType().getNonReferenceType();
3048 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003049 SemaRef.getLangOpts().CPlusPlus) {
3050 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003051 auto *UBExpr = TestIsLessOp ? UB : LB;
3052 auto *LBExpr = TestIsLessOp ? LB : UB;
3053 Expr *Upper = Transform.TransformExpr(UBExpr).get();
3054 Expr *Lower = Transform.TransformExpr(LBExpr).get();
3055 if (!Upper || !Lower)
3056 return nullptr;
3057 Upper = SemaRef.PerformImplicitConversion(Upper, UBExpr->getType(),
3058 Sema::AA_Converting,
3059 /*AllowExplicit=*/true)
3060 .get();
3061 Lower = SemaRef.PerformImplicitConversion(Lower, LBExpr->getType(),
3062 Sema::AA_Converting,
3063 /*AllowExplicit=*/true)
3064 .get();
3065 if (!Upper || !Lower)
3066 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003067
3068 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
3069
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003070 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00003071 // BuildBinOp already emitted error, this one is to point user to upper
3072 // and lower bound, and to tell what is passed to 'operator-'.
3073 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
3074 << Upper->getSourceRange() << Lower->getSourceRange();
3075 return nullptr;
3076 }
3077 }
3078
3079 if (!Diff.isUsable())
3080 return nullptr;
3081
3082 // Upper - Lower [- 1]
3083 if (TestIsStrictOp)
3084 Diff = SemaRef.BuildBinOp(
3085 S, DefaultLoc, BO_Sub, Diff.get(),
3086 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3087 if (!Diff.isUsable())
3088 return nullptr;
3089
3090 // Upper - Lower [- 1] + Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003091 auto NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
3092 if (NewStep.isInvalid())
3093 return nullptr;
3094 NewStep = SemaRef.PerformImplicitConversion(
3095 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
3096 /*AllowExplicit=*/true);
3097 if (NewStep.isInvalid())
3098 return nullptr;
3099 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003100 if (!Diff.isUsable())
3101 return nullptr;
3102
3103 // Parentheses (for dumping/debugging purposes only).
3104 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
3105 if (!Diff.isUsable())
3106 return nullptr;
3107
3108 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003109 NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
3110 if (NewStep.isInvalid())
3111 return nullptr;
3112 NewStep = SemaRef.PerformImplicitConversion(
3113 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
3114 /*AllowExplicit=*/true);
3115 if (NewStep.isInvalid())
3116 return nullptr;
3117 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003118 if (!Diff.isUsable())
3119 return nullptr;
3120
Alexander Musman174b3ca2014-10-06 11:16:29 +00003121 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003122 QualType Type = Diff.get()->getType();
3123 auto &C = SemaRef.Context;
3124 bool UseVarType = VarType->hasIntegerRepresentation() &&
3125 C.getTypeSize(Type) > C.getTypeSize(VarType);
3126 if (!Type->isIntegerType() || UseVarType) {
3127 unsigned NewSize =
3128 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
3129 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
3130 : Type->hasSignedIntegerRepresentation();
3131 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
3132 Diff = SemaRef.PerformImplicitConversion(
3133 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
3134 if (!Diff.isUsable())
3135 return nullptr;
3136 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00003137 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00003138 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
3139 if (NewSize != C.getTypeSize(Type)) {
3140 if (NewSize < C.getTypeSize(Type)) {
3141 assert(NewSize == 64 && "incorrect loop var size");
3142 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
3143 << InitSrcRange << ConditionSrcRange;
3144 }
3145 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003146 NewSize, Type->hasSignedIntegerRepresentation() ||
3147 C.getTypeSize(Type) < NewSize);
Alexander Musman174b3ca2014-10-06 11:16:29 +00003148 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
3149 Sema::AA_Converting, true);
3150 if (!Diff.isUsable())
3151 return nullptr;
3152 }
3153 }
3154
Alexander Musmana5f070a2014-10-01 06:03:56 +00003155 return Diff.get();
3156}
3157
Alexey Bataev62dbb972015-04-22 11:59:37 +00003158Expr *OpenMPIterationSpaceChecker::BuildPreCond(Scope *S, Expr *Cond) const {
3159 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
3160 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
3161 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003162 TransformToNewDefs Transform(SemaRef);
3163
3164 auto NewLB = Transform.TransformExpr(LB);
3165 auto NewUB = Transform.TransformExpr(UB);
3166 if (NewLB.isInvalid() || NewUB.isInvalid())
3167 return Cond;
3168 NewLB = SemaRef.PerformImplicitConversion(NewLB.get(), LB->getType(),
3169 Sema::AA_Converting,
3170 /*AllowExplicit=*/true);
3171 NewUB = SemaRef.PerformImplicitConversion(NewUB.get(), UB->getType(),
3172 Sema::AA_Converting,
3173 /*AllowExplicit=*/true);
3174 if (NewLB.isInvalid() || NewUB.isInvalid())
3175 return Cond;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003176 auto CondExpr = SemaRef.BuildBinOp(
3177 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
3178 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003179 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003180 if (CondExpr.isUsable()) {
3181 CondExpr = SemaRef.PerformImplicitConversion(
3182 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
3183 /*AllowExplicit=*/true);
3184 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00003185 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
3186 // Otherwise use original loop conditon and evaluate it in runtime.
3187 return CondExpr.isUsable() ? CondExpr.get() : Cond;
3188}
3189
Alexander Musmana5f070a2014-10-01 06:03:56 +00003190/// \brief Build reference expression to the counter be used for codegen.
3191Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const {
Alexey Bataeva8899172015-08-06 12:30:57 +00003192 return buildDeclRefExpr(SemaRef, Var, Var->getType().getNonReferenceType(),
3193 DefaultLoc);
3194}
3195
3196Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
3197 if (Var && !Var->isInvalidDecl()) {
3198 auto Type = Var->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003199 auto *PrivateVar =
3200 buildVarDecl(SemaRef, DefaultLoc, Type, Var->getName(),
3201 Var->hasAttrs() ? &Var->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00003202 if (PrivateVar->isInvalidDecl())
3203 return nullptr;
3204 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
3205 }
3206 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003207}
3208
3209/// \brief Build initization of the counter be used for codegen.
3210Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
3211
3212/// \brief Build step of the counter be used for codegen.
3213Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
3214
3215/// \brief Iteration space of a single for loop.
3216struct LoopIterationSpace {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003217 /// \brief Condition of the loop.
3218 Expr *PreCond;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003219 /// \brief This expression calculates the number of iterations in the loop.
3220 /// It is always possible to calculate it before starting the loop.
3221 Expr *NumIterations;
3222 /// \brief The loop counter variable.
3223 Expr *CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00003224 /// \brief Private loop counter variable.
3225 Expr *PrivateCounterVar;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003226 /// \brief This is initializer for the initial value of #CounterVar.
3227 Expr *CounterInit;
3228 /// \brief This is step for the #CounterVar used to generate its update:
3229 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
3230 Expr *CounterStep;
3231 /// \brief Should step be subtracted?
3232 bool Subtract;
3233 /// \brief Source range of the loop init.
3234 SourceRange InitSrcRange;
3235 /// \brief Source range of the loop condition.
3236 SourceRange CondSrcRange;
3237 /// \brief Source range of the loop increment.
3238 SourceRange IncSrcRange;
3239};
3240
Alexey Bataev23b69422014-06-18 07:08:49 +00003241} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003242
Alexey Bataev9c821032015-04-30 04:23:23 +00003243void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
3244 assert(getLangOpts().OpenMP && "OpenMP is not active.");
3245 assert(Init && "Expected loop in canonical form.");
3246 unsigned CollapseIteration = DSAStack->getCollapseNumber();
3247 if (CollapseIteration > 0 &&
3248 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
3249 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
3250 if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
3251 DSAStack->addLoopControlVariable(ISC.GetLoopVar());
3252 }
3253 DSAStack->setCollapseNumber(CollapseIteration - 1);
3254 }
3255}
3256
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003257/// \brief Called on a for stmt to check and extract its iteration space
3258/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00003259static bool CheckOpenMPIterationSpace(
3260 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
3261 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003262 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003263 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
3264 LoopIterationSpace &ResultIterSpace) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003265 // OpenMP [2.6, Canonical Loop Form]
3266 // for (init-expr; test-expr; incr-expr) structured-block
3267 auto For = dyn_cast_or_null<ForStmt>(S);
3268 if (!For) {
3269 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003270 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
3271 << getOpenMPDirectiveName(DKind) << NestedLoopCount
3272 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
3273 if (NestedLoopCount > 1) {
3274 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
3275 SemaRef.Diag(DSA.getConstructLoc(),
3276 diag::note_omp_collapse_ordered_expr)
3277 << 2 << CollapseLoopCountExpr->getSourceRange()
3278 << OrderedLoopCountExpr->getSourceRange();
3279 else if (CollapseLoopCountExpr)
3280 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
3281 diag::note_omp_collapse_ordered_expr)
3282 << 0 << CollapseLoopCountExpr->getSourceRange();
3283 else
3284 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
3285 diag::note_omp_collapse_ordered_expr)
3286 << 1 << OrderedLoopCountExpr->getSourceRange();
3287 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003288 return true;
3289 }
3290 assert(For->getBody());
3291
3292 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
3293
3294 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003295 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003296 if (ISC.CheckInit(Init)) {
3297 return true;
3298 }
3299
3300 bool HasErrors = false;
3301
3302 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003303 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003304
3305 // OpenMP [2.6, Canonical Loop Form]
3306 // Var is one of the following:
3307 // A variable of signed or unsigned integer type.
3308 // For C++, a variable of a random access iterator type.
3309 // For C, a variable of a pointer type.
Alexey Bataeva8899172015-08-06 12:30:57 +00003310 auto VarType = Var->getType().getNonReferenceType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003311 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
3312 !VarType->isPointerType() &&
3313 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
3314 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
3315 << SemaRef.getLangOpts().CPlusPlus;
3316 HasErrors = true;
3317 }
3318
Alexey Bataev4acb8592014-07-07 13:01:15 +00003319 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
3320 // Construct
3321 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3322 // parallel for construct is (are) private.
3323 // The loop iteration variable in the associated for-loop of a simd construct
3324 // with just one associated for-loop is linear with a constant-linear-step
3325 // that is the increment of the associated for-loop.
3326 // Exclude loop var from the list of variables with implicitly defined data
3327 // sharing attributes.
Benjamin Kramerad8e0792014-10-10 15:32:48 +00003328 VarsWithImplicitDSA.erase(Var);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003329
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003330 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
3331 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00003332 // The loop iteration variable in the associated for-loop of a simd construct
3333 // with just one associated for-loop may be listed in a linear clause with a
3334 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00003335 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3336 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003337 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003338 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
3339 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
3340 // declared in the loop and it is predetermined as a private.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003341 auto PredeterminedCKind =
3342 isOpenMPSimdDirective(DKind)
3343 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
3344 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003345 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataev0c024df2015-05-12 09:02:07 +00003346 DVar.CKind != OMPC_threadprivate && DVar.CKind != PredeterminedCKind) ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00003347 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop) &&
3348 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
3349 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate &&
3350 DVar.CKind != OMPC_threadprivate)) &&
Alexey Bataev0c024df2015-05-12 09:02:07 +00003351 ((DVar.CKind != OMPC_private && DVar.CKind != OMPC_threadprivate) ||
3352 DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003353 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00003354 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
3355 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataevf2453a02015-05-06 07:25:08 +00003356 if (DVar.RefExpr == nullptr)
3357 DVar.CKind = PredeterminedCKind;
3358 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, /*IsLoopIterVar=*/true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003359 HasErrors = true;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003360 } else if (LoopVarRefExpr != nullptr) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00003361 // Make the loop iteration variable private (for worksharing constructs),
3362 // linear (for simd directives with the only one associated loop) or
Alexey Bataev10e775f2015-07-30 11:36:16 +00003363 // lastprivate (for simd directives with several collapsed or ordered
3364 // loops).
Alexey Bataev9aba41c2014-11-14 04:08:45 +00003365 if (DVar.CKind == OMPC_unknown)
3366 DVar = DSA.hasDSA(Var, isOpenMPPrivate, MatchesAlways(),
3367 /*FromParent=*/false);
Alexey Bataev9c821032015-04-30 04:23:23 +00003368 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003369 }
3370
Alexey Bataev7ff55242014-06-19 09:13:45 +00003371 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00003372
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003373 // Check test-expr.
3374 HasErrors |= ISC.CheckCond(For->getCond());
3375
3376 // Check incr-expr.
3377 HasErrors |= ISC.CheckInc(For->getInc());
3378
Alexander Musmana5f070a2014-10-01 06:03:56 +00003379 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003380 return HasErrors;
3381
Alexander Musmana5f070a2014-10-01 06:03:56 +00003382 // Build the loop's iteration space representation.
Alexey Bataev62dbb972015-04-22 11:59:37 +00003383 ResultIterSpace.PreCond = ISC.BuildPreCond(DSA.getCurScope(), For->getCond());
Alexander Musman174b3ca2014-10-06 11:16:29 +00003384 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
Alexey Bataev49f6e782015-12-01 04:18:41 +00003385 DSA.getCurScope(),
3386 (isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop));
Alexander Musmana5f070a2014-10-01 06:03:56 +00003387 ResultIterSpace.CounterVar = ISC.BuildCounterVar();
Alexey Bataeva8899172015-08-06 12:30:57 +00003388 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003389 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
3390 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
3391 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
3392 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
3393 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
3394 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
3395
Alexey Bataev62dbb972015-04-22 11:59:37 +00003396 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
3397 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003398 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00003399 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003400 ResultIterSpace.CounterInit == nullptr ||
3401 ResultIterSpace.CounterStep == nullptr);
3402
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003403 return HasErrors;
3404}
3405
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003406/// \brief Build 'VarRef = Start.
3407static ExprResult BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc,
3408 ExprResult VarRef, ExprResult Start) {
3409 TransformToNewDefs Transform(SemaRef);
3410 // Build 'VarRef = Start.
3411 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
3412 if (NewStart.isInvalid())
3413 return ExprError();
3414 NewStart = SemaRef.PerformImplicitConversion(
3415 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
3416 Sema::AA_Converting,
3417 /*AllowExplicit=*/true);
3418 if (NewStart.isInvalid())
3419 return ExprError();
3420 NewStart = SemaRef.PerformImplicitConversion(
3421 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
3422 /*AllowExplicit=*/true);
3423 if (!NewStart.isUsable())
3424 return ExprError();
3425
3426 auto Init =
3427 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
3428 return Init;
3429}
3430
Alexander Musmana5f070a2014-10-01 06:03:56 +00003431/// \brief Build 'VarRef = Start + Iter * Step'.
3432static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S,
3433 SourceLocation Loc, ExprResult VarRef,
3434 ExprResult Start, ExprResult Iter,
3435 ExprResult Step, bool Subtract) {
3436 // Add parentheses (for debugging purposes only).
3437 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
3438 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
3439 !Step.isUsable())
3440 return ExprError();
3441
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003442 TransformToNewDefs Transform(SemaRef);
3443 auto NewStep = Transform.TransformExpr(Step.get()->IgnoreImplicit());
3444 if (NewStep.isInvalid())
3445 return ExprError();
3446 NewStep = SemaRef.PerformImplicitConversion(
3447 NewStep.get(), Step.get()->IgnoreImplicit()->getType(),
3448 Sema::AA_Converting,
3449 /*AllowExplicit=*/true);
3450 if (NewStep.isInvalid())
3451 return ExprError();
3452 ExprResult Update =
3453 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003454 if (!Update.isUsable())
3455 return ExprError();
3456
3457 // Build 'VarRef = Start + Iter * Step'.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003458 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
3459 if (NewStart.isInvalid())
3460 return ExprError();
3461 NewStart = SemaRef.PerformImplicitConversion(
3462 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
3463 Sema::AA_Converting,
3464 /*AllowExplicit=*/true);
3465 if (NewStart.isInvalid())
3466 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003467 Update = SemaRef.BuildBinOp(S, Loc, (Subtract ? BO_Sub : BO_Add),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003468 NewStart.get(), Update.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003469 if (!Update.isUsable())
3470 return ExprError();
3471
3472 Update = SemaRef.PerformImplicitConversion(
3473 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
3474 if (!Update.isUsable())
3475 return ExprError();
3476
3477 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
3478 return Update;
3479}
3480
3481/// \brief Convert integer expression \a E to make it have at least \a Bits
3482/// bits.
3483static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
3484 Sema &SemaRef) {
3485 if (E == nullptr)
3486 return ExprError();
3487 auto &C = SemaRef.Context;
3488 QualType OldType = E->getType();
3489 unsigned HasBits = C.getTypeSize(OldType);
3490 if (HasBits >= Bits)
3491 return ExprResult(E);
3492 // OK to convert to signed, because new type has more bits than old.
3493 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
3494 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
3495 true);
3496}
3497
3498/// \brief Check if the given expression \a E is a constant integer that fits
3499/// into \a Bits bits.
3500static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
3501 if (E == nullptr)
3502 return false;
3503 llvm::APSInt Result;
3504 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
3505 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
3506 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003507}
3508
3509/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00003510/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
3511/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003512static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00003513CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
3514 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
3515 DSAStackTy &DSA,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003516 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00003517 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003518 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003519 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003520 // Found 'collapse' clause - calculate collapse number.
3521 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003522 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00003523 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00003524 }
3525 if (OrderedLoopCountExpr) {
3526 // Found 'ordered' clause - calculate collapse number.
3527 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00003528 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
3529 if (Result.getLimitedValue() < NestedLoopCount) {
3530 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
3531 diag::err_omp_wrong_ordered_loop_count)
3532 << OrderedLoopCountExpr->getSourceRange();
3533 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
3534 diag::note_collapse_loop_count)
3535 << CollapseLoopCountExpr->getSourceRange();
3536 }
3537 NestedLoopCount = Result.getLimitedValue();
3538 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003539 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003540 // This is helper routine for loop directives (e.g., 'for', 'simd',
3541 // 'for simd', etc.).
Alexander Musmana5f070a2014-10-01 06:03:56 +00003542 SmallVector<LoopIterationSpace, 4> IterSpaces;
3543 IterSpaces.resize(NestedLoopCount);
3544 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003545 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003546 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003547 NestedLoopCount, CollapseLoopCountExpr,
3548 OrderedLoopCountExpr, VarsWithImplicitDSA,
3549 IterSpaces[Cnt]))
Alexey Bataevabfc0692014-06-25 06:52:00 +00003550 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003551 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003552 // OpenMP [2.8.1, simd construct, Restrictions]
3553 // All loops associated with the construct must be perfectly nested; that
3554 // is, there must be no intervening code nor any OpenMP directive between
3555 // any two loops.
3556 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003557 }
3558
Alexander Musmana5f070a2014-10-01 06:03:56 +00003559 Built.clear(/* size */ NestedLoopCount);
3560
3561 if (SemaRef.CurContext->isDependentContext())
3562 return NestedLoopCount;
3563
3564 // An example of what is generated for the following code:
3565 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00003566 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00003567 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003568 // for (k = 0; k < NK; ++k)
3569 // for (j = J0; j < NJ; j+=2) {
3570 // <loop body>
3571 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003572 //
3573 // We generate the code below.
3574 // Note: the loop body may be outlined in CodeGen.
3575 // Note: some counters may be C++ classes, operator- is used to find number of
3576 // iterations and operator+= to calculate counter value.
3577 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
3578 // or i64 is currently supported).
3579 //
3580 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
3581 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
3582 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
3583 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
3584 // // similar updates for vars in clauses (e.g. 'linear')
3585 // <loop body (using local i and j)>
3586 // }
3587 // i = NI; // assign final values of counters
3588 // j = NJ;
3589 //
3590
3591 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
3592 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00003593 // Precondition tests if there is at least one iteration (all conditions are
3594 // true).
3595 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003596 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003597 ExprResult LastIteration32 = WidenIterationCount(
3598 32 /* Bits */, SemaRef.PerformImplicitConversion(
3599 N0->IgnoreImpCasts(), N0->getType(),
3600 Sema::AA_Converting, /*AllowExplicit=*/true)
3601 .get(),
3602 SemaRef);
3603 ExprResult LastIteration64 = WidenIterationCount(
3604 64 /* Bits */, SemaRef.PerformImplicitConversion(
3605 N0->IgnoreImpCasts(), N0->getType(),
3606 Sema::AA_Converting, /*AllowExplicit=*/true)
3607 .get(),
3608 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003609
3610 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
3611 return NestedLoopCount;
3612
3613 auto &C = SemaRef.Context;
3614 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
3615
3616 Scope *CurScope = DSA.getCurScope();
3617 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003618 if (PreCond.isUsable()) {
3619 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
3620 PreCond.get(), IterSpaces[Cnt].PreCond);
3621 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003622 auto N = IterSpaces[Cnt].NumIterations;
3623 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
3624 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003625 LastIteration32 = SemaRef.BuildBinOp(
3626 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
3627 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
3628 Sema::AA_Converting,
3629 /*AllowExplicit=*/true)
3630 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003631 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003632 LastIteration64 = SemaRef.BuildBinOp(
3633 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
3634 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
3635 Sema::AA_Converting,
3636 /*AllowExplicit=*/true)
3637 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003638 }
3639
3640 // Choose either the 32-bit or 64-bit version.
3641 ExprResult LastIteration = LastIteration64;
3642 if (LastIteration32.isUsable() &&
3643 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
3644 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
3645 FitsInto(
3646 32 /* Bits */,
3647 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
3648 LastIteration64.get(), SemaRef)))
3649 LastIteration = LastIteration32;
3650
3651 if (!LastIteration.isUsable())
3652 return 0;
3653
3654 // Save the number of iterations.
3655 ExprResult NumIterations = LastIteration;
3656 {
3657 LastIteration = SemaRef.BuildBinOp(
3658 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
3659 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3660 if (!LastIteration.isUsable())
3661 return 0;
3662 }
3663
3664 // Calculate the last iteration number beforehand instead of doing this on
3665 // each iteration. Do not do this if the number of iterations may be kfold-ed.
3666 llvm::APSInt Result;
3667 bool IsConstant =
3668 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
3669 ExprResult CalcLastIteration;
3670 if (!IsConstant) {
3671 SourceLocation SaveLoc;
3672 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00003673 buildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00003674 ".omp.last.iteration");
Alexey Bataev39f915b82015-05-08 10:41:21 +00003675 ExprResult SaveRef = buildDeclRefExpr(
3676 SemaRef, SaveVar, LastIteration.get()->getType(), SaveLoc);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003677 CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign,
3678 SaveRef.get(), LastIteration.get());
3679 LastIteration = SaveRef;
3680
3681 // Prepare SaveRef + 1.
3682 NumIterations = SemaRef.BuildBinOp(
3683 CurScope, SaveLoc, BO_Add, SaveRef.get(),
3684 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3685 if (!NumIterations.isUsable())
3686 return 0;
3687 }
3688
3689 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
3690
Alexander Musmanc6388682014-12-15 07:07:06 +00003691 QualType VType = LastIteration.get()->getType();
3692 // Build variables passed into runtime, nesessary for worksharing directives.
3693 ExprResult LB, UB, IL, ST, EUB;
Alexey Bataev49f6e782015-12-01 04:18:41 +00003694 if ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00003695 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00003696 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
3697 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003698 SemaRef.AddInitializerToDecl(
3699 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
3700 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3701
3702 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00003703 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
3704 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003705 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
3706 /*DirectInit*/ false,
3707 /*TypeMayContainAuto*/ false);
3708
3709 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
3710 // This will be used to implement clause 'lastprivate'.
3711 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00003712 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
3713 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003714 SemaRef.AddInitializerToDecl(
3715 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
3716 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3717
3718 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev39f915b82015-05-08 10:41:21 +00003719 VarDecl *STDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.stride");
3720 ST = buildDeclRefExpr(SemaRef, STDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003721 SemaRef.AddInitializerToDecl(
3722 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
3723 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3724
3725 // Build expression: UB = min(UB, LastIteration)
3726 // It is nesessary for CodeGen of directives with static scheduling.
3727 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
3728 UB.get(), LastIteration.get());
3729 ExprResult CondOp = SemaRef.ActOnConditionalOp(
3730 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
3731 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
3732 CondOp.get());
3733 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
3734 }
3735
3736 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003737 ExprResult IV;
3738 ExprResult Init;
3739 {
Alexey Bataev39f915b82015-05-08 10:41:21 +00003740 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.iv");
3741 IV = buildDeclRefExpr(SemaRef, IVDecl, VType, InitLoc);
Alexey Bataev49f6e782015-12-01 04:18:41 +00003742 Expr *RHS = (isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop)
Alexander Musmanc6388682014-12-15 07:07:06 +00003743 ? LB.get()
3744 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
3745 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
3746 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003747 }
3748
Alexander Musmanc6388682014-12-15 07:07:06 +00003749 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003750 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00003751 ExprResult Cond =
Alexey Bataev49f6e782015-12-01 04:18:41 +00003752 (isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop)
Alexander Musmanc6388682014-12-15 07:07:06 +00003753 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
3754 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
3755 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003756
3757 // Loop increment (IV = IV + 1)
3758 SourceLocation IncLoc;
3759 ExprResult Inc =
3760 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
3761 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
3762 if (!Inc.isUsable())
3763 return 0;
3764 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00003765 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
3766 if (!Inc.isUsable())
3767 return 0;
3768
3769 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
3770 // Used for directives with static scheduling.
3771 ExprResult NextLB, NextUB;
Alexey Bataev49f6e782015-12-01 04:18:41 +00003772 if (isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop) {
Alexander Musmanc6388682014-12-15 07:07:06 +00003773 // LB + ST
3774 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
3775 if (!NextLB.isUsable())
3776 return 0;
3777 // LB = LB + ST
3778 NextLB =
3779 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
3780 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
3781 if (!NextLB.isUsable())
3782 return 0;
3783 // UB + ST
3784 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
3785 if (!NextUB.isUsable())
3786 return 0;
3787 // UB = UB + ST
3788 NextUB =
3789 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
3790 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
3791 if (!NextUB.isUsable())
3792 return 0;
3793 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003794
3795 // Build updates and final values of the loop counters.
3796 bool HasErrors = false;
3797 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003798 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003799 Built.Updates.resize(NestedLoopCount);
3800 Built.Finals.resize(NestedLoopCount);
3801 {
3802 ExprResult Div;
3803 // Go from inner nested loop to outer.
3804 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
3805 LoopIterationSpace &IS = IterSpaces[Cnt];
3806 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
3807 // Build: Iter = (IV / Div) % IS.NumIters
3808 // where Div is product of previous iterations' IS.NumIters.
3809 ExprResult Iter;
3810 if (Div.isUsable()) {
3811 Iter =
3812 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
3813 } else {
3814 Iter = IV;
3815 assert((Cnt == (int)NestedLoopCount - 1) &&
3816 "unusable div expected on first iteration only");
3817 }
3818
3819 if (Cnt != 0 && Iter.isUsable())
3820 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
3821 IS.NumIterations);
3822 if (!Iter.isUsable()) {
3823 HasErrors = true;
3824 break;
3825 }
3826
Alexey Bataev39f915b82015-05-08 10:41:21 +00003827 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
3828 auto *CounterVar = buildDeclRefExpr(
3829 SemaRef, cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()),
3830 IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
3831 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003832 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
3833 IS.CounterInit);
3834 if (!Init.isUsable()) {
3835 HasErrors = true;
3836 break;
3837 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003838 ExprResult Update =
Alexey Bataev39f915b82015-05-08 10:41:21 +00003839 BuildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003840 IS.CounterInit, Iter, IS.CounterStep, IS.Subtract);
3841 if (!Update.isUsable()) {
3842 HasErrors = true;
3843 break;
3844 }
3845
3846 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
3847 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00003848 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003849 IS.NumIterations, IS.CounterStep, IS.Subtract);
3850 if (!Final.isUsable()) {
3851 HasErrors = true;
3852 break;
3853 }
3854
3855 // Build Div for the next iteration: Div <- Div * IS.NumIters
3856 if (Cnt != 0) {
3857 if (Div.isUnset())
3858 Div = IS.NumIterations;
3859 else
3860 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
3861 IS.NumIterations);
3862
3863 // Add parentheses (for debugging purposes only).
3864 if (Div.isUsable())
3865 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
3866 if (!Div.isUsable()) {
3867 HasErrors = true;
3868 break;
3869 }
3870 }
3871 if (!Update.isUsable() || !Final.isUsable()) {
3872 HasErrors = true;
3873 break;
3874 }
3875 // Save results
3876 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00003877 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003878 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003879 Built.Updates[Cnt] = Update.get();
3880 Built.Finals[Cnt] = Final.get();
3881 }
3882 }
3883
3884 if (HasErrors)
3885 return 0;
3886
3887 // Save results
3888 Built.IterationVarRef = IV.get();
3889 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00003890 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003891 Built.CalcLastIteration =
3892 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003893 Built.PreCond = PreCond.get();
3894 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003895 Built.Init = Init.get();
3896 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00003897 Built.LB = LB.get();
3898 Built.UB = UB.get();
3899 Built.IL = IL.get();
3900 Built.ST = ST.get();
3901 Built.EUB = EUB.get();
3902 Built.NLB = NextLB.get();
3903 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003904
Alexey Bataevabfc0692014-06-25 06:52:00 +00003905 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003906}
3907
Alexey Bataev10e775f2015-07-30 11:36:16 +00003908static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00003909 auto CollapseClauses =
3910 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
3911 if (CollapseClauses.begin() != CollapseClauses.end())
3912 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003913 return nullptr;
3914}
3915
Alexey Bataev10e775f2015-07-30 11:36:16 +00003916static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00003917 auto OrderedClauses =
3918 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
3919 if (OrderedClauses.begin() != OrderedClauses.end())
3920 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00003921 return nullptr;
3922}
3923
Alexey Bataev66b15b52015-08-21 11:14:16 +00003924static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen,
3925 const Expr *Safelen) {
3926 llvm::APSInt SimdlenRes, SafelenRes;
3927 if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() ||
3928 Simdlen->isInstantiationDependent() ||
3929 Simdlen->containsUnexpandedParameterPack())
3930 return false;
3931 if (Safelen->isValueDependent() || Safelen->isTypeDependent() ||
3932 Safelen->isInstantiationDependent() ||
3933 Safelen->containsUnexpandedParameterPack())
3934 return false;
3935 Simdlen->EvaluateAsInt(SimdlenRes, S.Context);
3936 Safelen->EvaluateAsInt(SafelenRes, S.Context);
3937 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
3938 // If both simdlen and safelen clauses are specified, the value of the simdlen
3939 // parameter must be less than or equal to the value of the safelen parameter.
3940 if (SimdlenRes > SafelenRes) {
3941 S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values)
3942 << Simdlen->getSourceRange() << Safelen->getSourceRange();
3943 return true;
3944 }
3945 return false;
3946}
3947
Alexey Bataev4acb8592014-07-07 13:01:15 +00003948StmtResult Sema::ActOnOpenMPSimdDirective(
3949 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
3950 SourceLocation EndLoc,
3951 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003952 if (!AStmt)
3953 return StmtError();
3954
3955 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00003956 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003957 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
3958 // define the nested loops number.
3959 unsigned NestedLoopCount = CheckOpenMPLoop(
3960 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
3961 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00003962 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003963 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003964
Alexander Musmana5f070a2014-10-01 06:03:56 +00003965 assert((CurContext->isDependentContext() || B.builtAll()) &&
3966 "omp simd loop exprs were not built");
3967
Alexander Musman3276a272015-03-21 10:12:56 +00003968 if (!CurContext->isDependentContext()) {
3969 // Finalize the clauses that need pre-built expressions for CodeGen.
3970 for (auto C : Clauses) {
3971 if (auto LC = dyn_cast<OMPLinearClause>(C))
3972 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
3973 B.NumIterations, *this, CurScope))
3974 return StmtError();
3975 }
3976 }
3977
Alexey Bataev66b15b52015-08-21 11:14:16 +00003978 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
3979 // If both simdlen and safelen clauses are specified, the value of the simdlen
3980 // parameter must be less than or equal to the value of the safelen parameter.
3981 OMPSafelenClause *Safelen = nullptr;
3982 OMPSimdlenClause *Simdlen = nullptr;
3983 for (auto *Clause : Clauses) {
3984 if (Clause->getClauseKind() == OMPC_safelen)
3985 Safelen = cast<OMPSafelenClause>(Clause);
3986 else if (Clause->getClauseKind() == OMPC_simdlen)
3987 Simdlen = cast<OMPSimdlenClause>(Clause);
3988 if (Safelen && Simdlen)
3989 break;
3990 }
3991 if (Simdlen && Safelen &&
3992 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
3993 Safelen->getSafelen()))
3994 return StmtError();
3995
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003996 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00003997 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
3998 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003999}
4000
Alexey Bataev4acb8592014-07-07 13:01:15 +00004001StmtResult Sema::ActOnOpenMPForDirective(
4002 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4003 SourceLocation EndLoc,
4004 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004005 if (!AStmt)
4006 return StmtError();
4007
4008 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004009 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004010 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4011 // define the nested loops number.
4012 unsigned NestedLoopCount = CheckOpenMPLoop(
4013 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4014 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004015 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00004016 return StmtError();
4017
Alexander Musmana5f070a2014-10-01 06:03:56 +00004018 assert((CurContext->isDependentContext() || B.builtAll()) &&
4019 "omp for loop exprs were not built");
4020
Alexey Bataev54acd402015-08-04 11:18:19 +00004021 if (!CurContext->isDependentContext()) {
4022 // Finalize the clauses that need pre-built expressions for CodeGen.
4023 for (auto C : Clauses) {
4024 if (auto LC = dyn_cast<OMPLinearClause>(C))
4025 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4026 B.NumIterations, *this, CurScope))
4027 return StmtError();
4028 }
4029 }
4030
Alexey Bataevf29276e2014-06-18 04:14:57 +00004031 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004032 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004033 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00004034}
4035
Alexander Musmanf82886e2014-09-18 05:12:34 +00004036StmtResult Sema::ActOnOpenMPForSimdDirective(
4037 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4038 SourceLocation EndLoc,
4039 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004040 if (!AStmt)
4041 return StmtError();
4042
4043 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004044 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004045 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4046 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00004047 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004048 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
4049 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4050 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004051 if (NestedLoopCount == 0)
4052 return StmtError();
4053
Alexander Musmanc6388682014-12-15 07:07:06 +00004054 assert((CurContext->isDependentContext() || B.builtAll()) &&
4055 "omp for simd loop exprs were not built");
4056
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00004057 if (!CurContext->isDependentContext()) {
4058 // Finalize the clauses that need pre-built expressions for CodeGen.
4059 for (auto C : Clauses) {
4060 if (auto LC = dyn_cast<OMPLinearClause>(C))
4061 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4062 B.NumIterations, *this, CurScope))
4063 return StmtError();
4064 }
4065 }
4066
Alexey Bataev66b15b52015-08-21 11:14:16 +00004067 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4068 // If both simdlen and safelen clauses are specified, the value of the simdlen
4069 // parameter must be less than or equal to the value of the safelen parameter.
4070 OMPSafelenClause *Safelen = nullptr;
4071 OMPSimdlenClause *Simdlen = nullptr;
4072 for (auto *Clause : Clauses) {
4073 if (Clause->getClauseKind() == OMPC_safelen)
4074 Safelen = cast<OMPSafelenClause>(Clause);
4075 else if (Clause->getClauseKind() == OMPC_simdlen)
4076 Simdlen = cast<OMPSimdlenClause>(Clause);
4077 if (Safelen && Simdlen)
4078 break;
4079 }
4080 if (Simdlen && Safelen &&
4081 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4082 Safelen->getSafelen()))
4083 return StmtError();
4084
Alexander Musmanf82886e2014-09-18 05:12:34 +00004085 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004086 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4087 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004088}
4089
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004090StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
4091 Stmt *AStmt,
4092 SourceLocation StartLoc,
4093 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004094 if (!AStmt)
4095 return StmtError();
4096
4097 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004098 auto BaseStmt = AStmt;
4099 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4100 BaseStmt = CS->getCapturedStmt();
4101 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4102 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004103 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004104 return StmtError();
4105 // All associated statements must be '#pragma omp section' except for
4106 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004107 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004108 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4109 if (SectionStmt)
4110 Diag(SectionStmt->getLocStart(),
4111 diag::err_omp_sections_substmt_not_section);
4112 return StmtError();
4113 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004114 cast<OMPSectionDirective>(SectionStmt)
4115 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004116 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004117 } else {
4118 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
4119 return StmtError();
4120 }
4121
4122 getCurFunction()->setHasBranchProtectedScope();
4123
Alexey Bataev25e5b442015-09-15 12:52:43 +00004124 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4125 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004126}
4127
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004128StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
4129 SourceLocation StartLoc,
4130 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004131 if (!AStmt)
4132 return StmtError();
4133
4134 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004135
4136 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00004137 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004138
Alexey Bataev25e5b442015-09-15 12:52:43 +00004139 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
4140 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004141}
4142
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004143StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
4144 Stmt *AStmt,
4145 SourceLocation StartLoc,
4146 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004147 if (!AStmt)
4148 return StmtError();
4149
4150 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00004151
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004152 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00004153
Alexey Bataev3255bf32015-01-19 05:20:46 +00004154 // OpenMP [2.7.3, single Construct, Restrictions]
4155 // The copyprivate clause must not be used with the nowait clause.
4156 OMPClause *Nowait = nullptr;
4157 OMPClause *Copyprivate = nullptr;
4158 for (auto *Clause : Clauses) {
4159 if (Clause->getClauseKind() == OMPC_nowait)
4160 Nowait = Clause;
4161 else if (Clause->getClauseKind() == OMPC_copyprivate)
4162 Copyprivate = Clause;
4163 if (Copyprivate && Nowait) {
4164 Diag(Copyprivate->getLocStart(),
4165 diag::err_omp_single_copyprivate_with_nowait);
4166 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
4167 return StmtError();
4168 }
4169 }
4170
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004171 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4172}
4173
Alexander Musman80c22892014-07-17 08:54:58 +00004174StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
4175 SourceLocation StartLoc,
4176 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004177 if (!AStmt)
4178 return StmtError();
4179
4180 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00004181
4182 getCurFunction()->setHasBranchProtectedScope();
4183
4184 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
4185}
4186
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004187StmtResult
4188Sema::ActOnOpenMPCriticalDirective(const DeclarationNameInfo &DirName,
4189 Stmt *AStmt, SourceLocation StartLoc,
4190 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004191 if (!AStmt)
4192 return StmtError();
4193
4194 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004195
4196 getCurFunction()->setHasBranchProtectedScope();
4197
4198 return OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
4199 AStmt);
4200}
4201
Alexey Bataev4acb8592014-07-07 13:01:15 +00004202StmtResult Sema::ActOnOpenMPParallelForDirective(
4203 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4204 SourceLocation EndLoc,
4205 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004206 if (!AStmt)
4207 return StmtError();
4208
Alexey Bataev4acb8592014-07-07 13:01:15 +00004209 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4210 // 1.2.2 OpenMP Language Terminology
4211 // Structured block - An executable statement with a single entry at the
4212 // top and a single exit at the bottom.
4213 // The point of exit cannot be a branch out of the structured block.
4214 // longjmp() and throw() must not violate the entry/exit criteria.
4215 CS->getCapturedDecl()->setNothrow();
4216
Alexander Musmanc6388682014-12-15 07:07:06 +00004217 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004218 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4219 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004220 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004221 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
4222 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4223 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00004224 if (NestedLoopCount == 0)
4225 return StmtError();
4226
Alexander Musmana5f070a2014-10-01 06:03:56 +00004227 assert((CurContext->isDependentContext() || B.builtAll()) &&
4228 "omp parallel for loop exprs were not built");
4229
Alexey Bataev54acd402015-08-04 11:18:19 +00004230 if (!CurContext->isDependentContext()) {
4231 // Finalize the clauses that need pre-built expressions for CodeGen.
4232 for (auto C : Clauses) {
4233 if (auto LC = dyn_cast<OMPLinearClause>(C))
4234 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4235 B.NumIterations, *this, CurScope))
4236 return StmtError();
4237 }
4238 }
4239
Alexey Bataev4acb8592014-07-07 13:01:15 +00004240 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004241 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004242 NestedLoopCount, Clauses, AStmt, B,
4243 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00004244}
4245
Alexander Musmane4e893b2014-09-23 09:33:00 +00004246StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
4247 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4248 SourceLocation EndLoc,
4249 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004250 if (!AStmt)
4251 return StmtError();
4252
Alexander Musmane4e893b2014-09-23 09:33:00 +00004253 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4254 // 1.2.2 OpenMP Language Terminology
4255 // Structured block - An executable statement with a single entry at the
4256 // top and a single exit at the bottom.
4257 // The point of exit cannot be a branch out of the structured block.
4258 // longjmp() and throw() must not violate the entry/exit criteria.
4259 CS->getCapturedDecl()->setNothrow();
4260
Alexander Musmanc6388682014-12-15 07:07:06 +00004261 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004262 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4263 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00004264 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004265 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
4266 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4267 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004268 if (NestedLoopCount == 0)
4269 return StmtError();
4270
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00004271 if (!CurContext->isDependentContext()) {
4272 // Finalize the clauses that need pre-built expressions for CodeGen.
4273 for (auto C : Clauses) {
4274 if (auto LC = dyn_cast<OMPLinearClause>(C))
4275 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4276 B.NumIterations, *this, CurScope))
4277 return StmtError();
4278 }
4279 }
4280
Alexey Bataev66b15b52015-08-21 11:14:16 +00004281 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4282 // If both simdlen and safelen clauses are specified, the value of the simdlen
4283 // parameter must be less than or equal to the value of the safelen parameter.
4284 OMPSafelenClause *Safelen = nullptr;
4285 OMPSimdlenClause *Simdlen = nullptr;
4286 for (auto *Clause : Clauses) {
4287 if (Clause->getClauseKind() == OMPC_safelen)
4288 Safelen = cast<OMPSafelenClause>(Clause);
4289 else if (Clause->getClauseKind() == OMPC_simdlen)
4290 Simdlen = cast<OMPSimdlenClause>(Clause);
4291 if (Safelen && Simdlen)
4292 break;
4293 }
4294 if (Simdlen && Safelen &&
4295 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4296 Safelen->getSafelen()))
4297 return StmtError();
4298
Alexander Musmane4e893b2014-09-23 09:33:00 +00004299 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004300 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00004301 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004302}
4303
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004304StmtResult
4305Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
4306 Stmt *AStmt, SourceLocation StartLoc,
4307 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004308 if (!AStmt)
4309 return StmtError();
4310
4311 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004312 auto BaseStmt = AStmt;
4313 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4314 BaseStmt = CS->getCapturedStmt();
4315 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4316 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004317 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004318 return StmtError();
4319 // All associated statements must be '#pragma omp section' except for
4320 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004321 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004322 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4323 if (SectionStmt)
4324 Diag(SectionStmt->getLocStart(),
4325 diag::err_omp_parallel_sections_substmt_not_section);
4326 return StmtError();
4327 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004328 cast<OMPSectionDirective>(SectionStmt)
4329 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004330 }
4331 } else {
4332 Diag(AStmt->getLocStart(),
4333 diag::err_omp_parallel_sections_not_compound_stmt);
4334 return StmtError();
4335 }
4336
4337 getCurFunction()->setHasBranchProtectedScope();
4338
Alexey Bataev25e5b442015-09-15 12:52:43 +00004339 return OMPParallelSectionsDirective::Create(
4340 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004341}
4342
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004343StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
4344 Stmt *AStmt, SourceLocation StartLoc,
4345 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004346 if (!AStmt)
4347 return StmtError();
4348
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004349 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4350 // 1.2.2 OpenMP Language Terminology
4351 // Structured block - An executable statement with a single entry at the
4352 // top and a single exit at the bottom.
4353 // The point of exit cannot be a branch out of the structured block.
4354 // longjmp() and throw() must not violate the entry/exit criteria.
4355 CS->getCapturedDecl()->setNothrow();
4356
4357 getCurFunction()->setHasBranchProtectedScope();
4358
Alexey Bataev25e5b442015-09-15 12:52:43 +00004359 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4360 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004361}
4362
Alexey Bataev68446b72014-07-18 07:47:19 +00004363StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
4364 SourceLocation EndLoc) {
4365 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
4366}
4367
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00004368StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
4369 SourceLocation EndLoc) {
4370 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
4371}
4372
Alexey Bataev2df347a2014-07-18 10:17:07 +00004373StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
4374 SourceLocation EndLoc) {
4375 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
4376}
4377
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00004378StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
4379 SourceLocation StartLoc,
4380 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004381 if (!AStmt)
4382 return StmtError();
4383
4384 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00004385
4386 getCurFunction()->setHasBranchProtectedScope();
4387
4388 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
4389}
4390
Alexey Bataev6125da92014-07-21 11:26:11 +00004391StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
4392 SourceLocation StartLoc,
4393 SourceLocation EndLoc) {
4394 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
4395 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
4396}
4397
Alexey Bataev346265e2015-09-25 10:37:12 +00004398StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
4399 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004400 SourceLocation StartLoc,
4401 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004402 if (!AStmt)
4403 return StmtError();
4404
4405 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004406
4407 getCurFunction()->setHasBranchProtectedScope();
4408
Alexey Bataev346265e2015-09-25 10:37:12 +00004409 OMPThreadsClause *TC = nullptr;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004410 OMPSIMDClause *SC = nullptr;
Alexey Bataev346265e2015-09-25 10:37:12 +00004411 for (auto *C: Clauses) {
4412 if (C->getClauseKind() == OMPC_threads)
4413 TC = cast<OMPThreadsClause>(C);
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004414 else if (C->getClauseKind() == OMPC_simd)
4415 SC = cast<OMPSIMDClause>(C);
Alexey Bataev346265e2015-09-25 10:37:12 +00004416 }
4417
4418 // TODO: this must happen only if 'threads' clause specified or if no clauses
4419 // is specified.
4420 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
4421 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
4422 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param) << (TC != nullptr);
4423 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
4424 return StmtError();
4425 }
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004426 if (!SC && isOpenMPSimdDirective(DSAStack->getParentDirective())) {
4427 // OpenMP [2.8.1,simd Construct, Restrictions]
4428 // An ordered construct with the simd clause is the only OpenMP construct
4429 // that can appear in the simd region.
4430 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
4431 return StmtError();
4432 }
Alexey Bataev346265e2015-09-25 10:37:12 +00004433
4434 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004435}
4436
Alexey Bataev1d160b12015-03-13 12:27:31 +00004437namespace {
4438/// \brief Helper class for checking expression in 'omp atomic [update]'
4439/// construct.
4440class OpenMPAtomicUpdateChecker {
4441 /// \brief Error results for atomic update expressions.
4442 enum ExprAnalysisErrorCode {
4443 /// \brief A statement is not an expression statement.
4444 NotAnExpression,
4445 /// \brief Expression is not builtin binary or unary operation.
4446 NotABinaryOrUnaryExpression,
4447 /// \brief Unary operation is not post-/pre- increment/decrement operation.
4448 NotAnUnaryIncDecExpression,
4449 /// \brief An expression is not of scalar type.
4450 NotAScalarType,
4451 /// \brief A binary operation is not an assignment operation.
4452 NotAnAssignmentOp,
4453 /// \brief RHS part of the binary operation is not a binary expression.
4454 NotABinaryExpression,
4455 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
4456 /// expression.
4457 NotABinaryOperator,
4458 /// \brief RHS binary operation does not have reference to the updated LHS
4459 /// part.
4460 NotAnUpdateExpression,
4461 /// \brief No errors is found.
4462 NoError
4463 };
4464 /// \brief Reference to Sema.
4465 Sema &SemaRef;
4466 /// \brief A location for note diagnostics (when error is found).
4467 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004468 /// \brief 'x' lvalue part of the source atomic expression.
4469 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004470 /// \brief 'expr' rvalue part of the source atomic expression.
4471 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004472 /// \brief Helper expression of the form
4473 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
4474 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
4475 Expr *UpdateExpr;
4476 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
4477 /// important for non-associative operations.
4478 bool IsXLHSInRHSPart;
4479 BinaryOperatorKind Op;
4480 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004481 /// \brief true if the source expression is a postfix unary operation, false
4482 /// if it is a prefix unary operation.
4483 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004484
4485public:
4486 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00004487 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00004488 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00004489 /// \brief Check specified statement that it is suitable for 'atomic update'
4490 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00004491 /// expression. If DiagId and NoteId == 0, then only check is performed
4492 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00004493 /// \param DiagId Diagnostic which should be emitted if error is found.
4494 /// \param NoteId Diagnostic note for the main error message.
4495 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00004496 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00004497 /// \brief Return the 'x' lvalue part of the source atomic expression.
4498 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00004499 /// \brief Return the 'expr' rvalue part of the source atomic expression.
4500 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00004501 /// \brief Return the update expression used in calculation of the updated
4502 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
4503 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
4504 Expr *getUpdateExpr() const { return UpdateExpr; }
4505 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
4506 /// false otherwise.
4507 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
4508
Alexey Bataevb78ca832015-04-01 03:33:17 +00004509 /// \brief true if the source expression is a postfix unary operation, false
4510 /// if it is a prefix unary operation.
4511 bool isPostfixUpdate() const { return IsPostfixUpdate; }
4512
Alexey Bataev1d160b12015-03-13 12:27:31 +00004513private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00004514 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
4515 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00004516};
4517} // namespace
4518
4519bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
4520 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
4521 ExprAnalysisErrorCode ErrorFound = NoError;
4522 SourceLocation ErrorLoc, NoteLoc;
4523 SourceRange ErrorRange, NoteRange;
4524 // Allowed constructs are:
4525 // x = x binop expr;
4526 // x = expr binop x;
4527 if (AtomicBinOp->getOpcode() == BO_Assign) {
4528 X = AtomicBinOp->getLHS();
4529 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
4530 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
4531 if (AtomicInnerBinOp->isMultiplicativeOp() ||
4532 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
4533 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00004534 Op = AtomicInnerBinOp->getOpcode();
4535 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00004536 auto *LHS = AtomicInnerBinOp->getLHS();
4537 auto *RHS = AtomicInnerBinOp->getRHS();
4538 llvm::FoldingSetNodeID XId, LHSId, RHSId;
4539 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
4540 /*Canonical=*/true);
4541 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
4542 /*Canonical=*/true);
4543 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
4544 /*Canonical=*/true);
4545 if (XId == LHSId) {
4546 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004547 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004548 } else if (XId == RHSId) {
4549 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004550 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004551 } else {
4552 ErrorLoc = AtomicInnerBinOp->getExprLoc();
4553 ErrorRange = AtomicInnerBinOp->getSourceRange();
4554 NoteLoc = X->getExprLoc();
4555 NoteRange = X->getSourceRange();
4556 ErrorFound = NotAnUpdateExpression;
4557 }
4558 } else {
4559 ErrorLoc = AtomicInnerBinOp->getExprLoc();
4560 ErrorRange = AtomicInnerBinOp->getSourceRange();
4561 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
4562 NoteRange = SourceRange(NoteLoc, NoteLoc);
4563 ErrorFound = NotABinaryOperator;
4564 }
4565 } else {
4566 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
4567 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
4568 ErrorFound = NotABinaryExpression;
4569 }
4570 } else {
4571 ErrorLoc = AtomicBinOp->getExprLoc();
4572 ErrorRange = AtomicBinOp->getSourceRange();
4573 NoteLoc = AtomicBinOp->getOperatorLoc();
4574 NoteRange = SourceRange(NoteLoc, NoteLoc);
4575 ErrorFound = NotAnAssignmentOp;
4576 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00004577 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004578 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
4579 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
4580 return true;
4581 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00004582 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00004583 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004584}
4585
4586bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
4587 unsigned NoteId) {
4588 ExprAnalysisErrorCode ErrorFound = NoError;
4589 SourceLocation ErrorLoc, NoteLoc;
4590 SourceRange ErrorRange, NoteRange;
4591 // Allowed constructs are:
4592 // x++;
4593 // x--;
4594 // ++x;
4595 // --x;
4596 // x binop= expr;
4597 // x = x binop expr;
4598 // x = expr binop x;
4599 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
4600 AtomicBody = AtomicBody->IgnoreParenImpCasts();
4601 if (AtomicBody->getType()->isScalarType() ||
4602 AtomicBody->isInstantiationDependent()) {
4603 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
4604 AtomicBody->IgnoreParenImpCasts())) {
4605 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00004606 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00004607 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00004608 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00004609 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00004610 X = AtomicCompAssignOp->getLHS();
4611 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004612 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
4613 AtomicBody->IgnoreParenImpCasts())) {
4614 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00004615 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
4616 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004617 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00004618 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
4619 // Check for Unary Operation
4620 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004621 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00004622 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
4623 OpLoc = AtomicUnaryOp->getOperatorLoc();
4624 X = AtomicUnaryOp->getSubExpr();
4625 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
4626 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004627 } else {
4628 ErrorFound = NotAnUnaryIncDecExpression;
4629 ErrorLoc = AtomicUnaryOp->getExprLoc();
4630 ErrorRange = AtomicUnaryOp->getSourceRange();
4631 NoteLoc = AtomicUnaryOp->getOperatorLoc();
4632 NoteRange = SourceRange(NoteLoc, NoteLoc);
4633 }
Alexey Bataev5a195472015-09-04 12:55:50 +00004634 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004635 ErrorFound = NotABinaryOrUnaryExpression;
4636 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
4637 NoteRange = ErrorRange = AtomicBody->getSourceRange();
4638 }
4639 } else {
4640 ErrorFound = NotAScalarType;
4641 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
4642 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
4643 }
4644 } else {
4645 ErrorFound = NotAnExpression;
4646 NoteLoc = ErrorLoc = S->getLocStart();
4647 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
4648 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00004649 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004650 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
4651 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
4652 return true;
4653 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00004654 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00004655 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00004656 // Build an update expression of form 'OpaqueValueExpr(x) binop
4657 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
4658 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
4659 auto *OVEX = new (SemaRef.getASTContext())
4660 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
4661 auto *OVEExpr = new (SemaRef.getASTContext())
4662 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
4663 auto Update =
4664 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
4665 IsXLHSInRHSPart ? OVEExpr : OVEX);
4666 if (Update.isInvalid())
4667 return true;
4668 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
4669 Sema::AA_Casting);
4670 if (Update.isInvalid())
4671 return true;
4672 UpdateExpr = Update.get();
4673 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00004674 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004675}
4676
Alexey Bataev0162e452014-07-22 10:10:35 +00004677StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
4678 Stmt *AStmt,
4679 SourceLocation StartLoc,
4680 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004681 if (!AStmt)
4682 return StmtError();
4683
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004684 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00004685 // 1.2.2 OpenMP Language Terminology
4686 // Structured block - An executable statement with a single entry at the
4687 // top and a single exit at the bottom.
4688 // The point of exit cannot be a branch out of the structured block.
4689 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00004690 OpenMPClauseKind AtomicKind = OMPC_unknown;
4691 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004692 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00004693 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00004694 C->getClauseKind() == OMPC_update ||
4695 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00004696 if (AtomicKind != OMPC_unknown) {
4697 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
4698 << SourceRange(C->getLocStart(), C->getLocEnd());
4699 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
4700 << getOpenMPClauseName(AtomicKind);
4701 } else {
4702 AtomicKind = C->getClauseKind();
4703 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004704 }
4705 }
4706 }
Alexey Bataev62cec442014-11-18 10:14:22 +00004707
Alexey Bataev459dec02014-07-24 06:46:57 +00004708 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00004709 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
4710 Body = EWC->getSubExpr();
4711
Alexey Bataev62cec442014-11-18 10:14:22 +00004712 Expr *X = nullptr;
4713 Expr *V = nullptr;
4714 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004715 Expr *UE = nullptr;
4716 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004717 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00004718 // OpenMP [2.12.6, atomic Construct]
4719 // In the next expressions:
4720 // * x and v (as applicable) are both l-value expressions with scalar type.
4721 // * During the execution of an atomic region, multiple syntactic
4722 // occurrences of x must designate the same storage location.
4723 // * Neither of v and expr (as applicable) may access the storage location
4724 // designated by x.
4725 // * Neither of x and expr (as applicable) may access the storage location
4726 // designated by v.
4727 // * expr is an expression with scalar type.
4728 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
4729 // * binop, binop=, ++, and -- are not overloaded operators.
4730 // * The expression x binop expr must be numerically equivalent to x binop
4731 // (expr). This requirement is satisfied if the operators in expr have
4732 // precedence greater than binop, or by using parentheses around expr or
4733 // subexpressions of expr.
4734 // * The expression expr binop x must be numerically equivalent to (expr)
4735 // binop x. This requirement is satisfied if the operators in expr have
4736 // precedence equal to or greater than binop, or by using parentheses around
4737 // expr or subexpressions of expr.
4738 // * For forms that allow multiple occurrences of x, the number of times
4739 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00004740 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004741 enum {
4742 NotAnExpression,
4743 NotAnAssignmentOp,
4744 NotAScalarType,
4745 NotAnLValue,
4746 NoError
4747 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00004748 SourceLocation ErrorLoc, NoteLoc;
4749 SourceRange ErrorRange, NoteRange;
4750 // If clause is read:
4751 // v = x;
4752 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
4753 auto AtomicBinOp =
4754 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
4755 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
4756 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
4757 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
4758 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
4759 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
4760 if (!X->isLValue() || !V->isLValue()) {
4761 auto NotLValueExpr = X->isLValue() ? V : X;
4762 ErrorFound = NotAnLValue;
4763 ErrorLoc = AtomicBinOp->getExprLoc();
4764 ErrorRange = AtomicBinOp->getSourceRange();
4765 NoteLoc = NotLValueExpr->getExprLoc();
4766 NoteRange = NotLValueExpr->getSourceRange();
4767 }
4768 } else if (!X->isInstantiationDependent() ||
4769 !V->isInstantiationDependent()) {
4770 auto NotScalarExpr =
4771 (X->isInstantiationDependent() || X->getType()->isScalarType())
4772 ? V
4773 : X;
4774 ErrorFound = NotAScalarType;
4775 ErrorLoc = AtomicBinOp->getExprLoc();
4776 ErrorRange = AtomicBinOp->getSourceRange();
4777 NoteLoc = NotScalarExpr->getExprLoc();
4778 NoteRange = NotScalarExpr->getSourceRange();
4779 }
Alexey Bataev5a195472015-09-04 12:55:50 +00004780 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00004781 ErrorFound = NotAnAssignmentOp;
4782 ErrorLoc = AtomicBody->getExprLoc();
4783 ErrorRange = AtomicBody->getSourceRange();
4784 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
4785 : AtomicBody->getExprLoc();
4786 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
4787 : AtomicBody->getSourceRange();
4788 }
4789 } else {
4790 ErrorFound = NotAnExpression;
4791 NoteLoc = ErrorLoc = Body->getLocStart();
4792 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00004793 }
Alexey Bataev62cec442014-11-18 10:14:22 +00004794 if (ErrorFound != NoError) {
4795 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
4796 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00004797 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
4798 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00004799 return StmtError();
4800 } else if (CurContext->isDependentContext())
4801 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00004802 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004803 enum {
4804 NotAnExpression,
4805 NotAnAssignmentOp,
4806 NotAScalarType,
4807 NotAnLValue,
4808 NoError
4809 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00004810 SourceLocation ErrorLoc, NoteLoc;
4811 SourceRange ErrorRange, NoteRange;
4812 // If clause is write:
4813 // x = expr;
4814 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
4815 auto AtomicBinOp =
4816 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
4817 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00004818 X = AtomicBinOp->getLHS();
4819 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00004820 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
4821 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
4822 if (!X->isLValue()) {
4823 ErrorFound = NotAnLValue;
4824 ErrorLoc = AtomicBinOp->getExprLoc();
4825 ErrorRange = AtomicBinOp->getSourceRange();
4826 NoteLoc = X->getExprLoc();
4827 NoteRange = X->getSourceRange();
4828 }
4829 } else if (!X->isInstantiationDependent() ||
4830 !E->isInstantiationDependent()) {
4831 auto NotScalarExpr =
4832 (X->isInstantiationDependent() || X->getType()->isScalarType())
4833 ? E
4834 : X;
4835 ErrorFound = NotAScalarType;
4836 ErrorLoc = AtomicBinOp->getExprLoc();
4837 ErrorRange = AtomicBinOp->getSourceRange();
4838 NoteLoc = NotScalarExpr->getExprLoc();
4839 NoteRange = NotScalarExpr->getSourceRange();
4840 }
Alexey Bataev5a195472015-09-04 12:55:50 +00004841 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00004842 ErrorFound = NotAnAssignmentOp;
4843 ErrorLoc = AtomicBody->getExprLoc();
4844 ErrorRange = AtomicBody->getSourceRange();
4845 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
4846 : AtomicBody->getExprLoc();
4847 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
4848 : AtomicBody->getSourceRange();
4849 }
4850 } else {
4851 ErrorFound = NotAnExpression;
4852 NoteLoc = ErrorLoc = Body->getLocStart();
4853 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00004854 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00004855 if (ErrorFound != NoError) {
4856 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
4857 << ErrorRange;
4858 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
4859 << NoteRange;
4860 return StmtError();
4861 } else if (CurContext->isDependentContext())
4862 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00004863 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004864 // If clause is update:
4865 // x++;
4866 // x--;
4867 // ++x;
4868 // --x;
4869 // x binop= expr;
4870 // x = x binop expr;
4871 // x = expr binop x;
4872 OpenMPAtomicUpdateChecker Checker(*this);
4873 if (Checker.checkStatement(
4874 Body, (AtomicKind == OMPC_update)
4875 ? diag::err_omp_atomic_update_not_expression_statement
4876 : diag::err_omp_atomic_not_expression_statement,
4877 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00004878 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00004879 if (!CurContext->isDependentContext()) {
4880 E = Checker.getExpr();
4881 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00004882 UE = Checker.getUpdateExpr();
4883 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00004884 }
Alexey Bataev459dec02014-07-24 06:46:57 +00004885 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004886 enum {
4887 NotAnAssignmentOp,
4888 NotACompoundStatement,
4889 NotTwoSubstatements,
4890 NotASpecificExpression,
4891 NoError
4892 } ErrorFound = NoError;
4893 SourceLocation ErrorLoc, NoteLoc;
4894 SourceRange ErrorRange, NoteRange;
4895 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
4896 // If clause is a capture:
4897 // v = x++;
4898 // v = x--;
4899 // v = ++x;
4900 // v = --x;
4901 // v = x binop= expr;
4902 // v = x = x binop expr;
4903 // v = x = expr binop x;
4904 auto *AtomicBinOp =
4905 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
4906 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
4907 V = AtomicBinOp->getLHS();
4908 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
4909 OpenMPAtomicUpdateChecker Checker(*this);
4910 if (Checker.checkStatement(
4911 Body, diag::err_omp_atomic_capture_not_expression_statement,
4912 diag::note_omp_atomic_update))
4913 return StmtError();
4914 E = Checker.getExpr();
4915 X = Checker.getX();
4916 UE = Checker.getUpdateExpr();
4917 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
4918 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00004919 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004920 ErrorLoc = AtomicBody->getExprLoc();
4921 ErrorRange = AtomicBody->getSourceRange();
4922 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
4923 : AtomicBody->getExprLoc();
4924 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
4925 : AtomicBody->getSourceRange();
4926 ErrorFound = NotAnAssignmentOp;
4927 }
4928 if (ErrorFound != NoError) {
4929 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
4930 << ErrorRange;
4931 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
4932 return StmtError();
4933 } else if (CurContext->isDependentContext()) {
4934 UE = V = E = X = nullptr;
4935 }
4936 } else {
4937 // If clause is a capture:
4938 // { v = x; x = expr; }
4939 // { v = x; x++; }
4940 // { v = x; x--; }
4941 // { v = x; ++x; }
4942 // { v = x; --x; }
4943 // { v = x; x binop= expr; }
4944 // { v = x; x = x binop expr; }
4945 // { v = x; x = expr binop x; }
4946 // { x++; v = x; }
4947 // { x--; v = x; }
4948 // { ++x; v = x; }
4949 // { --x; v = x; }
4950 // { x binop= expr; v = x; }
4951 // { x = x binop expr; v = x; }
4952 // { x = expr binop x; v = x; }
4953 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
4954 // Check that this is { expr1; expr2; }
4955 if (CS->size() == 2) {
4956 auto *First = CS->body_front();
4957 auto *Second = CS->body_back();
4958 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
4959 First = EWC->getSubExpr()->IgnoreParenImpCasts();
4960 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
4961 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
4962 // Need to find what subexpression is 'v' and what is 'x'.
4963 OpenMPAtomicUpdateChecker Checker(*this);
4964 bool IsUpdateExprFound = !Checker.checkStatement(Second);
4965 BinaryOperator *BinOp = nullptr;
4966 if (IsUpdateExprFound) {
4967 BinOp = dyn_cast<BinaryOperator>(First);
4968 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
4969 }
4970 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
4971 // { v = x; x++; }
4972 // { v = x; x--; }
4973 // { v = x; ++x; }
4974 // { v = x; --x; }
4975 // { v = x; x binop= expr; }
4976 // { v = x; x = x binop expr; }
4977 // { v = x; x = expr binop x; }
4978 // Check that the first expression has form v = x.
4979 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
4980 llvm::FoldingSetNodeID XId, PossibleXId;
4981 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
4982 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
4983 IsUpdateExprFound = XId == PossibleXId;
4984 if (IsUpdateExprFound) {
4985 V = BinOp->getLHS();
4986 X = Checker.getX();
4987 E = Checker.getExpr();
4988 UE = Checker.getUpdateExpr();
4989 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00004990 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004991 }
4992 }
4993 if (!IsUpdateExprFound) {
4994 IsUpdateExprFound = !Checker.checkStatement(First);
4995 BinOp = nullptr;
4996 if (IsUpdateExprFound) {
4997 BinOp = dyn_cast<BinaryOperator>(Second);
4998 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
4999 }
5000 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5001 // { x++; v = x; }
5002 // { x--; v = x; }
5003 // { ++x; v = x; }
5004 // { --x; v = x; }
5005 // { x binop= expr; v = x; }
5006 // { x = x binop expr; v = x; }
5007 // { x = expr binop x; v = x; }
5008 // Check that the second expression has form v = x.
5009 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5010 llvm::FoldingSetNodeID XId, PossibleXId;
5011 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5012 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5013 IsUpdateExprFound = XId == PossibleXId;
5014 if (IsUpdateExprFound) {
5015 V = BinOp->getLHS();
5016 X = Checker.getX();
5017 E = Checker.getExpr();
5018 UE = Checker.getUpdateExpr();
5019 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005020 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005021 }
5022 }
5023 }
5024 if (!IsUpdateExprFound) {
5025 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00005026 auto *FirstExpr = dyn_cast<Expr>(First);
5027 auto *SecondExpr = dyn_cast<Expr>(Second);
5028 if (!FirstExpr || !SecondExpr ||
5029 !(FirstExpr->isInstantiationDependent() ||
5030 SecondExpr->isInstantiationDependent())) {
5031 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
5032 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005033 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00005034 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
5035 : First->getLocStart();
5036 NoteRange = ErrorRange = FirstBinOp
5037 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00005038 : SourceRange(ErrorLoc, ErrorLoc);
5039 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005040 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
5041 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
5042 ErrorFound = NotAnAssignmentOp;
5043 NoteLoc = ErrorLoc = SecondBinOp
5044 ? SecondBinOp->getOperatorLoc()
5045 : Second->getLocStart();
5046 NoteRange = ErrorRange =
5047 SecondBinOp ? SecondBinOp->getSourceRange()
5048 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00005049 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005050 auto *PossibleXRHSInFirst =
5051 FirstBinOp->getRHS()->IgnoreParenImpCasts();
5052 auto *PossibleXLHSInSecond =
5053 SecondBinOp->getLHS()->IgnoreParenImpCasts();
5054 llvm::FoldingSetNodeID X1Id, X2Id;
5055 PossibleXRHSInFirst->Profile(X1Id, Context,
5056 /*Canonical=*/true);
5057 PossibleXLHSInSecond->Profile(X2Id, Context,
5058 /*Canonical=*/true);
5059 IsUpdateExprFound = X1Id == X2Id;
5060 if (IsUpdateExprFound) {
5061 V = FirstBinOp->getLHS();
5062 X = SecondBinOp->getLHS();
5063 E = SecondBinOp->getRHS();
5064 UE = nullptr;
5065 IsXLHSInRHSPart = false;
5066 IsPostfixUpdate = true;
5067 } else {
5068 ErrorFound = NotASpecificExpression;
5069 ErrorLoc = FirstBinOp->getExprLoc();
5070 ErrorRange = FirstBinOp->getSourceRange();
5071 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
5072 NoteRange = SecondBinOp->getRHS()->getSourceRange();
5073 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005074 }
5075 }
5076 }
5077 }
5078 } else {
5079 NoteLoc = ErrorLoc = Body->getLocStart();
5080 NoteRange = ErrorRange =
5081 SourceRange(Body->getLocStart(), Body->getLocStart());
5082 ErrorFound = NotTwoSubstatements;
5083 }
5084 } else {
5085 NoteLoc = ErrorLoc = Body->getLocStart();
5086 NoteRange = ErrorRange =
5087 SourceRange(Body->getLocStart(), Body->getLocStart());
5088 ErrorFound = NotACompoundStatement;
5089 }
5090 if (ErrorFound != NoError) {
5091 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
5092 << ErrorRange;
5093 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5094 return StmtError();
5095 } else if (CurContext->isDependentContext()) {
5096 UE = V = E = X = nullptr;
5097 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005098 }
Alexey Bataevdea47612014-07-23 07:46:59 +00005099 }
Alexey Bataev0162e452014-07-22 10:10:35 +00005100
5101 getCurFunction()->setHasBranchProtectedScope();
5102
Alexey Bataev62cec442014-11-18 10:14:22 +00005103 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00005104 X, V, E, UE, IsXLHSInRHSPart,
5105 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00005106}
5107
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005108StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
5109 Stmt *AStmt,
5110 SourceLocation StartLoc,
5111 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005112 if (!AStmt)
5113 return StmtError();
5114
Samuel Antao4af1b7b2015-12-02 17:44:43 +00005115 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5116 // 1.2.2 OpenMP Language Terminology
5117 // Structured block - An executable statement with a single entry at the
5118 // top and a single exit at the bottom.
5119 // The point of exit cannot be a branch out of the structured block.
5120 // longjmp() and throw() must not violate the entry/exit criteria.
5121 CS->getCapturedDecl()->setNothrow();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005122
Alexey Bataev13314bf2014-10-09 04:18:56 +00005123 // OpenMP [2.16, Nesting of Regions]
5124 // If specified, a teams construct must be contained within a target
5125 // construct. That target construct must contain no statements or directives
5126 // outside of the teams construct.
5127 if (DSAStack->hasInnerTeamsRegion()) {
5128 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
5129 bool OMPTeamsFound = true;
5130 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
5131 auto I = CS->body_begin();
5132 while (I != CS->body_end()) {
5133 auto OED = dyn_cast<OMPExecutableDirective>(*I);
5134 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
5135 OMPTeamsFound = false;
5136 break;
5137 }
5138 ++I;
5139 }
5140 assert(I != CS->body_end() && "Not found statement");
5141 S = *I;
5142 }
5143 if (!OMPTeamsFound) {
5144 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
5145 Diag(DSAStack->getInnerTeamsRegionLoc(),
5146 diag::note_omp_nested_teams_construct_here);
5147 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
5148 << isa<OMPExecutableDirective>(S);
5149 return StmtError();
5150 }
5151 }
5152
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005153 getCurFunction()->setHasBranchProtectedScope();
5154
5155 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5156}
5157
Michael Wong65f367f2015-07-21 13:44:28 +00005158StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
5159 Stmt *AStmt,
5160 SourceLocation StartLoc,
5161 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005162 if (!AStmt)
5163 return StmtError();
5164
5165 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5166
Michael Wong65f367f2015-07-21 13:44:28 +00005167 getCurFunction()->setHasBranchProtectedScope();
5168
5169 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
5170 AStmt);
5171}
5172
Alexey Bataev13314bf2014-10-09 04:18:56 +00005173StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
5174 Stmt *AStmt, SourceLocation StartLoc,
5175 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005176 if (!AStmt)
5177 return StmtError();
5178
Alexey Bataev13314bf2014-10-09 04:18:56 +00005179 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5180 // 1.2.2 OpenMP Language Terminology
5181 // Structured block - An executable statement with a single entry at the
5182 // top and a single exit at the bottom.
5183 // The point of exit cannot be a branch out of the structured block.
5184 // longjmp() and throw() must not violate the entry/exit criteria.
5185 CS->getCapturedDecl()->setNothrow();
5186
5187 getCurFunction()->setHasBranchProtectedScope();
5188
5189 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5190}
5191
Alexey Bataev6d4ed052015-07-01 06:57:41 +00005192StmtResult
5193Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
5194 SourceLocation EndLoc,
5195 OpenMPDirectiveKind CancelRegion) {
5196 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
5197 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
5198 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
5199 << getOpenMPDirectiveName(CancelRegion);
5200 return StmtError();
5201 }
5202 if (DSAStack->isParentNowaitRegion()) {
5203 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
5204 return StmtError();
5205 }
5206 if (DSAStack->isParentOrderedRegion()) {
5207 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
5208 return StmtError();
5209 }
5210 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
5211 CancelRegion);
5212}
5213
Alexey Bataev87933c72015-09-18 08:07:34 +00005214StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
5215 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00005216 SourceLocation EndLoc,
5217 OpenMPDirectiveKind CancelRegion) {
5218 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
5219 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
5220 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
5221 << getOpenMPDirectiveName(CancelRegion);
5222 return StmtError();
5223 }
5224 if (DSAStack->isParentNowaitRegion()) {
5225 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
5226 return StmtError();
5227 }
5228 if (DSAStack->isParentOrderedRegion()) {
5229 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
5230 return StmtError();
5231 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005232 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00005233 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
5234 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00005235}
5236
Alexey Bataev49f6e782015-12-01 04:18:41 +00005237StmtResult Sema::ActOnOpenMPTaskLoopDirective(
5238 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5239 SourceLocation EndLoc,
5240 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
5241 if (!AStmt)
5242 return StmtError();
5243
5244 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5245 OMPLoopDirective::HelperExprs B;
5246 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5247 // define the nested loops number.
5248 unsigned NestedLoopCount =
5249 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
5250 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5251 VarsWithImplicitDSA, B);
5252 if (NestedLoopCount == 0)
5253 return StmtError();
5254
5255 assert((CurContext->isDependentContext() || B.builtAll()) &&
5256 "omp for loop exprs were not built");
5257
5258 getCurFunction()->setHasBranchProtectedScope();
5259 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
5260 NestedLoopCount, Clauses, AStmt, B);
5261}
5262
Alexey Bataeved09d242014-05-28 05:53:51 +00005263OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005264 SourceLocation StartLoc,
5265 SourceLocation LParenLoc,
5266 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005267 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005268 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00005269 case OMPC_final:
5270 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
5271 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00005272 case OMPC_num_threads:
5273 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
5274 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00005275 case OMPC_safelen:
5276 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
5277 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00005278 case OMPC_simdlen:
5279 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
5280 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00005281 case OMPC_collapse:
5282 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
5283 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005284 case OMPC_ordered:
5285 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
5286 break;
Michael Wonge710d542015-08-07 16:16:36 +00005287 case OMPC_device:
5288 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
5289 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00005290 case OMPC_num_teams:
5291 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
5292 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005293 case OMPC_thread_limit:
5294 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
5295 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00005296 case OMPC_priority:
5297 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
5298 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005299 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005300 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005301 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005302 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005303 case OMPC_private:
5304 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00005305 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005306 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00005307 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00005308 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00005309 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005310 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005311 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00005312 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005313 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005314 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005315 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00005316 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005317 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00005318 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00005319 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00005320 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005321 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005322 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00005323 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005324 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00005325 case OMPC_map:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005326 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005327 llvm_unreachable("Clause is not allowed.");
5328 }
5329 return Res;
5330}
5331
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005332OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
5333 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005334 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005335 SourceLocation NameModifierLoc,
5336 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005337 SourceLocation EndLoc) {
5338 Expr *ValExpr = Condition;
5339 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
5340 !Condition->isInstantiationDependent() &&
5341 !Condition->containsUnexpandedParameterPack()) {
5342 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00005343 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005344 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005345 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005346
Nikola Smiljanic01a75982014-05-29 10:55:11 +00005347 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005348 }
5349
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005350 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
5351 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005352}
5353
Alexey Bataev3778b602014-07-17 07:32:53 +00005354OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
5355 SourceLocation StartLoc,
5356 SourceLocation LParenLoc,
5357 SourceLocation EndLoc) {
5358 Expr *ValExpr = Condition;
5359 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
5360 !Condition->isInstantiationDependent() &&
5361 !Condition->containsUnexpandedParameterPack()) {
5362 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
5363 Condition->getExprLoc(), Condition);
5364 if (Val.isInvalid())
5365 return nullptr;
5366
5367 ValExpr = Val.get();
5368 }
5369
5370 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
5371}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005372ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
5373 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00005374 if (!Op)
5375 return ExprError();
5376
5377 class IntConvertDiagnoser : public ICEConvertDiagnoser {
5378 public:
5379 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00005380 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00005381 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
5382 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005383 return S.Diag(Loc, diag::err_omp_not_integral) << T;
5384 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005385 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
5386 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005387 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
5388 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005389 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
5390 QualType T,
5391 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005392 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
5393 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005394 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
5395 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005396 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00005397 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00005398 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005399 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
5400 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005401 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
5402 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005403 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
5404 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005405 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00005406 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00005407 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005408 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
5409 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005410 llvm_unreachable("conversion functions are permitted");
5411 }
5412 } ConvertDiagnoser;
5413 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
5414}
5415
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005416static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00005417 OpenMPClauseKind CKind,
5418 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005419 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
5420 !ValExpr->isInstantiationDependent()) {
5421 SourceLocation Loc = ValExpr->getExprLoc();
5422 ExprResult Value =
5423 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
5424 if (Value.isInvalid())
5425 return false;
5426
5427 ValExpr = Value.get();
5428 // The expression must evaluate to a non-negative integer value.
5429 llvm::APSInt Result;
5430 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00005431 Result.isSigned() &&
5432 !((!StrictlyPositive && Result.isNonNegative()) ||
5433 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005434 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00005435 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
5436 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005437 return false;
5438 }
5439 }
5440 return true;
5441}
5442
Alexey Bataev568a8332014-03-06 06:15:19 +00005443OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
5444 SourceLocation StartLoc,
5445 SourceLocation LParenLoc,
5446 SourceLocation EndLoc) {
5447 Expr *ValExpr = NumThreads;
Alexey Bataev568a8332014-03-06 06:15:19 +00005448
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005449 // OpenMP [2.5, Restrictions]
5450 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00005451 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
5452 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005453 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00005454
Alexey Bataeved09d242014-05-28 05:53:51 +00005455 return new (Context)
5456 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00005457}
5458
Alexey Bataev62c87d22014-03-21 04:51:18 +00005459ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
5460 OpenMPClauseKind CKind) {
5461 if (!E)
5462 return ExprError();
5463 if (E->isValueDependent() || E->isTypeDependent() ||
5464 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00005465 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00005466 llvm::APSInt Result;
5467 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
5468 if (ICE.isInvalid())
5469 return ExprError();
5470 if (!Result.isStrictlyPositive()) {
5471 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00005472 << getOpenMPClauseName(CKind) << 1 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00005473 return ExprError();
5474 }
Alexander Musman09184fe2014-09-30 05:29:28 +00005475 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
5476 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
5477 << E->getSourceRange();
5478 return ExprError();
5479 }
Alexey Bataev7b6bc882015-11-26 07:50:39 +00005480 if (CKind == OMPC_collapse)
5481 DSAStack->setCollapseNumber(Result.getExtValue());
5482 else if (CKind == OMPC_ordered)
5483 DSAStack->setCollapseNumber(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00005484 return ICE;
5485}
5486
5487OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
5488 SourceLocation LParenLoc,
5489 SourceLocation EndLoc) {
5490 // OpenMP [2.8.1, simd construct, Description]
5491 // The parameter of the safelen clause must be a constant
5492 // positive integer expression.
5493 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
5494 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005495 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00005496 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00005497 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00005498}
5499
Alexey Bataev66b15b52015-08-21 11:14:16 +00005500OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
5501 SourceLocation LParenLoc,
5502 SourceLocation EndLoc) {
5503 // OpenMP [2.8.1, simd construct, Description]
5504 // The parameter of the simdlen clause must be a constant
5505 // positive integer expression.
5506 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
5507 if (Simdlen.isInvalid())
5508 return nullptr;
5509 return new (Context)
5510 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
5511}
5512
Alexander Musman64d33f12014-06-04 07:53:32 +00005513OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
5514 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00005515 SourceLocation LParenLoc,
5516 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00005517 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00005518 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00005519 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00005520 // The parameter of the collapse clause must be a constant
5521 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00005522 ExprResult NumForLoopsResult =
5523 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
5524 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00005525 return nullptr;
5526 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00005527 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00005528}
5529
Alexey Bataev10e775f2015-07-30 11:36:16 +00005530OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
5531 SourceLocation EndLoc,
5532 SourceLocation LParenLoc,
5533 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00005534 // OpenMP [2.7.1, loop construct, Description]
5535 // OpenMP [2.8.1, simd construct, Description]
5536 // OpenMP [2.9.6, distribute construct, Description]
5537 // The parameter of the ordered clause must be a constant
5538 // positive integer expression if any.
5539 if (NumForLoops && LParenLoc.isValid()) {
5540 ExprResult NumForLoopsResult =
5541 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
5542 if (NumForLoopsResult.isInvalid())
5543 return nullptr;
5544 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00005545 } else
5546 NumForLoops = nullptr;
5547 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00005548 return new (Context)
5549 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
5550}
5551
Alexey Bataeved09d242014-05-28 05:53:51 +00005552OMPClause *Sema::ActOnOpenMPSimpleClause(
5553 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
5554 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005555 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005556 switch (Kind) {
5557 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00005558 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00005559 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
5560 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005561 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005562 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00005563 Res = ActOnOpenMPProcBindClause(
5564 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
5565 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005566 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005567 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00005568 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00005569 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00005570 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00005571 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00005572 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005573 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005574 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005575 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00005576 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00005577 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00005578 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00005579 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00005580 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005581 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005582 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005583 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00005584 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005585 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005586 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005587 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00005588 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005589 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00005590 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00005591 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00005592 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005593 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005594 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00005595 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00005596 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005597 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00005598 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00005599 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005600 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00005601 case OMPC_priority:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005602 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005603 llvm_unreachable("Clause is not allowed.");
5604 }
5605 return Res;
5606}
5607
5608OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
5609 SourceLocation KindKwLoc,
5610 SourceLocation StartLoc,
5611 SourceLocation LParenLoc,
5612 SourceLocation EndLoc) {
5613 if (Kind == OMPC_DEFAULT_unknown) {
5614 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005615 static_assert(OMPC_DEFAULT_unknown > 0,
5616 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00005617 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005618 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005619 Values += "'";
5620 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
5621 Values += "'";
5622 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005623 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005624 Values += " or ";
5625 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005626 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005627 break;
5628 default:
5629 Values += Sep;
5630 break;
5631 }
5632 }
5633 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00005634 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005635 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005636 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00005637 switch (Kind) {
5638 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005639 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005640 break;
5641 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005642 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005643 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005644 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005645 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00005646 break;
5647 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005648 return new (Context)
5649 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005650}
5651
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005652OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
5653 SourceLocation KindKwLoc,
5654 SourceLocation StartLoc,
5655 SourceLocation LParenLoc,
5656 SourceLocation EndLoc) {
5657 if (Kind == OMPC_PROC_BIND_unknown) {
5658 std::string Values;
5659 std::string Sep(", ");
5660 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
5661 Values += "'";
5662 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
5663 Values += "'";
5664 switch (i) {
5665 case OMPC_PROC_BIND_unknown - 2:
5666 Values += " or ";
5667 break;
5668 case OMPC_PROC_BIND_unknown - 1:
5669 break;
5670 default:
5671 Values += Sep;
5672 break;
5673 }
5674 }
5675 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00005676 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005677 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005678 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005679 return new (Context)
5680 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005681}
5682
Alexey Bataev56dafe82014-06-20 07:16:17 +00005683OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
5684 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
5685 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005686 SourceLocation ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00005687 SourceLocation EndLoc) {
5688 OMPClause *Res = nullptr;
5689 switch (Kind) {
5690 case OMPC_schedule:
5691 Res = ActOnOpenMPScheduleClause(
5692 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005693 LParenLoc, ArgumentLoc, DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00005694 break;
5695 case OMPC_if:
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005696 Res =
5697 ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument), Expr,
5698 StartLoc, LParenLoc, ArgumentLoc, DelimLoc, EndLoc);
5699 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00005700 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005701 case OMPC_num_threads:
5702 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00005703 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005704 case OMPC_collapse:
5705 case OMPC_default:
5706 case OMPC_proc_bind:
5707 case OMPC_private:
5708 case OMPC_firstprivate:
5709 case OMPC_lastprivate:
5710 case OMPC_shared:
5711 case OMPC_reduction:
5712 case OMPC_linear:
5713 case OMPC_aligned:
5714 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005715 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005716 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00005717 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005718 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005719 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005720 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00005721 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005722 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00005723 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00005724 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00005725 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005726 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005727 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00005728 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00005729 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005730 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00005731 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00005732 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005733 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00005734 case OMPC_priority:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005735 case OMPC_unknown:
5736 llvm_unreachable("Clause is not allowed.");
5737 }
5738 return Res;
5739}
5740
5741OMPClause *Sema::ActOnOpenMPScheduleClause(
5742 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
5743 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
5744 SourceLocation EndLoc) {
5745 if (Kind == OMPC_SCHEDULE_unknown) {
5746 std::string Values;
5747 std::string Sep(", ");
5748 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) {
5749 Values += "'";
5750 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i);
5751 Values += "'";
5752 switch (i) {
5753 case OMPC_SCHEDULE_unknown - 2:
5754 Values += " or ";
5755 break;
5756 case OMPC_SCHEDULE_unknown - 1:
5757 break;
5758 default:
5759 Values += Sep;
5760 break;
5761 }
5762 }
5763 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
5764 << Values << getOpenMPClauseName(OMPC_schedule);
5765 return nullptr;
5766 }
5767 Expr *ValExpr = ChunkSize;
Alexey Bataev040d5402015-05-12 08:35:28 +00005768 Expr *HelperValExpr = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00005769 if (ChunkSize) {
5770 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
5771 !ChunkSize->isInstantiationDependent() &&
5772 !ChunkSize->containsUnexpandedParameterPack()) {
5773 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
5774 ExprResult Val =
5775 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
5776 if (Val.isInvalid())
5777 return nullptr;
5778
5779 ValExpr = Val.get();
5780
5781 // OpenMP [2.7.1, Restrictions]
5782 // chunk_size must be a loop invariant integer expression with a positive
5783 // value.
5784 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00005785 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
5786 if (Result.isSigned() && !Result.isStrictlyPositive()) {
5787 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00005788 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00005789 return nullptr;
5790 }
5791 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
5792 auto *ImpVar = buildVarDecl(*this, ChunkSize->getExprLoc(),
5793 ChunkSize->getType(), ".chunk.");
5794 auto *ImpVarRef = buildDeclRefExpr(*this, ImpVar, ChunkSize->getType(),
5795 ChunkSize->getExprLoc(),
5796 /*RefersToCapture=*/true);
5797 HelperValExpr = ImpVarRef;
Alexey Bataev56dafe82014-06-20 07:16:17 +00005798 }
5799 }
5800 }
5801
5802 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc,
Alexey Bataev040d5402015-05-12 08:35:28 +00005803 EndLoc, Kind, ValExpr, HelperValExpr);
Alexey Bataev56dafe82014-06-20 07:16:17 +00005804}
5805
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005806OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
5807 SourceLocation StartLoc,
5808 SourceLocation EndLoc) {
5809 OMPClause *Res = nullptr;
5810 switch (Kind) {
5811 case OMPC_ordered:
5812 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
5813 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00005814 case OMPC_nowait:
5815 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
5816 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005817 case OMPC_untied:
5818 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
5819 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005820 case OMPC_mergeable:
5821 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
5822 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005823 case OMPC_read:
5824 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
5825 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00005826 case OMPC_write:
5827 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
5828 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00005829 case OMPC_update:
5830 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
5831 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00005832 case OMPC_capture:
5833 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
5834 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005835 case OMPC_seq_cst:
5836 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
5837 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00005838 case OMPC_threads:
5839 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
5840 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005841 case OMPC_simd:
5842 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
5843 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005844 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00005845 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005846 case OMPC_num_threads:
5847 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00005848 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005849 case OMPC_collapse:
5850 case OMPC_schedule:
5851 case OMPC_private:
5852 case OMPC_firstprivate:
5853 case OMPC_lastprivate:
5854 case OMPC_shared:
5855 case OMPC_reduction:
5856 case OMPC_linear:
5857 case OMPC_aligned:
5858 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005859 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005860 case OMPC_default:
5861 case OMPC_proc_bind:
5862 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00005863 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005864 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00005865 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00005866 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00005867 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005868 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00005869 case OMPC_priority:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005870 case OMPC_unknown:
5871 llvm_unreachable("Clause is not allowed.");
5872 }
5873 return Res;
5874}
5875
Alexey Bataev236070f2014-06-20 11:19:47 +00005876OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
5877 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00005878 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00005879 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
5880}
5881
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005882OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
5883 SourceLocation EndLoc) {
5884 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
5885}
5886
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005887OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
5888 SourceLocation EndLoc) {
5889 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
5890}
5891
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005892OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
5893 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005894 return new (Context) OMPReadClause(StartLoc, EndLoc);
5895}
5896
Alexey Bataevdea47612014-07-23 07:46:59 +00005897OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
5898 SourceLocation EndLoc) {
5899 return new (Context) OMPWriteClause(StartLoc, EndLoc);
5900}
5901
Alexey Bataev67a4f222014-07-23 10:25:33 +00005902OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
5903 SourceLocation EndLoc) {
5904 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
5905}
5906
Alexey Bataev459dec02014-07-24 06:46:57 +00005907OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
5908 SourceLocation EndLoc) {
5909 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
5910}
5911
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005912OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
5913 SourceLocation EndLoc) {
5914 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
5915}
5916
Alexey Bataev346265e2015-09-25 10:37:12 +00005917OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
5918 SourceLocation EndLoc) {
5919 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
5920}
5921
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005922OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
5923 SourceLocation EndLoc) {
5924 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
5925}
5926
Alexey Bataevc5e02582014-06-16 07:08:35 +00005927OMPClause *Sema::ActOnOpenMPVarListClause(
5928 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
5929 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
5930 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005931 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Kelvin Li0bff7af2015-11-23 05:32:03 +00005932 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
5933 OpenMPMapClauseKind MapType, SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005934 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005935 switch (Kind) {
5936 case OMPC_private:
5937 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
5938 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005939 case OMPC_firstprivate:
5940 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
5941 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00005942 case OMPC_lastprivate:
5943 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
5944 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00005945 case OMPC_shared:
5946 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
5947 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00005948 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00005949 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
5950 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00005951 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00005952 case OMPC_linear:
5953 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00005954 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00005955 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00005956 case OMPC_aligned:
5957 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
5958 ColonLoc, EndLoc);
5959 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005960 case OMPC_copyin:
5961 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
5962 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00005963 case OMPC_copyprivate:
5964 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
5965 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00005966 case OMPC_flush:
5967 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
5968 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005969 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00005970 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
5971 StartLoc, LParenLoc, EndLoc);
5972 break;
5973 case OMPC_map:
5974 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, DepLinMapLoc, ColonLoc,
5975 VarList, StartLoc, LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005976 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005977 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00005978 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00005979 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00005980 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00005981 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00005982 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005983 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005984 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005985 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005986 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00005987 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005988 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005989 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005990 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005991 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00005992 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00005993 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00005994 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005995 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00005996 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00005997 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005998 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00005999 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006000 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006001 case OMPC_priority:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006002 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006003 llvm_unreachable("Clause is not allowed.");
6004 }
6005 return Res;
6006}
6007
6008OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
6009 SourceLocation StartLoc,
6010 SourceLocation LParenLoc,
6011 SourceLocation EndLoc) {
6012 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00006013 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00006014 for (auto &RefExpr : VarList) {
6015 assert(RefExpr && "NULL expr in OpenMP private clause.");
6016 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006017 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006018 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006019 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006020 continue;
6021 }
6022
Alexey Bataeved09d242014-05-28 05:53:51 +00006023 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006024 // OpenMP [2.1, C/C++]
6025 // A list item is a variable name.
6026 // OpenMP [2.9.3.3, Restrictions, p.1]
6027 // A variable that is part of another variable (as an array or
6028 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00006029 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006030 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006031 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006032 continue;
6033 }
6034 Decl *D = DE->getDecl();
6035 VarDecl *VD = cast<VarDecl>(D);
6036
6037 QualType Type = VD->getType();
6038 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6039 // It will be analyzed later.
6040 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006041 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006042 continue;
6043 }
6044
6045 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
6046 // A variable that appears in a private clause must not have an incomplete
6047 // type or a reference type.
6048 if (RequireCompleteType(ELoc, Type,
6049 diag::err_omp_private_incomplete_type)) {
6050 continue;
6051 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006052 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006053
Alexey Bataev758e55e2013-09-06 18:03:48 +00006054 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6055 // in a Construct]
6056 // Variables with the predetermined data-sharing attributes may not be
6057 // listed in data-sharing attributes clauses, except for the cases
6058 // listed below. For these exceptions only, listing a predetermined
6059 // variable in a data-sharing attribute clause is allowed and overrides
6060 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006061 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006062 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006063 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
6064 << getOpenMPClauseName(OMPC_private);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006065 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006066 continue;
6067 }
6068
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006069 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00006070 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006071 DSAStack->getCurrentDirective() == OMPD_task) {
6072 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
6073 << getOpenMPClauseName(OMPC_private) << Type
6074 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
6075 bool IsDecl =
6076 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6077 Diag(VD->getLocation(),
6078 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6079 << VD;
6080 continue;
6081 }
6082
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006083 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
6084 // A variable of class type (or array thereof) that appears in a private
6085 // clause requires an accessible, unambiguous default constructor for the
6086 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00006087 // Generate helper private variable and initialize it with the default
6088 // value. The address of the original variable is replaced by the address of
6089 // the new private variable in CodeGen. This new variable is not added to
6090 // IdResolver, so the code in the OpenMP region uses original variable for
6091 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006092 Type = Type.getUnqualifiedType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006093 auto VDPrivate = buildVarDecl(*this, DE->getExprLoc(), Type, VD->getName(),
6094 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00006095 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006096 if (VDPrivate->isInvalidDecl())
6097 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006098 auto VDPrivateRefExpr = buildDeclRefExpr(
6099 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev03b340a2014-10-21 03:16:40 +00006100
Alexey Bataev758e55e2013-09-06 18:03:48 +00006101 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006102 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006103 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006104 }
6105
Alexey Bataeved09d242014-05-28 05:53:51 +00006106 if (Vars.empty())
6107 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006108
Alexey Bataev03b340a2014-10-21 03:16:40 +00006109 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
6110 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006111}
6112
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006113namespace {
6114class DiagsUninitializedSeveretyRAII {
6115private:
6116 DiagnosticsEngine &Diags;
6117 SourceLocation SavedLoc;
6118 bool IsIgnored;
6119
6120public:
6121 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
6122 bool IsIgnored)
6123 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
6124 if (!IsIgnored) {
6125 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
6126 /*Map*/ diag::Severity::Ignored, Loc);
6127 }
6128 }
6129 ~DiagsUninitializedSeveretyRAII() {
6130 if (!IsIgnored)
6131 Diags.popMappings(SavedLoc);
6132 }
6133};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00006134}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006135
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006136OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
6137 SourceLocation StartLoc,
6138 SourceLocation LParenLoc,
6139 SourceLocation EndLoc) {
6140 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006141 SmallVector<Expr *, 8> PrivateCopies;
6142 SmallVector<Expr *, 8> Inits;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006143 bool IsImplicitClause =
6144 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
6145 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
6146
Alexey Bataeved09d242014-05-28 05:53:51 +00006147 for (auto &RefExpr : VarList) {
6148 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
6149 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006150 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006151 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006152 PrivateCopies.push_back(nullptr);
6153 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006154 continue;
6155 }
6156
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006157 SourceLocation ELoc =
6158 IsImplicitClause ? ImplicitClauseLoc : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006159 // OpenMP [2.1, C/C++]
6160 // A list item is a variable name.
6161 // OpenMP [2.9.3.3, Restrictions, p.1]
6162 // A variable that is part of another variable (as an array or
6163 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00006164 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006165 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006166 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006167 continue;
6168 }
6169 Decl *D = DE->getDecl();
6170 VarDecl *VD = cast<VarDecl>(D);
6171
6172 QualType Type = VD->getType();
6173 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6174 // It will be analyzed later.
6175 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006176 PrivateCopies.push_back(nullptr);
6177 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006178 continue;
6179 }
6180
6181 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
6182 // A variable that appears in a private clause must not have an incomplete
6183 // type or a reference type.
6184 if (RequireCompleteType(ELoc, Type,
6185 diag::err_omp_firstprivate_incomplete_type)) {
6186 continue;
6187 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006188 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006189
6190 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
6191 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00006192 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006193 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006194 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006195
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006196 // If an implicit firstprivate variable found it was checked already.
6197 if (!IsImplicitClause) {
6198 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006199 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006200 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
6201 // A list item that specifies a given variable may not appear in more
6202 // than one clause on the same directive, except that a variable may be
6203 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006204 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00006205 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006206 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00006207 << getOpenMPClauseName(DVar.CKind)
6208 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006209 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006210 continue;
6211 }
6212
6213 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6214 // in a Construct]
6215 // Variables with the predetermined data-sharing attributes may not be
6216 // listed in data-sharing attributes clauses, except for the cases
6217 // listed below. For these exceptions only, listing a predetermined
6218 // variable in a data-sharing attribute clause is allowed and overrides
6219 // the variable's predetermined data-sharing attributes.
6220 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6221 // in a Construct, C/C++, p.2]
6222 // Variables with const-qualified type having no mutable member may be
6223 // listed in a firstprivate clause, even if they are static data members.
6224 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
6225 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
6226 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00006227 << getOpenMPClauseName(DVar.CKind)
6228 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006229 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006230 continue;
6231 }
6232
Alexey Bataevf29276e2014-06-18 04:14:57 +00006233 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006234 // OpenMP [2.9.3.4, Restrictions, p.2]
6235 // A list item that is private within a parallel region must not appear
6236 // in a firstprivate clause on a worksharing construct if any of the
6237 // worksharing regions arising from the worksharing construct ever bind
6238 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00006239 if (isOpenMPWorksharingDirective(CurrDir) &&
6240 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006241 DVar = DSAStack->getImplicitDSA(VD, true);
6242 if (DVar.CKind != OMPC_shared &&
6243 (isOpenMPParallelDirective(DVar.DKind) ||
6244 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00006245 Diag(ELoc, diag::err_omp_required_access)
6246 << getOpenMPClauseName(OMPC_firstprivate)
6247 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006248 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006249 continue;
6250 }
6251 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006252 // OpenMP [2.9.3.4, Restrictions, p.3]
6253 // A list item that appears in a reduction clause of a parallel construct
6254 // must not appear in a firstprivate clause on a worksharing or task
6255 // construct if any of the worksharing or task regions arising from the
6256 // worksharing or task construct ever bind to any of the parallel regions
6257 // arising from the parallel construct.
6258 // OpenMP [2.9.3.4, Restrictions, p.4]
6259 // A list item that appears in a reduction clause in worksharing
6260 // construct must not appear in a firstprivate clause in a task construct
6261 // encountered during execution of any of the worksharing regions arising
6262 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006263 if (CurrDir == OMPD_task) {
6264 DVar =
6265 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
6266 [](OpenMPDirectiveKind K) -> bool {
6267 return isOpenMPParallelDirective(K) ||
6268 isOpenMPWorksharingDirective(K);
6269 },
6270 false);
6271 if (DVar.CKind == OMPC_reduction &&
6272 (isOpenMPParallelDirective(DVar.DKind) ||
6273 isOpenMPWorksharingDirective(DVar.DKind))) {
6274 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
6275 << getOpenMPDirectiveName(DVar.DKind);
6276 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6277 continue;
6278 }
6279 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006280 }
6281
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006282 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00006283 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006284 DSAStack->getCurrentDirective() == OMPD_task) {
6285 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
6286 << getOpenMPClauseName(OMPC_firstprivate) << Type
6287 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
6288 bool IsDecl =
6289 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6290 Diag(VD->getLocation(),
6291 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6292 << VD;
6293 continue;
6294 }
6295
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006296 Type = Type.getUnqualifiedType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006297 auto VDPrivate = buildVarDecl(*this, ELoc, Type, VD->getName(),
6298 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006299 // Generate helper private variable and initialize it with the value of the
6300 // original variable. The address of the original variable is replaced by
6301 // the address of the new private variable in the CodeGen. This new variable
6302 // is not added to IdResolver, so the code in the OpenMP region uses
6303 // original variable for proper diagnostics and variable capturing.
6304 Expr *VDInitRefExpr = nullptr;
6305 // For arrays generate initializer for single element and replace it by the
6306 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006307 if (Type->isArrayType()) {
6308 auto VDInit =
6309 buildVarDecl(*this, DE->getExprLoc(), ElemType, VD->getName());
6310 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006311 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006312 ElemType = ElemType.getUnqualifiedType();
6313 auto *VDInitTemp = buildVarDecl(*this, DE->getLocStart(), ElemType,
6314 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00006315 InitializedEntity Entity =
6316 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006317 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
6318
6319 InitializationSequence InitSeq(*this, Entity, Kind, Init);
6320 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
6321 if (Result.isInvalid())
6322 VDPrivate->setInvalidDecl();
6323 else
6324 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006325 // Remove temp variable declaration.
6326 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006327 } else {
Alexey Bataev69c62a92015-04-15 04:52:20 +00006328 auto *VDInit =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006329 buildVarDecl(*this, DE->getLocStart(), Type, ".firstprivate.temp");
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006330 VDInitRefExpr =
6331 buildDeclRefExpr(*this, VDInit, DE->getType(), DE->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00006332 AddInitializerToDecl(VDPrivate,
6333 DefaultLvalueConversion(VDInitRefExpr).get(),
6334 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006335 }
6336 if (VDPrivate->isInvalidDecl()) {
6337 if (IsImplicitClause) {
6338 Diag(DE->getExprLoc(),
6339 diag::note_omp_task_predetermined_firstprivate_here);
6340 }
6341 continue;
6342 }
6343 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00006344 auto VDPrivateRefExpr = buildDeclRefExpr(
6345 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006346 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
6347 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006348 PrivateCopies.push_back(VDPrivateRefExpr);
6349 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006350 }
6351
Alexey Bataeved09d242014-05-28 05:53:51 +00006352 if (Vars.empty())
6353 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006354
6355 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006356 Vars, PrivateCopies, Inits);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006357}
6358
Alexander Musman1bb328c2014-06-04 13:06:39 +00006359OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
6360 SourceLocation StartLoc,
6361 SourceLocation LParenLoc,
6362 SourceLocation EndLoc) {
6363 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00006364 SmallVector<Expr *, 8> SrcExprs;
6365 SmallVector<Expr *, 8> DstExprs;
6366 SmallVector<Expr *, 8> AssignmentOps;
Alexander Musman1bb328c2014-06-04 13:06:39 +00006367 for (auto &RefExpr : VarList) {
6368 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
6369 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6370 // It will be analyzed later.
6371 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00006372 SrcExprs.push_back(nullptr);
6373 DstExprs.push_back(nullptr);
6374 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006375 continue;
6376 }
6377
6378 SourceLocation ELoc = RefExpr->getExprLoc();
6379 // OpenMP [2.1, C/C++]
6380 // A list item is a variable name.
6381 // OpenMP [2.14.3.5, Restrictions, p.1]
6382 // A variable that is part of another variable (as an array or structure
6383 // element) cannot appear in a lastprivate clause.
6384 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
6385 if (!DE || !isa<VarDecl>(DE->getDecl())) {
6386 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
6387 continue;
6388 }
6389 Decl *D = DE->getDecl();
6390 VarDecl *VD = cast<VarDecl>(D);
6391
6392 QualType Type = VD->getType();
6393 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6394 // It will be analyzed later.
6395 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00006396 SrcExprs.push_back(nullptr);
6397 DstExprs.push_back(nullptr);
6398 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006399 continue;
6400 }
6401
6402 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
6403 // A variable that appears in a lastprivate clause must not have an
6404 // incomplete type or a reference type.
6405 if (RequireCompleteType(ELoc, Type,
6406 diag::err_omp_lastprivate_incomplete_type)) {
6407 continue;
6408 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006409 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00006410
6411 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
6412 // in a Construct]
6413 // Variables with the predetermined data-sharing attributes may not be
6414 // listed in data-sharing attributes clauses, except for the cases
6415 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006416 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006417 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
6418 DVar.CKind != OMPC_firstprivate &&
6419 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
6420 Diag(ELoc, diag::err_omp_wrong_dsa)
6421 << getOpenMPClauseName(DVar.CKind)
6422 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006423 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006424 continue;
6425 }
6426
Alexey Bataevf29276e2014-06-18 04:14:57 +00006427 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
6428 // OpenMP [2.14.3.5, Restrictions, p.2]
6429 // A list item that is private within a parallel region, or that appears in
6430 // the reduction clause of a parallel construct, must not appear in a
6431 // lastprivate clause on a worksharing construct if any of the corresponding
6432 // worksharing regions ever binds to any of the corresponding parallel
6433 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00006434 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00006435 if (isOpenMPWorksharingDirective(CurrDir) &&
6436 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006437 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006438 if (DVar.CKind != OMPC_shared) {
6439 Diag(ELoc, diag::err_omp_required_access)
6440 << getOpenMPClauseName(OMPC_lastprivate)
6441 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006442 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006443 continue;
6444 }
6445 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00006446 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00006447 // A variable of class type (or array thereof) that appears in a
6448 // lastprivate clause requires an accessible, unambiguous default
6449 // constructor for the class type, unless the list item is also specified
6450 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00006451 // A variable of class type (or array thereof) that appears in a
6452 // lastprivate clause requires an accessible, unambiguous copy assignment
6453 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00006454 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00006455 auto *SrcVD = buildVarDecl(*this, DE->getLocStart(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006456 Type.getUnqualifiedType(), ".lastprivate.src",
6457 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00006458 auto *PseudoSrcExpr = buildDeclRefExpr(
6459 *this, SrcVD, Type.getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00006460 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006461 buildVarDecl(*this, DE->getLocStart(), Type, ".lastprivate.dst",
6462 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00006463 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006464 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00006465 // For arrays generate assignment operation for single element and replace
6466 // it by the original array element in CodeGen.
6467 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
6468 PseudoDstExpr, PseudoSrcExpr);
6469 if (AssignmentOp.isInvalid())
6470 continue;
6471 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
6472 /*DiscardedValue=*/true);
6473 if (AssignmentOp.isInvalid())
6474 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00006475
Alexey Bataev39f915b82015-05-08 10:41:21 +00006476 if (TopDVar.CKind != OMPC_firstprivate)
Alexey Bataevf29276e2014-06-18 04:14:57 +00006477 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006478 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00006479 SrcExprs.push_back(PseudoSrcExpr);
6480 DstExprs.push_back(PseudoDstExpr);
6481 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00006482 }
6483
6484 if (Vars.empty())
6485 return nullptr;
6486
6487 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00006488 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006489}
6490
Alexey Bataev758e55e2013-09-06 18:03:48 +00006491OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
6492 SourceLocation StartLoc,
6493 SourceLocation LParenLoc,
6494 SourceLocation EndLoc) {
6495 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00006496 for (auto &RefExpr : VarList) {
6497 assert(RefExpr && "NULL expr in OpenMP shared clause.");
6498 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00006499 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006500 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006501 continue;
6502 }
6503
Alexey Bataeved09d242014-05-28 05:53:51 +00006504 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00006505 // OpenMP [2.1, C/C++]
6506 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00006507 // OpenMP [2.14.3.2, Restrictions, p.1]
6508 // A variable that is part of another variable (as an array or structure
6509 // element) cannot appear in a shared unless it is a static data member
6510 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00006511 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006512 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006513 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00006514 continue;
6515 }
6516 Decl *D = DE->getDecl();
6517 VarDecl *VD = cast<VarDecl>(D);
6518
6519 QualType Type = VD->getType();
6520 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6521 // It will be analyzed later.
6522 Vars.push_back(DE);
6523 continue;
6524 }
6525
6526 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6527 // in a Construct]
6528 // Variables with the predetermined data-sharing attributes may not be
6529 // listed in data-sharing attributes clauses, except for the cases
6530 // listed below. For these exceptions only, listing a predetermined
6531 // variable in a data-sharing attribute clause is allowed and overrides
6532 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006533 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00006534 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
6535 DVar.RefExpr) {
6536 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
6537 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006538 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006539 continue;
6540 }
6541
6542 DSAStack->addDSA(VD, DE, OMPC_shared);
6543 Vars.push_back(DE);
6544 }
6545
Alexey Bataeved09d242014-05-28 05:53:51 +00006546 if (Vars.empty())
6547 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00006548
6549 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
6550}
6551
Alexey Bataevc5e02582014-06-16 07:08:35 +00006552namespace {
6553class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
6554 DSAStackTy *Stack;
6555
6556public:
6557 bool VisitDeclRefExpr(DeclRefExpr *E) {
6558 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006559 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006560 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
6561 return false;
6562 if (DVar.CKind != OMPC_unknown)
6563 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00006564 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006565 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006566 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00006567 return true;
6568 return false;
6569 }
6570 return false;
6571 }
6572 bool VisitStmt(Stmt *S) {
6573 for (auto Child : S->children()) {
6574 if (Child && Visit(Child))
6575 return true;
6576 }
6577 return false;
6578 }
Alexey Bataev23b69422014-06-18 07:08:49 +00006579 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00006580};
Alexey Bataev23b69422014-06-18 07:08:49 +00006581} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00006582
6583OMPClause *Sema::ActOnOpenMPReductionClause(
6584 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
6585 SourceLocation ColonLoc, SourceLocation EndLoc,
6586 CXXScopeSpec &ReductionIdScopeSpec,
6587 const DeclarationNameInfo &ReductionId) {
6588 // TODO: Allow scope specification search when 'declare reduction' is
6589 // supported.
6590 assert(ReductionIdScopeSpec.isEmpty() &&
6591 "No support for scoped reduction identifiers yet.");
6592
6593 auto DN = ReductionId.getName();
6594 auto OOK = DN.getCXXOverloadedOperator();
6595 BinaryOperatorKind BOK = BO_Comma;
6596
6597 // OpenMP [2.14.3.6, reduction clause]
6598 // C
6599 // reduction-identifier is either an identifier or one of the following
6600 // operators: +, -, *, &, |, ^, && and ||
6601 // C++
6602 // reduction-identifier is either an id-expression or one of the following
6603 // operators: +, -, *, &, |, ^, && and ||
6604 // FIXME: Only 'min' and 'max' identifiers are supported for now.
6605 switch (OOK) {
6606 case OO_Plus:
6607 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006608 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006609 break;
6610 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006611 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006612 break;
6613 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006614 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006615 break;
6616 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006617 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006618 break;
6619 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006620 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006621 break;
6622 case OO_AmpAmp:
6623 BOK = BO_LAnd;
6624 break;
6625 case OO_PipePipe:
6626 BOK = BO_LOr;
6627 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006628 case OO_New:
6629 case OO_Delete:
6630 case OO_Array_New:
6631 case OO_Array_Delete:
6632 case OO_Slash:
6633 case OO_Percent:
6634 case OO_Tilde:
6635 case OO_Exclaim:
6636 case OO_Equal:
6637 case OO_Less:
6638 case OO_Greater:
6639 case OO_LessEqual:
6640 case OO_GreaterEqual:
6641 case OO_PlusEqual:
6642 case OO_MinusEqual:
6643 case OO_StarEqual:
6644 case OO_SlashEqual:
6645 case OO_PercentEqual:
6646 case OO_CaretEqual:
6647 case OO_AmpEqual:
6648 case OO_PipeEqual:
6649 case OO_LessLess:
6650 case OO_GreaterGreater:
6651 case OO_LessLessEqual:
6652 case OO_GreaterGreaterEqual:
6653 case OO_EqualEqual:
6654 case OO_ExclaimEqual:
6655 case OO_PlusPlus:
6656 case OO_MinusMinus:
6657 case OO_Comma:
6658 case OO_ArrowStar:
6659 case OO_Arrow:
6660 case OO_Call:
6661 case OO_Subscript:
6662 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +00006663 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006664 case NUM_OVERLOADED_OPERATORS:
6665 llvm_unreachable("Unexpected reduction identifier");
6666 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006667 if (auto II = DN.getAsIdentifierInfo()) {
6668 if (II->isStr("max"))
6669 BOK = BO_GT;
6670 else if (II->isStr("min"))
6671 BOK = BO_LT;
6672 }
6673 break;
6674 }
6675 SourceRange ReductionIdRange;
6676 if (ReductionIdScopeSpec.isValid()) {
6677 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
6678 }
6679 ReductionIdRange.setEnd(ReductionId.getEndLoc());
6680 if (BOK == BO_Comma) {
6681 // Not allowed reduction identifier is found.
6682 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
6683 << ReductionIdRange;
6684 return nullptr;
6685 }
6686
6687 SmallVector<Expr *, 8> Vars;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006688 SmallVector<Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006689 SmallVector<Expr *, 8> LHSs;
6690 SmallVector<Expr *, 8> RHSs;
6691 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006692 for (auto RefExpr : VarList) {
6693 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
6694 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6695 // It will be analyzed later.
6696 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006697 Privates.push_back(nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006698 LHSs.push_back(nullptr);
6699 RHSs.push_back(nullptr);
6700 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006701 continue;
6702 }
6703
6704 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
6705 RefExpr->isInstantiationDependent() ||
6706 RefExpr->containsUnexpandedParameterPack()) {
6707 // It will be analyzed later.
6708 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006709 Privates.push_back(nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006710 LHSs.push_back(nullptr);
6711 RHSs.push_back(nullptr);
6712 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006713 continue;
6714 }
6715
6716 auto ELoc = RefExpr->getExprLoc();
6717 auto ERange = RefExpr->getSourceRange();
6718 // OpenMP [2.1, C/C++]
6719 // A list item is a variable or array section, subject to the restrictions
6720 // specified in Section 2.4 on page 42 and in each of the sections
6721 // describing clauses and directives for which a list appears.
6722 // OpenMP [2.14.3.3, Restrictions, p.1]
6723 // A variable that is part of another variable (as an array or
6724 // structure element) cannot appear in a private clause.
Alexey Bataeva1764212015-09-30 09:22:36 +00006725 auto *DE = dyn_cast<DeclRefExpr>(RefExpr);
6726 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr);
6727 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr);
6728 if (!ASE && !OASE && (!DE || !isa<VarDecl>(DE->getDecl()))) {
6729 Diag(ELoc, diag::err_omp_expected_var_name_or_array_item) << ERange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006730 continue;
6731 }
Alexey Bataeva1764212015-09-30 09:22:36 +00006732 QualType Type;
6733 VarDecl *VD = nullptr;
6734 if (DE) {
6735 auto D = DE->getDecl();
6736 VD = cast<VarDecl>(D);
6737 Type = VD->getType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006738 } else if (ASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00006739 Type = ASE->getType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006740 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
6741 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
6742 Base = TempASE->getBase()->IgnoreParenImpCasts();
6743 DE = dyn_cast<DeclRefExpr>(Base);
6744 if (DE)
6745 VD = dyn_cast<VarDecl>(DE->getDecl());
6746 if (!VD) {
6747 Diag(Base->getExprLoc(), diag::err_omp_expected_base_var_name)
6748 << 0 << Base->getSourceRange();
6749 continue;
6750 }
6751 } else if (OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00006752 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
6753 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
6754 Type = ATy->getElementType();
6755 else
6756 Type = BaseType->getPointeeType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006757 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
6758 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
6759 Base = TempOASE->getBase()->IgnoreParenImpCasts();
6760 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
6761 Base = TempASE->getBase()->IgnoreParenImpCasts();
6762 DE = dyn_cast<DeclRefExpr>(Base);
6763 if (DE)
6764 VD = dyn_cast<VarDecl>(DE->getDecl());
6765 if (!VD) {
6766 Diag(Base->getExprLoc(), diag::err_omp_expected_base_var_name)
6767 << 1 << Base->getSourceRange();
6768 continue;
6769 }
Alexey Bataeva1764212015-09-30 09:22:36 +00006770 }
6771
Alexey Bataevc5e02582014-06-16 07:08:35 +00006772 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
6773 // A variable that appears in a private clause must not have an incomplete
6774 // type or a reference type.
6775 if (RequireCompleteType(ELoc, Type,
6776 diag::err_omp_reduction_incomplete_type))
6777 continue;
6778 // OpenMP [2.14.3.6, reduction clause, Restrictions]
6779 // Arrays may not appear in a reduction clause.
6780 if (Type.getNonReferenceType()->isArrayType()) {
6781 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006782 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00006783 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
6784 VarDecl::DeclarationOnly;
6785 Diag(VD->getLocation(),
6786 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6787 << VD;
6788 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00006789 continue;
6790 }
6791 // OpenMP [2.14.3.6, reduction clause, Restrictions]
6792 // A list item that appears in a reduction clause must not be
6793 // const-qualified.
6794 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataeva1764212015-09-30 09:22:36 +00006795 Diag(ELoc, diag::err_omp_const_reduction_list_item)
Alexey Bataevc5e02582014-06-16 07:08:35 +00006796 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006797 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00006798 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
6799 VarDecl::DeclarationOnly;
6800 Diag(VD->getLocation(),
6801 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6802 << VD;
6803 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00006804 continue;
6805 }
6806 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
6807 // If a list-item is a reference type then it must bind to the same object
6808 // for all threads of the team.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006809 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00006810 VarDecl *VDDef = VD->getDefinition();
6811 if (Type->isReferenceType() && VDDef) {
6812 DSARefChecker Check(DSAStack);
6813 if (Check.Visit(VDDef->getInit())) {
6814 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
6815 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
6816 continue;
6817 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00006818 }
6819 }
6820 // OpenMP [2.14.3.6, reduction clause, Restrictions]
6821 // The type of a list item that appears in a reduction clause must be valid
6822 // for the reduction-identifier. For a max or min reduction in C, the type
6823 // of the list item must be an allowed arithmetic data type: char, int,
6824 // float, double, or _Bool, possibly modified with long, short, signed, or
6825 // unsigned. For a max or min reduction in C++, the type of the list item
6826 // must be an allowed arithmetic data type: char, wchar_t, int, float,
6827 // double, or bool, possibly modified with long, short, signed, or unsigned.
6828 if ((BOK == BO_GT || BOK == BO_LT) &&
6829 !(Type->isScalarType() ||
6830 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
6831 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
6832 << getLangOpts().CPlusPlus;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006833 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00006834 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
6835 VarDecl::DeclarationOnly;
6836 Diag(VD->getLocation(),
6837 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6838 << VD;
6839 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00006840 continue;
6841 }
6842 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
6843 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
6844 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006845 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00006846 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
6847 VarDecl::DeclarationOnly;
6848 Diag(VD->getLocation(),
6849 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6850 << VD;
6851 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00006852 continue;
6853 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00006854 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
6855 // in a Construct]
6856 // Variables with the predetermined data-sharing attributes may not be
6857 // listed in data-sharing attributes clauses, except for the cases
6858 // listed below. For these exceptions only, listing a predetermined
6859 // variable in a data-sharing attribute clause is allowed and overrides
6860 // the variable's predetermined data-sharing attributes.
6861 // OpenMP [2.14.3.6, Restrictions, p.3]
6862 // Any number of reduction clauses can be specified on the directive,
6863 // but a list item can appear only once in the reduction clauses for that
6864 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +00006865 DSAStackTy::DSAVarData DVar;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006866 DVar = DSAStack->getTopDSA(VD, false);
6867 if (DVar.CKind == OMPC_reduction) {
6868 Diag(ELoc, diag::err_omp_once_referenced)
6869 << getOpenMPClauseName(OMPC_reduction);
6870 if (DVar.RefExpr) {
6871 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006872 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006873 } else if (DVar.CKind != OMPC_unknown) {
6874 Diag(ELoc, diag::err_omp_wrong_dsa)
6875 << getOpenMPClauseName(DVar.CKind)
6876 << getOpenMPClauseName(OMPC_reduction);
6877 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6878 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006879 }
6880
6881 // OpenMP [2.14.3.6, Restrictions, p.1]
6882 // A list item that appears in a reduction clause of a worksharing
6883 // construct must be shared in the parallel regions to which any of the
6884 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006885 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
6886 if (isOpenMPWorksharingDirective(CurrDir) &&
6887 !isOpenMPParallelDirective(CurrDir)) {
6888 DVar = DSAStack->getImplicitDSA(VD, true);
6889 if (DVar.CKind != OMPC_shared) {
6890 Diag(ELoc, diag::err_omp_required_access)
6891 << getOpenMPClauseName(OMPC_reduction)
6892 << getOpenMPClauseName(OMPC_shared);
6893 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6894 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +00006895 }
6896 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006897
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006898 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006899 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
6900 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
6901 auto *RHSVD = buildVarDecl(*this, ELoc, Type, VD->getName(),
6902 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
6903 auto PrivateTy = Type;
6904 if (OASE) {
6905 // For array sections only:
6906 // Create pseudo array type for private copy. The size for this array will
6907 // be generated during codegen.
6908 // For array subscripts or single variables Private Ty is the same as Type
6909 // (type of the variable or single array element).
6910 PrivateTy = Context.getVariableArrayType(
6911 Type, new (Context) OpaqueValueExpr(SourceLocation(),
6912 Context.getSizeType(), VK_RValue),
6913 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
6914 }
6915 // Private copy.
6916 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, VD->getName(),
6917 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006918 // Add initializer for private variable.
6919 Expr *Init = nullptr;
6920 switch (BOK) {
6921 case BO_Add:
6922 case BO_Xor:
6923 case BO_Or:
6924 case BO_LOr:
6925 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
6926 if (Type->isScalarType() || Type->isAnyComplexType()) {
6927 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00006928 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006929 break;
6930 case BO_Mul:
6931 case BO_LAnd:
6932 if (Type->isScalarType() || Type->isAnyComplexType()) {
6933 // '*' and '&&' reduction ops - initializer is '1'.
6934 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
6935 }
6936 break;
6937 case BO_And: {
6938 // '&' reduction op - initializer is '~0'.
6939 QualType OrigType = Type;
6940 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) {
6941 Type = ComplexTy->getElementType();
6942 }
6943 if (Type->isRealFloatingType()) {
6944 llvm::APFloat InitValue =
6945 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
6946 /*isIEEE=*/true);
6947 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
6948 Type, ELoc);
6949 } else if (Type->isScalarType()) {
6950 auto Size = Context.getTypeSize(Type);
6951 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
6952 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
6953 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
6954 }
6955 if (Init && OrigType->isAnyComplexType()) {
6956 // Init = 0xFFFF + 0xFFFFi;
6957 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
6958 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
6959 }
6960 Type = OrigType;
6961 break;
6962 }
6963 case BO_LT:
6964 case BO_GT: {
6965 // 'min' reduction op - initializer is 'Largest representable number in
6966 // the reduction list item type'.
6967 // 'max' reduction op - initializer is 'Least representable number in
6968 // the reduction list item type'.
6969 if (Type->isIntegerType() || Type->isPointerType()) {
6970 bool IsSigned = Type->hasSignedIntegerRepresentation();
6971 auto Size = Context.getTypeSize(Type);
6972 QualType IntTy =
6973 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
6974 llvm::APInt InitValue =
6975 (BOK != BO_LT)
6976 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
6977 : llvm::APInt::getMinValue(Size)
6978 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
6979 : llvm::APInt::getMaxValue(Size);
6980 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
6981 if (Type->isPointerType()) {
6982 // Cast to pointer type.
6983 auto CastExpr = BuildCStyleCastExpr(
6984 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
6985 SourceLocation(), Init);
6986 if (CastExpr.isInvalid())
6987 continue;
6988 Init = CastExpr.get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00006989 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006990 } else if (Type->isRealFloatingType()) {
6991 llvm::APFloat InitValue = llvm::APFloat::getLargest(
6992 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
6993 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
6994 Type, ELoc);
6995 }
6996 break;
6997 }
6998 case BO_PtrMemD:
6999 case BO_PtrMemI:
7000 case BO_MulAssign:
7001 case BO_Div:
7002 case BO_Rem:
7003 case BO_Sub:
7004 case BO_Shl:
7005 case BO_Shr:
7006 case BO_LE:
7007 case BO_GE:
7008 case BO_EQ:
7009 case BO_NE:
7010 case BO_AndAssign:
7011 case BO_XorAssign:
7012 case BO_OrAssign:
7013 case BO_Assign:
7014 case BO_AddAssign:
7015 case BO_SubAssign:
7016 case BO_DivAssign:
7017 case BO_RemAssign:
7018 case BO_ShlAssign:
7019 case BO_ShrAssign:
7020 case BO_Comma:
7021 llvm_unreachable("Unexpected reduction operation");
7022 }
7023 if (Init) {
7024 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
7025 /*TypeMayContainAuto=*/false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007026 } else
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007027 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007028 if (!RHSVD->hasInit()) {
7029 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
7030 << ReductionIdRange;
Alexey Bataeva1764212015-09-30 09:22:36 +00007031 if (VD) {
7032 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7033 VarDecl::DeclarationOnly;
7034 Diag(VD->getLocation(),
7035 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7036 << VD;
7037 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007038 continue;
7039 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007040 // Store initializer for single element in private copy. Will be used during
7041 // codegen.
7042 PrivateVD->setInit(RHSVD->getInit());
7043 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataev39f915b82015-05-08 10:41:21 +00007044 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
7045 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007046 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007047 ExprResult ReductionOp =
7048 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
7049 LHSDRE, RHSDRE);
7050 if (ReductionOp.isUsable()) {
Alexey Bataev69a47792015-05-07 03:54:03 +00007051 if (BOK != BO_LT && BOK != BO_GT) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007052 ReductionOp =
7053 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
7054 BO_Assign, LHSDRE, ReductionOp.get());
7055 } else {
7056 auto *ConditionalOp = new (Context) ConditionalOperator(
7057 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
7058 RHSDRE, Type, VK_LValue, OK_Ordinary);
7059 ReductionOp =
7060 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
7061 BO_Assign, LHSDRE, ConditionalOp);
7062 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007063 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00007064 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007065 if (ReductionOp.isInvalid())
7066 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007067
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007068 DSAStack->addDSA(VD, DE, OMPC_reduction);
Alexey Bataeva1764212015-09-30 09:22:36 +00007069 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007070 Privates.push_back(PrivateDRE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007071 LHSs.push_back(LHSDRE);
7072 RHSs.push_back(RHSDRE);
7073 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00007074 }
7075
7076 if (Vars.empty())
7077 return nullptr;
7078
7079 return OMPReductionClause::Create(
7080 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007081 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
7082 LHSs, RHSs, ReductionOps);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007083}
7084
Alexey Bataev182227b2015-08-20 10:54:39 +00007085OMPClause *Sema::ActOnOpenMPLinearClause(
7086 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
7087 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
7088 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00007089 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007090 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00007091 SmallVector<Expr *, 8> Inits;
Alexey Bataev182227b2015-08-20 10:54:39 +00007092 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
7093 LinKind == OMPC_LINEAR_unknown) {
7094 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
7095 LinKind = OMPC_LINEAR_val;
7096 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007097 for (auto &RefExpr : VarList) {
7098 assert(RefExpr && "NULL expr in OpenMP linear clause.");
7099 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00007100 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007101 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007102 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00007103 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00007104 continue;
7105 }
7106
7107 // OpenMP [2.14.3.7, linear clause]
7108 // A list item that appears in a linear clause is subject to the private
7109 // clause semantics described in Section 2.14.3.3 on page 159 except as
7110 // noted. In addition, the value of the new list item on each iteration
7111 // of the associated loop(s) corresponds to the value of the original
7112 // list item before entering the construct plus the logical number of
7113 // the iteration times linear-step.
7114
Alexey Bataeved09d242014-05-28 05:53:51 +00007115 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00007116 // OpenMP [2.1, C/C++]
7117 // A list item is a variable name.
7118 // OpenMP [2.14.3.3, Restrictions, p.1]
7119 // A variable that is part of another variable (as an array or
7120 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00007121 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00007122 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00007123 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00007124 continue;
7125 }
7126
7127 VarDecl *VD = cast<VarDecl>(DE->getDecl());
7128
7129 // OpenMP [2.14.3.7, linear clause]
7130 // A list-item cannot appear in more than one linear clause.
7131 // A list-item that appears in a linear clause cannot appear in any
7132 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007133 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00007134 if (DVar.RefExpr) {
7135 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7136 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007137 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00007138 continue;
7139 }
7140
7141 QualType QType = VD->getType();
7142 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
7143 // It will be analyzed later.
7144 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007145 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00007146 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00007147 continue;
7148 }
7149
7150 // A variable must not have an incomplete type or a reference type.
7151 if (RequireCompleteType(ELoc, QType,
7152 diag::err_omp_linear_incomplete_type)) {
7153 continue;
7154 }
Alexey Bataev1185e192015-08-20 12:15:57 +00007155 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
7156 !QType->isReferenceType()) {
7157 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
7158 << QType << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
7159 continue;
7160 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007161 QType = QType.getNonReferenceType();
Alexander Musman8dba6642014-04-22 13:09:42 +00007162
7163 // A list item must not be const-qualified.
7164 if (QType.isConstant(Context)) {
7165 Diag(ELoc, diag::err_omp_const_variable)
7166 << getOpenMPClauseName(OMPC_linear);
7167 bool IsDecl =
7168 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7169 Diag(VD->getLocation(),
7170 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7171 << VD;
7172 continue;
7173 }
7174
7175 // A list item must be of integral or pointer type.
7176 QType = QType.getUnqualifiedType().getCanonicalType();
7177 const Type *Ty = QType.getTypePtrOrNull();
7178 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
7179 !Ty->isPointerType())) {
7180 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
7181 bool IsDecl =
7182 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7183 Diag(VD->getLocation(),
7184 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7185 << VD;
7186 continue;
7187 }
7188
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007189 // Build private copy of original var.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007190 auto *Private = buildVarDecl(*this, ELoc, QType, VD->getName(),
7191 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007192 auto *PrivateRef = buildDeclRefExpr(
7193 *this, Private, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman3276a272015-03-21 10:12:56 +00007194 // Build var to save initial value.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007195 VarDecl *Init = buildVarDecl(*this, ELoc, QType, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00007196 Expr *InitExpr;
7197 if (LinKind == OMPC_LINEAR_uval)
7198 InitExpr = VD->getInit();
7199 else
7200 InitExpr = DE;
7201 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexander Musman3276a272015-03-21 10:12:56 +00007202 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007203 auto InitRef = buildDeclRefExpr(
7204 *this, Init, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman8dba6642014-04-22 13:09:42 +00007205 DSAStack->addDSA(VD, DE, OMPC_linear);
7206 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007207 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00007208 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00007209 }
7210
7211 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007212 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00007213
7214 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00007215 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00007216 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
7217 !Step->isInstantiationDependent() &&
7218 !Step->containsUnexpandedParameterPack()) {
7219 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00007220 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00007221 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007222 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00007223 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00007224
Alexander Musman3276a272015-03-21 10:12:56 +00007225 // Build var to save the step value.
7226 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007227 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00007228 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007229 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00007230 ExprResult CalcStep =
7231 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007232 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00007233
Alexander Musman8dba6642014-04-22 13:09:42 +00007234 // Warn about zero linear step (it would be probably better specified as
7235 // making corresponding variables 'const').
7236 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00007237 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
7238 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00007239 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
7240 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00007241 if (!IsConstant && CalcStep.isUsable()) {
7242 // Calculate the step beforehand instead of doing this on each iteration.
7243 // (This is not used if the number of iterations may be kfold-ed).
7244 CalcStepExpr = CalcStep.get();
7245 }
Alexander Musman8dba6642014-04-22 13:09:42 +00007246 }
7247
Alexey Bataev182227b2015-08-20 10:54:39 +00007248 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
7249 ColonLoc, EndLoc, Vars, Privates, Inits,
7250 StepExpr, CalcStepExpr);
Alexander Musman3276a272015-03-21 10:12:56 +00007251}
7252
7253static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
7254 Expr *NumIterations, Sema &SemaRef,
7255 Scope *S) {
7256 // Walk the vars and build update/final expressions for the CodeGen.
7257 SmallVector<Expr *, 8> Updates;
7258 SmallVector<Expr *, 8> Finals;
7259 Expr *Step = Clause.getStep();
7260 Expr *CalcStep = Clause.getCalcStep();
7261 // OpenMP [2.14.3.7, linear clause]
7262 // If linear-step is not specified it is assumed to be 1.
7263 if (Step == nullptr)
7264 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
7265 else if (CalcStep)
7266 Step = cast<BinaryOperator>(CalcStep)->getLHS();
7267 bool HasErrors = false;
7268 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007269 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00007270 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00007271 for (auto &RefExpr : Clause.varlists()) {
7272 Expr *InitExpr = *CurInit;
7273
7274 // Build privatized reference to the current linear var.
7275 auto DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00007276 Expr *CapturedRef;
7277 if (LinKind == OMPC_LINEAR_uval)
7278 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
7279 else
7280 CapturedRef =
7281 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
7282 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
7283 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00007284
7285 // Build update: Var = InitExpr + IV * Step
7286 ExprResult Update =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007287 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
Alexander Musman3276a272015-03-21 10:12:56 +00007288 InitExpr, IV, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007289 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
7290 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00007291
7292 // Build final: Var = InitExpr + NumIterations * Step
7293 ExprResult Final =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007294 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
Alexey Bataev39f915b82015-05-08 10:41:21 +00007295 InitExpr, NumIterations, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007296 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
7297 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00007298 if (!Update.isUsable() || !Final.isUsable()) {
7299 Updates.push_back(nullptr);
7300 Finals.push_back(nullptr);
7301 HasErrors = true;
7302 } else {
7303 Updates.push_back(Update.get());
7304 Finals.push_back(Final.get());
7305 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007306 ++CurInit, ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00007307 }
7308 Clause.setUpdates(Updates);
7309 Clause.setFinals(Finals);
7310 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00007311}
7312
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007313OMPClause *Sema::ActOnOpenMPAlignedClause(
7314 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
7315 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
7316
7317 SmallVector<Expr *, 8> Vars;
7318 for (auto &RefExpr : VarList) {
7319 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
7320 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7321 // It will be analyzed later.
7322 Vars.push_back(RefExpr);
7323 continue;
7324 }
7325
7326 SourceLocation ELoc = RefExpr->getExprLoc();
7327 // OpenMP [2.1, C/C++]
7328 // A list item is a variable name.
7329 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
7330 if (!DE || !isa<VarDecl>(DE->getDecl())) {
7331 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
7332 continue;
7333 }
7334
7335 VarDecl *VD = cast<VarDecl>(DE->getDecl());
7336
7337 // OpenMP [2.8.1, simd construct, Restrictions]
7338 // The type of list items appearing in the aligned clause must be
7339 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007340 QualType QType = VD->getType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007341 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007342 const Type *Ty = QType.getTypePtrOrNull();
7343 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
7344 !Ty->isPointerType())) {
7345 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
7346 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
7347 bool IsDecl =
7348 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7349 Diag(VD->getLocation(),
7350 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7351 << VD;
7352 continue;
7353 }
7354
7355 // OpenMP [2.8.1, simd construct, Restrictions]
7356 // A list-item cannot appear in more than one aligned clause.
7357 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
7358 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
7359 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
7360 << getOpenMPClauseName(OMPC_aligned);
7361 continue;
7362 }
7363
7364 Vars.push_back(DE);
7365 }
7366
7367 // OpenMP [2.8.1, simd construct, Description]
7368 // The parameter of the aligned clause, alignment, must be a constant
7369 // positive integer expression.
7370 // If no optional parameter is specified, implementation-defined default
7371 // alignments for SIMD instructions on the target platforms are assumed.
7372 if (Alignment != nullptr) {
7373 ExprResult AlignResult =
7374 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
7375 if (AlignResult.isInvalid())
7376 return nullptr;
7377 Alignment = AlignResult.get();
7378 }
7379 if (Vars.empty())
7380 return nullptr;
7381
7382 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
7383 EndLoc, Vars, Alignment);
7384}
7385
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007386OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
7387 SourceLocation StartLoc,
7388 SourceLocation LParenLoc,
7389 SourceLocation EndLoc) {
7390 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007391 SmallVector<Expr *, 8> SrcExprs;
7392 SmallVector<Expr *, 8> DstExprs;
7393 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00007394 for (auto &RefExpr : VarList) {
7395 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
7396 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007397 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007398 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007399 SrcExprs.push_back(nullptr);
7400 DstExprs.push_back(nullptr);
7401 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007402 continue;
7403 }
7404
Alexey Bataeved09d242014-05-28 05:53:51 +00007405 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007406 // OpenMP [2.1, C/C++]
7407 // A list item is a variable name.
7408 // OpenMP [2.14.4.1, Restrictions, p.1]
7409 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00007410 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007411 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00007412 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007413 continue;
7414 }
7415
7416 Decl *D = DE->getDecl();
7417 VarDecl *VD = cast<VarDecl>(D);
7418
7419 QualType Type = VD->getType();
7420 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
7421 // It will be analyzed later.
7422 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007423 SrcExprs.push_back(nullptr);
7424 DstExprs.push_back(nullptr);
7425 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007426 continue;
7427 }
7428
7429 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
7430 // A list item that appears in a copyin clause must be threadprivate.
7431 if (!DSAStack->isThreadPrivate(VD)) {
7432 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00007433 << getOpenMPClauseName(OMPC_copyin)
7434 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007435 continue;
7436 }
7437
7438 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
7439 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00007440 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007441 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007442 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007443 auto *SrcVD =
7444 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
7445 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007446 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007447 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
7448 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007449 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
7450 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007451 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007452 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007453 // For arrays generate assignment operation for single element and replace
7454 // it by the original array element in CodeGen.
7455 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
7456 PseudoDstExpr, PseudoSrcExpr);
7457 if (AssignmentOp.isInvalid())
7458 continue;
7459 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
7460 /*DiscardedValue=*/true);
7461 if (AssignmentOp.isInvalid())
7462 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007463
7464 DSAStack->addDSA(VD, DE, OMPC_copyin);
7465 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007466 SrcExprs.push_back(PseudoSrcExpr);
7467 DstExprs.push_back(PseudoDstExpr);
7468 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007469 }
7470
Alexey Bataeved09d242014-05-28 05:53:51 +00007471 if (Vars.empty())
7472 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007473
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007474 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
7475 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007476}
7477
Alexey Bataevbae9a792014-06-27 10:37:06 +00007478OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
7479 SourceLocation StartLoc,
7480 SourceLocation LParenLoc,
7481 SourceLocation EndLoc) {
7482 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00007483 SmallVector<Expr *, 8> SrcExprs;
7484 SmallVector<Expr *, 8> DstExprs;
7485 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00007486 for (auto &RefExpr : VarList) {
7487 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
7488 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7489 // It will be analyzed later.
7490 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00007491 SrcExprs.push_back(nullptr);
7492 DstExprs.push_back(nullptr);
7493 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00007494 continue;
7495 }
7496
7497 SourceLocation ELoc = RefExpr->getExprLoc();
7498 // OpenMP [2.1, C/C++]
7499 // A list item is a variable name.
7500 // OpenMP [2.14.4.1, Restrictions, p.1]
7501 // A list item that appears in a copyin clause must be threadprivate.
7502 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
7503 if (!DE || !isa<VarDecl>(DE->getDecl())) {
7504 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
7505 continue;
7506 }
7507
7508 Decl *D = DE->getDecl();
7509 VarDecl *VD = cast<VarDecl>(D);
7510
7511 QualType Type = VD->getType();
7512 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
7513 // It will be analyzed later.
7514 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00007515 SrcExprs.push_back(nullptr);
7516 DstExprs.push_back(nullptr);
7517 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00007518 continue;
7519 }
7520
7521 // OpenMP [2.14.4.2, Restrictions, p.2]
7522 // A list item that appears in a copyprivate clause may not appear in a
7523 // private or firstprivate clause on the single construct.
7524 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007525 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00007526 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
7527 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00007528 Diag(ELoc, diag::err_omp_wrong_dsa)
7529 << getOpenMPClauseName(DVar.CKind)
7530 << getOpenMPClauseName(OMPC_copyprivate);
7531 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7532 continue;
7533 }
7534
7535 // OpenMP [2.11.4.2, Restrictions, p.1]
7536 // All list items that appear in a copyprivate clause must be either
7537 // threadprivate or private in the enclosing context.
7538 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007539 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00007540 if (DVar.CKind == OMPC_shared) {
7541 Diag(ELoc, diag::err_omp_required_access)
7542 << getOpenMPClauseName(OMPC_copyprivate)
7543 << "threadprivate or private in the enclosing context";
7544 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7545 continue;
7546 }
7547 }
7548 }
7549
Alexey Bataev7a3e5852015-05-19 08:19:24 +00007550 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007551 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00007552 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007553 << getOpenMPClauseName(OMPC_copyprivate) << Type
7554 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00007555 bool IsDecl =
7556 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7557 Diag(VD->getLocation(),
7558 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7559 << VD;
7560 continue;
7561 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007562
Alexey Bataevbae9a792014-06-27 10:37:06 +00007563 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
7564 // A variable of class type (or array thereof) that appears in a
7565 // copyin clause requires an accessible, unambiguous copy assignment
7566 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007567 Type = Context.getBaseElementType(Type.getNonReferenceType())
7568 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +00007569 auto *SrcVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007570 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.src",
7571 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00007572 auto *PseudoSrcExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007573 buildDeclRefExpr(*this, SrcVD, Type, DE->getExprLoc());
Alexey Bataev420d45b2015-04-14 05:11:24 +00007574 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007575 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.dst",
7576 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00007577 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007578 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataeva63048e2015-03-23 06:18:07 +00007579 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
7580 PseudoDstExpr, PseudoSrcExpr);
7581 if (AssignmentOp.isInvalid())
7582 continue;
7583 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
7584 /*DiscardedValue=*/true);
7585 if (AssignmentOp.isInvalid())
7586 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00007587
7588 // No need to mark vars as copyprivate, they are already threadprivate or
7589 // implicitly private.
7590 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00007591 SrcExprs.push_back(PseudoSrcExpr);
7592 DstExprs.push_back(PseudoDstExpr);
7593 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +00007594 }
7595
7596 if (Vars.empty())
7597 return nullptr;
7598
Alexey Bataeva63048e2015-03-23 06:18:07 +00007599 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
7600 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00007601}
7602
Alexey Bataev6125da92014-07-21 11:26:11 +00007603OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
7604 SourceLocation StartLoc,
7605 SourceLocation LParenLoc,
7606 SourceLocation EndLoc) {
7607 if (VarList.empty())
7608 return nullptr;
7609
7610 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
7611}
Alexey Bataevdea47612014-07-23 07:46:59 +00007612
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007613OMPClause *
7614Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
7615 SourceLocation DepLoc, SourceLocation ColonLoc,
7616 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
7617 SourceLocation LParenLoc, SourceLocation EndLoc) {
7618 if (DepKind == OMPC_DEPEND_unknown) {
7619 std::string Values;
7620 std::string Sep(", ");
7621 for (unsigned i = 0; i < OMPC_DEPEND_unknown; ++i) {
7622 Values += "'";
7623 Values += getOpenMPSimpleClauseTypeName(OMPC_depend, i);
7624 Values += "'";
7625 switch (i) {
7626 case OMPC_DEPEND_unknown - 2:
7627 Values += " or ";
7628 break;
7629 case OMPC_DEPEND_unknown - 1:
7630 break;
7631 default:
7632 Values += Sep;
7633 break;
7634 }
7635 }
7636 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
7637 << Values << getOpenMPClauseName(OMPC_depend);
7638 return nullptr;
7639 }
7640 SmallVector<Expr *, 8> Vars;
7641 for (auto &RefExpr : VarList) {
7642 assert(RefExpr && "NULL expr in OpenMP shared clause.");
7643 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7644 // It will be analyzed later.
7645 Vars.push_back(RefExpr);
7646 continue;
7647 }
7648
7649 SourceLocation ELoc = RefExpr->getExprLoc();
7650 // OpenMP [2.11.1.1, Restrictions, p.3]
7651 // A variable that is part of another variable (such as a field of a
7652 // structure) but is not an array element or an array section cannot appear
7653 // in a depend clause.
7654 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
Alexey Bataev1a3320e2015-08-25 14:24:04 +00007655 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
7656 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
7657 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
7658 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
7659 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007660 (ASE && !ASE->getBase()->getType()->isAnyPointerType() &&
7661 !ASE->getBase()->getType()->isArrayType())) {
7662 Diag(ELoc, diag::err_omp_expected_var_name_or_array_item)
7663 << RefExpr->getSourceRange();
7664 continue;
7665 }
7666
7667 Vars.push_back(RefExpr->IgnoreParenImpCasts());
7668 }
7669
7670 if (Vars.empty())
7671 return nullptr;
7672
7673 return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
7674 DepLoc, ColonLoc, Vars);
7675}
Michael Wonge710d542015-08-07 16:16:36 +00007676
7677OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
7678 SourceLocation LParenLoc,
7679 SourceLocation EndLoc) {
7680 Expr *ValExpr = Device;
Michael Wonge710d542015-08-07 16:16:36 +00007681
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007682 // OpenMP [2.9.1, Restrictions]
7683 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00007684 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
7685 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007686 return nullptr;
7687
Michael Wonge710d542015-08-07 16:16:36 +00007688 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
7689}
Kelvin Li0bff7af2015-11-23 05:32:03 +00007690
7691static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
7692 DSAStackTy *Stack, CXXRecordDecl *RD) {
7693 if (!RD || RD->isInvalidDecl())
7694 return true;
7695
7696 auto QTy = SemaRef.Context.getRecordType(RD);
7697 if (RD->isDynamicClass()) {
7698 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
7699 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
7700 return false;
7701 }
7702 auto *DC = RD;
7703 bool IsCorrect = true;
7704 for (auto *I : DC->decls()) {
7705 if (I) {
7706 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
7707 if (MD->isStatic()) {
7708 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
7709 SemaRef.Diag(MD->getLocation(),
7710 diag::note_omp_static_member_in_target);
7711 IsCorrect = false;
7712 }
7713 } else if (auto *VD = dyn_cast<VarDecl>(I)) {
7714 if (VD->isStaticDataMember()) {
7715 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
7716 SemaRef.Diag(VD->getLocation(),
7717 diag::note_omp_static_member_in_target);
7718 IsCorrect = false;
7719 }
7720 }
7721 }
7722 }
7723
7724 for (auto &I : RD->bases()) {
7725 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
7726 I.getType()->getAsCXXRecordDecl()))
7727 IsCorrect = false;
7728 }
7729 return IsCorrect;
7730}
7731
7732static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
7733 DSAStackTy *Stack, QualType QTy) {
7734 NamedDecl *ND;
7735 if (QTy->isIncompleteType(&ND)) {
7736 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
7737 return false;
7738 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
7739 if (!RD->isInvalidDecl() &&
7740 !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
7741 return false;
7742 }
7743 return true;
7744}
7745
7746OMPClause *Sema::ActOnOpenMPMapClause(
7747 OpenMPMapClauseKind MapTypeModifier, OpenMPMapClauseKind MapType,
7748 SourceLocation MapLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VarList,
7749 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
7750 SmallVector<Expr *, 4> Vars;
7751
7752 for (auto &RE : VarList) {
7753 assert(RE && "Null expr in omp map");
7754 if (isa<DependentScopeDeclRefExpr>(RE)) {
7755 // It will be analyzed later.
7756 Vars.push_back(RE);
7757 continue;
7758 }
7759 SourceLocation ELoc = RE->getExprLoc();
7760
7761 // OpenMP [2.14.5, Restrictions]
7762 // A variable that is part of another variable (such as field of a
7763 // structure) but is not an array element or an array section cannot appear
7764 // in a map clause.
7765 auto *VE = RE->IgnoreParenLValueCasts();
7766
7767 if (VE->isValueDependent() || VE->isTypeDependent() ||
7768 VE->isInstantiationDependent() ||
7769 VE->containsUnexpandedParameterPack()) {
7770 // It will be analyzed later.
7771 Vars.push_back(RE);
7772 continue;
7773 }
7774
7775 auto *SimpleExpr = RE->IgnoreParenCasts();
7776 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
7777 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
7778 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
7779
7780 if (!RE->IgnoreParenImpCasts()->isLValue() ||
7781 (!OASE && !ASE && !DE) ||
7782 (DE && !isa<VarDecl>(DE->getDecl())) ||
7783 (ASE && !ASE->getBase()->getType()->isAnyPointerType() &&
7784 !ASE->getBase()->getType()->isArrayType())) {
7785 Diag(ELoc, diag::err_omp_expected_var_name_or_array_item)
7786 << RE->getSourceRange();
7787 continue;
7788 }
7789
7790 Decl *D = nullptr;
7791 if (DE) {
7792 D = DE->getDecl();
7793 } else if (ASE) {
7794 auto *B = ASE->getBase()->IgnoreParenCasts();
7795 D = dyn_cast<DeclRefExpr>(B)->getDecl();
7796 } else if (OASE) {
7797 auto *B = OASE->getBase();
7798 D = dyn_cast<DeclRefExpr>(B)->getDecl();
7799 }
7800 assert(D && "Null decl on map clause.");
7801 auto *VD = cast<VarDecl>(D);
7802
7803 // OpenMP [2.14.5, Restrictions, p.8]
7804 // threadprivate variables cannot appear in a map clause.
7805 if (DSAStack->isThreadPrivate(VD)) {
7806 auto DVar = DSAStack->getTopDSA(VD, false);
7807 Diag(ELoc, diag::err_omp_threadprivate_in_map);
7808 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7809 continue;
7810 }
7811
7812 // OpenMP [2.14.5, Restrictions, p.2]
7813 // At most one list item can be an array item derived from a given variable
7814 // in map clauses of the same construct.
7815 // OpenMP [2.14.5, Restrictions, p.3]
7816 // List items of map clauses in the same construct must not share original
7817 // storage.
7818 // OpenMP [2.14.5, Restrictions, C/C++, p.2]
7819 // A variable for which the type is pointer, reference to array, or
7820 // reference to pointer and an array section derived from that variable
7821 // must not appear as list items of map clauses of the same construct.
7822 DSAStackTy::MapInfo MI = DSAStack->IsMappedInCurrentRegion(VD);
7823 if (MI.RefExpr) {
7824 Diag(ELoc, diag::err_omp_map_shared_storage) << ELoc;
7825 Diag(MI.RefExpr->getExprLoc(), diag::note_used_here)
7826 << MI.RefExpr->getSourceRange();
7827 continue;
7828 }
7829
7830 // OpenMP [2.14.5, Restrictions, C/C++, p.3,4]
7831 // A variable for which the type is pointer, reference to array, or
7832 // reference to pointer must not appear as a list item if the enclosing
7833 // device data environment already contains an array section derived from
7834 // that variable.
7835 // An array section derived from a variable for which the type is pointer,
7836 // reference to array, or reference to pointer must not appear as a list
7837 // item if the enclosing device data environment already contains that
7838 // variable.
7839 QualType Type = VD->getType();
7840 MI = DSAStack->getMapInfoForVar(VD);
7841 if (MI.RefExpr && (isa<DeclRefExpr>(MI.RefExpr->IgnoreParenLValueCasts()) !=
7842 isa<DeclRefExpr>(VE)) &&
7843 (Type->isPointerType() || Type->isReferenceType())) {
7844 Diag(ELoc, diag::err_omp_map_shared_storage) << ELoc;
7845 Diag(MI.RefExpr->getExprLoc(), diag::note_used_here)
7846 << MI.RefExpr->getSourceRange();
7847 continue;
7848 }
7849
7850 // OpenMP [2.14.5, Restrictions, C/C++, p.7]
7851 // A list item must have a mappable type.
7852 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), *this,
7853 DSAStack, Type))
7854 continue;
7855
7856 Vars.push_back(RE);
7857 MI.RefExpr = RE;
7858 DSAStack->addMapInfoForVar(VD, MI);
7859 }
7860 if (Vars.empty())
7861 return nullptr;
7862
7863 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
7864 MapTypeModifier, MapType, MapLoc);
7865}
Kelvin Li099bb8c2015-11-24 20:50:12 +00007866
7867OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
7868 SourceLocation StartLoc,
7869 SourceLocation LParenLoc,
7870 SourceLocation EndLoc) {
7871 Expr *ValExpr = NumTeams;
Kelvin Li099bb8c2015-11-24 20:50:12 +00007872
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007873 // OpenMP [teams Constrcut, Restrictions]
7874 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00007875 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
7876 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007877 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +00007878
7879 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
7880}
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007881
7882OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
7883 SourceLocation StartLoc,
7884 SourceLocation LParenLoc,
7885 SourceLocation EndLoc) {
7886 Expr *ValExpr = ThreadLimit;
7887
7888 // OpenMP [teams Constrcut, Restrictions]
7889 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00007890 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
7891 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007892 return nullptr;
7893
7894 return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc,
7895 EndLoc);
7896}
Alexey Bataeva0569352015-12-01 10:17:31 +00007897
7898OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
7899 SourceLocation StartLoc,
7900 SourceLocation LParenLoc,
7901 SourceLocation EndLoc) {
7902 Expr *ValExpr = Priority;
7903
7904 // OpenMP [2.9.1, task Constrcut]
7905 // The priority-value is a non-negative numerical scalar expression.
7906 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
7907 /*StrictlyPositive=*/false))
7908 return nullptr;
7909
7910 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
7911}