blob: 45d56d405f24c976302b728244a88934255e1a36 [file] [log] [blame]
Alexey Bataev9959db52014-05-06 10:08:46 +00001//===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===//
Alexey Bataeva769e072013-03-22 06:34:35 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10/// \brief This file implements semantic analysis for OpenMP directives and
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000011/// clauses.
Alexey Bataeva769e072013-03-22 06:34:35 +000012///
13//===----------------------------------------------------------------------===//
14
Alexey Bataevb08f89f2015-08-14 12:25:37 +000015#include "TreeTransform.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000016#include "clang/AST/ASTContext.h"
Alexey Bataev97720002014-11-11 04:05:39 +000017#include "clang/AST/ASTMutationListener.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000018#include "clang/AST/Decl.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000019#include "clang/AST/DeclCXX.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000020#include "clang/AST/DeclOpenMP.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000021#include "clang/AST/StmtCXX.h"
22#include "clang/AST/StmtOpenMP.h"
23#include "clang/AST/StmtVisitor.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000024#include "clang/Basic/OpenMPKinds.h"
Samuel Antaof8b50122015-07-13 22:54:53 +000025#include "clang/Basic/TargetInfo.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000026#include "clang/Lex/Preprocessor.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000027#include "clang/Sema/Initialization.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000028#include "clang/Sema/Lookup.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000029#include "clang/Sema/Scope.h"
30#include "clang/Sema/ScopeInfo.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000031#include "clang/Sema/SemaInternal.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000032using namespace clang;
33
Alexey Bataev758e55e2013-09-06 18:03:48 +000034//===----------------------------------------------------------------------===//
35// Stack of data-sharing attributes for variables
36//===----------------------------------------------------------------------===//
37
38namespace {
39/// \brief Default data sharing attributes, which can be applied to directive.
40enum DefaultDataSharingAttributes {
Alexey Bataeved09d242014-05-28 05:53:51 +000041 DSA_unspecified = 0, /// \brief Data sharing attribute not specified.
42 DSA_none = 1 << 0, /// \brief Default data sharing attribute 'none'.
43 DSA_shared = 1 << 1 /// \brief Default data sharing attribute 'shared'.
Alexey Bataev758e55e2013-09-06 18:03:48 +000044};
Alexey Bataev7ff55242014-06-19 09:13:45 +000045
Alexey Bataevf29276e2014-06-18 04:14:57 +000046template <class T> struct MatchesAny {
Alexey Bataev23b69422014-06-18 07:08:49 +000047 explicit MatchesAny(ArrayRef<T> Arr) : Arr(std::move(Arr)) {}
Alexey Bataevf29276e2014-06-18 04:14:57 +000048 bool operator()(T Kind) {
49 for (auto KindEl : Arr)
50 if (KindEl == Kind)
51 return true;
52 return false;
53 }
54
55private:
56 ArrayRef<T> Arr;
57};
Alexey Bataev23b69422014-06-18 07:08:49 +000058struct MatchesAlways {
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +000059 MatchesAlways() {}
Alexey Bataev7ff55242014-06-19 09:13:45 +000060 template <class T> bool operator()(T) { return true; }
Alexey Bataevf29276e2014-06-18 04:14:57 +000061};
62
63typedef MatchesAny<OpenMPClauseKind> MatchesAnyClause;
64typedef MatchesAny<OpenMPDirectiveKind> MatchesAnyDirective;
Alexey Bataev758e55e2013-09-06 18:03:48 +000065
66/// \brief Stack for tracking declarations used in OpenMP directives and
67/// clauses and their data-sharing attributes.
68class DSAStackTy {
69public:
70 struct DSAVarData {
71 OpenMPDirectiveKind DKind;
72 OpenMPClauseKind CKind;
73 DeclRefExpr *RefExpr;
Alexey Bataevbae9a792014-06-27 10:37:06 +000074 SourceLocation ImplicitDSALoc;
75 DSAVarData()
76 : DKind(OMPD_unknown), CKind(OMPC_unknown), RefExpr(nullptr),
77 ImplicitDSALoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000078 };
Alexey Bataeved09d242014-05-28 05:53:51 +000079
Kelvin Li0bff7af2015-11-23 05:32:03 +000080public:
81 struct MapInfo {
82 Expr *RefExpr;
83 };
84
Alexey Bataev758e55e2013-09-06 18:03:48 +000085private:
86 struct DSAInfo {
87 OpenMPClauseKind Attributes;
88 DeclRefExpr *RefExpr;
89 };
90 typedef llvm::SmallDenseMap<VarDecl *, DSAInfo, 64> DeclSAMapTy;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000091 typedef llvm::SmallDenseMap<VarDecl *, DeclRefExpr *, 64> AlignedMapTy;
Alexey Bataev9c821032015-04-30 04:23:23 +000092 typedef llvm::DenseSet<VarDecl *> LoopControlVariablesSetTy;
Kelvin Li0bff7af2015-11-23 05:32:03 +000093 typedef llvm::SmallDenseMap<VarDecl *, MapInfo, 64> MappedDeclsTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +000094
95 struct SharingMapTy {
96 DeclSAMapTy SharingMap;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000097 AlignedMapTy AlignedMap;
Kelvin Li0bff7af2015-11-23 05:32:03 +000098 MappedDeclsTy MappedDecls;
Alexey Bataev9c821032015-04-30 04:23:23 +000099 LoopControlVariablesSetTy LCVSet;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000100 DefaultDataSharingAttributes DefaultAttr;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000101 SourceLocation DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000102 OpenMPDirectiveKind Directive;
103 DeclarationNameInfo DirectiveName;
104 Scope *CurScope;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000105 SourceLocation ConstructLoc;
Alexey Bataev346265e2015-09-25 10:37:12 +0000106 /// \brief first argument (Expr *) contains optional argument of the
107 /// 'ordered' clause, the second one is true if the regions has 'ordered'
108 /// clause, false otherwise.
109 llvm::PointerIntPair<Expr *, 1, bool> OrderedRegion;
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000110 bool NowaitRegion;
Alexey Bataev25e5b442015-09-15 12:52:43 +0000111 bool CancelRegion;
Alexey Bataev9c821032015-04-30 04:23:23 +0000112 unsigned CollapseNumber;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000113 SourceLocation InnerTeamsRegionLoc;
Alexey Bataeved09d242014-05-28 05:53:51 +0000114 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000115 Scope *CurScope, SourceLocation Loc)
Alexey Bataev9c821032015-04-30 04:23:23 +0000116 : SharingMap(), AlignedMap(), LCVSet(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000117 Directive(DKind), DirectiveName(std::move(Name)), CurScope(CurScope),
Alexey Bataev346265e2015-09-25 10:37:12 +0000118 ConstructLoc(Loc), OrderedRegion(), NowaitRegion(false),
Alexey Bataev25e5b442015-09-15 12:52:43 +0000119 CancelRegion(false), CollapseNumber(1), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000120 SharingMapTy()
Alexey Bataev9c821032015-04-30 04:23:23 +0000121 : SharingMap(), AlignedMap(), LCVSet(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000122 Directive(OMPD_unknown), DirectiveName(), CurScope(nullptr),
Alexey Bataev346265e2015-09-25 10:37:12 +0000123 ConstructLoc(), OrderedRegion(), NowaitRegion(false),
Alexey Bataev25e5b442015-09-15 12:52:43 +0000124 CancelRegion(false), CollapseNumber(1), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000125 };
126
127 typedef SmallVector<SharingMapTy, 64> StackTy;
128
129 /// \brief Stack of used declaration and their data-sharing attributes.
130 StackTy Stack;
Alexey Bataev39f915b82015-05-08 10:41:21 +0000131 /// \brief true, if check for DSA must be from parent directive, false, if
132 /// from current directive.
Alexey Bataevaac108a2015-06-23 04:51:00 +0000133 OpenMPClauseKind ClauseKindMode;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000134 Sema &SemaRef;
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000135 bool ForceCapturing;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000136
137 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
138
139 DSAVarData getDSA(StackTy::reverse_iterator Iter, VarDecl *D);
Alexey Bataevec3da872014-01-31 05:15:34 +0000140
141 /// \brief Checks if the variable is a local for OpenMP region.
142 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
Alexey Bataeved09d242014-05-28 05:53:51 +0000143
Alexey Bataev758e55e2013-09-06 18:03:48 +0000144public:
Alexey Bataevaac108a2015-06-23 04:51:00 +0000145 explicit DSAStackTy(Sema &S)
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000146 : Stack(1), ClauseKindMode(OMPC_unknown), SemaRef(S),
147 ForceCapturing(false) {}
Alexey Bataev39f915b82015-05-08 10:41:21 +0000148
Alexey Bataevaac108a2015-06-23 04:51:00 +0000149 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
150 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000151
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000152 bool isForceVarCapturing() const { return ForceCapturing; }
153 void setForceVarCapturing(bool V) { ForceCapturing = V; }
154
Alexey Bataev758e55e2013-09-06 18:03:48 +0000155 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000156 Scope *CurScope, SourceLocation Loc) {
157 Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc));
158 Stack.back().DefaultAttrLoc = Loc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000159 }
160
161 void pop() {
162 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
163 Stack.pop_back();
164 }
165
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000166 /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000167 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000168 /// for diagnostics.
169 DeclRefExpr *addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE);
170
Alexey Bataev9c821032015-04-30 04:23:23 +0000171 /// \brief Register specified variable as loop control variable.
172 void addLoopControlVariable(VarDecl *D);
173 /// \brief Check if the specified variable is a loop control variable for
174 /// current region.
175 bool isLoopControlVariable(VarDecl *D);
176
Alexey Bataev758e55e2013-09-06 18:03:48 +0000177 /// \brief Adds explicit data sharing attribute to the specified declaration.
178 void addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A);
179
Alexey Bataev758e55e2013-09-06 18:03:48 +0000180 /// \brief Returns data sharing attributes from top of the stack for the
181 /// specified declaration.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000182 DSAVarData getTopDSA(VarDecl *D, bool FromParent);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000183 /// \brief Returns data-sharing attributes for the specified declaration.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000184 DSAVarData getImplicitDSA(VarDecl *D, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000185 /// \brief Checks if the specified variables has data-sharing attributes which
186 /// match specified \a CPred predicate in any directive which matches \a DPred
187 /// predicate.
188 template <class ClausesPredicate, class DirectivesPredicate>
189 DSAVarData hasDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000190 DirectivesPredicate DPred, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000191 /// \brief Checks if the specified variables has data-sharing attributes which
192 /// match specified \a CPred predicate in any innermost directive which
193 /// matches \a DPred predicate.
194 template <class ClausesPredicate, class DirectivesPredicate>
195 DSAVarData hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000196 DirectivesPredicate DPred,
197 bool FromParent);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000198 /// \brief Checks if the specified variables has explicit data-sharing
199 /// attributes which match specified \a CPred predicate at the specified
200 /// OpenMP region.
201 bool hasExplicitDSA(VarDecl *D,
202 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
203 unsigned Level);
Samuel Antao4be30e92015-10-02 17:14:03 +0000204
205 /// \brief Returns true if the directive at level \Level matches in the
206 /// specified \a DPred predicate.
207 bool hasExplicitDirective(
208 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
209 unsigned Level);
210
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000211 /// \brief Finds a directive which matches specified \a DPred predicate.
212 template <class NamedDirectivesPredicate>
213 bool hasDirective(NamedDirectivesPredicate DPred, bool FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000214
Alexey Bataev758e55e2013-09-06 18:03:48 +0000215 /// \brief Returns currently analyzed directive.
216 OpenMPDirectiveKind getCurrentDirective() const {
217 return Stack.back().Directive;
218 }
Alexey Bataev549210e2014-06-24 04:39:47 +0000219 /// \brief Returns parent directive.
220 OpenMPDirectiveKind getParentDirective() const {
221 if (Stack.size() > 2)
222 return Stack[Stack.size() - 2].Directive;
223 return OMPD_unknown;
224 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000225 /// \brief Return the directive associated with the provided scope.
226 OpenMPDirectiveKind getDirectiveForScope(const Scope *S) const;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000227
228 /// \brief Set default data sharing attribute to none.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000229 void setDefaultDSANone(SourceLocation Loc) {
230 Stack.back().DefaultAttr = DSA_none;
231 Stack.back().DefaultAttrLoc = Loc;
232 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000233 /// \brief Set default data sharing attribute to shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000234 void setDefaultDSAShared(SourceLocation Loc) {
235 Stack.back().DefaultAttr = DSA_shared;
236 Stack.back().DefaultAttrLoc = Loc;
237 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000238
239 DefaultDataSharingAttributes getDefaultDSA() const {
240 return Stack.back().DefaultAttr;
241 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000242 SourceLocation getDefaultDSALocation() const {
243 return Stack.back().DefaultAttrLoc;
244 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000245
Alexey Bataevf29276e2014-06-18 04:14:57 +0000246 /// \brief Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000247 bool isThreadPrivate(VarDecl *D) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000248 DSAVarData DVar = getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000249 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000250 }
251
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000252 /// \brief Marks current region as ordered (it has an 'ordered' clause).
Alexey Bataev346265e2015-09-25 10:37:12 +0000253 void setOrderedRegion(bool IsOrdered, Expr *Param) {
254 Stack.back().OrderedRegion.setInt(IsOrdered);
255 Stack.back().OrderedRegion.setPointer(Param);
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000256 }
257 /// \brief Returns true, if parent region is ordered (has associated
258 /// 'ordered' clause), false - otherwise.
259 bool isParentOrderedRegion() const {
260 if (Stack.size() > 2)
Alexey Bataev346265e2015-09-25 10:37:12 +0000261 return Stack[Stack.size() - 2].OrderedRegion.getInt();
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000262 return false;
263 }
Alexey Bataev346265e2015-09-25 10:37:12 +0000264 /// \brief Returns optional parameter for the ordered region.
265 Expr *getParentOrderedRegionParam() const {
266 if (Stack.size() > 2)
267 return Stack[Stack.size() - 2].OrderedRegion.getPointer();
268 return nullptr;
269 }
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000270 /// \brief Marks current region as nowait (it has a 'nowait' clause).
271 void setNowaitRegion(bool IsNowait = true) {
272 Stack.back().NowaitRegion = IsNowait;
273 }
274 /// \brief Returns true, if parent region is nowait (has associated
275 /// 'nowait' clause), false - otherwise.
276 bool isParentNowaitRegion() const {
277 if (Stack.size() > 2)
278 return Stack[Stack.size() - 2].NowaitRegion;
279 return false;
280 }
Alexey Bataev25e5b442015-09-15 12:52:43 +0000281 /// \brief Marks parent region as cancel region.
282 void setParentCancelRegion(bool Cancel = true) {
283 if (Stack.size() > 2)
284 Stack[Stack.size() - 2].CancelRegion =
285 Stack[Stack.size() - 2].CancelRegion || Cancel;
286 }
287 /// \brief Return true if current region has inner cancel construct.
288 bool isCancelRegion() const {
289 return Stack.back().CancelRegion;
290 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000291
Alexey Bataev9c821032015-04-30 04:23:23 +0000292 /// \brief Set collapse value for the region.
293 void setCollapseNumber(unsigned Val) { Stack.back().CollapseNumber = Val; }
294 /// \brief Return collapse value for region.
295 unsigned getCollapseNumber() const {
296 return Stack.back().CollapseNumber;
297 }
298
Alexey Bataev13314bf2014-10-09 04:18:56 +0000299 /// \brief Marks current target region as one with closely nested teams
300 /// region.
301 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
302 if (Stack.size() > 2)
303 Stack[Stack.size() - 2].InnerTeamsRegionLoc = TeamsRegionLoc;
304 }
305 /// \brief Returns true, if current region has closely nested teams region.
306 bool hasInnerTeamsRegion() const {
307 return getInnerTeamsRegionLoc().isValid();
308 }
309 /// \brief Returns location of the nested teams region (if any).
310 SourceLocation getInnerTeamsRegionLoc() const {
311 if (Stack.size() > 1)
312 return Stack.back().InnerTeamsRegionLoc;
313 return SourceLocation();
314 }
315
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000316 Scope *getCurScope() const { return Stack.back().CurScope; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000317 Scope *getCurScope() { return Stack.back().CurScope; }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000318 SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000319
320 MapInfo getMapInfoForVar(VarDecl *VD) {
321 MapInfo VarMI = {0};
322 for (auto Cnt = Stack.size() - 1; Cnt > 0; --Cnt) {
323 if (Stack[Cnt].MappedDecls.count(VD)) {
324 VarMI = Stack[Cnt].MappedDecls[VD];
325 break;
326 }
327 }
328 return VarMI;
329 }
330
331 void addMapInfoForVar(VarDecl *VD, MapInfo MI) {
332 if (Stack.size() > 1) {
333 Stack.back().MappedDecls[VD] = MI;
334 }
335 }
336
337 MapInfo IsMappedInCurrentRegion(VarDecl *VD) {
338 assert(Stack.size() > 1 && "Target level is 0");
339 MapInfo VarMI = {0};
340 if (Stack.size() > 1 && Stack.back().MappedDecls.count(VD)) {
341 VarMI = Stack.back().MappedDecls[VD];
342 }
343 return VarMI;
344 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000345};
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000346bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
347 return isOpenMPParallelDirective(DKind) || DKind == OMPD_task ||
Alexey Bataev49f6e782015-12-01 04:18:41 +0000348 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000349 isOpenMPTaskLoopDirective(DKind);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000350}
Alexey Bataeved09d242014-05-28 05:53:51 +0000351} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000352
353DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter,
354 VarDecl *D) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000355 D = D->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000356 DSAVarData DVar;
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000357 if (Iter == std::prev(Stack.rend())) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000358 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
359 // in a region but not in construct]
360 // File-scope or namespace-scope variables referenced in called routines
361 // in the region are shared unless they appear in a threadprivate
362 // directive.
Alexey Bataev8b9cb982014-07-24 02:33:58 +0000363 if (!D->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D))
Alexey Bataev750a58b2014-03-18 12:19:12 +0000364 DVar.CKind = OMPC_shared;
365
366 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
367 // in a region but not in construct]
368 // Variables with static storage duration that are declared in called
369 // routines in the region are shared.
370 if (D->hasGlobalStorage())
371 DVar.CKind = OMPC_shared;
372
Alexey Bataev758e55e2013-09-06 18:03:48 +0000373 return DVar;
374 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000375
Alexey Bataev758e55e2013-09-06 18:03:48 +0000376 DVar.DKind = Iter->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +0000377 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
378 // in a Construct, C/C++, predetermined, p.1]
379 // Variables with automatic storage duration that are declared in a scope
380 // inside the construct are private.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000381 if (isOpenMPLocal(D, Iter) && D->isLocalVarDecl() &&
382 (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) {
383 DVar.CKind = OMPC_private;
384 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000385 }
386
Alexey Bataev758e55e2013-09-06 18:03:48 +0000387 // Explicitly specified attributes and local variables with predetermined
388 // attributes.
389 if (Iter->SharingMap.count(D)) {
390 DVar.RefExpr = Iter->SharingMap[D].RefExpr;
391 DVar.CKind = Iter->SharingMap[D].Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000392 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000393 return DVar;
394 }
395
396 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
397 // in a Construct, C/C++, implicitly determined, p.1]
398 // In a parallel or task construct, the data-sharing attributes of these
399 // variables are determined by the default clause, if present.
400 switch (Iter->DefaultAttr) {
401 case DSA_shared:
402 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000403 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000404 return DVar;
405 case DSA_none:
406 return DVar;
407 case DSA_unspecified:
408 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
409 // in a Construct, implicitly determined, p.2]
410 // In a parallel construct, if no default clause is present, these
411 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000412 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000413 if (isOpenMPParallelDirective(DVar.DKind) ||
414 isOpenMPTeamsDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000415 DVar.CKind = OMPC_shared;
416 return DVar;
417 }
418
419 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
420 // in a Construct, implicitly determined, p.4]
421 // In a task construct, if no default clause is present, a variable that in
422 // the enclosing context is determined to be shared by all implicit tasks
423 // bound to the current team is shared.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000424 if (DVar.DKind == OMPD_task) {
425 DSAVarData DVarTemp;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000426 for (StackTy::reverse_iterator I = std::next(Iter), EE = Stack.rend();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000427 I != EE; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000428 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
429 // Referenced
Alexey Bataev758e55e2013-09-06 18:03:48 +0000430 // in a Construct, implicitly determined, p.6]
431 // In a task construct, if no default clause is present, a variable
432 // whose data-sharing attribute is not determined by the rules above is
433 // firstprivate.
434 DVarTemp = getDSA(I, D);
435 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000436 DVar.RefExpr = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000437 DVar.DKind = OMPD_task;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000438 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000439 return DVar;
440 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000441 if (isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000442 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000443 }
444 DVar.DKind = OMPD_task;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000445 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000446 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000447 return DVar;
448 }
449 }
450 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
451 // in a Construct, implicitly determined, p.3]
452 // For constructs other than task, if no default clause is present, these
453 // variables inherit their data-sharing attributes from the enclosing
454 // context.
Benjamin Kramer167e9992014-03-02 12:20:24 +0000455 return getDSA(std::next(Iter), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000456}
457
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000458DeclRefExpr *DSAStackTy::addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE) {
459 assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000460 D = D->getCanonicalDecl();
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000461 auto It = Stack.back().AlignedMap.find(D);
462 if (It == Stack.back().AlignedMap.end()) {
463 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
464 Stack.back().AlignedMap[D] = NewDE;
465 return nullptr;
466 } else {
467 assert(It->second && "Unexpected nullptr expr in the aligned map");
468 return It->second;
469 }
470 return nullptr;
471}
472
Alexey Bataev9c821032015-04-30 04:23:23 +0000473void DSAStackTy::addLoopControlVariable(VarDecl *D) {
474 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
475 D = D->getCanonicalDecl();
476 Stack.back().LCVSet.insert(D);
477}
478
479bool DSAStackTy::isLoopControlVariable(VarDecl *D) {
480 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
481 D = D->getCanonicalDecl();
482 return Stack.back().LCVSet.count(D) > 0;
483}
484
Alexey Bataev758e55e2013-09-06 18:03:48 +0000485void DSAStackTy::addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000486 D = D->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000487 if (A == OMPC_threadprivate) {
488 Stack[0].SharingMap[D].Attributes = A;
489 Stack[0].SharingMap[D].RefExpr = E;
490 } else {
491 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
492 Stack.back().SharingMap[D].Attributes = A;
493 Stack.back().SharingMap[D].RefExpr = E;
494 }
495}
496
Alexey Bataeved09d242014-05-28 05:53:51 +0000497bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000498 D = D->getCanonicalDecl();
Alexey Bataevec3da872014-01-31 05:15:34 +0000499 if (Stack.size() > 2) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000500 reverse_iterator I = Iter, E = std::prev(Stack.rend());
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000501 Scope *TopScope = nullptr;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000502 while (I != E && !isParallelOrTaskRegion(I->Directive)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000503 ++I;
504 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000505 if (I == E)
506 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000507 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000508 Scope *CurScope = getCurScope();
509 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000510 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000511 }
512 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000513 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000514 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000515}
516
Alexey Bataev39f915b82015-05-08 10:41:21 +0000517/// \brief Build a variable declaration for OpenMP loop iteration variable.
518static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000519 StringRef Name, const AttrVec *Attrs = nullptr) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000520 DeclContext *DC = SemaRef.CurContext;
521 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
522 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
523 VarDecl *Decl =
524 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000525 if (Attrs) {
526 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
527 I != E; ++I)
528 Decl->addAttr(*I);
529 }
Alexey Bataev39f915b82015-05-08 10:41:21 +0000530 Decl->setImplicit();
531 return Decl;
532}
533
534static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
535 SourceLocation Loc,
536 bool RefersToCapture = false) {
537 D->setReferenced();
538 D->markUsed(S.Context);
539 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
540 SourceLocation(), D, RefersToCapture, Loc, Ty,
541 VK_LValue);
542}
543
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000544DSAStackTy::DSAVarData DSAStackTy::getTopDSA(VarDecl *D, bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000545 D = D->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000546 DSAVarData DVar;
547
548 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
549 // in a Construct, C/C++, predetermined, p.1]
550 // Variables appearing in threadprivate directives are threadprivate.
Samuel Antaof8b50122015-07-13 22:54:53 +0000551 if ((D->getTLSKind() != VarDecl::TLS_None &&
552 !(D->hasAttr<OMPThreadPrivateDeclAttr>() &&
553 SemaRef.getLangOpts().OpenMPUseTLS &&
554 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +0000555 (D->getStorageClass() == SC_Register && D->hasAttr<AsmLabelAttr>() &&
556 !D->isLocalVarDecl())) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000557 addDSA(D, buildDeclRefExpr(SemaRef, D, D->getType().getNonReferenceType(),
558 D->getLocation()),
Alexey Bataevf2453a02015-05-06 07:25:08 +0000559 OMPC_threadprivate);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000560 }
561 if (Stack[0].SharingMap.count(D)) {
562 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
563 DVar.CKind = OMPC_threadprivate;
564 return DVar;
565 }
566
567 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000568 // in a Construct, C/C++, predetermined, p.4]
569 // Static data members are shared.
570 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
571 // in a Construct, C/C++, predetermined, p.7]
572 // Variables with static storage duration that are declared in a scope
573 // inside the construct are shared.
574 if (D->isStaticDataMember()) {
575 DSAVarData DVarTemp =
576 hasDSA(D, isOpenMPPrivate, MatchesAlways(), FromParent);
577 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
Alexey Bataevec3da872014-01-31 05:15:34 +0000578 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000579
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000580 DVar.CKind = OMPC_shared;
581 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000582 }
583
584 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +0000585 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
586 Type = SemaRef.getASTContext().getBaseElementType(Type);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000587 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
588 // in a Construct, C/C++, predetermined, p.6]
589 // Variables with const qualified type having no mutable member are
590 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000591 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000592 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000593 if (IsConstant &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000594 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000595 // Variables with const-qualified type having no mutable member may be
596 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000597 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
598 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000599 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
600 return DVar;
601
Alexey Bataev758e55e2013-09-06 18:03:48 +0000602 DVar.CKind = OMPC_shared;
603 return DVar;
604 }
605
Alexey Bataev758e55e2013-09-06 18:03:48 +0000606 // Explicitly specified attributes and local variables with predetermined
607 // attributes.
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000608 auto StartI = std::next(Stack.rbegin());
609 auto EndI = std::prev(Stack.rend());
610 if (FromParent && StartI != EndI) {
611 StartI = std::next(StartI);
612 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000613 auto I = std::prev(StartI);
614 if (I->SharingMap.count(D)) {
615 DVar.RefExpr = I->SharingMap[D].RefExpr;
616 DVar.CKind = I->SharingMap[D].Attributes;
617 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000618 }
619
620 return DVar;
621}
622
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000623DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D, bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000624 D = D->getCanonicalDecl();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000625 auto StartI = Stack.rbegin();
626 auto EndI = std::prev(Stack.rend());
627 if (FromParent && StartI != EndI) {
628 StartI = std::next(StartI);
629 }
630 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000631}
632
Alexey Bataevf29276e2014-06-18 04:14:57 +0000633template <class ClausesPredicate, class DirectivesPredicate>
634DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000635 DirectivesPredicate DPred,
636 bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000637 D = D->getCanonicalDecl();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000638 auto StartI = std::next(Stack.rbegin());
639 auto EndI = std::prev(Stack.rend());
640 if (FromParent && StartI != EndI) {
641 StartI = std::next(StartI);
642 }
643 for (auto I = StartI, EE = EndI; I != EE; ++I) {
644 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000645 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000646 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000647 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000648 return DVar;
649 }
650 return DSAVarData();
651}
652
Alexey Bataevf29276e2014-06-18 04:14:57 +0000653template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000654DSAStackTy::DSAVarData
655DSAStackTy::hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
656 DirectivesPredicate DPred, bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000657 D = D->getCanonicalDecl();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000658 auto StartI = std::next(Stack.rbegin());
659 auto EndI = std::prev(Stack.rend());
660 if (FromParent && StartI != EndI) {
661 StartI = std::next(StartI);
662 }
663 for (auto I = StartI, EE = EndI; I != EE; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000664 if (!DPred(I->Directive))
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000665 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000666 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000667 if (CPred(DVar.CKind))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000668 return DVar;
669 return DSAVarData();
670 }
671 return DSAVarData();
672}
673
Alexey Bataevaac108a2015-06-23 04:51:00 +0000674bool DSAStackTy::hasExplicitDSA(
675 VarDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
676 unsigned Level) {
677 if (CPred(ClauseKindMode))
678 return true;
679 if (isClauseParsingMode())
680 ++Level;
681 D = D->getCanonicalDecl();
682 auto StartI = Stack.rbegin();
683 auto EndI = std::prev(Stack.rend());
NAKAMURA Takumi0332eda2015-06-23 10:01:20 +0000684 if (std::distance(StartI, EndI) <= (int)Level)
Alexey Bataevaac108a2015-06-23 04:51:00 +0000685 return false;
686 std::advance(StartI, Level);
687 return (StartI->SharingMap.count(D) > 0) && StartI->SharingMap[D].RefExpr &&
688 CPred(StartI->SharingMap[D].Attributes);
689}
690
Samuel Antao4be30e92015-10-02 17:14:03 +0000691bool DSAStackTy::hasExplicitDirective(
692 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
693 unsigned Level) {
694 if (isClauseParsingMode())
695 ++Level;
696 auto StartI = Stack.rbegin();
697 auto EndI = std::prev(Stack.rend());
698 if (std::distance(StartI, EndI) <= (int)Level)
699 return false;
700 std::advance(StartI, Level);
701 return DPred(StartI->Directive);
702}
703
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000704template <class NamedDirectivesPredicate>
705bool DSAStackTy::hasDirective(NamedDirectivesPredicate DPred, bool FromParent) {
706 auto StartI = std::next(Stack.rbegin());
707 auto EndI = std::prev(Stack.rend());
708 if (FromParent && StartI != EndI) {
709 StartI = std::next(StartI);
710 }
711 for (auto I = StartI, EE = EndI; I != EE; ++I) {
712 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
713 return true;
714 }
715 return false;
716}
717
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000718OpenMPDirectiveKind DSAStackTy::getDirectiveForScope(const Scope *S) const {
719 for (auto I = Stack.rbegin(), EE = Stack.rend(); I != EE; ++I)
720 if (I->CurScope == S)
721 return I->Directive;
722 return OMPD_unknown;
723}
724
Alexey Bataev758e55e2013-09-06 18:03:48 +0000725void Sema::InitDataSharingAttributesStack() {
726 VarDataSharingAttributesStack = new DSAStackTy(*this);
727}
728
729#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
730
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000731bool Sema::IsOpenMPCapturedByRef(VarDecl *VD,
732 const CapturedRegionScopeInfo *RSI) {
733 assert(LangOpts.OpenMP && "OpenMP is not allowed");
734
735 auto &Ctx = getASTContext();
736 bool IsByRef = true;
737
738 // Find the directive that is associated with the provided scope.
739 auto DKind = DSAStack->getDirectiveForScope(RSI->TheScope);
740 auto Ty = VD->getType();
741
742 if (isOpenMPTargetDirective(DKind)) {
743 // This table summarizes how a given variable should be passed to the device
744 // given its type and the clauses where it appears. This table is based on
745 // the description in OpenMP 4.5 [2.10.4, target Construct] and
746 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
747 //
748 // =========================================================================
749 // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
750 // | |(tofrom:scalar)| | pvt | | | |
751 // =========================================================================
752 // | scl | | | | - | | bycopy|
753 // | scl | | - | x | - | - | bycopy|
754 // | scl | | x | - | - | - | null |
755 // | scl | x | | | - | | byref |
756 // | scl | x | - | x | - | - | bycopy|
757 // | scl | x | x | - | - | - | null |
758 // | scl | | - | - | - | x | byref |
759 // | scl | x | - | - | - | x | byref |
760 //
761 // | agg | n.a. | | | - | | byref |
762 // | agg | n.a. | - | x | - | - | byref |
763 // | agg | n.a. | x | - | - | - | null |
764 // | agg | n.a. | - | - | - | x | byref |
765 // | agg | n.a. | - | - | - | x[] | byref |
766 //
767 // | ptr | n.a. | | | - | | bycopy|
768 // | ptr | n.a. | - | x | - | - | bycopy|
769 // | ptr | n.a. | x | - | - | - | null |
770 // | ptr | n.a. | - | - | - | x | byref |
771 // | ptr | n.a. | - | - | - | x[] | bycopy|
772 // | ptr | n.a. | - | - | x | | bycopy|
773 // | ptr | n.a. | - | - | x | x | bycopy|
774 // | ptr | n.a. | - | - | x | x[] | bycopy|
775 // =========================================================================
776 // Legend:
777 // scl - scalar
778 // ptr - pointer
779 // agg - aggregate
780 // x - applies
781 // - - invalid in this combination
782 // [] - mapped with an array section
783 // byref - should be mapped by reference
784 // byval - should be mapped by value
785 // null - initialize a local variable to null on the device
786 //
787 // Observations:
788 // - All scalar declarations that show up in a map clause have to be passed
789 // by reference, because they may have been mapped in the enclosing data
790 // environment.
791 // - If the scalar value does not fit the size of uintptr, it has to be
792 // passed by reference, regardless the result in the table above.
793 // - For pointers mapped by value that have either an implicit map or an
794 // array section, the runtime library may pass the NULL value to the
795 // device instead of the value passed to it by the compiler.
796
797 // FIXME: Right now, only implicit maps are implemented. Properly mapping
798 // values requires having the map, private, and firstprivate clauses SEMA
799 // and parsing in place, which we don't yet.
800
801 if (Ty->isReferenceType())
802 Ty = Ty->castAs<ReferenceType>()->getPointeeType();
803 IsByRef = !Ty->isScalarType();
804 }
805
806 // When passing data by value, we need to make sure it fits the uintptr size
807 // and alignment, because the runtime library only deals with uintptr types.
808 // If it does not fit the uintptr size, we need to pass the data by reference
809 // instead.
810 if (!IsByRef &&
811 (Ctx.getTypeSizeInChars(Ty) >
812 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
813 Ctx.getDeclAlign(VD) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType())))
814 IsByRef = true;
815
816 return IsByRef;
817}
818
Alexey Bataevf841bd92014-12-16 07:00:22 +0000819bool Sema::IsOpenMPCapturedVar(VarDecl *VD) {
820 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000821 VD = VD->getCanonicalDecl();
Samuel Antao4be30e92015-10-02 17:14:03 +0000822
823 // If we are attempting to capture a global variable in a directive with
824 // 'target' we return true so that this global is also mapped to the device.
825 //
826 // FIXME: If the declaration is enclosed in a 'declare target' directive,
827 // then it should not be captured. Therefore, an extra check has to be
828 // inserted here once support for 'declare target' is added.
829 //
830 if (!VD->hasLocalStorage()) {
831 if (DSAStack->getCurrentDirective() == OMPD_target &&
832 !DSAStack->isClauseParsingMode()) {
833 return true;
834 }
835 if (DSAStack->getCurScope() &&
836 DSAStack->hasDirective(
837 [](OpenMPDirectiveKind K, const DeclarationNameInfo &DNI,
838 SourceLocation Loc) -> bool {
839 return isOpenMPTargetDirective(K);
840 },
841 false)) {
842 return true;
843 }
844 }
845
Alexey Bataev48977c32015-08-04 08:10:48 +0000846 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
847 (!DSAStack->isClauseParsingMode() ||
848 DSAStack->getParentDirective() != OMPD_unknown)) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000849 if (DSAStack->isLoopControlVariable(VD) ||
850 (VD->hasLocalStorage() &&
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000851 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
852 DSAStack->isForceVarCapturing())
Alexey Bataev9c821032015-04-30 04:23:23 +0000853 return true;
Alexey Bataevaac108a2015-06-23 04:51:00 +0000854 auto DVarPrivate = DSAStack->getTopDSA(VD, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000855 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
856 return true;
857 DVarPrivate = DSAStack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(),
Alexey Bataevaac108a2015-06-23 04:51:00 +0000858 DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000859 return DVarPrivate.CKind != OMPC_unknown;
860 }
861 return false;
862}
863
Alexey Bataevaac108a2015-06-23 04:51:00 +0000864bool Sema::isOpenMPPrivateVar(VarDecl *VD, unsigned Level) {
865 assert(LangOpts.OpenMP && "OpenMP is not allowed");
866 return DSAStack->hasExplicitDSA(
867 VD, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level);
868}
869
Samuel Antao4be30e92015-10-02 17:14:03 +0000870bool Sema::isOpenMPTargetCapturedVar(VarDecl *VD, unsigned Level) {
871 assert(LangOpts.OpenMP && "OpenMP is not allowed");
872 // Return true if the current level is no longer enclosed in a target region.
873
874 return !VD->hasLocalStorage() &&
875 DSAStack->hasExplicitDirective(isOpenMPTargetDirective, Level);
876}
877
Alexey Bataeved09d242014-05-28 05:53:51 +0000878void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000879
880void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
881 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000882 Scope *CurScope, SourceLocation Loc) {
883 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000884 PushExpressionEvaluationContext(PotentiallyEvaluated);
885}
886
Alexey Bataevaac108a2015-06-23 04:51:00 +0000887void Sema::StartOpenMPClause(OpenMPClauseKind K) {
888 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000889}
890
Alexey Bataevaac108a2015-06-23 04:51:00 +0000891void Sema::EndOpenMPClause() {
892 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000893}
894
Alexey Bataev758e55e2013-09-06 18:03:48 +0000895void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000896 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
897 // A variable of class type (or array thereof) that appears in a lastprivate
898 // clause requires an accessible, unambiguous default constructor for the
899 // class type, unless the list item is also specified in a firstprivate
900 // clause.
901 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000902 for (auto *C : D->clauses()) {
903 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
904 SmallVector<Expr *, 8> PrivateCopies;
905 for (auto *DE : Clause->varlists()) {
906 if (DE->isValueDependent() || DE->isTypeDependent()) {
907 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000908 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +0000909 }
910 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(DE)->getDecl());
Alexey Bataevbd9fec12015-08-18 06:47:21 +0000911 QualType Type = VD->getType().getNonReferenceType();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000912 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000913 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000914 // Generate helper private variable and initialize it with the
915 // default value. The address of the original variable is replaced
916 // by the address of the new private variable in CodeGen. This new
917 // variable is not added to IdResolver, so the code in the OpenMP
918 // region uses original variable for proper diagnostics.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000919 auto *VDPrivate = buildVarDecl(
920 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
921 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +0000922 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
923 if (VDPrivate->isInvalidDecl())
924 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +0000925 PrivateCopies.push_back(buildDeclRefExpr(
926 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +0000927 } else {
928 // The variable is also a firstprivate, so initialization sequence
929 // for private copy is generated already.
930 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000931 }
932 }
Alexey Bataev38e89532015-04-16 04:54:05 +0000933 // Set initializers to private copies if no errors were found.
934 if (PrivateCopies.size() == Clause->varlist_size()) {
935 Clause->setPrivateCopies(PrivateCopies);
936 }
Alexey Bataevf29276e2014-06-18 04:14:57 +0000937 }
938 }
939 }
940
Alexey Bataev758e55e2013-09-06 18:03:48 +0000941 DSAStack->pop();
942 DiscardCleanupsInEvaluationContext();
943 PopExpressionEvaluationContext();
944}
945
Alexander Musman3276a272015-03-21 10:12:56 +0000946static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
947 Expr *NumIterations, Sema &SemaRef,
948 Scope *S);
949
Alexey Bataeva769e072013-03-22 06:34:35 +0000950namespace {
951
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000952class VarDeclFilterCCC : public CorrectionCandidateCallback {
953private:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000954 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +0000955
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000956public:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000957 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +0000958 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000959 NamedDecl *ND = Candidate.getCorrectionDecl();
960 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
961 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000962 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
963 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +0000964 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000965 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000966 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000967};
Alexey Bataeved09d242014-05-28 05:53:51 +0000968} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000969
970ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
971 CXXScopeSpec &ScopeSpec,
972 const DeclarationNameInfo &Id) {
973 LookupResult Lookup(*this, Id, LookupOrdinaryName);
974 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
975
976 if (Lookup.isAmbiguous())
977 return ExprError();
978
979 VarDecl *VD;
980 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +0000981 if (TypoCorrection Corrected = CorrectTypo(
982 Id, LookupOrdinaryName, CurScope, nullptr,
983 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +0000984 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000985 PDiag(Lookup.empty()
986 ? diag::err_undeclared_var_use_suggest
987 : diag::err_omp_expected_var_arg_suggest)
988 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +0000989 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000990 } else {
Richard Smithf9b15102013-08-17 00:46:16 +0000991 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
992 : diag::err_omp_expected_var_arg)
993 << Id.getName();
994 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000995 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000996 } else {
997 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000998 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000999 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1000 return ExprError();
1001 }
1002 }
1003 Lookup.suppressDiagnostics();
1004
1005 // OpenMP [2.9.2, Syntax, C/C++]
1006 // Variables must be file-scope, namespace-scope, or static block-scope.
1007 if (!VD->hasGlobalStorage()) {
1008 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001009 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1010 bool IsDecl =
1011 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001012 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001013 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1014 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001015 return ExprError();
1016 }
1017
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001018 VarDecl *CanonicalVD = VD->getCanonicalDecl();
1019 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001020 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1021 // A threadprivate directive for file-scope variables must appear outside
1022 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001023 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1024 !getCurLexicalContext()->isTranslationUnit()) {
1025 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001026 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1027 bool IsDecl =
1028 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1029 Diag(VD->getLocation(),
1030 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1031 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001032 return ExprError();
1033 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001034 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1035 // A threadprivate directive for static class member variables must appear
1036 // in the class definition, in the same scope in which the member
1037 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001038 if (CanonicalVD->isStaticDataMember() &&
1039 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1040 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001041 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1042 bool IsDecl =
1043 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1044 Diag(VD->getLocation(),
1045 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1046 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001047 return ExprError();
1048 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001049 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1050 // A threadprivate directive for namespace-scope variables must appear
1051 // outside any definition or declaration other than the namespace
1052 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001053 if (CanonicalVD->getDeclContext()->isNamespace() &&
1054 (!getCurLexicalContext()->isFileContext() ||
1055 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
1056 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001057 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1058 bool IsDecl =
1059 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1060 Diag(VD->getLocation(),
1061 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1062 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001063 return ExprError();
1064 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001065 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
1066 // A threadprivate directive for static block-scope variables must appear
1067 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001068 if (CanonicalVD->isStaticLocal() && CurScope &&
1069 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001070 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001071 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1072 bool IsDecl =
1073 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1074 Diag(VD->getLocation(),
1075 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1076 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001077 return ExprError();
1078 }
1079
1080 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1081 // A threadprivate directive must lexically precede all references to any
1082 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001083 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001084 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +00001085 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001086 return ExprError();
1087 }
1088
1089 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00001090 ExprResult DE = buildDeclRefExpr(*this, VD, ExprType, Id.getLoc());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001091 return DE;
1092}
1093
Alexey Bataeved09d242014-05-28 05:53:51 +00001094Sema::DeclGroupPtrTy
1095Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1096 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001097 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001098 CurContext->addDecl(D);
1099 return DeclGroupPtrTy::make(DeclGroupRef(D));
1100 }
1101 return DeclGroupPtrTy();
1102}
1103
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001104namespace {
1105class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
1106 Sema &SemaRef;
1107
1108public:
1109 bool VisitDeclRefExpr(const DeclRefExpr *E) {
1110 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
1111 if (VD->hasLocalStorage()) {
1112 SemaRef.Diag(E->getLocStart(),
1113 diag::err_omp_local_var_in_threadprivate_init)
1114 << E->getSourceRange();
1115 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1116 << VD << VD->getSourceRange();
1117 return true;
1118 }
1119 }
1120 return false;
1121 }
1122 bool VisitStmt(const Stmt *S) {
1123 for (auto Child : S->children()) {
1124 if (Child && Visit(Child))
1125 return true;
1126 }
1127 return false;
1128 }
Alexey Bataev23b69422014-06-18 07:08:49 +00001129 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001130};
1131} // namespace
1132
Alexey Bataeved09d242014-05-28 05:53:51 +00001133OMPThreadPrivateDecl *
1134Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001135 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001136 for (auto &RefExpr : VarList) {
1137 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001138 VarDecl *VD = cast<VarDecl>(DE->getDecl());
1139 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +00001140
Alexey Bataevf56f98c2015-04-16 05:39:01 +00001141 QualType QType = VD->getType();
1142 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1143 // It will be analyzed later.
1144 Vars.push_back(DE);
1145 continue;
1146 }
1147
Alexey Bataeva769e072013-03-22 06:34:35 +00001148 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1149 // A threadprivate variable must not have an incomplete type.
1150 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001151 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001152 continue;
1153 }
1154
1155 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1156 // A threadprivate variable must not have a reference type.
1157 if (VD->getType()->isReferenceType()) {
1158 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001159 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
1160 bool IsDecl =
1161 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1162 Diag(VD->getLocation(),
1163 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1164 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001165 continue;
1166 }
1167
Samuel Antaof8b50122015-07-13 22:54:53 +00001168 // Check if this is a TLS variable. If TLS is not being supported, produce
1169 // the corresponding diagnostic.
1170 if ((VD->getTLSKind() != VarDecl::TLS_None &&
1171 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1172 getLangOpts().OpenMPUseTLS &&
1173 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +00001174 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
1175 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +00001176 Diag(ILoc, diag::err_omp_var_thread_local)
1177 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +00001178 bool IsDecl =
1179 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1180 Diag(VD->getLocation(),
1181 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1182 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001183 continue;
1184 }
1185
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001186 // Check if initial value of threadprivate variable reference variable with
1187 // local storage (it is not supported by runtime).
1188 if (auto Init = VD->getAnyInitializer()) {
1189 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001190 if (Checker.Visit(Init))
1191 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001192 }
1193
Alexey Bataeved09d242014-05-28 05:53:51 +00001194 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00001195 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00001196 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1197 Context, SourceRange(Loc, Loc)));
1198 if (auto *ML = Context.getASTMutationListener())
1199 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00001200 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001201 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001202 if (!Vars.empty()) {
1203 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1204 Vars);
1205 D->setAccess(AS_public);
1206 }
1207 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00001208}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001209
Alexey Bataev7ff55242014-06-19 09:13:45 +00001210static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
1211 const VarDecl *VD, DSAStackTy::DSAVarData DVar,
1212 bool IsLoopIterVar = false) {
1213 if (DVar.RefExpr) {
1214 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1215 << getOpenMPClauseName(DVar.CKind);
1216 return;
1217 }
1218 enum {
1219 PDSA_StaticMemberShared,
1220 PDSA_StaticLocalVarShared,
1221 PDSA_LoopIterVarPrivate,
1222 PDSA_LoopIterVarLinear,
1223 PDSA_LoopIterVarLastprivate,
1224 PDSA_ConstVarShared,
1225 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001226 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001227 PDSA_LocalVarPrivate,
1228 PDSA_Implicit
1229 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001230 bool ReportHint = false;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001231 auto ReportLoc = VD->getLocation();
Alexey Bataev7ff55242014-06-19 09:13:45 +00001232 if (IsLoopIterVar) {
1233 if (DVar.CKind == OMPC_private)
1234 Reason = PDSA_LoopIterVarPrivate;
1235 else if (DVar.CKind == OMPC_lastprivate)
1236 Reason = PDSA_LoopIterVarLastprivate;
1237 else
1238 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001239 } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) {
1240 Reason = PDSA_TaskVarFirstprivate;
1241 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001242 } else if (VD->isStaticLocal())
1243 Reason = PDSA_StaticLocalVarShared;
1244 else if (VD->isStaticDataMember())
1245 Reason = PDSA_StaticMemberShared;
1246 else if (VD->isFileVarDecl())
1247 Reason = PDSA_GlobalVarShared;
1248 else if (VD->getType().isConstant(SemaRef.getASTContext()))
1249 Reason = PDSA_ConstVarShared;
Alexey Bataevbae9a792014-06-27 10:37:06 +00001250 else if (VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00001251 ReportHint = true;
1252 Reason = PDSA_LocalVarPrivate;
1253 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00001254 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001255 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00001256 << Reason << ReportHint
1257 << getOpenMPDirectiveName(Stack->getCurrentDirective());
1258 } else if (DVar.ImplicitDSALoc.isValid()) {
1259 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1260 << getOpenMPClauseName(DVar.CKind);
1261 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00001262}
1263
Alexey Bataev758e55e2013-09-06 18:03:48 +00001264namespace {
1265class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1266 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001267 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001268 bool ErrorFound;
1269 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001270 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev4acb8592014-07-07 13:01:15 +00001271 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +00001272
Alexey Bataev758e55e2013-09-06 18:03:48 +00001273public:
1274 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001275 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001276 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +00001277 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1278 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001279
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001280 auto DVar = Stack->getTopDSA(VD, false);
1281 // Check if the variable has explicit DSA set and stop analysis if it so.
1282 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001283
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001284 auto ELoc = E->getExprLoc();
1285 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001286 // The default(none) clause requires that each variable that is referenced
1287 // in the construct, and does not have a predetermined data-sharing
1288 // attribute, must have its data-sharing attribute explicitly determined
1289 // by being listed in a data-sharing attribute clause.
1290 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001291 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001292 VarsWithInheritedDSA.count(VD) == 0) {
1293 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001294 return;
1295 }
1296
1297 // OpenMP [2.9.3.6, Restrictions, p.2]
1298 // A list item that appears in a reduction clause of the innermost
1299 // enclosing worksharing or parallel construct may not be accessed in an
1300 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +00001301 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001302 [](OpenMPDirectiveKind K) -> bool {
1303 return isOpenMPParallelDirective(K) ||
Alexey Bataev13314bf2014-10-09 04:18:56 +00001304 isOpenMPWorksharingDirective(K) ||
1305 isOpenMPTeamsDirective(K);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001306 },
1307 false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001308 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
1309 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001310 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1311 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001312 return;
1313 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001314
1315 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001316 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001317 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001318 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001319 }
1320 }
1321 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001322 for (auto *C : S->clauses()) {
1323 // Skip analysis of arguments of implicitly defined firstprivate clause
1324 // for task directives.
1325 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1326 for (auto *CC : C->children()) {
1327 if (CC)
1328 Visit(CC);
1329 }
1330 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001331 }
1332 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001333 for (auto *C : S->children()) {
1334 if (C && !isa<OMPExecutableDirective>(C))
1335 Visit(C);
1336 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001337 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001338
1339 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001340 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001341 llvm::DenseMap<VarDecl *, Expr *> &getVarsWithInheritedDSA() {
1342 return VarsWithInheritedDSA;
1343 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001344
Alexey Bataev7ff55242014-06-19 09:13:45 +00001345 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1346 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00001347};
Alexey Bataeved09d242014-05-28 05:53:51 +00001348} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001349
Alexey Bataevbae9a792014-06-27 10:37:06 +00001350void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001351 switch (DKind) {
1352 case OMPD_parallel: {
1353 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001354 QualType KmpInt32PtrTy =
1355 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001356 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001357 std::make_pair(".global_tid.", KmpInt32PtrTy),
1358 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1359 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001360 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001361 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1362 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001363 break;
1364 }
1365 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001366 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001367 std::make_pair(StringRef(), QualType()) // __context with shared vars
1368 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001369 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1370 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001371 break;
1372 }
1373 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001374 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001375 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001376 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001377 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1378 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001379 break;
1380 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001381 case OMPD_for_simd: {
1382 Sema::CapturedParamNameType Params[] = {
1383 std::make_pair(StringRef(), QualType()) // __context with shared vars
1384 };
1385 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1386 Params);
1387 break;
1388 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001389 case OMPD_sections: {
1390 Sema::CapturedParamNameType Params[] = {
1391 std::make_pair(StringRef(), QualType()) // __context with shared vars
1392 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001393 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1394 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001395 break;
1396 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001397 case OMPD_section: {
1398 Sema::CapturedParamNameType Params[] = {
1399 std::make_pair(StringRef(), QualType()) // __context with shared vars
1400 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001401 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1402 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001403 break;
1404 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001405 case OMPD_single: {
1406 Sema::CapturedParamNameType Params[] = {
1407 std::make_pair(StringRef(), QualType()) // __context with shared vars
1408 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001409 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1410 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001411 break;
1412 }
Alexander Musman80c22892014-07-17 08:54:58 +00001413 case OMPD_master: {
1414 Sema::CapturedParamNameType Params[] = {
1415 std::make_pair(StringRef(), QualType()) // __context with shared vars
1416 };
1417 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1418 Params);
1419 break;
1420 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001421 case OMPD_critical: {
1422 Sema::CapturedParamNameType Params[] = {
1423 std::make_pair(StringRef(), QualType()) // __context with shared vars
1424 };
1425 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1426 Params);
1427 break;
1428 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001429 case OMPD_parallel_for: {
1430 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001431 QualType KmpInt32PtrTy =
1432 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev4acb8592014-07-07 13:01:15 +00001433 Sema::CapturedParamNameType Params[] = {
1434 std::make_pair(".global_tid.", KmpInt32PtrTy),
1435 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1436 std::make_pair(StringRef(), QualType()) // __context with shared vars
1437 };
1438 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1439 Params);
1440 break;
1441 }
Alexander Musmane4e893b2014-09-23 09:33:00 +00001442 case OMPD_parallel_for_simd: {
1443 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001444 QualType KmpInt32PtrTy =
1445 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexander Musmane4e893b2014-09-23 09:33:00 +00001446 Sema::CapturedParamNameType Params[] = {
1447 std::make_pair(".global_tid.", KmpInt32PtrTy),
1448 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1449 std::make_pair(StringRef(), QualType()) // __context with shared vars
1450 };
1451 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1452 Params);
1453 break;
1454 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001455 case OMPD_parallel_sections: {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001456 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001457 QualType KmpInt32PtrTy =
1458 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001459 Sema::CapturedParamNameType Params[] = {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001460 std::make_pair(".global_tid.", KmpInt32PtrTy),
1461 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001462 std::make_pair(StringRef(), QualType()) // __context with shared vars
1463 };
1464 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1465 Params);
1466 break;
1467 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001468 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001469 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001470 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1471 FunctionProtoType::ExtProtoInfo EPI;
1472 EPI.Variadic = true;
1473 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001474 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001475 std::make_pair(".global_tid.", KmpInt32Ty),
1476 std::make_pair(".part_id.", KmpInt32Ty),
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001477 std::make_pair(".privates.",
1478 Context.VoidPtrTy.withConst().withRestrict()),
1479 std::make_pair(
1480 ".copy_fn.",
1481 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001482 std::make_pair(StringRef(), QualType()) // __context with shared vars
1483 };
1484 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1485 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001486 // Mark this captured region as inlined, because we don't use outlined
1487 // function directly.
1488 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1489 AlwaysInlineAttr::CreateImplicit(
1490 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001491 break;
1492 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001493 case OMPD_ordered: {
1494 Sema::CapturedParamNameType Params[] = {
1495 std::make_pair(StringRef(), QualType()) // __context with shared vars
1496 };
1497 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1498 Params);
1499 break;
1500 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001501 case OMPD_atomic: {
1502 Sema::CapturedParamNameType Params[] = {
1503 std::make_pair(StringRef(), QualType()) // __context with shared vars
1504 };
1505 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1506 Params);
1507 break;
1508 }
Michael Wong65f367f2015-07-21 13:44:28 +00001509 case OMPD_target_data:
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001510 case OMPD_target: {
1511 Sema::CapturedParamNameType Params[] = {
1512 std::make_pair(StringRef(), QualType()) // __context with shared vars
1513 };
1514 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1515 Params);
1516 break;
1517 }
Alexey Bataev13314bf2014-10-09 04:18:56 +00001518 case OMPD_teams: {
1519 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001520 QualType KmpInt32PtrTy =
1521 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev13314bf2014-10-09 04:18:56 +00001522 Sema::CapturedParamNameType Params[] = {
1523 std::make_pair(".global_tid.", KmpInt32PtrTy),
1524 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1525 std::make_pair(StringRef(), QualType()) // __context with shared vars
1526 };
1527 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1528 Params);
1529 break;
1530 }
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001531 case OMPD_taskgroup: {
1532 Sema::CapturedParamNameType Params[] = {
1533 std::make_pair(StringRef(), QualType()) // __context with shared vars
1534 };
1535 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1536 Params);
1537 break;
1538 }
Alexey Bataev49f6e782015-12-01 04:18:41 +00001539 case OMPD_taskloop: {
1540 Sema::CapturedParamNameType Params[] = {
1541 std::make_pair(StringRef(), QualType()) // __context with shared vars
1542 };
1543 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1544 Params);
1545 break;
1546 }
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001547 case OMPD_taskloop_simd: {
1548 Sema::CapturedParamNameType Params[] = {
1549 std::make_pair(StringRef(), QualType()) // __context with shared vars
1550 };
1551 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1552 Params);
1553 break;
1554 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001555 case OMPD_distribute: {
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 Bataev0a6ed842015-12-03 09:40:15 +00001646 // | parallel | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001647 // | parallel | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001648 // +------------------+-----------------+------------------------------------+
1649 // | for | parallel | * |
1650 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001651 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001652 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001653 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001654 // | for | simd | * |
1655 // | for | sections | + |
1656 // | for | section | + |
1657 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001658 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001659 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001660 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001661 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001662 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001663 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001664 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001665 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001666 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001667 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001668 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001669 // | for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001670 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001671 // | for | cancellation | |
1672 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001673 // | for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001674 // | for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001675 // | for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001676 // | for | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001677 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001678 // | master | parallel | * |
1679 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001680 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001681 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001682 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001683 // | master | simd | * |
1684 // | master | sections | + |
1685 // | master | section | + |
1686 // | master | single | + |
1687 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001688 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001689 // | master |parallel sections| * |
1690 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001691 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001692 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001693 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001694 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001695 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001696 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001697 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001698 // | master | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001699 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001700 // | master | cancellation | |
1701 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001702 // | master | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001703 // | master | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001704 // | master | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001705 // | master | distribute | |
Alexander Musman80c22892014-07-17 08:54:58 +00001706 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001707 // | critical | parallel | * |
1708 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001709 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001710 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001711 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001712 // | critical | simd | * |
1713 // | critical | sections | + |
1714 // | critical | section | + |
1715 // | critical | single | + |
1716 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001717 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001718 // | critical |parallel sections| * |
1719 // | critical | task | * |
1720 // | critical | taskyield | * |
1721 // | critical | barrier | + |
1722 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001723 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001724 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001725 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001726 // | critical | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001727 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001728 // | critical | cancellation | |
1729 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001730 // | critical | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001731 // | critical | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001732 // | critical | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001733 // | critical | distribute | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001734 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001735 // | simd | parallel | |
1736 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001737 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00001738 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001739 // | simd | critical | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001740 // | simd | simd | |
1741 // | simd | sections | |
1742 // | simd | section | |
1743 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001744 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001745 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001746 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001747 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00001748 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001749 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001750 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001751 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00001752 // | simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001753 // | simd | ordered | + (with simd clause) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001754 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001755 // | simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001756 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001757 // | simd | cancellation | |
1758 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001759 // | simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001760 // | simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001761 // | simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001762 // | simd | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001763 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00001764 // | for simd | parallel | |
1765 // | for simd | for | |
1766 // | for simd | for simd | |
1767 // | for simd | master | |
1768 // | for simd | critical | |
1769 // | for simd | simd | |
1770 // | for simd | sections | |
1771 // | for simd | section | |
1772 // | for simd | single | |
1773 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001774 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001775 // | for simd |parallel sections| |
1776 // | for simd | task | |
1777 // | for simd | taskyield | |
1778 // | for simd | barrier | |
1779 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001780 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001781 // | for simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001782 // | for simd | ordered | + (with simd clause) |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001783 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001784 // | for simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001785 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001786 // | for simd | cancellation | |
1787 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001788 // | for simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001789 // | for simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001790 // | for simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001791 // | for simd | distribute | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001792 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00001793 // | parallel for simd| parallel | |
1794 // | parallel for simd| for | |
1795 // | parallel for simd| for simd | |
1796 // | parallel for simd| master | |
1797 // | parallel for simd| critical | |
1798 // | parallel for simd| simd | |
1799 // | parallel for simd| sections | |
1800 // | parallel for simd| section | |
1801 // | parallel for simd| single | |
1802 // | parallel for simd| parallel for | |
1803 // | parallel for simd|parallel for simd| |
1804 // | parallel for simd|parallel sections| |
1805 // | parallel for simd| task | |
1806 // | parallel for simd| taskyield | |
1807 // | parallel for simd| barrier | |
1808 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001809 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001810 // | parallel for simd| flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001811 // | parallel for simd| ordered | + (with simd clause) |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001812 // | parallel for simd| atomic | |
1813 // | parallel for simd| target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001814 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001815 // | parallel for simd| cancellation | |
1816 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001817 // | parallel for simd| cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001818 // | parallel for simd| taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001819 // | parallel for simd| taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001820 // | parallel for simd| distribute | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001821 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001822 // | sections | parallel | * |
1823 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001824 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001825 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001826 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001827 // | sections | simd | * |
1828 // | sections | sections | + |
1829 // | sections | section | * |
1830 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001831 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001832 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001833 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001834 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001835 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001836 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001837 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001838 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001839 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001840 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001841 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001842 // | sections | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001843 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001844 // | sections | cancellation | |
1845 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001846 // | sections | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001847 // | sections | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001848 // | sections | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001849 // | sections | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001850 // +------------------+-----------------+------------------------------------+
1851 // | section | parallel | * |
1852 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001853 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001854 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001855 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001856 // | section | simd | * |
1857 // | section | sections | + |
1858 // | section | section | + |
1859 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001860 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001861 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001862 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001863 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001864 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001865 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001866 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001867 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001868 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001869 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001870 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001871 // | section | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001872 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001873 // | section | cancellation | |
1874 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001875 // | section | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001876 // | section | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001877 // | section | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001878 // | section | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001879 // +------------------+-----------------+------------------------------------+
1880 // | single | parallel | * |
1881 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001882 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001883 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001884 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001885 // | single | simd | * |
1886 // | single | sections | + |
1887 // | single | section | + |
1888 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001889 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001890 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001891 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001892 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001893 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001894 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001895 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001896 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001897 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001898 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001899 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001900 // | single | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001901 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001902 // | single | cancellation | |
1903 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001904 // | single | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001905 // | single | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001906 // | single | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001907 // | single | distribute | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001908 // +------------------+-----------------+------------------------------------+
1909 // | parallel for | parallel | * |
1910 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001911 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001912 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001913 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001914 // | parallel for | simd | * |
1915 // | parallel for | sections | + |
1916 // | parallel for | section | + |
1917 // | parallel for | single | + |
1918 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001919 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001920 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001921 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001922 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001923 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001924 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001925 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001926 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001927 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001928 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001929 // | parallel for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001930 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001931 // | parallel for | cancellation | |
1932 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001933 // | parallel for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001934 // | parallel for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001935 // | parallel for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001936 // | parallel for | distribute | |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001937 // +------------------+-----------------+------------------------------------+
1938 // | parallel sections| parallel | * |
1939 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001940 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001941 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001942 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001943 // | parallel sections| simd | * |
1944 // | parallel sections| sections | + |
1945 // | parallel sections| section | * |
1946 // | parallel sections| single | + |
1947 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001948 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001949 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001950 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001951 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001952 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001953 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001954 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001955 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001956 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001957 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001958 // | parallel sections| target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001959 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001960 // | parallel sections| cancellation | |
1961 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001962 // | parallel sections| cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001963 // | parallel sections| taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001964 // | parallel sections| taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001965 // | parallel sections| distribute | |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001966 // +------------------+-----------------+------------------------------------+
1967 // | task | parallel | * |
1968 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001969 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001970 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001971 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001972 // | task | simd | * |
1973 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001974 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001975 // | task | single | + |
1976 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001977 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001978 // | task |parallel sections| * |
1979 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001980 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001981 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001982 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001983 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001984 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001985 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001986 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001987 // | task | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001988 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001989 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00001990 // | | point | ! |
1991 // | task | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001992 // | task | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001993 // | task | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001994 // | task | distribute | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001995 // +------------------+-----------------+------------------------------------+
1996 // | ordered | parallel | * |
1997 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001998 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001999 // | ordered | master | * |
2000 // | ordered | critical | * |
2001 // | ordered | simd | * |
2002 // | ordered | sections | + |
2003 // | ordered | section | + |
2004 // | ordered | single | + |
2005 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002006 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002007 // | ordered |parallel sections| * |
2008 // | ordered | task | * |
2009 // | ordered | taskyield | * |
2010 // | ordered | barrier | + |
2011 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002012 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002013 // | ordered | flush | * |
2014 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002015 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002016 // | ordered | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002017 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002018 // | ordered | cancellation | |
2019 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002020 // | ordered | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002021 // | ordered | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002022 // | ordered | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002023 // | ordered | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002024 // +------------------+-----------------+------------------------------------+
2025 // | atomic | parallel | |
2026 // | atomic | for | |
2027 // | atomic | for simd | |
2028 // | atomic | master | |
2029 // | atomic | critical | |
2030 // | atomic | simd | |
2031 // | atomic | sections | |
2032 // | atomic | section | |
2033 // | atomic | single | |
2034 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002035 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002036 // | atomic |parallel sections| |
2037 // | atomic | task | |
2038 // | atomic | taskyield | |
2039 // | atomic | barrier | |
2040 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002041 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002042 // | atomic | flush | |
2043 // | atomic | ordered | |
2044 // | atomic | atomic | |
2045 // | atomic | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002046 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002047 // | atomic | cancellation | |
2048 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002049 // | atomic | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002050 // | atomic | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002051 // | atomic | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002052 // | atomic | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002053 // +------------------+-----------------+------------------------------------+
2054 // | target | parallel | * |
2055 // | target | for | * |
2056 // | target | for simd | * |
2057 // | target | master | * |
2058 // | target | critical | * |
2059 // | target | simd | * |
2060 // | target | sections | * |
2061 // | target | section | * |
2062 // | target | single | * |
2063 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002064 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002065 // | target |parallel sections| * |
2066 // | target | task | * |
2067 // | target | taskyield | * |
2068 // | target | barrier | * |
2069 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002070 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002071 // | target | flush | * |
2072 // | target | ordered | * |
2073 // | target | atomic | * |
2074 // | target | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002075 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002076 // | target | cancellation | |
2077 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002078 // | target | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002079 // | target | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002080 // | target | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002081 // | target | distribute | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002082 // +------------------+-----------------+------------------------------------+
2083 // | teams | parallel | * |
2084 // | teams | for | + |
2085 // | teams | for simd | + |
2086 // | teams | master | + |
2087 // | teams | critical | + |
2088 // | teams | simd | + |
2089 // | teams | sections | + |
2090 // | teams | section | + |
2091 // | teams | single | + |
2092 // | teams | parallel for | * |
2093 // | teams |parallel for simd| * |
2094 // | teams |parallel sections| * |
2095 // | teams | task | + |
2096 // | teams | taskyield | + |
2097 // | teams | barrier | + |
2098 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00002099 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002100 // | teams | flush | + |
2101 // | teams | ordered | + |
2102 // | teams | atomic | + |
2103 // | teams | target | + |
2104 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002105 // | teams | cancellation | |
2106 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002107 // | teams | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002108 // | teams | taskloop | + |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002109 // | teams | taskloop simd | + |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002110 // | teams | distribute | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002111 // +------------------+-----------------+------------------------------------+
2112 // | taskloop | parallel | * |
2113 // | taskloop | for | + |
2114 // | taskloop | for simd | + |
2115 // | taskloop | master | + |
2116 // | taskloop | critical | * |
2117 // | taskloop | simd | * |
2118 // | taskloop | sections | + |
2119 // | taskloop | section | + |
2120 // | taskloop | single | + |
2121 // | taskloop | parallel for | * |
2122 // | taskloop |parallel for simd| * |
2123 // | taskloop |parallel sections| * |
2124 // | taskloop | task | * |
2125 // | taskloop | taskyield | * |
2126 // | taskloop | barrier | + |
2127 // | taskloop | taskwait | * |
2128 // | taskloop | taskgroup | * |
2129 // | taskloop | flush | * |
2130 // | taskloop | ordered | + |
2131 // | taskloop | atomic | * |
2132 // | taskloop | target | * |
2133 // | taskloop | teams | + |
2134 // | taskloop | cancellation | |
2135 // | | point | |
2136 // | taskloop | cancel | |
2137 // | taskloop | taskloop | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002138 // | taskloop | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002139 // +------------------+-----------------+------------------------------------+
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002140 // | taskloop simd | parallel | |
2141 // | taskloop simd | for | |
2142 // | taskloop simd | for simd | |
2143 // | taskloop simd | master | |
2144 // | taskloop simd | critical | |
2145 // | taskloop simd | simd | |
2146 // | taskloop simd | sections | |
2147 // | taskloop simd | section | |
2148 // | taskloop simd | single | |
2149 // | taskloop simd | parallel for | |
2150 // | taskloop simd |parallel for simd| |
2151 // | taskloop simd |parallel sections| |
2152 // | taskloop simd | task | |
2153 // | taskloop simd | taskyield | |
2154 // | taskloop simd | barrier | |
2155 // | taskloop simd | taskwait | |
2156 // | taskloop simd | taskgroup | |
2157 // | taskloop simd | flush | |
2158 // | taskloop simd | ordered | + (with simd clause) |
2159 // | taskloop simd | atomic | |
2160 // | taskloop simd | target | |
2161 // | taskloop simd | teams | |
2162 // | taskloop simd | cancellation | |
2163 // | | point | |
2164 // | taskloop simd | cancel | |
2165 // | taskloop simd | taskloop | |
2166 // | taskloop simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002167 // | taskloop simd | distribute | |
2168 // +------------------+-----------------+------------------------------------+
2169 // | distribute | parallel | * |
2170 // | distribute | for | * |
2171 // | distribute | for simd | * |
2172 // | distribute | master | * |
2173 // | distribute | critical | * |
2174 // | distribute | simd | * |
2175 // | distribute | sections | * |
2176 // | distribute | section | * |
2177 // | distribute | single | * |
2178 // | distribute | parallel for | * |
2179 // | distribute |parallel for simd| * |
2180 // | distribute |parallel sections| * |
2181 // | distribute | task | * |
2182 // | distribute | taskyield | * |
2183 // | distribute | barrier | * |
2184 // | distribute | taskwait | * |
2185 // | distribute | taskgroup | * |
2186 // | distribute | flush | * |
2187 // | distribute | ordered | + |
2188 // | distribute | atomic | * |
2189 // | distribute | target | |
2190 // | distribute | teams | |
2191 // | distribute | cancellation | + |
2192 // | | point | |
2193 // | distribute | cancel | + |
2194 // | distribute | taskloop | * |
2195 // | distribute | taskloop simd | * |
2196 // | distribute | distribute | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002197 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00002198 if (Stack->getCurScope()) {
2199 auto ParentRegion = Stack->getParentDirective();
2200 bool NestingProhibited = false;
2201 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002202 enum {
2203 NoRecommend,
2204 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00002205 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002206 ShouldBeInTargetRegion,
2207 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002208 } Recommend = NoRecommend;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002209 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002210 // OpenMP [2.16, Nesting of Regions]
2211 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002212 // OpenMP [2.8.1,simd Construct, Restrictions]
2213 // An ordered construct with the simd clause is the only OpenMP construct
2214 // that can appear in the simd region.
Alexey Bataev549210e2014-06-24 04:39:47 +00002215 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
2216 return true;
2217 }
Alexey Bataev0162e452014-07-22 10:10:35 +00002218 if (ParentRegion == OMPD_atomic) {
2219 // OpenMP [2.16, Nesting of Regions]
2220 // OpenMP constructs may not be nested inside an atomic region.
2221 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
2222 return true;
2223 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002224 if (CurrentRegion == OMPD_section) {
2225 // OpenMP [2.7.2, sections Construct, Restrictions]
2226 // Orphaned section directives are prohibited. That is, the section
2227 // directives must appear within the sections construct and must not be
2228 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002229 if (ParentRegion != OMPD_sections &&
2230 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002231 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
2232 << (ParentRegion != OMPD_unknown)
2233 << getOpenMPDirectiveName(ParentRegion);
2234 return true;
2235 }
2236 return false;
2237 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002238 // Allow some constructs to be orphaned (they could be used in functions,
2239 // called from OpenMP regions with the required preconditions).
2240 if (ParentRegion == OMPD_unknown)
2241 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00002242 if (CurrentRegion == OMPD_cancellation_point ||
2243 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002244 // OpenMP [2.16, Nesting of Regions]
2245 // A cancellation point construct for which construct-type-clause is
2246 // taskgroup must be nested inside a task construct. A cancellation
2247 // point construct for which construct-type-clause is not taskgroup must
2248 // be closely nested inside an OpenMP construct that matches the type
2249 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00002250 // A cancel construct for which construct-type-clause is taskgroup must be
2251 // nested inside a task construct. A cancel construct for which
2252 // construct-type-clause is not taskgroup must be closely nested inside an
2253 // OpenMP construct that matches the type specified in
2254 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002255 NestingProhibited =
2256 !((CancelRegion == OMPD_parallel && ParentRegion == OMPD_parallel) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00002257 (CancelRegion == OMPD_for &&
2258 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002259 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
2260 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00002261 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
2262 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002263 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00002264 // OpenMP [2.16, Nesting of Regions]
2265 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002266 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00002267 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002268 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002269 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002270 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
2271 // OpenMP [2.16, Nesting of Regions]
2272 // A critical region may not be nested (closely or otherwise) inside a
2273 // critical region with the same name. Note that this restriction is not
2274 // sufficient to prevent deadlock.
2275 SourceLocation PreviousCriticalLoc;
2276 bool DeadLock =
2277 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
2278 OpenMPDirectiveKind K,
2279 const DeclarationNameInfo &DNI,
2280 SourceLocation Loc)
2281 ->bool {
2282 if (K == OMPD_critical &&
2283 DNI.getName() == CurrentName.getName()) {
2284 PreviousCriticalLoc = Loc;
2285 return true;
2286 } else
2287 return false;
2288 },
2289 false /* skip top directive */);
2290 if (DeadLock) {
2291 SemaRef.Diag(StartLoc,
2292 diag::err_omp_prohibited_region_critical_same_name)
2293 << CurrentName.getName();
2294 if (PreviousCriticalLoc.isValid())
2295 SemaRef.Diag(PreviousCriticalLoc,
2296 diag::note_omp_previous_critical_region);
2297 return true;
2298 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002299 } else if (CurrentRegion == OMPD_barrier) {
2300 // OpenMP [2.16, Nesting of Regions]
2301 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002302 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002303 NestingProhibited =
2304 isOpenMPWorksharingDirective(ParentRegion) ||
2305 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002306 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002307 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musman80c22892014-07-17 08:54:58 +00002308 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00002309 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002310 // OpenMP [2.16, Nesting of Regions]
2311 // A worksharing region may not be closely nested inside a worksharing,
2312 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002313 NestingProhibited =
Alexander Musmanf82886e2014-09-18 05:12:34 +00002314 isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002315 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002316 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002317 isOpenMPTaskLoopDirective(ParentRegion);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002318 Recommend = ShouldBeInParallelRegion;
2319 } else if (CurrentRegion == OMPD_ordered) {
2320 // OpenMP [2.16, Nesting of Regions]
2321 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00002322 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002323 // An ordered region must be closely nested inside a loop region (or
2324 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002325 // OpenMP [2.8.1,simd Construct, Restrictions]
2326 // An ordered construct with the simd clause is the only OpenMP construct
2327 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002328 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00002329 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002330 isOpenMPTaskLoopDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002331 !(isOpenMPSimdDirective(ParentRegion) ||
2332 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002333 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002334 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
2335 // OpenMP [2.16, Nesting of Regions]
2336 // If specified, a teams construct must be contained within a target
2337 // construct.
2338 NestingProhibited = ParentRegion != OMPD_target;
2339 Recommend = ShouldBeInTargetRegion;
2340 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
2341 }
2342 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
2343 // OpenMP [2.16, Nesting of Regions]
2344 // distribute, parallel, parallel sections, parallel workshare, and the
2345 // parallel loop and parallel loop SIMD constructs are the only OpenMP
2346 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002347 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
2348 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00002349 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002350 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002351 if (!NestingProhibited && isOpenMPDistributeDirective(CurrentRegion)) {
2352 // OpenMP 4.5 [2.17 Nesting of Regions]
2353 // The region associated with the distribute construct must be strictly
2354 // nested inside a teams region
2355 NestingProhibited = !isOpenMPTeamsDirective(ParentRegion);
2356 Recommend = ShouldBeInTeamsRegion;
2357 }
Alexey Bataev549210e2014-06-24 04:39:47 +00002358 if (NestingProhibited) {
2359 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002360 << CloseNesting << getOpenMPDirectiveName(ParentRegion) << Recommend
2361 << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00002362 return true;
2363 }
2364 }
2365 return false;
2366}
2367
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002368static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
2369 ArrayRef<OMPClause *> Clauses,
2370 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
2371 bool ErrorFound = false;
2372 unsigned NamedModifiersNumber = 0;
2373 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
2374 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00002375 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002376 for (const auto *C : Clauses) {
2377 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
2378 // At most one if clause without a directive-name-modifier can appear on
2379 // the directive.
2380 OpenMPDirectiveKind CurNM = IC->getNameModifier();
2381 if (FoundNameModifiers[CurNM]) {
2382 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
2383 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
2384 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
2385 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002386 } else if (CurNM != OMPD_unknown) {
2387 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002388 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002389 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002390 FoundNameModifiers[CurNM] = IC;
2391 if (CurNM == OMPD_unknown)
2392 continue;
2393 // Check if the specified name modifier is allowed for the current
2394 // directive.
2395 // At most one if clause with the particular directive-name-modifier can
2396 // appear on the directive.
2397 bool MatchFound = false;
2398 for (auto NM : AllowedNameModifiers) {
2399 if (CurNM == NM) {
2400 MatchFound = true;
2401 break;
2402 }
2403 }
2404 if (!MatchFound) {
2405 S.Diag(IC->getNameModifierLoc(),
2406 diag::err_omp_wrong_if_directive_name_modifier)
2407 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
2408 ErrorFound = true;
2409 }
2410 }
2411 }
2412 // If any if clause on the directive includes a directive-name-modifier then
2413 // all if clauses on the directive must include a directive-name-modifier.
2414 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
2415 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
2416 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
2417 diag::err_omp_no_more_if_clause);
2418 } else {
2419 std::string Values;
2420 std::string Sep(", ");
2421 unsigned AllowedCnt = 0;
2422 unsigned TotalAllowedNum =
2423 AllowedNameModifiers.size() - NamedModifiersNumber;
2424 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
2425 ++Cnt) {
2426 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
2427 if (!FoundNameModifiers[NM]) {
2428 Values += "'";
2429 Values += getOpenMPDirectiveName(NM);
2430 Values += "'";
2431 if (AllowedCnt + 2 == TotalAllowedNum)
2432 Values += " or ";
2433 else if (AllowedCnt + 1 != TotalAllowedNum)
2434 Values += Sep;
2435 ++AllowedCnt;
2436 }
2437 }
2438 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
2439 diag::err_omp_unnamed_if_clause)
2440 << (TotalAllowedNum > 1) << Values;
2441 }
Alexey Bataevecb156a2015-09-15 17:23:56 +00002442 for (auto Loc : NameModifierLoc) {
2443 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
2444 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002445 ErrorFound = true;
2446 }
2447 return ErrorFound;
2448}
2449
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002450StmtResult Sema::ActOnOpenMPExecutableDirective(
2451 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
2452 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
2453 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002454 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002455 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
2456 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00002457 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002458
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002459 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev68446b72014-07-18 07:47:19 +00002460 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002461 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00002462 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00002463 if (AStmt) {
2464 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
2465
2466 // Check default data sharing attributes for referenced variables.
2467 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
2468 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
2469 if (DSAChecker.isErrorFound())
2470 return StmtError();
2471 // Generate list of implicitly defined firstprivate variables.
2472 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00002473
2474 if (!DSAChecker.getImplicitFirstprivate().empty()) {
2475 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
2476 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
2477 SourceLocation(), SourceLocation())) {
2478 ClausesWithImplicit.push_back(Implicit);
2479 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
2480 DSAChecker.getImplicitFirstprivate().size();
2481 } else
2482 ErrorFound = true;
2483 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002484 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002485
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002486 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002487 switch (Kind) {
2488 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00002489 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
2490 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002491 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002492 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002493 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002494 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2495 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002496 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002497 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002498 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2499 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002500 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00002501 case OMPD_for_simd:
2502 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2503 EndLoc, VarsWithInheritedDSA);
2504 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002505 case OMPD_sections:
2506 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
2507 EndLoc);
2508 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002509 case OMPD_section:
2510 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00002511 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002512 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
2513 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002514 case OMPD_single:
2515 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
2516 EndLoc);
2517 break;
Alexander Musman80c22892014-07-17 08:54:58 +00002518 case OMPD_master:
2519 assert(ClausesWithImplicit.empty() &&
2520 "No clauses are allowed for 'omp master' directive");
2521 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
2522 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002523 case OMPD_critical:
2524 assert(ClausesWithImplicit.empty() &&
2525 "No clauses are allowed for 'omp critical' directive");
2526 Res = ActOnOpenMPCriticalDirective(DirName, AStmt, StartLoc, EndLoc);
2527 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002528 case OMPD_parallel_for:
2529 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
2530 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002531 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00002532 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00002533 case OMPD_parallel_for_simd:
2534 Res = ActOnOpenMPParallelForSimdDirective(
2535 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002536 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00002537 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002538 case OMPD_parallel_sections:
2539 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
2540 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002541 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002542 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002543 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002544 Res =
2545 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002546 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002547 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00002548 case OMPD_taskyield:
2549 assert(ClausesWithImplicit.empty() &&
2550 "No clauses are allowed for 'omp taskyield' directive");
2551 assert(AStmt == nullptr &&
2552 "No associated statement allowed for 'omp taskyield' directive");
2553 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
2554 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002555 case OMPD_barrier:
2556 assert(ClausesWithImplicit.empty() &&
2557 "No clauses are allowed for 'omp barrier' directive");
2558 assert(AStmt == nullptr &&
2559 "No associated statement allowed for 'omp barrier' directive");
2560 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
2561 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00002562 case OMPD_taskwait:
2563 assert(ClausesWithImplicit.empty() &&
2564 "No clauses are allowed for 'omp taskwait' directive");
2565 assert(AStmt == nullptr &&
2566 "No associated statement allowed for 'omp taskwait' directive");
2567 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
2568 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002569 case OMPD_taskgroup:
2570 assert(ClausesWithImplicit.empty() &&
2571 "No clauses are allowed for 'omp taskgroup' directive");
2572 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
2573 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00002574 case OMPD_flush:
2575 assert(AStmt == nullptr &&
2576 "No associated statement allowed for 'omp flush' directive");
2577 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
2578 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002579 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00002580 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
2581 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002582 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00002583 case OMPD_atomic:
2584 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
2585 EndLoc);
2586 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002587 case OMPD_teams:
2588 Res =
2589 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
2590 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002591 case OMPD_target:
2592 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
2593 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002594 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002595 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002596 case OMPD_cancellation_point:
2597 assert(ClausesWithImplicit.empty() &&
2598 "No clauses are allowed for 'omp cancellation point' directive");
2599 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
2600 "cancellation point' directive");
2601 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
2602 break;
Alexey Bataev80909872015-07-02 11:25:17 +00002603 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00002604 assert(AStmt == nullptr &&
2605 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00002606 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
2607 CancelRegion);
2608 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00002609 break;
Michael Wong65f367f2015-07-21 13:44:28 +00002610 case OMPD_target_data:
2611 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
2612 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002613 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00002614 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00002615 case OMPD_taskloop:
2616 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
2617 EndLoc, VarsWithInheritedDSA);
2618 AllowedNameModifiers.push_back(OMPD_taskloop);
2619 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002620 case OMPD_taskloop_simd:
2621 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2622 EndLoc, VarsWithInheritedDSA);
2623 AllowedNameModifiers.push_back(OMPD_taskloop);
2624 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002625 case OMPD_distribute:
2626 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
2627 EndLoc, VarsWithInheritedDSA);
2628 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002629 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002630 llvm_unreachable("OpenMP Directive is not allowed");
2631 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002632 llvm_unreachable("Unknown OpenMP directive");
2633 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002634
Alexey Bataev4acb8592014-07-07 13:01:15 +00002635 for (auto P : VarsWithInheritedDSA) {
2636 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
2637 << P.first << P.second->getSourceRange();
2638 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002639 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
2640
2641 if (!AllowedNameModifiers.empty())
2642 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
2643 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002644
Alexey Bataeved09d242014-05-28 05:53:51 +00002645 if (ErrorFound)
2646 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002647 return Res;
2648}
2649
2650StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
2651 Stmt *AStmt,
2652 SourceLocation StartLoc,
2653 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002654 if (!AStmt)
2655 return StmtError();
2656
Alexey Bataev9959db52014-05-06 10:08:46 +00002657 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
2658 // 1.2.2 OpenMP Language Terminology
2659 // Structured block - An executable statement with a single entry at the
2660 // top and a single exit at the bottom.
2661 // The point of exit cannot be a branch out of the structured block.
2662 // longjmp() and throw() must not violate the entry/exit criteria.
2663 CS->getCapturedDecl()->setNothrow();
2664
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002665 getCurFunction()->setHasBranchProtectedScope();
2666
Alexey Bataev25e5b442015-09-15 12:52:43 +00002667 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
2668 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002669}
2670
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002671namespace {
2672/// \brief Helper class for checking canonical form of the OpenMP loops and
2673/// extracting iteration space of each loop in the loop nest, that will be used
2674/// for IR generation.
2675class OpenMPIterationSpaceChecker {
2676 /// \brief Reference to Sema.
2677 Sema &SemaRef;
2678 /// \brief A location for diagnostics (when there is no some better location).
2679 SourceLocation DefaultLoc;
2680 /// \brief A location for diagnostics (when increment is not compatible).
2681 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002682 /// \brief A source location for referring to loop init later.
2683 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002684 /// \brief A source location for referring to condition later.
2685 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002686 /// \brief A source location for referring to increment later.
2687 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002688 /// \brief Loop variable.
2689 VarDecl *Var;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002690 /// \brief Reference to loop variable.
2691 DeclRefExpr *VarRef;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002692 /// \brief Lower bound (initializer for the var).
2693 Expr *LB;
2694 /// \brief Upper bound.
2695 Expr *UB;
2696 /// \brief Loop step (increment).
2697 Expr *Step;
2698 /// \brief This flag is true when condition is one of:
2699 /// Var < UB
2700 /// Var <= UB
2701 /// UB > Var
2702 /// UB >= Var
2703 bool TestIsLessOp;
2704 /// \brief This flag is true when condition is strict ( < or > ).
2705 bool TestIsStrictOp;
2706 /// \brief This flag is true when step is subtracted on each iteration.
2707 bool SubtractStep;
2708
2709public:
2710 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
2711 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
Alexander Musmana5f070a2014-10-01 06:03:56 +00002712 InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()),
2713 IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002714 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
2715 TestIsStrictOp(false), SubtractStep(false) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002716 /// \brief Check init-expr for canonical loop form and save loop counter
2717 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00002718 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002719 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
2720 /// for less/greater and for strict/non-strict comparison.
2721 bool CheckCond(Expr *S);
2722 /// \brief Check incr-expr for canonical loop form and return true if it
2723 /// does not conform, otherwise save loop step (#Step).
2724 bool CheckInc(Expr *S);
2725 /// \brief Return the loop counter variable.
2726 VarDecl *GetLoopVar() const { return Var; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002727 /// \brief Return the reference expression to loop counter variable.
2728 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002729 /// \brief Source range of the loop init.
2730 SourceRange GetInitSrcRange() const { return InitSrcRange; }
2731 /// \brief Source range of the loop condition.
2732 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
2733 /// \brief Source range of the loop increment.
2734 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
2735 /// \brief True if the step should be subtracted.
2736 bool ShouldSubtractStep() const { return SubtractStep; }
2737 /// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00002738 Expr *BuildNumIterations(Scope *S, const bool LimitedType) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00002739 /// \brief Build the precondition expression for the loops.
2740 Expr *BuildPreCond(Scope *S, Expr *Cond) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002741 /// \brief Build reference expression to the counter be used for codegen.
2742 Expr *BuildCounterVar() const;
Alexey Bataeva8899172015-08-06 12:30:57 +00002743 /// \brief Build reference expression to the private counter be used for
2744 /// codegen.
2745 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002746 /// \brief Build initization of the counter be used for codegen.
2747 Expr *BuildCounterInit() const;
2748 /// \brief Build step of the counter be used for codegen.
2749 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002750 /// \brief Return true if any expression is dependent.
2751 bool Dependent() const;
2752
2753private:
2754 /// \brief Check the right-hand side of an assignment in the increment
2755 /// expression.
2756 bool CheckIncRHS(Expr *RHS);
2757 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002758 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002759 /// \brief Helper to set upper bound.
Craig Toppere335f252015-10-04 04:53:55 +00002760 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00002761 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002762 /// \brief Helper to set loop increment.
2763 bool SetStep(Expr *NewStep, bool Subtract);
2764};
2765
2766bool OpenMPIterationSpaceChecker::Dependent() const {
2767 if (!Var) {
2768 assert(!LB && !UB && !Step);
2769 return false;
2770 }
2771 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
2772 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
2773}
2774
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002775template <typename T>
2776static T *getExprAsWritten(T *E) {
2777 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
2778 E = ExprTemp->getSubExpr();
2779
2780 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
2781 E = MTE->GetTemporaryExpr();
2782
2783 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
2784 E = Binder->getSubExpr();
2785
2786 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
2787 E = ICE->getSubExprAsWritten();
2788 return E->IgnoreParens();
2789}
2790
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002791bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
2792 DeclRefExpr *NewVarRefExpr,
2793 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002794 // State consistency checking to ensure correct usage.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002795 assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
2796 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002797 if (!NewVar || !NewLB)
2798 return true;
2799 Var = NewVar;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002800 VarRef = NewVarRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002801 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
2802 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00002803 if ((Ctor->isCopyOrMoveConstructor() ||
2804 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
2805 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002806 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002807 LB = NewLB;
2808 return false;
2809}
2810
2811bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00002812 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002813 // State consistency checking to ensure correct usage.
2814 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
2815 !TestIsLessOp && !TestIsStrictOp);
2816 if (!NewUB)
2817 return true;
2818 UB = NewUB;
2819 TestIsLessOp = LessOp;
2820 TestIsStrictOp = StrictOp;
2821 ConditionSrcRange = SR;
2822 ConditionLoc = SL;
2823 return false;
2824}
2825
2826bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
2827 // State consistency checking to ensure correct usage.
2828 assert(Var != nullptr && LB != nullptr && Step == nullptr);
2829 if (!NewStep)
2830 return true;
2831 if (!NewStep->isValueDependent()) {
2832 // Check that the step is integer expression.
2833 SourceLocation StepLoc = NewStep->getLocStart();
2834 ExprResult Val =
2835 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
2836 if (Val.isInvalid())
2837 return true;
2838 NewStep = Val.get();
2839
2840 // OpenMP [2.6, Canonical Loop Form, Restrictions]
2841 // If test-expr is of form var relational-op b and relational-op is < or
2842 // <= then incr-expr must cause var to increase on each iteration of the
2843 // loop. If test-expr is of form var relational-op b and relational-op is
2844 // > or >= then incr-expr must cause var to decrease on each iteration of
2845 // the loop.
2846 // If test-expr is of form b relational-op var and relational-op is < or
2847 // <= then incr-expr must cause var to decrease on each iteration of the
2848 // loop. If test-expr is of form b relational-op var and relational-op is
2849 // > or >= then incr-expr must cause var to increase on each iteration of
2850 // the loop.
2851 llvm::APSInt Result;
2852 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
2853 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
2854 bool IsConstNeg =
2855 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00002856 bool IsConstPos =
2857 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002858 bool IsConstZero = IsConstant && !Result.getBoolValue();
2859 if (UB && (IsConstZero ||
2860 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00002861 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002862 SemaRef.Diag(NewStep->getExprLoc(),
2863 diag::err_omp_loop_incr_not_compatible)
2864 << Var << TestIsLessOp << NewStep->getSourceRange();
2865 SemaRef.Diag(ConditionLoc,
2866 diag::note_omp_loop_cond_requres_compatible_incr)
2867 << TestIsLessOp << ConditionSrcRange;
2868 return true;
2869 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002870 if (TestIsLessOp == Subtract) {
2871 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
2872 NewStep).get();
2873 Subtract = !Subtract;
2874 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002875 }
2876
2877 Step = NewStep;
2878 SubtractStep = Subtract;
2879 return false;
2880}
2881
Alexey Bataev9c821032015-04-30 04:23:23 +00002882bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002883 // Check init-expr for canonical loop form and save loop counter
2884 // variable - #Var and its initialization value - #LB.
2885 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
2886 // var = lb
2887 // integer-type var = lb
2888 // random-access-iterator-type var = lb
2889 // pointer-type var = lb
2890 //
2891 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00002892 if (EmitDiags) {
2893 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
2894 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002895 return true;
2896 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002897 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002898 if (Expr *E = dyn_cast<Expr>(S))
2899 S = E->IgnoreParens();
2900 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2901 if (BO->getOpcode() == BO_Assign)
2902 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002903 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002904 BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002905 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
2906 if (DS->isSingleDecl()) {
2907 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00002908 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002909 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00002910 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002911 SemaRef.Diag(S->getLocStart(),
2912 diag::ext_omp_loop_not_canonical_init)
2913 << S->getSourceRange();
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002914 return SetVarAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002915 }
2916 }
2917 }
2918 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
2919 if (CE->getOperator() == OO_Equal)
2920 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002921 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
2922 CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002923
Alexey Bataev9c821032015-04-30 04:23:23 +00002924 if (EmitDiags) {
2925 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
2926 << S->getSourceRange();
2927 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002928 return true;
2929}
2930
Alexey Bataev23b69422014-06-18 07:08:49 +00002931/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002932/// variable (which may be the loop variable) if possible.
2933static const VarDecl *GetInitVarDecl(const Expr *E) {
2934 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00002935 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002936 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002937 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
2938 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00002939 if ((Ctor->isCopyOrMoveConstructor() ||
2940 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
2941 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002942 E = CE->getArg(0)->IgnoreParenImpCasts();
2943 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
2944 if (!DRE)
2945 return nullptr;
2946 return dyn_cast<VarDecl>(DRE->getDecl());
2947}
2948
2949bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
2950 // Check test-expr for canonical form, save upper-bound UB, flags for
2951 // less/greater and for strict/non-strict comparison.
2952 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2953 // var relational-op b
2954 // b relational-op var
2955 //
2956 if (!S) {
2957 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
2958 return true;
2959 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002960 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002961 SourceLocation CondLoc = S->getLocStart();
2962 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2963 if (BO->isRelationalOp()) {
2964 if (GetInitVarDecl(BO->getLHS()) == Var)
2965 return SetUB(BO->getRHS(),
2966 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
2967 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2968 BO->getSourceRange(), BO->getOperatorLoc());
2969 if (GetInitVarDecl(BO->getRHS()) == Var)
2970 return SetUB(BO->getLHS(),
2971 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
2972 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2973 BO->getSourceRange(), BO->getOperatorLoc());
2974 }
2975 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2976 if (CE->getNumArgs() == 2) {
2977 auto Op = CE->getOperator();
2978 switch (Op) {
2979 case OO_Greater:
2980 case OO_GreaterEqual:
2981 case OO_Less:
2982 case OO_LessEqual:
2983 if (GetInitVarDecl(CE->getArg(0)) == Var)
2984 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
2985 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2986 CE->getOperatorLoc());
2987 if (GetInitVarDecl(CE->getArg(1)) == Var)
2988 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
2989 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2990 CE->getOperatorLoc());
2991 break;
2992 default:
2993 break;
2994 }
2995 }
2996 }
2997 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
2998 << S->getSourceRange() << Var;
2999 return true;
3000}
3001
3002bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
3003 // RHS of canonical loop form increment can be:
3004 // var + incr
3005 // incr + var
3006 // var - incr
3007 //
3008 RHS = RHS->IgnoreParenImpCasts();
3009 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
3010 if (BO->isAdditiveOp()) {
3011 bool IsAdd = BO->getOpcode() == BO_Add;
3012 if (GetInitVarDecl(BO->getLHS()) == Var)
3013 return SetStep(BO->getRHS(), !IsAdd);
3014 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
3015 return SetStep(BO->getLHS(), false);
3016 }
3017 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
3018 bool IsAdd = CE->getOperator() == OO_Plus;
3019 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
3020 if (GetInitVarDecl(CE->getArg(0)) == Var)
3021 return SetStep(CE->getArg(1), !IsAdd);
3022 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
3023 return SetStep(CE->getArg(0), false);
3024 }
3025 }
3026 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3027 << RHS->getSourceRange() << Var;
3028 return true;
3029}
3030
3031bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
3032 // Check incr-expr for canonical loop form and return true if it
3033 // does not conform.
3034 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3035 // ++var
3036 // var++
3037 // --var
3038 // var--
3039 // var += incr
3040 // var -= incr
3041 // var = var + incr
3042 // var = incr + var
3043 // var = var - incr
3044 //
3045 if (!S) {
3046 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
3047 return true;
3048 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003049 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003050 S = S->IgnoreParens();
3051 if (auto UO = dyn_cast<UnaryOperator>(S)) {
3052 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
3053 return SetStep(
3054 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
3055 (UO->isDecrementOp() ? -1 : 1)).get(),
3056 false);
3057 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
3058 switch (BO->getOpcode()) {
3059 case BO_AddAssign:
3060 case BO_SubAssign:
3061 if (GetInitVarDecl(BO->getLHS()) == Var)
3062 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
3063 break;
3064 case BO_Assign:
3065 if (GetInitVarDecl(BO->getLHS()) == Var)
3066 return CheckIncRHS(BO->getRHS());
3067 break;
3068 default:
3069 break;
3070 }
3071 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3072 switch (CE->getOperator()) {
3073 case OO_PlusPlus:
3074 case OO_MinusMinus:
3075 if (GetInitVarDecl(CE->getArg(0)) == Var)
3076 return SetStep(
3077 SemaRef.ActOnIntegerConstant(
3078 CE->getLocStart(),
3079 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
3080 false);
3081 break;
3082 case OO_PlusEqual:
3083 case OO_MinusEqual:
3084 if (GetInitVarDecl(CE->getArg(0)) == Var)
3085 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
3086 break;
3087 case OO_Equal:
3088 if (GetInitVarDecl(CE->getArg(0)) == Var)
3089 return CheckIncRHS(CE->getArg(1));
3090 break;
3091 default:
3092 break;
3093 }
3094 }
3095 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3096 << S->getSourceRange() << Var;
3097 return true;
3098}
Alexander Musmana5f070a2014-10-01 06:03:56 +00003099
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003100namespace {
3101// Transform variables declared in GNU statement expressions to new ones to
3102// avoid crash on codegen.
3103class TransformToNewDefs : public TreeTransform<TransformToNewDefs> {
3104 typedef TreeTransform<TransformToNewDefs> BaseTransform;
3105
3106public:
3107 TransformToNewDefs(Sema &SemaRef) : BaseTransform(SemaRef) {}
3108
3109 Decl *TransformDefinition(SourceLocation Loc, Decl *D) {
3110 if (auto *VD = cast<VarDecl>(D))
3111 if (!isa<ParmVarDecl>(D) && !isa<VarTemplateSpecializationDecl>(D) &&
3112 !isa<ImplicitParamDecl>(D)) {
3113 auto *NewVD = VarDecl::Create(
3114 SemaRef.Context, VD->getDeclContext(), VD->getLocStart(),
3115 VD->getLocation(), VD->getIdentifier(), VD->getType(),
3116 VD->getTypeSourceInfo(), VD->getStorageClass());
3117 NewVD->setTSCSpec(VD->getTSCSpec());
3118 NewVD->setInit(VD->getInit());
3119 NewVD->setInitStyle(VD->getInitStyle());
3120 NewVD->setExceptionVariable(VD->isExceptionVariable());
3121 NewVD->setNRVOVariable(VD->isNRVOVariable());
3122 NewVD->setCXXForRangeDecl(VD->isInExternCXXContext());
3123 NewVD->setConstexpr(VD->isConstexpr());
3124 NewVD->setInitCapture(VD->isInitCapture());
3125 NewVD->setPreviousDeclInSameBlockScope(
3126 VD->isPreviousDeclInSameBlockScope());
3127 VD->getDeclContext()->addHiddenDecl(NewVD);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003128 if (VD->hasAttrs())
3129 NewVD->setAttrs(VD->getAttrs());
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003130 transformedLocalDecl(VD, NewVD);
3131 return NewVD;
3132 }
3133 return BaseTransform::TransformDefinition(Loc, D);
3134 }
3135
3136 ExprResult TransformDeclRefExpr(DeclRefExpr *E) {
3137 if (auto *NewD = TransformDecl(E->getExprLoc(), E->getDecl()))
3138 if (E->getDecl() != NewD) {
3139 NewD->setReferenced();
3140 NewD->markUsed(SemaRef.Context);
3141 return DeclRefExpr::Create(
3142 SemaRef.Context, E->getQualifierLoc(), E->getTemplateKeywordLoc(),
3143 cast<ValueDecl>(NewD), E->refersToEnclosingVariableOrCapture(),
3144 E->getNameInfo(), E->getType(), E->getValueKind());
3145 }
3146 return BaseTransform::TransformDeclRefExpr(E);
3147 }
3148};
3149}
3150
Alexander Musmana5f070a2014-10-01 06:03:56 +00003151/// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00003152Expr *
3153OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S,
3154 const bool LimitedType) const {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003155 TransformToNewDefs Transform(SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003156 ExprResult Diff;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003157 auto VarType = Var->getType().getNonReferenceType();
3158 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003159 SemaRef.getLangOpts().CPlusPlus) {
3160 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003161 auto *UBExpr = TestIsLessOp ? UB : LB;
3162 auto *LBExpr = TestIsLessOp ? LB : UB;
3163 Expr *Upper = Transform.TransformExpr(UBExpr).get();
3164 Expr *Lower = Transform.TransformExpr(LBExpr).get();
3165 if (!Upper || !Lower)
3166 return nullptr;
3167 Upper = SemaRef.PerformImplicitConversion(Upper, UBExpr->getType(),
3168 Sema::AA_Converting,
3169 /*AllowExplicit=*/true)
3170 .get();
3171 Lower = SemaRef.PerformImplicitConversion(Lower, LBExpr->getType(),
3172 Sema::AA_Converting,
3173 /*AllowExplicit=*/true)
3174 .get();
3175 if (!Upper || !Lower)
3176 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003177
3178 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
3179
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003180 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00003181 // BuildBinOp already emitted error, this one is to point user to upper
3182 // and lower bound, and to tell what is passed to 'operator-'.
3183 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
3184 << Upper->getSourceRange() << Lower->getSourceRange();
3185 return nullptr;
3186 }
3187 }
3188
3189 if (!Diff.isUsable())
3190 return nullptr;
3191
3192 // Upper - Lower [- 1]
3193 if (TestIsStrictOp)
3194 Diff = SemaRef.BuildBinOp(
3195 S, DefaultLoc, BO_Sub, Diff.get(),
3196 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3197 if (!Diff.isUsable())
3198 return nullptr;
3199
3200 // Upper - Lower [- 1] + Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003201 auto NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
3202 if (NewStep.isInvalid())
3203 return nullptr;
3204 NewStep = SemaRef.PerformImplicitConversion(
3205 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
3206 /*AllowExplicit=*/true);
3207 if (NewStep.isInvalid())
3208 return nullptr;
3209 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003210 if (!Diff.isUsable())
3211 return nullptr;
3212
3213 // Parentheses (for dumping/debugging purposes only).
3214 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
3215 if (!Diff.isUsable())
3216 return nullptr;
3217
3218 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003219 NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
3220 if (NewStep.isInvalid())
3221 return nullptr;
3222 NewStep = SemaRef.PerformImplicitConversion(
3223 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
3224 /*AllowExplicit=*/true);
3225 if (NewStep.isInvalid())
3226 return nullptr;
3227 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003228 if (!Diff.isUsable())
3229 return nullptr;
3230
Alexander Musman174b3ca2014-10-06 11:16:29 +00003231 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003232 QualType Type = Diff.get()->getType();
3233 auto &C = SemaRef.Context;
3234 bool UseVarType = VarType->hasIntegerRepresentation() &&
3235 C.getTypeSize(Type) > C.getTypeSize(VarType);
3236 if (!Type->isIntegerType() || UseVarType) {
3237 unsigned NewSize =
3238 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
3239 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
3240 : Type->hasSignedIntegerRepresentation();
3241 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
3242 Diff = SemaRef.PerformImplicitConversion(
3243 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
3244 if (!Diff.isUsable())
3245 return nullptr;
3246 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00003247 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00003248 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
3249 if (NewSize != C.getTypeSize(Type)) {
3250 if (NewSize < C.getTypeSize(Type)) {
3251 assert(NewSize == 64 && "incorrect loop var size");
3252 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
3253 << InitSrcRange << ConditionSrcRange;
3254 }
3255 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003256 NewSize, Type->hasSignedIntegerRepresentation() ||
3257 C.getTypeSize(Type) < NewSize);
Alexander Musman174b3ca2014-10-06 11:16:29 +00003258 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
3259 Sema::AA_Converting, true);
3260 if (!Diff.isUsable())
3261 return nullptr;
3262 }
3263 }
3264
Alexander Musmana5f070a2014-10-01 06:03:56 +00003265 return Diff.get();
3266}
3267
Alexey Bataev62dbb972015-04-22 11:59:37 +00003268Expr *OpenMPIterationSpaceChecker::BuildPreCond(Scope *S, Expr *Cond) const {
3269 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
3270 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
3271 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003272 TransformToNewDefs Transform(SemaRef);
3273
3274 auto NewLB = Transform.TransformExpr(LB);
3275 auto NewUB = Transform.TransformExpr(UB);
3276 if (NewLB.isInvalid() || NewUB.isInvalid())
3277 return Cond;
3278 NewLB = SemaRef.PerformImplicitConversion(NewLB.get(), LB->getType(),
3279 Sema::AA_Converting,
3280 /*AllowExplicit=*/true);
3281 NewUB = SemaRef.PerformImplicitConversion(NewUB.get(), UB->getType(),
3282 Sema::AA_Converting,
3283 /*AllowExplicit=*/true);
3284 if (NewLB.isInvalid() || NewUB.isInvalid())
3285 return Cond;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003286 auto CondExpr = SemaRef.BuildBinOp(
3287 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
3288 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003289 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003290 if (CondExpr.isUsable()) {
3291 CondExpr = SemaRef.PerformImplicitConversion(
3292 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
3293 /*AllowExplicit=*/true);
3294 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00003295 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
3296 // Otherwise use original loop conditon and evaluate it in runtime.
3297 return CondExpr.isUsable() ? CondExpr.get() : Cond;
3298}
3299
Alexander Musmana5f070a2014-10-01 06:03:56 +00003300/// \brief Build reference expression to the counter be used for codegen.
3301Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const {
Alexey Bataeva8899172015-08-06 12:30:57 +00003302 return buildDeclRefExpr(SemaRef, Var, Var->getType().getNonReferenceType(),
3303 DefaultLoc);
3304}
3305
3306Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
3307 if (Var && !Var->isInvalidDecl()) {
3308 auto Type = Var->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003309 auto *PrivateVar =
3310 buildVarDecl(SemaRef, DefaultLoc, Type, Var->getName(),
3311 Var->hasAttrs() ? &Var->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00003312 if (PrivateVar->isInvalidDecl())
3313 return nullptr;
3314 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
3315 }
3316 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003317}
3318
3319/// \brief Build initization of the counter be used for codegen.
3320Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
3321
3322/// \brief Build step of the counter be used for codegen.
3323Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
3324
3325/// \brief Iteration space of a single for loop.
3326struct LoopIterationSpace {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003327 /// \brief Condition of the loop.
3328 Expr *PreCond;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003329 /// \brief This expression calculates the number of iterations in the loop.
3330 /// It is always possible to calculate it before starting the loop.
3331 Expr *NumIterations;
3332 /// \brief The loop counter variable.
3333 Expr *CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00003334 /// \brief Private loop counter variable.
3335 Expr *PrivateCounterVar;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003336 /// \brief This is initializer for the initial value of #CounterVar.
3337 Expr *CounterInit;
3338 /// \brief This is step for the #CounterVar used to generate its update:
3339 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
3340 Expr *CounterStep;
3341 /// \brief Should step be subtracted?
3342 bool Subtract;
3343 /// \brief Source range of the loop init.
3344 SourceRange InitSrcRange;
3345 /// \brief Source range of the loop condition.
3346 SourceRange CondSrcRange;
3347 /// \brief Source range of the loop increment.
3348 SourceRange IncSrcRange;
3349};
3350
Alexey Bataev23b69422014-06-18 07:08:49 +00003351} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003352
Alexey Bataev9c821032015-04-30 04:23:23 +00003353void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
3354 assert(getLangOpts().OpenMP && "OpenMP is not active.");
3355 assert(Init && "Expected loop in canonical form.");
3356 unsigned CollapseIteration = DSAStack->getCollapseNumber();
3357 if (CollapseIteration > 0 &&
3358 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
3359 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
3360 if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
3361 DSAStack->addLoopControlVariable(ISC.GetLoopVar());
3362 }
3363 DSAStack->setCollapseNumber(CollapseIteration - 1);
3364 }
3365}
3366
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003367/// \brief Called on a for stmt to check and extract its iteration space
3368/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00003369static bool CheckOpenMPIterationSpace(
3370 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
3371 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003372 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003373 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
3374 LoopIterationSpace &ResultIterSpace) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003375 // OpenMP [2.6, Canonical Loop Form]
3376 // for (init-expr; test-expr; incr-expr) structured-block
3377 auto For = dyn_cast_or_null<ForStmt>(S);
3378 if (!For) {
3379 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003380 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
3381 << getOpenMPDirectiveName(DKind) << NestedLoopCount
3382 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
3383 if (NestedLoopCount > 1) {
3384 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
3385 SemaRef.Diag(DSA.getConstructLoc(),
3386 diag::note_omp_collapse_ordered_expr)
3387 << 2 << CollapseLoopCountExpr->getSourceRange()
3388 << OrderedLoopCountExpr->getSourceRange();
3389 else if (CollapseLoopCountExpr)
3390 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
3391 diag::note_omp_collapse_ordered_expr)
3392 << 0 << CollapseLoopCountExpr->getSourceRange();
3393 else
3394 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
3395 diag::note_omp_collapse_ordered_expr)
3396 << 1 << OrderedLoopCountExpr->getSourceRange();
3397 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003398 return true;
3399 }
3400 assert(For->getBody());
3401
3402 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
3403
3404 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003405 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003406 if (ISC.CheckInit(Init)) {
3407 return true;
3408 }
3409
3410 bool HasErrors = false;
3411
3412 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003413 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003414
3415 // OpenMP [2.6, Canonical Loop Form]
3416 // Var is one of the following:
3417 // A variable of signed or unsigned integer type.
3418 // For C++, a variable of a random access iterator type.
3419 // For C, a variable of a pointer type.
Alexey Bataeva8899172015-08-06 12:30:57 +00003420 auto VarType = Var->getType().getNonReferenceType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003421 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
3422 !VarType->isPointerType() &&
3423 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
3424 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
3425 << SemaRef.getLangOpts().CPlusPlus;
3426 HasErrors = true;
3427 }
3428
Alexey Bataev4acb8592014-07-07 13:01:15 +00003429 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
3430 // Construct
3431 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3432 // parallel for construct is (are) private.
3433 // The loop iteration variable in the associated for-loop of a simd construct
3434 // with just one associated for-loop is linear with a constant-linear-step
3435 // that is the increment of the associated for-loop.
3436 // Exclude loop var from the list of variables with implicitly defined data
3437 // sharing attributes.
Benjamin Kramerad8e0792014-10-10 15:32:48 +00003438 VarsWithImplicitDSA.erase(Var);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003439
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003440 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
3441 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00003442 // The loop iteration variable in the associated for-loop of a simd construct
3443 // with just one associated for-loop may be listed in a linear clause with a
3444 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00003445 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3446 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003447 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003448 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
3449 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
3450 // declared in the loop and it is predetermined as a private.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003451 auto PredeterminedCKind =
3452 isOpenMPSimdDirective(DKind)
3453 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
3454 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003455 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataev0c024df2015-05-12 09:02:07 +00003456 DVar.CKind != OMPC_threadprivate && DVar.CKind != PredeterminedCKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003457 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
3458 isOpenMPDistributeDirective(DKind)) &&
Alexey Bataev49f6e782015-12-01 04:18:41 +00003459 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
3460 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate &&
3461 DVar.CKind != OMPC_threadprivate)) &&
Alexey Bataev0c024df2015-05-12 09:02:07 +00003462 ((DVar.CKind != OMPC_private && DVar.CKind != OMPC_threadprivate) ||
3463 DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003464 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00003465 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
3466 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataevf2453a02015-05-06 07:25:08 +00003467 if (DVar.RefExpr == nullptr)
3468 DVar.CKind = PredeterminedCKind;
3469 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, /*IsLoopIterVar=*/true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003470 HasErrors = true;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003471 } else if (LoopVarRefExpr != nullptr) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00003472 // Make the loop iteration variable private (for worksharing constructs),
3473 // linear (for simd directives with the only one associated loop) or
Alexey Bataev10e775f2015-07-30 11:36:16 +00003474 // lastprivate (for simd directives with several collapsed or ordered
3475 // loops).
Alexey Bataev9aba41c2014-11-14 04:08:45 +00003476 if (DVar.CKind == OMPC_unknown)
3477 DVar = DSA.hasDSA(Var, isOpenMPPrivate, MatchesAlways(),
3478 /*FromParent=*/false);
Alexey Bataev9c821032015-04-30 04:23:23 +00003479 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003480 }
3481
Alexey Bataev7ff55242014-06-19 09:13:45 +00003482 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00003483
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003484 // Check test-expr.
3485 HasErrors |= ISC.CheckCond(For->getCond());
3486
3487 // Check incr-expr.
3488 HasErrors |= ISC.CheckInc(For->getInc());
3489
Alexander Musmana5f070a2014-10-01 06:03:56 +00003490 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003491 return HasErrors;
3492
Alexander Musmana5f070a2014-10-01 06:03:56 +00003493 // Build the loop's iteration space representation.
Alexey Bataev62dbb972015-04-22 11:59:37 +00003494 ResultIterSpace.PreCond = ISC.BuildPreCond(DSA.getCurScope(), For->getCond());
Alexander Musman174b3ca2014-10-06 11:16:29 +00003495 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003496 DSA.getCurScope(), (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003497 isOpenMPTaskLoopDirective(DKind) ||
3498 isOpenMPDistributeDirective(DKind)));
Alexander Musmana5f070a2014-10-01 06:03:56 +00003499 ResultIterSpace.CounterVar = ISC.BuildCounterVar();
Alexey Bataeva8899172015-08-06 12:30:57 +00003500 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003501 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
3502 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
3503 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
3504 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
3505 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
3506 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
3507
Alexey Bataev62dbb972015-04-22 11:59:37 +00003508 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
3509 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003510 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00003511 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003512 ResultIterSpace.CounterInit == nullptr ||
3513 ResultIterSpace.CounterStep == nullptr);
3514
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003515 return HasErrors;
3516}
3517
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003518/// \brief Build 'VarRef = Start.
3519static ExprResult BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc,
3520 ExprResult VarRef, ExprResult Start) {
3521 TransformToNewDefs Transform(SemaRef);
3522 // Build 'VarRef = Start.
3523 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
3524 if (NewStart.isInvalid())
3525 return ExprError();
3526 NewStart = SemaRef.PerformImplicitConversion(
3527 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
3528 Sema::AA_Converting,
3529 /*AllowExplicit=*/true);
3530 if (NewStart.isInvalid())
3531 return ExprError();
3532 NewStart = SemaRef.PerformImplicitConversion(
3533 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
3534 /*AllowExplicit=*/true);
3535 if (!NewStart.isUsable())
3536 return ExprError();
3537
3538 auto Init =
3539 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
3540 return Init;
3541}
3542
Alexander Musmana5f070a2014-10-01 06:03:56 +00003543/// \brief Build 'VarRef = Start + Iter * Step'.
3544static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S,
3545 SourceLocation Loc, ExprResult VarRef,
3546 ExprResult Start, ExprResult Iter,
3547 ExprResult Step, bool Subtract) {
3548 // Add parentheses (for debugging purposes only).
3549 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
3550 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
3551 !Step.isUsable())
3552 return ExprError();
3553
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003554 TransformToNewDefs Transform(SemaRef);
3555 auto NewStep = Transform.TransformExpr(Step.get()->IgnoreImplicit());
3556 if (NewStep.isInvalid())
3557 return ExprError();
3558 NewStep = SemaRef.PerformImplicitConversion(
3559 NewStep.get(), Step.get()->IgnoreImplicit()->getType(),
3560 Sema::AA_Converting,
3561 /*AllowExplicit=*/true);
3562 if (NewStep.isInvalid())
3563 return ExprError();
3564 ExprResult Update =
3565 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003566 if (!Update.isUsable())
3567 return ExprError();
3568
3569 // Build 'VarRef = Start + Iter * Step'.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003570 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
3571 if (NewStart.isInvalid())
3572 return ExprError();
3573 NewStart = SemaRef.PerformImplicitConversion(
3574 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
3575 Sema::AA_Converting,
3576 /*AllowExplicit=*/true);
3577 if (NewStart.isInvalid())
3578 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003579 Update = SemaRef.BuildBinOp(S, Loc, (Subtract ? BO_Sub : BO_Add),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003580 NewStart.get(), Update.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003581 if (!Update.isUsable())
3582 return ExprError();
3583
3584 Update = SemaRef.PerformImplicitConversion(
3585 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
3586 if (!Update.isUsable())
3587 return ExprError();
3588
3589 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
3590 return Update;
3591}
3592
3593/// \brief Convert integer expression \a E to make it have at least \a Bits
3594/// bits.
3595static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
3596 Sema &SemaRef) {
3597 if (E == nullptr)
3598 return ExprError();
3599 auto &C = SemaRef.Context;
3600 QualType OldType = E->getType();
3601 unsigned HasBits = C.getTypeSize(OldType);
3602 if (HasBits >= Bits)
3603 return ExprResult(E);
3604 // OK to convert to signed, because new type has more bits than old.
3605 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
3606 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
3607 true);
3608}
3609
3610/// \brief Check if the given expression \a E is a constant integer that fits
3611/// into \a Bits bits.
3612static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
3613 if (E == nullptr)
3614 return false;
3615 llvm::APSInt Result;
3616 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
3617 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
3618 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003619}
3620
3621/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00003622/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
3623/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003624static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00003625CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
3626 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
3627 DSAStackTy &DSA,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003628 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00003629 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003630 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003631 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003632 // Found 'collapse' clause - calculate collapse number.
3633 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003634 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00003635 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00003636 }
3637 if (OrderedLoopCountExpr) {
3638 // Found 'ordered' clause - calculate collapse number.
3639 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00003640 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
3641 if (Result.getLimitedValue() < NestedLoopCount) {
3642 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
3643 diag::err_omp_wrong_ordered_loop_count)
3644 << OrderedLoopCountExpr->getSourceRange();
3645 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
3646 diag::note_collapse_loop_count)
3647 << CollapseLoopCountExpr->getSourceRange();
3648 }
3649 NestedLoopCount = Result.getLimitedValue();
3650 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003651 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003652 // This is helper routine for loop directives (e.g., 'for', 'simd',
3653 // 'for simd', etc.).
Alexander Musmana5f070a2014-10-01 06:03:56 +00003654 SmallVector<LoopIterationSpace, 4> IterSpaces;
3655 IterSpaces.resize(NestedLoopCount);
3656 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003657 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003658 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003659 NestedLoopCount, CollapseLoopCountExpr,
3660 OrderedLoopCountExpr, VarsWithImplicitDSA,
3661 IterSpaces[Cnt]))
Alexey Bataevabfc0692014-06-25 06:52:00 +00003662 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003663 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003664 // OpenMP [2.8.1, simd construct, Restrictions]
3665 // All loops associated with the construct must be perfectly nested; that
3666 // is, there must be no intervening code nor any OpenMP directive between
3667 // any two loops.
3668 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003669 }
3670
Alexander Musmana5f070a2014-10-01 06:03:56 +00003671 Built.clear(/* size */ NestedLoopCount);
3672
3673 if (SemaRef.CurContext->isDependentContext())
3674 return NestedLoopCount;
3675
3676 // An example of what is generated for the following code:
3677 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00003678 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00003679 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003680 // for (k = 0; k < NK; ++k)
3681 // for (j = J0; j < NJ; j+=2) {
3682 // <loop body>
3683 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003684 //
3685 // We generate the code below.
3686 // Note: the loop body may be outlined in CodeGen.
3687 // Note: some counters may be C++ classes, operator- is used to find number of
3688 // iterations and operator+= to calculate counter value.
3689 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
3690 // or i64 is currently supported).
3691 //
3692 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
3693 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
3694 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
3695 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
3696 // // similar updates for vars in clauses (e.g. 'linear')
3697 // <loop body (using local i and j)>
3698 // }
3699 // i = NI; // assign final values of counters
3700 // j = NJ;
3701 //
3702
3703 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
3704 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00003705 // Precondition tests if there is at least one iteration (all conditions are
3706 // true).
3707 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003708 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003709 ExprResult LastIteration32 = WidenIterationCount(
3710 32 /* Bits */, SemaRef.PerformImplicitConversion(
3711 N0->IgnoreImpCasts(), N0->getType(),
3712 Sema::AA_Converting, /*AllowExplicit=*/true)
3713 .get(),
3714 SemaRef);
3715 ExprResult LastIteration64 = WidenIterationCount(
3716 64 /* Bits */, SemaRef.PerformImplicitConversion(
3717 N0->IgnoreImpCasts(), N0->getType(),
3718 Sema::AA_Converting, /*AllowExplicit=*/true)
3719 .get(),
3720 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003721
3722 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
3723 return NestedLoopCount;
3724
3725 auto &C = SemaRef.Context;
3726 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
3727
3728 Scope *CurScope = DSA.getCurScope();
3729 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003730 if (PreCond.isUsable()) {
3731 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
3732 PreCond.get(), IterSpaces[Cnt].PreCond);
3733 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003734 auto N = IterSpaces[Cnt].NumIterations;
3735 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
3736 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003737 LastIteration32 = SemaRef.BuildBinOp(
3738 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
3739 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
3740 Sema::AA_Converting,
3741 /*AllowExplicit=*/true)
3742 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003743 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003744 LastIteration64 = SemaRef.BuildBinOp(
3745 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
3746 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
3747 Sema::AA_Converting,
3748 /*AllowExplicit=*/true)
3749 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003750 }
3751
3752 // Choose either the 32-bit or 64-bit version.
3753 ExprResult LastIteration = LastIteration64;
3754 if (LastIteration32.isUsable() &&
3755 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
3756 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
3757 FitsInto(
3758 32 /* Bits */,
3759 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
3760 LastIteration64.get(), SemaRef)))
3761 LastIteration = LastIteration32;
3762
3763 if (!LastIteration.isUsable())
3764 return 0;
3765
3766 // Save the number of iterations.
3767 ExprResult NumIterations = LastIteration;
3768 {
3769 LastIteration = SemaRef.BuildBinOp(
3770 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
3771 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3772 if (!LastIteration.isUsable())
3773 return 0;
3774 }
3775
3776 // Calculate the last iteration number beforehand instead of doing this on
3777 // each iteration. Do not do this if the number of iterations may be kfold-ed.
3778 llvm::APSInt Result;
3779 bool IsConstant =
3780 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
3781 ExprResult CalcLastIteration;
3782 if (!IsConstant) {
3783 SourceLocation SaveLoc;
3784 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00003785 buildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00003786 ".omp.last.iteration");
Alexey Bataev39f915b82015-05-08 10:41:21 +00003787 ExprResult SaveRef = buildDeclRefExpr(
3788 SemaRef, SaveVar, LastIteration.get()->getType(), SaveLoc);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003789 CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign,
3790 SaveRef.get(), LastIteration.get());
3791 LastIteration = SaveRef;
3792
3793 // Prepare SaveRef + 1.
3794 NumIterations = SemaRef.BuildBinOp(
3795 CurScope, SaveLoc, BO_Add, SaveRef.get(),
3796 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3797 if (!NumIterations.isUsable())
3798 return 0;
3799 }
3800
3801 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
3802
Alexander Musmanc6388682014-12-15 07:07:06 +00003803 QualType VType = LastIteration.get()->getType();
3804 // Build variables passed into runtime, nesessary for worksharing directives.
3805 ExprResult LB, UB, IL, ST, EUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003806 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
3807 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00003808 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00003809 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
3810 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003811 SemaRef.AddInitializerToDecl(
3812 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
3813 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3814
3815 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00003816 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
3817 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003818 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
3819 /*DirectInit*/ false,
3820 /*TypeMayContainAuto*/ false);
3821
3822 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
3823 // This will be used to implement clause 'lastprivate'.
3824 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00003825 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
3826 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003827 SemaRef.AddInitializerToDecl(
3828 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
3829 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3830
3831 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev39f915b82015-05-08 10:41:21 +00003832 VarDecl *STDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.stride");
3833 ST = buildDeclRefExpr(SemaRef, STDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003834 SemaRef.AddInitializerToDecl(
3835 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
3836 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3837
3838 // Build expression: UB = min(UB, LastIteration)
3839 // It is nesessary for CodeGen of directives with static scheduling.
3840 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
3841 UB.get(), LastIteration.get());
3842 ExprResult CondOp = SemaRef.ActOnConditionalOp(
3843 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
3844 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
3845 CondOp.get());
3846 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
3847 }
3848
3849 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003850 ExprResult IV;
3851 ExprResult Init;
3852 {
Alexey Bataev39f915b82015-05-08 10:41:21 +00003853 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.iv");
3854 IV = buildDeclRefExpr(SemaRef, IVDecl, VType, InitLoc);
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003855 Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003856 isOpenMPTaskLoopDirective(DKind) ||
3857 isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00003858 ? LB.get()
3859 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
3860 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
3861 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003862 }
3863
Alexander Musmanc6388682014-12-15 07:07:06 +00003864 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003865 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00003866 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003867 (isOpenMPWorksharingDirective(DKind) ||
3868 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00003869 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
3870 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
3871 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003872
3873 // Loop increment (IV = IV + 1)
3874 SourceLocation IncLoc;
3875 ExprResult Inc =
3876 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
3877 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
3878 if (!Inc.isUsable())
3879 return 0;
3880 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00003881 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
3882 if (!Inc.isUsable())
3883 return 0;
3884
3885 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
3886 // Used for directives with static scheduling.
3887 ExprResult NextLB, NextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003888 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
3889 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00003890 // LB + ST
3891 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
3892 if (!NextLB.isUsable())
3893 return 0;
3894 // LB = LB + ST
3895 NextLB =
3896 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
3897 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
3898 if (!NextLB.isUsable())
3899 return 0;
3900 // UB + ST
3901 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
3902 if (!NextUB.isUsable())
3903 return 0;
3904 // UB = UB + ST
3905 NextUB =
3906 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
3907 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
3908 if (!NextUB.isUsable())
3909 return 0;
3910 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003911
3912 // Build updates and final values of the loop counters.
3913 bool HasErrors = false;
3914 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003915 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003916 Built.Updates.resize(NestedLoopCount);
3917 Built.Finals.resize(NestedLoopCount);
3918 {
3919 ExprResult Div;
3920 // Go from inner nested loop to outer.
3921 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
3922 LoopIterationSpace &IS = IterSpaces[Cnt];
3923 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
3924 // Build: Iter = (IV / Div) % IS.NumIters
3925 // where Div is product of previous iterations' IS.NumIters.
3926 ExprResult Iter;
3927 if (Div.isUsable()) {
3928 Iter =
3929 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
3930 } else {
3931 Iter = IV;
3932 assert((Cnt == (int)NestedLoopCount - 1) &&
3933 "unusable div expected on first iteration only");
3934 }
3935
3936 if (Cnt != 0 && Iter.isUsable())
3937 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
3938 IS.NumIterations);
3939 if (!Iter.isUsable()) {
3940 HasErrors = true;
3941 break;
3942 }
3943
Alexey Bataev39f915b82015-05-08 10:41:21 +00003944 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
3945 auto *CounterVar = buildDeclRefExpr(
3946 SemaRef, cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()),
3947 IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
3948 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003949 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
3950 IS.CounterInit);
3951 if (!Init.isUsable()) {
3952 HasErrors = true;
3953 break;
3954 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003955 ExprResult Update =
Alexey Bataev39f915b82015-05-08 10:41:21 +00003956 BuildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003957 IS.CounterInit, Iter, IS.CounterStep, IS.Subtract);
3958 if (!Update.isUsable()) {
3959 HasErrors = true;
3960 break;
3961 }
3962
3963 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
3964 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00003965 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003966 IS.NumIterations, IS.CounterStep, IS.Subtract);
3967 if (!Final.isUsable()) {
3968 HasErrors = true;
3969 break;
3970 }
3971
3972 // Build Div for the next iteration: Div <- Div * IS.NumIters
3973 if (Cnt != 0) {
3974 if (Div.isUnset())
3975 Div = IS.NumIterations;
3976 else
3977 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
3978 IS.NumIterations);
3979
3980 // Add parentheses (for debugging purposes only).
3981 if (Div.isUsable())
3982 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
3983 if (!Div.isUsable()) {
3984 HasErrors = true;
3985 break;
3986 }
3987 }
3988 if (!Update.isUsable() || !Final.isUsable()) {
3989 HasErrors = true;
3990 break;
3991 }
3992 // Save results
3993 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00003994 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003995 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003996 Built.Updates[Cnt] = Update.get();
3997 Built.Finals[Cnt] = Final.get();
3998 }
3999 }
4000
4001 if (HasErrors)
4002 return 0;
4003
4004 // Save results
4005 Built.IterationVarRef = IV.get();
4006 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00004007 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004008 Built.CalcLastIteration =
4009 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004010 Built.PreCond = PreCond.get();
4011 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004012 Built.Init = Init.get();
4013 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00004014 Built.LB = LB.get();
4015 Built.UB = UB.get();
4016 Built.IL = IL.get();
4017 Built.ST = ST.get();
4018 Built.EUB = EUB.get();
4019 Built.NLB = NextLB.get();
4020 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004021
Alexey Bataevabfc0692014-06-25 06:52:00 +00004022 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004023}
4024
Alexey Bataev10e775f2015-07-30 11:36:16 +00004025static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004026 auto CollapseClauses =
4027 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
4028 if (CollapseClauses.begin() != CollapseClauses.end())
4029 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004030 return nullptr;
4031}
4032
Alexey Bataev10e775f2015-07-30 11:36:16 +00004033static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004034 auto OrderedClauses =
4035 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
4036 if (OrderedClauses.begin() != OrderedClauses.end())
4037 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004038 return nullptr;
4039}
4040
Alexey Bataev66b15b52015-08-21 11:14:16 +00004041static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen,
4042 const Expr *Safelen) {
4043 llvm::APSInt SimdlenRes, SafelenRes;
4044 if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() ||
4045 Simdlen->isInstantiationDependent() ||
4046 Simdlen->containsUnexpandedParameterPack())
4047 return false;
4048 if (Safelen->isValueDependent() || Safelen->isTypeDependent() ||
4049 Safelen->isInstantiationDependent() ||
4050 Safelen->containsUnexpandedParameterPack())
4051 return false;
4052 Simdlen->EvaluateAsInt(SimdlenRes, S.Context);
4053 Safelen->EvaluateAsInt(SafelenRes, S.Context);
4054 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4055 // If both simdlen and safelen clauses are specified, the value of the simdlen
4056 // parameter must be less than or equal to the value of the safelen parameter.
4057 if (SimdlenRes > SafelenRes) {
4058 S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values)
4059 << Simdlen->getSourceRange() << Safelen->getSourceRange();
4060 return true;
4061 }
4062 return false;
4063}
4064
Alexey Bataev4acb8592014-07-07 13:01:15 +00004065StmtResult Sema::ActOnOpenMPSimdDirective(
4066 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4067 SourceLocation EndLoc,
4068 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004069 if (!AStmt)
4070 return StmtError();
4071
4072 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004073 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004074 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4075 // define the nested loops number.
4076 unsigned NestedLoopCount = CheckOpenMPLoop(
4077 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4078 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004079 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004080 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004081
Alexander Musmana5f070a2014-10-01 06:03:56 +00004082 assert((CurContext->isDependentContext() || B.builtAll()) &&
4083 "omp simd loop exprs were not built");
4084
Alexander Musman3276a272015-03-21 10:12:56 +00004085 if (!CurContext->isDependentContext()) {
4086 // Finalize the clauses that need pre-built expressions for CodeGen.
4087 for (auto C : Clauses) {
4088 if (auto LC = dyn_cast<OMPLinearClause>(C))
4089 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4090 B.NumIterations, *this, CurScope))
4091 return StmtError();
4092 }
4093 }
4094
Alexey Bataev66b15b52015-08-21 11:14:16 +00004095 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4096 // If both simdlen and safelen clauses are specified, the value of the simdlen
4097 // parameter must be less than or equal to the value of the safelen parameter.
4098 OMPSafelenClause *Safelen = nullptr;
4099 OMPSimdlenClause *Simdlen = nullptr;
4100 for (auto *Clause : Clauses) {
4101 if (Clause->getClauseKind() == OMPC_safelen)
4102 Safelen = cast<OMPSafelenClause>(Clause);
4103 else if (Clause->getClauseKind() == OMPC_simdlen)
4104 Simdlen = cast<OMPSimdlenClause>(Clause);
4105 if (Safelen && Simdlen)
4106 break;
4107 }
4108 if (Simdlen && Safelen &&
4109 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4110 Safelen->getSafelen()))
4111 return StmtError();
4112
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004113 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004114 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4115 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004116}
4117
Alexey Bataev4acb8592014-07-07 13:01:15 +00004118StmtResult Sema::ActOnOpenMPForDirective(
4119 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4120 SourceLocation EndLoc,
4121 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004122 if (!AStmt)
4123 return StmtError();
4124
4125 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004126 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004127 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4128 // define the nested loops number.
4129 unsigned NestedLoopCount = CheckOpenMPLoop(
4130 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4131 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004132 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00004133 return StmtError();
4134
Alexander Musmana5f070a2014-10-01 06:03:56 +00004135 assert((CurContext->isDependentContext() || B.builtAll()) &&
4136 "omp for loop exprs were not built");
4137
Alexey Bataev54acd402015-08-04 11:18:19 +00004138 if (!CurContext->isDependentContext()) {
4139 // Finalize the clauses that need pre-built expressions for CodeGen.
4140 for (auto C : Clauses) {
4141 if (auto LC = dyn_cast<OMPLinearClause>(C))
4142 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4143 B.NumIterations, *this, CurScope))
4144 return StmtError();
4145 }
4146 }
4147
Alexey Bataevf29276e2014-06-18 04:14:57 +00004148 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004149 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004150 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00004151}
4152
Alexander Musmanf82886e2014-09-18 05:12:34 +00004153StmtResult Sema::ActOnOpenMPForSimdDirective(
4154 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4155 SourceLocation EndLoc,
4156 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004157 if (!AStmt)
4158 return StmtError();
4159
4160 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004161 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004162 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4163 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00004164 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004165 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
4166 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4167 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004168 if (NestedLoopCount == 0)
4169 return StmtError();
4170
Alexander Musmanc6388682014-12-15 07:07:06 +00004171 assert((CurContext->isDependentContext() || B.builtAll()) &&
4172 "omp for simd loop exprs were not built");
4173
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00004174 if (!CurContext->isDependentContext()) {
4175 // Finalize the clauses that need pre-built expressions for CodeGen.
4176 for (auto C : Clauses) {
4177 if (auto LC = dyn_cast<OMPLinearClause>(C))
4178 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4179 B.NumIterations, *this, CurScope))
4180 return StmtError();
4181 }
4182 }
4183
Alexey Bataev66b15b52015-08-21 11:14:16 +00004184 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4185 // If both simdlen and safelen clauses are specified, the value of the simdlen
4186 // parameter must be less than or equal to the value of the safelen parameter.
4187 OMPSafelenClause *Safelen = nullptr;
4188 OMPSimdlenClause *Simdlen = nullptr;
4189 for (auto *Clause : Clauses) {
4190 if (Clause->getClauseKind() == OMPC_safelen)
4191 Safelen = cast<OMPSafelenClause>(Clause);
4192 else if (Clause->getClauseKind() == OMPC_simdlen)
4193 Simdlen = cast<OMPSimdlenClause>(Clause);
4194 if (Safelen && Simdlen)
4195 break;
4196 }
4197 if (Simdlen && Safelen &&
4198 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4199 Safelen->getSafelen()))
4200 return StmtError();
4201
Alexander Musmanf82886e2014-09-18 05:12:34 +00004202 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004203 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4204 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004205}
4206
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004207StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
4208 Stmt *AStmt,
4209 SourceLocation StartLoc,
4210 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004211 if (!AStmt)
4212 return StmtError();
4213
4214 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004215 auto BaseStmt = AStmt;
4216 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4217 BaseStmt = CS->getCapturedStmt();
4218 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4219 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004220 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004221 return StmtError();
4222 // All associated statements must be '#pragma omp section' except for
4223 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004224 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004225 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4226 if (SectionStmt)
4227 Diag(SectionStmt->getLocStart(),
4228 diag::err_omp_sections_substmt_not_section);
4229 return StmtError();
4230 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004231 cast<OMPSectionDirective>(SectionStmt)
4232 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004233 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004234 } else {
4235 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
4236 return StmtError();
4237 }
4238
4239 getCurFunction()->setHasBranchProtectedScope();
4240
Alexey Bataev25e5b442015-09-15 12:52:43 +00004241 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4242 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004243}
4244
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004245StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
4246 SourceLocation StartLoc,
4247 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004248 if (!AStmt)
4249 return StmtError();
4250
4251 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004252
4253 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00004254 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004255
Alexey Bataev25e5b442015-09-15 12:52:43 +00004256 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
4257 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004258}
4259
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004260StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
4261 Stmt *AStmt,
4262 SourceLocation StartLoc,
4263 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004264 if (!AStmt)
4265 return StmtError();
4266
4267 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00004268
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004269 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00004270
Alexey Bataev3255bf32015-01-19 05:20:46 +00004271 // OpenMP [2.7.3, single Construct, Restrictions]
4272 // The copyprivate clause must not be used with the nowait clause.
4273 OMPClause *Nowait = nullptr;
4274 OMPClause *Copyprivate = nullptr;
4275 for (auto *Clause : Clauses) {
4276 if (Clause->getClauseKind() == OMPC_nowait)
4277 Nowait = Clause;
4278 else if (Clause->getClauseKind() == OMPC_copyprivate)
4279 Copyprivate = Clause;
4280 if (Copyprivate && Nowait) {
4281 Diag(Copyprivate->getLocStart(),
4282 diag::err_omp_single_copyprivate_with_nowait);
4283 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
4284 return StmtError();
4285 }
4286 }
4287
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004288 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4289}
4290
Alexander Musman80c22892014-07-17 08:54:58 +00004291StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
4292 SourceLocation StartLoc,
4293 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004294 if (!AStmt)
4295 return StmtError();
4296
4297 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00004298
4299 getCurFunction()->setHasBranchProtectedScope();
4300
4301 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
4302}
4303
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004304StmtResult
4305Sema::ActOnOpenMPCriticalDirective(const DeclarationNameInfo &DirName,
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");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004312
4313 getCurFunction()->setHasBranchProtectedScope();
4314
4315 return OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
4316 AStmt);
4317}
4318
Alexey Bataev4acb8592014-07-07 13:01:15 +00004319StmtResult Sema::ActOnOpenMPParallelForDirective(
4320 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4321 SourceLocation EndLoc,
4322 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004323 if (!AStmt)
4324 return StmtError();
4325
Alexey Bataev4acb8592014-07-07 13:01:15 +00004326 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4327 // 1.2.2 OpenMP Language Terminology
4328 // Structured block - An executable statement with a single entry at the
4329 // top and a single exit at the bottom.
4330 // The point of exit cannot be a branch out of the structured block.
4331 // longjmp() and throw() must not violate the entry/exit criteria.
4332 CS->getCapturedDecl()->setNothrow();
4333
Alexander Musmanc6388682014-12-15 07:07:06 +00004334 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004335 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4336 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004337 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004338 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
4339 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4340 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00004341 if (NestedLoopCount == 0)
4342 return StmtError();
4343
Alexander Musmana5f070a2014-10-01 06:03:56 +00004344 assert((CurContext->isDependentContext() || B.builtAll()) &&
4345 "omp parallel for loop exprs were not built");
4346
Alexey Bataev54acd402015-08-04 11:18:19 +00004347 if (!CurContext->isDependentContext()) {
4348 // Finalize the clauses that need pre-built expressions for CodeGen.
4349 for (auto C : Clauses) {
4350 if (auto LC = dyn_cast<OMPLinearClause>(C))
4351 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4352 B.NumIterations, *this, CurScope))
4353 return StmtError();
4354 }
4355 }
4356
Alexey Bataev4acb8592014-07-07 13:01:15 +00004357 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004358 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004359 NestedLoopCount, Clauses, AStmt, B,
4360 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00004361}
4362
Alexander Musmane4e893b2014-09-23 09:33:00 +00004363StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
4364 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4365 SourceLocation EndLoc,
4366 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004367 if (!AStmt)
4368 return StmtError();
4369
Alexander Musmane4e893b2014-09-23 09:33:00 +00004370 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4371 // 1.2.2 OpenMP Language Terminology
4372 // Structured block - An executable statement with a single entry at the
4373 // top and a single exit at the bottom.
4374 // The point of exit cannot be a branch out of the structured block.
4375 // longjmp() and throw() must not violate the entry/exit criteria.
4376 CS->getCapturedDecl()->setNothrow();
4377
Alexander Musmanc6388682014-12-15 07:07:06 +00004378 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004379 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4380 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00004381 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004382 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
4383 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4384 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004385 if (NestedLoopCount == 0)
4386 return StmtError();
4387
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00004388 if (!CurContext->isDependentContext()) {
4389 // Finalize the clauses that need pre-built expressions for CodeGen.
4390 for (auto C : Clauses) {
4391 if (auto LC = dyn_cast<OMPLinearClause>(C))
4392 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4393 B.NumIterations, *this, CurScope))
4394 return StmtError();
4395 }
4396 }
4397
Alexey Bataev66b15b52015-08-21 11:14:16 +00004398 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4399 // If both simdlen and safelen clauses are specified, the value of the simdlen
4400 // parameter must be less than or equal to the value of the safelen parameter.
4401 OMPSafelenClause *Safelen = nullptr;
4402 OMPSimdlenClause *Simdlen = nullptr;
4403 for (auto *Clause : Clauses) {
4404 if (Clause->getClauseKind() == OMPC_safelen)
4405 Safelen = cast<OMPSafelenClause>(Clause);
4406 else if (Clause->getClauseKind() == OMPC_simdlen)
4407 Simdlen = cast<OMPSimdlenClause>(Clause);
4408 if (Safelen && Simdlen)
4409 break;
4410 }
4411 if (Simdlen && Safelen &&
4412 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4413 Safelen->getSafelen()))
4414 return StmtError();
4415
Alexander Musmane4e893b2014-09-23 09:33:00 +00004416 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004417 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00004418 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004419}
4420
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004421StmtResult
4422Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
4423 Stmt *AStmt, SourceLocation StartLoc,
4424 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004425 if (!AStmt)
4426 return StmtError();
4427
4428 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004429 auto BaseStmt = AStmt;
4430 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4431 BaseStmt = CS->getCapturedStmt();
4432 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4433 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004434 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004435 return StmtError();
4436 // All associated statements must be '#pragma omp section' except for
4437 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004438 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004439 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4440 if (SectionStmt)
4441 Diag(SectionStmt->getLocStart(),
4442 diag::err_omp_parallel_sections_substmt_not_section);
4443 return StmtError();
4444 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004445 cast<OMPSectionDirective>(SectionStmt)
4446 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004447 }
4448 } else {
4449 Diag(AStmt->getLocStart(),
4450 diag::err_omp_parallel_sections_not_compound_stmt);
4451 return StmtError();
4452 }
4453
4454 getCurFunction()->setHasBranchProtectedScope();
4455
Alexey Bataev25e5b442015-09-15 12:52:43 +00004456 return OMPParallelSectionsDirective::Create(
4457 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004458}
4459
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004460StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
4461 Stmt *AStmt, SourceLocation StartLoc,
4462 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004463 if (!AStmt)
4464 return StmtError();
4465
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004466 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4467 // 1.2.2 OpenMP Language Terminology
4468 // Structured block - An executable statement with a single entry at the
4469 // top and a single exit at the bottom.
4470 // The point of exit cannot be a branch out of the structured block.
4471 // longjmp() and throw() must not violate the entry/exit criteria.
4472 CS->getCapturedDecl()->setNothrow();
4473
4474 getCurFunction()->setHasBranchProtectedScope();
4475
Alexey Bataev25e5b442015-09-15 12:52:43 +00004476 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4477 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004478}
4479
Alexey Bataev68446b72014-07-18 07:47:19 +00004480StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
4481 SourceLocation EndLoc) {
4482 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
4483}
4484
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00004485StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
4486 SourceLocation EndLoc) {
4487 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
4488}
4489
Alexey Bataev2df347a2014-07-18 10:17:07 +00004490StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
4491 SourceLocation EndLoc) {
4492 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
4493}
4494
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00004495StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
4496 SourceLocation StartLoc,
4497 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004498 if (!AStmt)
4499 return StmtError();
4500
4501 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00004502
4503 getCurFunction()->setHasBranchProtectedScope();
4504
4505 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
4506}
4507
Alexey Bataev6125da92014-07-21 11:26:11 +00004508StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
4509 SourceLocation StartLoc,
4510 SourceLocation EndLoc) {
4511 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
4512 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
4513}
4514
Alexey Bataev346265e2015-09-25 10:37:12 +00004515StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
4516 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004517 SourceLocation StartLoc,
4518 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004519 if (!AStmt)
4520 return StmtError();
4521
4522 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004523
4524 getCurFunction()->setHasBranchProtectedScope();
4525
Alexey Bataev346265e2015-09-25 10:37:12 +00004526 OMPThreadsClause *TC = nullptr;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004527 OMPSIMDClause *SC = nullptr;
Alexey Bataev346265e2015-09-25 10:37:12 +00004528 for (auto *C: Clauses) {
4529 if (C->getClauseKind() == OMPC_threads)
4530 TC = cast<OMPThreadsClause>(C);
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004531 else if (C->getClauseKind() == OMPC_simd)
4532 SC = cast<OMPSIMDClause>(C);
Alexey Bataev346265e2015-09-25 10:37:12 +00004533 }
4534
4535 // TODO: this must happen only if 'threads' clause specified or if no clauses
4536 // is specified.
4537 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
4538 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
4539 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param) << (TC != nullptr);
4540 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
4541 return StmtError();
4542 }
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004543 if (!SC && isOpenMPSimdDirective(DSAStack->getParentDirective())) {
4544 // OpenMP [2.8.1,simd Construct, Restrictions]
4545 // An ordered construct with the simd clause is the only OpenMP construct
4546 // that can appear in the simd region.
4547 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
4548 return StmtError();
4549 }
Alexey Bataev346265e2015-09-25 10:37:12 +00004550
4551 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004552}
4553
Alexey Bataev1d160b12015-03-13 12:27:31 +00004554namespace {
4555/// \brief Helper class for checking expression in 'omp atomic [update]'
4556/// construct.
4557class OpenMPAtomicUpdateChecker {
4558 /// \brief Error results for atomic update expressions.
4559 enum ExprAnalysisErrorCode {
4560 /// \brief A statement is not an expression statement.
4561 NotAnExpression,
4562 /// \brief Expression is not builtin binary or unary operation.
4563 NotABinaryOrUnaryExpression,
4564 /// \brief Unary operation is not post-/pre- increment/decrement operation.
4565 NotAnUnaryIncDecExpression,
4566 /// \brief An expression is not of scalar type.
4567 NotAScalarType,
4568 /// \brief A binary operation is not an assignment operation.
4569 NotAnAssignmentOp,
4570 /// \brief RHS part of the binary operation is not a binary expression.
4571 NotABinaryExpression,
4572 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
4573 /// expression.
4574 NotABinaryOperator,
4575 /// \brief RHS binary operation does not have reference to the updated LHS
4576 /// part.
4577 NotAnUpdateExpression,
4578 /// \brief No errors is found.
4579 NoError
4580 };
4581 /// \brief Reference to Sema.
4582 Sema &SemaRef;
4583 /// \brief A location for note diagnostics (when error is found).
4584 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004585 /// \brief 'x' lvalue part of the source atomic expression.
4586 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004587 /// \brief 'expr' rvalue part of the source atomic expression.
4588 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004589 /// \brief Helper expression of the form
4590 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
4591 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
4592 Expr *UpdateExpr;
4593 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
4594 /// important for non-associative operations.
4595 bool IsXLHSInRHSPart;
4596 BinaryOperatorKind Op;
4597 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004598 /// \brief true if the source expression is a postfix unary operation, false
4599 /// if it is a prefix unary operation.
4600 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004601
4602public:
4603 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00004604 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00004605 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00004606 /// \brief Check specified statement that it is suitable for 'atomic update'
4607 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00004608 /// expression. If DiagId and NoteId == 0, then only check is performed
4609 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00004610 /// \param DiagId Diagnostic which should be emitted if error is found.
4611 /// \param NoteId Diagnostic note for the main error message.
4612 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00004613 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00004614 /// \brief Return the 'x' lvalue part of the source atomic expression.
4615 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00004616 /// \brief Return the 'expr' rvalue part of the source atomic expression.
4617 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00004618 /// \brief Return the update expression used in calculation of the updated
4619 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
4620 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
4621 Expr *getUpdateExpr() const { return UpdateExpr; }
4622 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
4623 /// false otherwise.
4624 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
4625
Alexey Bataevb78ca832015-04-01 03:33:17 +00004626 /// \brief true if the source expression is a postfix unary operation, false
4627 /// if it is a prefix unary operation.
4628 bool isPostfixUpdate() const { return IsPostfixUpdate; }
4629
Alexey Bataev1d160b12015-03-13 12:27:31 +00004630private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00004631 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
4632 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00004633};
4634} // namespace
4635
4636bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
4637 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
4638 ExprAnalysisErrorCode ErrorFound = NoError;
4639 SourceLocation ErrorLoc, NoteLoc;
4640 SourceRange ErrorRange, NoteRange;
4641 // Allowed constructs are:
4642 // x = x binop expr;
4643 // x = expr binop x;
4644 if (AtomicBinOp->getOpcode() == BO_Assign) {
4645 X = AtomicBinOp->getLHS();
4646 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
4647 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
4648 if (AtomicInnerBinOp->isMultiplicativeOp() ||
4649 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
4650 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00004651 Op = AtomicInnerBinOp->getOpcode();
4652 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00004653 auto *LHS = AtomicInnerBinOp->getLHS();
4654 auto *RHS = AtomicInnerBinOp->getRHS();
4655 llvm::FoldingSetNodeID XId, LHSId, RHSId;
4656 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
4657 /*Canonical=*/true);
4658 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
4659 /*Canonical=*/true);
4660 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
4661 /*Canonical=*/true);
4662 if (XId == LHSId) {
4663 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004664 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004665 } else if (XId == RHSId) {
4666 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004667 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004668 } else {
4669 ErrorLoc = AtomicInnerBinOp->getExprLoc();
4670 ErrorRange = AtomicInnerBinOp->getSourceRange();
4671 NoteLoc = X->getExprLoc();
4672 NoteRange = X->getSourceRange();
4673 ErrorFound = NotAnUpdateExpression;
4674 }
4675 } else {
4676 ErrorLoc = AtomicInnerBinOp->getExprLoc();
4677 ErrorRange = AtomicInnerBinOp->getSourceRange();
4678 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
4679 NoteRange = SourceRange(NoteLoc, NoteLoc);
4680 ErrorFound = NotABinaryOperator;
4681 }
4682 } else {
4683 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
4684 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
4685 ErrorFound = NotABinaryExpression;
4686 }
4687 } else {
4688 ErrorLoc = AtomicBinOp->getExprLoc();
4689 ErrorRange = AtomicBinOp->getSourceRange();
4690 NoteLoc = AtomicBinOp->getOperatorLoc();
4691 NoteRange = SourceRange(NoteLoc, NoteLoc);
4692 ErrorFound = NotAnAssignmentOp;
4693 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00004694 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004695 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
4696 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
4697 return true;
4698 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00004699 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00004700 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004701}
4702
4703bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
4704 unsigned NoteId) {
4705 ExprAnalysisErrorCode ErrorFound = NoError;
4706 SourceLocation ErrorLoc, NoteLoc;
4707 SourceRange ErrorRange, NoteRange;
4708 // Allowed constructs are:
4709 // x++;
4710 // x--;
4711 // ++x;
4712 // --x;
4713 // x binop= expr;
4714 // x = x binop expr;
4715 // x = expr binop x;
4716 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
4717 AtomicBody = AtomicBody->IgnoreParenImpCasts();
4718 if (AtomicBody->getType()->isScalarType() ||
4719 AtomicBody->isInstantiationDependent()) {
4720 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
4721 AtomicBody->IgnoreParenImpCasts())) {
4722 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00004723 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00004724 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00004725 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00004726 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00004727 X = AtomicCompAssignOp->getLHS();
4728 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004729 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
4730 AtomicBody->IgnoreParenImpCasts())) {
4731 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00004732 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
4733 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004734 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00004735 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
4736 // Check for Unary Operation
4737 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004738 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00004739 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
4740 OpLoc = AtomicUnaryOp->getOperatorLoc();
4741 X = AtomicUnaryOp->getSubExpr();
4742 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
4743 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004744 } else {
4745 ErrorFound = NotAnUnaryIncDecExpression;
4746 ErrorLoc = AtomicUnaryOp->getExprLoc();
4747 ErrorRange = AtomicUnaryOp->getSourceRange();
4748 NoteLoc = AtomicUnaryOp->getOperatorLoc();
4749 NoteRange = SourceRange(NoteLoc, NoteLoc);
4750 }
Alexey Bataev5a195472015-09-04 12:55:50 +00004751 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004752 ErrorFound = NotABinaryOrUnaryExpression;
4753 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
4754 NoteRange = ErrorRange = AtomicBody->getSourceRange();
4755 }
4756 } else {
4757 ErrorFound = NotAScalarType;
4758 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
4759 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
4760 }
4761 } else {
4762 ErrorFound = NotAnExpression;
4763 NoteLoc = ErrorLoc = S->getLocStart();
4764 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
4765 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00004766 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004767 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
4768 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
4769 return true;
4770 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00004771 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00004772 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00004773 // Build an update expression of form 'OpaqueValueExpr(x) binop
4774 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
4775 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
4776 auto *OVEX = new (SemaRef.getASTContext())
4777 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
4778 auto *OVEExpr = new (SemaRef.getASTContext())
4779 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
4780 auto Update =
4781 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
4782 IsXLHSInRHSPart ? OVEExpr : OVEX);
4783 if (Update.isInvalid())
4784 return true;
4785 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
4786 Sema::AA_Casting);
4787 if (Update.isInvalid())
4788 return true;
4789 UpdateExpr = Update.get();
4790 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00004791 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004792}
4793
Alexey Bataev0162e452014-07-22 10:10:35 +00004794StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
4795 Stmt *AStmt,
4796 SourceLocation StartLoc,
4797 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004798 if (!AStmt)
4799 return StmtError();
4800
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004801 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00004802 // 1.2.2 OpenMP Language Terminology
4803 // Structured block - An executable statement with a single entry at the
4804 // top and a single exit at the bottom.
4805 // The point of exit cannot be a branch out of the structured block.
4806 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00004807 OpenMPClauseKind AtomicKind = OMPC_unknown;
4808 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004809 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00004810 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00004811 C->getClauseKind() == OMPC_update ||
4812 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00004813 if (AtomicKind != OMPC_unknown) {
4814 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
4815 << SourceRange(C->getLocStart(), C->getLocEnd());
4816 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
4817 << getOpenMPClauseName(AtomicKind);
4818 } else {
4819 AtomicKind = C->getClauseKind();
4820 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004821 }
4822 }
4823 }
Alexey Bataev62cec442014-11-18 10:14:22 +00004824
Alexey Bataev459dec02014-07-24 06:46:57 +00004825 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00004826 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
4827 Body = EWC->getSubExpr();
4828
Alexey Bataev62cec442014-11-18 10:14:22 +00004829 Expr *X = nullptr;
4830 Expr *V = nullptr;
4831 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004832 Expr *UE = nullptr;
4833 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004834 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00004835 // OpenMP [2.12.6, atomic Construct]
4836 // In the next expressions:
4837 // * x and v (as applicable) are both l-value expressions with scalar type.
4838 // * During the execution of an atomic region, multiple syntactic
4839 // occurrences of x must designate the same storage location.
4840 // * Neither of v and expr (as applicable) may access the storage location
4841 // designated by x.
4842 // * Neither of x and expr (as applicable) may access the storage location
4843 // designated by v.
4844 // * expr is an expression with scalar type.
4845 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
4846 // * binop, binop=, ++, and -- are not overloaded operators.
4847 // * The expression x binop expr must be numerically equivalent to x binop
4848 // (expr). This requirement is satisfied if the operators in expr have
4849 // precedence greater than binop, or by using parentheses around expr or
4850 // subexpressions of expr.
4851 // * The expression expr binop x must be numerically equivalent to (expr)
4852 // binop x. This requirement is satisfied if the operators in expr have
4853 // precedence equal to or greater than binop, or by using parentheses around
4854 // expr or subexpressions of expr.
4855 // * For forms that allow multiple occurrences of x, the number of times
4856 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00004857 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004858 enum {
4859 NotAnExpression,
4860 NotAnAssignmentOp,
4861 NotAScalarType,
4862 NotAnLValue,
4863 NoError
4864 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00004865 SourceLocation ErrorLoc, NoteLoc;
4866 SourceRange ErrorRange, NoteRange;
4867 // If clause is read:
4868 // v = x;
4869 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
4870 auto AtomicBinOp =
4871 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
4872 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
4873 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
4874 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
4875 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
4876 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
4877 if (!X->isLValue() || !V->isLValue()) {
4878 auto NotLValueExpr = X->isLValue() ? V : X;
4879 ErrorFound = NotAnLValue;
4880 ErrorLoc = AtomicBinOp->getExprLoc();
4881 ErrorRange = AtomicBinOp->getSourceRange();
4882 NoteLoc = NotLValueExpr->getExprLoc();
4883 NoteRange = NotLValueExpr->getSourceRange();
4884 }
4885 } else if (!X->isInstantiationDependent() ||
4886 !V->isInstantiationDependent()) {
4887 auto NotScalarExpr =
4888 (X->isInstantiationDependent() || X->getType()->isScalarType())
4889 ? V
4890 : X;
4891 ErrorFound = NotAScalarType;
4892 ErrorLoc = AtomicBinOp->getExprLoc();
4893 ErrorRange = AtomicBinOp->getSourceRange();
4894 NoteLoc = NotScalarExpr->getExprLoc();
4895 NoteRange = NotScalarExpr->getSourceRange();
4896 }
Alexey Bataev5a195472015-09-04 12:55:50 +00004897 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00004898 ErrorFound = NotAnAssignmentOp;
4899 ErrorLoc = AtomicBody->getExprLoc();
4900 ErrorRange = AtomicBody->getSourceRange();
4901 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
4902 : AtomicBody->getExprLoc();
4903 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
4904 : AtomicBody->getSourceRange();
4905 }
4906 } else {
4907 ErrorFound = NotAnExpression;
4908 NoteLoc = ErrorLoc = Body->getLocStart();
4909 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00004910 }
Alexey Bataev62cec442014-11-18 10:14:22 +00004911 if (ErrorFound != NoError) {
4912 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
4913 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00004914 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
4915 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00004916 return StmtError();
4917 } else if (CurContext->isDependentContext())
4918 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00004919 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004920 enum {
4921 NotAnExpression,
4922 NotAnAssignmentOp,
4923 NotAScalarType,
4924 NotAnLValue,
4925 NoError
4926 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00004927 SourceLocation ErrorLoc, NoteLoc;
4928 SourceRange ErrorRange, NoteRange;
4929 // If clause is write:
4930 // x = expr;
4931 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
4932 auto AtomicBinOp =
4933 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
4934 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00004935 X = AtomicBinOp->getLHS();
4936 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00004937 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
4938 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
4939 if (!X->isLValue()) {
4940 ErrorFound = NotAnLValue;
4941 ErrorLoc = AtomicBinOp->getExprLoc();
4942 ErrorRange = AtomicBinOp->getSourceRange();
4943 NoteLoc = X->getExprLoc();
4944 NoteRange = X->getSourceRange();
4945 }
4946 } else if (!X->isInstantiationDependent() ||
4947 !E->isInstantiationDependent()) {
4948 auto NotScalarExpr =
4949 (X->isInstantiationDependent() || X->getType()->isScalarType())
4950 ? E
4951 : X;
4952 ErrorFound = NotAScalarType;
4953 ErrorLoc = AtomicBinOp->getExprLoc();
4954 ErrorRange = AtomicBinOp->getSourceRange();
4955 NoteLoc = NotScalarExpr->getExprLoc();
4956 NoteRange = NotScalarExpr->getSourceRange();
4957 }
Alexey Bataev5a195472015-09-04 12:55:50 +00004958 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00004959 ErrorFound = NotAnAssignmentOp;
4960 ErrorLoc = AtomicBody->getExprLoc();
4961 ErrorRange = AtomicBody->getSourceRange();
4962 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
4963 : AtomicBody->getExprLoc();
4964 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
4965 : AtomicBody->getSourceRange();
4966 }
4967 } else {
4968 ErrorFound = NotAnExpression;
4969 NoteLoc = ErrorLoc = Body->getLocStart();
4970 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00004971 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00004972 if (ErrorFound != NoError) {
4973 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
4974 << ErrorRange;
4975 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
4976 << NoteRange;
4977 return StmtError();
4978 } else if (CurContext->isDependentContext())
4979 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00004980 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004981 // If clause is update:
4982 // x++;
4983 // x--;
4984 // ++x;
4985 // --x;
4986 // x binop= expr;
4987 // x = x binop expr;
4988 // x = expr binop x;
4989 OpenMPAtomicUpdateChecker Checker(*this);
4990 if (Checker.checkStatement(
4991 Body, (AtomicKind == OMPC_update)
4992 ? diag::err_omp_atomic_update_not_expression_statement
4993 : diag::err_omp_atomic_not_expression_statement,
4994 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00004995 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00004996 if (!CurContext->isDependentContext()) {
4997 E = Checker.getExpr();
4998 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00004999 UE = Checker.getUpdateExpr();
5000 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00005001 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005002 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005003 enum {
5004 NotAnAssignmentOp,
5005 NotACompoundStatement,
5006 NotTwoSubstatements,
5007 NotASpecificExpression,
5008 NoError
5009 } ErrorFound = NoError;
5010 SourceLocation ErrorLoc, NoteLoc;
5011 SourceRange ErrorRange, NoteRange;
5012 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
5013 // If clause is a capture:
5014 // v = x++;
5015 // v = x--;
5016 // v = ++x;
5017 // v = --x;
5018 // v = x binop= expr;
5019 // v = x = x binop expr;
5020 // v = x = expr binop x;
5021 auto *AtomicBinOp =
5022 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5023 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5024 V = AtomicBinOp->getLHS();
5025 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5026 OpenMPAtomicUpdateChecker Checker(*this);
5027 if (Checker.checkStatement(
5028 Body, diag::err_omp_atomic_capture_not_expression_statement,
5029 diag::note_omp_atomic_update))
5030 return StmtError();
5031 E = Checker.getExpr();
5032 X = Checker.getX();
5033 UE = Checker.getUpdateExpr();
5034 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
5035 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00005036 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005037 ErrorLoc = AtomicBody->getExprLoc();
5038 ErrorRange = AtomicBody->getSourceRange();
5039 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5040 : AtomicBody->getExprLoc();
5041 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5042 : AtomicBody->getSourceRange();
5043 ErrorFound = NotAnAssignmentOp;
5044 }
5045 if (ErrorFound != NoError) {
5046 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
5047 << ErrorRange;
5048 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5049 return StmtError();
5050 } else if (CurContext->isDependentContext()) {
5051 UE = V = E = X = nullptr;
5052 }
5053 } else {
5054 // If clause is a capture:
5055 // { v = x; x = expr; }
5056 // { v = x; x++; }
5057 // { v = x; x--; }
5058 // { v = x; ++x; }
5059 // { v = x; --x; }
5060 // { v = x; x binop= expr; }
5061 // { v = x; x = x binop expr; }
5062 // { v = x; x = expr binop x; }
5063 // { x++; v = x; }
5064 // { x--; v = x; }
5065 // { ++x; v = x; }
5066 // { --x; v = x; }
5067 // { x binop= expr; v = x; }
5068 // { x = x binop expr; v = x; }
5069 // { x = expr binop x; v = x; }
5070 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
5071 // Check that this is { expr1; expr2; }
5072 if (CS->size() == 2) {
5073 auto *First = CS->body_front();
5074 auto *Second = CS->body_back();
5075 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
5076 First = EWC->getSubExpr()->IgnoreParenImpCasts();
5077 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
5078 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
5079 // Need to find what subexpression is 'v' and what is 'x'.
5080 OpenMPAtomicUpdateChecker Checker(*this);
5081 bool IsUpdateExprFound = !Checker.checkStatement(Second);
5082 BinaryOperator *BinOp = nullptr;
5083 if (IsUpdateExprFound) {
5084 BinOp = dyn_cast<BinaryOperator>(First);
5085 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5086 }
5087 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5088 // { v = x; x++; }
5089 // { v = x; x--; }
5090 // { v = x; ++x; }
5091 // { v = x; --x; }
5092 // { v = x; x binop= expr; }
5093 // { v = x; x = x binop expr; }
5094 // { v = x; x = expr binop x; }
5095 // Check that the first expression has form v = x.
5096 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5097 llvm::FoldingSetNodeID XId, PossibleXId;
5098 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5099 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5100 IsUpdateExprFound = XId == PossibleXId;
5101 if (IsUpdateExprFound) {
5102 V = BinOp->getLHS();
5103 X = Checker.getX();
5104 E = Checker.getExpr();
5105 UE = Checker.getUpdateExpr();
5106 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005107 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005108 }
5109 }
5110 if (!IsUpdateExprFound) {
5111 IsUpdateExprFound = !Checker.checkStatement(First);
5112 BinOp = nullptr;
5113 if (IsUpdateExprFound) {
5114 BinOp = dyn_cast<BinaryOperator>(Second);
5115 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5116 }
5117 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5118 // { x++; v = x; }
5119 // { x--; v = x; }
5120 // { ++x; v = x; }
5121 // { --x; v = x; }
5122 // { x binop= expr; v = x; }
5123 // { x = x binop expr; v = x; }
5124 // { x = expr binop x; v = x; }
5125 // Check that the second expression has form v = x.
5126 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5127 llvm::FoldingSetNodeID XId, PossibleXId;
5128 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5129 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5130 IsUpdateExprFound = XId == PossibleXId;
5131 if (IsUpdateExprFound) {
5132 V = BinOp->getLHS();
5133 X = Checker.getX();
5134 E = Checker.getExpr();
5135 UE = Checker.getUpdateExpr();
5136 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005137 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005138 }
5139 }
5140 }
5141 if (!IsUpdateExprFound) {
5142 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00005143 auto *FirstExpr = dyn_cast<Expr>(First);
5144 auto *SecondExpr = dyn_cast<Expr>(Second);
5145 if (!FirstExpr || !SecondExpr ||
5146 !(FirstExpr->isInstantiationDependent() ||
5147 SecondExpr->isInstantiationDependent())) {
5148 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
5149 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005150 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00005151 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
5152 : First->getLocStart();
5153 NoteRange = ErrorRange = FirstBinOp
5154 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00005155 : SourceRange(ErrorLoc, ErrorLoc);
5156 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005157 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
5158 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
5159 ErrorFound = NotAnAssignmentOp;
5160 NoteLoc = ErrorLoc = SecondBinOp
5161 ? SecondBinOp->getOperatorLoc()
5162 : Second->getLocStart();
5163 NoteRange = ErrorRange =
5164 SecondBinOp ? SecondBinOp->getSourceRange()
5165 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00005166 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005167 auto *PossibleXRHSInFirst =
5168 FirstBinOp->getRHS()->IgnoreParenImpCasts();
5169 auto *PossibleXLHSInSecond =
5170 SecondBinOp->getLHS()->IgnoreParenImpCasts();
5171 llvm::FoldingSetNodeID X1Id, X2Id;
5172 PossibleXRHSInFirst->Profile(X1Id, Context,
5173 /*Canonical=*/true);
5174 PossibleXLHSInSecond->Profile(X2Id, Context,
5175 /*Canonical=*/true);
5176 IsUpdateExprFound = X1Id == X2Id;
5177 if (IsUpdateExprFound) {
5178 V = FirstBinOp->getLHS();
5179 X = SecondBinOp->getLHS();
5180 E = SecondBinOp->getRHS();
5181 UE = nullptr;
5182 IsXLHSInRHSPart = false;
5183 IsPostfixUpdate = true;
5184 } else {
5185 ErrorFound = NotASpecificExpression;
5186 ErrorLoc = FirstBinOp->getExprLoc();
5187 ErrorRange = FirstBinOp->getSourceRange();
5188 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
5189 NoteRange = SecondBinOp->getRHS()->getSourceRange();
5190 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005191 }
5192 }
5193 }
5194 }
5195 } else {
5196 NoteLoc = ErrorLoc = Body->getLocStart();
5197 NoteRange = ErrorRange =
5198 SourceRange(Body->getLocStart(), Body->getLocStart());
5199 ErrorFound = NotTwoSubstatements;
5200 }
5201 } else {
5202 NoteLoc = ErrorLoc = Body->getLocStart();
5203 NoteRange = ErrorRange =
5204 SourceRange(Body->getLocStart(), Body->getLocStart());
5205 ErrorFound = NotACompoundStatement;
5206 }
5207 if (ErrorFound != NoError) {
5208 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
5209 << ErrorRange;
5210 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5211 return StmtError();
5212 } else if (CurContext->isDependentContext()) {
5213 UE = V = E = X = nullptr;
5214 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005215 }
Alexey Bataevdea47612014-07-23 07:46:59 +00005216 }
Alexey Bataev0162e452014-07-22 10:10:35 +00005217
5218 getCurFunction()->setHasBranchProtectedScope();
5219
Alexey Bataev62cec442014-11-18 10:14:22 +00005220 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00005221 X, V, E, UE, IsXLHSInRHSPart,
5222 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00005223}
5224
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005225StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
5226 Stmt *AStmt,
5227 SourceLocation StartLoc,
5228 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005229 if (!AStmt)
5230 return StmtError();
5231
Samuel Antao4af1b7b2015-12-02 17:44:43 +00005232 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5233 // 1.2.2 OpenMP Language Terminology
5234 // Structured block - An executable statement with a single entry at the
5235 // top and a single exit at the bottom.
5236 // The point of exit cannot be a branch out of the structured block.
5237 // longjmp() and throw() must not violate the entry/exit criteria.
5238 CS->getCapturedDecl()->setNothrow();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005239
Alexey Bataev13314bf2014-10-09 04:18:56 +00005240 // OpenMP [2.16, Nesting of Regions]
5241 // If specified, a teams construct must be contained within a target
5242 // construct. That target construct must contain no statements or directives
5243 // outside of the teams construct.
5244 if (DSAStack->hasInnerTeamsRegion()) {
5245 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
5246 bool OMPTeamsFound = true;
5247 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
5248 auto I = CS->body_begin();
5249 while (I != CS->body_end()) {
5250 auto OED = dyn_cast<OMPExecutableDirective>(*I);
5251 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
5252 OMPTeamsFound = false;
5253 break;
5254 }
5255 ++I;
5256 }
5257 assert(I != CS->body_end() && "Not found statement");
5258 S = *I;
5259 }
5260 if (!OMPTeamsFound) {
5261 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
5262 Diag(DSAStack->getInnerTeamsRegionLoc(),
5263 diag::note_omp_nested_teams_construct_here);
5264 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
5265 << isa<OMPExecutableDirective>(S);
5266 return StmtError();
5267 }
5268 }
5269
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005270 getCurFunction()->setHasBranchProtectedScope();
5271
5272 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5273}
5274
Michael Wong65f367f2015-07-21 13:44:28 +00005275StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
5276 Stmt *AStmt,
5277 SourceLocation StartLoc,
5278 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005279 if (!AStmt)
5280 return StmtError();
5281
5282 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5283
Michael Wong65f367f2015-07-21 13:44:28 +00005284 getCurFunction()->setHasBranchProtectedScope();
5285
5286 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
5287 AStmt);
5288}
5289
Alexey Bataev13314bf2014-10-09 04:18:56 +00005290StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
5291 Stmt *AStmt, SourceLocation StartLoc,
5292 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005293 if (!AStmt)
5294 return StmtError();
5295
Alexey Bataev13314bf2014-10-09 04:18:56 +00005296 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5297 // 1.2.2 OpenMP Language Terminology
5298 // Structured block - An executable statement with a single entry at the
5299 // top and a single exit at the bottom.
5300 // The point of exit cannot be a branch out of the structured block.
5301 // longjmp() and throw() must not violate the entry/exit criteria.
5302 CS->getCapturedDecl()->setNothrow();
5303
5304 getCurFunction()->setHasBranchProtectedScope();
5305
5306 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5307}
5308
Alexey Bataev6d4ed052015-07-01 06:57:41 +00005309StmtResult
5310Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
5311 SourceLocation EndLoc,
5312 OpenMPDirectiveKind CancelRegion) {
5313 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
5314 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
5315 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
5316 << getOpenMPDirectiveName(CancelRegion);
5317 return StmtError();
5318 }
5319 if (DSAStack->isParentNowaitRegion()) {
5320 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
5321 return StmtError();
5322 }
5323 if (DSAStack->isParentOrderedRegion()) {
5324 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
5325 return StmtError();
5326 }
5327 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
5328 CancelRegion);
5329}
5330
Alexey Bataev87933c72015-09-18 08:07:34 +00005331StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
5332 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00005333 SourceLocation EndLoc,
5334 OpenMPDirectiveKind CancelRegion) {
5335 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
5336 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
5337 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
5338 << getOpenMPDirectiveName(CancelRegion);
5339 return StmtError();
5340 }
5341 if (DSAStack->isParentNowaitRegion()) {
5342 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
5343 return StmtError();
5344 }
5345 if (DSAStack->isParentOrderedRegion()) {
5346 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
5347 return StmtError();
5348 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005349 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00005350 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
5351 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00005352}
5353
Alexey Bataev382967a2015-12-08 12:06:20 +00005354static bool checkGrainsizeNumTasksClauses(Sema &S,
5355 ArrayRef<OMPClause *> Clauses) {
5356 OMPClause *PrevClause = nullptr;
5357 bool ErrorFound = false;
5358 for (auto *C : Clauses) {
5359 if (C->getClauseKind() == OMPC_grainsize ||
5360 C->getClauseKind() == OMPC_num_tasks) {
5361 if (!PrevClause)
5362 PrevClause = C;
5363 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
5364 S.Diag(C->getLocStart(),
5365 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
5366 << getOpenMPClauseName(C->getClauseKind())
5367 << getOpenMPClauseName(PrevClause->getClauseKind());
5368 S.Diag(PrevClause->getLocStart(),
5369 diag::note_omp_previous_grainsize_num_tasks)
5370 << getOpenMPClauseName(PrevClause->getClauseKind());
5371 ErrorFound = true;
5372 }
5373 }
5374 }
5375 return ErrorFound;
5376}
5377
Alexey Bataev49f6e782015-12-01 04:18:41 +00005378StmtResult Sema::ActOnOpenMPTaskLoopDirective(
5379 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5380 SourceLocation EndLoc,
5381 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
5382 if (!AStmt)
5383 return StmtError();
5384
5385 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5386 OMPLoopDirective::HelperExprs B;
5387 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5388 // define the nested loops number.
5389 unsigned NestedLoopCount =
5390 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005391 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00005392 VarsWithImplicitDSA, B);
5393 if (NestedLoopCount == 0)
5394 return StmtError();
5395
5396 assert((CurContext->isDependentContext() || B.builtAll()) &&
5397 "omp for loop exprs were not built");
5398
Alexey Bataev382967a2015-12-08 12:06:20 +00005399 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
5400 // The grainsize clause and num_tasks clause are mutually exclusive and may
5401 // not appear on the same taskloop directive.
5402 if (checkGrainsizeNumTasksClauses(*this, Clauses))
5403 return StmtError();
5404
Alexey Bataev49f6e782015-12-01 04:18:41 +00005405 getCurFunction()->setHasBranchProtectedScope();
5406 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
5407 NestedLoopCount, Clauses, AStmt, B);
5408}
5409
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005410StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
5411 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5412 SourceLocation EndLoc,
5413 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
5414 if (!AStmt)
5415 return StmtError();
5416
5417 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5418 OMPLoopDirective::HelperExprs B;
5419 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5420 // define the nested loops number.
5421 unsigned NestedLoopCount =
5422 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
5423 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
5424 VarsWithImplicitDSA, B);
5425 if (NestedLoopCount == 0)
5426 return StmtError();
5427
5428 assert((CurContext->isDependentContext() || B.builtAll()) &&
5429 "omp for loop exprs were not built");
5430
Alexey Bataev382967a2015-12-08 12:06:20 +00005431 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
5432 // The grainsize clause and num_tasks clause are mutually exclusive and may
5433 // not appear on the same taskloop directive.
5434 if (checkGrainsizeNumTasksClauses(*this, Clauses))
5435 return StmtError();
5436
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005437 getCurFunction()->setHasBranchProtectedScope();
5438 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
5439 NestedLoopCount, Clauses, AStmt, B);
5440}
5441
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005442StmtResult Sema::ActOnOpenMPDistributeDirective(
5443 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5444 SourceLocation EndLoc,
5445 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
5446 if (!AStmt)
5447 return StmtError();
5448
5449 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5450 OMPLoopDirective::HelperExprs B;
5451 // In presence of clause 'collapse' with number of loops, it will
5452 // define the nested loops number.
5453 unsigned NestedLoopCount =
5454 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
5455 nullptr /*ordered not a clause on distribute*/, AStmt,
5456 *this, *DSAStack, VarsWithImplicitDSA, B);
5457 if (NestedLoopCount == 0)
5458 return StmtError();
5459
5460 assert((CurContext->isDependentContext() || B.builtAll()) &&
5461 "omp for loop exprs were not built");
5462
5463 getCurFunction()->setHasBranchProtectedScope();
5464 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
5465 NestedLoopCount, Clauses, AStmt, B);
5466}
5467
Alexey Bataeved09d242014-05-28 05:53:51 +00005468OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005469 SourceLocation StartLoc,
5470 SourceLocation LParenLoc,
5471 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005472 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005473 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00005474 case OMPC_final:
5475 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
5476 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00005477 case OMPC_num_threads:
5478 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
5479 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00005480 case OMPC_safelen:
5481 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
5482 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00005483 case OMPC_simdlen:
5484 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
5485 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00005486 case OMPC_collapse:
5487 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
5488 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005489 case OMPC_ordered:
5490 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
5491 break;
Michael Wonge710d542015-08-07 16:16:36 +00005492 case OMPC_device:
5493 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
5494 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00005495 case OMPC_num_teams:
5496 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
5497 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005498 case OMPC_thread_limit:
5499 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
5500 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00005501 case OMPC_priority:
5502 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
5503 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00005504 case OMPC_grainsize:
5505 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
5506 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00005507 case OMPC_num_tasks:
5508 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
5509 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005510 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005511 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005512 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005513 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005514 case OMPC_private:
5515 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00005516 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005517 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00005518 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00005519 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00005520 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005521 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005522 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00005523 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005524 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005525 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005526 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00005527 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005528 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00005529 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00005530 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00005531 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005532 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005533 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00005534 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005535 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00005536 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00005537 case OMPC_nogroup:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005538 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005539 llvm_unreachable("Clause is not allowed.");
5540 }
5541 return Res;
5542}
5543
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005544OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
5545 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005546 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005547 SourceLocation NameModifierLoc,
5548 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005549 SourceLocation EndLoc) {
5550 Expr *ValExpr = Condition;
5551 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
5552 !Condition->isInstantiationDependent() &&
5553 !Condition->containsUnexpandedParameterPack()) {
5554 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00005555 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005556 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005557 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005558
Nikola Smiljanic01a75982014-05-29 10:55:11 +00005559 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005560 }
5561
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005562 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
5563 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005564}
5565
Alexey Bataev3778b602014-07-17 07:32:53 +00005566OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
5567 SourceLocation StartLoc,
5568 SourceLocation LParenLoc,
5569 SourceLocation EndLoc) {
5570 Expr *ValExpr = Condition;
5571 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
5572 !Condition->isInstantiationDependent() &&
5573 !Condition->containsUnexpandedParameterPack()) {
5574 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
5575 Condition->getExprLoc(), Condition);
5576 if (Val.isInvalid())
5577 return nullptr;
5578
5579 ValExpr = Val.get();
5580 }
5581
5582 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
5583}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005584ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
5585 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00005586 if (!Op)
5587 return ExprError();
5588
5589 class IntConvertDiagnoser : public ICEConvertDiagnoser {
5590 public:
5591 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00005592 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00005593 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
5594 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005595 return S.Diag(Loc, diag::err_omp_not_integral) << T;
5596 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005597 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
5598 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005599 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
5600 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005601 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
5602 QualType T,
5603 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005604 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
5605 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005606 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
5607 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005608 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00005609 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00005610 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005611 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
5612 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005613 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
5614 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005615 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
5616 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005617 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00005618 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00005619 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005620 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
5621 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005622 llvm_unreachable("conversion functions are permitted");
5623 }
5624 } ConvertDiagnoser;
5625 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
5626}
5627
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005628static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00005629 OpenMPClauseKind CKind,
5630 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005631 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
5632 !ValExpr->isInstantiationDependent()) {
5633 SourceLocation Loc = ValExpr->getExprLoc();
5634 ExprResult Value =
5635 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
5636 if (Value.isInvalid())
5637 return false;
5638
5639 ValExpr = Value.get();
5640 // The expression must evaluate to a non-negative integer value.
5641 llvm::APSInt Result;
5642 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00005643 Result.isSigned() &&
5644 !((!StrictlyPositive && Result.isNonNegative()) ||
5645 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005646 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00005647 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
5648 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005649 return false;
5650 }
5651 }
5652 return true;
5653}
5654
Alexey Bataev568a8332014-03-06 06:15:19 +00005655OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
5656 SourceLocation StartLoc,
5657 SourceLocation LParenLoc,
5658 SourceLocation EndLoc) {
5659 Expr *ValExpr = NumThreads;
Alexey Bataev568a8332014-03-06 06:15:19 +00005660
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005661 // OpenMP [2.5, Restrictions]
5662 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00005663 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
5664 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005665 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00005666
Alexey Bataeved09d242014-05-28 05:53:51 +00005667 return new (Context)
5668 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00005669}
5670
Alexey Bataev62c87d22014-03-21 04:51:18 +00005671ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
5672 OpenMPClauseKind CKind) {
5673 if (!E)
5674 return ExprError();
5675 if (E->isValueDependent() || E->isTypeDependent() ||
5676 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00005677 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00005678 llvm::APSInt Result;
5679 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
5680 if (ICE.isInvalid())
5681 return ExprError();
5682 if (!Result.isStrictlyPositive()) {
5683 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00005684 << getOpenMPClauseName(CKind) << 1 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00005685 return ExprError();
5686 }
Alexander Musman09184fe2014-09-30 05:29:28 +00005687 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
5688 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
5689 << E->getSourceRange();
5690 return ExprError();
5691 }
Alexey Bataev7b6bc882015-11-26 07:50:39 +00005692 if (CKind == OMPC_collapse)
5693 DSAStack->setCollapseNumber(Result.getExtValue());
5694 else if (CKind == OMPC_ordered)
5695 DSAStack->setCollapseNumber(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00005696 return ICE;
5697}
5698
5699OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
5700 SourceLocation LParenLoc,
5701 SourceLocation EndLoc) {
5702 // OpenMP [2.8.1, simd construct, Description]
5703 // The parameter of the safelen clause must be a constant
5704 // positive integer expression.
5705 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
5706 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005707 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00005708 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00005709 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00005710}
5711
Alexey Bataev66b15b52015-08-21 11:14:16 +00005712OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
5713 SourceLocation LParenLoc,
5714 SourceLocation EndLoc) {
5715 // OpenMP [2.8.1, simd construct, Description]
5716 // The parameter of the simdlen clause must be a constant
5717 // positive integer expression.
5718 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
5719 if (Simdlen.isInvalid())
5720 return nullptr;
5721 return new (Context)
5722 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
5723}
5724
Alexander Musman64d33f12014-06-04 07:53:32 +00005725OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
5726 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00005727 SourceLocation LParenLoc,
5728 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00005729 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00005730 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00005731 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00005732 // The parameter of the collapse clause must be a constant
5733 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00005734 ExprResult NumForLoopsResult =
5735 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
5736 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00005737 return nullptr;
5738 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00005739 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00005740}
5741
Alexey Bataev10e775f2015-07-30 11:36:16 +00005742OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
5743 SourceLocation EndLoc,
5744 SourceLocation LParenLoc,
5745 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00005746 // OpenMP [2.7.1, loop construct, Description]
5747 // OpenMP [2.8.1, simd construct, Description]
5748 // OpenMP [2.9.6, distribute construct, Description]
5749 // The parameter of the ordered clause must be a constant
5750 // positive integer expression if any.
5751 if (NumForLoops && LParenLoc.isValid()) {
5752 ExprResult NumForLoopsResult =
5753 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
5754 if (NumForLoopsResult.isInvalid())
5755 return nullptr;
5756 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00005757 } else
5758 NumForLoops = nullptr;
5759 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00005760 return new (Context)
5761 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
5762}
5763
Alexey Bataeved09d242014-05-28 05:53:51 +00005764OMPClause *Sema::ActOnOpenMPSimpleClause(
5765 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
5766 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005767 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005768 switch (Kind) {
5769 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00005770 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00005771 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
5772 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005773 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005774 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00005775 Res = ActOnOpenMPProcBindClause(
5776 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
5777 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005778 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005779 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00005780 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00005781 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00005782 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00005783 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00005784 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005785 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005786 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005787 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00005788 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00005789 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00005790 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00005791 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00005792 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005793 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005794 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005795 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00005796 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005797 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005798 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005799 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00005800 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005801 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00005802 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00005803 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00005804 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005805 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005806 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00005807 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00005808 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005809 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00005810 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00005811 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005812 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00005813 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00005814 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00005815 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00005816 case OMPC_num_tasks:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005817 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005818 llvm_unreachable("Clause is not allowed.");
5819 }
5820 return Res;
5821}
5822
5823OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
5824 SourceLocation KindKwLoc,
5825 SourceLocation StartLoc,
5826 SourceLocation LParenLoc,
5827 SourceLocation EndLoc) {
5828 if (Kind == OMPC_DEFAULT_unknown) {
5829 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005830 static_assert(OMPC_DEFAULT_unknown > 0,
5831 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00005832 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005833 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005834 Values += "'";
5835 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
5836 Values += "'";
5837 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005838 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005839 Values += " or ";
5840 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005841 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005842 break;
5843 default:
5844 Values += Sep;
5845 break;
5846 }
5847 }
5848 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00005849 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005850 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005851 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00005852 switch (Kind) {
5853 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005854 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005855 break;
5856 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005857 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005858 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005859 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005860 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00005861 break;
5862 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005863 return new (Context)
5864 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005865}
5866
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005867OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
5868 SourceLocation KindKwLoc,
5869 SourceLocation StartLoc,
5870 SourceLocation LParenLoc,
5871 SourceLocation EndLoc) {
5872 if (Kind == OMPC_PROC_BIND_unknown) {
5873 std::string Values;
5874 std::string Sep(", ");
5875 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
5876 Values += "'";
5877 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
5878 Values += "'";
5879 switch (i) {
5880 case OMPC_PROC_BIND_unknown - 2:
5881 Values += " or ";
5882 break;
5883 case OMPC_PROC_BIND_unknown - 1:
5884 break;
5885 default:
5886 Values += Sep;
5887 break;
5888 }
5889 }
5890 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00005891 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005892 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005893 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005894 return new (Context)
5895 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005896}
5897
Alexey Bataev56dafe82014-06-20 07:16:17 +00005898OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
5899 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
5900 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005901 SourceLocation ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00005902 SourceLocation EndLoc) {
5903 OMPClause *Res = nullptr;
5904 switch (Kind) {
5905 case OMPC_schedule:
5906 Res = ActOnOpenMPScheduleClause(
5907 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005908 LParenLoc, ArgumentLoc, DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00005909 break;
5910 case OMPC_if:
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005911 Res =
5912 ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument), Expr,
5913 StartLoc, LParenLoc, ArgumentLoc, DelimLoc, EndLoc);
5914 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00005915 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005916 case OMPC_num_threads:
5917 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00005918 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005919 case OMPC_collapse:
5920 case OMPC_default:
5921 case OMPC_proc_bind:
5922 case OMPC_private:
5923 case OMPC_firstprivate:
5924 case OMPC_lastprivate:
5925 case OMPC_shared:
5926 case OMPC_reduction:
5927 case OMPC_linear:
5928 case OMPC_aligned:
5929 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005930 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005931 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00005932 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005933 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005934 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005935 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00005936 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005937 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00005938 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00005939 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00005940 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005941 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005942 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00005943 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00005944 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005945 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00005946 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00005947 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005948 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00005949 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00005950 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00005951 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00005952 case OMPC_num_tasks:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005953 case OMPC_unknown:
5954 llvm_unreachable("Clause is not allowed.");
5955 }
5956 return Res;
5957}
5958
5959OMPClause *Sema::ActOnOpenMPScheduleClause(
5960 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
5961 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
5962 SourceLocation EndLoc) {
5963 if (Kind == OMPC_SCHEDULE_unknown) {
5964 std::string Values;
5965 std::string Sep(", ");
5966 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) {
5967 Values += "'";
5968 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i);
5969 Values += "'";
5970 switch (i) {
5971 case OMPC_SCHEDULE_unknown - 2:
5972 Values += " or ";
5973 break;
5974 case OMPC_SCHEDULE_unknown - 1:
5975 break;
5976 default:
5977 Values += Sep;
5978 break;
5979 }
5980 }
5981 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
5982 << Values << getOpenMPClauseName(OMPC_schedule);
5983 return nullptr;
5984 }
5985 Expr *ValExpr = ChunkSize;
Alexey Bataev040d5402015-05-12 08:35:28 +00005986 Expr *HelperValExpr = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00005987 if (ChunkSize) {
5988 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
5989 !ChunkSize->isInstantiationDependent() &&
5990 !ChunkSize->containsUnexpandedParameterPack()) {
5991 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
5992 ExprResult Val =
5993 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
5994 if (Val.isInvalid())
5995 return nullptr;
5996
5997 ValExpr = Val.get();
5998
5999 // OpenMP [2.7.1, Restrictions]
6000 // chunk_size must be a loop invariant integer expression with a positive
6001 // value.
6002 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00006003 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
6004 if (Result.isSigned() && !Result.isStrictlyPositive()) {
6005 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00006006 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00006007 return nullptr;
6008 }
6009 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
6010 auto *ImpVar = buildVarDecl(*this, ChunkSize->getExprLoc(),
6011 ChunkSize->getType(), ".chunk.");
6012 auto *ImpVarRef = buildDeclRefExpr(*this, ImpVar, ChunkSize->getType(),
6013 ChunkSize->getExprLoc(),
6014 /*RefersToCapture=*/true);
6015 HelperValExpr = ImpVarRef;
Alexey Bataev56dafe82014-06-20 07:16:17 +00006016 }
6017 }
6018 }
6019
6020 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc,
Alexey Bataev040d5402015-05-12 08:35:28 +00006021 EndLoc, Kind, ValExpr, HelperValExpr);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006022}
6023
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006024OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
6025 SourceLocation StartLoc,
6026 SourceLocation EndLoc) {
6027 OMPClause *Res = nullptr;
6028 switch (Kind) {
6029 case OMPC_ordered:
6030 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
6031 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00006032 case OMPC_nowait:
6033 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
6034 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006035 case OMPC_untied:
6036 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
6037 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006038 case OMPC_mergeable:
6039 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
6040 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006041 case OMPC_read:
6042 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
6043 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00006044 case OMPC_write:
6045 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
6046 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006047 case OMPC_update:
6048 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
6049 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00006050 case OMPC_capture:
6051 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
6052 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006053 case OMPC_seq_cst:
6054 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
6055 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00006056 case OMPC_threads:
6057 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
6058 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006059 case OMPC_simd:
6060 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
6061 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00006062 case OMPC_nogroup:
6063 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
6064 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006065 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006066 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006067 case OMPC_num_threads:
6068 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006069 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006070 case OMPC_collapse:
6071 case OMPC_schedule:
6072 case OMPC_private:
6073 case OMPC_firstprivate:
6074 case OMPC_lastprivate:
6075 case OMPC_shared:
6076 case OMPC_reduction:
6077 case OMPC_linear:
6078 case OMPC_aligned:
6079 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006080 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006081 case OMPC_default:
6082 case OMPC_proc_bind:
6083 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006084 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006085 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006086 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006087 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006088 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006089 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006090 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006091 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00006092 case OMPC_num_tasks:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006093 case OMPC_unknown:
6094 llvm_unreachable("Clause is not allowed.");
6095 }
6096 return Res;
6097}
6098
Alexey Bataev236070f2014-06-20 11:19:47 +00006099OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
6100 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006101 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00006102 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
6103}
6104
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006105OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
6106 SourceLocation EndLoc) {
6107 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
6108}
6109
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006110OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
6111 SourceLocation EndLoc) {
6112 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
6113}
6114
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006115OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
6116 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006117 return new (Context) OMPReadClause(StartLoc, EndLoc);
6118}
6119
Alexey Bataevdea47612014-07-23 07:46:59 +00006120OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
6121 SourceLocation EndLoc) {
6122 return new (Context) OMPWriteClause(StartLoc, EndLoc);
6123}
6124
Alexey Bataev67a4f222014-07-23 10:25:33 +00006125OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
6126 SourceLocation EndLoc) {
6127 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
6128}
6129
Alexey Bataev459dec02014-07-24 06:46:57 +00006130OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
6131 SourceLocation EndLoc) {
6132 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
6133}
6134
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006135OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
6136 SourceLocation EndLoc) {
6137 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
6138}
6139
Alexey Bataev346265e2015-09-25 10:37:12 +00006140OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
6141 SourceLocation EndLoc) {
6142 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
6143}
6144
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006145OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
6146 SourceLocation EndLoc) {
6147 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
6148}
6149
Alexey Bataevb825de12015-12-07 10:51:44 +00006150OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
6151 SourceLocation EndLoc) {
6152 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
6153}
6154
Alexey Bataevc5e02582014-06-16 07:08:35 +00006155OMPClause *Sema::ActOnOpenMPVarListClause(
6156 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
6157 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
6158 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006159 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Kelvin Li0bff7af2015-11-23 05:32:03 +00006160 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
6161 OpenMPMapClauseKind MapType, SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006162 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006163 switch (Kind) {
6164 case OMPC_private:
6165 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6166 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006167 case OMPC_firstprivate:
6168 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6169 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00006170 case OMPC_lastprivate:
6171 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6172 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00006173 case OMPC_shared:
6174 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
6175 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006176 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00006177 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
6178 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006179 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00006180 case OMPC_linear:
6181 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00006182 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00006183 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006184 case OMPC_aligned:
6185 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
6186 ColonLoc, EndLoc);
6187 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006188 case OMPC_copyin:
6189 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
6190 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00006191 case OMPC_copyprivate:
6192 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6193 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00006194 case OMPC_flush:
6195 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
6196 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006197 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006198 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
6199 StartLoc, LParenLoc, EndLoc);
6200 break;
6201 case OMPC_map:
6202 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, DepLinMapLoc, ColonLoc,
6203 VarList, StartLoc, LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006204 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006205 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006206 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00006207 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00006208 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006209 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00006210 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006211 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006212 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006213 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006214 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006215 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006216 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006217 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006218 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006219 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006220 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006221 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006222 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006223 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00006224 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006225 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006226 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006227 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006228 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006229 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006230 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006231 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006232 case OMPC_num_tasks:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006233 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006234 llvm_unreachable("Clause is not allowed.");
6235 }
6236 return Res;
6237}
6238
6239OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
6240 SourceLocation StartLoc,
6241 SourceLocation LParenLoc,
6242 SourceLocation EndLoc) {
6243 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00006244 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00006245 for (auto &RefExpr : VarList) {
6246 assert(RefExpr && "NULL expr in OpenMP private clause.");
6247 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006248 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006249 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006250 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006251 continue;
6252 }
6253
Alexey Bataeved09d242014-05-28 05:53:51 +00006254 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006255 // OpenMP [2.1, C/C++]
6256 // A list item is a variable name.
6257 // OpenMP [2.9.3.3, Restrictions, p.1]
6258 // A variable that is part of another variable (as an array or
6259 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00006260 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006261 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006262 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006263 continue;
6264 }
6265 Decl *D = DE->getDecl();
6266 VarDecl *VD = cast<VarDecl>(D);
6267
6268 QualType Type = VD->getType();
6269 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6270 // It will be analyzed later.
6271 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006272 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006273 continue;
6274 }
6275
6276 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
6277 // A variable that appears in a private clause must not have an incomplete
6278 // type or a reference type.
6279 if (RequireCompleteType(ELoc, Type,
6280 diag::err_omp_private_incomplete_type)) {
6281 continue;
6282 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006283 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006284
Alexey Bataev758e55e2013-09-06 18:03:48 +00006285 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6286 // in a Construct]
6287 // Variables with the predetermined data-sharing attributes may not be
6288 // listed in data-sharing attributes clauses, except for the cases
6289 // listed below. For these exceptions only, listing a predetermined
6290 // variable in a data-sharing attribute clause is allowed and overrides
6291 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006292 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006293 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006294 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
6295 << getOpenMPClauseName(OMPC_private);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006296 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006297 continue;
6298 }
6299
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006300 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00006301 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006302 DSAStack->getCurrentDirective() == OMPD_task) {
6303 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
6304 << getOpenMPClauseName(OMPC_private) << Type
6305 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
6306 bool IsDecl =
6307 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6308 Diag(VD->getLocation(),
6309 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6310 << VD;
6311 continue;
6312 }
6313
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006314 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
6315 // A variable of class type (or array thereof) that appears in a private
6316 // clause requires an accessible, unambiguous default constructor for the
6317 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00006318 // Generate helper private variable and initialize it with the default
6319 // value. The address of the original variable is replaced by the address of
6320 // the new private variable in CodeGen. This new variable is not added to
6321 // IdResolver, so the code in the OpenMP region uses original variable for
6322 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006323 Type = Type.getUnqualifiedType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006324 auto VDPrivate = buildVarDecl(*this, DE->getExprLoc(), Type, VD->getName(),
6325 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00006326 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006327 if (VDPrivate->isInvalidDecl())
6328 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006329 auto VDPrivateRefExpr = buildDeclRefExpr(
6330 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev03b340a2014-10-21 03:16:40 +00006331
Alexey Bataev758e55e2013-09-06 18:03:48 +00006332 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006333 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006334 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006335 }
6336
Alexey Bataeved09d242014-05-28 05:53:51 +00006337 if (Vars.empty())
6338 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006339
Alexey Bataev03b340a2014-10-21 03:16:40 +00006340 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
6341 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006342}
6343
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006344namespace {
6345class DiagsUninitializedSeveretyRAII {
6346private:
6347 DiagnosticsEngine &Diags;
6348 SourceLocation SavedLoc;
6349 bool IsIgnored;
6350
6351public:
6352 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
6353 bool IsIgnored)
6354 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
6355 if (!IsIgnored) {
6356 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
6357 /*Map*/ diag::Severity::Ignored, Loc);
6358 }
6359 }
6360 ~DiagsUninitializedSeveretyRAII() {
6361 if (!IsIgnored)
6362 Diags.popMappings(SavedLoc);
6363 }
6364};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00006365}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006366
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006367OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
6368 SourceLocation StartLoc,
6369 SourceLocation LParenLoc,
6370 SourceLocation EndLoc) {
6371 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006372 SmallVector<Expr *, 8> PrivateCopies;
6373 SmallVector<Expr *, 8> Inits;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006374 bool IsImplicitClause =
6375 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
6376 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
6377
Alexey Bataeved09d242014-05-28 05:53:51 +00006378 for (auto &RefExpr : VarList) {
6379 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
6380 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006381 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006382 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006383 PrivateCopies.push_back(nullptr);
6384 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006385 continue;
6386 }
6387
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006388 SourceLocation ELoc =
6389 IsImplicitClause ? ImplicitClauseLoc : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006390 // OpenMP [2.1, C/C++]
6391 // A list item is a variable name.
6392 // OpenMP [2.9.3.3, Restrictions, p.1]
6393 // A variable that is part of another variable (as an array or
6394 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00006395 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006396 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006397 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006398 continue;
6399 }
6400 Decl *D = DE->getDecl();
6401 VarDecl *VD = cast<VarDecl>(D);
6402
6403 QualType Type = VD->getType();
6404 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6405 // It will be analyzed later.
6406 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006407 PrivateCopies.push_back(nullptr);
6408 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006409 continue;
6410 }
6411
6412 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
6413 // A variable that appears in a private clause must not have an incomplete
6414 // type or a reference type.
6415 if (RequireCompleteType(ELoc, Type,
6416 diag::err_omp_firstprivate_incomplete_type)) {
6417 continue;
6418 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006419 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006420
6421 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
6422 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00006423 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006424 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006425 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006426
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006427 // If an implicit firstprivate variable found it was checked already.
6428 if (!IsImplicitClause) {
6429 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006430 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006431 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
6432 // A list item that specifies a given variable may not appear in more
6433 // than one clause on the same directive, except that a variable may be
6434 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006435 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00006436 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006437 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00006438 << getOpenMPClauseName(DVar.CKind)
6439 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006440 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006441 continue;
6442 }
6443
6444 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6445 // in a Construct]
6446 // Variables with the predetermined data-sharing attributes may not be
6447 // listed in data-sharing attributes clauses, except for the cases
6448 // listed below. For these exceptions only, listing a predetermined
6449 // variable in a data-sharing attribute clause is allowed and overrides
6450 // the variable's predetermined data-sharing attributes.
6451 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6452 // in a Construct, C/C++, p.2]
6453 // Variables with const-qualified type having no mutable member may be
6454 // listed in a firstprivate clause, even if they are static data members.
6455 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
6456 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
6457 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00006458 << getOpenMPClauseName(DVar.CKind)
6459 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006460 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006461 continue;
6462 }
6463
Alexey Bataevf29276e2014-06-18 04:14:57 +00006464 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006465 // OpenMP [2.9.3.4, Restrictions, p.2]
6466 // A list item that is private within a parallel region must not appear
6467 // in a firstprivate clause on a worksharing construct if any of the
6468 // worksharing regions arising from the worksharing construct ever bind
6469 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00006470 if (isOpenMPWorksharingDirective(CurrDir) &&
6471 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006472 DVar = DSAStack->getImplicitDSA(VD, true);
6473 if (DVar.CKind != OMPC_shared &&
6474 (isOpenMPParallelDirective(DVar.DKind) ||
6475 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00006476 Diag(ELoc, diag::err_omp_required_access)
6477 << getOpenMPClauseName(OMPC_firstprivate)
6478 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006479 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006480 continue;
6481 }
6482 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006483 // OpenMP [2.9.3.4, Restrictions, p.3]
6484 // A list item that appears in a reduction clause of a parallel construct
6485 // must not appear in a firstprivate clause on a worksharing or task
6486 // construct if any of the worksharing or task regions arising from the
6487 // worksharing or task construct ever bind to any of the parallel regions
6488 // arising from the parallel construct.
6489 // OpenMP [2.9.3.4, Restrictions, p.4]
6490 // A list item that appears in a reduction clause in worksharing
6491 // construct must not appear in a firstprivate clause in a task construct
6492 // encountered during execution of any of the worksharing regions arising
6493 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006494 if (CurrDir == OMPD_task) {
6495 DVar =
6496 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
6497 [](OpenMPDirectiveKind K) -> bool {
6498 return isOpenMPParallelDirective(K) ||
6499 isOpenMPWorksharingDirective(K);
6500 },
6501 false);
6502 if (DVar.CKind == OMPC_reduction &&
6503 (isOpenMPParallelDirective(DVar.DKind) ||
6504 isOpenMPWorksharingDirective(DVar.DKind))) {
6505 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
6506 << getOpenMPDirectiveName(DVar.DKind);
6507 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6508 continue;
6509 }
6510 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006511
6512 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
6513 // A list item that is private within a teams region must not appear in a
6514 // firstprivate clause on a distribute construct if any of the distribute
6515 // regions arising from the distribute construct ever bind to any of the
6516 // teams regions arising from the teams construct.
6517 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
6518 // A list item that appears in a reduction clause of a teams construct
6519 // must not appear in a firstprivate clause on a distribute construct if
6520 // any of the distribute regions arising from the distribute construct
6521 // ever bind to any of the teams regions arising from the teams construct.
6522 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
6523 // A list item may appear in a firstprivate or lastprivate clause but not
6524 // both.
6525 if (CurrDir == OMPD_distribute) {
6526 DVar = DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_private),
6527 [](OpenMPDirectiveKind K) -> bool {
6528 return isOpenMPTeamsDirective(K);
6529 },
6530 false);
6531 if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
6532 Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
6533 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6534 continue;
6535 }
6536 DVar = DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
6537 [](OpenMPDirectiveKind K) -> bool {
6538 return isOpenMPTeamsDirective(K);
6539 },
6540 false);
6541 if (DVar.CKind == OMPC_reduction &&
6542 isOpenMPTeamsDirective(DVar.DKind)) {
6543 Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
6544 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6545 continue;
6546 }
6547 DVar = DSAStack->getTopDSA(VD, false);
6548 if (DVar.CKind == OMPC_lastprivate) {
6549 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
6550 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6551 continue;
6552 }
6553 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006554 }
6555
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006556 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00006557 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006558 DSAStack->getCurrentDirective() == OMPD_task) {
6559 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
6560 << getOpenMPClauseName(OMPC_firstprivate) << Type
6561 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
6562 bool IsDecl =
6563 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6564 Diag(VD->getLocation(),
6565 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6566 << VD;
6567 continue;
6568 }
6569
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006570 Type = Type.getUnqualifiedType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006571 auto VDPrivate = buildVarDecl(*this, ELoc, Type, VD->getName(),
6572 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006573 // Generate helper private variable and initialize it with the value of the
6574 // original variable. The address of the original variable is replaced by
6575 // the address of the new private variable in the CodeGen. This new variable
6576 // is not added to IdResolver, so the code in the OpenMP region uses
6577 // original variable for proper diagnostics and variable capturing.
6578 Expr *VDInitRefExpr = nullptr;
6579 // For arrays generate initializer for single element and replace it by the
6580 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006581 if (Type->isArrayType()) {
6582 auto VDInit =
6583 buildVarDecl(*this, DE->getExprLoc(), ElemType, VD->getName());
6584 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006585 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006586 ElemType = ElemType.getUnqualifiedType();
6587 auto *VDInitTemp = buildVarDecl(*this, DE->getLocStart(), ElemType,
6588 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00006589 InitializedEntity Entity =
6590 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006591 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
6592
6593 InitializationSequence InitSeq(*this, Entity, Kind, Init);
6594 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
6595 if (Result.isInvalid())
6596 VDPrivate->setInvalidDecl();
6597 else
6598 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006599 // Remove temp variable declaration.
6600 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006601 } else {
Alexey Bataev69c62a92015-04-15 04:52:20 +00006602 auto *VDInit =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006603 buildVarDecl(*this, DE->getLocStart(), Type, ".firstprivate.temp");
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006604 VDInitRefExpr =
6605 buildDeclRefExpr(*this, VDInit, DE->getType(), DE->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00006606 AddInitializerToDecl(VDPrivate,
6607 DefaultLvalueConversion(VDInitRefExpr).get(),
6608 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006609 }
6610 if (VDPrivate->isInvalidDecl()) {
6611 if (IsImplicitClause) {
6612 Diag(DE->getExprLoc(),
6613 diag::note_omp_task_predetermined_firstprivate_here);
6614 }
6615 continue;
6616 }
6617 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00006618 auto VDPrivateRefExpr = buildDeclRefExpr(
6619 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006620 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
6621 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006622 PrivateCopies.push_back(VDPrivateRefExpr);
6623 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006624 }
6625
Alexey Bataeved09d242014-05-28 05:53:51 +00006626 if (Vars.empty())
6627 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006628
6629 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006630 Vars, PrivateCopies, Inits);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006631}
6632
Alexander Musman1bb328c2014-06-04 13:06:39 +00006633OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
6634 SourceLocation StartLoc,
6635 SourceLocation LParenLoc,
6636 SourceLocation EndLoc) {
6637 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00006638 SmallVector<Expr *, 8> SrcExprs;
6639 SmallVector<Expr *, 8> DstExprs;
6640 SmallVector<Expr *, 8> AssignmentOps;
Alexander Musman1bb328c2014-06-04 13:06:39 +00006641 for (auto &RefExpr : VarList) {
6642 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
6643 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6644 // It will be analyzed later.
6645 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00006646 SrcExprs.push_back(nullptr);
6647 DstExprs.push_back(nullptr);
6648 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006649 continue;
6650 }
6651
6652 SourceLocation ELoc = RefExpr->getExprLoc();
6653 // OpenMP [2.1, C/C++]
6654 // A list item is a variable name.
6655 // OpenMP [2.14.3.5, Restrictions, p.1]
6656 // A variable that is part of another variable (as an array or structure
6657 // element) cannot appear in a lastprivate clause.
6658 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
6659 if (!DE || !isa<VarDecl>(DE->getDecl())) {
6660 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
6661 continue;
6662 }
6663 Decl *D = DE->getDecl();
6664 VarDecl *VD = cast<VarDecl>(D);
6665
6666 QualType Type = VD->getType();
6667 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6668 // It will be analyzed later.
6669 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00006670 SrcExprs.push_back(nullptr);
6671 DstExprs.push_back(nullptr);
6672 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006673 continue;
6674 }
6675
6676 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
6677 // A variable that appears in a lastprivate clause must not have an
6678 // incomplete type or a reference type.
6679 if (RequireCompleteType(ELoc, Type,
6680 diag::err_omp_lastprivate_incomplete_type)) {
6681 continue;
6682 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006683 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00006684
6685 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
6686 // in a Construct]
6687 // Variables with the predetermined data-sharing attributes may not be
6688 // listed in data-sharing attributes clauses, except for the cases
6689 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006690 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006691 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
6692 DVar.CKind != OMPC_firstprivate &&
6693 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
6694 Diag(ELoc, diag::err_omp_wrong_dsa)
6695 << getOpenMPClauseName(DVar.CKind)
6696 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006697 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006698 continue;
6699 }
6700
Alexey Bataevf29276e2014-06-18 04:14:57 +00006701 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
6702 // OpenMP [2.14.3.5, Restrictions, p.2]
6703 // A list item that is private within a parallel region, or that appears in
6704 // the reduction clause of a parallel construct, must not appear in a
6705 // lastprivate clause on a worksharing construct if any of the corresponding
6706 // worksharing regions ever binds to any of the corresponding parallel
6707 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00006708 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00006709 if (isOpenMPWorksharingDirective(CurrDir) &&
6710 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006711 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006712 if (DVar.CKind != OMPC_shared) {
6713 Diag(ELoc, diag::err_omp_required_access)
6714 << getOpenMPClauseName(OMPC_lastprivate)
6715 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006716 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006717 continue;
6718 }
6719 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00006720 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00006721 // A variable of class type (or array thereof) that appears in a
6722 // lastprivate clause requires an accessible, unambiguous default
6723 // constructor for the class type, unless the list item is also specified
6724 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00006725 // A variable of class type (or array thereof) that appears in a
6726 // lastprivate clause requires an accessible, unambiguous copy assignment
6727 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00006728 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00006729 auto *SrcVD = buildVarDecl(*this, DE->getLocStart(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006730 Type.getUnqualifiedType(), ".lastprivate.src",
6731 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00006732 auto *PseudoSrcExpr = buildDeclRefExpr(
6733 *this, SrcVD, Type.getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00006734 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006735 buildVarDecl(*this, DE->getLocStart(), Type, ".lastprivate.dst",
6736 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00006737 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006738 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00006739 // For arrays generate assignment operation for single element and replace
6740 // it by the original array element in CodeGen.
6741 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
6742 PseudoDstExpr, PseudoSrcExpr);
6743 if (AssignmentOp.isInvalid())
6744 continue;
6745 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
6746 /*DiscardedValue=*/true);
6747 if (AssignmentOp.isInvalid())
6748 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00006749
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006750 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
6751 // A list item may appear in a firstprivate or lastprivate clause but not
6752 // both.
6753 if (CurrDir == OMPD_distribute) {
6754 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
6755 if (DVar.CKind == OMPC_firstprivate) {
6756 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
6757 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6758 continue;
6759 }
6760 }
6761
Alexey Bataev39f915b82015-05-08 10:41:21 +00006762 if (TopDVar.CKind != OMPC_firstprivate)
Alexey Bataevf29276e2014-06-18 04:14:57 +00006763 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006764 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00006765 SrcExprs.push_back(PseudoSrcExpr);
6766 DstExprs.push_back(PseudoDstExpr);
6767 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00006768 }
6769
6770 if (Vars.empty())
6771 return nullptr;
6772
6773 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00006774 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006775}
6776
Alexey Bataev758e55e2013-09-06 18:03:48 +00006777OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
6778 SourceLocation StartLoc,
6779 SourceLocation LParenLoc,
6780 SourceLocation EndLoc) {
6781 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00006782 for (auto &RefExpr : VarList) {
6783 assert(RefExpr && "NULL expr in OpenMP shared clause.");
6784 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00006785 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006786 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006787 continue;
6788 }
6789
Alexey Bataeved09d242014-05-28 05:53:51 +00006790 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00006791 // OpenMP [2.1, C/C++]
6792 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00006793 // OpenMP [2.14.3.2, Restrictions, p.1]
6794 // A variable that is part of another variable (as an array or structure
6795 // element) cannot appear in a shared unless it is a static data member
6796 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00006797 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006798 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006799 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00006800 continue;
6801 }
6802 Decl *D = DE->getDecl();
6803 VarDecl *VD = cast<VarDecl>(D);
6804
6805 QualType Type = VD->getType();
6806 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6807 // It will be analyzed later.
6808 Vars.push_back(DE);
6809 continue;
6810 }
6811
6812 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6813 // in a Construct]
6814 // Variables with the predetermined data-sharing attributes may not be
6815 // listed in data-sharing attributes clauses, except for the cases
6816 // listed below. For these exceptions only, listing a predetermined
6817 // variable in a data-sharing attribute clause is allowed and overrides
6818 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006819 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00006820 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
6821 DVar.RefExpr) {
6822 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
6823 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006824 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006825 continue;
6826 }
6827
6828 DSAStack->addDSA(VD, DE, OMPC_shared);
6829 Vars.push_back(DE);
6830 }
6831
Alexey Bataeved09d242014-05-28 05:53:51 +00006832 if (Vars.empty())
6833 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00006834
6835 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
6836}
6837
Alexey Bataevc5e02582014-06-16 07:08:35 +00006838namespace {
6839class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
6840 DSAStackTy *Stack;
6841
6842public:
6843 bool VisitDeclRefExpr(DeclRefExpr *E) {
6844 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006845 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006846 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
6847 return false;
6848 if (DVar.CKind != OMPC_unknown)
6849 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00006850 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006851 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006852 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00006853 return true;
6854 return false;
6855 }
6856 return false;
6857 }
6858 bool VisitStmt(Stmt *S) {
6859 for (auto Child : S->children()) {
6860 if (Child && Visit(Child))
6861 return true;
6862 }
6863 return false;
6864 }
Alexey Bataev23b69422014-06-18 07:08:49 +00006865 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00006866};
Alexey Bataev23b69422014-06-18 07:08:49 +00006867} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00006868
6869OMPClause *Sema::ActOnOpenMPReductionClause(
6870 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
6871 SourceLocation ColonLoc, SourceLocation EndLoc,
6872 CXXScopeSpec &ReductionIdScopeSpec,
6873 const DeclarationNameInfo &ReductionId) {
6874 // TODO: Allow scope specification search when 'declare reduction' is
6875 // supported.
6876 assert(ReductionIdScopeSpec.isEmpty() &&
6877 "No support for scoped reduction identifiers yet.");
6878
6879 auto DN = ReductionId.getName();
6880 auto OOK = DN.getCXXOverloadedOperator();
6881 BinaryOperatorKind BOK = BO_Comma;
6882
6883 // OpenMP [2.14.3.6, reduction clause]
6884 // C
6885 // reduction-identifier is either an identifier or one of the following
6886 // operators: +, -, *, &, |, ^, && and ||
6887 // C++
6888 // reduction-identifier is either an id-expression or one of the following
6889 // operators: +, -, *, &, |, ^, && and ||
6890 // FIXME: Only 'min' and 'max' identifiers are supported for now.
6891 switch (OOK) {
6892 case OO_Plus:
6893 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006894 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006895 break;
6896 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006897 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006898 break;
6899 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006900 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006901 break;
6902 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006903 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006904 break;
6905 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006906 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006907 break;
6908 case OO_AmpAmp:
6909 BOK = BO_LAnd;
6910 break;
6911 case OO_PipePipe:
6912 BOK = BO_LOr;
6913 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006914 case OO_New:
6915 case OO_Delete:
6916 case OO_Array_New:
6917 case OO_Array_Delete:
6918 case OO_Slash:
6919 case OO_Percent:
6920 case OO_Tilde:
6921 case OO_Exclaim:
6922 case OO_Equal:
6923 case OO_Less:
6924 case OO_Greater:
6925 case OO_LessEqual:
6926 case OO_GreaterEqual:
6927 case OO_PlusEqual:
6928 case OO_MinusEqual:
6929 case OO_StarEqual:
6930 case OO_SlashEqual:
6931 case OO_PercentEqual:
6932 case OO_CaretEqual:
6933 case OO_AmpEqual:
6934 case OO_PipeEqual:
6935 case OO_LessLess:
6936 case OO_GreaterGreater:
6937 case OO_LessLessEqual:
6938 case OO_GreaterGreaterEqual:
6939 case OO_EqualEqual:
6940 case OO_ExclaimEqual:
6941 case OO_PlusPlus:
6942 case OO_MinusMinus:
6943 case OO_Comma:
6944 case OO_ArrowStar:
6945 case OO_Arrow:
6946 case OO_Call:
6947 case OO_Subscript:
6948 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +00006949 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006950 case NUM_OVERLOADED_OPERATORS:
6951 llvm_unreachable("Unexpected reduction identifier");
6952 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006953 if (auto II = DN.getAsIdentifierInfo()) {
6954 if (II->isStr("max"))
6955 BOK = BO_GT;
6956 else if (II->isStr("min"))
6957 BOK = BO_LT;
6958 }
6959 break;
6960 }
6961 SourceRange ReductionIdRange;
6962 if (ReductionIdScopeSpec.isValid()) {
6963 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
6964 }
6965 ReductionIdRange.setEnd(ReductionId.getEndLoc());
6966 if (BOK == BO_Comma) {
6967 // Not allowed reduction identifier is found.
6968 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
6969 << ReductionIdRange;
6970 return nullptr;
6971 }
6972
6973 SmallVector<Expr *, 8> Vars;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006974 SmallVector<Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006975 SmallVector<Expr *, 8> LHSs;
6976 SmallVector<Expr *, 8> RHSs;
6977 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006978 for (auto RefExpr : VarList) {
6979 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
6980 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6981 // It will be analyzed later.
6982 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006983 Privates.push_back(nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006984 LHSs.push_back(nullptr);
6985 RHSs.push_back(nullptr);
6986 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006987 continue;
6988 }
6989
6990 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
6991 RefExpr->isInstantiationDependent() ||
6992 RefExpr->containsUnexpandedParameterPack()) {
6993 // It will be analyzed later.
6994 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006995 Privates.push_back(nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006996 LHSs.push_back(nullptr);
6997 RHSs.push_back(nullptr);
6998 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006999 continue;
7000 }
7001
7002 auto ELoc = RefExpr->getExprLoc();
7003 auto ERange = RefExpr->getSourceRange();
7004 // OpenMP [2.1, C/C++]
7005 // A list item is a variable or array section, subject to the restrictions
7006 // specified in Section 2.4 on page 42 and in each of the sections
7007 // describing clauses and directives for which a list appears.
7008 // OpenMP [2.14.3.3, Restrictions, p.1]
7009 // A variable that is part of another variable (as an array or
7010 // structure element) cannot appear in a private clause.
Alexey Bataeva1764212015-09-30 09:22:36 +00007011 auto *DE = dyn_cast<DeclRefExpr>(RefExpr);
7012 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr);
7013 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr);
7014 if (!ASE && !OASE && (!DE || !isa<VarDecl>(DE->getDecl()))) {
7015 Diag(ELoc, diag::err_omp_expected_var_name_or_array_item) << ERange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007016 continue;
7017 }
Alexey Bataeva1764212015-09-30 09:22:36 +00007018 QualType Type;
7019 VarDecl *VD = nullptr;
7020 if (DE) {
7021 auto D = DE->getDecl();
7022 VD = cast<VarDecl>(D);
7023 Type = VD->getType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007024 } else if (ASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007025 Type = ASE->getType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007026 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
7027 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7028 Base = TempASE->getBase()->IgnoreParenImpCasts();
7029 DE = dyn_cast<DeclRefExpr>(Base);
7030 if (DE)
7031 VD = dyn_cast<VarDecl>(DE->getDecl());
7032 if (!VD) {
7033 Diag(Base->getExprLoc(), diag::err_omp_expected_base_var_name)
7034 << 0 << Base->getSourceRange();
7035 continue;
7036 }
7037 } else if (OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007038 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
7039 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
7040 Type = ATy->getElementType();
7041 else
7042 Type = BaseType->getPointeeType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007043 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
7044 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
7045 Base = TempOASE->getBase()->IgnoreParenImpCasts();
7046 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7047 Base = TempASE->getBase()->IgnoreParenImpCasts();
7048 DE = dyn_cast<DeclRefExpr>(Base);
7049 if (DE)
7050 VD = dyn_cast<VarDecl>(DE->getDecl());
7051 if (!VD) {
7052 Diag(Base->getExprLoc(), diag::err_omp_expected_base_var_name)
7053 << 1 << Base->getSourceRange();
7054 continue;
7055 }
Alexey Bataeva1764212015-09-30 09:22:36 +00007056 }
7057
Alexey Bataevc5e02582014-06-16 07:08:35 +00007058 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7059 // A variable that appears in a private clause must not have an incomplete
7060 // type or a reference type.
7061 if (RequireCompleteType(ELoc, Type,
7062 diag::err_omp_reduction_incomplete_type))
7063 continue;
7064 // OpenMP [2.14.3.6, reduction clause, Restrictions]
7065 // Arrays may not appear in a reduction clause.
7066 if (Type.getNonReferenceType()->isArrayType()) {
7067 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007068 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007069 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7070 VarDecl::DeclarationOnly;
7071 Diag(VD->getLocation(),
7072 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7073 << VD;
7074 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007075 continue;
7076 }
7077 // OpenMP [2.14.3.6, reduction clause, Restrictions]
7078 // A list item that appears in a reduction clause must not be
7079 // const-qualified.
7080 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007081 Diag(ELoc, diag::err_omp_const_reduction_list_item)
Alexey Bataevc5e02582014-06-16 07:08:35 +00007082 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007083 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007084 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7085 VarDecl::DeclarationOnly;
7086 Diag(VD->getLocation(),
7087 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7088 << VD;
7089 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007090 continue;
7091 }
7092 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
7093 // If a list-item is a reference type then it must bind to the same object
7094 // for all threads of the team.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007095 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007096 VarDecl *VDDef = VD->getDefinition();
7097 if (Type->isReferenceType() && VDDef) {
7098 DSARefChecker Check(DSAStack);
7099 if (Check.Visit(VDDef->getInit())) {
7100 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
7101 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
7102 continue;
7103 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007104 }
7105 }
7106 // OpenMP [2.14.3.6, reduction clause, Restrictions]
7107 // The type of a list item that appears in a reduction clause must be valid
7108 // for the reduction-identifier. For a max or min reduction in C, the type
7109 // of the list item must be an allowed arithmetic data type: char, int,
7110 // float, double, or _Bool, possibly modified with long, short, signed, or
7111 // unsigned. For a max or min reduction in C++, the type of the list item
7112 // must be an allowed arithmetic data type: char, wchar_t, int, float,
7113 // double, or bool, possibly modified with long, short, signed, or unsigned.
7114 if ((BOK == BO_GT || BOK == BO_LT) &&
7115 !(Type->isScalarType() ||
7116 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
7117 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
7118 << getLangOpts().CPlusPlus;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007119 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007120 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7121 VarDecl::DeclarationOnly;
7122 Diag(VD->getLocation(),
7123 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7124 << VD;
7125 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007126 continue;
7127 }
7128 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
7129 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
7130 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007131 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007132 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7133 VarDecl::DeclarationOnly;
7134 Diag(VD->getLocation(),
7135 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7136 << VD;
7137 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007138 continue;
7139 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007140 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
7141 // in a Construct]
7142 // Variables with the predetermined data-sharing attributes may not be
7143 // listed in data-sharing attributes clauses, except for the cases
7144 // listed below. For these exceptions only, listing a predetermined
7145 // variable in a data-sharing attribute clause is allowed and overrides
7146 // the variable's predetermined data-sharing attributes.
7147 // OpenMP [2.14.3.6, Restrictions, p.3]
7148 // Any number of reduction clauses can be specified on the directive,
7149 // but a list item can appear only once in the reduction clauses for that
7150 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +00007151 DSAStackTy::DSAVarData DVar;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007152 DVar = DSAStack->getTopDSA(VD, false);
7153 if (DVar.CKind == OMPC_reduction) {
7154 Diag(ELoc, diag::err_omp_once_referenced)
7155 << getOpenMPClauseName(OMPC_reduction);
7156 if (DVar.RefExpr) {
7157 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007158 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007159 } else if (DVar.CKind != OMPC_unknown) {
7160 Diag(ELoc, diag::err_omp_wrong_dsa)
7161 << getOpenMPClauseName(DVar.CKind)
7162 << getOpenMPClauseName(OMPC_reduction);
7163 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7164 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007165 }
7166
7167 // OpenMP [2.14.3.6, Restrictions, p.1]
7168 // A list item that appears in a reduction clause of a worksharing
7169 // construct must be shared in the parallel regions to which any of the
7170 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007171 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
7172 if (isOpenMPWorksharingDirective(CurrDir) &&
7173 !isOpenMPParallelDirective(CurrDir)) {
7174 DVar = DSAStack->getImplicitDSA(VD, true);
7175 if (DVar.CKind != OMPC_shared) {
7176 Diag(ELoc, diag::err_omp_required_access)
7177 << getOpenMPClauseName(OMPC_reduction)
7178 << getOpenMPClauseName(OMPC_shared);
7179 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7180 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +00007181 }
7182 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007183
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007184 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007185 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
7186 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
7187 auto *RHSVD = buildVarDecl(*this, ELoc, Type, VD->getName(),
7188 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
7189 auto PrivateTy = Type;
7190 if (OASE) {
7191 // For array sections only:
7192 // Create pseudo array type for private copy. The size for this array will
7193 // be generated during codegen.
7194 // For array subscripts or single variables Private Ty is the same as Type
7195 // (type of the variable or single array element).
7196 PrivateTy = Context.getVariableArrayType(
7197 Type, new (Context) OpaqueValueExpr(SourceLocation(),
7198 Context.getSizeType(), VK_RValue),
7199 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
7200 }
7201 // Private copy.
7202 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, VD->getName(),
7203 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007204 // Add initializer for private variable.
7205 Expr *Init = nullptr;
7206 switch (BOK) {
7207 case BO_Add:
7208 case BO_Xor:
7209 case BO_Or:
7210 case BO_LOr:
7211 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
7212 if (Type->isScalarType() || Type->isAnyComplexType()) {
7213 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00007214 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007215 break;
7216 case BO_Mul:
7217 case BO_LAnd:
7218 if (Type->isScalarType() || Type->isAnyComplexType()) {
7219 // '*' and '&&' reduction ops - initializer is '1'.
7220 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
7221 }
7222 break;
7223 case BO_And: {
7224 // '&' reduction op - initializer is '~0'.
7225 QualType OrigType = Type;
7226 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) {
7227 Type = ComplexTy->getElementType();
7228 }
7229 if (Type->isRealFloatingType()) {
7230 llvm::APFloat InitValue =
7231 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
7232 /*isIEEE=*/true);
7233 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
7234 Type, ELoc);
7235 } else if (Type->isScalarType()) {
7236 auto Size = Context.getTypeSize(Type);
7237 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
7238 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
7239 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
7240 }
7241 if (Init && OrigType->isAnyComplexType()) {
7242 // Init = 0xFFFF + 0xFFFFi;
7243 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
7244 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
7245 }
7246 Type = OrigType;
7247 break;
7248 }
7249 case BO_LT:
7250 case BO_GT: {
7251 // 'min' reduction op - initializer is 'Largest representable number in
7252 // the reduction list item type'.
7253 // 'max' reduction op - initializer is 'Least representable number in
7254 // the reduction list item type'.
7255 if (Type->isIntegerType() || Type->isPointerType()) {
7256 bool IsSigned = Type->hasSignedIntegerRepresentation();
7257 auto Size = Context.getTypeSize(Type);
7258 QualType IntTy =
7259 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
7260 llvm::APInt InitValue =
7261 (BOK != BO_LT)
7262 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
7263 : llvm::APInt::getMinValue(Size)
7264 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
7265 : llvm::APInt::getMaxValue(Size);
7266 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
7267 if (Type->isPointerType()) {
7268 // Cast to pointer type.
7269 auto CastExpr = BuildCStyleCastExpr(
7270 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
7271 SourceLocation(), Init);
7272 if (CastExpr.isInvalid())
7273 continue;
7274 Init = CastExpr.get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00007275 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007276 } else if (Type->isRealFloatingType()) {
7277 llvm::APFloat InitValue = llvm::APFloat::getLargest(
7278 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
7279 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
7280 Type, ELoc);
7281 }
7282 break;
7283 }
7284 case BO_PtrMemD:
7285 case BO_PtrMemI:
7286 case BO_MulAssign:
7287 case BO_Div:
7288 case BO_Rem:
7289 case BO_Sub:
7290 case BO_Shl:
7291 case BO_Shr:
7292 case BO_LE:
7293 case BO_GE:
7294 case BO_EQ:
7295 case BO_NE:
7296 case BO_AndAssign:
7297 case BO_XorAssign:
7298 case BO_OrAssign:
7299 case BO_Assign:
7300 case BO_AddAssign:
7301 case BO_SubAssign:
7302 case BO_DivAssign:
7303 case BO_RemAssign:
7304 case BO_ShlAssign:
7305 case BO_ShrAssign:
7306 case BO_Comma:
7307 llvm_unreachable("Unexpected reduction operation");
7308 }
7309 if (Init) {
7310 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
7311 /*TypeMayContainAuto=*/false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007312 } else
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007313 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007314 if (!RHSVD->hasInit()) {
7315 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
7316 << ReductionIdRange;
Alexey Bataeva1764212015-09-30 09:22:36 +00007317 if (VD) {
7318 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7319 VarDecl::DeclarationOnly;
7320 Diag(VD->getLocation(),
7321 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7322 << VD;
7323 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007324 continue;
7325 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007326 // Store initializer for single element in private copy. Will be used during
7327 // codegen.
7328 PrivateVD->setInit(RHSVD->getInit());
7329 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataev39f915b82015-05-08 10:41:21 +00007330 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
7331 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007332 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007333 ExprResult ReductionOp =
7334 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
7335 LHSDRE, RHSDRE);
7336 if (ReductionOp.isUsable()) {
Alexey Bataev69a47792015-05-07 03:54:03 +00007337 if (BOK != BO_LT && BOK != BO_GT) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007338 ReductionOp =
7339 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
7340 BO_Assign, LHSDRE, ReductionOp.get());
7341 } else {
7342 auto *ConditionalOp = new (Context) ConditionalOperator(
7343 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
7344 RHSDRE, Type, VK_LValue, OK_Ordinary);
7345 ReductionOp =
7346 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
7347 BO_Assign, LHSDRE, ConditionalOp);
7348 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007349 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00007350 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007351 if (ReductionOp.isInvalid())
7352 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007353
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007354 DSAStack->addDSA(VD, DE, OMPC_reduction);
Alexey Bataeva1764212015-09-30 09:22:36 +00007355 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007356 Privates.push_back(PrivateDRE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007357 LHSs.push_back(LHSDRE);
7358 RHSs.push_back(RHSDRE);
7359 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00007360 }
7361
7362 if (Vars.empty())
7363 return nullptr;
7364
7365 return OMPReductionClause::Create(
7366 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007367 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
7368 LHSs, RHSs, ReductionOps);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007369}
7370
Alexey Bataev182227b2015-08-20 10:54:39 +00007371OMPClause *Sema::ActOnOpenMPLinearClause(
7372 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
7373 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
7374 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00007375 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007376 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00007377 SmallVector<Expr *, 8> Inits;
Alexey Bataev182227b2015-08-20 10:54:39 +00007378 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
7379 LinKind == OMPC_LINEAR_unknown) {
7380 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
7381 LinKind = OMPC_LINEAR_val;
7382 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007383 for (auto &RefExpr : VarList) {
7384 assert(RefExpr && "NULL expr in OpenMP linear clause.");
7385 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00007386 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007387 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007388 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00007389 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00007390 continue;
7391 }
7392
7393 // OpenMP [2.14.3.7, linear clause]
7394 // A list item that appears in a linear clause is subject to the private
7395 // clause semantics described in Section 2.14.3.3 on page 159 except as
7396 // noted. In addition, the value of the new list item on each iteration
7397 // of the associated loop(s) corresponds to the value of the original
7398 // list item before entering the construct plus the logical number of
7399 // the iteration times linear-step.
7400
Alexey Bataeved09d242014-05-28 05:53:51 +00007401 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00007402 // OpenMP [2.1, C/C++]
7403 // A list item is a variable name.
7404 // OpenMP [2.14.3.3, Restrictions, p.1]
7405 // A variable that is part of another variable (as an array or
7406 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00007407 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00007408 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00007409 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00007410 continue;
7411 }
7412
7413 VarDecl *VD = cast<VarDecl>(DE->getDecl());
7414
7415 // OpenMP [2.14.3.7, linear clause]
7416 // A list-item cannot appear in more than one linear clause.
7417 // A list-item that appears in a linear clause cannot appear in any
7418 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007419 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00007420 if (DVar.RefExpr) {
7421 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7422 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007423 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00007424 continue;
7425 }
7426
7427 QualType QType = VD->getType();
7428 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
7429 // It will be analyzed later.
7430 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007431 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00007432 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00007433 continue;
7434 }
7435
7436 // A variable must not have an incomplete type or a reference type.
7437 if (RequireCompleteType(ELoc, QType,
7438 diag::err_omp_linear_incomplete_type)) {
7439 continue;
7440 }
Alexey Bataev1185e192015-08-20 12:15:57 +00007441 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
7442 !QType->isReferenceType()) {
7443 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
7444 << QType << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
7445 continue;
7446 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007447 QType = QType.getNonReferenceType();
Alexander Musman8dba6642014-04-22 13:09:42 +00007448
7449 // A list item must not be const-qualified.
7450 if (QType.isConstant(Context)) {
7451 Diag(ELoc, diag::err_omp_const_variable)
7452 << getOpenMPClauseName(OMPC_linear);
7453 bool IsDecl =
7454 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7455 Diag(VD->getLocation(),
7456 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7457 << VD;
7458 continue;
7459 }
7460
7461 // A list item must be of integral or pointer type.
7462 QType = QType.getUnqualifiedType().getCanonicalType();
7463 const Type *Ty = QType.getTypePtrOrNull();
7464 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
7465 !Ty->isPointerType())) {
7466 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
7467 bool IsDecl =
7468 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7469 Diag(VD->getLocation(),
7470 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7471 << VD;
7472 continue;
7473 }
7474
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007475 // Build private copy of original var.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007476 auto *Private = buildVarDecl(*this, ELoc, QType, VD->getName(),
7477 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007478 auto *PrivateRef = buildDeclRefExpr(
7479 *this, Private, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman3276a272015-03-21 10:12:56 +00007480 // Build var to save initial value.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007481 VarDecl *Init = buildVarDecl(*this, ELoc, QType, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00007482 Expr *InitExpr;
7483 if (LinKind == OMPC_LINEAR_uval)
7484 InitExpr = VD->getInit();
7485 else
7486 InitExpr = DE;
7487 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexander Musman3276a272015-03-21 10:12:56 +00007488 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007489 auto InitRef = buildDeclRefExpr(
7490 *this, Init, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman8dba6642014-04-22 13:09:42 +00007491 DSAStack->addDSA(VD, DE, OMPC_linear);
7492 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007493 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00007494 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00007495 }
7496
7497 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007498 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00007499
7500 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00007501 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00007502 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
7503 !Step->isInstantiationDependent() &&
7504 !Step->containsUnexpandedParameterPack()) {
7505 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00007506 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00007507 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007508 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00007509 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00007510
Alexander Musman3276a272015-03-21 10:12:56 +00007511 // Build var to save the step value.
7512 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007513 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00007514 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007515 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00007516 ExprResult CalcStep =
7517 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007518 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00007519
Alexander Musman8dba6642014-04-22 13:09:42 +00007520 // Warn about zero linear step (it would be probably better specified as
7521 // making corresponding variables 'const').
7522 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00007523 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
7524 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00007525 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
7526 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00007527 if (!IsConstant && CalcStep.isUsable()) {
7528 // Calculate the step beforehand instead of doing this on each iteration.
7529 // (This is not used if the number of iterations may be kfold-ed).
7530 CalcStepExpr = CalcStep.get();
7531 }
Alexander Musman8dba6642014-04-22 13:09:42 +00007532 }
7533
Alexey Bataev182227b2015-08-20 10:54:39 +00007534 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
7535 ColonLoc, EndLoc, Vars, Privates, Inits,
7536 StepExpr, CalcStepExpr);
Alexander Musman3276a272015-03-21 10:12:56 +00007537}
7538
7539static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
7540 Expr *NumIterations, Sema &SemaRef,
7541 Scope *S) {
7542 // Walk the vars and build update/final expressions for the CodeGen.
7543 SmallVector<Expr *, 8> Updates;
7544 SmallVector<Expr *, 8> Finals;
7545 Expr *Step = Clause.getStep();
7546 Expr *CalcStep = Clause.getCalcStep();
7547 // OpenMP [2.14.3.7, linear clause]
7548 // If linear-step is not specified it is assumed to be 1.
7549 if (Step == nullptr)
7550 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
7551 else if (CalcStep)
7552 Step = cast<BinaryOperator>(CalcStep)->getLHS();
7553 bool HasErrors = false;
7554 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007555 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00007556 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00007557 for (auto &RefExpr : Clause.varlists()) {
7558 Expr *InitExpr = *CurInit;
7559
7560 // Build privatized reference to the current linear var.
7561 auto DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00007562 Expr *CapturedRef;
7563 if (LinKind == OMPC_LINEAR_uval)
7564 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
7565 else
7566 CapturedRef =
7567 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
7568 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
7569 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00007570
7571 // Build update: Var = InitExpr + IV * Step
7572 ExprResult Update =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007573 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
Alexander Musman3276a272015-03-21 10:12:56 +00007574 InitExpr, IV, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007575 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
7576 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00007577
7578 // Build final: Var = InitExpr + NumIterations * Step
7579 ExprResult Final =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007580 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
Alexey Bataev39f915b82015-05-08 10:41:21 +00007581 InitExpr, NumIterations, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007582 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
7583 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00007584 if (!Update.isUsable() || !Final.isUsable()) {
7585 Updates.push_back(nullptr);
7586 Finals.push_back(nullptr);
7587 HasErrors = true;
7588 } else {
7589 Updates.push_back(Update.get());
7590 Finals.push_back(Final.get());
7591 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007592 ++CurInit, ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00007593 }
7594 Clause.setUpdates(Updates);
7595 Clause.setFinals(Finals);
7596 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00007597}
7598
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007599OMPClause *Sema::ActOnOpenMPAlignedClause(
7600 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
7601 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
7602
7603 SmallVector<Expr *, 8> Vars;
7604 for (auto &RefExpr : VarList) {
7605 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
7606 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7607 // It will be analyzed later.
7608 Vars.push_back(RefExpr);
7609 continue;
7610 }
7611
7612 SourceLocation ELoc = RefExpr->getExprLoc();
7613 // OpenMP [2.1, C/C++]
7614 // A list item is a variable name.
7615 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
7616 if (!DE || !isa<VarDecl>(DE->getDecl())) {
7617 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
7618 continue;
7619 }
7620
7621 VarDecl *VD = cast<VarDecl>(DE->getDecl());
7622
7623 // OpenMP [2.8.1, simd construct, Restrictions]
7624 // The type of list items appearing in the aligned clause must be
7625 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007626 QualType QType = VD->getType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007627 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007628 const Type *Ty = QType.getTypePtrOrNull();
7629 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
7630 !Ty->isPointerType())) {
7631 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
7632 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
7633 bool IsDecl =
7634 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7635 Diag(VD->getLocation(),
7636 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7637 << VD;
7638 continue;
7639 }
7640
7641 // OpenMP [2.8.1, simd construct, Restrictions]
7642 // A list-item cannot appear in more than one aligned clause.
7643 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
7644 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
7645 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
7646 << getOpenMPClauseName(OMPC_aligned);
7647 continue;
7648 }
7649
7650 Vars.push_back(DE);
7651 }
7652
7653 // OpenMP [2.8.1, simd construct, Description]
7654 // The parameter of the aligned clause, alignment, must be a constant
7655 // positive integer expression.
7656 // If no optional parameter is specified, implementation-defined default
7657 // alignments for SIMD instructions on the target platforms are assumed.
7658 if (Alignment != nullptr) {
7659 ExprResult AlignResult =
7660 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
7661 if (AlignResult.isInvalid())
7662 return nullptr;
7663 Alignment = AlignResult.get();
7664 }
7665 if (Vars.empty())
7666 return nullptr;
7667
7668 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
7669 EndLoc, Vars, Alignment);
7670}
7671
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007672OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
7673 SourceLocation StartLoc,
7674 SourceLocation LParenLoc,
7675 SourceLocation EndLoc) {
7676 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007677 SmallVector<Expr *, 8> SrcExprs;
7678 SmallVector<Expr *, 8> DstExprs;
7679 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00007680 for (auto &RefExpr : VarList) {
7681 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
7682 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007683 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007684 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007685 SrcExprs.push_back(nullptr);
7686 DstExprs.push_back(nullptr);
7687 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007688 continue;
7689 }
7690
Alexey Bataeved09d242014-05-28 05:53:51 +00007691 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007692 // OpenMP [2.1, C/C++]
7693 // A list item is a variable name.
7694 // OpenMP [2.14.4.1, Restrictions, p.1]
7695 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00007696 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007697 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00007698 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007699 continue;
7700 }
7701
7702 Decl *D = DE->getDecl();
7703 VarDecl *VD = cast<VarDecl>(D);
7704
7705 QualType Type = VD->getType();
7706 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
7707 // It will be analyzed later.
7708 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007709 SrcExprs.push_back(nullptr);
7710 DstExprs.push_back(nullptr);
7711 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007712 continue;
7713 }
7714
7715 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
7716 // A list item that appears in a copyin clause must be threadprivate.
7717 if (!DSAStack->isThreadPrivate(VD)) {
7718 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00007719 << getOpenMPClauseName(OMPC_copyin)
7720 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007721 continue;
7722 }
7723
7724 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
7725 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00007726 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007727 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007728 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007729 auto *SrcVD =
7730 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
7731 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007732 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007733 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
7734 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007735 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
7736 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007737 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007738 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007739 // For arrays generate assignment operation for single element and replace
7740 // it by the original array element in CodeGen.
7741 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
7742 PseudoDstExpr, PseudoSrcExpr);
7743 if (AssignmentOp.isInvalid())
7744 continue;
7745 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
7746 /*DiscardedValue=*/true);
7747 if (AssignmentOp.isInvalid())
7748 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007749
7750 DSAStack->addDSA(VD, DE, OMPC_copyin);
7751 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007752 SrcExprs.push_back(PseudoSrcExpr);
7753 DstExprs.push_back(PseudoDstExpr);
7754 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007755 }
7756
Alexey Bataeved09d242014-05-28 05:53:51 +00007757 if (Vars.empty())
7758 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007759
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007760 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
7761 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007762}
7763
Alexey Bataevbae9a792014-06-27 10:37:06 +00007764OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
7765 SourceLocation StartLoc,
7766 SourceLocation LParenLoc,
7767 SourceLocation EndLoc) {
7768 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00007769 SmallVector<Expr *, 8> SrcExprs;
7770 SmallVector<Expr *, 8> DstExprs;
7771 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00007772 for (auto &RefExpr : VarList) {
7773 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
7774 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7775 // It will be analyzed later.
7776 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00007777 SrcExprs.push_back(nullptr);
7778 DstExprs.push_back(nullptr);
7779 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00007780 continue;
7781 }
7782
7783 SourceLocation ELoc = RefExpr->getExprLoc();
7784 // OpenMP [2.1, C/C++]
7785 // A list item is a variable name.
7786 // OpenMP [2.14.4.1, Restrictions, p.1]
7787 // A list item that appears in a copyin clause must be threadprivate.
7788 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
7789 if (!DE || !isa<VarDecl>(DE->getDecl())) {
7790 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
7791 continue;
7792 }
7793
7794 Decl *D = DE->getDecl();
7795 VarDecl *VD = cast<VarDecl>(D);
7796
7797 QualType Type = VD->getType();
7798 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
7799 // It will be analyzed later.
7800 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00007801 SrcExprs.push_back(nullptr);
7802 DstExprs.push_back(nullptr);
7803 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00007804 continue;
7805 }
7806
7807 // OpenMP [2.14.4.2, Restrictions, p.2]
7808 // A list item that appears in a copyprivate clause may not appear in a
7809 // private or firstprivate clause on the single construct.
7810 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007811 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00007812 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
7813 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00007814 Diag(ELoc, diag::err_omp_wrong_dsa)
7815 << getOpenMPClauseName(DVar.CKind)
7816 << getOpenMPClauseName(OMPC_copyprivate);
7817 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7818 continue;
7819 }
7820
7821 // OpenMP [2.11.4.2, Restrictions, p.1]
7822 // All list items that appear in a copyprivate clause must be either
7823 // threadprivate or private in the enclosing context.
7824 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007825 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00007826 if (DVar.CKind == OMPC_shared) {
7827 Diag(ELoc, diag::err_omp_required_access)
7828 << getOpenMPClauseName(OMPC_copyprivate)
7829 << "threadprivate or private in the enclosing context";
7830 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7831 continue;
7832 }
7833 }
7834 }
7835
Alexey Bataev7a3e5852015-05-19 08:19:24 +00007836 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007837 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00007838 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007839 << getOpenMPClauseName(OMPC_copyprivate) << Type
7840 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00007841 bool IsDecl =
7842 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7843 Diag(VD->getLocation(),
7844 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7845 << VD;
7846 continue;
7847 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007848
Alexey Bataevbae9a792014-06-27 10:37:06 +00007849 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
7850 // A variable of class type (or array thereof) that appears in a
7851 // copyin clause requires an accessible, unambiguous copy assignment
7852 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007853 Type = Context.getBaseElementType(Type.getNonReferenceType())
7854 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +00007855 auto *SrcVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007856 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.src",
7857 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00007858 auto *PseudoSrcExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007859 buildDeclRefExpr(*this, SrcVD, Type, DE->getExprLoc());
Alexey Bataev420d45b2015-04-14 05:11:24 +00007860 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007861 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.dst",
7862 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00007863 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007864 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataeva63048e2015-03-23 06:18:07 +00007865 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
7866 PseudoDstExpr, PseudoSrcExpr);
7867 if (AssignmentOp.isInvalid())
7868 continue;
7869 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
7870 /*DiscardedValue=*/true);
7871 if (AssignmentOp.isInvalid())
7872 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00007873
7874 // No need to mark vars as copyprivate, they are already threadprivate or
7875 // implicitly private.
7876 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00007877 SrcExprs.push_back(PseudoSrcExpr);
7878 DstExprs.push_back(PseudoDstExpr);
7879 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +00007880 }
7881
7882 if (Vars.empty())
7883 return nullptr;
7884
Alexey Bataeva63048e2015-03-23 06:18:07 +00007885 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
7886 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00007887}
7888
Alexey Bataev6125da92014-07-21 11:26:11 +00007889OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
7890 SourceLocation StartLoc,
7891 SourceLocation LParenLoc,
7892 SourceLocation EndLoc) {
7893 if (VarList.empty())
7894 return nullptr;
7895
7896 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
7897}
Alexey Bataevdea47612014-07-23 07:46:59 +00007898
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007899OMPClause *
7900Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
7901 SourceLocation DepLoc, SourceLocation ColonLoc,
7902 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
7903 SourceLocation LParenLoc, SourceLocation EndLoc) {
7904 if (DepKind == OMPC_DEPEND_unknown) {
7905 std::string Values;
7906 std::string Sep(", ");
7907 for (unsigned i = 0; i < OMPC_DEPEND_unknown; ++i) {
7908 Values += "'";
7909 Values += getOpenMPSimpleClauseTypeName(OMPC_depend, i);
7910 Values += "'";
7911 switch (i) {
7912 case OMPC_DEPEND_unknown - 2:
7913 Values += " or ";
7914 break;
7915 case OMPC_DEPEND_unknown - 1:
7916 break;
7917 default:
7918 Values += Sep;
7919 break;
7920 }
7921 }
7922 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
7923 << Values << getOpenMPClauseName(OMPC_depend);
7924 return nullptr;
7925 }
7926 SmallVector<Expr *, 8> Vars;
7927 for (auto &RefExpr : VarList) {
7928 assert(RefExpr && "NULL expr in OpenMP shared clause.");
7929 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7930 // It will be analyzed later.
7931 Vars.push_back(RefExpr);
7932 continue;
7933 }
7934
7935 SourceLocation ELoc = RefExpr->getExprLoc();
7936 // OpenMP [2.11.1.1, Restrictions, p.3]
7937 // A variable that is part of another variable (such as a field of a
7938 // structure) but is not an array element or an array section cannot appear
7939 // in a depend clause.
7940 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
Alexey Bataev1a3320e2015-08-25 14:24:04 +00007941 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
7942 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
7943 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
7944 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
7945 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007946 (ASE && !ASE->getBase()->getType()->isAnyPointerType() &&
7947 !ASE->getBase()->getType()->isArrayType())) {
7948 Diag(ELoc, diag::err_omp_expected_var_name_or_array_item)
7949 << RefExpr->getSourceRange();
7950 continue;
7951 }
7952
7953 Vars.push_back(RefExpr->IgnoreParenImpCasts());
7954 }
7955
7956 if (Vars.empty())
7957 return nullptr;
7958
7959 return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
7960 DepLoc, ColonLoc, Vars);
7961}
Michael Wonge710d542015-08-07 16:16:36 +00007962
7963OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
7964 SourceLocation LParenLoc,
7965 SourceLocation EndLoc) {
7966 Expr *ValExpr = Device;
Michael Wonge710d542015-08-07 16:16:36 +00007967
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007968 // OpenMP [2.9.1, Restrictions]
7969 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00007970 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
7971 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007972 return nullptr;
7973
Michael Wonge710d542015-08-07 16:16:36 +00007974 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
7975}
Kelvin Li0bff7af2015-11-23 05:32:03 +00007976
7977static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
7978 DSAStackTy *Stack, CXXRecordDecl *RD) {
7979 if (!RD || RD->isInvalidDecl())
7980 return true;
7981
7982 auto QTy = SemaRef.Context.getRecordType(RD);
7983 if (RD->isDynamicClass()) {
7984 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
7985 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
7986 return false;
7987 }
7988 auto *DC = RD;
7989 bool IsCorrect = true;
7990 for (auto *I : DC->decls()) {
7991 if (I) {
7992 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
7993 if (MD->isStatic()) {
7994 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
7995 SemaRef.Diag(MD->getLocation(),
7996 diag::note_omp_static_member_in_target);
7997 IsCorrect = false;
7998 }
7999 } else if (auto *VD = dyn_cast<VarDecl>(I)) {
8000 if (VD->isStaticDataMember()) {
8001 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8002 SemaRef.Diag(VD->getLocation(),
8003 diag::note_omp_static_member_in_target);
8004 IsCorrect = false;
8005 }
8006 }
8007 }
8008 }
8009
8010 for (auto &I : RD->bases()) {
8011 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
8012 I.getType()->getAsCXXRecordDecl()))
8013 IsCorrect = false;
8014 }
8015 return IsCorrect;
8016}
8017
8018static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
8019 DSAStackTy *Stack, QualType QTy) {
8020 NamedDecl *ND;
8021 if (QTy->isIncompleteType(&ND)) {
8022 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
8023 return false;
8024 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
8025 if (!RD->isInvalidDecl() &&
8026 !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
8027 return false;
8028 }
8029 return true;
8030}
8031
8032OMPClause *Sema::ActOnOpenMPMapClause(
8033 OpenMPMapClauseKind MapTypeModifier, OpenMPMapClauseKind MapType,
8034 SourceLocation MapLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VarList,
8035 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
8036 SmallVector<Expr *, 4> Vars;
8037
8038 for (auto &RE : VarList) {
8039 assert(RE && "Null expr in omp map");
8040 if (isa<DependentScopeDeclRefExpr>(RE)) {
8041 // It will be analyzed later.
8042 Vars.push_back(RE);
8043 continue;
8044 }
8045 SourceLocation ELoc = RE->getExprLoc();
8046
8047 // OpenMP [2.14.5, Restrictions]
8048 // A variable that is part of another variable (such as field of a
8049 // structure) but is not an array element or an array section cannot appear
8050 // in a map clause.
8051 auto *VE = RE->IgnoreParenLValueCasts();
8052
8053 if (VE->isValueDependent() || VE->isTypeDependent() ||
8054 VE->isInstantiationDependent() ||
8055 VE->containsUnexpandedParameterPack()) {
8056 // It will be analyzed later.
8057 Vars.push_back(RE);
8058 continue;
8059 }
8060
8061 auto *SimpleExpr = RE->IgnoreParenCasts();
8062 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
8063 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
8064 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
8065
8066 if (!RE->IgnoreParenImpCasts()->isLValue() ||
8067 (!OASE && !ASE && !DE) ||
8068 (DE && !isa<VarDecl>(DE->getDecl())) ||
8069 (ASE && !ASE->getBase()->getType()->isAnyPointerType() &&
8070 !ASE->getBase()->getType()->isArrayType())) {
8071 Diag(ELoc, diag::err_omp_expected_var_name_or_array_item)
8072 << RE->getSourceRange();
8073 continue;
8074 }
8075
8076 Decl *D = nullptr;
8077 if (DE) {
8078 D = DE->getDecl();
8079 } else if (ASE) {
8080 auto *B = ASE->getBase()->IgnoreParenCasts();
8081 D = dyn_cast<DeclRefExpr>(B)->getDecl();
8082 } else if (OASE) {
8083 auto *B = OASE->getBase();
8084 D = dyn_cast<DeclRefExpr>(B)->getDecl();
8085 }
8086 assert(D && "Null decl on map clause.");
8087 auto *VD = cast<VarDecl>(D);
8088
8089 // OpenMP [2.14.5, Restrictions, p.8]
8090 // threadprivate variables cannot appear in a map clause.
8091 if (DSAStack->isThreadPrivate(VD)) {
8092 auto DVar = DSAStack->getTopDSA(VD, false);
8093 Diag(ELoc, diag::err_omp_threadprivate_in_map);
8094 ReportOriginalDSA(*this, DSAStack, VD, DVar);
8095 continue;
8096 }
8097
8098 // OpenMP [2.14.5, Restrictions, p.2]
8099 // At most one list item can be an array item derived from a given variable
8100 // in map clauses of the same construct.
8101 // OpenMP [2.14.5, Restrictions, p.3]
8102 // List items of map clauses in the same construct must not share original
8103 // storage.
8104 // OpenMP [2.14.5, Restrictions, C/C++, p.2]
8105 // A variable for which the type is pointer, reference to array, or
8106 // reference to pointer and an array section derived from that variable
8107 // must not appear as list items of map clauses of the same construct.
8108 DSAStackTy::MapInfo MI = DSAStack->IsMappedInCurrentRegion(VD);
8109 if (MI.RefExpr) {
8110 Diag(ELoc, diag::err_omp_map_shared_storage) << ELoc;
8111 Diag(MI.RefExpr->getExprLoc(), diag::note_used_here)
8112 << MI.RefExpr->getSourceRange();
8113 continue;
8114 }
8115
8116 // OpenMP [2.14.5, Restrictions, C/C++, p.3,4]
8117 // A variable for which the type is pointer, reference to array, or
8118 // reference to pointer must not appear as a list item if the enclosing
8119 // device data environment already contains an array section derived from
8120 // that variable.
8121 // An array section derived from a variable for which the type is pointer,
8122 // reference to array, or reference to pointer must not appear as a list
8123 // item if the enclosing device data environment already contains that
8124 // variable.
8125 QualType Type = VD->getType();
8126 MI = DSAStack->getMapInfoForVar(VD);
8127 if (MI.RefExpr && (isa<DeclRefExpr>(MI.RefExpr->IgnoreParenLValueCasts()) !=
8128 isa<DeclRefExpr>(VE)) &&
8129 (Type->isPointerType() || Type->isReferenceType())) {
8130 Diag(ELoc, diag::err_omp_map_shared_storage) << ELoc;
8131 Diag(MI.RefExpr->getExprLoc(), diag::note_used_here)
8132 << MI.RefExpr->getSourceRange();
8133 continue;
8134 }
8135
8136 // OpenMP [2.14.5, Restrictions, C/C++, p.7]
8137 // A list item must have a mappable type.
8138 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), *this,
8139 DSAStack, Type))
8140 continue;
8141
8142 Vars.push_back(RE);
8143 MI.RefExpr = RE;
8144 DSAStack->addMapInfoForVar(VD, MI);
8145 }
8146 if (Vars.empty())
8147 return nullptr;
8148
8149 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
8150 MapTypeModifier, MapType, MapLoc);
8151}
Kelvin Li099bb8c2015-11-24 20:50:12 +00008152
8153OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
8154 SourceLocation StartLoc,
8155 SourceLocation LParenLoc,
8156 SourceLocation EndLoc) {
8157 Expr *ValExpr = NumTeams;
Kelvin Li099bb8c2015-11-24 20:50:12 +00008158
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008159 // OpenMP [teams Constrcut, Restrictions]
8160 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00008161 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
8162 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008163 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +00008164
8165 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8166}
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008167
8168OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
8169 SourceLocation StartLoc,
8170 SourceLocation LParenLoc,
8171 SourceLocation EndLoc) {
8172 Expr *ValExpr = ThreadLimit;
8173
8174 // OpenMP [teams Constrcut, Restrictions]
8175 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00008176 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
8177 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008178 return nullptr;
8179
8180 return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc,
8181 EndLoc);
8182}
Alexey Bataeva0569352015-12-01 10:17:31 +00008183
8184OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
8185 SourceLocation StartLoc,
8186 SourceLocation LParenLoc,
8187 SourceLocation EndLoc) {
8188 Expr *ValExpr = Priority;
8189
8190 // OpenMP [2.9.1, task Constrcut]
8191 // The priority-value is a non-negative numerical scalar expression.
8192 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
8193 /*StrictlyPositive=*/false))
8194 return nullptr;
8195
8196 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8197}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008198
8199OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
8200 SourceLocation StartLoc,
8201 SourceLocation LParenLoc,
8202 SourceLocation EndLoc) {
8203 Expr *ValExpr = Grainsize;
8204
8205 // OpenMP [2.9.2, taskloop Constrcut]
8206 // The parameter of the grainsize clause must be a positive integer
8207 // expression.
8208 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
8209 /*StrictlyPositive=*/true))
8210 return nullptr;
8211
8212 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8213}
Alexey Bataev382967a2015-12-08 12:06:20 +00008214
8215OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
8216 SourceLocation StartLoc,
8217 SourceLocation LParenLoc,
8218 SourceLocation EndLoc) {
8219 Expr *ValExpr = NumTasks;
8220
8221 // OpenMP [2.9.2, taskloop Constrcut]
8222 // The parameter of the num_tasks clause must be a positive integer
8223 // expression.
8224 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
8225 /*StrictlyPositive=*/true))
8226 return nullptr;
8227
8228 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8229}
8230