blob: 20c3c73c94ad5ea0f5dd91050b155bb626db7793 [file] [log] [blame]
Alexey Bataev9959db52014-05-06 10:08:46 +00001//===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===//
Alexey Bataeva769e072013-03-22 06:34:35 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10/// \brief This file implements semantic analysis for OpenMP directives and
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000011/// clauses.
Alexey Bataeva769e072013-03-22 06:34:35 +000012///
13//===----------------------------------------------------------------------===//
14
Alexey Bataevb08f89f2015-08-14 12:25:37 +000015#include "TreeTransform.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000016#include "clang/AST/ASTContext.h"
Alexey Bataev97720002014-11-11 04:05:39 +000017#include "clang/AST/ASTMutationListener.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000018#include "clang/AST/Decl.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000019#include "clang/AST/DeclCXX.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000020#include "clang/AST/DeclOpenMP.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000021#include "clang/AST/StmtCXX.h"
22#include "clang/AST/StmtOpenMP.h"
23#include "clang/AST/StmtVisitor.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000024#include "clang/Basic/OpenMPKinds.h"
Samuel Antaof8b50122015-07-13 22:54:53 +000025#include "clang/Basic/TargetInfo.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000026#include "clang/Lex/Preprocessor.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000027#include "clang/Sema/Initialization.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000028#include "clang/Sema/Lookup.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000029#include "clang/Sema/Scope.h"
30#include "clang/Sema/ScopeInfo.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000031#include "clang/Sema/SemaInternal.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000032using namespace clang;
33
Alexey Bataev758e55e2013-09-06 18:03:48 +000034//===----------------------------------------------------------------------===//
35// Stack of data-sharing attributes for variables
36//===----------------------------------------------------------------------===//
37
38namespace {
39/// \brief Default data sharing attributes, which can be applied to directive.
40enum DefaultDataSharingAttributes {
Alexey Bataeved09d242014-05-28 05:53:51 +000041 DSA_unspecified = 0, /// \brief Data sharing attribute not specified.
42 DSA_none = 1 << 0, /// \brief Default data sharing attribute 'none'.
43 DSA_shared = 1 << 1 /// \brief Default data sharing attribute 'shared'.
Alexey Bataev758e55e2013-09-06 18:03:48 +000044};
Alexey Bataev7ff55242014-06-19 09:13:45 +000045
Alexey Bataevf29276e2014-06-18 04:14:57 +000046template <class T> struct MatchesAny {
Alexey Bataev23b69422014-06-18 07:08:49 +000047 explicit MatchesAny(ArrayRef<T> Arr) : Arr(std::move(Arr)) {}
Alexey Bataevf29276e2014-06-18 04:14:57 +000048 bool operator()(T Kind) {
49 for (auto KindEl : Arr)
50 if (KindEl == Kind)
51 return true;
52 return false;
53 }
54
55private:
56 ArrayRef<T> Arr;
57};
Alexey Bataev23b69422014-06-18 07:08:49 +000058struct MatchesAlways {
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +000059 MatchesAlways() {}
Alexey Bataev7ff55242014-06-19 09:13:45 +000060 template <class T> bool operator()(T) { return true; }
Alexey Bataevf29276e2014-06-18 04:14:57 +000061};
62
63typedef MatchesAny<OpenMPClauseKind> MatchesAnyClause;
64typedef MatchesAny<OpenMPDirectiveKind> MatchesAnyDirective;
Alexey Bataev758e55e2013-09-06 18:03:48 +000065
66/// \brief Stack for tracking declarations used in OpenMP directives and
67/// clauses and their data-sharing attributes.
68class DSAStackTy {
69public:
70 struct DSAVarData {
71 OpenMPDirectiveKind DKind;
72 OpenMPClauseKind CKind;
73 DeclRefExpr *RefExpr;
Alexey Bataevbae9a792014-06-27 10:37:06 +000074 SourceLocation ImplicitDSALoc;
75 DSAVarData()
76 : DKind(OMPD_unknown), CKind(OMPC_unknown), RefExpr(nullptr),
77 ImplicitDSALoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000078 };
Alexey Bataeved09d242014-05-28 05:53:51 +000079
Kelvin Li0bff7af2015-11-23 05:32:03 +000080public:
81 struct MapInfo {
82 Expr *RefExpr;
83 };
84
Alexey Bataev758e55e2013-09-06 18:03:48 +000085private:
86 struct DSAInfo {
87 OpenMPClauseKind Attributes;
88 DeclRefExpr *RefExpr;
89 };
90 typedef llvm::SmallDenseMap<VarDecl *, DSAInfo, 64> DeclSAMapTy;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000091 typedef llvm::SmallDenseMap<VarDecl *, DeclRefExpr *, 64> AlignedMapTy;
Alexey Bataev9c821032015-04-30 04:23:23 +000092 typedef llvm::DenseSet<VarDecl *> LoopControlVariablesSetTy;
Kelvin Li0bff7af2015-11-23 05:32:03 +000093 typedef llvm::SmallDenseMap<VarDecl *, MapInfo, 64> MappedDeclsTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +000094
95 struct SharingMapTy {
96 DeclSAMapTy SharingMap;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000097 AlignedMapTy AlignedMap;
Kelvin Li0bff7af2015-11-23 05:32:03 +000098 MappedDeclsTy MappedDecls;
Alexey Bataev9c821032015-04-30 04:23:23 +000099 LoopControlVariablesSetTy LCVSet;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000100 DefaultDataSharingAttributes DefaultAttr;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000101 SourceLocation DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000102 OpenMPDirectiveKind Directive;
103 DeclarationNameInfo DirectiveName;
104 Scope *CurScope;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000105 SourceLocation ConstructLoc;
Alexey Bataev346265e2015-09-25 10:37:12 +0000106 /// \brief first argument (Expr *) contains optional argument of the
107 /// 'ordered' clause, the second one is true if the regions has 'ordered'
108 /// clause, false otherwise.
109 llvm::PointerIntPair<Expr *, 1, bool> OrderedRegion;
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000110 bool NowaitRegion;
Alexey Bataev25e5b442015-09-15 12:52:43 +0000111 bool CancelRegion;
Alexey Bataev9c821032015-04-30 04:23:23 +0000112 unsigned CollapseNumber;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000113 SourceLocation InnerTeamsRegionLoc;
Alexey Bataeved09d242014-05-28 05:53:51 +0000114 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000115 Scope *CurScope, SourceLocation Loc)
Alexey Bataev9c821032015-04-30 04:23:23 +0000116 : SharingMap(), AlignedMap(), LCVSet(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000117 Directive(DKind), DirectiveName(std::move(Name)), CurScope(CurScope),
Alexey Bataev346265e2015-09-25 10:37:12 +0000118 ConstructLoc(Loc), OrderedRegion(), NowaitRegion(false),
Alexey Bataev25e5b442015-09-15 12:52:43 +0000119 CancelRegion(false), CollapseNumber(1), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000120 SharingMapTy()
Alexey Bataev9c821032015-04-30 04:23:23 +0000121 : SharingMap(), AlignedMap(), LCVSet(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000122 Directive(OMPD_unknown), DirectiveName(), CurScope(nullptr),
Alexey Bataev346265e2015-09-25 10:37:12 +0000123 ConstructLoc(), OrderedRegion(), NowaitRegion(false),
Alexey Bataev25e5b442015-09-15 12:52:43 +0000124 CancelRegion(false), CollapseNumber(1), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000125 };
126
127 typedef SmallVector<SharingMapTy, 64> StackTy;
128
129 /// \brief Stack of used declaration and their data-sharing attributes.
130 StackTy Stack;
Alexey Bataev39f915b82015-05-08 10:41:21 +0000131 /// \brief true, if check for DSA must be from parent directive, false, if
132 /// from current directive.
Alexey Bataevaac108a2015-06-23 04:51:00 +0000133 OpenMPClauseKind ClauseKindMode;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000134 Sema &SemaRef;
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000135 bool ForceCapturing;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000136
137 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
138
139 DSAVarData getDSA(StackTy::reverse_iterator Iter, VarDecl *D);
Alexey Bataevec3da872014-01-31 05:15:34 +0000140
141 /// \brief Checks if the variable is a local for OpenMP region.
142 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
Alexey Bataeved09d242014-05-28 05:53:51 +0000143
Alexey Bataev758e55e2013-09-06 18:03:48 +0000144public:
Alexey Bataevaac108a2015-06-23 04:51:00 +0000145 explicit DSAStackTy(Sema &S)
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000146 : Stack(1), ClauseKindMode(OMPC_unknown), SemaRef(S),
147 ForceCapturing(false) {}
Alexey Bataev39f915b82015-05-08 10:41:21 +0000148
Alexey Bataevaac108a2015-06-23 04:51:00 +0000149 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
150 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000151
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000152 bool isForceVarCapturing() const { return ForceCapturing; }
153 void setForceVarCapturing(bool V) { ForceCapturing = V; }
154
Alexey Bataev758e55e2013-09-06 18:03:48 +0000155 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000156 Scope *CurScope, SourceLocation Loc) {
157 Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc));
158 Stack.back().DefaultAttrLoc = Loc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000159 }
160
161 void pop() {
162 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
163 Stack.pop_back();
164 }
165
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000166 /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000167 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000168 /// for diagnostics.
169 DeclRefExpr *addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE);
170
Alexey Bataev9c821032015-04-30 04:23:23 +0000171 /// \brief Register specified variable as loop control variable.
172 void addLoopControlVariable(VarDecl *D);
173 /// \brief Check if the specified variable is a loop control variable for
174 /// current region.
175 bool isLoopControlVariable(VarDecl *D);
176
Alexey Bataev758e55e2013-09-06 18:03:48 +0000177 /// \brief Adds explicit data sharing attribute to the specified declaration.
178 void addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A);
179
Alexey Bataev758e55e2013-09-06 18:03:48 +0000180 /// \brief Returns data sharing attributes from top of the stack for the
181 /// specified declaration.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000182 DSAVarData getTopDSA(VarDecl *D, bool FromParent);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000183 /// \brief Returns data-sharing attributes for the specified declaration.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000184 DSAVarData getImplicitDSA(VarDecl *D, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000185 /// \brief Checks if the specified variables has data-sharing attributes which
186 /// match specified \a CPred predicate in any directive which matches \a DPred
187 /// predicate.
188 template <class ClausesPredicate, class DirectivesPredicate>
189 DSAVarData hasDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000190 DirectivesPredicate DPred, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000191 /// \brief Checks if the specified variables has data-sharing attributes which
192 /// match specified \a CPred predicate in any innermost directive which
193 /// matches \a DPred predicate.
194 template <class ClausesPredicate, class DirectivesPredicate>
195 DSAVarData hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000196 DirectivesPredicate DPred,
197 bool FromParent);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000198 /// \brief Checks if the specified variables has explicit data-sharing
199 /// attributes which match specified \a CPred predicate at the specified
200 /// OpenMP region.
201 bool hasExplicitDSA(VarDecl *D,
202 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
203 unsigned Level);
Samuel Antao4be30e92015-10-02 17:14:03 +0000204
205 /// \brief Returns true if the directive at level \Level matches in the
206 /// specified \a DPred predicate.
207 bool hasExplicitDirective(
208 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
209 unsigned Level);
210
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000211 /// \brief Finds a directive which matches specified \a DPred predicate.
212 template <class NamedDirectivesPredicate>
213 bool hasDirective(NamedDirectivesPredicate DPred, bool FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000214
Alexey Bataev758e55e2013-09-06 18:03:48 +0000215 /// \brief Returns currently analyzed directive.
216 OpenMPDirectiveKind getCurrentDirective() const {
217 return Stack.back().Directive;
218 }
Alexey Bataev549210e2014-06-24 04:39:47 +0000219 /// \brief Returns parent directive.
220 OpenMPDirectiveKind getParentDirective() const {
221 if (Stack.size() > 2)
222 return Stack[Stack.size() - 2].Directive;
223 return OMPD_unknown;
224 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000225 /// \brief Return the directive associated with the provided scope.
226 OpenMPDirectiveKind getDirectiveForScope(const Scope *S) const;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000227
228 /// \brief Set default data sharing attribute to none.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000229 void setDefaultDSANone(SourceLocation Loc) {
230 Stack.back().DefaultAttr = DSA_none;
231 Stack.back().DefaultAttrLoc = Loc;
232 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000233 /// \brief Set default data sharing attribute to shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000234 void setDefaultDSAShared(SourceLocation Loc) {
235 Stack.back().DefaultAttr = DSA_shared;
236 Stack.back().DefaultAttrLoc = Loc;
237 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000238
239 DefaultDataSharingAttributes getDefaultDSA() const {
240 return Stack.back().DefaultAttr;
241 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000242 SourceLocation getDefaultDSALocation() const {
243 return Stack.back().DefaultAttrLoc;
244 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000245
Alexey Bataevf29276e2014-06-18 04:14:57 +0000246 /// \brief Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000247 bool isThreadPrivate(VarDecl *D) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000248 DSAVarData DVar = getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000249 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000250 }
251
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000252 /// \brief Marks current region as ordered (it has an 'ordered' clause).
Alexey Bataev346265e2015-09-25 10:37:12 +0000253 void setOrderedRegion(bool IsOrdered, Expr *Param) {
254 Stack.back().OrderedRegion.setInt(IsOrdered);
255 Stack.back().OrderedRegion.setPointer(Param);
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000256 }
257 /// \brief Returns true, if parent region is ordered (has associated
258 /// 'ordered' clause), false - otherwise.
259 bool isParentOrderedRegion() const {
260 if (Stack.size() > 2)
Alexey Bataev346265e2015-09-25 10:37:12 +0000261 return Stack[Stack.size() - 2].OrderedRegion.getInt();
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000262 return false;
263 }
Alexey Bataev346265e2015-09-25 10:37:12 +0000264 /// \brief Returns optional parameter for the ordered region.
265 Expr *getParentOrderedRegionParam() const {
266 if (Stack.size() > 2)
267 return Stack[Stack.size() - 2].OrderedRegion.getPointer();
268 return nullptr;
269 }
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000270 /// \brief Marks current region as nowait (it has a 'nowait' clause).
271 void setNowaitRegion(bool IsNowait = true) {
272 Stack.back().NowaitRegion = IsNowait;
273 }
274 /// \brief Returns true, if parent region is nowait (has associated
275 /// 'nowait' clause), false - otherwise.
276 bool isParentNowaitRegion() const {
277 if (Stack.size() > 2)
278 return Stack[Stack.size() - 2].NowaitRegion;
279 return false;
280 }
Alexey Bataev25e5b442015-09-15 12:52:43 +0000281 /// \brief Marks parent region as cancel region.
282 void setParentCancelRegion(bool Cancel = true) {
283 if (Stack.size() > 2)
284 Stack[Stack.size() - 2].CancelRegion =
285 Stack[Stack.size() - 2].CancelRegion || Cancel;
286 }
287 /// \brief Return true if current region has inner cancel construct.
288 bool isCancelRegion() const {
289 return Stack.back().CancelRegion;
290 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000291
Alexey Bataev9c821032015-04-30 04:23:23 +0000292 /// \brief Set collapse value for the region.
293 void setCollapseNumber(unsigned Val) { Stack.back().CollapseNumber = Val; }
294 /// \brief Return collapse value for region.
295 unsigned getCollapseNumber() const {
296 return Stack.back().CollapseNumber;
297 }
298
Alexey Bataev13314bf2014-10-09 04:18:56 +0000299 /// \brief Marks current target region as one with closely nested teams
300 /// region.
301 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
302 if (Stack.size() > 2)
303 Stack[Stack.size() - 2].InnerTeamsRegionLoc = TeamsRegionLoc;
304 }
305 /// \brief Returns true, if current region has closely nested teams region.
306 bool hasInnerTeamsRegion() const {
307 return getInnerTeamsRegionLoc().isValid();
308 }
309 /// \brief Returns location of the nested teams region (if any).
310 SourceLocation getInnerTeamsRegionLoc() const {
311 if (Stack.size() > 1)
312 return Stack.back().InnerTeamsRegionLoc;
313 return SourceLocation();
314 }
315
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000316 Scope *getCurScope() const { return Stack.back().CurScope; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000317 Scope *getCurScope() { return Stack.back().CurScope; }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000318 SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000319
320 MapInfo getMapInfoForVar(VarDecl *VD) {
321 MapInfo VarMI = {0};
322 for (auto Cnt = Stack.size() - 1; Cnt > 0; --Cnt) {
323 if (Stack[Cnt].MappedDecls.count(VD)) {
324 VarMI = Stack[Cnt].MappedDecls[VD];
325 break;
326 }
327 }
328 return VarMI;
329 }
330
331 void addMapInfoForVar(VarDecl *VD, MapInfo MI) {
332 if (Stack.size() > 1) {
333 Stack.back().MappedDecls[VD] = MI;
334 }
335 }
336
337 MapInfo IsMappedInCurrentRegion(VarDecl *VD) {
338 assert(Stack.size() > 1 && "Target level is 0");
339 MapInfo VarMI = {0};
340 if (Stack.size() > 1 && Stack.back().MappedDecls.count(VD)) {
341 VarMI = Stack.back().MappedDecls[VD];
342 }
343 return VarMI;
344 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000345};
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000346bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
347 return isOpenMPParallelDirective(DKind) || DKind == OMPD_task ||
Alexey Bataev49f6e782015-12-01 04:18:41 +0000348 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000349 isOpenMPTaskLoopDirective(DKind);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000350}
Alexey Bataeved09d242014-05-28 05:53:51 +0000351} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000352
353DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter,
354 VarDecl *D) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000355 D = D->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000356 DSAVarData DVar;
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000357 if (Iter == std::prev(Stack.rend())) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000358 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
359 // in a region but not in construct]
360 // File-scope or namespace-scope variables referenced in called routines
361 // in the region are shared unless they appear in a threadprivate
362 // directive.
Alexey Bataev8b9cb982014-07-24 02:33:58 +0000363 if (!D->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D))
Alexey Bataev750a58b2014-03-18 12:19:12 +0000364 DVar.CKind = OMPC_shared;
365
366 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
367 // in a region but not in construct]
368 // Variables with static storage duration that are declared in called
369 // routines in the region are shared.
370 if (D->hasGlobalStorage())
371 DVar.CKind = OMPC_shared;
372
Alexey Bataev758e55e2013-09-06 18:03:48 +0000373 return DVar;
374 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000375
Alexey Bataev758e55e2013-09-06 18:03:48 +0000376 DVar.DKind = Iter->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +0000377 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
378 // in a Construct, C/C++, predetermined, p.1]
379 // Variables with automatic storage duration that are declared in a scope
380 // inside the construct are private.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000381 if (isOpenMPLocal(D, Iter) && D->isLocalVarDecl() &&
382 (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) {
383 DVar.CKind = OMPC_private;
384 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000385 }
386
Alexey Bataev758e55e2013-09-06 18:03:48 +0000387 // Explicitly specified attributes and local variables with predetermined
388 // attributes.
389 if (Iter->SharingMap.count(D)) {
390 DVar.RefExpr = Iter->SharingMap[D].RefExpr;
391 DVar.CKind = Iter->SharingMap[D].Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000392 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000393 return DVar;
394 }
395
396 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
397 // in a Construct, C/C++, implicitly determined, p.1]
398 // In a parallel or task construct, the data-sharing attributes of these
399 // variables are determined by the default clause, if present.
400 switch (Iter->DefaultAttr) {
401 case DSA_shared:
402 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000403 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000404 return DVar;
405 case DSA_none:
406 return DVar;
407 case DSA_unspecified:
408 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
409 // in a Construct, implicitly determined, p.2]
410 // In a parallel construct, if no default clause is present, these
411 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000412 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000413 if (isOpenMPParallelDirective(DVar.DKind) ||
414 isOpenMPTeamsDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000415 DVar.CKind = OMPC_shared;
416 return DVar;
417 }
418
419 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
420 // in a Construct, implicitly determined, p.4]
421 // In a task construct, if no default clause is present, a variable that in
422 // the enclosing context is determined to be shared by all implicit tasks
423 // bound to the current team is shared.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000424 if (DVar.DKind == OMPD_task) {
425 DSAVarData DVarTemp;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000426 for (StackTy::reverse_iterator I = std::next(Iter), EE = Stack.rend();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000427 I != EE; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000428 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
429 // Referenced
Alexey Bataev758e55e2013-09-06 18:03:48 +0000430 // in a Construct, implicitly determined, p.6]
431 // In a task construct, if no default clause is present, a variable
432 // whose data-sharing attribute is not determined by the rules above is
433 // firstprivate.
434 DVarTemp = getDSA(I, D);
435 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000436 DVar.RefExpr = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000437 DVar.DKind = OMPD_task;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000438 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000439 return DVar;
440 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000441 if (isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000442 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000443 }
444 DVar.DKind = OMPD_task;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000445 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000446 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000447 return DVar;
448 }
449 }
450 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
451 // in a Construct, implicitly determined, p.3]
452 // For constructs other than task, if no default clause is present, these
453 // variables inherit their data-sharing attributes from the enclosing
454 // context.
Benjamin Kramer167e9992014-03-02 12:20:24 +0000455 return getDSA(std::next(Iter), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000456}
457
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000458DeclRefExpr *DSAStackTy::addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE) {
459 assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000460 D = D->getCanonicalDecl();
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000461 auto It = Stack.back().AlignedMap.find(D);
462 if (It == Stack.back().AlignedMap.end()) {
463 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
464 Stack.back().AlignedMap[D] = NewDE;
465 return nullptr;
466 } else {
467 assert(It->second && "Unexpected nullptr expr in the aligned map");
468 return It->second;
469 }
470 return nullptr;
471}
472
Alexey Bataev9c821032015-04-30 04:23:23 +0000473void DSAStackTy::addLoopControlVariable(VarDecl *D) {
474 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
475 D = D->getCanonicalDecl();
476 Stack.back().LCVSet.insert(D);
477}
478
479bool DSAStackTy::isLoopControlVariable(VarDecl *D) {
480 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
481 D = D->getCanonicalDecl();
482 return Stack.back().LCVSet.count(D) > 0;
483}
484
Alexey Bataev758e55e2013-09-06 18:03:48 +0000485void DSAStackTy::addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000486 D = D->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000487 if (A == OMPC_threadprivate) {
488 Stack[0].SharingMap[D].Attributes = A;
489 Stack[0].SharingMap[D].RefExpr = E;
490 } else {
491 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
492 Stack.back().SharingMap[D].Attributes = A;
493 Stack.back().SharingMap[D].RefExpr = E;
494 }
495}
496
Alexey Bataeved09d242014-05-28 05:53:51 +0000497bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000498 D = D->getCanonicalDecl();
Alexey Bataevec3da872014-01-31 05:15:34 +0000499 if (Stack.size() > 2) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000500 reverse_iterator I = Iter, E = std::prev(Stack.rend());
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000501 Scope *TopScope = nullptr;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000502 while (I != E && !isParallelOrTaskRegion(I->Directive)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000503 ++I;
504 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000505 if (I == E)
506 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000507 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000508 Scope *CurScope = getCurScope();
509 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000510 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000511 }
512 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000513 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000514 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000515}
516
Alexey Bataev39f915b82015-05-08 10:41:21 +0000517/// \brief Build a variable declaration for OpenMP loop iteration variable.
518static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000519 StringRef Name, const AttrVec *Attrs = nullptr) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000520 DeclContext *DC = SemaRef.CurContext;
521 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
522 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
523 VarDecl *Decl =
524 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000525 if (Attrs) {
526 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
527 I != E; ++I)
528 Decl->addAttr(*I);
529 }
Alexey Bataev39f915b82015-05-08 10:41:21 +0000530 Decl->setImplicit();
531 return Decl;
532}
533
534static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
535 SourceLocation Loc,
536 bool RefersToCapture = false) {
537 D->setReferenced();
538 D->markUsed(S.Context);
539 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
540 SourceLocation(), D, RefersToCapture, Loc, Ty,
541 VK_LValue);
542}
543
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000544DSAStackTy::DSAVarData DSAStackTy::getTopDSA(VarDecl *D, bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000545 D = D->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000546 DSAVarData DVar;
547
548 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
549 // in a Construct, C/C++, predetermined, p.1]
550 // Variables appearing in threadprivate directives are threadprivate.
Samuel Antaof8b50122015-07-13 22:54:53 +0000551 if ((D->getTLSKind() != VarDecl::TLS_None &&
552 !(D->hasAttr<OMPThreadPrivateDeclAttr>() &&
553 SemaRef.getLangOpts().OpenMPUseTLS &&
554 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +0000555 (D->getStorageClass() == SC_Register && D->hasAttr<AsmLabelAttr>() &&
556 !D->isLocalVarDecl())) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000557 addDSA(D, buildDeclRefExpr(SemaRef, D, D->getType().getNonReferenceType(),
558 D->getLocation()),
Alexey Bataevf2453a02015-05-06 07:25:08 +0000559 OMPC_threadprivate);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000560 }
561 if (Stack[0].SharingMap.count(D)) {
562 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
563 DVar.CKind = OMPC_threadprivate;
564 return DVar;
565 }
566
567 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
568 // in a Construct, C/C++, predetermined, p.1]
569 // Variables with automatic storage duration that are declared in a scope
570 // inside the construct are private.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000571 OpenMPDirectiveKind Kind =
572 FromParent ? getParentDirective() : getCurrentDirective();
573 auto StartI = std::next(Stack.rbegin());
574 auto EndI = std::prev(Stack.rend());
575 if (FromParent && StartI != EndI) {
576 StartI = std::next(StartI);
577 }
578 if (!isParallelOrTaskRegion(Kind)) {
Alexey Bataev8b9cb982014-07-24 02:33:58 +0000579 if (isOpenMPLocal(D, StartI) &&
580 ((D->isLocalVarDecl() && (D->getStorageClass() == SC_Auto ||
581 D->getStorageClass() == SC_None)) ||
582 isa<ParmVarDecl>(D))) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000583 DVar.CKind = OMPC_private;
584 return DVar;
Alexander Musman8dba6642014-04-22 13:09:42 +0000585 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000586
Alexey Bataev24b04aa2015-01-16 07:11:33 +0000587 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
588 // in a Construct, C/C++, predetermined, p.4]
589 // Static data members are shared.
Alexey Bataev24b04aa2015-01-16 07:11:33 +0000590 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
591 // in a Construct, C/C++, predetermined, p.7]
592 // Variables with static storage duration that are declared in a scope
593 // inside the construct are shared.
Kelvin Li4eea8c62015-09-15 18:56:58 +0000594 if (D->isStaticDataMember()) {
Alexey Bataev42971a32015-01-20 07:03:46 +0000595 DSAVarData DVarTemp =
596 hasDSA(D, isOpenMPPrivate, MatchesAlways(), FromParent);
597 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
598 return DVar;
599
Alexey Bataev24b04aa2015-01-16 07:11:33 +0000600 DVar.CKind = OMPC_shared;
601 return DVar;
602 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000603 }
604
605 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +0000606 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
607 Type = SemaRef.getASTContext().getBaseElementType(Type);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000608 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
609 // in a Construct, C/C++, predetermined, p.6]
610 // Variables with const qualified type having no mutable member are
611 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000612 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000613 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000614 if (IsConstant &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000615 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000616 // Variables with const-qualified type having no mutable member may be
617 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000618 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
619 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000620 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
621 return DVar;
622
Alexey Bataev758e55e2013-09-06 18:03:48 +0000623 DVar.CKind = OMPC_shared;
624 return DVar;
625 }
626
Alexey Bataev758e55e2013-09-06 18:03:48 +0000627 // Explicitly specified attributes and local variables with predetermined
628 // attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000629 auto I = std::prev(StartI);
630 if (I->SharingMap.count(D)) {
631 DVar.RefExpr = I->SharingMap[D].RefExpr;
632 DVar.CKind = I->SharingMap[D].Attributes;
633 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000634 }
635
636 return DVar;
637}
638
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000639DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D, bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000640 D = D->getCanonicalDecl();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000641 auto StartI = Stack.rbegin();
642 auto EndI = std::prev(Stack.rend());
643 if (FromParent && StartI != EndI) {
644 StartI = std::next(StartI);
645 }
646 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000647}
648
Alexey Bataevf29276e2014-06-18 04:14:57 +0000649template <class ClausesPredicate, class DirectivesPredicate>
650DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000651 DirectivesPredicate DPred,
652 bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000653 D = D->getCanonicalDecl();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000654 auto StartI = std::next(Stack.rbegin());
655 auto EndI = std::prev(Stack.rend());
656 if (FromParent && StartI != EndI) {
657 StartI = std::next(StartI);
658 }
659 for (auto I = StartI, EE = EndI; I != EE; ++I) {
660 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000661 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000662 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000663 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000664 return DVar;
665 }
666 return DSAVarData();
667}
668
Alexey Bataevf29276e2014-06-18 04:14:57 +0000669template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000670DSAStackTy::DSAVarData
671DSAStackTy::hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
672 DirectivesPredicate DPred, bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000673 D = D->getCanonicalDecl();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000674 auto StartI = std::next(Stack.rbegin());
675 auto EndI = std::prev(Stack.rend());
676 if (FromParent && StartI != EndI) {
677 StartI = std::next(StartI);
678 }
679 for (auto I = StartI, EE = EndI; I != EE; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000680 if (!DPred(I->Directive))
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000681 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000682 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000683 if (CPred(DVar.CKind))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000684 return DVar;
685 return DSAVarData();
686 }
687 return DSAVarData();
688}
689
Alexey Bataevaac108a2015-06-23 04:51:00 +0000690bool DSAStackTy::hasExplicitDSA(
691 VarDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
692 unsigned Level) {
693 if (CPred(ClauseKindMode))
694 return true;
695 if (isClauseParsingMode())
696 ++Level;
697 D = D->getCanonicalDecl();
698 auto StartI = Stack.rbegin();
699 auto EndI = std::prev(Stack.rend());
NAKAMURA Takumi0332eda2015-06-23 10:01:20 +0000700 if (std::distance(StartI, EndI) <= (int)Level)
Alexey Bataevaac108a2015-06-23 04:51:00 +0000701 return false;
702 std::advance(StartI, Level);
703 return (StartI->SharingMap.count(D) > 0) && StartI->SharingMap[D].RefExpr &&
704 CPred(StartI->SharingMap[D].Attributes);
705}
706
Samuel Antao4be30e92015-10-02 17:14:03 +0000707bool DSAStackTy::hasExplicitDirective(
708 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
709 unsigned Level) {
710 if (isClauseParsingMode())
711 ++Level;
712 auto StartI = Stack.rbegin();
713 auto EndI = std::prev(Stack.rend());
714 if (std::distance(StartI, EndI) <= (int)Level)
715 return false;
716 std::advance(StartI, Level);
717 return DPred(StartI->Directive);
718}
719
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000720template <class NamedDirectivesPredicate>
721bool DSAStackTy::hasDirective(NamedDirectivesPredicate DPred, bool FromParent) {
722 auto StartI = std::next(Stack.rbegin());
723 auto EndI = std::prev(Stack.rend());
724 if (FromParent && StartI != EndI) {
725 StartI = std::next(StartI);
726 }
727 for (auto I = StartI, EE = EndI; I != EE; ++I) {
728 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
729 return true;
730 }
731 return false;
732}
733
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000734OpenMPDirectiveKind DSAStackTy::getDirectiveForScope(const Scope *S) const {
735 for (auto I = Stack.rbegin(), EE = Stack.rend(); I != EE; ++I)
736 if (I->CurScope == S)
737 return I->Directive;
738 return OMPD_unknown;
739}
740
Alexey Bataev758e55e2013-09-06 18:03:48 +0000741void Sema::InitDataSharingAttributesStack() {
742 VarDataSharingAttributesStack = new DSAStackTy(*this);
743}
744
745#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
746
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000747bool Sema::IsOpenMPCapturedByRef(VarDecl *VD,
748 const CapturedRegionScopeInfo *RSI) {
749 assert(LangOpts.OpenMP && "OpenMP is not allowed");
750
751 auto &Ctx = getASTContext();
752 bool IsByRef = true;
753
754 // Find the directive that is associated with the provided scope.
755 auto DKind = DSAStack->getDirectiveForScope(RSI->TheScope);
756 auto Ty = VD->getType();
757
758 if (isOpenMPTargetDirective(DKind)) {
759 // This table summarizes how a given variable should be passed to the device
760 // given its type and the clauses where it appears. This table is based on
761 // the description in OpenMP 4.5 [2.10.4, target Construct] and
762 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
763 //
764 // =========================================================================
765 // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
766 // | |(tofrom:scalar)| | pvt | | | |
767 // =========================================================================
768 // | scl | | | | - | | bycopy|
769 // | scl | | - | x | - | - | bycopy|
770 // | scl | | x | - | - | - | null |
771 // | scl | x | | | - | | byref |
772 // | scl | x | - | x | - | - | bycopy|
773 // | scl | x | x | - | - | - | null |
774 // | scl | | - | - | - | x | byref |
775 // | scl | x | - | - | - | x | byref |
776 //
777 // | agg | n.a. | | | - | | byref |
778 // | agg | n.a. | - | x | - | - | byref |
779 // | agg | n.a. | x | - | - | - | null |
780 // | agg | n.a. | - | - | - | x | byref |
781 // | agg | n.a. | - | - | - | x[] | byref |
782 //
783 // | ptr | n.a. | | | - | | bycopy|
784 // | ptr | n.a. | - | x | - | - | bycopy|
785 // | ptr | n.a. | x | - | - | - | null |
786 // | ptr | n.a. | - | - | - | x | byref |
787 // | ptr | n.a. | - | - | - | x[] | bycopy|
788 // | ptr | n.a. | - | - | x | | bycopy|
789 // | ptr | n.a. | - | - | x | x | bycopy|
790 // | ptr | n.a. | - | - | x | x[] | bycopy|
791 // =========================================================================
792 // Legend:
793 // scl - scalar
794 // ptr - pointer
795 // agg - aggregate
796 // x - applies
797 // - - invalid in this combination
798 // [] - mapped with an array section
799 // byref - should be mapped by reference
800 // byval - should be mapped by value
801 // null - initialize a local variable to null on the device
802 //
803 // Observations:
804 // - All scalar declarations that show up in a map clause have to be passed
805 // by reference, because they may have been mapped in the enclosing data
806 // environment.
807 // - If the scalar value does not fit the size of uintptr, it has to be
808 // passed by reference, regardless the result in the table above.
809 // - For pointers mapped by value that have either an implicit map or an
810 // array section, the runtime library may pass the NULL value to the
811 // device instead of the value passed to it by the compiler.
812
813 // FIXME: Right now, only implicit maps are implemented. Properly mapping
814 // values requires having the map, private, and firstprivate clauses SEMA
815 // and parsing in place, which we don't yet.
816
817 if (Ty->isReferenceType())
818 Ty = Ty->castAs<ReferenceType>()->getPointeeType();
819 IsByRef = !Ty->isScalarType();
820 }
821
822 // When passing data by value, we need to make sure it fits the uintptr size
823 // and alignment, because the runtime library only deals with uintptr types.
824 // If it does not fit the uintptr size, we need to pass the data by reference
825 // instead.
826 if (!IsByRef &&
827 (Ctx.getTypeSizeInChars(Ty) >
828 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
829 Ctx.getDeclAlign(VD) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType())))
830 IsByRef = true;
831
832 return IsByRef;
833}
834
Alexey Bataevf841bd92014-12-16 07:00:22 +0000835bool Sema::IsOpenMPCapturedVar(VarDecl *VD) {
836 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000837 VD = VD->getCanonicalDecl();
Samuel Antao4be30e92015-10-02 17:14:03 +0000838
839 // If we are attempting to capture a global variable in a directive with
840 // 'target' we return true so that this global is also mapped to the device.
841 //
842 // FIXME: If the declaration is enclosed in a 'declare target' directive,
843 // then it should not be captured. Therefore, an extra check has to be
844 // inserted here once support for 'declare target' is added.
845 //
846 if (!VD->hasLocalStorage()) {
847 if (DSAStack->getCurrentDirective() == OMPD_target &&
848 !DSAStack->isClauseParsingMode()) {
849 return true;
850 }
851 if (DSAStack->getCurScope() &&
852 DSAStack->hasDirective(
853 [](OpenMPDirectiveKind K, const DeclarationNameInfo &DNI,
854 SourceLocation Loc) -> bool {
855 return isOpenMPTargetDirective(K);
856 },
857 false)) {
858 return true;
859 }
860 }
861
Alexey Bataev48977c32015-08-04 08:10:48 +0000862 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
863 (!DSAStack->isClauseParsingMode() ||
864 DSAStack->getParentDirective() != OMPD_unknown)) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000865 if (DSAStack->isLoopControlVariable(VD) ||
866 (VD->hasLocalStorage() &&
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000867 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
868 DSAStack->isForceVarCapturing())
Alexey Bataev9c821032015-04-30 04:23:23 +0000869 return true;
Alexey Bataevaac108a2015-06-23 04:51:00 +0000870 auto DVarPrivate = DSAStack->getTopDSA(VD, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000871 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
872 return true;
873 DVarPrivate = DSAStack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(),
Alexey Bataevaac108a2015-06-23 04:51:00 +0000874 DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000875 return DVarPrivate.CKind != OMPC_unknown;
876 }
877 return false;
878}
879
Alexey Bataevaac108a2015-06-23 04:51:00 +0000880bool Sema::isOpenMPPrivateVar(VarDecl *VD, unsigned Level) {
881 assert(LangOpts.OpenMP && "OpenMP is not allowed");
882 return DSAStack->hasExplicitDSA(
883 VD, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level);
884}
885
Samuel Antao4be30e92015-10-02 17:14:03 +0000886bool Sema::isOpenMPTargetCapturedVar(VarDecl *VD, unsigned Level) {
887 assert(LangOpts.OpenMP && "OpenMP is not allowed");
888 // Return true if the current level is no longer enclosed in a target region.
889
890 return !VD->hasLocalStorage() &&
891 DSAStack->hasExplicitDirective(isOpenMPTargetDirective, Level);
892}
893
Alexey Bataeved09d242014-05-28 05:53:51 +0000894void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000895
896void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
897 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000898 Scope *CurScope, SourceLocation Loc) {
899 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000900 PushExpressionEvaluationContext(PotentiallyEvaluated);
901}
902
Alexey Bataevaac108a2015-06-23 04:51:00 +0000903void Sema::StartOpenMPClause(OpenMPClauseKind K) {
904 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000905}
906
Alexey Bataevaac108a2015-06-23 04:51:00 +0000907void Sema::EndOpenMPClause() {
908 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000909}
910
Alexey Bataev758e55e2013-09-06 18:03:48 +0000911void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000912 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
913 // A variable of class type (or array thereof) that appears in a lastprivate
914 // clause requires an accessible, unambiguous default constructor for the
915 // class type, unless the list item is also specified in a firstprivate
916 // clause.
917 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000918 for (auto *C : D->clauses()) {
919 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
920 SmallVector<Expr *, 8> PrivateCopies;
921 for (auto *DE : Clause->varlists()) {
922 if (DE->isValueDependent() || DE->isTypeDependent()) {
923 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000924 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +0000925 }
926 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(DE)->getDecl());
Alexey Bataevbd9fec12015-08-18 06:47:21 +0000927 QualType Type = VD->getType().getNonReferenceType();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000928 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000929 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000930 // Generate helper private variable and initialize it with the
931 // default value. The address of the original variable is replaced
932 // by the address of the new private variable in CodeGen. This new
933 // variable is not added to IdResolver, so the code in the OpenMP
934 // region uses original variable for proper diagnostics.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000935 auto *VDPrivate = buildVarDecl(
936 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
937 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +0000938 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
939 if (VDPrivate->isInvalidDecl())
940 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +0000941 PrivateCopies.push_back(buildDeclRefExpr(
942 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +0000943 } else {
944 // The variable is also a firstprivate, so initialization sequence
945 // for private copy is generated already.
946 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000947 }
948 }
Alexey Bataev38e89532015-04-16 04:54:05 +0000949 // Set initializers to private copies if no errors were found.
950 if (PrivateCopies.size() == Clause->varlist_size()) {
951 Clause->setPrivateCopies(PrivateCopies);
952 }
Alexey Bataevf29276e2014-06-18 04:14:57 +0000953 }
954 }
955 }
956
Alexey Bataev758e55e2013-09-06 18:03:48 +0000957 DSAStack->pop();
958 DiscardCleanupsInEvaluationContext();
959 PopExpressionEvaluationContext();
960}
961
Alexander Musman3276a272015-03-21 10:12:56 +0000962static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
963 Expr *NumIterations, Sema &SemaRef,
964 Scope *S);
965
Alexey Bataeva769e072013-03-22 06:34:35 +0000966namespace {
967
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000968class VarDeclFilterCCC : public CorrectionCandidateCallback {
969private:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000970 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +0000971
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000972public:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000973 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +0000974 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000975 NamedDecl *ND = Candidate.getCorrectionDecl();
976 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
977 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000978 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
979 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +0000980 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000981 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000982 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000983};
Alexey Bataeved09d242014-05-28 05:53:51 +0000984} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000985
986ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
987 CXXScopeSpec &ScopeSpec,
988 const DeclarationNameInfo &Id) {
989 LookupResult Lookup(*this, Id, LookupOrdinaryName);
990 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
991
992 if (Lookup.isAmbiguous())
993 return ExprError();
994
995 VarDecl *VD;
996 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +0000997 if (TypoCorrection Corrected = CorrectTypo(
998 Id, LookupOrdinaryName, CurScope, nullptr,
999 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +00001000 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001001 PDiag(Lookup.empty()
1002 ? diag::err_undeclared_var_use_suggest
1003 : diag::err_omp_expected_var_arg_suggest)
1004 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +00001005 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001006 } else {
Richard Smithf9b15102013-08-17 00:46:16 +00001007 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
1008 : diag::err_omp_expected_var_arg)
1009 << Id.getName();
1010 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001011 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001012 } else {
1013 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001014 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001015 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1016 return ExprError();
1017 }
1018 }
1019 Lookup.suppressDiagnostics();
1020
1021 // OpenMP [2.9.2, Syntax, C/C++]
1022 // Variables must be file-scope, namespace-scope, or static block-scope.
1023 if (!VD->hasGlobalStorage()) {
1024 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001025 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1026 bool IsDecl =
1027 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001028 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001029 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1030 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001031 return ExprError();
1032 }
1033
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001034 VarDecl *CanonicalVD = VD->getCanonicalDecl();
1035 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001036 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1037 // A threadprivate directive for file-scope variables must appear outside
1038 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001039 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1040 !getCurLexicalContext()->isTranslationUnit()) {
1041 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001042 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1043 bool IsDecl =
1044 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1045 Diag(VD->getLocation(),
1046 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1047 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001048 return ExprError();
1049 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001050 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1051 // A threadprivate directive for static class member variables must appear
1052 // in the class definition, in the same scope in which the member
1053 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001054 if (CanonicalVD->isStaticDataMember() &&
1055 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1056 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001057 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1058 bool IsDecl =
1059 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1060 Diag(VD->getLocation(),
1061 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1062 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001063 return ExprError();
1064 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001065 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1066 // A threadprivate directive for namespace-scope variables must appear
1067 // outside any definition or declaration other than the namespace
1068 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001069 if (CanonicalVD->getDeclContext()->isNamespace() &&
1070 (!getCurLexicalContext()->isFileContext() ||
1071 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
1072 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001073 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1074 bool IsDecl =
1075 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1076 Diag(VD->getLocation(),
1077 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1078 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001079 return ExprError();
1080 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001081 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
1082 // A threadprivate directive for static block-scope variables must appear
1083 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001084 if (CanonicalVD->isStaticLocal() && CurScope &&
1085 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001086 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001087 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1088 bool IsDecl =
1089 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1090 Diag(VD->getLocation(),
1091 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1092 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001093 return ExprError();
1094 }
1095
1096 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1097 // A threadprivate directive must lexically precede all references to any
1098 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001099 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001100 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +00001101 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001102 return ExprError();
1103 }
1104
1105 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00001106 ExprResult DE = buildDeclRefExpr(*this, VD, ExprType, Id.getLoc());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001107 return DE;
1108}
1109
Alexey Bataeved09d242014-05-28 05:53:51 +00001110Sema::DeclGroupPtrTy
1111Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1112 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001113 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001114 CurContext->addDecl(D);
1115 return DeclGroupPtrTy::make(DeclGroupRef(D));
1116 }
1117 return DeclGroupPtrTy();
1118}
1119
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001120namespace {
1121class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
1122 Sema &SemaRef;
1123
1124public:
1125 bool VisitDeclRefExpr(const DeclRefExpr *E) {
1126 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
1127 if (VD->hasLocalStorage()) {
1128 SemaRef.Diag(E->getLocStart(),
1129 diag::err_omp_local_var_in_threadprivate_init)
1130 << E->getSourceRange();
1131 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1132 << VD << VD->getSourceRange();
1133 return true;
1134 }
1135 }
1136 return false;
1137 }
1138 bool VisitStmt(const Stmt *S) {
1139 for (auto Child : S->children()) {
1140 if (Child && Visit(Child))
1141 return true;
1142 }
1143 return false;
1144 }
Alexey Bataev23b69422014-06-18 07:08:49 +00001145 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001146};
1147} // namespace
1148
Alexey Bataeved09d242014-05-28 05:53:51 +00001149OMPThreadPrivateDecl *
1150Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001151 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001152 for (auto &RefExpr : VarList) {
1153 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001154 VarDecl *VD = cast<VarDecl>(DE->getDecl());
1155 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +00001156
Alexey Bataevf56f98c2015-04-16 05:39:01 +00001157 QualType QType = VD->getType();
1158 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1159 // It will be analyzed later.
1160 Vars.push_back(DE);
1161 continue;
1162 }
1163
Alexey Bataeva769e072013-03-22 06:34:35 +00001164 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1165 // A threadprivate variable must not have an incomplete type.
1166 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001167 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001168 continue;
1169 }
1170
1171 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1172 // A threadprivate variable must not have a reference type.
1173 if (VD->getType()->isReferenceType()) {
1174 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001175 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
1176 bool IsDecl =
1177 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1178 Diag(VD->getLocation(),
1179 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1180 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001181 continue;
1182 }
1183
Samuel Antaof8b50122015-07-13 22:54:53 +00001184 // Check if this is a TLS variable. If TLS is not being supported, produce
1185 // the corresponding diagnostic.
1186 if ((VD->getTLSKind() != VarDecl::TLS_None &&
1187 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1188 getLangOpts().OpenMPUseTLS &&
1189 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +00001190 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
1191 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +00001192 Diag(ILoc, diag::err_omp_var_thread_local)
1193 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +00001194 bool IsDecl =
1195 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1196 Diag(VD->getLocation(),
1197 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1198 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001199 continue;
1200 }
1201
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001202 // Check if initial value of threadprivate variable reference variable with
1203 // local storage (it is not supported by runtime).
1204 if (auto Init = VD->getAnyInitializer()) {
1205 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001206 if (Checker.Visit(Init))
1207 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001208 }
1209
Alexey Bataeved09d242014-05-28 05:53:51 +00001210 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00001211 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00001212 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1213 Context, SourceRange(Loc, Loc)));
1214 if (auto *ML = Context.getASTMutationListener())
1215 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00001216 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001217 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001218 if (!Vars.empty()) {
1219 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1220 Vars);
1221 D->setAccess(AS_public);
1222 }
1223 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00001224}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001225
Alexey Bataev7ff55242014-06-19 09:13:45 +00001226static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
1227 const VarDecl *VD, DSAStackTy::DSAVarData DVar,
1228 bool IsLoopIterVar = false) {
1229 if (DVar.RefExpr) {
1230 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1231 << getOpenMPClauseName(DVar.CKind);
1232 return;
1233 }
1234 enum {
1235 PDSA_StaticMemberShared,
1236 PDSA_StaticLocalVarShared,
1237 PDSA_LoopIterVarPrivate,
1238 PDSA_LoopIterVarLinear,
1239 PDSA_LoopIterVarLastprivate,
1240 PDSA_ConstVarShared,
1241 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001242 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001243 PDSA_LocalVarPrivate,
1244 PDSA_Implicit
1245 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001246 bool ReportHint = false;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001247 auto ReportLoc = VD->getLocation();
Alexey Bataev7ff55242014-06-19 09:13:45 +00001248 if (IsLoopIterVar) {
1249 if (DVar.CKind == OMPC_private)
1250 Reason = PDSA_LoopIterVarPrivate;
1251 else if (DVar.CKind == OMPC_lastprivate)
1252 Reason = PDSA_LoopIterVarLastprivate;
1253 else
1254 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001255 } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) {
1256 Reason = PDSA_TaskVarFirstprivate;
1257 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001258 } else if (VD->isStaticLocal())
1259 Reason = PDSA_StaticLocalVarShared;
1260 else if (VD->isStaticDataMember())
1261 Reason = PDSA_StaticMemberShared;
1262 else if (VD->isFileVarDecl())
1263 Reason = PDSA_GlobalVarShared;
1264 else if (VD->getType().isConstant(SemaRef.getASTContext()))
1265 Reason = PDSA_ConstVarShared;
Alexey Bataevbae9a792014-06-27 10:37:06 +00001266 else if (VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00001267 ReportHint = true;
1268 Reason = PDSA_LocalVarPrivate;
1269 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00001270 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001271 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00001272 << Reason << ReportHint
1273 << getOpenMPDirectiveName(Stack->getCurrentDirective());
1274 } else if (DVar.ImplicitDSALoc.isValid()) {
1275 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1276 << getOpenMPClauseName(DVar.CKind);
1277 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00001278}
1279
Alexey Bataev758e55e2013-09-06 18:03:48 +00001280namespace {
1281class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1282 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001283 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001284 bool ErrorFound;
1285 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001286 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev4acb8592014-07-07 13:01:15 +00001287 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +00001288
Alexey Bataev758e55e2013-09-06 18:03:48 +00001289public:
1290 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001291 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001292 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +00001293 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1294 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001295
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001296 auto DVar = Stack->getTopDSA(VD, false);
1297 // Check if the variable has explicit DSA set and stop analysis if it so.
1298 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001299
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001300 auto ELoc = E->getExprLoc();
1301 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001302 // The default(none) clause requires that each variable that is referenced
1303 // in the construct, and does not have a predetermined data-sharing
1304 // attribute, must have its data-sharing attribute explicitly determined
1305 // by being listed in a data-sharing attribute clause.
1306 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001307 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001308 VarsWithInheritedDSA.count(VD) == 0) {
1309 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001310 return;
1311 }
1312
1313 // OpenMP [2.9.3.6, Restrictions, p.2]
1314 // A list item that appears in a reduction clause of the innermost
1315 // enclosing worksharing or parallel construct may not be accessed in an
1316 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +00001317 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001318 [](OpenMPDirectiveKind K) -> bool {
1319 return isOpenMPParallelDirective(K) ||
Alexey Bataev13314bf2014-10-09 04:18:56 +00001320 isOpenMPWorksharingDirective(K) ||
1321 isOpenMPTeamsDirective(K);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001322 },
1323 false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001324 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
1325 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001326 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1327 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001328 return;
1329 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001330
1331 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001332 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001333 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001334 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001335 }
1336 }
1337 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001338 for (auto *C : S->clauses()) {
1339 // Skip analysis of arguments of implicitly defined firstprivate clause
1340 // for task directives.
1341 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1342 for (auto *CC : C->children()) {
1343 if (CC)
1344 Visit(CC);
1345 }
1346 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001347 }
1348 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001349 for (auto *C : S->children()) {
1350 if (C && !isa<OMPExecutableDirective>(C))
1351 Visit(C);
1352 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001353 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001354
1355 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001356 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001357 llvm::DenseMap<VarDecl *, Expr *> &getVarsWithInheritedDSA() {
1358 return VarsWithInheritedDSA;
1359 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001360
Alexey Bataev7ff55242014-06-19 09:13:45 +00001361 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1362 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00001363};
Alexey Bataeved09d242014-05-28 05:53:51 +00001364} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001365
Alexey Bataevbae9a792014-06-27 10:37:06 +00001366void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001367 switch (DKind) {
1368 case OMPD_parallel: {
1369 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001370 QualType KmpInt32PtrTy =
1371 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001372 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001373 std::make_pair(".global_tid.", KmpInt32PtrTy),
1374 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1375 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001376 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001377 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1378 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001379 break;
1380 }
1381 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001382 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001383 std::make_pair(StringRef(), QualType()) // __context with shared vars
1384 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001385 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1386 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001387 break;
1388 }
1389 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001390 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001391 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001392 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001393 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1394 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001395 break;
1396 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001397 case OMPD_for_simd: {
1398 Sema::CapturedParamNameType Params[] = {
1399 std::make_pair(StringRef(), QualType()) // __context with shared vars
1400 };
1401 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1402 Params);
1403 break;
1404 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001405 case OMPD_sections: {
1406 Sema::CapturedParamNameType Params[] = {
1407 std::make_pair(StringRef(), QualType()) // __context with shared vars
1408 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001409 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1410 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001411 break;
1412 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001413 case OMPD_section: {
1414 Sema::CapturedParamNameType Params[] = {
1415 std::make_pair(StringRef(), QualType()) // __context with shared vars
1416 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001417 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1418 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001419 break;
1420 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001421 case OMPD_single: {
1422 Sema::CapturedParamNameType Params[] = {
1423 std::make_pair(StringRef(), QualType()) // __context with shared vars
1424 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001425 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1426 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001427 break;
1428 }
Alexander Musman80c22892014-07-17 08:54:58 +00001429 case OMPD_master: {
1430 Sema::CapturedParamNameType Params[] = {
1431 std::make_pair(StringRef(), QualType()) // __context with shared vars
1432 };
1433 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1434 Params);
1435 break;
1436 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001437 case OMPD_critical: {
1438 Sema::CapturedParamNameType Params[] = {
1439 std::make_pair(StringRef(), QualType()) // __context with shared vars
1440 };
1441 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1442 Params);
1443 break;
1444 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001445 case OMPD_parallel_for: {
1446 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001447 QualType KmpInt32PtrTy =
1448 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev4acb8592014-07-07 13:01:15 +00001449 Sema::CapturedParamNameType Params[] = {
1450 std::make_pair(".global_tid.", KmpInt32PtrTy),
1451 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1452 std::make_pair(StringRef(), QualType()) // __context with shared vars
1453 };
1454 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1455 Params);
1456 break;
1457 }
Alexander Musmane4e893b2014-09-23 09:33:00 +00001458 case OMPD_parallel_for_simd: {
1459 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001460 QualType KmpInt32PtrTy =
1461 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexander Musmane4e893b2014-09-23 09:33:00 +00001462 Sema::CapturedParamNameType Params[] = {
1463 std::make_pair(".global_tid.", KmpInt32PtrTy),
1464 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1465 std::make_pair(StringRef(), QualType()) // __context with shared vars
1466 };
1467 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1468 Params);
1469 break;
1470 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001471 case OMPD_parallel_sections: {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001472 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001473 QualType KmpInt32PtrTy =
1474 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001475 Sema::CapturedParamNameType Params[] = {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001476 std::make_pair(".global_tid.", KmpInt32PtrTy),
1477 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001478 std::make_pair(StringRef(), QualType()) // __context with shared vars
1479 };
1480 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1481 Params);
1482 break;
1483 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001484 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001485 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001486 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1487 FunctionProtoType::ExtProtoInfo EPI;
1488 EPI.Variadic = true;
1489 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001490 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001491 std::make_pair(".global_tid.", KmpInt32Ty),
1492 std::make_pair(".part_id.", KmpInt32Ty),
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001493 std::make_pair(".privates.",
1494 Context.VoidPtrTy.withConst().withRestrict()),
1495 std::make_pair(
1496 ".copy_fn.",
1497 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001498 std::make_pair(StringRef(), QualType()) // __context with shared vars
1499 };
1500 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1501 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001502 // Mark this captured region as inlined, because we don't use outlined
1503 // function directly.
1504 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1505 AlwaysInlineAttr::CreateImplicit(
1506 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001507 break;
1508 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001509 case OMPD_ordered: {
1510 Sema::CapturedParamNameType Params[] = {
1511 std::make_pair(StringRef(), QualType()) // __context with shared vars
1512 };
1513 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1514 Params);
1515 break;
1516 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001517 case OMPD_atomic: {
1518 Sema::CapturedParamNameType Params[] = {
1519 std::make_pair(StringRef(), QualType()) // __context with shared vars
1520 };
1521 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1522 Params);
1523 break;
1524 }
Michael Wong65f367f2015-07-21 13:44:28 +00001525 case OMPD_target_data:
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001526 case OMPD_target: {
1527 Sema::CapturedParamNameType Params[] = {
1528 std::make_pair(StringRef(), QualType()) // __context with shared vars
1529 };
1530 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1531 Params);
1532 break;
1533 }
Alexey Bataev13314bf2014-10-09 04:18:56 +00001534 case OMPD_teams: {
1535 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001536 QualType KmpInt32PtrTy =
1537 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev13314bf2014-10-09 04:18:56 +00001538 Sema::CapturedParamNameType Params[] = {
1539 std::make_pair(".global_tid.", KmpInt32PtrTy),
1540 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1541 std::make_pair(StringRef(), QualType()) // __context with shared vars
1542 };
1543 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1544 Params);
1545 break;
1546 }
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001547 case OMPD_taskgroup: {
1548 Sema::CapturedParamNameType Params[] = {
1549 std::make_pair(StringRef(), QualType()) // __context with shared vars
1550 };
1551 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1552 Params);
1553 break;
1554 }
Alexey Bataev49f6e782015-12-01 04:18:41 +00001555 case OMPD_taskloop: {
1556 Sema::CapturedParamNameType Params[] = {
1557 std::make_pair(StringRef(), QualType()) // __context with shared vars
1558 };
1559 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1560 Params);
1561 break;
1562 }
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001563 case OMPD_taskloop_simd: {
1564 Sema::CapturedParamNameType Params[] = {
1565 std::make_pair(StringRef(), QualType()) // __context with shared vars
1566 };
1567 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1568 Params);
1569 break;
1570 }
Carlo Bertollib9bfa752015-12-08 04:21:03 +00001571 case OMPD_distribute: {
1572 Sema::CapturedParamNameType Params[] = {
1573 std::make_pair(StringRef(), QualType()) // __context with shared vars
1574 };
1575 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1576 Params);
1577 break;
1578 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001579 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001580 case OMPD_taskyield:
1581 case OMPD_barrier:
1582 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001583 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001584 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00001585 case OMPD_flush:
Alexey Bataev9959db52014-05-06 10:08:46 +00001586 llvm_unreachable("OpenMP Directive is not allowed");
1587 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001588 llvm_unreachable("Unknown OpenMP directive");
1589 }
1590}
1591
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001592StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1593 ArrayRef<OMPClause *> Clauses) {
1594 if (!S.isUsable()) {
1595 ActOnCapturedRegionError();
1596 return StmtError();
1597 }
Alexey Bataev040d5402015-05-12 08:35:28 +00001598 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001599 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001600 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001601 Clause->getClauseKind() == OMPC_copyprivate ||
1602 (getLangOpts().OpenMPUseTLS &&
1603 getASTContext().getTargetInfo().isTLSSupported() &&
1604 Clause->getClauseKind() == OMPC_copyin)) {
1605 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001606 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001607 for (auto *VarRef : Clause->children()) {
1608 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001609 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001610 }
1611 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001612 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev040d5402015-05-12 08:35:28 +00001613 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
1614 Clause->getClauseKind() == OMPC_schedule) {
1615 // Mark all variables in private list clauses as used in inner region.
1616 // Required for proper codegen of combined directives.
1617 // TODO: add processing for other clauses.
1618 if (auto *E = cast_or_null<Expr>(
1619 cast<OMPScheduleClause>(Clause)->getHelperChunkSize())) {
1620 MarkDeclarationsReferencedInExpr(E);
1621 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001622 }
1623 }
1624 return ActOnCapturedRegionEnd(S.get());
1625}
1626
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001627static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1628 OpenMPDirectiveKind CurrentRegion,
1629 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001630 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001631 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001632 // Allowed nesting of constructs
1633 // +------------------+-----------------+------------------------------------+
1634 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1635 // +------------------+-----------------+------------------------------------+
1636 // | parallel | parallel | * |
1637 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001638 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001639 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001640 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001641 // | parallel | simd | * |
1642 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001643 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001644 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001645 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001646 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001647 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001648 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001649 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001650 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001651 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001652 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001653 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001654 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001655 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001656 // | parallel | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001657 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001658 // | parallel | cancellation | |
1659 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001660 // | parallel | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001661 // | parallel | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001662 // | parallel | taskloop simd | * |
Carlo Bertollib9bfa752015-12-08 04:21:03 +00001663 // | parallel | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001664 // +------------------+-----------------+------------------------------------+
1665 // | for | parallel | * |
1666 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001667 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001668 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001669 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001670 // | for | simd | * |
1671 // | for | sections | + |
1672 // | for | section | + |
1673 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001674 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001675 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001676 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001677 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001678 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001679 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001680 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001681 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001682 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001683 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001684 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001685 // | for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001686 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001687 // | for | cancellation | |
1688 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001689 // | for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001690 // | for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001691 // | for | taskloop simd | * |
Carlo Bertollib9bfa752015-12-08 04:21:03 +00001692 // | for | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001693 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001694 // | master | parallel | * |
1695 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001696 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001697 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001698 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001699 // | master | simd | * |
1700 // | master | sections | + |
1701 // | master | section | + |
1702 // | master | single | + |
1703 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001704 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001705 // | master |parallel sections| * |
1706 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001707 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001708 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001709 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001710 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001711 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001712 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001713 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001714 // | master | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001715 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001716 // | master | cancellation | |
1717 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001718 // | master | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001719 // | master | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001720 // | master | taskloop simd | * |
Carlo Bertollib9bfa752015-12-08 04:21:03 +00001721 // | master | distribute | |
Alexander Musman80c22892014-07-17 08:54:58 +00001722 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001723 // | critical | parallel | * |
1724 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001725 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001726 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001727 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001728 // | critical | simd | * |
1729 // | critical | sections | + |
1730 // | critical | section | + |
1731 // | critical | single | + |
1732 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001733 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001734 // | critical |parallel sections| * |
1735 // | critical | task | * |
1736 // | critical | taskyield | * |
1737 // | critical | barrier | + |
1738 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001739 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001740 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001741 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001742 // | critical | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001743 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001744 // | critical | cancellation | |
1745 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001746 // | critical | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001747 // | critical | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001748 // | critical | taskloop simd | * |
Carlo Bertollib9bfa752015-12-08 04:21:03 +00001749 // | critical | distribute | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001750 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001751 // | simd | parallel | |
1752 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001753 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00001754 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001755 // | simd | critical | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001756 // | simd | simd | |
1757 // | simd | sections | |
1758 // | simd | section | |
1759 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001760 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001761 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001762 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001763 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00001764 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001765 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001766 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001767 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00001768 // | simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001769 // | simd | ordered | + (with simd clause) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001770 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001771 // | simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001772 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001773 // | simd | cancellation | |
1774 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001775 // | simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001776 // | simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001777 // | simd | taskloop simd | |
Carlo Bertollib9bfa752015-12-08 04:21:03 +00001778 // | simd | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001779 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00001780 // | for simd | parallel | |
1781 // | for simd | for | |
1782 // | for simd | for simd | |
1783 // | for simd | master | |
1784 // | for simd | critical | |
1785 // | for simd | simd | |
1786 // | for simd | sections | |
1787 // | for simd | section | |
1788 // | for simd | single | |
1789 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001790 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001791 // | for simd |parallel sections| |
1792 // | for simd | task | |
1793 // | for simd | taskyield | |
1794 // | for simd | barrier | |
1795 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001796 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001797 // | for simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001798 // | for simd | ordered | + (with simd clause) |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001799 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001800 // | for simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001801 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001802 // | for simd | cancellation | |
1803 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001804 // | for simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001805 // | for simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001806 // | for simd | taskloop simd | |
Carlo Bertollib9bfa752015-12-08 04:21:03 +00001807 // | for simd | distribute | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001808 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00001809 // | parallel for simd| parallel | |
1810 // | parallel for simd| for | |
1811 // | parallel for simd| for simd | |
1812 // | parallel for simd| master | |
1813 // | parallel for simd| critical | |
1814 // | parallel for simd| simd | |
1815 // | parallel for simd| sections | |
1816 // | parallel for simd| section | |
1817 // | parallel for simd| single | |
1818 // | parallel for simd| parallel for | |
1819 // | parallel for simd|parallel for simd| |
1820 // | parallel for simd|parallel sections| |
1821 // | parallel for simd| task | |
1822 // | parallel for simd| taskyield | |
1823 // | parallel for simd| barrier | |
1824 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001825 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001826 // | parallel for simd| flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001827 // | parallel for simd| ordered | + (with simd clause) |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001828 // | parallel for simd| atomic | |
1829 // | parallel for simd| target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001830 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001831 // | parallel for simd| cancellation | |
1832 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001833 // | parallel for simd| cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001834 // | parallel for simd| taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001835 // | parallel for simd| taskloop simd | |
Carlo Bertollib9bfa752015-12-08 04:21:03 +00001836 // | parallel for simd| distribute | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001837 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001838 // | sections | parallel | * |
1839 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001840 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001841 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001842 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001843 // | sections | simd | * |
1844 // | sections | sections | + |
1845 // | sections | section | * |
1846 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001847 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001848 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001849 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001850 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001851 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001852 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001853 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001854 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001855 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001856 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001857 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001858 // | sections | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001859 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001860 // | sections | cancellation | |
1861 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001862 // | sections | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001863 // | sections | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001864 // | sections | taskloop simd | * |
Carlo Bertollib9bfa752015-12-08 04:21:03 +00001865 // | sections | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001866 // +------------------+-----------------+------------------------------------+
1867 // | section | parallel | * |
1868 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001869 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001870 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001871 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001872 // | section | simd | * |
1873 // | section | sections | + |
1874 // | section | section | + |
1875 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001876 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001877 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001878 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001879 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001880 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001881 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001882 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001883 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001884 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001885 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001886 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001887 // | section | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001888 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001889 // | section | cancellation | |
1890 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001891 // | section | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001892 // | section | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001893 // | section | taskloop simd | * |
Carlo Bertollib9bfa752015-12-08 04:21:03 +00001894 // | section | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001895 // +------------------+-----------------+------------------------------------+
1896 // | single | parallel | * |
1897 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001898 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001899 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001900 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001901 // | single | simd | * |
1902 // | single | sections | + |
1903 // | single | section | + |
1904 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001905 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001906 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001907 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001908 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001909 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001910 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001911 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001912 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001913 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001914 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001915 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001916 // | single | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001917 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001918 // | single | cancellation | |
1919 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001920 // | single | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001921 // | single | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001922 // | single | taskloop simd | * |
Carlo Bertollib9bfa752015-12-08 04:21:03 +00001923 // | single | distribute | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001924 // +------------------+-----------------+------------------------------------+
1925 // | parallel for | parallel | * |
1926 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001927 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001928 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001929 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001930 // | parallel for | simd | * |
1931 // | parallel for | sections | + |
1932 // | parallel for | section | + |
1933 // | parallel for | single | + |
1934 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001935 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001936 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001937 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001938 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001939 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001940 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001941 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001942 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001943 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001944 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001945 // | parallel for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001946 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001947 // | parallel for | cancellation | |
1948 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001949 // | parallel for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001950 // | parallel for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001951 // | parallel for | taskloop simd | * |
Carlo Bertollib9bfa752015-12-08 04:21:03 +00001952 // | parallel for | distribute | |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001953 // +------------------+-----------------+------------------------------------+
1954 // | parallel sections| parallel | * |
1955 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001956 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001957 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001958 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001959 // | parallel sections| simd | * |
1960 // | parallel sections| sections | + |
1961 // | parallel sections| section | * |
1962 // | parallel sections| single | + |
1963 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001964 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001965 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001966 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001967 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001968 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001969 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001970 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001971 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001972 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001973 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001974 // | parallel sections| target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001975 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001976 // | parallel sections| cancellation | |
1977 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001978 // | parallel sections| cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001979 // | parallel sections| taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001980 // | parallel sections| taskloop simd | * |
Carlo Bertollib9bfa752015-12-08 04:21:03 +00001981 // | parallel sections| distribute | |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001982 // +------------------+-----------------+------------------------------------+
1983 // | task | parallel | * |
1984 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001985 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001986 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001987 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001988 // | task | simd | * |
1989 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001990 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001991 // | task | single | + |
1992 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001993 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001994 // | task |parallel sections| * |
1995 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001996 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001997 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001998 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001999 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00002000 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002001 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002002 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002003 // | task | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002004 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002005 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00002006 // | | point | ! |
2007 // | task | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002008 // | task | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002009 // | task | taskloop simd | * |
Carlo Bertollib9bfa752015-12-08 04:21:03 +00002010 // | task | distribute | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002011 // +------------------+-----------------+------------------------------------+
2012 // | ordered | parallel | * |
2013 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002014 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002015 // | ordered | master | * |
2016 // | ordered | critical | * |
2017 // | ordered | simd | * |
2018 // | ordered | sections | + |
2019 // | ordered | section | + |
2020 // | ordered | single | + |
2021 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002022 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002023 // | ordered |parallel sections| * |
2024 // | ordered | task | * |
2025 // | ordered | taskyield | * |
2026 // | ordered | barrier | + |
2027 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002028 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002029 // | ordered | flush | * |
2030 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002031 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002032 // | ordered | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002033 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002034 // | ordered | cancellation | |
2035 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002036 // | ordered | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002037 // | ordered | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002038 // | ordered | taskloop simd | * |
Carlo Bertollib9bfa752015-12-08 04:21:03 +00002039 // | ordered | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002040 // +------------------+-----------------+------------------------------------+
2041 // | atomic | parallel | |
2042 // | atomic | for | |
2043 // | atomic | for simd | |
2044 // | atomic | master | |
2045 // | atomic | critical | |
2046 // | atomic | simd | |
2047 // | atomic | sections | |
2048 // | atomic | section | |
2049 // | atomic | single | |
2050 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002051 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002052 // | atomic |parallel sections| |
2053 // | atomic | task | |
2054 // | atomic | taskyield | |
2055 // | atomic | barrier | |
2056 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002057 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002058 // | atomic | flush | |
2059 // | atomic | ordered | |
2060 // | atomic | atomic | |
2061 // | atomic | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002062 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002063 // | atomic | cancellation | |
2064 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002065 // | atomic | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002066 // | atomic | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002067 // | atomic | taskloop simd | |
Carlo Bertollib9bfa752015-12-08 04:21:03 +00002068 // | atomic | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002069 // +------------------+-----------------+------------------------------------+
2070 // | target | parallel | * |
2071 // | target | for | * |
2072 // | target | for simd | * |
2073 // | target | master | * |
2074 // | target | critical | * |
2075 // | target | simd | * |
2076 // | target | sections | * |
2077 // | target | section | * |
2078 // | target | single | * |
2079 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002080 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002081 // | target |parallel sections| * |
2082 // | target | task | * |
2083 // | target | taskyield | * |
2084 // | target | barrier | * |
2085 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002086 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002087 // | target | flush | * |
2088 // | target | ordered | * |
2089 // | target | atomic | * |
2090 // | target | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002091 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002092 // | target | cancellation | |
2093 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002094 // | target | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002095 // | target | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002096 // | target | taskloop simd | * |
Carlo Bertollib9bfa752015-12-08 04:21:03 +00002097 // | target | distribute | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002098 // +------------------+-----------------+------------------------------------+
2099 // | teams | parallel | * |
2100 // | teams | for | + |
2101 // | teams | for simd | + |
2102 // | teams | master | + |
2103 // | teams | critical | + |
2104 // | teams | simd | + |
2105 // | teams | sections | + |
2106 // | teams | section | + |
2107 // | teams | single | + |
2108 // | teams | parallel for | * |
2109 // | teams |parallel for simd| * |
2110 // | teams |parallel sections| * |
2111 // | teams | task | + |
2112 // | teams | taskyield | + |
2113 // | teams | barrier | + |
2114 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00002115 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002116 // | teams | flush | + |
2117 // | teams | ordered | + |
2118 // | teams | atomic | + |
2119 // | teams | target | + |
2120 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002121 // | teams | cancellation | |
2122 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002123 // | teams | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002124 // | teams | taskloop | + |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002125 // | teams | taskloop simd | + |
Carlo Bertollib9bfa752015-12-08 04:21:03 +00002126 // | teams | distribute | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002127 // +------------------+-----------------+------------------------------------+
2128 // | taskloop | parallel | * |
2129 // | taskloop | for | + |
2130 // | taskloop | for simd | + |
2131 // | taskloop | master | + |
2132 // | taskloop | critical | * |
2133 // | taskloop | simd | * |
2134 // | taskloop | sections | + |
2135 // | taskloop | section | + |
2136 // | taskloop | single | + |
2137 // | taskloop | parallel for | * |
2138 // | taskloop |parallel for simd| * |
2139 // | taskloop |parallel sections| * |
2140 // | taskloop | task | * |
2141 // | taskloop | taskyield | * |
2142 // | taskloop | barrier | + |
2143 // | taskloop | taskwait | * |
2144 // | taskloop | taskgroup | * |
2145 // | taskloop | flush | * |
2146 // | taskloop | ordered | + |
2147 // | taskloop | atomic | * |
2148 // | taskloop | target | * |
2149 // | taskloop | teams | + |
2150 // | taskloop | cancellation | |
2151 // | | point | |
2152 // | taskloop | cancel | |
2153 // | taskloop | taskloop | * |
Carlo Bertollib9bfa752015-12-08 04:21:03 +00002154 // | taskloop | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002155 // +------------------+-----------------+------------------------------------+
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002156 // | taskloop simd | parallel | |
2157 // | taskloop simd | for | |
2158 // | taskloop simd | for simd | |
2159 // | taskloop simd | master | |
2160 // | taskloop simd | critical | |
2161 // | taskloop simd | simd | |
2162 // | taskloop simd | sections | |
2163 // | taskloop simd | section | |
2164 // | taskloop simd | single | |
2165 // | taskloop simd | parallel for | |
2166 // | taskloop simd |parallel for simd| |
2167 // | taskloop simd |parallel sections| |
2168 // | taskloop simd | task | |
2169 // | taskloop simd | taskyield | |
2170 // | taskloop simd | barrier | |
2171 // | taskloop simd | taskwait | |
2172 // | taskloop simd | taskgroup | |
2173 // | taskloop simd | flush | |
2174 // | taskloop simd | ordered | + (with simd clause) |
2175 // | taskloop simd | atomic | |
2176 // | taskloop simd | target | |
2177 // | taskloop simd | teams | |
2178 // | taskloop simd | cancellation | |
2179 // | | point | |
2180 // | taskloop simd | cancel | |
2181 // | taskloop simd | taskloop | |
2182 // | taskloop simd | taskloop simd | |
Carlo Bertollib9bfa752015-12-08 04:21:03 +00002183 // | taskloop simd | distribute | |
2184 // +------------------+-----------------+------------------------------------+
2185 // | distribute | parallel | * |
2186 // | distribute | for | * |
2187 // | distribute | for simd | * |
2188 // | distribute | master | * |
2189 // | distribute | critical | * |
2190 // | distribute | simd | * |
2191 // | distribute | sections | * |
2192 // | distribute | section | * |
2193 // | distribute | single | * |
2194 // | distribute | parallel for | * |
2195 // | distribute |parallel for simd| * |
2196 // | distribute |parallel sections| * |
2197 // | distribute | task | * |
2198 // | distribute | taskyield | * |
2199 // | distribute | barrier | * |
2200 // | distribute | taskwait | * |
2201 // | distribute | taskgroup | * |
2202 // | distribute | flush | * |
2203 // | distribute | ordered | + |
2204 // | distribute | atomic | * |
2205 // | distribute | target | |
2206 // | distribute | teams | |
2207 // | distribute | cancellation | + |
2208 // | | point | |
2209 // | distribute | cancel | + |
2210 // | distribute | taskloop | * |
2211 // | distribute | taskloop simd | * |
2212 // | distribute | distribute | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002213 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00002214 if (Stack->getCurScope()) {
2215 auto ParentRegion = Stack->getParentDirective();
2216 bool NestingProhibited = false;
2217 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002218 enum {
2219 NoRecommend,
2220 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00002221 ShouldBeInOrderedRegion,
Carlo Bertollib9bfa752015-12-08 04:21:03 +00002222 ShouldBeInTargetRegion,
2223 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002224 } Recommend = NoRecommend;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002225 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002226 // OpenMP [2.16, Nesting of Regions]
2227 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002228 // OpenMP [2.8.1,simd Construct, Restrictions]
2229 // An ordered construct with the simd clause is the only OpenMP construct
2230 // that can appear in the simd region.
Alexey Bataev549210e2014-06-24 04:39:47 +00002231 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
2232 return true;
2233 }
Alexey Bataev0162e452014-07-22 10:10:35 +00002234 if (ParentRegion == OMPD_atomic) {
2235 // OpenMP [2.16, Nesting of Regions]
2236 // OpenMP constructs may not be nested inside an atomic region.
2237 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
2238 return true;
2239 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002240 if (CurrentRegion == OMPD_section) {
2241 // OpenMP [2.7.2, sections Construct, Restrictions]
2242 // Orphaned section directives are prohibited. That is, the section
2243 // directives must appear within the sections construct and must not be
2244 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002245 if (ParentRegion != OMPD_sections &&
2246 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002247 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
2248 << (ParentRegion != OMPD_unknown)
2249 << getOpenMPDirectiveName(ParentRegion);
2250 return true;
2251 }
2252 return false;
2253 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002254 // Allow some constructs to be orphaned (they could be used in functions,
2255 // called from OpenMP regions with the required preconditions).
2256 if (ParentRegion == OMPD_unknown)
2257 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00002258 if (CurrentRegion == OMPD_cancellation_point ||
2259 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002260 // OpenMP [2.16, Nesting of Regions]
2261 // A cancellation point construct for which construct-type-clause is
2262 // taskgroup must be nested inside a task construct. A cancellation
2263 // point construct for which construct-type-clause is not taskgroup must
2264 // be closely nested inside an OpenMP construct that matches the type
2265 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00002266 // A cancel construct for which construct-type-clause is taskgroup must be
2267 // nested inside a task construct. A cancel construct for which
2268 // construct-type-clause is not taskgroup must be closely nested inside an
2269 // OpenMP construct that matches the type specified in
2270 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002271 NestingProhibited =
2272 !((CancelRegion == OMPD_parallel && ParentRegion == OMPD_parallel) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00002273 (CancelRegion == OMPD_for &&
2274 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002275 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
2276 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00002277 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
2278 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002279 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00002280 // OpenMP [2.16, Nesting of Regions]
2281 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002282 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00002283 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002284 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002285 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002286 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
2287 // OpenMP [2.16, Nesting of Regions]
2288 // A critical region may not be nested (closely or otherwise) inside a
2289 // critical region with the same name. Note that this restriction is not
2290 // sufficient to prevent deadlock.
2291 SourceLocation PreviousCriticalLoc;
2292 bool DeadLock =
2293 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
2294 OpenMPDirectiveKind K,
2295 const DeclarationNameInfo &DNI,
2296 SourceLocation Loc)
2297 ->bool {
2298 if (K == OMPD_critical &&
2299 DNI.getName() == CurrentName.getName()) {
2300 PreviousCriticalLoc = Loc;
2301 return true;
2302 } else
2303 return false;
2304 },
2305 false /* skip top directive */);
2306 if (DeadLock) {
2307 SemaRef.Diag(StartLoc,
2308 diag::err_omp_prohibited_region_critical_same_name)
2309 << CurrentName.getName();
2310 if (PreviousCriticalLoc.isValid())
2311 SemaRef.Diag(PreviousCriticalLoc,
2312 diag::note_omp_previous_critical_region);
2313 return true;
2314 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002315 } else if (CurrentRegion == OMPD_barrier) {
2316 // OpenMP [2.16, Nesting of Regions]
2317 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002318 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002319 NestingProhibited =
2320 isOpenMPWorksharingDirective(ParentRegion) ||
2321 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002322 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002323 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musman80c22892014-07-17 08:54:58 +00002324 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00002325 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002326 // OpenMP [2.16, Nesting of Regions]
2327 // A worksharing region may not be closely nested inside a worksharing,
2328 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002329 NestingProhibited =
Alexander Musmanf82886e2014-09-18 05:12:34 +00002330 isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002331 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002332 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002333 isOpenMPTaskLoopDirective(ParentRegion);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002334 Recommend = ShouldBeInParallelRegion;
2335 } else if (CurrentRegion == OMPD_ordered) {
2336 // OpenMP [2.16, Nesting of Regions]
2337 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00002338 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002339 // An ordered region must be closely nested inside a loop region (or
2340 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002341 // OpenMP [2.8.1,simd Construct, Restrictions]
2342 // An ordered construct with the simd clause is the only OpenMP construct
2343 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002344 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00002345 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002346 isOpenMPTaskLoopDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002347 !(isOpenMPSimdDirective(ParentRegion) ||
2348 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002349 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002350 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
2351 // OpenMP [2.16, Nesting of Regions]
2352 // If specified, a teams construct must be contained within a target
2353 // construct.
2354 NestingProhibited = ParentRegion != OMPD_target;
2355 Recommend = ShouldBeInTargetRegion;
2356 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
2357 }
2358 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
2359 // OpenMP [2.16, Nesting of Regions]
2360 // distribute, parallel, parallel sections, parallel workshare, and the
2361 // parallel loop and parallel loop SIMD constructs are the only OpenMP
2362 // constructs that can be closely nested in the teams region.
Carlo Bertollib9bfa752015-12-08 04:21:03 +00002363 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
2364 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00002365 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002366 }
Carlo Bertollib9bfa752015-12-08 04:21:03 +00002367 if (!NestingProhibited && isOpenMPDistributeDirective(CurrentRegion)) {
2368 // OpenMP 4.5 [2.17 Nesting of Regions]
2369 // The region associated with the distribute construct must be strictly
2370 // nested inside a teams region
2371 NestingProhibited = !isOpenMPTeamsDirective(ParentRegion);
2372 Recommend = ShouldBeInTeamsRegion;
2373 }
Alexey Bataev549210e2014-06-24 04:39:47 +00002374 if (NestingProhibited) {
2375 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002376 << CloseNesting << getOpenMPDirectiveName(ParentRegion) << Recommend
2377 << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00002378 return true;
2379 }
2380 }
2381 return false;
2382}
2383
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002384static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
2385 ArrayRef<OMPClause *> Clauses,
2386 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
2387 bool ErrorFound = false;
2388 unsigned NamedModifiersNumber = 0;
2389 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
2390 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00002391 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002392 for (const auto *C : Clauses) {
2393 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
2394 // At most one if clause without a directive-name-modifier can appear on
2395 // the directive.
2396 OpenMPDirectiveKind CurNM = IC->getNameModifier();
2397 if (FoundNameModifiers[CurNM]) {
2398 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
2399 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
2400 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
2401 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002402 } else if (CurNM != OMPD_unknown) {
2403 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002404 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002405 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002406 FoundNameModifiers[CurNM] = IC;
2407 if (CurNM == OMPD_unknown)
2408 continue;
2409 // Check if the specified name modifier is allowed for the current
2410 // directive.
2411 // At most one if clause with the particular directive-name-modifier can
2412 // appear on the directive.
2413 bool MatchFound = false;
2414 for (auto NM : AllowedNameModifiers) {
2415 if (CurNM == NM) {
2416 MatchFound = true;
2417 break;
2418 }
2419 }
2420 if (!MatchFound) {
2421 S.Diag(IC->getNameModifierLoc(),
2422 diag::err_omp_wrong_if_directive_name_modifier)
2423 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
2424 ErrorFound = true;
2425 }
2426 }
2427 }
2428 // If any if clause on the directive includes a directive-name-modifier then
2429 // all if clauses on the directive must include a directive-name-modifier.
2430 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
2431 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
2432 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
2433 diag::err_omp_no_more_if_clause);
2434 } else {
2435 std::string Values;
2436 std::string Sep(", ");
2437 unsigned AllowedCnt = 0;
2438 unsigned TotalAllowedNum =
2439 AllowedNameModifiers.size() - NamedModifiersNumber;
2440 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
2441 ++Cnt) {
2442 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
2443 if (!FoundNameModifiers[NM]) {
2444 Values += "'";
2445 Values += getOpenMPDirectiveName(NM);
2446 Values += "'";
2447 if (AllowedCnt + 2 == TotalAllowedNum)
2448 Values += " or ";
2449 else if (AllowedCnt + 1 != TotalAllowedNum)
2450 Values += Sep;
2451 ++AllowedCnt;
2452 }
2453 }
2454 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
2455 diag::err_omp_unnamed_if_clause)
2456 << (TotalAllowedNum > 1) << Values;
2457 }
Alexey Bataevecb156a2015-09-15 17:23:56 +00002458 for (auto Loc : NameModifierLoc) {
2459 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
2460 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002461 ErrorFound = true;
2462 }
2463 return ErrorFound;
2464}
2465
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002466StmtResult Sema::ActOnOpenMPExecutableDirective(
2467 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
2468 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
2469 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002470 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002471 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
2472 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00002473 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002474
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002475 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev68446b72014-07-18 07:47:19 +00002476 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002477 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00002478 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00002479 if (AStmt) {
2480 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
2481
2482 // Check default data sharing attributes for referenced variables.
2483 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
2484 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
2485 if (DSAChecker.isErrorFound())
2486 return StmtError();
2487 // Generate list of implicitly defined firstprivate variables.
2488 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00002489
2490 if (!DSAChecker.getImplicitFirstprivate().empty()) {
2491 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
2492 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
2493 SourceLocation(), SourceLocation())) {
2494 ClausesWithImplicit.push_back(Implicit);
2495 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
2496 DSAChecker.getImplicitFirstprivate().size();
2497 } else
2498 ErrorFound = true;
2499 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002500 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002501
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002502 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002503 switch (Kind) {
2504 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00002505 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
2506 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002507 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002508 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002509 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002510 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2511 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002512 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002513 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002514 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2515 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002516 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00002517 case OMPD_for_simd:
2518 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2519 EndLoc, VarsWithInheritedDSA);
2520 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002521 case OMPD_sections:
2522 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
2523 EndLoc);
2524 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002525 case OMPD_section:
2526 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00002527 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002528 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
2529 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002530 case OMPD_single:
2531 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
2532 EndLoc);
2533 break;
Alexander Musman80c22892014-07-17 08:54:58 +00002534 case OMPD_master:
2535 assert(ClausesWithImplicit.empty() &&
2536 "No clauses are allowed for 'omp master' directive");
2537 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
2538 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002539 case OMPD_critical:
2540 assert(ClausesWithImplicit.empty() &&
2541 "No clauses are allowed for 'omp critical' directive");
2542 Res = ActOnOpenMPCriticalDirective(DirName, AStmt, StartLoc, EndLoc);
2543 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002544 case OMPD_parallel_for:
2545 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
2546 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002547 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00002548 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00002549 case OMPD_parallel_for_simd:
2550 Res = ActOnOpenMPParallelForSimdDirective(
2551 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002552 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00002553 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002554 case OMPD_parallel_sections:
2555 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
2556 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002557 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002558 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002559 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002560 Res =
2561 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002562 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002563 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00002564 case OMPD_taskyield:
2565 assert(ClausesWithImplicit.empty() &&
2566 "No clauses are allowed for 'omp taskyield' directive");
2567 assert(AStmt == nullptr &&
2568 "No associated statement allowed for 'omp taskyield' directive");
2569 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
2570 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002571 case OMPD_barrier:
2572 assert(ClausesWithImplicit.empty() &&
2573 "No clauses are allowed for 'omp barrier' directive");
2574 assert(AStmt == nullptr &&
2575 "No associated statement allowed for 'omp barrier' directive");
2576 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
2577 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00002578 case OMPD_taskwait:
2579 assert(ClausesWithImplicit.empty() &&
2580 "No clauses are allowed for 'omp taskwait' directive");
2581 assert(AStmt == nullptr &&
2582 "No associated statement allowed for 'omp taskwait' directive");
2583 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
2584 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002585 case OMPD_taskgroup:
2586 assert(ClausesWithImplicit.empty() &&
2587 "No clauses are allowed for 'omp taskgroup' directive");
2588 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
2589 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00002590 case OMPD_flush:
2591 assert(AStmt == nullptr &&
2592 "No associated statement allowed for 'omp flush' directive");
2593 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
2594 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002595 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00002596 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
2597 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002598 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00002599 case OMPD_atomic:
2600 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
2601 EndLoc);
2602 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002603 case OMPD_teams:
2604 Res =
2605 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
2606 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002607 case OMPD_target:
2608 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
2609 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002610 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002611 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002612 case OMPD_cancellation_point:
2613 assert(ClausesWithImplicit.empty() &&
2614 "No clauses are allowed for 'omp cancellation point' directive");
2615 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
2616 "cancellation point' directive");
2617 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
2618 break;
Alexey Bataev80909872015-07-02 11:25:17 +00002619 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00002620 assert(AStmt == nullptr &&
2621 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00002622 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
2623 CancelRegion);
2624 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00002625 break;
Michael Wong65f367f2015-07-21 13:44:28 +00002626 case OMPD_target_data:
2627 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
2628 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002629 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00002630 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00002631 case OMPD_taskloop:
2632 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
2633 EndLoc, VarsWithInheritedDSA);
2634 AllowedNameModifiers.push_back(OMPD_taskloop);
2635 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002636 case OMPD_taskloop_simd:
2637 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2638 EndLoc, VarsWithInheritedDSA);
2639 AllowedNameModifiers.push_back(OMPD_taskloop);
2640 break;
Carlo Bertollib9bfa752015-12-08 04:21:03 +00002641 case OMPD_distribute:
2642 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
2643 EndLoc, VarsWithInheritedDSA);
2644 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002645 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002646 llvm_unreachable("OpenMP Directive is not allowed");
2647 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002648 llvm_unreachable("Unknown OpenMP directive");
2649 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002650
Alexey Bataev4acb8592014-07-07 13:01:15 +00002651 for (auto P : VarsWithInheritedDSA) {
2652 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
2653 << P.first << P.second->getSourceRange();
2654 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002655 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
2656
2657 if (!AllowedNameModifiers.empty())
2658 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
2659 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002660
Alexey Bataeved09d242014-05-28 05:53:51 +00002661 if (ErrorFound)
2662 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002663 return Res;
2664}
2665
2666StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
2667 Stmt *AStmt,
2668 SourceLocation StartLoc,
2669 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002670 if (!AStmt)
2671 return StmtError();
2672
Alexey Bataev9959db52014-05-06 10:08:46 +00002673 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
2674 // 1.2.2 OpenMP Language Terminology
2675 // Structured block - An executable statement with a single entry at the
2676 // top and a single exit at the bottom.
2677 // The point of exit cannot be a branch out of the structured block.
2678 // longjmp() and throw() must not violate the entry/exit criteria.
2679 CS->getCapturedDecl()->setNothrow();
2680
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002681 getCurFunction()->setHasBranchProtectedScope();
2682
Alexey Bataev25e5b442015-09-15 12:52:43 +00002683 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
2684 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002685}
2686
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002687namespace {
2688/// \brief Helper class for checking canonical form of the OpenMP loops and
2689/// extracting iteration space of each loop in the loop nest, that will be used
2690/// for IR generation.
2691class OpenMPIterationSpaceChecker {
2692 /// \brief Reference to Sema.
2693 Sema &SemaRef;
2694 /// \brief A location for diagnostics (when there is no some better location).
2695 SourceLocation DefaultLoc;
2696 /// \brief A location for diagnostics (when increment is not compatible).
2697 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002698 /// \brief A source location for referring to loop init later.
2699 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002700 /// \brief A source location for referring to condition later.
2701 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002702 /// \brief A source location for referring to increment later.
2703 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002704 /// \brief Loop variable.
2705 VarDecl *Var;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002706 /// \brief Reference to loop variable.
2707 DeclRefExpr *VarRef;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002708 /// \brief Lower bound (initializer for the var).
2709 Expr *LB;
2710 /// \brief Upper bound.
2711 Expr *UB;
2712 /// \brief Loop step (increment).
2713 Expr *Step;
2714 /// \brief This flag is true when condition is one of:
2715 /// Var < UB
2716 /// Var <= UB
2717 /// UB > Var
2718 /// UB >= Var
2719 bool TestIsLessOp;
2720 /// \brief This flag is true when condition is strict ( < or > ).
2721 bool TestIsStrictOp;
2722 /// \brief This flag is true when step is subtracted on each iteration.
2723 bool SubtractStep;
2724
2725public:
2726 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
2727 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
Alexander Musmana5f070a2014-10-01 06:03:56 +00002728 InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()),
2729 IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002730 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
2731 TestIsStrictOp(false), SubtractStep(false) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002732 /// \brief Check init-expr for canonical loop form and save loop counter
2733 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00002734 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002735 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
2736 /// for less/greater and for strict/non-strict comparison.
2737 bool CheckCond(Expr *S);
2738 /// \brief Check incr-expr for canonical loop form and return true if it
2739 /// does not conform, otherwise save loop step (#Step).
2740 bool CheckInc(Expr *S);
2741 /// \brief Return the loop counter variable.
2742 VarDecl *GetLoopVar() const { return Var; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002743 /// \brief Return the reference expression to loop counter variable.
2744 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002745 /// \brief Source range of the loop init.
2746 SourceRange GetInitSrcRange() const { return InitSrcRange; }
2747 /// \brief Source range of the loop condition.
2748 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
2749 /// \brief Source range of the loop increment.
2750 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
2751 /// \brief True if the step should be subtracted.
2752 bool ShouldSubtractStep() const { return SubtractStep; }
2753 /// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00002754 Expr *BuildNumIterations(Scope *S, const bool LimitedType) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00002755 /// \brief Build the precondition expression for the loops.
2756 Expr *BuildPreCond(Scope *S, Expr *Cond) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002757 /// \brief Build reference expression to the counter be used for codegen.
2758 Expr *BuildCounterVar() const;
Alexey Bataeva8899172015-08-06 12:30:57 +00002759 /// \brief Build reference expression to the private counter be used for
2760 /// codegen.
2761 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002762 /// \brief Build initization of the counter be used for codegen.
2763 Expr *BuildCounterInit() const;
2764 /// \brief Build step of the counter be used for codegen.
2765 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002766 /// \brief Return true if any expression is dependent.
2767 bool Dependent() const;
2768
2769private:
2770 /// \brief Check the right-hand side of an assignment in the increment
2771 /// expression.
2772 bool CheckIncRHS(Expr *RHS);
2773 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002774 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002775 /// \brief Helper to set upper bound.
Craig Toppere335f252015-10-04 04:53:55 +00002776 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00002777 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002778 /// \brief Helper to set loop increment.
2779 bool SetStep(Expr *NewStep, bool Subtract);
2780};
2781
2782bool OpenMPIterationSpaceChecker::Dependent() const {
2783 if (!Var) {
2784 assert(!LB && !UB && !Step);
2785 return false;
2786 }
2787 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
2788 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
2789}
2790
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002791template <typename T>
2792static T *getExprAsWritten(T *E) {
2793 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
2794 E = ExprTemp->getSubExpr();
2795
2796 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
2797 E = MTE->GetTemporaryExpr();
2798
2799 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
2800 E = Binder->getSubExpr();
2801
2802 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
2803 E = ICE->getSubExprAsWritten();
2804 return E->IgnoreParens();
2805}
2806
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002807bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
2808 DeclRefExpr *NewVarRefExpr,
2809 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002810 // State consistency checking to ensure correct usage.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002811 assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
2812 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002813 if (!NewVar || !NewLB)
2814 return true;
2815 Var = NewVar;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002816 VarRef = NewVarRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002817 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
2818 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00002819 if ((Ctor->isCopyOrMoveConstructor() ||
2820 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
2821 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002822 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002823 LB = NewLB;
2824 return false;
2825}
2826
2827bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00002828 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002829 // State consistency checking to ensure correct usage.
2830 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
2831 !TestIsLessOp && !TestIsStrictOp);
2832 if (!NewUB)
2833 return true;
2834 UB = NewUB;
2835 TestIsLessOp = LessOp;
2836 TestIsStrictOp = StrictOp;
2837 ConditionSrcRange = SR;
2838 ConditionLoc = SL;
2839 return false;
2840}
2841
2842bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
2843 // State consistency checking to ensure correct usage.
2844 assert(Var != nullptr && LB != nullptr && Step == nullptr);
2845 if (!NewStep)
2846 return true;
2847 if (!NewStep->isValueDependent()) {
2848 // Check that the step is integer expression.
2849 SourceLocation StepLoc = NewStep->getLocStart();
2850 ExprResult Val =
2851 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
2852 if (Val.isInvalid())
2853 return true;
2854 NewStep = Val.get();
2855
2856 // OpenMP [2.6, Canonical Loop Form, Restrictions]
2857 // If test-expr is of form var relational-op b and relational-op is < or
2858 // <= then incr-expr must cause var to increase on each iteration of the
2859 // loop. If test-expr is of form var relational-op b and relational-op is
2860 // > or >= then incr-expr must cause var to decrease on each iteration of
2861 // the loop.
2862 // If test-expr is of form b relational-op var and relational-op is < or
2863 // <= then incr-expr must cause var to decrease on each iteration of the
2864 // loop. If test-expr is of form b relational-op var and relational-op is
2865 // > or >= then incr-expr must cause var to increase on each iteration of
2866 // the loop.
2867 llvm::APSInt Result;
2868 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
2869 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
2870 bool IsConstNeg =
2871 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00002872 bool IsConstPos =
2873 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002874 bool IsConstZero = IsConstant && !Result.getBoolValue();
2875 if (UB && (IsConstZero ||
2876 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00002877 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002878 SemaRef.Diag(NewStep->getExprLoc(),
2879 diag::err_omp_loop_incr_not_compatible)
2880 << Var << TestIsLessOp << NewStep->getSourceRange();
2881 SemaRef.Diag(ConditionLoc,
2882 diag::note_omp_loop_cond_requres_compatible_incr)
2883 << TestIsLessOp << ConditionSrcRange;
2884 return true;
2885 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002886 if (TestIsLessOp == Subtract) {
2887 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
2888 NewStep).get();
2889 Subtract = !Subtract;
2890 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002891 }
2892
2893 Step = NewStep;
2894 SubtractStep = Subtract;
2895 return false;
2896}
2897
Alexey Bataev9c821032015-04-30 04:23:23 +00002898bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002899 // Check init-expr for canonical loop form and save loop counter
2900 // variable - #Var and its initialization value - #LB.
2901 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
2902 // var = lb
2903 // integer-type var = lb
2904 // random-access-iterator-type var = lb
2905 // pointer-type var = lb
2906 //
2907 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00002908 if (EmitDiags) {
2909 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
2910 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002911 return true;
2912 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002913 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002914 if (Expr *E = dyn_cast<Expr>(S))
2915 S = E->IgnoreParens();
2916 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2917 if (BO->getOpcode() == BO_Assign)
2918 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002919 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002920 BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002921 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
2922 if (DS->isSingleDecl()) {
2923 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00002924 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002925 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00002926 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002927 SemaRef.Diag(S->getLocStart(),
2928 diag::ext_omp_loop_not_canonical_init)
2929 << S->getSourceRange();
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002930 return SetVarAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002931 }
2932 }
2933 }
2934 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
2935 if (CE->getOperator() == OO_Equal)
2936 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002937 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
2938 CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002939
Alexey Bataev9c821032015-04-30 04:23:23 +00002940 if (EmitDiags) {
2941 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
2942 << S->getSourceRange();
2943 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002944 return true;
2945}
2946
Alexey Bataev23b69422014-06-18 07:08:49 +00002947/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002948/// variable (which may be the loop variable) if possible.
2949static const VarDecl *GetInitVarDecl(const Expr *E) {
2950 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00002951 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002952 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002953 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
2954 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00002955 if ((Ctor->isCopyOrMoveConstructor() ||
2956 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
2957 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002958 E = CE->getArg(0)->IgnoreParenImpCasts();
2959 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
2960 if (!DRE)
2961 return nullptr;
2962 return dyn_cast<VarDecl>(DRE->getDecl());
2963}
2964
2965bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
2966 // Check test-expr for canonical form, save upper-bound UB, flags for
2967 // less/greater and for strict/non-strict comparison.
2968 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2969 // var relational-op b
2970 // b relational-op var
2971 //
2972 if (!S) {
2973 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
2974 return true;
2975 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002976 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002977 SourceLocation CondLoc = S->getLocStart();
2978 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2979 if (BO->isRelationalOp()) {
2980 if (GetInitVarDecl(BO->getLHS()) == Var)
2981 return SetUB(BO->getRHS(),
2982 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
2983 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2984 BO->getSourceRange(), BO->getOperatorLoc());
2985 if (GetInitVarDecl(BO->getRHS()) == Var)
2986 return SetUB(BO->getLHS(),
2987 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
2988 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2989 BO->getSourceRange(), BO->getOperatorLoc());
2990 }
2991 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2992 if (CE->getNumArgs() == 2) {
2993 auto Op = CE->getOperator();
2994 switch (Op) {
2995 case OO_Greater:
2996 case OO_GreaterEqual:
2997 case OO_Less:
2998 case OO_LessEqual:
2999 if (GetInitVarDecl(CE->getArg(0)) == Var)
3000 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
3001 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3002 CE->getOperatorLoc());
3003 if (GetInitVarDecl(CE->getArg(1)) == Var)
3004 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
3005 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3006 CE->getOperatorLoc());
3007 break;
3008 default:
3009 break;
3010 }
3011 }
3012 }
3013 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
3014 << S->getSourceRange() << Var;
3015 return true;
3016}
3017
3018bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
3019 // RHS of canonical loop form increment can be:
3020 // var + incr
3021 // incr + var
3022 // var - incr
3023 //
3024 RHS = RHS->IgnoreParenImpCasts();
3025 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
3026 if (BO->isAdditiveOp()) {
3027 bool IsAdd = BO->getOpcode() == BO_Add;
3028 if (GetInitVarDecl(BO->getLHS()) == Var)
3029 return SetStep(BO->getRHS(), !IsAdd);
3030 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
3031 return SetStep(BO->getLHS(), false);
3032 }
3033 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
3034 bool IsAdd = CE->getOperator() == OO_Plus;
3035 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
3036 if (GetInitVarDecl(CE->getArg(0)) == Var)
3037 return SetStep(CE->getArg(1), !IsAdd);
3038 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
3039 return SetStep(CE->getArg(0), false);
3040 }
3041 }
3042 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3043 << RHS->getSourceRange() << Var;
3044 return true;
3045}
3046
3047bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
3048 // Check incr-expr for canonical loop form and return true if it
3049 // does not conform.
3050 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3051 // ++var
3052 // var++
3053 // --var
3054 // var--
3055 // var += incr
3056 // var -= incr
3057 // var = var + incr
3058 // var = incr + var
3059 // var = var - incr
3060 //
3061 if (!S) {
3062 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
3063 return true;
3064 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003065 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003066 S = S->IgnoreParens();
3067 if (auto UO = dyn_cast<UnaryOperator>(S)) {
3068 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
3069 return SetStep(
3070 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
3071 (UO->isDecrementOp() ? -1 : 1)).get(),
3072 false);
3073 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
3074 switch (BO->getOpcode()) {
3075 case BO_AddAssign:
3076 case BO_SubAssign:
3077 if (GetInitVarDecl(BO->getLHS()) == Var)
3078 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
3079 break;
3080 case BO_Assign:
3081 if (GetInitVarDecl(BO->getLHS()) == Var)
3082 return CheckIncRHS(BO->getRHS());
3083 break;
3084 default:
3085 break;
3086 }
3087 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3088 switch (CE->getOperator()) {
3089 case OO_PlusPlus:
3090 case OO_MinusMinus:
3091 if (GetInitVarDecl(CE->getArg(0)) == Var)
3092 return SetStep(
3093 SemaRef.ActOnIntegerConstant(
3094 CE->getLocStart(),
3095 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
3096 false);
3097 break;
3098 case OO_PlusEqual:
3099 case OO_MinusEqual:
3100 if (GetInitVarDecl(CE->getArg(0)) == Var)
3101 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
3102 break;
3103 case OO_Equal:
3104 if (GetInitVarDecl(CE->getArg(0)) == Var)
3105 return CheckIncRHS(CE->getArg(1));
3106 break;
3107 default:
3108 break;
3109 }
3110 }
3111 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3112 << S->getSourceRange() << Var;
3113 return true;
3114}
Alexander Musmana5f070a2014-10-01 06:03:56 +00003115
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003116namespace {
3117// Transform variables declared in GNU statement expressions to new ones to
3118// avoid crash on codegen.
3119class TransformToNewDefs : public TreeTransform<TransformToNewDefs> {
3120 typedef TreeTransform<TransformToNewDefs> BaseTransform;
3121
3122public:
3123 TransformToNewDefs(Sema &SemaRef) : BaseTransform(SemaRef) {}
3124
3125 Decl *TransformDefinition(SourceLocation Loc, Decl *D) {
3126 if (auto *VD = cast<VarDecl>(D))
3127 if (!isa<ParmVarDecl>(D) && !isa<VarTemplateSpecializationDecl>(D) &&
3128 !isa<ImplicitParamDecl>(D)) {
3129 auto *NewVD = VarDecl::Create(
3130 SemaRef.Context, VD->getDeclContext(), VD->getLocStart(),
3131 VD->getLocation(), VD->getIdentifier(), VD->getType(),
3132 VD->getTypeSourceInfo(), VD->getStorageClass());
3133 NewVD->setTSCSpec(VD->getTSCSpec());
3134 NewVD->setInit(VD->getInit());
3135 NewVD->setInitStyle(VD->getInitStyle());
3136 NewVD->setExceptionVariable(VD->isExceptionVariable());
3137 NewVD->setNRVOVariable(VD->isNRVOVariable());
3138 NewVD->setCXXForRangeDecl(VD->isInExternCXXContext());
3139 NewVD->setConstexpr(VD->isConstexpr());
3140 NewVD->setInitCapture(VD->isInitCapture());
3141 NewVD->setPreviousDeclInSameBlockScope(
3142 VD->isPreviousDeclInSameBlockScope());
3143 VD->getDeclContext()->addHiddenDecl(NewVD);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003144 if (VD->hasAttrs())
3145 NewVD->setAttrs(VD->getAttrs());
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003146 transformedLocalDecl(VD, NewVD);
3147 return NewVD;
3148 }
3149 return BaseTransform::TransformDefinition(Loc, D);
3150 }
3151
3152 ExprResult TransformDeclRefExpr(DeclRefExpr *E) {
3153 if (auto *NewD = TransformDecl(E->getExprLoc(), E->getDecl()))
3154 if (E->getDecl() != NewD) {
3155 NewD->setReferenced();
3156 NewD->markUsed(SemaRef.Context);
3157 return DeclRefExpr::Create(
3158 SemaRef.Context, E->getQualifierLoc(), E->getTemplateKeywordLoc(),
3159 cast<ValueDecl>(NewD), E->refersToEnclosingVariableOrCapture(),
3160 E->getNameInfo(), E->getType(), E->getValueKind());
3161 }
3162 return BaseTransform::TransformDeclRefExpr(E);
3163 }
3164};
3165}
3166
Alexander Musmana5f070a2014-10-01 06:03:56 +00003167/// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00003168Expr *
3169OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S,
3170 const bool LimitedType) const {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003171 TransformToNewDefs Transform(SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003172 ExprResult Diff;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003173 auto VarType = Var->getType().getNonReferenceType();
3174 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003175 SemaRef.getLangOpts().CPlusPlus) {
3176 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003177 auto *UBExpr = TestIsLessOp ? UB : LB;
3178 auto *LBExpr = TestIsLessOp ? LB : UB;
3179 Expr *Upper = Transform.TransformExpr(UBExpr).get();
3180 Expr *Lower = Transform.TransformExpr(LBExpr).get();
3181 if (!Upper || !Lower)
3182 return nullptr;
3183 Upper = SemaRef.PerformImplicitConversion(Upper, UBExpr->getType(),
3184 Sema::AA_Converting,
3185 /*AllowExplicit=*/true)
3186 .get();
3187 Lower = SemaRef.PerformImplicitConversion(Lower, LBExpr->getType(),
3188 Sema::AA_Converting,
3189 /*AllowExplicit=*/true)
3190 .get();
3191 if (!Upper || !Lower)
3192 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003193
3194 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
3195
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003196 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00003197 // BuildBinOp already emitted error, this one is to point user to upper
3198 // and lower bound, and to tell what is passed to 'operator-'.
3199 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
3200 << Upper->getSourceRange() << Lower->getSourceRange();
3201 return nullptr;
3202 }
3203 }
3204
3205 if (!Diff.isUsable())
3206 return nullptr;
3207
3208 // Upper - Lower [- 1]
3209 if (TestIsStrictOp)
3210 Diff = SemaRef.BuildBinOp(
3211 S, DefaultLoc, BO_Sub, Diff.get(),
3212 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3213 if (!Diff.isUsable())
3214 return nullptr;
3215
3216 // Upper - Lower [- 1] + Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003217 auto NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
3218 if (NewStep.isInvalid())
3219 return nullptr;
3220 NewStep = SemaRef.PerformImplicitConversion(
3221 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
3222 /*AllowExplicit=*/true);
3223 if (NewStep.isInvalid())
3224 return nullptr;
3225 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003226 if (!Diff.isUsable())
3227 return nullptr;
3228
3229 // Parentheses (for dumping/debugging purposes only).
3230 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
3231 if (!Diff.isUsable())
3232 return nullptr;
3233
3234 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003235 NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
3236 if (NewStep.isInvalid())
3237 return nullptr;
3238 NewStep = SemaRef.PerformImplicitConversion(
3239 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
3240 /*AllowExplicit=*/true);
3241 if (NewStep.isInvalid())
3242 return nullptr;
3243 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003244 if (!Diff.isUsable())
3245 return nullptr;
3246
Alexander Musman174b3ca2014-10-06 11:16:29 +00003247 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003248 QualType Type = Diff.get()->getType();
3249 auto &C = SemaRef.Context;
3250 bool UseVarType = VarType->hasIntegerRepresentation() &&
3251 C.getTypeSize(Type) > C.getTypeSize(VarType);
3252 if (!Type->isIntegerType() || UseVarType) {
3253 unsigned NewSize =
3254 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
3255 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
3256 : Type->hasSignedIntegerRepresentation();
3257 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
3258 Diff = SemaRef.PerformImplicitConversion(
3259 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
3260 if (!Diff.isUsable())
3261 return nullptr;
3262 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00003263 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00003264 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
3265 if (NewSize != C.getTypeSize(Type)) {
3266 if (NewSize < C.getTypeSize(Type)) {
3267 assert(NewSize == 64 && "incorrect loop var size");
3268 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
3269 << InitSrcRange << ConditionSrcRange;
3270 }
3271 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003272 NewSize, Type->hasSignedIntegerRepresentation() ||
3273 C.getTypeSize(Type) < NewSize);
Alexander Musman174b3ca2014-10-06 11:16:29 +00003274 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
3275 Sema::AA_Converting, true);
3276 if (!Diff.isUsable())
3277 return nullptr;
3278 }
3279 }
3280
Alexander Musmana5f070a2014-10-01 06:03:56 +00003281 return Diff.get();
3282}
3283
Alexey Bataev62dbb972015-04-22 11:59:37 +00003284Expr *OpenMPIterationSpaceChecker::BuildPreCond(Scope *S, Expr *Cond) const {
3285 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
3286 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
3287 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003288 TransformToNewDefs Transform(SemaRef);
3289
3290 auto NewLB = Transform.TransformExpr(LB);
3291 auto NewUB = Transform.TransformExpr(UB);
3292 if (NewLB.isInvalid() || NewUB.isInvalid())
3293 return Cond;
3294 NewLB = SemaRef.PerformImplicitConversion(NewLB.get(), LB->getType(),
3295 Sema::AA_Converting,
3296 /*AllowExplicit=*/true);
3297 NewUB = SemaRef.PerformImplicitConversion(NewUB.get(), UB->getType(),
3298 Sema::AA_Converting,
3299 /*AllowExplicit=*/true);
3300 if (NewLB.isInvalid() || NewUB.isInvalid())
3301 return Cond;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003302 auto CondExpr = SemaRef.BuildBinOp(
3303 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
3304 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003305 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003306 if (CondExpr.isUsable()) {
3307 CondExpr = SemaRef.PerformImplicitConversion(
3308 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
3309 /*AllowExplicit=*/true);
3310 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00003311 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
3312 // Otherwise use original loop conditon and evaluate it in runtime.
3313 return CondExpr.isUsable() ? CondExpr.get() : Cond;
3314}
3315
Alexander Musmana5f070a2014-10-01 06:03:56 +00003316/// \brief Build reference expression to the counter be used for codegen.
3317Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const {
Alexey Bataeva8899172015-08-06 12:30:57 +00003318 return buildDeclRefExpr(SemaRef, Var, Var->getType().getNonReferenceType(),
3319 DefaultLoc);
3320}
3321
3322Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
3323 if (Var && !Var->isInvalidDecl()) {
3324 auto Type = Var->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003325 auto *PrivateVar =
3326 buildVarDecl(SemaRef, DefaultLoc, Type, Var->getName(),
3327 Var->hasAttrs() ? &Var->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00003328 if (PrivateVar->isInvalidDecl())
3329 return nullptr;
3330 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
3331 }
3332 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003333}
3334
3335/// \brief Build initization of the counter be used for codegen.
3336Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
3337
3338/// \brief Build step of the counter be used for codegen.
3339Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
3340
3341/// \brief Iteration space of a single for loop.
3342struct LoopIterationSpace {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003343 /// \brief Condition of the loop.
3344 Expr *PreCond;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003345 /// \brief This expression calculates the number of iterations in the loop.
3346 /// It is always possible to calculate it before starting the loop.
3347 Expr *NumIterations;
3348 /// \brief The loop counter variable.
3349 Expr *CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00003350 /// \brief Private loop counter variable.
3351 Expr *PrivateCounterVar;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003352 /// \brief This is initializer for the initial value of #CounterVar.
3353 Expr *CounterInit;
3354 /// \brief This is step for the #CounterVar used to generate its update:
3355 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
3356 Expr *CounterStep;
3357 /// \brief Should step be subtracted?
3358 bool Subtract;
3359 /// \brief Source range of the loop init.
3360 SourceRange InitSrcRange;
3361 /// \brief Source range of the loop condition.
3362 SourceRange CondSrcRange;
3363 /// \brief Source range of the loop increment.
3364 SourceRange IncSrcRange;
3365};
3366
Alexey Bataev23b69422014-06-18 07:08:49 +00003367} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003368
Alexey Bataev9c821032015-04-30 04:23:23 +00003369void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
3370 assert(getLangOpts().OpenMP && "OpenMP is not active.");
3371 assert(Init && "Expected loop in canonical form.");
3372 unsigned CollapseIteration = DSAStack->getCollapseNumber();
3373 if (CollapseIteration > 0 &&
3374 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
3375 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
3376 if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
3377 DSAStack->addLoopControlVariable(ISC.GetLoopVar());
3378 }
3379 DSAStack->setCollapseNumber(CollapseIteration - 1);
3380 }
3381}
3382
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003383/// \brief Called on a for stmt to check and extract its iteration space
3384/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00003385static bool CheckOpenMPIterationSpace(
3386 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
3387 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003388 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003389 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
3390 LoopIterationSpace &ResultIterSpace) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003391 // OpenMP [2.6, Canonical Loop Form]
3392 // for (init-expr; test-expr; incr-expr) structured-block
3393 auto For = dyn_cast_or_null<ForStmt>(S);
3394 if (!For) {
3395 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003396 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
3397 << getOpenMPDirectiveName(DKind) << NestedLoopCount
3398 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
3399 if (NestedLoopCount > 1) {
3400 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
3401 SemaRef.Diag(DSA.getConstructLoc(),
3402 diag::note_omp_collapse_ordered_expr)
3403 << 2 << CollapseLoopCountExpr->getSourceRange()
3404 << OrderedLoopCountExpr->getSourceRange();
3405 else if (CollapseLoopCountExpr)
3406 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
3407 diag::note_omp_collapse_ordered_expr)
3408 << 0 << CollapseLoopCountExpr->getSourceRange();
3409 else
3410 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
3411 diag::note_omp_collapse_ordered_expr)
3412 << 1 << OrderedLoopCountExpr->getSourceRange();
3413 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003414 return true;
3415 }
3416 assert(For->getBody());
3417
3418 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
3419
3420 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003421 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003422 if (ISC.CheckInit(Init)) {
3423 return true;
3424 }
3425
3426 bool HasErrors = false;
3427
3428 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003429 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003430
3431 // OpenMP [2.6, Canonical Loop Form]
3432 // Var is one of the following:
3433 // A variable of signed or unsigned integer type.
3434 // For C++, a variable of a random access iterator type.
3435 // For C, a variable of a pointer type.
Alexey Bataeva8899172015-08-06 12:30:57 +00003436 auto VarType = Var->getType().getNonReferenceType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003437 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
3438 !VarType->isPointerType() &&
3439 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
3440 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
3441 << SemaRef.getLangOpts().CPlusPlus;
3442 HasErrors = true;
3443 }
3444
Alexey Bataev4acb8592014-07-07 13:01:15 +00003445 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
3446 // Construct
3447 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3448 // parallel for construct is (are) private.
3449 // The loop iteration variable in the associated for-loop of a simd construct
3450 // with just one associated for-loop is linear with a constant-linear-step
3451 // that is the increment of the associated for-loop.
3452 // Exclude loop var from the list of variables with implicitly defined data
3453 // sharing attributes.
Benjamin Kramerad8e0792014-10-10 15:32:48 +00003454 VarsWithImplicitDSA.erase(Var);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003455
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003456 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
3457 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00003458 // The loop iteration variable in the associated for-loop of a simd construct
3459 // with just one associated for-loop may be listed in a linear clause with a
3460 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00003461 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3462 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003463 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003464 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
3465 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
3466 // declared in the loop and it is predetermined as a private.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003467 auto PredeterminedCKind =
3468 isOpenMPSimdDirective(DKind)
3469 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
3470 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003471 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataev0c024df2015-05-12 09:02:07 +00003472 DVar.CKind != OMPC_threadprivate && DVar.CKind != PredeterminedCKind) ||
Carlo Bertollib9bfa752015-12-08 04:21:03 +00003473 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
3474 isOpenMPDistributeDirective(DKind)) &&
Alexey Bataev49f6e782015-12-01 04:18:41 +00003475 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
3476 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate &&
3477 DVar.CKind != OMPC_threadprivate)) &&
Alexey Bataev0c024df2015-05-12 09:02:07 +00003478 ((DVar.CKind != OMPC_private && DVar.CKind != OMPC_threadprivate) ||
3479 DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003480 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00003481 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
3482 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataevf2453a02015-05-06 07:25:08 +00003483 if (DVar.RefExpr == nullptr)
3484 DVar.CKind = PredeterminedCKind;
3485 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, /*IsLoopIterVar=*/true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003486 HasErrors = true;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003487 } else if (LoopVarRefExpr != nullptr) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00003488 // Make the loop iteration variable private (for worksharing constructs),
3489 // linear (for simd directives with the only one associated loop) or
Alexey Bataev10e775f2015-07-30 11:36:16 +00003490 // lastprivate (for simd directives with several collapsed or ordered
3491 // loops).
Alexey Bataev9aba41c2014-11-14 04:08:45 +00003492 if (DVar.CKind == OMPC_unknown)
3493 DVar = DSA.hasDSA(Var, isOpenMPPrivate, MatchesAlways(),
3494 /*FromParent=*/false);
Alexey Bataev9c821032015-04-30 04:23:23 +00003495 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003496 }
3497
Alexey Bataev7ff55242014-06-19 09:13:45 +00003498 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00003499
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003500 // Check test-expr.
3501 HasErrors |= ISC.CheckCond(For->getCond());
3502
3503 // Check incr-expr.
3504 HasErrors |= ISC.CheckInc(For->getInc());
3505
Alexander Musmana5f070a2014-10-01 06:03:56 +00003506 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003507 return HasErrors;
3508
Alexander Musmana5f070a2014-10-01 06:03:56 +00003509 // Build the loop's iteration space representation.
Alexey Bataev62dbb972015-04-22 11:59:37 +00003510 ResultIterSpace.PreCond = ISC.BuildPreCond(DSA.getCurScope(), For->getCond());
Alexander Musman174b3ca2014-10-06 11:16:29 +00003511 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003512 DSA.getCurScope(), (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertollib9bfa752015-12-08 04:21:03 +00003513 isOpenMPTaskLoopDirective(DKind) ||
3514 isOpenMPDistributeDirective(DKind)));
Alexander Musmana5f070a2014-10-01 06:03:56 +00003515 ResultIterSpace.CounterVar = ISC.BuildCounterVar();
Alexey Bataeva8899172015-08-06 12:30:57 +00003516 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003517 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
3518 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
3519 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
3520 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
3521 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
3522 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
3523
Alexey Bataev62dbb972015-04-22 11:59:37 +00003524 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
3525 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003526 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00003527 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003528 ResultIterSpace.CounterInit == nullptr ||
3529 ResultIterSpace.CounterStep == nullptr);
3530
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003531 return HasErrors;
3532}
3533
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003534/// \brief Build 'VarRef = Start.
3535static ExprResult BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc,
3536 ExprResult VarRef, ExprResult Start) {
3537 TransformToNewDefs Transform(SemaRef);
3538 // Build 'VarRef = Start.
3539 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
3540 if (NewStart.isInvalid())
3541 return ExprError();
3542 NewStart = SemaRef.PerformImplicitConversion(
3543 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
3544 Sema::AA_Converting,
3545 /*AllowExplicit=*/true);
3546 if (NewStart.isInvalid())
3547 return ExprError();
3548 NewStart = SemaRef.PerformImplicitConversion(
3549 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
3550 /*AllowExplicit=*/true);
3551 if (!NewStart.isUsable())
3552 return ExprError();
3553
3554 auto Init =
3555 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
3556 return Init;
3557}
3558
Alexander Musmana5f070a2014-10-01 06:03:56 +00003559/// \brief Build 'VarRef = Start + Iter * Step'.
3560static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S,
3561 SourceLocation Loc, ExprResult VarRef,
3562 ExprResult Start, ExprResult Iter,
3563 ExprResult Step, bool Subtract) {
3564 // Add parentheses (for debugging purposes only).
3565 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
3566 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
3567 !Step.isUsable())
3568 return ExprError();
3569
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003570 TransformToNewDefs Transform(SemaRef);
3571 auto NewStep = Transform.TransformExpr(Step.get()->IgnoreImplicit());
3572 if (NewStep.isInvalid())
3573 return ExprError();
3574 NewStep = SemaRef.PerformImplicitConversion(
3575 NewStep.get(), Step.get()->IgnoreImplicit()->getType(),
3576 Sema::AA_Converting,
3577 /*AllowExplicit=*/true);
3578 if (NewStep.isInvalid())
3579 return ExprError();
3580 ExprResult Update =
3581 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003582 if (!Update.isUsable())
3583 return ExprError();
3584
3585 // Build 'VarRef = Start + Iter * Step'.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003586 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
3587 if (NewStart.isInvalid())
3588 return ExprError();
3589 NewStart = SemaRef.PerformImplicitConversion(
3590 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
3591 Sema::AA_Converting,
3592 /*AllowExplicit=*/true);
3593 if (NewStart.isInvalid())
3594 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003595 Update = SemaRef.BuildBinOp(S, Loc, (Subtract ? BO_Sub : BO_Add),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003596 NewStart.get(), Update.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003597 if (!Update.isUsable())
3598 return ExprError();
3599
3600 Update = SemaRef.PerformImplicitConversion(
3601 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
3602 if (!Update.isUsable())
3603 return ExprError();
3604
3605 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
3606 return Update;
3607}
3608
3609/// \brief Convert integer expression \a E to make it have at least \a Bits
3610/// bits.
3611static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
3612 Sema &SemaRef) {
3613 if (E == nullptr)
3614 return ExprError();
3615 auto &C = SemaRef.Context;
3616 QualType OldType = E->getType();
3617 unsigned HasBits = C.getTypeSize(OldType);
3618 if (HasBits >= Bits)
3619 return ExprResult(E);
3620 // OK to convert to signed, because new type has more bits than old.
3621 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
3622 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
3623 true);
3624}
3625
3626/// \brief Check if the given expression \a E is a constant integer that fits
3627/// into \a Bits bits.
3628static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
3629 if (E == nullptr)
3630 return false;
3631 llvm::APSInt Result;
3632 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
3633 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
3634 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003635}
3636
3637/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00003638/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
3639/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003640static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00003641CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
3642 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
3643 DSAStackTy &DSA,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003644 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00003645 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003646 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003647 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003648 // Found 'collapse' clause - calculate collapse number.
3649 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003650 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00003651 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00003652 }
3653 if (OrderedLoopCountExpr) {
3654 // Found 'ordered' clause - calculate collapse number.
3655 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00003656 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
3657 if (Result.getLimitedValue() < NestedLoopCount) {
3658 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
3659 diag::err_omp_wrong_ordered_loop_count)
3660 << OrderedLoopCountExpr->getSourceRange();
3661 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
3662 diag::note_collapse_loop_count)
3663 << CollapseLoopCountExpr->getSourceRange();
3664 }
3665 NestedLoopCount = Result.getLimitedValue();
3666 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003667 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003668 // This is helper routine for loop directives (e.g., 'for', 'simd',
3669 // 'for simd', etc.).
Alexander Musmana5f070a2014-10-01 06:03:56 +00003670 SmallVector<LoopIterationSpace, 4> IterSpaces;
3671 IterSpaces.resize(NestedLoopCount);
3672 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003673 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003674 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003675 NestedLoopCount, CollapseLoopCountExpr,
3676 OrderedLoopCountExpr, VarsWithImplicitDSA,
3677 IterSpaces[Cnt]))
Alexey Bataevabfc0692014-06-25 06:52:00 +00003678 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003679 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003680 // OpenMP [2.8.1, simd construct, Restrictions]
3681 // All loops associated with the construct must be perfectly nested; that
3682 // is, there must be no intervening code nor any OpenMP directive between
3683 // any two loops.
3684 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003685 }
3686
Alexander Musmana5f070a2014-10-01 06:03:56 +00003687 Built.clear(/* size */ NestedLoopCount);
3688
3689 if (SemaRef.CurContext->isDependentContext())
3690 return NestedLoopCount;
3691
3692 // An example of what is generated for the following code:
3693 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00003694 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00003695 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003696 // for (k = 0; k < NK; ++k)
3697 // for (j = J0; j < NJ; j+=2) {
3698 // <loop body>
3699 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003700 //
3701 // We generate the code below.
3702 // Note: the loop body may be outlined in CodeGen.
3703 // Note: some counters may be C++ classes, operator- is used to find number of
3704 // iterations and operator+= to calculate counter value.
3705 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
3706 // or i64 is currently supported).
3707 //
3708 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
3709 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
3710 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
3711 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
3712 // // similar updates for vars in clauses (e.g. 'linear')
3713 // <loop body (using local i and j)>
3714 // }
3715 // i = NI; // assign final values of counters
3716 // j = NJ;
3717 //
3718
3719 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
3720 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00003721 // Precondition tests if there is at least one iteration (all conditions are
3722 // true).
3723 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003724 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003725 ExprResult LastIteration32 = WidenIterationCount(
3726 32 /* Bits */, SemaRef.PerformImplicitConversion(
3727 N0->IgnoreImpCasts(), N0->getType(),
3728 Sema::AA_Converting, /*AllowExplicit=*/true)
3729 .get(),
3730 SemaRef);
3731 ExprResult LastIteration64 = WidenIterationCount(
3732 64 /* Bits */, SemaRef.PerformImplicitConversion(
3733 N0->IgnoreImpCasts(), N0->getType(),
3734 Sema::AA_Converting, /*AllowExplicit=*/true)
3735 .get(),
3736 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003737
3738 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
3739 return NestedLoopCount;
3740
3741 auto &C = SemaRef.Context;
3742 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
3743
3744 Scope *CurScope = DSA.getCurScope();
3745 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003746 if (PreCond.isUsable()) {
3747 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
3748 PreCond.get(), IterSpaces[Cnt].PreCond);
3749 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003750 auto N = IterSpaces[Cnt].NumIterations;
3751 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
3752 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003753 LastIteration32 = SemaRef.BuildBinOp(
3754 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
3755 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
3756 Sema::AA_Converting,
3757 /*AllowExplicit=*/true)
3758 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003759 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003760 LastIteration64 = SemaRef.BuildBinOp(
3761 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
3762 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
3763 Sema::AA_Converting,
3764 /*AllowExplicit=*/true)
3765 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003766 }
3767
3768 // Choose either the 32-bit or 64-bit version.
3769 ExprResult LastIteration = LastIteration64;
3770 if (LastIteration32.isUsable() &&
3771 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
3772 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
3773 FitsInto(
3774 32 /* Bits */,
3775 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
3776 LastIteration64.get(), SemaRef)))
3777 LastIteration = LastIteration32;
3778
3779 if (!LastIteration.isUsable())
3780 return 0;
3781
3782 // Save the number of iterations.
3783 ExprResult NumIterations = LastIteration;
3784 {
3785 LastIteration = SemaRef.BuildBinOp(
3786 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
3787 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3788 if (!LastIteration.isUsable())
3789 return 0;
3790 }
3791
3792 // Calculate the last iteration number beforehand instead of doing this on
3793 // each iteration. Do not do this if the number of iterations may be kfold-ed.
3794 llvm::APSInt Result;
3795 bool IsConstant =
3796 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
3797 ExprResult CalcLastIteration;
3798 if (!IsConstant) {
3799 SourceLocation SaveLoc;
3800 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00003801 buildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00003802 ".omp.last.iteration");
Alexey Bataev39f915b82015-05-08 10:41:21 +00003803 ExprResult SaveRef = buildDeclRefExpr(
3804 SemaRef, SaveVar, LastIteration.get()->getType(), SaveLoc);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003805 CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign,
3806 SaveRef.get(), LastIteration.get());
3807 LastIteration = SaveRef;
3808
3809 // Prepare SaveRef + 1.
3810 NumIterations = SemaRef.BuildBinOp(
3811 CurScope, SaveLoc, BO_Add, SaveRef.get(),
3812 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3813 if (!NumIterations.isUsable())
3814 return 0;
3815 }
3816
3817 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
3818
Alexander Musmanc6388682014-12-15 07:07:06 +00003819 QualType VType = LastIteration.get()->getType();
3820 // Build variables passed into runtime, nesessary for worksharing directives.
3821 ExprResult LB, UB, IL, ST, EUB;
Carlo Bertollib9bfa752015-12-08 04:21:03 +00003822 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
3823 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00003824 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00003825 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
3826 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003827 SemaRef.AddInitializerToDecl(
3828 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
3829 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3830
3831 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00003832 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
3833 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003834 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
3835 /*DirectInit*/ false,
3836 /*TypeMayContainAuto*/ false);
3837
3838 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
3839 // This will be used to implement clause 'lastprivate'.
3840 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00003841 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
3842 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003843 SemaRef.AddInitializerToDecl(
3844 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
3845 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3846
3847 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev39f915b82015-05-08 10:41:21 +00003848 VarDecl *STDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.stride");
3849 ST = buildDeclRefExpr(SemaRef, STDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003850 SemaRef.AddInitializerToDecl(
3851 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
3852 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3853
3854 // Build expression: UB = min(UB, LastIteration)
3855 // It is nesessary for CodeGen of directives with static scheduling.
3856 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
3857 UB.get(), LastIteration.get());
3858 ExprResult CondOp = SemaRef.ActOnConditionalOp(
3859 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
3860 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
3861 CondOp.get());
3862 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
3863 }
3864
3865 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003866 ExprResult IV;
3867 ExprResult Init;
3868 {
Alexey Bataev39f915b82015-05-08 10:41:21 +00003869 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.iv");
3870 IV = buildDeclRefExpr(SemaRef, IVDecl, VType, InitLoc);
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003871 Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertollib9bfa752015-12-08 04:21:03 +00003872 isOpenMPTaskLoopDirective(DKind) ||
3873 isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00003874 ? LB.get()
3875 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
3876 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
3877 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003878 }
3879
Alexander Musmanc6388682014-12-15 07:07:06 +00003880 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003881 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00003882 ExprResult Cond =
Carlo Bertollib9bfa752015-12-08 04:21:03 +00003883 (isOpenMPWorksharingDirective(DKind) ||
3884 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00003885 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
3886 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
3887 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003888
3889 // Loop increment (IV = IV + 1)
3890 SourceLocation IncLoc;
3891 ExprResult Inc =
3892 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
3893 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
3894 if (!Inc.isUsable())
3895 return 0;
3896 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00003897 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
3898 if (!Inc.isUsable())
3899 return 0;
3900
3901 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
3902 // Used for directives with static scheduling.
3903 ExprResult NextLB, NextUB;
Carlo Bertollib9bfa752015-12-08 04:21:03 +00003904 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
3905 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00003906 // LB + ST
3907 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
3908 if (!NextLB.isUsable())
3909 return 0;
3910 // LB = LB + ST
3911 NextLB =
3912 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
3913 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
3914 if (!NextLB.isUsable())
3915 return 0;
3916 // UB + ST
3917 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
3918 if (!NextUB.isUsable())
3919 return 0;
3920 // UB = UB + ST
3921 NextUB =
3922 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
3923 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
3924 if (!NextUB.isUsable())
3925 return 0;
3926 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003927
3928 // Build updates and final values of the loop counters.
3929 bool HasErrors = false;
3930 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003931 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003932 Built.Updates.resize(NestedLoopCount);
3933 Built.Finals.resize(NestedLoopCount);
3934 {
3935 ExprResult Div;
3936 // Go from inner nested loop to outer.
3937 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
3938 LoopIterationSpace &IS = IterSpaces[Cnt];
3939 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
3940 // Build: Iter = (IV / Div) % IS.NumIters
3941 // where Div is product of previous iterations' IS.NumIters.
3942 ExprResult Iter;
3943 if (Div.isUsable()) {
3944 Iter =
3945 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
3946 } else {
3947 Iter = IV;
3948 assert((Cnt == (int)NestedLoopCount - 1) &&
3949 "unusable div expected on first iteration only");
3950 }
3951
3952 if (Cnt != 0 && Iter.isUsable())
3953 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
3954 IS.NumIterations);
3955 if (!Iter.isUsable()) {
3956 HasErrors = true;
3957 break;
3958 }
3959
Alexey Bataev39f915b82015-05-08 10:41:21 +00003960 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
3961 auto *CounterVar = buildDeclRefExpr(
3962 SemaRef, cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()),
3963 IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
3964 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003965 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
3966 IS.CounterInit);
3967 if (!Init.isUsable()) {
3968 HasErrors = true;
3969 break;
3970 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003971 ExprResult Update =
Alexey Bataev39f915b82015-05-08 10:41:21 +00003972 BuildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003973 IS.CounterInit, Iter, IS.CounterStep, IS.Subtract);
3974 if (!Update.isUsable()) {
3975 HasErrors = true;
3976 break;
3977 }
3978
3979 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
3980 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00003981 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003982 IS.NumIterations, IS.CounterStep, IS.Subtract);
3983 if (!Final.isUsable()) {
3984 HasErrors = true;
3985 break;
3986 }
3987
3988 // Build Div for the next iteration: Div <- Div * IS.NumIters
3989 if (Cnt != 0) {
3990 if (Div.isUnset())
3991 Div = IS.NumIterations;
3992 else
3993 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
3994 IS.NumIterations);
3995
3996 // Add parentheses (for debugging purposes only).
3997 if (Div.isUsable())
3998 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
3999 if (!Div.isUsable()) {
4000 HasErrors = true;
4001 break;
4002 }
4003 }
4004 if (!Update.isUsable() || !Final.isUsable()) {
4005 HasErrors = true;
4006 break;
4007 }
4008 // Save results
4009 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00004010 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004011 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004012 Built.Updates[Cnt] = Update.get();
4013 Built.Finals[Cnt] = Final.get();
4014 }
4015 }
4016
4017 if (HasErrors)
4018 return 0;
4019
4020 // Save results
4021 Built.IterationVarRef = IV.get();
4022 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00004023 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004024 Built.CalcLastIteration =
4025 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004026 Built.PreCond = PreCond.get();
4027 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004028 Built.Init = Init.get();
4029 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00004030 Built.LB = LB.get();
4031 Built.UB = UB.get();
4032 Built.IL = IL.get();
4033 Built.ST = ST.get();
4034 Built.EUB = EUB.get();
4035 Built.NLB = NextLB.get();
4036 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004037
Alexey Bataevabfc0692014-06-25 06:52:00 +00004038 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004039}
4040
Alexey Bataev10e775f2015-07-30 11:36:16 +00004041static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004042 auto CollapseClauses =
4043 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
4044 if (CollapseClauses.begin() != CollapseClauses.end())
4045 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004046 return nullptr;
4047}
4048
Alexey Bataev10e775f2015-07-30 11:36:16 +00004049static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004050 auto OrderedClauses =
4051 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
4052 if (OrderedClauses.begin() != OrderedClauses.end())
4053 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004054 return nullptr;
4055}
4056
Alexey Bataev66b15b52015-08-21 11:14:16 +00004057static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen,
4058 const Expr *Safelen) {
4059 llvm::APSInt SimdlenRes, SafelenRes;
4060 if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() ||
4061 Simdlen->isInstantiationDependent() ||
4062 Simdlen->containsUnexpandedParameterPack())
4063 return false;
4064 if (Safelen->isValueDependent() || Safelen->isTypeDependent() ||
4065 Safelen->isInstantiationDependent() ||
4066 Safelen->containsUnexpandedParameterPack())
4067 return false;
4068 Simdlen->EvaluateAsInt(SimdlenRes, S.Context);
4069 Safelen->EvaluateAsInt(SafelenRes, S.Context);
4070 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4071 // If both simdlen and safelen clauses are specified, the value of the simdlen
4072 // parameter must be less than or equal to the value of the safelen parameter.
4073 if (SimdlenRes > SafelenRes) {
4074 S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values)
4075 << Simdlen->getSourceRange() << Safelen->getSourceRange();
4076 return true;
4077 }
4078 return false;
4079}
4080
Alexey Bataev4acb8592014-07-07 13:01:15 +00004081StmtResult Sema::ActOnOpenMPSimdDirective(
4082 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4083 SourceLocation EndLoc,
4084 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004085 if (!AStmt)
4086 return StmtError();
4087
4088 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004089 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004090 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4091 // define the nested loops number.
4092 unsigned NestedLoopCount = CheckOpenMPLoop(
4093 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4094 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004095 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004096 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004097
Alexander Musmana5f070a2014-10-01 06:03:56 +00004098 assert((CurContext->isDependentContext() || B.builtAll()) &&
4099 "omp simd loop exprs were not built");
4100
Alexander Musman3276a272015-03-21 10:12:56 +00004101 if (!CurContext->isDependentContext()) {
4102 // Finalize the clauses that need pre-built expressions for CodeGen.
4103 for (auto C : Clauses) {
4104 if (auto LC = dyn_cast<OMPLinearClause>(C))
4105 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4106 B.NumIterations, *this, CurScope))
4107 return StmtError();
4108 }
4109 }
4110
Alexey Bataev66b15b52015-08-21 11:14:16 +00004111 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4112 // If both simdlen and safelen clauses are specified, the value of the simdlen
4113 // parameter must be less than or equal to the value of the safelen parameter.
4114 OMPSafelenClause *Safelen = nullptr;
4115 OMPSimdlenClause *Simdlen = nullptr;
4116 for (auto *Clause : Clauses) {
4117 if (Clause->getClauseKind() == OMPC_safelen)
4118 Safelen = cast<OMPSafelenClause>(Clause);
4119 else if (Clause->getClauseKind() == OMPC_simdlen)
4120 Simdlen = cast<OMPSimdlenClause>(Clause);
4121 if (Safelen && Simdlen)
4122 break;
4123 }
4124 if (Simdlen && Safelen &&
4125 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4126 Safelen->getSafelen()))
4127 return StmtError();
4128
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004129 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004130 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4131 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004132}
4133
Alexey Bataev4acb8592014-07-07 13:01:15 +00004134StmtResult Sema::ActOnOpenMPForDirective(
4135 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4136 SourceLocation EndLoc,
4137 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004138 if (!AStmt)
4139 return StmtError();
4140
4141 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004142 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004143 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4144 // define the nested loops number.
4145 unsigned NestedLoopCount = CheckOpenMPLoop(
4146 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4147 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004148 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00004149 return StmtError();
4150
Alexander Musmana5f070a2014-10-01 06:03:56 +00004151 assert((CurContext->isDependentContext() || B.builtAll()) &&
4152 "omp for loop exprs were not built");
4153
Alexey Bataev54acd402015-08-04 11:18:19 +00004154 if (!CurContext->isDependentContext()) {
4155 // Finalize the clauses that need pre-built expressions for CodeGen.
4156 for (auto C : Clauses) {
4157 if (auto LC = dyn_cast<OMPLinearClause>(C))
4158 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4159 B.NumIterations, *this, CurScope))
4160 return StmtError();
4161 }
4162 }
4163
Alexey Bataevf29276e2014-06-18 04:14:57 +00004164 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004165 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004166 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00004167}
4168
Alexander Musmanf82886e2014-09-18 05:12:34 +00004169StmtResult Sema::ActOnOpenMPForSimdDirective(
4170 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4171 SourceLocation EndLoc,
4172 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004173 if (!AStmt)
4174 return StmtError();
4175
4176 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004177 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004178 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4179 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00004180 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004181 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
4182 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4183 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004184 if (NestedLoopCount == 0)
4185 return StmtError();
4186
Alexander Musmanc6388682014-12-15 07:07:06 +00004187 assert((CurContext->isDependentContext() || B.builtAll()) &&
4188 "omp for simd loop exprs were not built");
4189
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00004190 if (!CurContext->isDependentContext()) {
4191 // Finalize the clauses that need pre-built expressions for CodeGen.
4192 for (auto C : Clauses) {
4193 if (auto LC = dyn_cast<OMPLinearClause>(C))
4194 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4195 B.NumIterations, *this, CurScope))
4196 return StmtError();
4197 }
4198 }
4199
Alexey Bataev66b15b52015-08-21 11:14:16 +00004200 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4201 // If both simdlen and safelen clauses are specified, the value of the simdlen
4202 // parameter must be less than or equal to the value of the safelen parameter.
4203 OMPSafelenClause *Safelen = nullptr;
4204 OMPSimdlenClause *Simdlen = nullptr;
4205 for (auto *Clause : Clauses) {
4206 if (Clause->getClauseKind() == OMPC_safelen)
4207 Safelen = cast<OMPSafelenClause>(Clause);
4208 else if (Clause->getClauseKind() == OMPC_simdlen)
4209 Simdlen = cast<OMPSimdlenClause>(Clause);
4210 if (Safelen && Simdlen)
4211 break;
4212 }
4213 if (Simdlen && Safelen &&
4214 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4215 Safelen->getSafelen()))
4216 return StmtError();
4217
Alexander Musmanf82886e2014-09-18 05:12:34 +00004218 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004219 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4220 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004221}
4222
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004223StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
4224 Stmt *AStmt,
4225 SourceLocation StartLoc,
4226 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004227 if (!AStmt)
4228 return StmtError();
4229
4230 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004231 auto BaseStmt = AStmt;
4232 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4233 BaseStmt = CS->getCapturedStmt();
4234 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4235 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004236 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004237 return StmtError();
4238 // All associated statements must be '#pragma omp section' except for
4239 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004240 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004241 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4242 if (SectionStmt)
4243 Diag(SectionStmt->getLocStart(),
4244 diag::err_omp_sections_substmt_not_section);
4245 return StmtError();
4246 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004247 cast<OMPSectionDirective>(SectionStmt)
4248 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004249 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004250 } else {
4251 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
4252 return StmtError();
4253 }
4254
4255 getCurFunction()->setHasBranchProtectedScope();
4256
Alexey Bataev25e5b442015-09-15 12:52:43 +00004257 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4258 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004259}
4260
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004261StmtResult Sema::ActOnOpenMPSectionDirective(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 Bataev1e0498a2014-06-26 08:21:58 +00004268
4269 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00004270 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004271
Alexey Bataev25e5b442015-09-15 12:52:43 +00004272 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
4273 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004274}
4275
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004276StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
4277 Stmt *AStmt,
4278 SourceLocation StartLoc,
4279 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004280 if (!AStmt)
4281 return StmtError();
4282
4283 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00004284
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004285 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00004286
Alexey Bataev3255bf32015-01-19 05:20:46 +00004287 // OpenMP [2.7.3, single Construct, Restrictions]
4288 // The copyprivate clause must not be used with the nowait clause.
4289 OMPClause *Nowait = nullptr;
4290 OMPClause *Copyprivate = nullptr;
4291 for (auto *Clause : Clauses) {
4292 if (Clause->getClauseKind() == OMPC_nowait)
4293 Nowait = Clause;
4294 else if (Clause->getClauseKind() == OMPC_copyprivate)
4295 Copyprivate = Clause;
4296 if (Copyprivate && Nowait) {
4297 Diag(Copyprivate->getLocStart(),
4298 diag::err_omp_single_copyprivate_with_nowait);
4299 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
4300 return StmtError();
4301 }
4302 }
4303
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004304 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4305}
4306
Alexander Musman80c22892014-07-17 08:54:58 +00004307StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
4308 SourceLocation StartLoc,
4309 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004310 if (!AStmt)
4311 return StmtError();
4312
4313 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00004314
4315 getCurFunction()->setHasBranchProtectedScope();
4316
4317 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
4318}
4319
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004320StmtResult
4321Sema::ActOnOpenMPCriticalDirective(const DeclarationNameInfo &DirName,
4322 Stmt *AStmt, SourceLocation StartLoc,
4323 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004324 if (!AStmt)
4325 return StmtError();
4326
4327 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004328
4329 getCurFunction()->setHasBranchProtectedScope();
4330
4331 return OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
4332 AStmt);
4333}
4334
Alexey Bataev4acb8592014-07-07 13:01:15 +00004335StmtResult Sema::ActOnOpenMPParallelForDirective(
4336 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4337 SourceLocation EndLoc,
4338 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004339 if (!AStmt)
4340 return StmtError();
4341
Alexey Bataev4acb8592014-07-07 13:01:15 +00004342 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4343 // 1.2.2 OpenMP Language Terminology
4344 // Structured block - An executable statement with a single entry at the
4345 // top and a single exit at the bottom.
4346 // The point of exit cannot be a branch out of the structured block.
4347 // longjmp() and throw() must not violate the entry/exit criteria.
4348 CS->getCapturedDecl()->setNothrow();
4349
Alexander Musmanc6388682014-12-15 07:07:06 +00004350 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004351 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4352 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004353 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004354 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
4355 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4356 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00004357 if (NestedLoopCount == 0)
4358 return StmtError();
4359
Alexander Musmana5f070a2014-10-01 06:03:56 +00004360 assert((CurContext->isDependentContext() || B.builtAll()) &&
4361 "omp parallel for loop exprs were not built");
4362
Alexey Bataev54acd402015-08-04 11:18:19 +00004363 if (!CurContext->isDependentContext()) {
4364 // Finalize the clauses that need pre-built expressions for CodeGen.
4365 for (auto C : Clauses) {
4366 if (auto LC = dyn_cast<OMPLinearClause>(C))
4367 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4368 B.NumIterations, *this, CurScope))
4369 return StmtError();
4370 }
4371 }
4372
Alexey Bataev4acb8592014-07-07 13:01:15 +00004373 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004374 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004375 NestedLoopCount, Clauses, AStmt, B,
4376 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00004377}
4378
Alexander Musmane4e893b2014-09-23 09:33:00 +00004379StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
4380 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4381 SourceLocation EndLoc,
4382 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004383 if (!AStmt)
4384 return StmtError();
4385
Alexander Musmane4e893b2014-09-23 09:33:00 +00004386 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4387 // 1.2.2 OpenMP Language Terminology
4388 // Structured block - An executable statement with a single entry at the
4389 // top and a single exit at the bottom.
4390 // The point of exit cannot be a branch out of the structured block.
4391 // longjmp() and throw() must not violate the entry/exit criteria.
4392 CS->getCapturedDecl()->setNothrow();
4393
Alexander Musmanc6388682014-12-15 07:07:06 +00004394 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004395 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4396 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00004397 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004398 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
4399 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4400 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004401 if (NestedLoopCount == 0)
4402 return StmtError();
4403
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00004404 if (!CurContext->isDependentContext()) {
4405 // Finalize the clauses that need pre-built expressions for CodeGen.
4406 for (auto C : Clauses) {
4407 if (auto LC = dyn_cast<OMPLinearClause>(C))
4408 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4409 B.NumIterations, *this, CurScope))
4410 return StmtError();
4411 }
4412 }
4413
Alexey Bataev66b15b52015-08-21 11:14:16 +00004414 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4415 // If both simdlen and safelen clauses are specified, the value of the simdlen
4416 // parameter must be less than or equal to the value of the safelen parameter.
4417 OMPSafelenClause *Safelen = nullptr;
4418 OMPSimdlenClause *Simdlen = nullptr;
4419 for (auto *Clause : Clauses) {
4420 if (Clause->getClauseKind() == OMPC_safelen)
4421 Safelen = cast<OMPSafelenClause>(Clause);
4422 else if (Clause->getClauseKind() == OMPC_simdlen)
4423 Simdlen = cast<OMPSimdlenClause>(Clause);
4424 if (Safelen && Simdlen)
4425 break;
4426 }
4427 if (Simdlen && Safelen &&
4428 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4429 Safelen->getSafelen()))
4430 return StmtError();
4431
Alexander Musmane4e893b2014-09-23 09:33:00 +00004432 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004433 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00004434 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004435}
4436
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004437StmtResult
4438Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
4439 Stmt *AStmt, SourceLocation StartLoc,
4440 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004441 if (!AStmt)
4442 return StmtError();
4443
4444 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004445 auto BaseStmt = AStmt;
4446 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4447 BaseStmt = CS->getCapturedStmt();
4448 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4449 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004450 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004451 return StmtError();
4452 // All associated statements must be '#pragma omp section' except for
4453 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004454 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004455 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4456 if (SectionStmt)
4457 Diag(SectionStmt->getLocStart(),
4458 diag::err_omp_parallel_sections_substmt_not_section);
4459 return StmtError();
4460 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004461 cast<OMPSectionDirective>(SectionStmt)
4462 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004463 }
4464 } else {
4465 Diag(AStmt->getLocStart(),
4466 diag::err_omp_parallel_sections_not_compound_stmt);
4467 return StmtError();
4468 }
4469
4470 getCurFunction()->setHasBranchProtectedScope();
4471
Alexey Bataev25e5b442015-09-15 12:52:43 +00004472 return OMPParallelSectionsDirective::Create(
4473 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004474}
4475
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004476StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
4477 Stmt *AStmt, SourceLocation StartLoc,
4478 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004479 if (!AStmt)
4480 return StmtError();
4481
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004482 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4483 // 1.2.2 OpenMP Language Terminology
4484 // Structured block - An executable statement with a single entry at the
4485 // top and a single exit at the bottom.
4486 // The point of exit cannot be a branch out of the structured block.
4487 // longjmp() and throw() must not violate the entry/exit criteria.
4488 CS->getCapturedDecl()->setNothrow();
4489
4490 getCurFunction()->setHasBranchProtectedScope();
4491
Alexey Bataev25e5b442015-09-15 12:52:43 +00004492 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4493 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004494}
4495
Alexey Bataev68446b72014-07-18 07:47:19 +00004496StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
4497 SourceLocation EndLoc) {
4498 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
4499}
4500
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00004501StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
4502 SourceLocation EndLoc) {
4503 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
4504}
4505
Alexey Bataev2df347a2014-07-18 10:17:07 +00004506StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
4507 SourceLocation EndLoc) {
4508 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
4509}
4510
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00004511StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
4512 SourceLocation StartLoc,
4513 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004514 if (!AStmt)
4515 return StmtError();
4516
4517 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00004518
4519 getCurFunction()->setHasBranchProtectedScope();
4520
4521 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
4522}
4523
Alexey Bataev6125da92014-07-21 11:26:11 +00004524StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
4525 SourceLocation StartLoc,
4526 SourceLocation EndLoc) {
4527 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
4528 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
4529}
4530
Alexey Bataev346265e2015-09-25 10:37:12 +00004531StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
4532 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004533 SourceLocation StartLoc,
4534 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004535 if (!AStmt)
4536 return StmtError();
4537
4538 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004539
4540 getCurFunction()->setHasBranchProtectedScope();
4541
Alexey Bataev346265e2015-09-25 10:37:12 +00004542 OMPThreadsClause *TC = nullptr;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004543 OMPSIMDClause *SC = nullptr;
Alexey Bataev346265e2015-09-25 10:37:12 +00004544 for (auto *C: Clauses) {
4545 if (C->getClauseKind() == OMPC_threads)
4546 TC = cast<OMPThreadsClause>(C);
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004547 else if (C->getClauseKind() == OMPC_simd)
4548 SC = cast<OMPSIMDClause>(C);
Alexey Bataev346265e2015-09-25 10:37:12 +00004549 }
4550
4551 // TODO: this must happen only if 'threads' clause specified or if no clauses
4552 // is specified.
4553 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
4554 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
4555 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param) << (TC != nullptr);
4556 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
4557 return StmtError();
4558 }
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004559 if (!SC && isOpenMPSimdDirective(DSAStack->getParentDirective())) {
4560 // OpenMP [2.8.1,simd Construct, Restrictions]
4561 // An ordered construct with the simd clause is the only OpenMP construct
4562 // that can appear in the simd region.
4563 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
4564 return StmtError();
4565 }
Alexey Bataev346265e2015-09-25 10:37:12 +00004566
4567 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004568}
4569
Alexey Bataev1d160b12015-03-13 12:27:31 +00004570namespace {
4571/// \brief Helper class for checking expression in 'omp atomic [update]'
4572/// construct.
4573class OpenMPAtomicUpdateChecker {
4574 /// \brief Error results for atomic update expressions.
4575 enum ExprAnalysisErrorCode {
4576 /// \brief A statement is not an expression statement.
4577 NotAnExpression,
4578 /// \brief Expression is not builtin binary or unary operation.
4579 NotABinaryOrUnaryExpression,
4580 /// \brief Unary operation is not post-/pre- increment/decrement operation.
4581 NotAnUnaryIncDecExpression,
4582 /// \brief An expression is not of scalar type.
4583 NotAScalarType,
4584 /// \brief A binary operation is not an assignment operation.
4585 NotAnAssignmentOp,
4586 /// \brief RHS part of the binary operation is not a binary expression.
4587 NotABinaryExpression,
4588 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
4589 /// expression.
4590 NotABinaryOperator,
4591 /// \brief RHS binary operation does not have reference to the updated LHS
4592 /// part.
4593 NotAnUpdateExpression,
4594 /// \brief No errors is found.
4595 NoError
4596 };
4597 /// \brief Reference to Sema.
4598 Sema &SemaRef;
4599 /// \brief A location for note diagnostics (when error is found).
4600 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004601 /// \brief 'x' lvalue part of the source atomic expression.
4602 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004603 /// \brief 'expr' rvalue part of the source atomic expression.
4604 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004605 /// \brief Helper expression of the form
4606 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
4607 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
4608 Expr *UpdateExpr;
4609 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
4610 /// important for non-associative operations.
4611 bool IsXLHSInRHSPart;
4612 BinaryOperatorKind Op;
4613 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004614 /// \brief true if the source expression is a postfix unary operation, false
4615 /// if it is a prefix unary operation.
4616 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004617
4618public:
4619 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00004620 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00004621 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00004622 /// \brief Check specified statement that it is suitable for 'atomic update'
4623 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00004624 /// expression. If DiagId and NoteId == 0, then only check is performed
4625 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00004626 /// \param DiagId Diagnostic which should be emitted if error is found.
4627 /// \param NoteId Diagnostic note for the main error message.
4628 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00004629 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00004630 /// \brief Return the 'x' lvalue part of the source atomic expression.
4631 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00004632 /// \brief Return the 'expr' rvalue part of the source atomic expression.
4633 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00004634 /// \brief Return the update expression used in calculation of the updated
4635 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
4636 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
4637 Expr *getUpdateExpr() const { return UpdateExpr; }
4638 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
4639 /// false otherwise.
4640 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
4641
Alexey Bataevb78ca832015-04-01 03:33:17 +00004642 /// \brief true if the source expression is a postfix unary operation, false
4643 /// if it is a prefix unary operation.
4644 bool isPostfixUpdate() const { return IsPostfixUpdate; }
4645
Alexey Bataev1d160b12015-03-13 12:27:31 +00004646private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00004647 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
4648 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00004649};
4650} // namespace
4651
4652bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
4653 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
4654 ExprAnalysisErrorCode ErrorFound = NoError;
4655 SourceLocation ErrorLoc, NoteLoc;
4656 SourceRange ErrorRange, NoteRange;
4657 // Allowed constructs are:
4658 // x = x binop expr;
4659 // x = expr binop x;
4660 if (AtomicBinOp->getOpcode() == BO_Assign) {
4661 X = AtomicBinOp->getLHS();
4662 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
4663 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
4664 if (AtomicInnerBinOp->isMultiplicativeOp() ||
4665 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
4666 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00004667 Op = AtomicInnerBinOp->getOpcode();
4668 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00004669 auto *LHS = AtomicInnerBinOp->getLHS();
4670 auto *RHS = AtomicInnerBinOp->getRHS();
4671 llvm::FoldingSetNodeID XId, LHSId, RHSId;
4672 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
4673 /*Canonical=*/true);
4674 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
4675 /*Canonical=*/true);
4676 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
4677 /*Canonical=*/true);
4678 if (XId == LHSId) {
4679 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004680 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004681 } else if (XId == RHSId) {
4682 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004683 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004684 } else {
4685 ErrorLoc = AtomicInnerBinOp->getExprLoc();
4686 ErrorRange = AtomicInnerBinOp->getSourceRange();
4687 NoteLoc = X->getExprLoc();
4688 NoteRange = X->getSourceRange();
4689 ErrorFound = NotAnUpdateExpression;
4690 }
4691 } else {
4692 ErrorLoc = AtomicInnerBinOp->getExprLoc();
4693 ErrorRange = AtomicInnerBinOp->getSourceRange();
4694 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
4695 NoteRange = SourceRange(NoteLoc, NoteLoc);
4696 ErrorFound = NotABinaryOperator;
4697 }
4698 } else {
4699 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
4700 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
4701 ErrorFound = NotABinaryExpression;
4702 }
4703 } else {
4704 ErrorLoc = AtomicBinOp->getExprLoc();
4705 ErrorRange = AtomicBinOp->getSourceRange();
4706 NoteLoc = AtomicBinOp->getOperatorLoc();
4707 NoteRange = SourceRange(NoteLoc, NoteLoc);
4708 ErrorFound = NotAnAssignmentOp;
4709 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00004710 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004711 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
4712 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
4713 return true;
4714 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00004715 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00004716 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004717}
4718
4719bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
4720 unsigned NoteId) {
4721 ExprAnalysisErrorCode ErrorFound = NoError;
4722 SourceLocation ErrorLoc, NoteLoc;
4723 SourceRange ErrorRange, NoteRange;
4724 // Allowed constructs are:
4725 // x++;
4726 // x--;
4727 // ++x;
4728 // --x;
4729 // x binop= expr;
4730 // x = x binop expr;
4731 // x = expr binop x;
4732 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
4733 AtomicBody = AtomicBody->IgnoreParenImpCasts();
4734 if (AtomicBody->getType()->isScalarType() ||
4735 AtomicBody->isInstantiationDependent()) {
4736 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
4737 AtomicBody->IgnoreParenImpCasts())) {
4738 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00004739 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00004740 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00004741 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00004742 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00004743 X = AtomicCompAssignOp->getLHS();
4744 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004745 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
4746 AtomicBody->IgnoreParenImpCasts())) {
4747 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00004748 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
4749 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004750 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00004751 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
4752 // Check for Unary Operation
4753 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004754 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00004755 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
4756 OpLoc = AtomicUnaryOp->getOperatorLoc();
4757 X = AtomicUnaryOp->getSubExpr();
4758 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
4759 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004760 } else {
4761 ErrorFound = NotAnUnaryIncDecExpression;
4762 ErrorLoc = AtomicUnaryOp->getExprLoc();
4763 ErrorRange = AtomicUnaryOp->getSourceRange();
4764 NoteLoc = AtomicUnaryOp->getOperatorLoc();
4765 NoteRange = SourceRange(NoteLoc, NoteLoc);
4766 }
Alexey Bataev5a195472015-09-04 12:55:50 +00004767 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004768 ErrorFound = NotABinaryOrUnaryExpression;
4769 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
4770 NoteRange = ErrorRange = AtomicBody->getSourceRange();
4771 }
4772 } else {
4773 ErrorFound = NotAScalarType;
4774 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
4775 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
4776 }
4777 } else {
4778 ErrorFound = NotAnExpression;
4779 NoteLoc = ErrorLoc = S->getLocStart();
4780 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
4781 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00004782 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004783 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
4784 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
4785 return true;
4786 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00004787 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00004788 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00004789 // Build an update expression of form 'OpaqueValueExpr(x) binop
4790 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
4791 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
4792 auto *OVEX = new (SemaRef.getASTContext())
4793 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
4794 auto *OVEExpr = new (SemaRef.getASTContext())
4795 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
4796 auto Update =
4797 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
4798 IsXLHSInRHSPart ? OVEExpr : OVEX);
4799 if (Update.isInvalid())
4800 return true;
4801 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
4802 Sema::AA_Casting);
4803 if (Update.isInvalid())
4804 return true;
4805 UpdateExpr = Update.get();
4806 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00004807 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004808}
4809
Alexey Bataev0162e452014-07-22 10:10:35 +00004810StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
4811 Stmt *AStmt,
4812 SourceLocation StartLoc,
4813 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004814 if (!AStmt)
4815 return StmtError();
4816
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004817 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00004818 // 1.2.2 OpenMP Language Terminology
4819 // Structured block - An executable statement with a single entry at the
4820 // top and a single exit at the bottom.
4821 // The point of exit cannot be a branch out of the structured block.
4822 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00004823 OpenMPClauseKind AtomicKind = OMPC_unknown;
4824 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004825 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00004826 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00004827 C->getClauseKind() == OMPC_update ||
4828 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00004829 if (AtomicKind != OMPC_unknown) {
4830 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
4831 << SourceRange(C->getLocStart(), C->getLocEnd());
4832 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
4833 << getOpenMPClauseName(AtomicKind);
4834 } else {
4835 AtomicKind = C->getClauseKind();
4836 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004837 }
4838 }
4839 }
Alexey Bataev62cec442014-11-18 10:14:22 +00004840
Alexey Bataev459dec02014-07-24 06:46:57 +00004841 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00004842 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
4843 Body = EWC->getSubExpr();
4844
Alexey Bataev62cec442014-11-18 10:14:22 +00004845 Expr *X = nullptr;
4846 Expr *V = nullptr;
4847 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004848 Expr *UE = nullptr;
4849 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004850 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00004851 // OpenMP [2.12.6, atomic Construct]
4852 // In the next expressions:
4853 // * x and v (as applicable) are both l-value expressions with scalar type.
4854 // * During the execution of an atomic region, multiple syntactic
4855 // occurrences of x must designate the same storage location.
4856 // * Neither of v and expr (as applicable) may access the storage location
4857 // designated by x.
4858 // * Neither of x and expr (as applicable) may access the storage location
4859 // designated by v.
4860 // * expr is an expression with scalar type.
4861 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
4862 // * binop, binop=, ++, and -- are not overloaded operators.
4863 // * The expression x binop expr must be numerically equivalent to x binop
4864 // (expr). This requirement is satisfied if the operators in expr have
4865 // precedence greater than binop, or by using parentheses around expr or
4866 // subexpressions of expr.
4867 // * The expression expr binop x must be numerically equivalent to (expr)
4868 // binop x. This requirement is satisfied if the operators in expr have
4869 // precedence equal to or greater than binop, or by using parentheses around
4870 // expr or subexpressions of expr.
4871 // * For forms that allow multiple occurrences of x, the number of times
4872 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00004873 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004874 enum {
4875 NotAnExpression,
4876 NotAnAssignmentOp,
4877 NotAScalarType,
4878 NotAnLValue,
4879 NoError
4880 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00004881 SourceLocation ErrorLoc, NoteLoc;
4882 SourceRange ErrorRange, NoteRange;
4883 // If clause is read:
4884 // v = x;
4885 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
4886 auto AtomicBinOp =
4887 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
4888 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
4889 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
4890 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
4891 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
4892 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
4893 if (!X->isLValue() || !V->isLValue()) {
4894 auto NotLValueExpr = X->isLValue() ? V : X;
4895 ErrorFound = NotAnLValue;
4896 ErrorLoc = AtomicBinOp->getExprLoc();
4897 ErrorRange = AtomicBinOp->getSourceRange();
4898 NoteLoc = NotLValueExpr->getExprLoc();
4899 NoteRange = NotLValueExpr->getSourceRange();
4900 }
4901 } else if (!X->isInstantiationDependent() ||
4902 !V->isInstantiationDependent()) {
4903 auto NotScalarExpr =
4904 (X->isInstantiationDependent() || X->getType()->isScalarType())
4905 ? V
4906 : X;
4907 ErrorFound = NotAScalarType;
4908 ErrorLoc = AtomicBinOp->getExprLoc();
4909 ErrorRange = AtomicBinOp->getSourceRange();
4910 NoteLoc = NotScalarExpr->getExprLoc();
4911 NoteRange = NotScalarExpr->getSourceRange();
4912 }
Alexey Bataev5a195472015-09-04 12:55:50 +00004913 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00004914 ErrorFound = NotAnAssignmentOp;
4915 ErrorLoc = AtomicBody->getExprLoc();
4916 ErrorRange = AtomicBody->getSourceRange();
4917 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
4918 : AtomicBody->getExprLoc();
4919 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
4920 : AtomicBody->getSourceRange();
4921 }
4922 } else {
4923 ErrorFound = NotAnExpression;
4924 NoteLoc = ErrorLoc = Body->getLocStart();
4925 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00004926 }
Alexey Bataev62cec442014-11-18 10:14:22 +00004927 if (ErrorFound != NoError) {
4928 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
4929 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00004930 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
4931 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00004932 return StmtError();
4933 } else if (CurContext->isDependentContext())
4934 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00004935 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004936 enum {
4937 NotAnExpression,
4938 NotAnAssignmentOp,
4939 NotAScalarType,
4940 NotAnLValue,
4941 NoError
4942 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00004943 SourceLocation ErrorLoc, NoteLoc;
4944 SourceRange ErrorRange, NoteRange;
4945 // If clause is write:
4946 // x = expr;
4947 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
4948 auto AtomicBinOp =
4949 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
4950 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00004951 X = AtomicBinOp->getLHS();
4952 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00004953 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
4954 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
4955 if (!X->isLValue()) {
4956 ErrorFound = NotAnLValue;
4957 ErrorLoc = AtomicBinOp->getExprLoc();
4958 ErrorRange = AtomicBinOp->getSourceRange();
4959 NoteLoc = X->getExprLoc();
4960 NoteRange = X->getSourceRange();
4961 }
4962 } else if (!X->isInstantiationDependent() ||
4963 !E->isInstantiationDependent()) {
4964 auto NotScalarExpr =
4965 (X->isInstantiationDependent() || X->getType()->isScalarType())
4966 ? E
4967 : X;
4968 ErrorFound = NotAScalarType;
4969 ErrorLoc = AtomicBinOp->getExprLoc();
4970 ErrorRange = AtomicBinOp->getSourceRange();
4971 NoteLoc = NotScalarExpr->getExprLoc();
4972 NoteRange = NotScalarExpr->getSourceRange();
4973 }
Alexey Bataev5a195472015-09-04 12:55:50 +00004974 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00004975 ErrorFound = NotAnAssignmentOp;
4976 ErrorLoc = AtomicBody->getExprLoc();
4977 ErrorRange = AtomicBody->getSourceRange();
4978 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
4979 : AtomicBody->getExprLoc();
4980 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
4981 : AtomicBody->getSourceRange();
4982 }
4983 } else {
4984 ErrorFound = NotAnExpression;
4985 NoteLoc = ErrorLoc = Body->getLocStart();
4986 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00004987 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00004988 if (ErrorFound != NoError) {
4989 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
4990 << ErrorRange;
4991 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
4992 << NoteRange;
4993 return StmtError();
4994 } else if (CurContext->isDependentContext())
4995 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00004996 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004997 // If clause is update:
4998 // x++;
4999 // x--;
5000 // ++x;
5001 // --x;
5002 // x binop= expr;
5003 // x = x binop expr;
5004 // x = expr binop x;
5005 OpenMPAtomicUpdateChecker Checker(*this);
5006 if (Checker.checkStatement(
5007 Body, (AtomicKind == OMPC_update)
5008 ? diag::err_omp_atomic_update_not_expression_statement
5009 : diag::err_omp_atomic_not_expression_statement,
5010 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00005011 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005012 if (!CurContext->isDependentContext()) {
5013 E = Checker.getExpr();
5014 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005015 UE = Checker.getUpdateExpr();
5016 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00005017 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005018 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005019 enum {
5020 NotAnAssignmentOp,
5021 NotACompoundStatement,
5022 NotTwoSubstatements,
5023 NotASpecificExpression,
5024 NoError
5025 } ErrorFound = NoError;
5026 SourceLocation ErrorLoc, NoteLoc;
5027 SourceRange ErrorRange, NoteRange;
5028 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
5029 // If clause is a capture:
5030 // v = x++;
5031 // v = x--;
5032 // v = ++x;
5033 // v = --x;
5034 // v = x binop= expr;
5035 // v = x = x binop expr;
5036 // v = x = expr binop x;
5037 auto *AtomicBinOp =
5038 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5039 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5040 V = AtomicBinOp->getLHS();
5041 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5042 OpenMPAtomicUpdateChecker Checker(*this);
5043 if (Checker.checkStatement(
5044 Body, diag::err_omp_atomic_capture_not_expression_statement,
5045 diag::note_omp_atomic_update))
5046 return StmtError();
5047 E = Checker.getExpr();
5048 X = Checker.getX();
5049 UE = Checker.getUpdateExpr();
5050 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
5051 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00005052 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005053 ErrorLoc = AtomicBody->getExprLoc();
5054 ErrorRange = AtomicBody->getSourceRange();
5055 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5056 : AtomicBody->getExprLoc();
5057 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5058 : AtomicBody->getSourceRange();
5059 ErrorFound = NotAnAssignmentOp;
5060 }
5061 if (ErrorFound != NoError) {
5062 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
5063 << ErrorRange;
5064 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5065 return StmtError();
5066 } else if (CurContext->isDependentContext()) {
5067 UE = V = E = X = nullptr;
5068 }
5069 } else {
5070 // If clause is a capture:
5071 // { v = x; x = expr; }
5072 // { v = x; x++; }
5073 // { v = x; x--; }
5074 // { v = x; ++x; }
5075 // { v = x; --x; }
5076 // { v = x; x binop= expr; }
5077 // { v = x; x = x binop expr; }
5078 // { v = x; x = expr binop x; }
5079 // { x++; v = x; }
5080 // { x--; v = x; }
5081 // { ++x; v = x; }
5082 // { --x; v = x; }
5083 // { x binop= expr; v = x; }
5084 // { x = x binop expr; v = x; }
5085 // { x = expr binop x; v = x; }
5086 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
5087 // Check that this is { expr1; expr2; }
5088 if (CS->size() == 2) {
5089 auto *First = CS->body_front();
5090 auto *Second = CS->body_back();
5091 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
5092 First = EWC->getSubExpr()->IgnoreParenImpCasts();
5093 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
5094 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
5095 // Need to find what subexpression is 'v' and what is 'x'.
5096 OpenMPAtomicUpdateChecker Checker(*this);
5097 bool IsUpdateExprFound = !Checker.checkStatement(Second);
5098 BinaryOperator *BinOp = nullptr;
5099 if (IsUpdateExprFound) {
5100 BinOp = dyn_cast<BinaryOperator>(First);
5101 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5102 }
5103 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5104 // { v = x; x++; }
5105 // { v = x; x--; }
5106 // { v = x; ++x; }
5107 // { v = x; --x; }
5108 // { v = x; x binop= expr; }
5109 // { v = x; x = x binop expr; }
5110 // { v = x; x = expr binop x; }
5111 // Check that the first expression has form v = x.
5112 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5113 llvm::FoldingSetNodeID XId, PossibleXId;
5114 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5115 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5116 IsUpdateExprFound = XId == PossibleXId;
5117 if (IsUpdateExprFound) {
5118 V = BinOp->getLHS();
5119 X = Checker.getX();
5120 E = Checker.getExpr();
5121 UE = Checker.getUpdateExpr();
5122 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005123 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005124 }
5125 }
5126 if (!IsUpdateExprFound) {
5127 IsUpdateExprFound = !Checker.checkStatement(First);
5128 BinOp = nullptr;
5129 if (IsUpdateExprFound) {
5130 BinOp = dyn_cast<BinaryOperator>(Second);
5131 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5132 }
5133 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5134 // { x++; v = x; }
5135 // { x--; v = x; }
5136 // { ++x; v = x; }
5137 // { --x; v = x; }
5138 // { x binop= expr; v = x; }
5139 // { x = x binop expr; v = x; }
5140 // { x = expr binop x; v = x; }
5141 // Check that the second expression has form v = x.
5142 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5143 llvm::FoldingSetNodeID XId, PossibleXId;
5144 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5145 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5146 IsUpdateExprFound = XId == PossibleXId;
5147 if (IsUpdateExprFound) {
5148 V = BinOp->getLHS();
5149 X = Checker.getX();
5150 E = Checker.getExpr();
5151 UE = Checker.getUpdateExpr();
5152 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005153 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005154 }
5155 }
5156 }
5157 if (!IsUpdateExprFound) {
5158 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00005159 auto *FirstExpr = dyn_cast<Expr>(First);
5160 auto *SecondExpr = dyn_cast<Expr>(Second);
5161 if (!FirstExpr || !SecondExpr ||
5162 !(FirstExpr->isInstantiationDependent() ||
5163 SecondExpr->isInstantiationDependent())) {
5164 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
5165 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005166 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00005167 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
5168 : First->getLocStart();
5169 NoteRange = ErrorRange = FirstBinOp
5170 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00005171 : SourceRange(ErrorLoc, ErrorLoc);
5172 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005173 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
5174 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
5175 ErrorFound = NotAnAssignmentOp;
5176 NoteLoc = ErrorLoc = SecondBinOp
5177 ? SecondBinOp->getOperatorLoc()
5178 : Second->getLocStart();
5179 NoteRange = ErrorRange =
5180 SecondBinOp ? SecondBinOp->getSourceRange()
5181 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00005182 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005183 auto *PossibleXRHSInFirst =
5184 FirstBinOp->getRHS()->IgnoreParenImpCasts();
5185 auto *PossibleXLHSInSecond =
5186 SecondBinOp->getLHS()->IgnoreParenImpCasts();
5187 llvm::FoldingSetNodeID X1Id, X2Id;
5188 PossibleXRHSInFirst->Profile(X1Id, Context,
5189 /*Canonical=*/true);
5190 PossibleXLHSInSecond->Profile(X2Id, Context,
5191 /*Canonical=*/true);
5192 IsUpdateExprFound = X1Id == X2Id;
5193 if (IsUpdateExprFound) {
5194 V = FirstBinOp->getLHS();
5195 X = SecondBinOp->getLHS();
5196 E = SecondBinOp->getRHS();
5197 UE = nullptr;
5198 IsXLHSInRHSPart = false;
5199 IsPostfixUpdate = true;
5200 } else {
5201 ErrorFound = NotASpecificExpression;
5202 ErrorLoc = FirstBinOp->getExprLoc();
5203 ErrorRange = FirstBinOp->getSourceRange();
5204 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
5205 NoteRange = SecondBinOp->getRHS()->getSourceRange();
5206 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005207 }
5208 }
5209 }
5210 }
5211 } else {
5212 NoteLoc = ErrorLoc = Body->getLocStart();
5213 NoteRange = ErrorRange =
5214 SourceRange(Body->getLocStart(), Body->getLocStart());
5215 ErrorFound = NotTwoSubstatements;
5216 }
5217 } else {
5218 NoteLoc = ErrorLoc = Body->getLocStart();
5219 NoteRange = ErrorRange =
5220 SourceRange(Body->getLocStart(), Body->getLocStart());
5221 ErrorFound = NotACompoundStatement;
5222 }
5223 if (ErrorFound != NoError) {
5224 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
5225 << ErrorRange;
5226 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5227 return StmtError();
5228 } else if (CurContext->isDependentContext()) {
5229 UE = V = E = X = nullptr;
5230 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005231 }
Alexey Bataevdea47612014-07-23 07:46:59 +00005232 }
Alexey Bataev0162e452014-07-22 10:10:35 +00005233
5234 getCurFunction()->setHasBranchProtectedScope();
5235
Alexey Bataev62cec442014-11-18 10:14:22 +00005236 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00005237 X, V, E, UE, IsXLHSInRHSPart,
5238 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00005239}
5240
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005241StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
5242 Stmt *AStmt,
5243 SourceLocation StartLoc,
5244 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005245 if (!AStmt)
5246 return StmtError();
5247
Samuel Antao4af1b7b2015-12-02 17:44:43 +00005248 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5249 // 1.2.2 OpenMP Language Terminology
5250 // Structured block - An executable statement with a single entry at the
5251 // top and a single exit at the bottom.
5252 // The point of exit cannot be a branch out of the structured block.
5253 // longjmp() and throw() must not violate the entry/exit criteria.
5254 CS->getCapturedDecl()->setNothrow();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005255
Alexey Bataev13314bf2014-10-09 04:18:56 +00005256 // OpenMP [2.16, Nesting of Regions]
5257 // If specified, a teams construct must be contained within a target
5258 // construct. That target construct must contain no statements or directives
5259 // outside of the teams construct.
5260 if (DSAStack->hasInnerTeamsRegion()) {
5261 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
5262 bool OMPTeamsFound = true;
5263 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
5264 auto I = CS->body_begin();
5265 while (I != CS->body_end()) {
5266 auto OED = dyn_cast<OMPExecutableDirective>(*I);
5267 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
5268 OMPTeamsFound = false;
5269 break;
5270 }
5271 ++I;
5272 }
5273 assert(I != CS->body_end() && "Not found statement");
5274 S = *I;
5275 }
5276 if (!OMPTeamsFound) {
5277 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
5278 Diag(DSAStack->getInnerTeamsRegionLoc(),
5279 diag::note_omp_nested_teams_construct_here);
5280 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
5281 << isa<OMPExecutableDirective>(S);
5282 return StmtError();
5283 }
5284 }
5285
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005286 getCurFunction()->setHasBranchProtectedScope();
5287
5288 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5289}
5290
Michael Wong65f367f2015-07-21 13:44:28 +00005291StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
5292 Stmt *AStmt,
5293 SourceLocation StartLoc,
5294 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005295 if (!AStmt)
5296 return StmtError();
5297
5298 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5299
Michael Wong65f367f2015-07-21 13:44:28 +00005300 getCurFunction()->setHasBranchProtectedScope();
5301
5302 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
5303 AStmt);
5304}
5305
Alexey Bataev13314bf2014-10-09 04:18:56 +00005306StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
5307 Stmt *AStmt, SourceLocation StartLoc,
5308 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005309 if (!AStmt)
5310 return StmtError();
5311
Alexey Bataev13314bf2014-10-09 04:18:56 +00005312 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5313 // 1.2.2 OpenMP Language Terminology
5314 // Structured block - An executable statement with a single entry at the
5315 // top and a single exit at the bottom.
5316 // The point of exit cannot be a branch out of the structured block.
5317 // longjmp() and throw() must not violate the entry/exit criteria.
5318 CS->getCapturedDecl()->setNothrow();
5319
5320 getCurFunction()->setHasBranchProtectedScope();
5321
5322 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5323}
5324
Alexey Bataev6d4ed052015-07-01 06:57:41 +00005325StmtResult
5326Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
5327 SourceLocation EndLoc,
5328 OpenMPDirectiveKind CancelRegion) {
5329 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
5330 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
5331 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
5332 << getOpenMPDirectiveName(CancelRegion);
5333 return StmtError();
5334 }
5335 if (DSAStack->isParentNowaitRegion()) {
5336 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
5337 return StmtError();
5338 }
5339 if (DSAStack->isParentOrderedRegion()) {
5340 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
5341 return StmtError();
5342 }
5343 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
5344 CancelRegion);
5345}
5346
Alexey Bataev87933c72015-09-18 08:07:34 +00005347StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
5348 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00005349 SourceLocation EndLoc,
5350 OpenMPDirectiveKind CancelRegion) {
5351 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
5352 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
5353 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
5354 << getOpenMPDirectiveName(CancelRegion);
5355 return StmtError();
5356 }
5357 if (DSAStack->isParentNowaitRegion()) {
5358 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
5359 return StmtError();
5360 }
5361 if (DSAStack->isParentOrderedRegion()) {
5362 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
5363 return StmtError();
5364 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005365 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00005366 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
5367 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00005368}
5369
Alexey Bataev49f6e782015-12-01 04:18:41 +00005370StmtResult Sema::ActOnOpenMPTaskLoopDirective(
5371 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5372 SourceLocation EndLoc,
5373 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
5374 if (!AStmt)
5375 return StmtError();
5376
5377 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5378 OMPLoopDirective::HelperExprs B;
5379 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5380 // define the nested loops number.
5381 unsigned NestedLoopCount =
5382 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005383 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00005384 VarsWithImplicitDSA, B);
5385 if (NestedLoopCount == 0)
5386 return StmtError();
5387
5388 assert((CurContext->isDependentContext() || B.builtAll()) &&
5389 "omp for loop exprs were not built");
5390
5391 getCurFunction()->setHasBranchProtectedScope();
5392 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
5393 NestedLoopCount, Clauses, AStmt, B);
5394}
5395
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005396StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
5397 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5398 SourceLocation EndLoc,
5399 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
5400 if (!AStmt)
5401 return StmtError();
5402
5403 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5404 OMPLoopDirective::HelperExprs B;
5405 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5406 // define the nested loops number.
5407 unsigned NestedLoopCount =
5408 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
5409 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
5410 VarsWithImplicitDSA, B);
5411 if (NestedLoopCount == 0)
5412 return StmtError();
5413
5414 assert((CurContext->isDependentContext() || B.builtAll()) &&
5415 "omp for loop exprs were not built");
5416
5417 getCurFunction()->setHasBranchProtectedScope();
5418 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
5419 NestedLoopCount, Clauses, AStmt, B);
5420}
5421
Carlo Bertollib9bfa752015-12-08 04:21:03 +00005422StmtResult Sema::ActOnOpenMPDistributeDirective(
5423 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5424 SourceLocation EndLoc,
5425 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
5426 if (!AStmt)
5427 return StmtError();
5428
5429 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5430 OMPLoopDirective::HelperExprs B;
5431 // In presence of clause 'collapse' with number of loops, it will
5432 // define the nested loops number.
5433 unsigned NestedLoopCount =
5434 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
5435 nullptr /*ordered not a clause on distribute*/, AStmt,
5436 *this, *DSAStack, VarsWithImplicitDSA, B);
5437 if (NestedLoopCount == 0)
5438 return StmtError();
5439
5440 assert((CurContext->isDependentContext() || B.builtAll()) &&
5441 "omp for loop exprs were not built");
5442
5443 getCurFunction()->setHasBranchProtectedScope();
5444 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
5445 NestedLoopCount, Clauses, AStmt, B);
5446}
5447
Alexey Bataeved09d242014-05-28 05:53:51 +00005448OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005449 SourceLocation StartLoc,
5450 SourceLocation LParenLoc,
5451 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005452 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005453 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00005454 case OMPC_final:
5455 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
5456 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00005457 case OMPC_num_threads:
5458 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
5459 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00005460 case OMPC_safelen:
5461 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
5462 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00005463 case OMPC_simdlen:
5464 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
5465 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00005466 case OMPC_collapse:
5467 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
5468 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005469 case OMPC_ordered:
5470 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
5471 break;
Michael Wonge710d542015-08-07 16:16:36 +00005472 case OMPC_device:
5473 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
5474 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00005475 case OMPC_num_teams:
5476 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
5477 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005478 case OMPC_thread_limit:
5479 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
5480 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00005481 case OMPC_priority:
5482 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
5483 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00005484 case OMPC_grainsize:
5485 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
5486 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005487 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005488 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005489 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005490 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005491 case OMPC_private:
5492 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00005493 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005494 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00005495 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00005496 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00005497 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005498 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005499 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00005500 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005501 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005502 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005503 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00005504 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005505 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00005506 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00005507 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00005508 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005509 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005510 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00005511 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005512 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00005513 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00005514 case OMPC_nogroup:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005515 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005516 llvm_unreachable("Clause is not allowed.");
5517 }
5518 return Res;
5519}
5520
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005521OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
5522 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005523 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005524 SourceLocation NameModifierLoc,
5525 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005526 SourceLocation EndLoc) {
5527 Expr *ValExpr = Condition;
5528 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
5529 !Condition->isInstantiationDependent() &&
5530 !Condition->containsUnexpandedParameterPack()) {
5531 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00005532 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005533 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005534 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005535
Nikola Smiljanic01a75982014-05-29 10:55:11 +00005536 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005537 }
5538
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005539 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
5540 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005541}
5542
Alexey Bataev3778b602014-07-17 07:32:53 +00005543OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
5544 SourceLocation StartLoc,
5545 SourceLocation LParenLoc,
5546 SourceLocation EndLoc) {
5547 Expr *ValExpr = Condition;
5548 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
5549 !Condition->isInstantiationDependent() &&
5550 !Condition->containsUnexpandedParameterPack()) {
5551 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
5552 Condition->getExprLoc(), Condition);
5553 if (Val.isInvalid())
5554 return nullptr;
5555
5556 ValExpr = Val.get();
5557 }
5558
5559 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
5560}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005561ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
5562 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00005563 if (!Op)
5564 return ExprError();
5565
5566 class IntConvertDiagnoser : public ICEConvertDiagnoser {
5567 public:
5568 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00005569 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00005570 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
5571 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005572 return S.Diag(Loc, diag::err_omp_not_integral) << T;
5573 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005574 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
5575 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005576 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
5577 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005578 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
5579 QualType T,
5580 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005581 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
5582 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005583 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
5584 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005585 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00005586 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00005587 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005588 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
5589 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005590 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
5591 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005592 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
5593 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005594 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00005595 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00005596 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005597 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
5598 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005599 llvm_unreachable("conversion functions are permitted");
5600 }
5601 } ConvertDiagnoser;
5602 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
5603}
5604
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005605static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00005606 OpenMPClauseKind CKind,
5607 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005608 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
5609 !ValExpr->isInstantiationDependent()) {
5610 SourceLocation Loc = ValExpr->getExprLoc();
5611 ExprResult Value =
5612 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
5613 if (Value.isInvalid())
5614 return false;
5615
5616 ValExpr = Value.get();
5617 // The expression must evaluate to a non-negative integer value.
5618 llvm::APSInt Result;
5619 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00005620 Result.isSigned() &&
5621 !((!StrictlyPositive && Result.isNonNegative()) ||
5622 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005623 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00005624 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
5625 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005626 return false;
5627 }
5628 }
5629 return true;
5630}
5631
Alexey Bataev568a8332014-03-06 06:15:19 +00005632OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
5633 SourceLocation StartLoc,
5634 SourceLocation LParenLoc,
5635 SourceLocation EndLoc) {
5636 Expr *ValExpr = NumThreads;
Alexey Bataev568a8332014-03-06 06:15:19 +00005637
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005638 // OpenMP [2.5, Restrictions]
5639 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00005640 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
5641 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005642 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00005643
Alexey Bataeved09d242014-05-28 05:53:51 +00005644 return new (Context)
5645 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00005646}
5647
Alexey Bataev62c87d22014-03-21 04:51:18 +00005648ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
5649 OpenMPClauseKind CKind) {
5650 if (!E)
5651 return ExprError();
5652 if (E->isValueDependent() || E->isTypeDependent() ||
5653 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00005654 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00005655 llvm::APSInt Result;
5656 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
5657 if (ICE.isInvalid())
5658 return ExprError();
5659 if (!Result.isStrictlyPositive()) {
5660 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00005661 << getOpenMPClauseName(CKind) << 1 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00005662 return ExprError();
5663 }
Alexander Musman09184fe2014-09-30 05:29:28 +00005664 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
5665 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
5666 << E->getSourceRange();
5667 return ExprError();
5668 }
Alexey Bataev7b6bc882015-11-26 07:50:39 +00005669 if (CKind == OMPC_collapse)
5670 DSAStack->setCollapseNumber(Result.getExtValue());
5671 else if (CKind == OMPC_ordered)
5672 DSAStack->setCollapseNumber(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00005673 return ICE;
5674}
5675
5676OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
5677 SourceLocation LParenLoc,
5678 SourceLocation EndLoc) {
5679 // OpenMP [2.8.1, simd construct, Description]
5680 // The parameter of the safelen clause must be a constant
5681 // positive integer expression.
5682 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
5683 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005684 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00005685 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00005686 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00005687}
5688
Alexey Bataev66b15b52015-08-21 11:14:16 +00005689OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
5690 SourceLocation LParenLoc,
5691 SourceLocation EndLoc) {
5692 // OpenMP [2.8.1, simd construct, Description]
5693 // The parameter of the simdlen clause must be a constant
5694 // positive integer expression.
5695 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
5696 if (Simdlen.isInvalid())
5697 return nullptr;
5698 return new (Context)
5699 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
5700}
5701
Alexander Musman64d33f12014-06-04 07:53:32 +00005702OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
5703 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00005704 SourceLocation LParenLoc,
5705 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00005706 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00005707 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00005708 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00005709 // The parameter of the collapse clause must be a constant
5710 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00005711 ExprResult NumForLoopsResult =
5712 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
5713 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00005714 return nullptr;
5715 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00005716 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00005717}
5718
Alexey Bataev10e775f2015-07-30 11:36:16 +00005719OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
5720 SourceLocation EndLoc,
5721 SourceLocation LParenLoc,
5722 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00005723 // OpenMP [2.7.1, loop construct, Description]
5724 // OpenMP [2.8.1, simd construct, Description]
5725 // OpenMP [2.9.6, distribute construct, Description]
5726 // The parameter of the ordered clause must be a constant
5727 // positive integer expression if any.
5728 if (NumForLoops && LParenLoc.isValid()) {
5729 ExprResult NumForLoopsResult =
5730 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
5731 if (NumForLoopsResult.isInvalid())
5732 return nullptr;
5733 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00005734 } else
5735 NumForLoops = nullptr;
5736 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00005737 return new (Context)
5738 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
5739}
5740
Alexey Bataeved09d242014-05-28 05:53:51 +00005741OMPClause *Sema::ActOnOpenMPSimpleClause(
5742 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
5743 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005744 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005745 switch (Kind) {
5746 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00005747 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00005748 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
5749 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005750 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005751 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00005752 Res = ActOnOpenMPProcBindClause(
5753 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
5754 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005755 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005756 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00005757 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00005758 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00005759 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00005760 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00005761 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005762 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005763 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005764 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00005765 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00005766 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00005767 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00005768 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00005769 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005770 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005771 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005772 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00005773 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005774 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005775 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005776 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00005777 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005778 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00005779 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00005780 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00005781 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005782 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005783 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00005784 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00005785 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005786 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00005787 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00005788 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005789 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00005790 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00005791 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00005792 case OMPC_nogroup:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005793 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005794 llvm_unreachable("Clause is not allowed.");
5795 }
5796 return Res;
5797}
5798
5799OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
5800 SourceLocation KindKwLoc,
5801 SourceLocation StartLoc,
5802 SourceLocation LParenLoc,
5803 SourceLocation EndLoc) {
5804 if (Kind == OMPC_DEFAULT_unknown) {
5805 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005806 static_assert(OMPC_DEFAULT_unknown > 0,
5807 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00005808 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005809 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005810 Values += "'";
5811 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
5812 Values += "'";
5813 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005814 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005815 Values += " or ";
5816 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005817 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005818 break;
5819 default:
5820 Values += Sep;
5821 break;
5822 }
5823 }
5824 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00005825 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005826 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005827 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00005828 switch (Kind) {
5829 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005830 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005831 break;
5832 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005833 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005834 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005835 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005836 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00005837 break;
5838 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005839 return new (Context)
5840 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005841}
5842
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005843OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
5844 SourceLocation KindKwLoc,
5845 SourceLocation StartLoc,
5846 SourceLocation LParenLoc,
5847 SourceLocation EndLoc) {
5848 if (Kind == OMPC_PROC_BIND_unknown) {
5849 std::string Values;
5850 std::string Sep(", ");
5851 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
5852 Values += "'";
5853 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
5854 Values += "'";
5855 switch (i) {
5856 case OMPC_PROC_BIND_unknown - 2:
5857 Values += " or ";
5858 break;
5859 case OMPC_PROC_BIND_unknown - 1:
5860 break;
5861 default:
5862 Values += Sep;
5863 break;
5864 }
5865 }
5866 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00005867 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005868 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005869 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005870 return new (Context)
5871 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005872}
5873
Alexey Bataev56dafe82014-06-20 07:16:17 +00005874OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
5875 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
5876 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005877 SourceLocation ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00005878 SourceLocation EndLoc) {
5879 OMPClause *Res = nullptr;
5880 switch (Kind) {
5881 case OMPC_schedule:
5882 Res = ActOnOpenMPScheduleClause(
5883 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005884 LParenLoc, ArgumentLoc, DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00005885 break;
5886 case OMPC_if:
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005887 Res =
5888 ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument), Expr,
5889 StartLoc, LParenLoc, ArgumentLoc, DelimLoc, EndLoc);
5890 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00005891 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005892 case OMPC_num_threads:
5893 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00005894 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005895 case OMPC_collapse:
5896 case OMPC_default:
5897 case OMPC_proc_bind:
5898 case OMPC_private:
5899 case OMPC_firstprivate:
5900 case OMPC_lastprivate:
5901 case OMPC_shared:
5902 case OMPC_reduction:
5903 case OMPC_linear:
5904 case OMPC_aligned:
5905 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005906 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005907 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00005908 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005909 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005910 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005911 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00005912 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005913 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00005914 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00005915 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00005916 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005917 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005918 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00005919 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00005920 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005921 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00005922 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00005923 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005924 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00005925 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00005926 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00005927 case OMPC_nogroup:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005928 case OMPC_unknown:
5929 llvm_unreachable("Clause is not allowed.");
5930 }
5931 return Res;
5932}
5933
5934OMPClause *Sema::ActOnOpenMPScheduleClause(
5935 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
5936 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
5937 SourceLocation EndLoc) {
5938 if (Kind == OMPC_SCHEDULE_unknown) {
5939 std::string Values;
5940 std::string Sep(", ");
5941 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) {
5942 Values += "'";
5943 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i);
5944 Values += "'";
5945 switch (i) {
5946 case OMPC_SCHEDULE_unknown - 2:
5947 Values += " or ";
5948 break;
5949 case OMPC_SCHEDULE_unknown - 1:
5950 break;
5951 default:
5952 Values += Sep;
5953 break;
5954 }
5955 }
5956 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
5957 << Values << getOpenMPClauseName(OMPC_schedule);
5958 return nullptr;
5959 }
5960 Expr *ValExpr = ChunkSize;
Alexey Bataev040d5402015-05-12 08:35:28 +00005961 Expr *HelperValExpr = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00005962 if (ChunkSize) {
5963 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
5964 !ChunkSize->isInstantiationDependent() &&
5965 !ChunkSize->containsUnexpandedParameterPack()) {
5966 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
5967 ExprResult Val =
5968 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
5969 if (Val.isInvalid())
5970 return nullptr;
5971
5972 ValExpr = Val.get();
5973
5974 // OpenMP [2.7.1, Restrictions]
5975 // chunk_size must be a loop invariant integer expression with a positive
5976 // value.
5977 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00005978 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
5979 if (Result.isSigned() && !Result.isStrictlyPositive()) {
5980 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00005981 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00005982 return nullptr;
5983 }
5984 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
5985 auto *ImpVar = buildVarDecl(*this, ChunkSize->getExprLoc(),
5986 ChunkSize->getType(), ".chunk.");
5987 auto *ImpVarRef = buildDeclRefExpr(*this, ImpVar, ChunkSize->getType(),
5988 ChunkSize->getExprLoc(),
5989 /*RefersToCapture=*/true);
5990 HelperValExpr = ImpVarRef;
Alexey Bataev56dafe82014-06-20 07:16:17 +00005991 }
5992 }
5993 }
5994
5995 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc,
Alexey Bataev040d5402015-05-12 08:35:28 +00005996 EndLoc, Kind, ValExpr, HelperValExpr);
Alexey Bataev56dafe82014-06-20 07:16:17 +00005997}
5998
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005999OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
6000 SourceLocation StartLoc,
6001 SourceLocation EndLoc) {
6002 OMPClause *Res = nullptr;
6003 switch (Kind) {
6004 case OMPC_ordered:
6005 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
6006 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00006007 case OMPC_nowait:
6008 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
6009 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006010 case OMPC_untied:
6011 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
6012 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006013 case OMPC_mergeable:
6014 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
6015 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006016 case OMPC_read:
6017 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
6018 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00006019 case OMPC_write:
6020 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
6021 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006022 case OMPC_update:
6023 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
6024 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00006025 case OMPC_capture:
6026 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
6027 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006028 case OMPC_seq_cst:
6029 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
6030 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00006031 case OMPC_threads:
6032 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
6033 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006034 case OMPC_simd:
6035 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
6036 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00006037 case OMPC_nogroup:
6038 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
6039 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006040 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006041 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006042 case OMPC_num_threads:
6043 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006044 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006045 case OMPC_collapse:
6046 case OMPC_schedule:
6047 case OMPC_private:
6048 case OMPC_firstprivate:
6049 case OMPC_lastprivate:
6050 case OMPC_shared:
6051 case OMPC_reduction:
6052 case OMPC_linear:
6053 case OMPC_aligned:
6054 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006055 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006056 case OMPC_default:
6057 case OMPC_proc_bind:
6058 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006059 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006060 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006061 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006062 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006063 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006064 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006065 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006066 case OMPC_grainsize:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006067 case OMPC_unknown:
6068 llvm_unreachable("Clause is not allowed.");
6069 }
6070 return Res;
6071}
6072
Alexey Bataev236070f2014-06-20 11:19:47 +00006073OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
6074 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006075 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00006076 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
6077}
6078
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006079OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
6080 SourceLocation EndLoc) {
6081 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
6082}
6083
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006084OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
6085 SourceLocation EndLoc) {
6086 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
6087}
6088
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006089OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
6090 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006091 return new (Context) OMPReadClause(StartLoc, EndLoc);
6092}
6093
Alexey Bataevdea47612014-07-23 07:46:59 +00006094OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
6095 SourceLocation EndLoc) {
6096 return new (Context) OMPWriteClause(StartLoc, EndLoc);
6097}
6098
Alexey Bataev67a4f222014-07-23 10:25:33 +00006099OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
6100 SourceLocation EndLoc) {
6101 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
6102}
6103
Alexey Bataev459dec02014-07-24 06:46:57 +00006104OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
6105 SourceLocation EndLoc) {
6106 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
6107}
6108
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006109OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
6110 SourceLocation EndLoc) {
6111 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
6112}
6113
Alexey Bataev346265e2015-09-25 10:37:12 +00006114OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
6115 SourceLocation EndLoc) {
6116 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
6117}
6118
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006119OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
6120 SourceLocation EndLoc) {
6121 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
6122}
6123
Alexey Bataevb825de12015-12-07 10:51:44 +00006124OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
6125 SourceLocation EndLoc) {
6126 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
6127}
6128
Alexey Bataevc5e02582014-06-16 07:08:35 +00006129OMPClause *Sema::ActOnOpenMPVarListClause(
6130 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
6131 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
6132 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006133 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Kelvin Li0bff7af2015-11-23 05:32:03 +00006134 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
6135 OpenMPMapClauseKind MapType, SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006136 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006137 switch (Kind) {
6138 case OMPC_private:
6139 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6140 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006141 case OMPC_firstprivate:
6142 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6143 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00006144 case OMPC_lastprivate:
6145 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6146 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00006147 case OMPC_shared:
6148 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
6149 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006150 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00006151 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
6152 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006153 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00006154 case OMPC_linear:
6155 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00006156 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00006157 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006158 case OMPC_aligned:
6159 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
6160 ColonLoc, EndLoc);
6161 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006162 case OMPC_copyin:
6163 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
6164 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00006165 case OMPC_copyprivate:
6166 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6167 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00006168 case OMPC_flush:
6169 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
6170 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006171 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006172 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
6173 StartLoc, LParenLoc, EndLoc);
6174 break;
6175 case OMPC_map:
6176 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, DepLinMapLoc, ColonLoc,
6177 VarList, StartLoc, LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006178 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006179 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006180 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00006181 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00006182 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006183 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00006184 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006185 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006186 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006187 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006188 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006189 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006190 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006191 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006192 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006193 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006194 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006195 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006196 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006197 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00006198 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006199 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006200 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006201 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006202 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006203 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006204 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006205 case OMPC_nogroup:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006206 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006207 llvm_unreachable("Clause is not allowed.");
6208 }
6209 return Res;
6210}
6211
6212OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
6213 SourceLocation StartLoc,
6214 SourceLocation LParenLoc,
6215 SourceLocation EndLoc) {
6216 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00006217 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00006218 for (auto &RefExpr : VarList) {
6219 assert(RefExpr && "NULL expr in OpenMP private clause.");
6220 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006221 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006222 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006223 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006224 continue;
6225 }
6226
Alexey Bataeved09d242014-05-28 05:53:51 +00006227 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006228 // OpenMP [2.1, C/C++]
6229 // A list item is a variable name.
6230 // OpenMP [2.9.3.3, Restrictions, p.1]
6231 // A variable that is part of another variable (as an array or
6232 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00006233 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006234 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006235 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006236 continue;
6237 }
6238 Decl *D = DE->getDecl();
6239 VarDecl *VD = cast<VarDecl>(D);
6240
6241 QualType Type = VD->getType();
6242 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6243 // It will be analyzed later.
6244 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006245 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006246 continue;
6247 }
6248
6249 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
6250 // A variable that appears in a private clause must not have an incomplete
6251 // type or a reference type.
6252 if (RequireCompleteType(ELoc, Type,
6253 diag::err_omp_private_incomplete_type)) {
6254 continue;
6255 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006256 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006257
Alexey Bataev758e55e2013-09-06 18:03:48 +00006258 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6259 // in a Construct]
6260 // Variables with the predetermined data-sharing attributes may not be
6261 // listed in data-sharing attributes clauses, except for the cases
6262 // listed below. For these exceptions only, listing a predetermined
6263 // variable in a data-sharing attribute clause is allowed and overrides
6264 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006265 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006266 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006267 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
6268 << getOpenMPClauseName(OMPC_private);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006269 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006270 continue;
6271 }
6272
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006273 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00006274 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006275 DSAStack->getCurrentDirective() == OMPD_task) {
6276 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
6277 << getOpenMPClauseName(OMPC_private) << Type
6278 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
6279 bool IsDecl =
6280 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6281 Diag(VD->getLocation(),
6282 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6283 << VD;
6284 continue;
6285 }
6286
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006287 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
6288 // A variable of class type (or array thereof) that appears in a private
6289 // clause requires an accessible, unambiguous default constructor for the
6290 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00006291 // Generate helper private variable and initialize it with the default
6292 // value. The address of the original variable is replaced by the address of
6293 // the new private variable in CodeGen. This new variable is not added to
6294 // IdResolver, so the code in the OpenMP region uses original variable for
6295 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006296 Type = Type.getUnqualifiedType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006297 auto VDPrivate = buildVarDecl(*this, DE->getExprLoc(), Type, VD->getName(),
6298 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00006299 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006300 if (VDPrivate->isInvalidDecl())
6301 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006302 auto VDPrivateRefExpr = buildDeclRefExpr(
6303 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev03b340a2014-10-21 03:16:40 +00006304
Alexey Bataev758e55e2013-09-06 18:03:48 +00006305 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006306 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006307 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006308 }
6309
Alexey Bataeved09d242014-05-28 05:53:51 +00006310 if (Vars.empty())
6311 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006312
Alexey Bataev03b340a2014-10-21 03:16:40 +00006313 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
6314 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006315}
6316
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006317namespace {
6318class DiagsUninitializedSeveretyRAII {
6319private:
6320 DiagnosticsEngine &Diags;
6321 SourceLocation SavedLoc;
6322 bool IsIgnored;
6323
6324public:
6325 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
6326 bool IsIgnored)
6327 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
6328 if (!IsIgnored) {
6329 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
6330 /*Map*/ diag::Severity::Ignored, Loc);
6331 }
6332 }
6333 ~DiagsUninitializedSeveretyRAII() {
6334 if (!IsIgnored)
6335 Diags.popMappings(SavedLoc);
6336 }
6337};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00006338}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006339
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006340OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
6341 SourceLocation StartLoc,
6342 SourceLocation LParenLoc,
6343 SourceLocation EndLoc) {
6344 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006345 SmallVector<Expr *, 8> PrivateCopies;
6346 SmallVector<Expr *, 8> Inits;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006347 bool IsImplicitClause =
6348 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
6349 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
6350
Alexey Bataeved09d242014-05-28 05:53:51 +00006351 for (auto &RefExpr : VarList) {
6352 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
6353 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006354 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006355 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006356 PrivateCopies.push_back(nullptr);
6357 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006358 continue;
6359 }
6360
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006361 SourceLocation ELoc =
6362 IsImplicitClause ? ImplicitClauseLoc : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006363 // OpenMP [2.1, C/C++]
6364 // A list item is a variable name.
6365 // OpenMP [2.9.3.3, Restrictions, p.1]
6366 // A variable that is part of another variable (as an array or
6367 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00006368 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006369 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006370 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006371 continue;
6372 }
6373 Decl *D = DE->getDecl();
6374 VarDecl *VD = cast<VarDecl>(D);
6375
6376 QualType Type = VD->getType();
6377 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6378 // It will be analyzed later.
6379 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006380 PrivateCopies.push_back(nullptr);
6381 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006382 continue;
6383 }
6384
6385 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
6386 // A variable that appears in a private clause must not have an incomplete
6387 // type or a reference type.
6388 if (RequireCompleteType(ELoc, Type,
6389 diag::err_omp_firstprivate_incomplete_type)) {
6390 continue;
6391 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006392 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006393
6394 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
6395 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00006396 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006397 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006398 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006399
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006400 // If an implicit firstprivate variable found it was checked already.
6401 if (!IsImplicitClause) {
6402 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006403 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006404 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
6405 // A list item that specifies a given variable may not appear in more
6406 // than one clause on the same directive, except that a variable may be
6407 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006408 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00006409 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006410 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00006411 << getOpenMPClauseName(DVar.CKind)
6412 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006413 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006414 continue;
6415 }
6416
6417 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6418 // in a Construct]
6419 // Variables with the predetermined data-sharing attributes may not be
6420 // listed in data-sharing attributes clauses, except for the cases
6421 // listed below. For these exceptions only, listing a predetermined
6422 // variable in a data-sharing attribute clause is allowed and overrides
6423 // the variable's predetermined data-sharing attributes.
6424 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6425 // in a Construct, C/C++, p.2]
6426 // Variables with const-qualified type having no mutable member may be
6427 // listed in a firstprivate clause, even if they are static data members.
6428 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
6429 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
6430 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00006431 << getOpenMPClauseName(DVar.CKind)
6432 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006433 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006434 continue;
6435 }
6436
Alexey Bataevf29276e2014-06-18 04:14:57 +00006437 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006438 // OpenMP [2.9.3.4, Restrictions, p.2]
6439 // A list item that is private within a parallel region must not appear
6440 // in a firstprivate clause on a worksharing construct if any of the
6441 // worksharing regions arising from the worksharing construct ever bind
6442 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00006443 if (isOpenMPWorksharingDirective(CurrDir) &&
6444 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006445 DVar = DSAStack->getImplicitDSA(VD, true);
6446 if (DVar.CKind != OMPC_shared &&
6447 (isOpenMPParallelDirective(DVar.DKind) ||
6448 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00006449 Diag(ELoc, diag::err_omp_required_access)
6450 << getOpenMPClauseName(OMPC_firstprivate)
6451 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006452 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006453 continue;
6454 }
6455 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006456 // OpenMP [2.9.3.4, Restrictions, p.3]
6457 // A list item that appears in a reduction clause of a parallel construct
6458 // must not appear in a firstprivate clause on a worksharing or task
6459 // construct if any of the worksharing or task regions arising from the
6460 // worksharing or task construct ever bind to any of the parallel regions
6461 // arising from the parallel construct.
6462 // OpenMP [2.9.3.4, Restrictions, p.4]
6463 // A list item that appears in a reduction clause in worksharing
6464 // construct must not appear in a firstprivate clause in a task construct
6465 // encountered during execution of any of the worksharing regions arising
6466 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006467 if (CurrDir == OMPD_task) {
6468 DVar =
6469 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
6470 [](OpenMPDirectiveKind K) -> bool {
6471 return isOpenMPParallelDirective(K) ||
6472 isOpenMPWorksharingDirective(K);
6473 },
6474 false);
6475 if (DVar.CKind == OMPC_reduction &&
6476 (isOpenMPParallelDirective(DVar.DKind) ||
6477 isOpenMPWorksharingDirective(DVar.DKind))) {
6478 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
6479 << getOpenMPDirectiveName(DVar.DKind);
6480 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6481 continue;
6482 }
6483 }
Carlo Bertollib9bfa752015-12-08 04:21:03 +00006484
6485 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
6486 // A list item that is private within a teams region must not appear in a
6487 // firstprivate clause on a distribute construct if any of the distribute
6488 // regions arising from the distribute construct ever bind to any of the
6489 // teams regions arising from the teams construct.
6490 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
6491 // A list item that appears in a reduction clause of a teams construct
6492 // must not appear in a firstprivate clause on a distribute construct if
6493 // any of the distribute regions arising from the distribute construct
6494 // ever bind to any of the teams regions arising from the teams construct.
6495 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
6496 // A list item may appear in a firstprivate or lastprivate clause but not
6497 // both.
6498 if (CurrDir == OMPD_distribute) {
6499 DVar = DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_private),
6500 [](OpenMPDirectiveKind K) -> bool {
6501 return isOpenMPTeamsDirective(K);
6502 },
6503 false);
6504 if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
6505 Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
6506 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6507 continue;
6508 }
6509 DVar = DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
6510 [](OpenMPDirectiveKind K) -> bool {
6511 return isOpenMPTeamsDirective(K);
6512 },
6513 false);
6514 if (DVar.CKind == OMPC_reduction &&
6515 isOpenMPTeamsDirective(DVar.DKind)) {
6516 Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
6517 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6518 continue;
6519 }
6520 DVar = DSAStack->getTopDSA(VD, false);
6521 if (DVar.CKind == OMPC_lastprivate) {
6522 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
6523 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6524 continue;
6525 }
6526 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006527 }
6528
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006529 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00006530 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006531 DSAStack->getCurrentDirective() == OMPD_task) {
6532 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
6533 << getOpenMPClauseName(OMPC_firstprivate) << Type
6534 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
6535 bool IsDecl =
6536 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6537 Diag(VD->getLocation(),
6538 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6539 << VD;
6540 continue;
6541 }
6542
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006543 Type = Type.getUnqualifiedType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006544 auto VDPrivate = buildVarDecl(*this, ELoc, Type, VD->getName(),
6545 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006546 // Generate helper private variable and initialize it with the value of the
6547 // original variable. The address of the original variable is replaced by
6548 // the address of the new private variable in the CodeGen. This new variable
6549 // is not added to IdResolver, so the code in the OpenMP region uses
6550 // original variable for proper diagnostics and variable capturing.
6551 Expr *VDInitRefExpr = nullptr;
6552 // For arrays generate initializer for single element and replace it by the
6553 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006554 if (Type->isArrayType()) {
6555 auto VDInit =
6556 buildVarDecl(*this, DE->getExprLoc(), ElemType, VD->getName());
6557 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006558 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006559 ElemType = ElemType.getUnqualifiedType();
6560 auto *VDInitTemp = buildVarDecl(*this, DE->getLocStart(), ElemType,
6561 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00006562 InitializedEntity Entity =
6563 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006564 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
6565
6566 InitializationSequence InitSeq(*this, Entity, Kind, Init);
6567 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
6568 if (Result.isInvalid())
6569 VDPrivate->setInvalidDecl();
6570 else
6571 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006572 // Remove temp variable declaration.
6573 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006574 } else {
Alexey Bataev69c62a92015-04-15 04:52:20 +00006575 auto *VDInit =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006576 buildVarDecl(*this, DE->getLocStart(), Type, ".firstprivate.temp");
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006577 VDInitRefExpr =
6578 buildDeclRefExpr(*this, VDInit, DE->getType(), DE->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00006579 AddInitializerToDecl(VDPrivate,
6580 DefaultLvalueConversion(VDInitRefExpr).get(),
6581 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006582 }
6583 if (VDPrivate->isInvalidDecl()) {
6584 if (IsImplicitClause) {
6585 Diag(DE->getExprLoc(),
6586 diag::note_omp_task_predetermined_firstprivate_here);
6587 }
6588 continue;
6589 }
6590 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00006591 auto VDPrivateRefExpr = buildDeclRefExpr(
6592 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006593 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
6594 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006595 PrivateCopies.push_back(VDPrivateRefExpr);
6596 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006597 }
6598
Alexey Bataeved09d242014-05-28 05:53:51 +00006599 if (Vars.empty())
6600 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006601
6602 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006603 Vars, PrivateCopies, Inits);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006604}
6605
Alexander Musman1bb328c2014-06-04 13:06:39 +00006606OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
6607 SourceLocation StartLoc,
6608 SourceLocation LParenLoc,
6609 SourceLocation EndLoc) {
6610 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00006611 SmallVector<Expr *, 8> SrcExprs;
6612 SmallVector<Expr *, 8> DstExprs;
6613 SmallVector<Expr *, 8> AssignmentOps;
Alexander Musman1bb328c2014-06-04 13:06:39 +00006614 for (auto &RefExpr : VarList) {
6615 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
6616 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6617 // It will be analyzed later.
6618 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00006619 SrcExprs.push_back(nullptr);
6620 DstExprs.push_back(nullptr);
6621 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006622 continue;
6623 }
6624
6625 SourceLocation ELoc = RefExpr->getExprLoc();
6626 // OpenMP [2.1, C/C++]
6627 // A list item is a variable name.
6628 // OpenMP [2.14.3.5, Restrictions, p.1]
6629 // A variable that is part of another variable (as an array or structure
6630 // element) cannot appear in a lastprivate clause.
6631 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
6632 if (!DE || !isa<VarDecl>(DE->getDecl())) {
6633 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
6634 continue;
6635 }
6636 Decl *D = DE->getDecl();
6637 VarDecl *VD = cast<VarDecl>(D);
6638
6639 QualType Type = VD->getType();
6640 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6641 // It will be analyzed later.
6642 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00006643 SrcExprs.push_back(nullptr);
6644 DstExprs.push_back(nullptr);
6645 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006646 continue;
6647 }
6648
6649 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
6650 // A variable that appears in a lastprivate clause must not have an
6651 // incomplete type or a reference type.
6652 if (RequireCompleteType(ELoc, Type,
6653 diag::err_omp_lastprivate_incomplete_type)) {
6654 continue;
6655 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006656 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00006657
6658 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
6659 // in a Construct]
6660 // Variables with the predetermined data-sharing attributes may not be
6661 // listed in data-sharing attributes clauses, except for the cases
6662 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006663 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006664 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
6665 DVar.CKind != OMPC_firstprivate &&
6666 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
6667 Diag(ELoc, diag::err_omp_wrong_dsa)
6668 << getOpenMPClauseName(DVar.CKind)
6669 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006670 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006671 continue;
6672 }
6673
Alexey Bataevf29276e2014-06-18 04:14:57 +00006674 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
6675 // OpenMP [2.14.3.5, Restrictions, p.2]
6676 // A list item that is private within a parallel region, or that appears in
6677 // the reduction clause of a parallel construct, must not appear in a
6678 // lastprivate clause on a worksharing construct if any of the corresponding
6679 // worksharing regions ever binds to any of the corresponding parallel
6680 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00006681 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00006682 if (isOpenMPWorksharingDirective(CurrDir) &&
6683 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006684 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006685 if (DVar.CKind != OMPC_shared) {
6686 Diag(ELoc, diag::err_omp_required_access)
6687 << getOpenMPClauseName(OMPC_lastprivate)
6688 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006689 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006690 continue;
6691 }
6692 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00006693 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00006694 // A variable of class type (or array thereof) that appears in a
6695 // lastprivate clause requires an accessible, unambiguous default
6696 // constructor for the class type, unless the list item is also specified
6697 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00006698 // A variable of class type (or array thereof) that appears in a
6699 // lastprivate clause requires an accessible, unambiguous copy assignment
6700 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00006701 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00006702 auto *SrcVD = buildVarDecl(*this, DE->getLocStart(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006703 Type.getUnqualifiedType(), ".lastprivate.src",
6704 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00006705 auto *PseudoSrcExpr = buildDeclRefExpr(
6706 *this, SrcVD, Type.getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00006707 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006708 buildVarDecl(*this, DE->getLocStart(), Type, ".lastprivate.dst",
6709 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00006710 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006711 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00006712 // For arrays generate assignment operation for single element and replace
6713 // it by the original array element in CodeGen.
6714 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
6715 PseudoDstExpr, PseudoSrcExpr);
6716 if (AssignmentOp.isInvalid())
6717 continue;
6718 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
6719 /*DiscardedValue=*/true);
6720 if (AssignmentOp.isInvalid())
6721 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00006722
Carlo Bertollib9bfa752015-12-08 04:21:03 +00006723 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
6724 // A list item may appear in a firstprivate or lastprivate clause but not
6725 // both.
6726 if (CurrDir == OMPD_distribute) {
6727 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
6728 if (DVar.CKind == OMPC_firstprivate) {
6729 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
6730 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6731 continue;
6732 }
6733 }
6734
Alexey Bataev39f915b82015-05-08 10:41:21 +00006735 if (TopDVar.CKind != OMPC_firstprivate)
Alexey Bataevf29276e2014-06-18 04:14:57 +00006736 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006737 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00006738 SrcExprs.push_back(PseudoSrcExpr);
6739 DstExprs.push_back(PseudoDstExpr);
6740 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00006741 }
6742
6743 if (Vars.empty())
6744 return nullptr;
6745
6746 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00006747 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006748}
6749
Alexey Bataev758e55e2013-09-06 18:03:48 +00006750OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
6751 SourceLocation StartLoc,
6752 SourceLocation LParenLoc,
6753 SourceLocation EndLoc) {
6754 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00006755 for (auto &RefExpr : VarList) {
6756 assert(RefExpr && "NULL expr in OpenMP shared clause.");
6757 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00006758 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006759 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006760 continue;
6761 }
6762
Alexey Bataeved09d242014-05-28 05:53:51 +00006763 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00006764 // OpenMP [2.1, C/C++]
6765 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00006766 // OpenMP [2.14.3.2, Restrictions, p.1]
6767 // A variable that is part of another variable (as an array or structure
6768 // element) cannot appear in a shared unless it is a static data member
6769 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00006770 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006771 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006772 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00006773 continue;
6774 }
6775 Decl *D = DE->getDecl();
6776 VarDecl *VD = cast<VarDecl>(D);
6777
6778 QualType Type = VD->getType();
6779 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6780 // It will be analyzed later.
6781 Vars.push_back(DE);
6782 continue;
6783 }
6784
6785 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6786 // in a Construct]
6787 // Variables with the predetermined data-sharing attributes may not be
6788 // listed in data-sharing attributes clauses, except for the cases
6789 // listed below. For these exceptions only, listing a predetermined
6790 // variable in a data-sharing attribute clause is allowed and overrides
6791 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006792 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00006793 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
6794 DVar.RefExpr) {
6795 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
6796 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006797 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006798 continue;
6799 }
6800
6801 DSAStack->addDSA(VD, DE, OMPC_shared);
6802 Vars.push_back(DE);
6803 }
6804
Alexey Bataeved09d242014-05-28 05:53:51 +00006805 if (Vars.empty())
6806 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00006807
6808 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
6809}
6810
Alexey Bataevc5e02582014-06-16 07:08:35 +00006811namespace {
6812class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
6813 DSAStackTy *Stack;
6814
6815public:
6816 bool VisitDeclRefExpr(DeclRefExpr *E) {
6817 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006818 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006819 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
6820 return false;
6821 if (DVar.CKind != OMPC_unknown)
6822 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00006823 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006824 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006825 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00006826 return true;
6827 return false;
6828 }
6829 return false;
6830 }
6831 bool VisitStmt(Stmt *S) {
6832 for (auto Child : S->children()) {
6833 if (Child && Visit(Child))
6834 return true;
6835 }
6836 return false;
6837 }
Alexey Bataev23b69422014-06-18 07:08:49 +00006838 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00006839};
Alexey Bataev23b69422014-06-18 07:08:49 +00006840} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00006841
6842OMPClause *Sema::ActOnOpenMPReductionClause(
6843 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
6844 SourceLocation ColonLoc, SourceLocation EndLoc,
6845 CXXScopeSpec &ReductionIdScopeSpec,
6846 const DeclarationNameInfo &ReductionId) {
6847 // TODO: Allow scope specification search when 'declare reduction' is
6848 // supported.
6849 assert(ReductionIdScopeSpec.isEmpty() &&
6850 "No support for scoped reduction identifiers yet.");
6851
6852 auto DN = ReductionId.getName();
6853 auto OOK = DN.getCXXOverloadedOperator();
6854 BinaryOperatorKind BOK = BO_Comma;
6855
6856 // OpenMP [2.14.3.6, reduction clause]
6857 // C
6858 // reduction-identifier is either an identifier or one of the following
6859 // operators: +, -, *, &, |, ^, && and ||
6860 // C++
6861 // reduction-identifier is either an id-expression or one of the following
6862 // operators: +, -, *, &, |, ^, && and ||
6863 // FIXME: Only 'min' and 'max' identifiers are supported for now.
6864 switch (OOK) {
6865 case OO_Plus:
6866 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006867 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006868 break;
6869 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006870 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006871 break;
6872 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006873 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006874 break;
6875 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006876 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006877 break;
6878 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006879 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006880 break;
6881 case OO_AmpAmp:
6882 BOK = BO_LAnd;
6883 break;
6884 case OO_PipePipe:
6885 BOK = BO_LOr;
6886 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006887 case OO_New:
6888 case OO_Delete:
6889 case OO_Array_New:
6890 case OO_Array_Delete:
6891 case OO_Slash:
6892 case OO_Percent:
6893 case OO_Tilde:
6894 case OO_Exclaim:
6895 case OO_Equal:
6896 case OO_Less:
6897 case OO_Greater:
6898 case OO_LessEqual:
6899 case OO_GreaterEqual:
6900 case OO_PlusEqual:
6901 case OO_MinusEqual:
6902 case OO_StarEqual:
6903 case OO_SlashEqual:
6904 case OO_PercentEqual:
6905 case OO_CaretEqual:
6906 case OO_AmpEqual:
6907 case OO_PipeEqual:
6908 case OO_LessLess:
6909 case OO_GreaterGreater:
6910 case OO_LessLessEqual:
6911 case OO_GreaterGreaterEqual:
6912 case OO_EqualEqual:
6913 case OO_ExclaimEqual:
6914 case OO_PlusPlus:
6915 case OO_MinusMinus:
6916 case OO_Comma:
6917 case OO_ArrowStar:
6918 case OO_Arrow:
6919 case OO_Call:
6920 case OO_Subscript:
6921 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +00006922 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006923 case NUM_OVERLOADED_OPERATORS:
6924 llvm_unreachable("Unexpected reduction identifier");
6925 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00006926 if (auto II = DN.getAsIdentifierInfo()) {
6927 if (II->isStr("max"))
6928 BOK = BO_GT;
6929 else if (II->isStr("min"))
6930 BOK = BO_LT;
6931 }
6932 break;
6933 }
6934 SourceRange ReductionIdRange;
6935 if (ReductionIdScopeSpec.isValid()) {
6936 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
6937 }
6938 ReductionIdRange.setEnd(ReductionId.getEndLoc());
6939 if (BOK == BO_Comma) {
6940 // Not allowed reduction identifier is found.
6941 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
6942 << ReductionIdRange;
6943 return nullptr;
6944 }
6945
6946 SmallVector<Expr *, 8> Vars;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006947 SmallVector<Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006948 SmallVector<Expr *, 8> LHSs;
6949 SmallVector<Expr *, 8> RHSs;
6950 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006951 for (auto RefExpr : VarList) {
6952 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
6953 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6954 // It will be analyzed later.
6955 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006956 Privates.push_back(nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006957 LHSs.push_back(nullptr);
6958 RHSs.push_back(nullptr);
6959 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006960 continue;
6961 }
6962
6963 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
6964 RefExpr->isInstantiationDependent() ||
6965 RefExpr->containsUnexpandedParameterPack()) {
6966 // It will be analyzed later.
6967 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006968 Privates.push_back(nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006969 LHSs.push_back(nullptr);
6970 RHSs.push_back(nullptr);
6971 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006972 continue;
6973 }
6974
6975 auto ELoc = RefExpr->getExprLoc();
6976 auto ERange = RefExpr->getSourceRange();
6977 // OpenMP [2.1, C/C++]
6978 // A list item is a variable or array section, subject to the restrictions
6979 // specified in Section 2.4 on page 42 and in each of the sections
6980 // describing clauses and directives for which a list appears.
6981 // OpenMP [2.14.3.3, Restrictions, p.1]
6982 // A variable that is part of another variable (as an array or
6983 // structure element) cannot appear in a private clause.
Alexey Bataeva1764212015-09-30 09:22:36 +00006984 auto *DE = dyn_cast<DeclRefExpr>(RefExpr);
6985 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr);
6986 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr);
6987 if (!ASE && !OASE && (!DE || !isa<VarDecl>(DE->getDecl()))) {
6988 Diag(ELoc, diag::err_omp_expected_var_name_or_array_item) << ERange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006989 continue;
6990 }
Alexey Bataeva1764212015-09-30 09:22:36 +00006991 QualType Type;
6992 VarDecl *VD = nullptr;
6993 if (DE) {
6994 auto D = DE->getDecl();
6995 VD = cast<VarDecl>(D);
6996 Type = VD->getType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006997 } else if (ASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00006998 Type = ASE->getType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006999 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
7000 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7001 Base = TempASE->getBase()->IgnoreParenImpCasts();
7002 DE = dyn_cast<DeclRefExpr>(Base);
7003 if (DE)
7004 VD = dyn_cast<VarDecl>(DE->getDecl());
7005 if (!VD) {
7006 Diag(Base->getExprLoc(), diag::err_omp_expected_base_var_name)
7007 << 0 << Base->getSourceRange();
7008 continue;
7009 }
7010 } else if (OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007011 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
7012 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
7013 Type = ATy->getElementType();
7014 else
7015 Type = BaseType->getPointeeType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007016 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
7017 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
7018 Base = TempOASE->getBase()->IgnoreParenImpCasts();
7019 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7020 Base = TempASE->getBase()->IgnoreParenImpCasts();
7021 DE = dyn_cast<DeclRefExpr>(Base);
7022 if (DE)
7023 VD = dyn_cast<VarDecl>(DE->getDecl());
7024 if (!VD) {
7025 Diag(Base->getExprLoc(), diag::err_omp_expected_base_var_name)
7026 << 1 << Base->getSourceRange();
7027 continue;
7028 }
Alexey Bataeva1764212015-09-30 09:22:36 +00007029 }
7030
Alexey Bataevc5e02582014-06-16 07:08:35 +00007031 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7032 // A variable that appears in a private clause must not have an incomplete
7033 // type or a reference type.
7034 if (RequireCompleteType(ELoc, Type,
7035 diag::err_omp_reduction_incomplete_type))
7036 continue;
7037 // OpenMP [2.14.3.6, reduction clause, Restrictions]
7038 // Arrays may not appear in a reduction clause.
7039 if (Type.getNonReferenceType()->isArrayType()) {
7040 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007041 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007042 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7043 VarDecl::DeclarationOnly;
7044 Diag(VD->getLocation(),
7045 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7046 << VD;
7047 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007048 continue;
7049 }
7050 // OpenMP [2.14.3.6, reduction clause, Restrictions]
7051 // A list item that appears in a reduction clause must not be
7052 // const-qualified.
7053 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007054 Diag(ELoc, diag::err_omp_const_reduction_list_item)
Alexey Bataevc5e02582014-06-16 07:08:35 +00007055 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007056 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007057 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7058 VarDecl::DeclarationOnly;
7059 Diag(VD->getLocation(),
7060 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7061 << VD;
7062 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007063 continue;
7064 }
7065 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
7066 // If a list-item is a reference type then it must bind to the same object
7067 // for all threads of the team.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007068 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007069 VarDecl *VDDef = VD->getDefinition();
7070 if (Type->isReferenceType() && VDDef) {
7071 DSARefChecker Check(DSAStack);
7072 if (Check.Visit(VDDef->getInit())) {
7073 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
7074 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
7075 continue;
7076 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007077 }
7078 }
7079 // OpenMP [2.14.3.6, reduction clause, Restrictions]
7080 // The type of a list item that appears in a reduction clause must be valid
7081 // for the reduction-identifier. For a max or min reduction in C, the type
7082 // of the list item must be an allowed arithmetic data type: char, int,
7083 // float, double, or _Bool, possibly modified with long, short, signed, or
7084 // unsigned. For a max or min reduction in C++, the type of the list item
7085 // must be an allowed arithmetic data type: char, wchar_t, int, float,
7086 // double, or bool, possibly modified with long, short, signed, or unsigned.
7087 if ((BOK == BO_GT || BOK == BO_LT) &&
7088 !(Type->isScalarType() ||
7089 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
7090 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
7091 << getLangOpts().CPlusPlus;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007092 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007093 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7094 VarDecl::DeclarationOnly;
7095 Diag(VD->getLocation(),
7096 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7097 << VD;
7098 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007099 continue;
7100 }
7101 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
7102 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
7103 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007104 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007105 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7106 VarDecl::DeclarationOnly;
7107 Diag(VD->getLocation(),
7108 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7109 << VD;
7110 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007111 continue;
7112 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007113 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
7114 // in a Construct]
7115 // Variables with the predetermined data-sharing attributes may not be
7116 // listed in data-sharing attributes clauses, except for the cases
7117 // listed below. For these exceptions only, listing a predetermined
7118 // variable in a data-sharing attribute clause is allowed and overrides
7119 // the variable's predetermined data-sharing attributes.
7120 // OpenMP [2.14.3.6, Restrictions, p.3]
7121 // Any number of reduction clauses can be specified on the directive,
7122 // but a list item can appear only once in the reduction clauses for that
7123 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +00007124 DSAStackTy::DSAVarData DVar;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007125 DVar = DSAStack->getTopDSA(VD, false);
7126 if (DVar.CKind == OMPC_reduction) {
7127 Diag(ELoc, diag::err_omp_once_referenced)
7128 << getOpenMPClauseName(OMPC_reduction);
7129 if (DVar.RefExpr) {
7130 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007131 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007132 } else if (DVar.CKind != OMPC_unknown) {
7133 Diag(ELoc, diag::err_omp_wrong_dsa)
7134 << getOpenMPClauseName(DVar.CKind)
7135 << getOpenMPClauseName(OMPC_reduction);
7136 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7137 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007138 }
7139
7140 // OpenMP [2.14.3.6, Restrictions, p.1]
7141 // A list item that appears in a reduction clause of a worksharing
7142 // construct must be shared in the parallel regions to which any of the
7143 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007144 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
7145 if (isOpenMPWorksharingDirective(CurrDir) &&
7146 !isOpenMPParallelDirective(CurrDir)) {
7147 DVar = DSAStack->getImplicitDSA(VD, true);
7148 if (DVar.CKind != OMPC_shared) {
7149 Diag(ELoc, diag::err_omp_required_access)
7150 << getOpenMPClauseName(OMPC_reduction)
7151 << getOpenMPClauseName(OMPC_shared);
7152 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7153 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +00007154 }
7155 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007156
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007157 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007158 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
7159 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
7160 auto *RHSVD = buildVarDecl(*this, ELoc, Type, VD->getName(),
7161 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
7162 auto PrivateTy = Type;
7163 if (OASE) {
7164 // For array sections only:
7165 // Create pseudo array type for private copy. The size for this array will
7166 // be generated during codegen.
7167 // For array subscripts or single variables Private Ty is the same as Type
7168 // (type of the variable or single array element).
7169 PrivateTy = Context.getVariableArrayType(
7170 Type, new (Context) OpaqueValueExpr(SourceLocation(),
7171 Context.getSizeType(), VK_RValue),
7172 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
7173 }
7174 // Private copy.
7175 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, VD->getName(),
7176 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007177 // Add initializer for private variable.
7178 Expr *Init = nullptr;
7179 switch (BOK) {
7180 case BO_Add:
7181 case BO_Xor:
7182 case BO_Or:
7183 case BO_LOr:
7184 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
7185 if (Type->isScalarType() || Type->isAnyComplexType()) {
7186 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00007187 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007188 break;
7189 case BO_Mul:
7190 case BO_LAnd:
7191 if (Type->isScalarType() || Type->isAnyComplexType()) {
7192 // '*' and '&&' reduction ops - initializer is '1'.
7193 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
7194 }
7195 break;
7196 case BO_And: {
7197 // '&' reduction op - initializer is '~0'.
7198 QualType OrigType = Type;
7199 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) {
7200 Type = ComplexTy->getElementType();
7201 }
7202 if (Type->isRealFloatingType()) {
7203 llvm::APFloat InitValue =
7204 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
7205 /*isIEEE=*/true);
7206 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
7207 Type, ELoc);
7208 } else if (Type->isScalarType()) {
7209 auto Size = Context.getTypeSize(Type);
7210 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
7211 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
7212 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
7213 }
7214 if (Init && OrigType->isAnyComplexType()) {
7215 // Init = 0xFFFF + 0xFFFFi;
7216 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
7217 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
7218 }
7219 Type = OrigType;
7220 break;
7221 }
7222 case BO_LT:
7223 case BO_GT: {
7224 // 'min' reduction op - initializer is 'Largest representable number in
7225 // the reduction list item type'.
7226 // 'max' reduction op - initializer is 'Least representable number in
7227 // the reduction list item type'.
7228 if (Type->isIntegerType() || Type->isPointerType()) {
7229 bool IsSigned = Type->hasSignedIntegerRepresentation();
7230 auto Size = Context.getTypeSize(Type);
7231 QualType IntTy =
7232 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
7233 llvm::APInt InitValue =
7234 (BOK != BO_LT)
7235 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
7236 : llvm::APInt::getMinValue(Size)
7237 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
7238 : llvm::APInt::getMaxValue(Size);
7239 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
7240 if (Type->isPointerType()) {
7241 // Cast to pointer type.
7242 auto CastExpr = BuildCStyleCastExpr(
7243 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
7244 SourceLocation(), Init);
7245 if (CastExpr.isInvalid())
7246 continue;
7247 Init = CastExpr.get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00007248 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007249 } else if (Type->isRealFloatingType()) {
7250 llvm::APFloat InitValue = llvm::APFloat::getLargest(
7251 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
7252 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
7253 Type, ELoc);
7254 }
7255 break;
7256 }
7257 case BO_PtrMemD:
7258 case BO_PtrMemI:
7259 case BO_MulAssign:
7260 case BO_Div:
7261 case BO_Rem:
7262 case BO_Sub:
7263 case BO_Shl:
7264 case BO_Shr:
7265 case BO_LE:
7266 case BO_GE:
7267 case BO_EQ:
7268 case BO_NE:
7269 case BO_AndAssign:
7270 case BO_XorAssign:
7271 case BO_OrAssign:
7272 case BO_Assign:
7273 case BO_AddAssign:
7274 case BO_SubAssign:
7275 case BO_DivAssign:
7276 case BO_RemAssign:
7277 case BO_ShlAssign:
7278 case BO_ShrAssign:
7279 case BO_Comma:
7280 llvm_unreachable("Unexpected reduction operation");
7281 }
7282 if (Init) {
7283 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
7284 /*TypeMayContainAuto=*/false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007285 } else
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007286 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007287 if (!RHSVD->hasInit()) {
7288 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
7289 << ReductionIdRange;
Alexey Bataeva1764212015-09-30 09:22:36 +00007290 if (VD) {
7291 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7292 VarDecl::DeclarationOnly;
7293 Diag(VD->getLocation(),
7294 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7295 << VD;
7296 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007297 continue;
7298 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007299 // Store initializer for single element in private copy. Will be used during
7300 // codegen.
7301 PrivateVD->setInit(RHSVD->getInit());
7302 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataev39f915b82015-05-08 10:41:21 +00007303 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
7304 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007305 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007306 ExprResult ReductionOp =
7307 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
7308 LHSDRE, RHSDRE);
7309 if (ReductionOp.isUsable()) {
Alexey Bataev69a47792015-05-07 03:54:03 +00007310 if (BOK != BO_LT && BOK != BO_GT) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007311 ReductionOp =
7312 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
7313 BO_Assign, LHSDRE, ReductionOp.get());
7314 } else {
7315 auto *ConditionalOp = new (Context) ConditionalOperator(
7316 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
7317 RHSDRE, Type, VK_LValue, OK_Ordinary);
7318 ReductionOp =
7319 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
7320 BO_Assign, LHSDRE, ConditionalOp);
7321 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007322 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00007323 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007324 if (ReductionOp.isInvalid())
7325 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007326
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007327 DSAStack->addDSA(VD, DE, OMPC_reduction);
Alexey Bataeva1764212015-09-30 09:22:36 +00007328 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007329 Privates.push_back(PrivateDRE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007330 LHSs.push_back(LHSDRE);
7331 RHSs.push_back(RHSDRE);
7332 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00007333 }
7334
7335 if (Vars.empty())
7336 return nullptr;
7337
7338 return OMPReductionClause::Create(
7339 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007340 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
7341 LHSs, RHSs, ReductionOps);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007342}
7343
Alexey Bataev182227b2015-08-20 10:54:39 +00007344OMPClause *Sema::ActOnOpenMPLinearClause(
7345 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
7346 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
7347 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00007348 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007349 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00007350 SmallVector<Expr *, 8> Inits;
Alexey Bataev182227b2015-08-20 10:54:39 +00007351 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
7352 LinKind == OMPC_LINEAR_unknown) {
7353 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
7354 LinKind = OMPC_LINEAR_val;
7355 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007356 for (auto &RefExpr : VarList) {
7357 assert(RefExpr && "NULL expr in OpenMP linear clause.");
7358 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00007359 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007360 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007361 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00007362 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00007363 continue;
7364 }
7365
7366 // OpenMP [2.14.3.7, linear clause]
7367 // A list item that appears in a linear clause is subject to the private
7368 // clause semantics described in Section 2.14.3.3 on page 159 except as
7369 // noted. In addition, the value of the new list item on each iteration
7370 // of the associated loop(s) corresponds to the value of the original
7371 // list item before entering the construct plus the logical number of
7372 // the iteration times linear-step.
7373
Alexey Bataeved09d242014-05-28 05:53:51 +00007374 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00007375 // OpenMP [2.1, C/C++]
7376 // A list item is a variable name.
7377 // OpenMP [2.14.3.3, Restrictions, p.1]
7378 // A variable that is part of another variable (as an array or
7379 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00007380 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00007381 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00007382 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00007383 continue;
7384 }
7385
7386 VarDecl *VD = cast<VarDecl>(DE->getDecl());
7387
7388 // OpenMP [2.14.3.7, linear clause]
7389 // A list-item cannot appear in more than one linear clause.
7390 // A list-item that appears in a linear clause cannot appear in any
7391 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007392 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00007393 if (DVar.RefExpr) {
7394 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7395 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007396 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00007397 continue;
7398 }
7399
7400 QualType QType = VD->getType();
7401 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
7402 // It will be analyzed later.
7403 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007404 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00007405 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00007406 continue;
7407 }
7408
7409 // A variable must not have an incomplete type or a reference type.
7410 if (RequireCompleteType(ELoc, QType,
7411 diag::err_omp_linear_incomplete_type)) {
7412 continue;
7413 }
Alexey Bataev1185e192015-08-20 12:15:57 +00007414 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
7415 !QType->isReferenceType()) {
7416 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
7417 << QType << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
7418 continue;
7419 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007420 QType = QType.getNonReferenceType();
Alexander Musman8dba6642014-04-22 13:09:42 +00007421
7422 // A list item must not be const-qualified.
7423 if (QType.isConstant(Context)) {
7424 Diag(ELoc, diag::err_omp_const_variable)
7425 << getOpenMPClauseName(OMPC_linear);
7426 bool IsDecl =
7427 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7428 Diag(VD->getLocation(),
7429 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7430 << VD;
7431 continue;
7432 }
7433
7434 // A list item must be of integral or pointer type.
7435 QType = QType.getUnqualifiedType().getCanonicalType();
7436 const Type *Ty = QType.getTypePtrOrNull();
7437 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
7438 !Ty->isPointerType())) {
7439 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
7440 bool IsDecl =
7441 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7442 Diag(VD->getLocation(),
7443 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7444 << VD;
7445 continue;
7446 }
7447
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007448 // Build private copy of original var.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007449 auto *Private = buildVarDecl(*this, ELoc, QType, VD->getName(),
7450 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007451 auto *PrivateRef = buildDeclRefExpr(
7452 *this, Private, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman3276a272015-03-21 10:12:56 +00007453 // Build var to save initial value.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007454 VarDecl *Init = buildVarDecl(*this, ELoc, QType, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00007455 Expr *InitExpr;
7456 if (LinKind == OMPC_LINEAR_uval)
7457 InitExpr = VD->getInit();
7458 else
7459 InitExpr = DE;
7460 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexander Musman3276a272015-03-21 10:12:56 +00007461 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007462 auto InitRef = buildDeclRefExpr(
7463 *this, Init, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman8dba6642014-04-22 13:09:42 +00007464 DSAStack->addDSA(VD, DE, OMPC_linear);
7465 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007466 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00007467 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00007468 }
7469
7470 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007471 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00007472
7473 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00007474 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00007475 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
7476 !Step->isInstantiationDependent() &&
7477 !Step->containsUnexpandedParameterPack()) {
7478 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00007479 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00007480 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007481 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00007482 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00007483
Alexander Musman3276a272015-03-21 10:12:56 +00007484 // Build var to save the step value.
7485 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007486 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00007487 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007488 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00007489 ExprResult CalcStep =
7490 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007491 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00007492
Alexander Musman8dba6642014-04-22 13:09:42 +00007493 // Warn about zero linear step (it would be probably better specified as
7494 // making corresponding variables 'const').
7495 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00007496 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
7497 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00007498 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
7499 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00007500 if (!IsConstant && CalcStep.isUsable()) {
7501 // Calculate the step beforehand instead of doing this on each iteration.
7502 // (This is not used if the number of iterations may be kfold-ed).
7503 CalcStepExpr = CalcStep.get();
7504 }
Alexander Musman8dba6642014-04-22 13:09:42 +00007505 }
7506
Alexey Bataev182227b2015-08-20 10:54:39 +00007507 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
7508 ColonLoc, EndLoc, Vars, Privates, Inits,
7509 StepExpr, CalcStepExpr);
Alexander Musman3276a272015-03-21 10:12:56 +00007510}
7511
7512static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
7513 Expr *NumIterations, Sema &SemaRef,
7514 Scope *S) {
7515 // Walk the vars and build update/final expressions for the CodeGen.
7516 SmallVector<Expr *, 8> Updates;
7517 SmallVector<Expr *, 8> Finals;
7518 Expr *Step = Clause.getStep();
7519 Expr *CalcStep = Clause.getCalcStep();
7520 // OpenMP [2.14.3.7, linear clause]
7521 // If linear-step is not specified it is assumed to be 1.
7522 if (Step == nullptr)
7523 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
7524 else if (CalcStep)
7525 Step = cast<BinaryOperator>(CalcStep)->getLHS();
7526 bool HasErrors = false;
7527 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007528 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00007529 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00007530 for (auto &RefExpr : Clause.varlists()) {
7531 Expr *InitExpr = *CurInit;
7532
7533 // Build privatized reference to the current linear var.
7534 auto DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00007535 Expr *CapturedRef;
7536 if (LinKind == OMPC_LINEAR_uval)
7537 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
7538 else
7539 CapturedRef =
7540 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
7541 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
7542 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00007543
7544 // Build update: Var = InitExpr + IV * Step
7545 ExprResult Update =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007546 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
Alexander Musman3276a272015-03-21 10:12:56 +00007547 InitExpr, IV, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007548 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
7549 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00007550
7551 // Build final: Var = InitExpr + NumIterations * Step
7552 ExprResult Final =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007553 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
Alexey Bataev39f915b82015-05-08 10:41:21 +00007554 InitExpr, NumIterations, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007555 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
7556 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00007557 if (!Update.isUsable() || !Final.isUsable()) {
7558 Updates.push_back(nullptr);
7559 Finals.push_back(nullptr);
7560 HasErrors = true;
7561 } else {
7562 Updates.push_back(Update.get());
7563 Finals.push_back(Final.get());
7564 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007565 ++CurInit, ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00007566 }
7567 Clause.setUpdates(Updates);
7568 Clause.setFinals(Finals);
7569 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00007570}
7571
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007572OMPClause *Sema::ActOnOpenMPAlignedClause(
7573 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
7574 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
7575
7576 SmallVector<Expr *, 8> Vars;
7577 for (auto &RefExpr : VarList) {
7578 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
7579 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7580 // It will be analyzed later.
7581 Vars.push_back(RefExpr);
7582 continue;
7583 }
7584
7585 SourceLocation ELoc = RefExpr->getExprLoc();
7586 // OpenMP [2.1, C/C++]
7587 // A list item is a variable name.
7588 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
7589 if (!DE || !isa<VarDecl>(DE->getDecl())) {
7590 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
7591 continue;
7592 }
7593
7594 VarDecl *VD = cast<VarDecl>(DE->getDecl());
7595
7596 // OpenMP [2.8.1, simd construct, Restrictions]
7597 // The type of list items appearing in the aligned clause must be
7598 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007599 QualType QType = VD->getType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007600 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007601 const Type *Ty = QType.getTypePtrOrNull();
7602 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
7603 !Ty->isPointerType())) {
7604 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
7605 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
7606 bool IsDecl =
7607 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7608 Diag(VD->getLocation(),
7609 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7610 << VD;
7611 continue;
7612 }
7613
7614 // OpenMP [2.8.1, simd construct, Restrictions]
7615 // A list-item cannot appear in more than one aligned clause.
7616 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
7617 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
7618 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
7619 << getOpenMPClauseName(OMPC_aligned);
7620 continue;
7621 }
7622
7623 Vars.push_back(DE);
7624 }
7625
7626 // OpenMP [2.8.1, simd construct, Description]
7627 // The parameter of the aligned clause, alignment, must be a constant
7628 // positive integer expression.
7629 // If no optional parameter is specified, implementation-defined default
7630 // alignments for SIMD instructions on the target platforms are assumed.
7631 if (Alignment != nullptr) {
7632 ExprResult AlignResult =
7633 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
7634 if (AlignResult.isInvalid())
7635 return nullptr;
7636 Alignment = AlignResult.get();
7637 }
7638 if (Vars.empty())
7639 return nullptr;
7640
7641 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
7642 EndLoc, Vars, Alignment);
7643}
7644
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007645OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
7646 SourceLocation StartLoc,
7647 SourceLocation LParenLoc,
7648 SourceLocation EndLoc) {
7649 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007650 SmallVector<Expr *, 8> SrcExprs;
7651 SmallVector<Expr *, 8> DstExprs;
7652 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00007653 for (auto &RefExpr : VarList) {
7654 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
7655 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007656 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007657 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007658 SrcExprs.push_back(nullptr);
7659 DstExprs.push_back(nullptr);
7660 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007661 continue;
7662 }
7663
Alexey Bataeved09d242014-05-28 05:53:51 +00007664 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007665 // OpenMP [2.1, C/C++]
7666 // A list item is a variable name.
7667 // OpenMP [2.14.4.1, Restrictions, p.1]
7668 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00007669 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007670 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00007671 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007672 continue;
7673 }
7674
7675 Decl *D = DE->getDecl();
7676 VarDecl *VD = cast<VarDecl>(D);
7677
7678 QualType Type = VD->getType();
7679 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
7680 // It will be analyzed later.
7681 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007682 SrcExprs.push_back(nullptr);
7683 DstExprs.push_back(nullptr);
7684 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007685 continue;
7686 }
7687
7688 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
7689 // A list item that appears in a copyin clause must be threadprivate.
7690 if (!DSAStack->isThreadPrivate(VD)) {
7691 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00007692 << getOpenMPClauseName(OMPC_copyin)
7693 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007694 continue;
7695 }
7696
7697 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
7698 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00007699 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007700 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007701 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007702 auto *SrcVD =
7703 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
7704 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007705 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007706 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
7707 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007708 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
7709 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007710 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007711 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007712 // For arrays generate assignment operation for single element and replace
7713 // it by the original array element in CodeGen.
7714 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
7715 PseudoDstExpr, PseudoSrcExpr);
7716 if (AssignmentOp.isInvalid())
7717 continue;
7718 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
7719 /*DiscardedValue=*/true);
7720 if (AssignmentOp.isInvalid())
7721 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007722
7723 DSAStack->addDSA(VD, DE, OMPC_copyin);
7724 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007725 SrcExprs.push_back(PseudoSrcExpr);
7726 DstExprs.push_back(PseudoDstExpr);
7727 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007728 }
7729
Alexey Bataeved09d242014-05-28 05:53:51 +00007730 if (Vars.empty())
7731 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007732
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007733 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
7734 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007735}
7736
Alexey Bataevbae9a792014-06-27 10:37:06 +00007737OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
7738 SourceLocation StartLoc,
7739 SourceLocation LParenLoc,
7740 SourceLocation EndLoc) {
7741 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00007742 SmallVector<Expr *, 8> SrcExprs;
7743 SmallVector<Expr *, 8> DstExprs;
7744 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00007745 for (auto &RefExpr : VarList) {
7746 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
7747 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7748 // It will be analyzed later.
7749 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00007750 SrcExprs.push_back(nullptr);
7751 DstExprs.push_back(nullptr);
7752 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00007753 continue;
7754 }
7755
7756 SourceLocation ELoc = RefExpr->getExprLoc();
7757 // OpenMP [2.1, C/C++]
7758 // A list item is a variable name.
7759 // OpenMP [2.14.4.1, Restrictions, p.1]
7760 // A list item that appears in a copyin clause must be threadprivate.
7761 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
7762 if (!DE || !isa<VarDecl>(DE->getDecl())) {
7763 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
7764 continue;
7765 }
7766
7767 Decl *D = DE->getDecl();
7768 VarDecl *VD = cast<VarDecl>(D);
7769
7770 QualType Type = VD->getType();
7771 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
7772 // It will be analyzed later.
7773 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00007774 SrcExprs.push_back(nullptr);
7775 DstExprs.push_back(nullptr);
7776 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00007777 continue;
7778 }
7779
7780 // OpenMP [2.14.4.2, Restrictions, p.2]
7781 // A list item that appears in a copyprivate clause may not appear in a
7782 // private or firstprivate clause on the single construct.
7783 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007784 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00007785 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
7786 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00007787 Diag(ELoc, diag::err_omp_wrong_dsa)
7788 << getOpenMPClauseName(DVar.CKind)
7789 << getOpenMPClauseName(OMPC_copyprivate);
7790 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7791 continue;
7792 }
7793
7794 // OpenMP [2.11.4.2, Restrictions, p.1]
7795 // All list items that appear in a copyprivate clause must be either
7796 // threadprivate or private in the enclosing context.
7797 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007798 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00007799 if (DVar.CKind == OMPC_shared) {
7800 Diag(ELoc, diag::err_omp_required_access)
7801 << getOpenMPClauseName(OMPC_copyprivate)
7802 << "threadprivate or private in the enclosing context";
7803 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7804 continue;
7805 }
7806 }
7807 }
7808
Alexey Bataev7a3e5852015-05-19 08:19:24 +00007809 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007810 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00007811 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007812 << getOpenMPClauseName(OMPC_copyprivate) << Type
7813 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00007814 bool IsDecl =
7815 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7816 Diag(VD->getLocation(),
7817 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7818 << VD;
7819 continue;
7820 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007821
Alexey Bataevbae9a792014-06-27 10:37:06 +00007822 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
7823 // A variable of class type (or array thereof) that appears in a
7824 // copyin clause requires an accessible, unambiguous copy assignment
7825 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007826 Type = Context.getBaseElementType(Type.getNonReferenceType())
7827 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +00007828 auto *SrcVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007829 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.src",
7830 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00007831 auto *PseudoSrcExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007832 buildDeclRefExpr(*this, SrcVD, Type, DE->getExprLoc());
Alexey Bataev420d45b2015-04-14 05:11:24 +00007833 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007834 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.dst",
7835 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00007836 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007837 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataeva63048e2015-03-23 06:18:07 +00007838 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
7839 PseudoDstExpr, PseudoSrcExpr);
7840 if (AssignmentOp.isInvalid())
7841 continue;
7842 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
7843 /*DiscardedValue=*/true);
7844 if (AssignmentOp.isInvalid())
7845 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00007846
7847 // No need to mark vars as copyprivate, they are already threadprivate or
7848 // implicitly private.
7849 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00007850 SrcExprs.push_back(PseudoSrcExpr);
7851 DstExprs.push_back(PseudoDstExpr);
7852 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +00007853 }
7854
7855 if (Vars.empty())
7856 return nullptr;
7857
Alexey Bataeva63048e2015-03-23 06:18:07 +00007858 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
7859 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00007860}
7861
Alexey Bataev6125da92014-07-21 11:26:11 +00007862OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
7863 SourceLocation StartLoc,
7864 SourceLocation LParenLoc,
7865 SourceLocation EndLoc) {
7866 if (VarList.empty())
7867 return nullptr;
7868
7869 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
7870}
Alexey Bataevdea47612014-07-23 07:46:59 +00007871
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007872OMPClause *
7873Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
7874 SourceLocation DepLoc, SourceLocation ColonLoc,
7875 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
7876 SourceLocation LParenLoc, SourceLocation EndLoc) {
7877 if (DepKind == OMPC_DEPEND_unknown) {
7878 std::string Values;
7879 std::string Sep(", ");
7880 for (unsigned i = 0; i < OMPC_DEPEND_unknown; ++i) {
7881 Values += "'";
7882 Values += getOpenMPSimpleClauseTypeName(OMPC_depend, i);
7883 Values += "'";
7884 switch (i) {
7885 case OMPC_DEPEND_unknown - 2:
7886 Values += " or ";
7887 break;
7888 case OMPC_DEPEND_unknown - 1:
7889 break;
7890 default:
7891 Values += Sep;
7892 break;
7893 }
7894 }
7895 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
7896 << Values << getOpenMPClauseName(OMPC_depend);
7897 return nullptr;
7898 }
7899 SmallVector<Expr *, 8> Vars;
7900 for (auto &RefExpr : VarList) {
7901 assert(RefExpr && "NULL expr in OpenMP shared clause.");
7902 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7903 // It will be analyzed later.
7904 Vars.push_back(RefExpr);
7905 continue;
7906 }
7907
7908 SourceLocation ELoc = RefExpr->getExprLoc();
7909 // OpenMP [2.11.1.1, Restrictions, p.3]
7910 // A variable that is part of another variable (such as a field of a
7911 // structure) but is not an array element or an array section cannot appear
7912 // in a depend clause.
7913 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
Alexey Bataev1a3320e2015-08-25 14:24:04 +00007914 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
7915 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
7916 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
7917 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
7918 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007919 (ASE && !ASE->getBase()->getType()->isAnyPointerType() &&
7920 !ASE->getBase()->getType()->isArrayType())) {
7921 Diag(ELoc, diag::err_omp_expected_var_name_or_array_item)
7922 << RefExpr->getSourceRange();
7923 continue;
7924 }
7925
7926 Vars.push_back(RefExpr->IgnoreParenImpCasts());
7927 }
7928
7929 if (Vars.empty())
7930 return nullptr;
7931
7932 return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
7933 DepLoc, ColonLoc, Vars);
7934}
Michael Wonge710d542015-08-07 16:16:36 +00007935
7936OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
7937 SourceLocation LParenLoc,
7938 SourceLocation EndLoc) {
7939 Expr *ValExpr = Device;
Michael Wonge710d542015-08-07 16:16:36 +00007940
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007941 // OpenMP [2.9.1, Restrictions]
7942 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00007943 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
7944 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00007945 return nullptr;
7946
Michael Wonge710d542015-08-07 16:16:36 +00007947 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
7948}
Kelvin Li0bff7af2015-11-23 05:32:03 +00007949
7950static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
7951 DSAStackTy *Stack, CXXRecordDecl *RD) {
7952 if (!RD || RD->isInvalidDecl())
7953 return true;
7954
7955 auto QTy = SemaRef.Context.getRecordType(RD);
7956 if (RD->isDynamicClass()) {
7957 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
7958 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
7959 return false;
7960 }
7961 auto *DC = RD;
7962 bool IsCorrect = true;
7963 for (auto *I : DC->decls()) {
7964 if (I) {
7965 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
7966 if (MD->isStatic()) {
7967 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
7968 SemaRef.Diag(MD->getLocation(),
7969 diag::note_omp_static_member_in_target);
7970 IsCorrect = false;
7971 }
7972 } else if (auto *VD = dyn_cast<VarDecl>(I)) {
7973 if (VD->isStaticDataMember()) {
7974 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
7975 SemaRef.Diag(VD->getLocation(),
7976 diag::note_omp_static_member_in_target);
7977 IsCorrect = false;
7978 }
7979 }
7980 }
7981 }
7982
7983 for (auto &I : RD->bases()) {
7984 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
7985 I.getType()->getAsCXXRecordDecl()))
7986 IsCorrect = false;
7987 }
7988 return IsCorrect;
7989}
7990
7991static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
7992 DSAStackTy *Stack, QualType QTy) {
7993 NamedDecl *ND;
7994 if (QTy->isIncompleteType(&ND)) {
7995 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
7996 return false;
7997 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
7998 if (!RD->isInvalidDecl() &&
7999 !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
8000 return false;
8001 }
8002 return true;
8003}
8004
8005OMPClause *Sema::ActOnOpenMPMapClause(
8006 OpenMPMapClauseKind MapTypeModifier, OpenMPMapClauseKind MapType,
8007 SourceLocation MapLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VarList,
8008 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
8009 SmallVector<Expr *, 4> Vars;
8010
8011 for (auto &RE : VarList) {
8012 assert(RE && "Null expr in omp map");
8013 if (isa<DependentScopeDeclRefExpr>(RE)) {
8014 // It will be analyzed later.
8015 Vars.push_back(RE);
8016 continue;
8017 }
8018 SourceLocation ELoc = RE->getExprLoc();
8019
8020 // OpenMP [2.14.5, Restrictions]
8021 // A variable that is part of another variable (such as field of a
8022 // structure) but is not an array element or an array section cannot appear
8023 // in a map clause.
8024 auto *VE = RE->IgnoreParenLValueCasts();
8025
8026 if (VE->isValueDependent() || VE->isTypeDependent() ||
8027 VE->isInstantiationDependent() ||
8028 VE->containsUnexpandedParameterPack()) {
8029 // It will be analyzed later.
8030 Vars.push_back(RE);
8031 continue;
8032 }
8033
8034 auto *SimpleExpr = RE->IgnoreParenCasts();
8035 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
8036 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
8037 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
8038
8039 if (!RE->IgnoreParenImpCasts()->isLValue() ||
8040 (!OASE && !ASE && !DE) ||
8041 (DE && !isa<VarDecl>(DE->getDecl())) ||
8042 (ASE && !ASE->getBase()->getType()->isAnyPointerType() &&
8043 !ASE->getBase()->getType()->isArrayType())) {
8044 Diag(ELoc, diag::err_omp_expected_var_name_or_array_item)
8045 << RE->getSourceRange();
8046 continue;
8047 }
8048
8049 Decl *D = nullptr;
8050 if (DE) {
8051 D = DE->getDecl();
8052 } else if (ASE) {
8053 auto *B = ASE->getBase()->IgnoreParenCasts();
8054 D = dyn_cast<DeclRefExpr>(B)->getDecl();
8055 } else if (OASE) {
8056 auto *B = OASE->getBase();
8057 D = dyn_cast<DeclRefExpr>(B)->getDecl();
8058 }
8059 assert(D && "Null decl on map clause.");
8060 auto *VD = cast<VarDecl>(D);
8061
8062 // OpenMP [2.14.5, Restrictions, p.8]
8063 // threadprivate variables cannot appear in a map clause.
8064 if (DSAStack->isThreadPrivate(VD)) {
8065 auto DVar = DSAStack->getTopDSA(VD, false);
8066 Diag(ELoc, diag::err_omp_threadprivate_in_map);
8067 ReportOriginalDSA(*this, DSAStack, VD, DVar);
8068 continue;
8069 }
8070
8071 // OpenMP [2.14.5, Restrictions, p.2]
8072 // At most one list item can be an array item derived from a given variable
8073 // in map clauses of the same construct.
8074 // OpenMP [2.14.5, Restrictions, p.3]
8075 // List items of map clauses in the same construct must not share original
8076 // storage.
8077 // OpenMP [2.14.5, Restrictions, C/C++, p.2]
8078 // A variable for which the type is pointer, reference to array, or
8079 // reference to pointer and an array section derived from that variable
8080 // must not appear as list items of map clauses of the same construct.
8081 DSAStackTy::MapInfo MI = DSAStack->IsMappedInCurrentRegion(VD);
8082 if (MI.RefExpr) {
8083 Diag(ELoc, diag::err_omp_map_shared_storage) << ELoc;
8084 Diag(MI.RefExpr->getExprLoc(), diag::note_used_here)
8085 << MI.RefExpr->getSourceRange();
8086 continue;
8087 }
8088
8089 // OpenMP [2.14.5, Restrictions, C/C++, p.3,4]
8090 // A variable for which the type is pointer, reference to array, or
8091 // reference to pointer must not appear as a list item if the enclosing
8092 // device data environment already contains an array section derived from
8093 // that variable.
8094 // An array section derived from a variable for which the type is pointer,
8095 // reference to array, or reference to pointer must not appear as a list
8096 // item if the enclosing device data environment already contains that
8097 // variable.
8098 QualType Type = VD->getType();
8099 MI = DSAStack->getMapInfoForVar(VD);
8100 if (MI.RefExpr && (isa<DeclRefExpr>(MI.RefExpr->IgnoreParenLValueCasts()) !=
8101 isa<DeclRefExpr>(VE)) &&
8102 (Type->isPointerType() || Type->isReferenceType())) {
8103 Diag(ELoc, diag::err_omp_map_shared_storage) << ELoc;
8104 Diag(MI.RefExpr->getExprLoc(), diag::note_used_here)
8105 << MI.RefExpr->getSourceRange();
8106 continue;
8107 }
8108
8109 // OpenMP [2.14.5, Restrictions, C/C++, p.7]
8110 // A list item must have a mappable type.
8111 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), *this,
8112 DSAStack, Type))
8113 continue;
8114
8115 Vars.push_back(RE);
8116 MI.RefExpr = RE;
8117 DSAStack->addMapInfoForVar(VD, MI);
8118 }
8119 if (Vars.empty())
8120 return nullptr;
8121
8122 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
8123 MapTypeModifier, MapType, MapLoc);
8124}
Kelvin Li099bb8c2015-11-24 20:50:12 +00008125
8126OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
8127 SourceLocation StartLoc,
8128 SourceLocation LParenLoc,
8129 SourceLocation EndLoc) {
8130 Expr *ValExpr = NumTeams;
Kelvin Li099bb8c2015-11-24 20:50:12 +00008131
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008132 // OpenMP [teams Constrcut, Restrictions]
8133 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00008134 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
8135 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008136 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +00008137
8138 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8139}
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008140
8141OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
8142 SourceLocation StartLoc,
8143 SourceLocation LParenLoc,
8144 SourceLocation EndLoc) {
8145 Expr *ValExpr = ThreadLimit;
8146
8147 // OpenMP [teams Constrcut, Restrictions]
8148 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00008149 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
8150 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008151 return nullptr;
8152
8153 return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc,
8154 EndLoc);
8155}
Alexey Bataeva0569352015-12-01 10:17:31 +00008156
8157OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
8158 SourceLocation StartLoc,
8159 SourceLocation LParenLoc,
8160 SourceLocation EndLoc) {
8161 Expr *ValExpr = Priority;
8162
8163 // OpenMP [2.9.1, task Constrcut]
8164 // The priority-value is a non-negative numerical scalar expression.
8165 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
8166 /*StrictlyPositive=*/false))
8167 return nullptr;
8168
8169 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8170}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008171
8172OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
8173 SourceLocation StartLoc,
8174 SourceLocation LParenLoc,
8175 SourceLocation EndLoc) {
8176 Expr *ValExpr = Grainsize;
8177
8178 // OpenMP [2.9.2, taskloop Constrcut]
8179 // The parameter of the grainsize clause must be a positive integer
8180 // expression.
8181 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
8182 /*StrictlyPositive=*/true))
8183 return nullptr;
8184
8185 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8186}