blob: 152c1183daebad1789396590fd1d2b71d1afb708 [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 Bataev28c75412015-12-15 08:19:24 +000094 typedef llvm::StringMap<std::pair<OMPCriticalDirective *, llvm::APSInt>>
95 CriticalsWithHintsTy;
Alexey Bataev758e55e2013-09-06 18:03:48 +000096
97 struct SharingMapTy {
98 DeclSAMapTy SharingMap;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000099 AlignedMapTy AlignedMap;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000100 MappedDeclsTy MappedDecls;
Alexey Bataev9c821032015-04-30 04:23:23 +0000101 LoopControlVariablesSetTy LCVSet;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000102 DefaultDataSharingAttributes DefaultAttr;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000103 SourceLocation DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000104 OpenMPDirectiveKind Directive;
105 DeclarationNameInfo DirectiveName;
106 Scope *CurScope;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000107 SourceLocation ConstructLoc;
Alexey Bataev346265e2015-09-25 10:37:12 +0000108 /// \brief first argument (Expr *) contains optional argument of the
109 /// 'ordered' clause, the second one is true if the regions has 'ordered'
110 /// clause, false otherwise.
111 llvm::PointerIntPair<Expr *, 1, bool> OrderedRegion;
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000112 bool NowaitRegion;
Alexey Bataev25e5b442015-09-15 12:52:43 +0000113 bool CancelRegion;
Alexey Bataev9c821032015-04-30 04:23:23 +0000114 unsigned CollapseNumber;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000115 SourceLocation InnerTeamsRegionLoc;
Alexey Bataeved09d242014-05-28 05:53:51 +0000116 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000117 Scope *CurScope, SourceLocation Loc)
Alexey Bataev9c821032015-04-30 04:23:23 +0000118 : SharingMap(), AlignedMap(), LCVSet(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000119 Directive(DKind), DirectiveName(std::move(Name)), CurScope(CurScope),
Alexey Bataev346265e2015-09-25 10:37:12 +0000120 ConstructLoc(Loc), OrderedRegion(), NowaitRegion(false),
Alexey Bataev25e5b442015-09-15 12:52:43 +0000121 CancelRegion(false), CollapseNumber(1), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000122 SharingMapTy()
Alexey Bataev9c821032015-04-30 04:23:23 +0000123 : SharingMap(), AlignedMap(), LCVSet(), DefaultAttr(DSA_unspecified),
Alexey Bataevbae9a792014-06-27 10:37:06 +0000124 Directive(OMPD_unknown), DirectiveName(), CurScope(nullptr),
Alexey Bataev346265e2015-09-25 10:37:12 +0000125 ConstructLoc(), OrderedRegion(), NowaitRegion(false),
Alexey Bataev25e5b442015-09-15 12:52:43 +0000126 CancelRegion(false), CollapseNumber(1), InnerTeamsRegionLoc() {}
Alexey Bataev758e55e2013-09-06 18:03:48 +0000127 };
128
129 typedef SmallVector<SharingMapTy, 64> StackTy;
130
131 /// \brief Stack of used declaration and their data-sharing attributes.
132 StackTy Stack;
Alexey Bataev39f915b82015-05-08 10:41:21 +0000133 /// \brief true, if check for DSA must be from parent directive, false, if
134 /// from current directive.
Alexey Bataevaac108a2015-06-23 04:51:00 +0000135 OpenMPClauseKind ClauseKindMode;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000136 Sema &SemaRef;
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000137 bool ForceCapturing;
Alexey Bataev28c75412015-12-15 08:19:24 +0000138 CriticalsWithHintsTy Criticals;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000139
140 typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
141
142 DSAVarData getDSA(StackTy::reverse_iterator Iter, VarDecl *D);
Alexey Bataevec3da872014-01-31 05:15:34 +0000143
144 /// \brief Checks if the variable is a local for OpenMP region.
145 bool isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter);
Alexey Bataeved09d242014-05-28 05:53:51 +0000146
Alexey Bataev758e55e2013-09-06 18:03:48 +0000147public:
Alexey Bataevaac108a2015-06-23 04:51:00 +0000148 explicit DSAStackTy(Sema &S)
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000149 : Stack(1), ClauseKindMode(OMPC_unknown), SemaRef(S),
150 ForceCapturing(false) {}
Alexey Bataev39f915b82015-05-08 10:41:21 +0000151
Alexey Bataevaac108a2015-06-23 04:51:00 +0000152 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
153 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000154
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000155 bool isForceVarCapturing() const { return ForceCapturing; }
156 void setForceVarCapturing(bool V) { ForceCapturing = V; }
157
Alexey Bataev758e55e2013-09-06 18:03:48 +0000158 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000159 Scope *CurScope, SourceLocation Loc) {
160 Stack.push_back(SharingMapTy(DKind, DirName, CurScope, Loc));
161 Stack.back().DefaultAttrLoc = Loc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000162 }
163
164 void pop() {
165 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
166 Stack.pop_back();
167 }
168
Alexey Bataev28c75412015-12-15 08:19:24 +0000169 void addCriticalWithHint(OMPCriticalDirective *D, llvm::APSInt Hint) {
170 Criticals[D->getDirectiveName().getAsString()] = std::make_pair(D, Hint);
171 }
172 const std::pair<OMPCriticalDirective *, llvm::APSInt>
173 getCriticalWithHint(const DeclarationNameInfo &Name) const {
174 auto I = Criticals.find(Name.getAsString());
175 if (I != Criticals.end())
176 return I->second;
177 return std::make_pair(nullptr, llvm::APSInt());
178 }
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000179 /// \brief If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000180 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000181 /// for diagnostics.
182 DeclRefExpr *addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE);
183
Alexey Bataev9c821032015-04-30 04:23:23 +0000184 /// \brief Register specified variable as loop control variable.
185 void addLoopControlVariable(VarDecl *D);
186 /// \brief Check if the specified variable is a loop control variable for
187 /// current region.
188 bool isLoopControlVariable(VarDecl *D);
189
Alexey Bataev758e55e2013-09-06 18:03:48 +0000190 /// \brief Adds explicit data sharing attribute to the specified declaration.
191 void addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A);
192
Alexey Bataev758e55e2013-09-06 18:03:48 +0000193 /// \brief Returns data sharing attributes from top of the stack for the
194 /// specified declaration.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000195 DSAVarData getTopDSA(VarDecl *D, bool FromParent);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000196 /// \brief Returns data-sharing attributes for the specified declaration.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000197 DSAVarData getImplicitDSA(VarDecl *D, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000198 /// \brief Checks if the specified variables has data-sharing attributes which
199 /// match specified \a CPred predicate in any directive which matches \a DPred
200 /// predicate.
201 template <class ClausesPredicate, class DirectivesPredicate>
202 DSAVarData hasDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000203 DirectivesPredicate DPred, bool FromParent);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000204 /// \brief Checks if the specified variables has data-sharing attributes which
205 /// match specified \a CPred predicate in any innermost directive which
206 /// matches \a DPred predicate.
207 template <class ClausesPredicate, class DirectivesPredicate>
208 DSAVarData hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000209 DirectivesPredicate DPred,
210 bool FromParent);
Alexey Bataevaac108a2015-06-23 04:51:00 +0000211 /// \brief Checks if the specified variables has explicit data-sharing
212 /// attributes which match specified \a CPred predicate at the specified
213 /// OpenMP region.
214 bool hasExplicitDSA(VarDecl *D,
215 const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
216 unsigned Level);
Samuel Antao4be30e92015-10-02 17:14:03 +0000217
218 /// \brief Returns true if the directive at level \Level matches in the
219 /// specified \a DPred predicate.
220 bool hasExplicitDirective(
221 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
222 unsigned Level);
223
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000224 /// \brief Finds a directive which matches specified \a DPred predicate.
225 template <class NamedDirectivesPredicate>
226 bool hasDirective(NamedDirectivesPredicate DPred, bool FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000227
Alexey Bataev758e55e2013-09-06 18:03:48 +0000228 /// \brief Returns currently analyzed directive.
229 OpenMPDirectiveKind getCurrentDirective() const {
230 return Stack.back().Directive;
231 }
Alexey Bataev549210e2014-06-24 04:39:47 +0000232 /// \brief Returns parent directive.
233 OpenMPDirectiveKind getParentDirective() const {
234 if (Stack.size() > 2)
235 return Stack[Stack.size() - 2].Directive;
236 return OMPD_unknown;
237 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000238 /// \brief Return the directive associated with the provided scope.
239 OpenMPDirectiveKind getDirectiveForScope(const Scope *S) const;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000240
241 /// \brief Set default data sharing attribute to none.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000242 void setDefaultDSANone(SourceLocation Loc) {
243 Stack.back().DefaultAttr = DSA_none;
244 Stack.back().DefaultAttrLoc = Loc;
245 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000246 /// \brief Set default data sharing attribute to shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000247 void setDefaultDSAShared(SourceLocation Loc) {
248 Stack.back().DefaultAttr = DSA_shared;
249 Stack.back().DefaultAttrLoc = Loc;
250 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000251
252 DefaultDataSharingAttributes getDefaultDSA() const {
253 return Stack.back().DefaultAttr;
254 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000255 SourceLocation getDefaultDSALocation() const {
256 return Stack.back().DefaultAttrLoc;
257 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000258
Alexey Bataevf29276e2014-06-18 04:14:57 +0000259 /// \brief Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000260 bool isThreadPrivate(VarDecl *D) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000261 DSAVarData DVar = getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000262 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000263 }
264
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000265 /// \brief Marks current region as ordered (it has an 'ordered' clause).
Alexey Bataev346265e2015-09-25 10:37:12 +0000266 void setOrderedRegion(bool IsOrdered, Expr *Param) {
267 Stack.back().OrderedRegion.setInt(IsOrdered);
268 Stack.back().OrderedRegion.setPointer(Param);
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000269 }
270 /// \brief Returns true, if parent region is ordered (has associated
271 /// 'ordered' clause), false - otherwise.
272 bool isParentOrderedRegion() const {
273 if (Stack.size() > 2)
Alexey Bataev346265e2015-09-25 10:37:12 +0000274 return Stack[Stack.size() - 2].OrderedRegion.getInt();
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000275 return false;
276 }
Alexey Bataev346265e2015-09-25 10:37:12 +0000277 /// \brief Returns optional parameter for the ordered region.
278 Expr *getParentOrderedRegionParam() const {
279 if (Stack.size() > 2)
280 return Stack[Stack.size() - 2].OrderedRegion.getPointer();
281 return nullptr;
282 }
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000283 /// \brief Marks current region as nowait (it has a 'nowait' clause).
284 void setNowaitRegion(bool IsNowait = true) {
285 Stack.back().NowaitRegion = IsNowait;
286 }
287 /// \brief Returns true, if parent region is nowait (has associated
288 /// 'nowait' clause), false - otherwise.
289 bool isParentNowaitRegion() const {
290 if (Stack.size() > 2)
291 return Stack[Stack.size() - 2].NowaitRegion;
292 return false;
293 }
Alexey Bataev25e5b442015-09-15 12:52:43 +0000294 /// \brief Marks parent region as cancel region.
295 void setParentCancelRegion(bool Cancel = true) {
296 if (Stack.size() > 2)
297 Stack[Stack.size() - 2].CancelRegion =
298 Stack[Stack.size() - 2].CancelRegion || Cancel;
299 }
300 /// \brief Return true if current region has inner cancel construct.
301 bool isCancelRegion() const {
302 return Stack.back().CancelRegion;
303 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000304
Alexey Bataev9c821032015-04-30 04:23:23 +0000305 /// \brief Set collapse value for the region.
306 void setCollapseNumber(unsigned Val) { Stack.back().CollapseNumber = Val; }
307 /// \brief Return collapse value for region.
308 unsigned getCollapseNumber() const {
309 return Stack.back().CollapseNumber;
310 }
311
Alexey Bataev13314bf2014-10-09 04:18:56 +0000312 /// \brief Marks current target region as one with closely nested teams
313 /// region.
314 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
315 if (Stack.size() > 2)
316 Stack[Stack.size() - 2].InnerTeamsRegionLoc = TeamsRegionLoc;
317 }
318 /// \brief Returns true, if current region has closely nested teams region.
319 bool hasInnerTeamsRegion() const {
320 return getInnerTeamsRegionLoc().isValid();
321 }
322 /// \brief Returns location of the nested teams region (if any).
323 SourceLocation getInnerTeamsRegionLoc() const {
324 if (Stack.size() > 1)
325 return Stack.back().InnerTeamsRegionLoc;
326 return SourceLocation();
327 }
328
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000329 Scope *getCurScope() const { return Stack.back().CurScope; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000330 Scope *getCurScope() { return Stack.back().CurScope; }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000331 SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000332
333 MapInfo getMapInfoForVar(VarDecl *VD) {
334 MapInfo VarMI = {0};
335 for (auto Cnt = Stack.size() - 1; Cnt > 0; --Cnt) {
336 if (Stack[Cnt].MappedDecls.count(VD)) {
337 VarMI = Stack[Cnt].MappedDecls[VD];
338 break;
339 }
340 }
341 return VarMI;
342 }
343
344 void addMapInfoForVar(VarDecl *VD, MapInfo MI) {
345 if (Stack.size() > 1) {
346 Stack.back().MappedDecls[VD] = MI;
347 }
348 }
349
350 MapInfo IsMappedInCurrentRegion(VarDecl *VD) {
351 assert(Stack.size() > 1 && "Target level is 0");
352 MapInfo VarMI = {0};
353 if (Stack.size() > 1 && Stack.back().MappedDecls.count(VD)) {
354 VarMI = Stack.back().MappedDecls[VD];
355 }
356 return VarMI;
357 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000358};
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000359bool isParallelOrTaskRegion(OpenMPDirectiveKind DKind) {
360 return isOpenMPParallelDirective(DKind) || DKind == OMPD_task ||
Alexey Bataev49f6e782015-12-01 04:18:41 +0000361 isOpenMPTeamsDirective(DKind) || DKind == OMPD_unknown ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +0000362 isOpenMPTaskLoopDirective(DKind);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000363}
Alexey Bataeved09d242014-05-28 05:53:51 +0000364} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000365
366DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter,
367 VarDecl *D) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000368 D = D->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000369 DSAVarData DVar;
Alexey Bataevdf9b1592014-06-25 04:09:13 +0000370 if (Iter == std::prev(Stack.rend())) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000371 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
372 // in a region but not in construct]
373 // File-scope or namespace-scope variables referenced in called routines
374 // in the region are shared unless they appear in a threadprivate
375 // directive.
Alexey Bataev8b9cb982014-07-24 02:33:58 +0000376 if (!D->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(D))
Alexey Bataev750a58b2014-03-18 12:19:12 +0000377 DVar.CKind = OMPC_shared;
378
379 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
380 // in a region but not in construct]
381 // Variables with static storage duration that are declared in called
382 // routines in the region are shared.
383 if (D->hasGlobalStorage())
384 DVar.CKind = OMPC_shared;
385
Alexey Bataev758e55e2013-09-06 18:03:48 +0000386 return DVar;
387 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000388
Alexey Bataev758e55e2013-09-06 18:03:48 +0000389 DVar.DKind = Iter->Directive;
Alexey Bataevec3da872014-01-31 05:15:34 +0000390 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
391 // in a Construct, C/C++, predetermined, p.1]
392 // Variables with automatic storage duration that are declared in a scope
393 // inside the construct are private.
Alexey Bataevf29276e2014-06-18 04:14:57 +0000394 if (isOpenMPLocal(D, Iter) && D->isLocalVarDecl() &&
395 (D->getStorageClass() == SC_Auto || D->getStorageClass() == SC_None)) {
396 DVar.CKind = OMPC_private;
397 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000398 }
399
Alexey Bataev758e55e2013-09-06 18:03:48 +0000400 // Explicitly specified attributes and local variables with predetermined
401 // attributes.
402 if (Iter->SharingMap.count(D)) {
403 DVar.RefExpr = Iter->SharingMap[D].RefExpr;
404 DVar.CKind = Iter->SharingMap[D].Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000405 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000406 return DVar;
407 }
408
409 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
410 // in a Construct, C/C++, implicitly determined, p.1]
411 // In a parallel or task construct, the data-sharing attributes of these
412 // variables are determined by the default clause, if present.
413 switch (Iter->DefaultAttr) {
414 case DSA_shared:
415 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000416 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000417 return DVar;
418 case DSA_none:
419 return DVar;
420 case DSA_unspecified:
421 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
422 // in a Construct, implicitly determined, p.2]
423 // In a parallel construct, if no default clause is present, these
424 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000425 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000426 if (isOpenMPParallelDirective(DVar.DKind) ||
427 isOpenMPTeamsDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000428 DVar.CKind = OMPC_shared;
429 return DVar;
430 }
431
432 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
433 // in a Construct, implicitly determined, p.4]
434 // In a task construct, if no default clause is present, a variable that in
435 // the enclosing context is determined to be shared by all implicit tasks
436 // bound to the current team is shared.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000437 if (DVar.DKind == OMPD_task) {
438 DSAVarData DVarTemp;
Alexey Bataev62b63b12015-03-10 07:28:44 +0000439 for (StackTy::reverse_iterator I = std::next(Iter), EE = Stack.rend();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000440 I != EE; ++I) {
Alexey Bataeved09d242014-05-28 05:53:51 +0000441 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
442 // Referenced
Alexey Bataev758e55e2013-09-06 18:03:48 +0000443 // in a Construct, implicitly determined, p.6]
444 // In a task construct, if no default clause is present, a variable
445 // whose data-sharing attribute is not determined by the rules above is
446 // firstprivate.
447 DVarTemp = getDSA(I, D);
448 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000449 DVar.RefExpr = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000450 DVar.DKind = OMPD_task;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000451 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000452 return DVar;
453 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000454 if (isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000455 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000456 }
457 DVar.DKind = OMPD_task;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000458 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000459 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000460 return DVar;
461 }
462 }
463 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
464 // in a Construct, implicitly determined, p.3]
465 // For constructs other than task, if no default clause is present, these
466 // variables inherit their data-sharing attributes from the enclosing
467 // context.
Benjamin Kramer167e9992014-03-02 12:20:24 +0000468 return getDSA(std::next(Iter), D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000469}
470
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000471DeclRefExpr *DSAStackTy::addUniqueAligned(VarDecl *D, DeclRefExpr *NewDE) {
472 assert(Stack.size() > 1 && "Data sharing attributes stack is empty");
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000473 D = D->getCanonicalDecl();
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000474 auto It = Stack.back().AlignedMap.find(D);
475 if (It == Stack.back().AlignedMap.end()) {
476 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
477 Stack.back().AlignedMap[D] = NewDE;
478 return nullptr;
479 } else {
480 assert(It->second && "Unexpected nullptr expr in the aligned map");
481 return It->second;
482 }
483 return nullptr;
484}
485
Alexey Bataev9c821032015-04-30 04:23:23 +0000486void DSAStackTy::addLoopControlVariable(VarDecl *D) {
487 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
488 D = D->getCanonicalDecl();
489 Stack.back().LCVSet.insert(D);
490}
491
492bool DSAStackTy::isLoopControlVariable(VarDecl *D) {
493 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
494 D = D->getCanonicalDecl();
495 return Stack.back().LCVSet.count(D) > 0;
496}
497
Alexey Bataev758e55e2013-09-06 18:03:48 +0000498void DSAStackTy::addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000499 D = D->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000500 if (A == OMPC_threadprivate) {
501 Stack[0].SharingMap[D].Attributes = A;
502 Stack[0].SharingMap[D].RefExpr = E;
503 } else {
504 assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
505 Stack.back().SharingMap[D].Attributes = A;
506 Stack.back().SharingMap[D].RefExpr = E;
507 }
508}
509
Alexey Bataeved09d242014-05-28 05:53:51 +0000510bool DSAStackTy::isOpenMPLocal(VarDecl *D, StackTy::reverse_iterator Iter) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000511 D = D->getCanonicalDecl();
Alexey Bataevec3da872014-01-31 05:15:34 +0000512 if (Stack.size() > 2) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000513 reverse_iterator I = Iter, E = std::prev(Stack.rend());
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000514 Scope *TopScope = nullptr;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000515 while (I != E && !isParallelOrTaskRegion(I->Directive)) {
Alexey Bataevec3da872014-01-31 05:15:34 +0000516 ++I;
517 }
Alexey Bataeved09d242014-05-28 05:53:51 +0000518 if (I == E)
519 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000520 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +0000521 Scope *CurScope = getCurScope();
522 while (CurScope != TopScope && !CurScope->isDeclScope(D)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000523 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +0000524 }
525 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000526 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000527 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000528}
529
Alexey Bataev39f915b82015-05-08 10:41:21 +0000530/// \brief Build a variable declaration for OpenMP loop iteration variable.
531static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000532 StringRef Name, const AttrVec *Attrs = nullptr) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000533 DeclContext *DC = SemaRef.CurContext;
534 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
535 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
536 VarDecl *Decl =
537 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000538 if (Attrs) {
539 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
540 I != E; ++I)
541 Decl->addAttr(*I);
542 }
Alexey Bataev39f915b82015-05-08 10:41:21 +0000543 Decl->setImplicit();
544 return Decl;
545}
546
547static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
548 SourceLocation Loc,
549 bool RefersToCapture = false) {
550 D->setReferenced();
551 D->markUsed(S.Context);
552 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
553 SourceLocation(), D, RefersToCapture, Loc, Ty,
554 VK_LValue);
555}
556
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000557DSAStackTy::DSAVarData DSAStackTy::getTopDSA(VarDecl *D, bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000558 D = D->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000559 DSAVarData DVar;
560
561 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
562 // in a Construct, C/C++, predetermined, p.1]
563 // Variables appearing in threadprivate directives are threadprivate.
Samuel Antaof8b50122015-07-13 22:54:53 +0000564 if ((D->getTLSKind() != VarDecl::TLS_None &&
565 !(D->hasAttr<OMPThreadPrivateDeclAttr>() &&
566 SemaRef.getLangOpts().OpenMPUseTLS &&
567 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +0000568 (D->getStorageClass() == SC_Register && D->hasAttr<AsmLabelAttr>() &&
569 !D->isLocalVarDecl())) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000570 addDSA(D, buildDeclRefExpr(SemaRef, D, D->getType().getNonReferenceType(),
571 D->getLocation()),
Alexey Bataevf2453a02015-05-06 07:25:08 +0000572 OMPC_threadprivate);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000573 }
574 if (Stack[0].SharingMap.count(D)) {
575 DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
576 DVar.CKind = OMPC_threadprivate;
577 return DVar;
578 }
579
580 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000581 // in a Construct, C/C++, predetermined, p.4]
582 // Static data members are shared.
583 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
584 // in a Construct, C/C++, predetermined, p.7]
585 // Variables with static storage duration that are declared in a scope
586 // inside the construct are shared.
587 if (D->isStaticDataMember()) {
588 DSAVarData DVarTemp =
589 hasDSA(D, isOpenMPPrivate, MatchesAlways(), FromParent);
590 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
Alexey Bataevec3da872014-01-31 05:15:34 +0000591 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000592
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000593 DVar.CKind = OMPC_shared;
594 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000595 }
596
597 QualType Type = D->getType().getNonReferenceType().getCanonicalType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +0000598 bool IsConstant = Type.isConstant(SemaRef.getASTContext());
599 Type = SemaRef.getASTContext().getBaseElementType(Type);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000600 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
601 // in a Construct, C/C++, predetermined, p.6]
602 // Variables with const qualified type having no mutable member are
603 // shared.
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000604 CXXRecordDecl *RD =
Alexey Bataev7ff55242014-06-19 09:13:45 +0000605 SemaRef.getLangOpts().CPlusPlus ? Type->getAsCXXRecordDecl() : nullptr;
Alexey Bataevc9bd03d2015-12-17 06:55:08 +0000606 if (auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
607 if (auto *CTD = CTSD->getSpecializedTemplate())
608 RD = CTD->getTemplatedDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +0000609 if (IsConstant &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000610 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000611 // Variables with const-qualified type having no mutable member may be
612 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000613 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
614 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000615 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
616 return DVar;
617
Alexey Bataev758e55e2013-09-06 18:03:48 +0000618 DVar.CKind = OMPC_shared;
619 return DVar;
620 }
621
Alexey Bataev758e55e2013-09-06 18:03:48 +0000622 // Explicitly specified attributes and local variables with predetermined
623 // attributes.
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000624 auto StartI = std::next(Stack.rbegin());
625 auto EndI = std::prev(Stack.rend());
626 if (FromParent && StartI != EndI) {
627 StartI = std::next(StartI);
628 }
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 Bertolli6200a3d2015-12-14 14:51:25 +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 Bertolli6200a3d2015-12-14 14:51:25 +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 Bertolli6200a3d2015-12-14 14:51:25 +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 Bertolli6200a3d2015-12-14 14:51:25 +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 Bertolli6200a3d2015-12-14 14:51:25 +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 Bertolli6200a3d2015-12-14 14:51:25 +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 Bertolli6200a3d2015-12-14 14:51:25 +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 Bertolli6200a3d2015-12-14 14:51:25 +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 Bertolli6200a3d2015-12-14 14:51:25 +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 Bertolli6200a3d2015-12-14 14:51:25 +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 Bertolli6200a3d2015-12-14 14:51:25 +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 Bertolli6200a3d2015-12-14 14:51:25 +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 Bertolli6200a3d2015-12-14 14:51:25 +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 Bertolli6200a3d2015-12-14 14:51:25 +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 Bertolli6200a3d2015-12-14 14:51:25 +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 Bertolli6200a3d2015-12-14 14:51:25 +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 Bertolli6200a3d2015-12-14 14:51:25 +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 Bertolli6200a3d2015-12-14 14:51:25 +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 Bertolli6200a3d2015-12-14 14:51:25 +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 Bertolli6200a3d2015-12-14 14:51:25 +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 Bertolli6200a3d2015-12-14 14:51:25 +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 Bertolli6200a3d2015-12-14 14:51:25 +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 Bertolli6200a3d2015-12-14 14:51:25 +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:
Alexey Bataev28c75412015-12-15 08:19:24 +00002540 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
2541 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002542 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002543 case OMPD_parallel_for:
2544 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
2545 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002546 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00002547 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00002548 case OMPD_parallel_for_simd:
2549 Res = ActOnOpenMPParallelForSimdDirective(
2550 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002551 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00002552 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002553 case OMPD_parallel_sections:
2554 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
2555 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002556 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002557 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002558 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002559 Res =
2560 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002561 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002562 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00002563 case OMPD_taskyield:
2564 assert(ClausesWithImplicit.empty() &&
2565 "No clauses are allowed for 'omp taskyield' directive");
2566 assert(AStmt == nullptr &&
2567 "No associated statement allowed for 'omp taskyield' directive");
2568 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
2569 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002570 case OMPD_barrier:
2571 assert(ClausesWithImplicit.empty() &&
2572 "No clauses are allowed for 'omp barrier' directive");
2573 assert(AStmt == nullptr &&
2574 "No associated statement allowed for 'omp barrier' directive");
2575 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
2576 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00002577 case OMPD_taskwait:
2578 assert(ClausesWithImplicit.empty() &&
2579 "No clauses are allowed for 'omp taskwait' directive");
2580 assert(AStmt == nullptr &&
2581 "No associated statement allowed for 'omp taskwait' directive");
2582 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
2583 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002584 case OMPD_taskgroup:
2585 assert(ClausesWithImplicit.empty() &&
2586 "No clauses are allowed for 'omp taskgroup' directive");
2587 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
2588 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00002589 case OMPD_flush:
2590 assert(AStmt == nullptr &&
2591 "No associated statement allowed for 'omp flush' directive");
2592 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
2593 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002594 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00002595 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
2596 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002597 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00002598 case OMPD_atomic:
2599 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
2600 EndLoc);
2601 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002602 case OMPD_teams:
2603 Res =
2604 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
2605 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002606 case OMPD_target:
2607 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
2608 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002609 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002610 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002611 case OMPD_cancellation_point:
2612 assert(ClausesWithImplicit.empty() &&
2613 "No clauses are allowed for 'omp cancellation point' directive");
2614 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
2615 "cancellation point' directive");
2616 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
2617 break;
Alexey Bataev80909872015-07-02 11:25:17 +00002618 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00002619 assert(AStmt == nullptr &&
2620 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00002621 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
2622 CancelRegion);
2623 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00002624 break;
Michael Wong65f367f2015-07-21 13:44:28 +00002625 case OMPD_target_data:
2626 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
2627 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002628 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00002629 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00002630 case OMPD_taskloop:
2631 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
2632 EndLoc, VarsWithInheritedDSA);
2633 AllowedNameModifiers.push_back(OMPD_taskloop);
2634 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002635 case OMPD_taskloop_simd:
2636 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2637 EndLoc, VarsWithInheritedDSA);
2638 AllowedNameModifiers.push_back(OMPD_taskloop);
2639 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002640 case OMPD_distribute:
2641 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
2642 EndLoc, VarsWithInheritedDSA);
2643 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002644 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002645 llvm_unreachable("OpenMP Directive is not allowed");
2646 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002647 llvm_unreachable("Unknown OpenMP directive");
2648 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002649
Alexey Bataev4acb8592014-07-07 13:01:15 +00002650 for (auto P : VarsWithInheritedDSA) {
2651 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
2652 << P.first << P.second->getSourceRange();
2653 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002654 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
2655
2656 if (!AllowedNameModifiers.empty())
2657 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
2658 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002659
Alexey Bataeved09d242014-05-28 05:53:51 +00002660 if (ErrorFound)
2661 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002662 return Res;
2663}
2664
2665StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
2666 Stmt *AStmt,
2667 SourceLocation StartLoc,
2668 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002669 if (!AStmt)
2670 return StmtError();
2671
Alexey Bataev9959db52014-05-06 10:08:46 +00002672 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
2673 // 1.2.2 OpenMP Language Terminology
2674 // Structured block - An executable statement with a single entry at the
2675 // top and a single exit at the bottom.
2676 // The point of exit cannot be a branch out of the structured block.
2677 // longjmp() and throw() must not violate the entry/exit criteria.
2678 CS->getCapturedDecl()->setNothrow();
2679
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002680 getCurFunction()->setHasBranchProtectedScope();
2681
Alexey Bataev25e5b442015-09-15 12:52:43 +00002682 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
2683 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002684}
2685
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002686namespace {
2687/// \brief Helper class for checking canonical form of the OpenMP loops and
2688/// extracting iteration space of each loop in the loop nest, that will be used
2689/// for IR generation.
2690class OpenMPIterationSpaceChecker {
2691 /// \brief Reference to Sema.
2692 Sema &SemaRef;
2693 /// \brief A location for diagnostics (when there is no some better location).
2694 SourceLocation DefaultLoc;
2695 /// \brief A location for diagnostics (when increment is not compatible).
2696 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002697 /// \brief A source location for referring to loop init later.
2698 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002699 /// \brief A source location for referring to condition later.
2700 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002701 /// \brief A source location for referring to increment later.
2702 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002703 /// \brief Loop variable.
2704 VarDecl *Var;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002705 /// \brief Reference to loop variable.
2706 DeclRefExpr *VarRef;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002707 /// \brief Lower bound (initializer for the var).
2708 Expr *LB;
2709 /// \brief Upper bound.
2710 Expr *UB;
2711 /// \brief Loop step (increment).
2712 Expr *Step;
2713 /// \brief This flag is true when condition is one of:
2714 /// Var < UB
2715 /// Var <= UB
2716 /// UB > Var
2717 /// UB >= Var
2718 bool TestIsLessOp;
2719 /// \brief This flag is true when condition is strict ( < or > ).
2720 bool TestIsStrictOp;
2721 /// \brief This flag is true when step is subtracted on each iteration.
2722 bool SubtractStep;
2723
2724public:
2725 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
2726 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
Alexander Musmana5f070a2014-10-01 06:03:56 +00002727 InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()),
2728 IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002729 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
2730 TestIsStrictOp(false), SubtractStep(false) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002731 /// \brief Check init-expr for canonical loop form and save loop counter
2732 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00002733 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002734 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
2735 /// for less/greater and for strict/non-strict comparison.
2736 bool CheckCond(Expr *S);
2737 /// \brief Check incr-expr for canonical loop form and return true if it
2738 /// does not conform, otherwise save loop step (#Step).
2739 bool CheckInc(Expr *S);
2740 /// \brief Return the loop counter variable.
2741 VarDecl *GetLoopVar() const { return Var; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002742 /// \brief Return the reference expression to loop counter variable.
2743 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002744 /// \brief Source range of the loop init.
2745 SourceRange GetInitSrcRange() const { return InitSrcRange; }
2746 /// \brief Source range of the loop condition.
2747 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
2748 /// \brief Source range of the loop increment.
2749 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
2750 /// \brief True if the step should be subtracted.
2751 bool ShouldSubtractStep() const { return SubtractStep; }
2752 /// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00002753 Expr *BuildNumIterations(Scope *S, const bool LimitedType) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00002754 /// \brief Build the precondition expression for the loops.
2755 Expr *BuildPreCond(Scope *S, Expr *Cond) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002756 /// \brief Build reference expression to the counter be used for codegen.
2757 Expr *BuildCounterVar() const;
Alexey Bataeva8899172015-08-06 12:30:57 +00002758 /// \brief Build reference expression to the private counter be used for
2759 /// codegen.
2760 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002761 /// \brief Build initization of the counter be used for codegen.
2762 Expr *BuildCounterInit() const;
2763 /// \brief Build step of the counter be used for codegen.
2764 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002765 /// \brief Return true if any expression is dependent.
2766 bool Dependent() const;
2767
2768private:
2769 /// \brief Check the right-hand side of an assignment in the increment
2770 /// expression.
2771 bool CheckIncRHS(Expr *RHS);
2772 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002773 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002774 /// \brief Helper to set upper bound.
Craig Toppere335f252015-10-04 04:53:55 +00002775 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00002776 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002777 /// \brief Helper to set loop increment.
2778 bool SetStep(Expr *NewStep, bool Subtract);
2779};
2780
2781bool OpenMPIterationSpaceChecker::Dependent() const {
2782 if (!Var) {
2783 assert(!LB && !UB && !Step);
2784 return false;
2785 }
2786 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
2787 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
2788}
2789
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002790template <typename T>
2791static T *getExprAsWritten(T *E) {
2792 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
2793 E = ExprTemp->getSubExpr();
2794
2795 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
2796 E = MTE->GetTemporaryExpr();
2797
2798 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
2799 E = Binder->getSubExpr();
2800
2801 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
2802 E = ICE->getSubExprAsWritten();
2803 return E->IgnoreParens();
2804}
2805
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002806bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
2807 DeclRefExpr *NewVarRefExpr,
2808 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002809 // State consistency checking to ensure correct usage.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002810 assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
2811 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002812 if (!NewVar || !NewLB)
2813 return true;
2814 Var = NewVar;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002815 VarRef = NewVarRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002816 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
2817 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00002818 if ((Ctor->isCopyOrMoveConstructor() ||
2819 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
2820 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002821 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002822 LB = NewLB;
2823 return false;
2824}
2825
2826bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00002827 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002828 // State consistency checking to ensure correct usage.
2829 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
2830 !TestIsLessOp && !TestIsStrictOp);
2831 if (!NewUB)
2832 return true;
2833 UB = NewUB;
2834 TestIsLessOp = LessOp;
2835 TestIsStrictOp = StrictOp;
2836 ConditionSrcRange = SR;
2837 ConditionLoc = SL;
2838 return false;
2839}
2840
2841bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
2842 // State consistency checking to ensure correct usage.
2843 assert(Var != nullptr && LB != nullptr && Step == nullptr);
2844 if (!NewStep)
2845 return true;
2846 if (!NewStep->isValueDependent()) {
2847 // Check that the step is integer expression.
2848 SourceLocation StepLoc = NewStep->getLocStart();
2849 ExprResult Val =
2850 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
2851 if (Val.isInvalid())
2852 return true;
2853 NewStep = Val.get();
2854
2855 // OpenMP [2.6, Canonical Loop Form, Restrictions]
2856 // If test-expr is of form var relational-op b and relational-op is < or
2857 // <= then incr-expr must cause var to increase on each iteration of the
2858 // loop. If test-expr is of form var relational-op b and relational-op is
2859 // > or >= then incr-expr must cause var to decrease on each iteration of
2860 // the loop.
2861 // If test-expr is of form b relational-op var and relational-op is < or
2862 // <= then incr-expr must cause var to decrease on each iteration of the
2863 // loop. If test-expr is of form b relational-op var and relational-op is
2864 // > or >= then incr-expr must cause var to increase on each iteration of
2865 // the loop.
2866 llvm::APSInt Result;
2867 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
2868 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
2869 bool IsConstNeg =
2870 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00002871 bool IsConstPos =
2872 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002873 bool IsConstZero = IsConstant && !Result.getBoolValue();
2874 if (UB && (IsConstZero ||
2875 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00002876 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002877 SemaRef.Diag(NewStep->getExprLoc(),
2878 diag::err_omp_loop_incr_not_compatible)
2879 << Var << TestIsLessOp << NewStep->getSourceRange();
2880 SemaRef.Diag(ConditionLoc,
2881 diag::note_omp_loop_cond_requres_compatible_incr)
2882 << TestIsLessOp << ConditionSrcRange;
2883 return true;
2884 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002885 if (TestIsLessOp == Subtract) {
2886 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
2887 NewStep).get();
2888 Subtract = !Subtract;
2889 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002890 }
2891
2892 Step = NewStep;
2893 SubtractStep = Subtract;
2894 return false;
2895}
2896
Alexey Bataev9c821032015-04-30 04:23:23 +00002897bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002898 // Check init-expr for canonical loop form and save loop counter
2899 // variable - #Var and its initialization value - #LB.
2900 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
2901 // var = lb
2902 // integer-type var = lb
2903 // random-access-iterator-type var = lb
2904 // pointer-type var = lb
2905 //
2906 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00002907 if (EmitDiags) {
2908 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
2909 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002910 return true;
2911 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002912 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002913 if (Expr *E = dyn_cast<Expr>(S))
2914 S = E->IgnoreParens();
2915 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2916 if (BO->getOpcode() == BO_Assign)
2917 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002918 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002919 BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002920 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
2921 if (DS->isSingleDecl()) {
2922 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00002923 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002924 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00002925 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002926 SemaRef.Diag(S->getLocStart(),
2927 diag::ext_omp_loop_not_canonical_init)
2928 << S->getSourceRange();
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002929 return SetVarAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002930 }
2931 }
2932 }
2933 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
2934 if (CE->getOperator() == OO_Equal)
2935 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002936 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
2937 CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002938
Alexey Bataev9c821032015-04-30 04:23:23 +00002939 if (EmitDiags) {
2940 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
2941 << S->getSourceRange();
2942 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002943 return true;
2944}
2945
Alexey Bataev23b69422014-06-18 07:08:49 +00002946/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002947/// variable (which may be the loop variable) if possible.
2948static const VarDecl *GetInitVarDecl(const Expr *E) {
2949 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00002950 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002951 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002952 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
2953 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00002954 if ((Ctor->isCopyOrMoveConstructor() ||
2955 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
2956 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002957 E = CE->getArg(0)->IgnoreParenImpCasts();
2958 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
2959 if (!DRE)
2960 return nullptr;
2961 return dyn_cast<VarDecl>(DRE->getDecl());
2962}
2963
2964bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
2965 // Check test-expr for canonical form, save upper-bound UB, flags for
2966 // less/greater and for strict/non-strict comparison.
2967 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2968 // var relational-op b
2969 // b relational-op var
2970 //
2971 if (!S) {
2972 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
2973 return true;
2974 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002975 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002976 SourceLocation CondLoc = S->getLocStart();
2977 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2978 if (BO->isRelationalOp()) {
2979 if (GetInitVarDecl(BO->getLHS()) == Var)
2980 return SetUB(BO->getRHS(),
2981 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
2982 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2983 BO->getSourceRange(), BO->getOperatorLoc());
2984 if (GetInitVarDecl(BO->getRHS()) == Var)
2985 return SetUB(BO->getLHS(),
2986 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
2987 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2988 BO->getSourceRange(), BO->getOperatorLoc());
2989 }
2990 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2991 if (CE->getNumArgs() == 2) {
2992 auto Op = CE->getOperator();
2993 switch (Op) {
2994 case OO_Greater:
2995 case OO_GreaterEqual:
2996 case OO_Less:
2997 case OO_LessEqual:
2998 if (GetInitVarDecl(CE->getArg(0)) == Var)
2999 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
3000 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3001 CE->getOperatorLoc());
3002 if (GetInitVarDecl(CE->getArg(1)) == Var)
3003 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
3004 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3005 CE->getOperatorLoc());
3006 break;
3007 default:
3008 break;
3009 }
3010 }
3011 }
3012 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
3013 << S->getSourceRange() << Var;
3014 return true;
3015}
3016
3017bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
3018 // RHS of canonical loop form increment can be:
3019 // var + incr
3020 // incr + var
3021 // var - incr
3022 //
3023 RHS = RHS->IgnoreParenImpCasts();
3024 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
3025 if (BO->isAdditiveOp()) {
3026 bool IsAdd = BO->getOpcode() == BO_Add;
3027 if (GetInitVarDecl(BO->getLHS()) == Var)
3028 return SetStep(BO->getRHS(), !IsAdd);
3029 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
3030 return SetStep(BO->getLHS(), false);
3031 }
3032 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
3033 bool IsAdd = CE->getOperator() == OO_Plus;
3034 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
3035 if (GetInitVarDecl(CE->getArg(0)) == Var)
3036 return SetStep(CE->getArg(1), !IsAdd);
3037 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
3038 return SetStep(CE->getArg(0), false);
3039 }
3040 }
3041 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3042 << RHS->getSourceRange() << Var;
3043 return true;
3044}
3045
3046bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
3047 // Check incr-expr for canonical loop form and return true if it
3048 // does not conform.
3049 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3050 // ++var
3051 // var++
3052 // --var
3053 // var--
3054 // var += incr
3055 // var -= incr
3056 // var = var + incr
3057 // var = incr + var
3058 // var = var - incr
3059 //
3060 if (!S) {
3061 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
3062 return true;
3063 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003064 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003065 S = S->IgnoreParens();
3066 if (auto UO = dyn_cast<UnaryOperator>(S)) {
3067 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
3068 return SetStep(
3069 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
3070 (UO->isDecrementOp() ? -1 : 1)).get(),
3071 false);
3072 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
3073 switch (BO->getOpcode()) {
3074 case BO_AddAssign:
3075 case BO_SubAssign:
3076 if (GetInitVarDecl(BO->getLHS()) == Var)
3077 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
3078 break;
3079 case BO_Assign:
3080 if (GetInitVarDecl(BO->getLHS()) == Var)
3081 return CheckIncRHS(BO->getRHS());
3082 break;
3083 default:
3084 break;
3085 }
3086 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3087 switch (CE->getOperator()) {
3088 case OO_PlusPlus:
3089 case OO_MinusMinus:
3090 if (GetInitVarDecl(CE->getArg(0)) == Var)
3091 return SetStep(
3092 SemaRef.ActOnIntegerConstant(
3093 CE->getLocStart(),
3094 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
3095 false);
3096 break;
3097 case OO_PlusEqual:
3098 case OO_MinusEqual:
3099 if (GetInitVarDecl(CE->getArg(0)) == Var)
3100 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
3101 break;
3102 case OO_Equal:
3103 if (GetInitVarDecl(CE->getArg(0)) == Var)
3104 return CheckIncRHS(CE->getArg(1));
3105 break;
3106 default:
3107 break;
3108 }
3109 }
3110 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3111 << S->getSourceRange() << Var;
3112 return true;
3113}
Alexander Musmana5f070a2014-10-01 06:03:56 +00003114
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003115namespace {
3116// Transform variables declared in GNU statement expressions to new ones to
3117// avoid crash on codegen.
3118class TransformToNewDefs : public TreeTransform<TransformToNewDefs> {
3119 typedef TreeTransform<TransformToNewDefs> BaseTransform;
3120
3121public:
3122 TransformToNewDefs(Sema &SemaRef) : BaseTransform(SemaRef) {}
3123
3124 Decl *TransformDefinition(SourceLocation Loc, Decl *D) {
3125 if (auto *VD = cast<VarDecl>(D))
3126 if (!isa<ParmVarDecl>(D) && !isa<VarTemplateSpecializationDecl>(D) &&
3127 !isa<ImplicitParamDecl>(D)) {
3128 auto *NewVD = VarDecl::Create(
3129 SemaRef.Context, VD->getDeclContext(), VD->getLocStart(),
3130 VD->getLocation(), VD->getIdentifier(), VD->getType(),
3131 VD->getTypeSourceInfo(), VD->getStorageClass());
3132 NewVD->setTSCSpec(VD->getTSCSpec());
3133 NewVD->setInit(VD->getInit());
3134 NewVD->setInitStyle(VD->getInitStyle());
3135 NewVD->setExceptionVariable(VD->isExceptionVariable());
3136 NewVD->setNRVOVariable(VD->isNRVOVariable());
3137 NewVD->setCXXForRangeDecl(VD->isInExternCXXContext());
3138 NewVD->setConstexpr(VD->isConstexpr());
3139 NewVD->setInitCapture(VD->isInitCapture());
3140 NewVD->setPreviousDeclInSameBlockScope(
3141 VD->isPreviousDeclInSameBlockScope());
3142 VD->getDeclContext()->addHiddenDecl(NewVD);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003143 if (VD->hasAttrs())
3144 NewVD->setAttrs(VD->getAttrs());
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003145 transformedLocalDecl(VD, NewVD);
3146 return NewVD;
3147 }
3148 return BaseTransform::TransformDefinition(Loc, D);
3149 }
3150
3151 ExprResult TransformDeclRefExpr(DeclRefExpr *E) {
3152 if (auto *NewD = TransformDecl(E->getExprLoc(), E->getDecl()))
3153 if (E->getDecl() != NewD) {
3154 NewD->setReferenced();
3155 NewD->markUsed(SemaRef.Context);
3156 return DeclRefExpr::Create(
3157 SemaRef.Context, E->getQualifierLoc(), E->getTemplateKeywordLoc(),
3158 cast<ValueDecl>(NewD), E->refersToEnclosingVariableOrCapture(),
3159 E->getNameInfo(), E->getType(), E->getValueKind());
3160 }
3161 return BaseTransform::TransformDeclRefExpr(E);
3162 }
3163};
3164}
3165
Alexander Musmana5f070a2014-10-01 06:03:56 +00003166/// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00003167Expr *
3168OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S,
3169 const bool LimitedType) const {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003170 TransformToNewDefs Transform(SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003171 ExprResult Diff;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003172 auto VarType = Var->getType().getNonReferenceType();
3173 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003174 SemaRef.getLangOpts().CPlusPlus) {
3175 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003176 auto *UBExpr = TestIsLessOp ? UB : LB;
3177 auto *LBExpr = TestIsLessOp ? LB : UB;
3178 Expr *Upper = Transform.TransformExpr(UBExpr).get();
3179 Expr *Lower = Transform.TransformExpr(LBExpr).get();
3180 if (!Upper || !Lower)
3181 return nullptr;
3182 Upper = SemaRef.PerformImplicitConversion(Upper, UBExpr->getType(),
3183 Sema::AA_Converting,
3184 /*AllowExplicit=*/true)
3185 .get();
3186 Lower = SemaRef.PerformImplicitConversion(Lower, LBExpr->getType(),
3187 Sema::AA_Converting,
3188 /*AllowExplicit=*/true)
3189 .get();
3190 if (!Upper || !Lower)
3191 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003192
3193 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
3194
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003195 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00003196 // BuildBinOp already emitted error, this one is to point user to upper
3197 // and lower bound, and to tell what is passed to 'operator-'.
3198 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
3199 << Upper->getSourceRange() << Lower->getSourceRange();
3200 return nullptr;
3201 }
3202 }
3203
3204 if (!Diff.isUsable())
3205 return nullptr;
3206
3207 // Upper - Lower [- 1]
3208 if (TestIsStrictOp)
3209 Diff = SemaRef.BuildBinOp(
3210 S, DefaultLoc, BO_Sub, Diff.get(),
3211 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3212 if (!Diff.isUsable())
3213 return nullptr;
3214
3215 // Upper - Lower [- 1] + Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003216 auto NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
3217 if (NewStep.isInvalid())
3218 return nullptr;
3219 NewStep = SemaRef.PerformImplicitConversion(
3220 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
3221 /*AllowExplicit=*/true);
3222 if (NewStep.isInvalid())
3223 return nullptr;
3224 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003225 if (!Diff.isUsable())
3226 return nullptr;
3227
3228 // Parentheses (for dumping/debugging purposes only).
3229 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
3230 if (!Diff.isUsable())
3231 return nullptr;
3232
3233 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003234 NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
3235 if (NewStep.isInvalid())
3236 return nullptr;
3237 NewStep = SemaRef.PerformImplicitConversion(
3238 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
3239 /*AllowExplicit=*/true);
3240 if (NewStep.isInvalid())
3241 return nullptr;
3242 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003243 if (!Diff.isUsable())
3244 return nullptr;
3245
Alexander Musman174b3ca2014-10-06 11:16:29 +00003246 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003247 QualType Type = Diff.get()->getType();
3248 auto &C = SemaRef.Context;
3249 bool UseVarType = VarType->hasIntegerRepresentation() &&
3250 C.getTypeSize(Type) > C.getTypeSize(VarType);
3251 if (!Type->isIntegerType() || UseVarType) {
3252 unsigned NewSize =
3253 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
3254 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
3255 : Type->hasSignedIntegerRepresentation();
3256 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
3257 Diff = SemaRef.PerformImplicitConversion(
3258 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
3259 if (!Diff.isUsable())
3260 return nullptr;
3261 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00003262 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00003263 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
3264 if (NewSize != C.getTypeSize(Type)) {
3265 if (NewSize < C.getTypeSize(Type)) {
3266 assert(NewSize == 64 && "incorrect loop var size");
3267 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
3268 << InitSrcRange << ConditionSrcRange;
3269 }
3270 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003271 NewSize, Type->hasSignedIntegerRepresentation() ||
3272 C.getTypeSize(Type) < NewSize);
Alexander Musman174b3ca2014-10-06 11:16:29 +00003273 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
3274 Sema::AA_Converting, true);
3275 if (!Diff.isUsable())
3276 return nullptr;
3277 }
3278 }
3279
Alexander Musmana5f070a2014-10-01 06:03:56 +00003280 return Diff.get();
3281}
3282
Alexey Bataev62dbb972015-04-22 11:59:37 +00003283Expr *OpenMPIterationSpaceChecker::BuildPreCond(Scope *S, Expr *Cond) const {
3284 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
3285 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
3286 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003287 TransformToNewDefs Transform(SemaRef);
3288
3289 auto NewLB = Transform.TransformExpr(LB);
3290 auto NewUB = Transform.TransformExpr(UB);
3291 if (NewLB.isInvalid() || NewUB.isInvalid())
3292 return Cond;
3293 NewLB = SemaRef.PerformImplicitConversion(NewLB.get(), LB->getType(),
3294 Sema::AA_Converting,
3295 /*AllowExplicit=*/true);
3296 NewUB = SemaRef.PerformImplicitConversion(NewUB.get(), UB->getType(),
3297 Sema::AA_Converting,
3298 /*AllowExplicit=*/true);
3299 if (NewLB.isInvalid() || NewUB.isInvalid())
3300 return Cond;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003301 auto CondExpr = SemaRef.BuildBinOp(
3302 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
3303 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003304 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003305 if (CondExpr.isUsable()) {
3306 CondExpr = SemaRef.PerformImplicitConversion(
3307 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
3308 /*AllowExplicit=*/true);
3309 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00003310 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
3311 // Otherwise use original loop conditon and evaluate it in runtime.
3312 return CondExpr.isUsable() ? CondExpr.get() : Cond;
3313}
3314
Alexander Musmana5f070a2014-10-01 06:03:56 +00003315/// \brief Build reference expression to the counter be used for codegen.
3316Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const {
Alexey Bataeva8899172015-08-06 12:30:57 +00003317 return buildDeclRefExpr(SemaRef, Var, Var->getType().getNonReferenceType(),
3318 DefaultLoc);
3319}
3320
3321Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
3322 if (Var && !Var->isInvalidDecl()) {
3323 auto Type = Var->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003324 auto *PrivateVar =
3325 buildVarDecl(SemaRef, DefaultLoc, Type, Var->getName(),
3326 Var->hasAttrs() ? &Var->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00003327 if (PrivateVar->isInvalidDecl())
3328 return nullptr;
3329 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
3330 }
3331 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003332}
3333
3334/// \brief Build initization of the counter be used for codegen.
3335Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
3336
3337/// \brief Build step of the counter be used for codegen.
3338Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
3339
3340/// \brief Iteration space of a single for loop.
3341struct LoopIterationSpace {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003342 /// \brief Condition of the loop.
3343 Expr *PreCond;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003344 /// \brief This expression calculates the number of iterations in the loop.
3345 /// It is always possible to calculate it before starting the loop.
3346 Expr *NumIterations;
3347 /// \brief The loop counter variable.
3348 Expr *CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00003349 /// \brief Private loop counter variable.
3350 Expr *PrivateCounterVar;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003351 /// \brief This is initializer for the initial value of #CounterVar.
3352 Expr *CounterInit;
3353 /// \brief This is step for the #CounterVar used to generate its update:
3354 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
3355 Expr *CounterStep;
3356 /// \brief Should step be subtracted?
3357 bool Subtract;
3358 /// \brief Source range of the loop init.
3359 SourceRange InitSrcRange;
3360 /// \brief Source range of the loop condition.
3361 SourceRange CondSrcRange;
3362 /// \brief Source range of the loop increment.
3363 SourceRange IncSrcRange;
3364};
3365
Alexey Bataev23b69422014-06-18 07:08:49 +00003366} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003367
Alexey Bataev9c821032015-04-30 04:23:23 +00003368void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
3369 assert(getLangOpts().OpenMP && "OpenMP is not active.");
3370 assert(Init && "Expected loop in canonical form.");
3371 unsigned CollapseIteration = DSAStack->getCollapseNumber();
3372 if (CollapseIteration > 0 &&
3373 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
3374 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
3375 if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
3376 DSAStack->addLoopControlVariable(ISC.GetLoopVar());
3377 }
3378 DSAStack->setCollapseNumber(CollapseIteration - 1);
3379 }
3380}
3381
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003382/// \brief Called on a for stmt to check and extract its iteration space
3383/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00003384static bool CheckOpenMPIterationSpace(
3385 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
3386 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003387 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003388 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
3389 LoopIterationSpace &ResultIterSpace) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003390 // OpenMP [2.6, Canonical Loop Form]
3391 // for (init-expr; test-expr; incr-expr) structured-block
3392 auto For = dyn_cast_or_null<ForStmt>(S);
3393 if (!For) {
3394 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003395 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
3396 << getOpenMPDirectiveName(DKind) << NestedLoopCount
3397 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
3398 if (NestedLoopCount > 1) {
3399 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
3400 SemaRef.Diag(DSA.getConstructLoc(),
3401 diag::note_omp_collapse_ordered_expr)
3402 << 2 << CollapseLoopCountExpr->getSourceRange()
3403 << OrderedLoopCountExpr->getSourceRange();
3404 else if (CollapseLoopCountExpr)
3405 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
3406 diag::note_omp_collapse_ordered_expr)
3407 << 0 << CollapseLoopCountExpr->getSourceRange();
3408 else
3409 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
3410 diag::note_omp_collapse_ordered_expr)
3411 << 1 << OrderedLoopCountExpr->getSourceRange();
3412 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003413 return true;
3414 }
3415 assert(For->getBody());
3416
3417 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
3418
3419 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003420 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003421 if (ISC.CheckInit(Init)) {
3422 return true;
3423 }
3424
3425 bool HasErrors = false;
3426
3427 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003428 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003429
3430 // OpenMP [2.6, Canonical Loop Form]
3431 // Var is one of the following:
3432 // A variable of signed or unsigned integer type.
3433 // For C++, a variable of a random access iterator type.
3434 // For C, a variable of a pointer type.
Alexey Bataeva8899172015-08-06 12:30:57 +00003435 auto VarType = Var->getType().getNonReferenceType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003436 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
3437 !VarType->isPointerType() &&
3438 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
3439 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
3440 << SemaRef.getLangOpts().CPlusPlus;
3441 HasErrors = true;
3442 }
3443
Alexey Bataev4acb8592014-07-07 13:01:15 +00003444 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
3445 // Construct
3446 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3447 // parallel for construct is (are) private.
3448 // The loop iteration variable in the associated for-loop of a simd construct
3449 // with just one associated for-loop is linear with a constant-linear-step
3450 // that is the increment of the associated for-loop.
3451 // Exclude loop var from the list of variables with implicitly defined data
3452 // sharing attributes.
Benjamin Kramerad8e0792014-10-10 15:32:48 +00003453 VarsWithImplicitDSA.erase(Var);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003454
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003455 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
3456 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00003457 // The loop iteration variable in the associated for-loop of a simd construct
3458 // with just one associated for-loop may be listed in a linear clause with a
3459 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00003460 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3461 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003462 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003463 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
3464 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
3465 // declared in the loop and it is predetermined as a private.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003466 auto PredeterminedCKind =
3467 isOpenMPSimdDirective(DKind)
3468 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
3469 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003470 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataev0c024df2015-05-12 09:02:07 +00003471 DVar.CKind != OMPC_threadprivate && DVar.CKind != PredeterminedCKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003472 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
3473 isOpenMPDistributeDirective(DKind)) &&
Alexey Bataev49f6e782015-12-01 04:18:41 +00003474 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
3475 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate &&
3476 DVar.CKind != OMPC_threadprivate)) &&
Alexey Bataev0c024df2015-05-12 09:02:07 +00003477 ((DVar.CKind != OMPC_private && DVar.CKind != OMPC_threadprivate) ||
3478 DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003479 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00003480 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
3481 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataevf2453a02015-05-06 07:25:08 +00003482 if (DVar.RefExpr == nullptr)
3483 DVar.CKind = PredeterminedCKind;
3484 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, /*IsLoopIterVar=*/true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003485 HasErrors = true;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003486 } else if (LoopVarRefExpr != nullptr) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00003487 // Make the loop iteration variable private (for worksharing constructs),
3488 // linear (for simd directives with the only one associated loop) or
Alexey Bataev10e775f2015-07-30 11:36:16 +00003489 // lastprivate (for simd directives with several collapsed or ordered
3490 // loops).
Alexey Bataev9aba41c2014-11-14 04:08:45 +00003491 if (DVar.CKind == OMPC_unknown)
3492 DVar = DSA.hasDSA(Var, isOpenMPPrivate, MatchesAlways(),
3493 /*FromParent=*/false);
Alexey Bataev9c821032015-04-30 04:23:23 +00003494 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003495 }
3496
Alexey Bataev7ff55242014-06-19 09:13:45 +00003497 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00003498
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003499 // Check test-expr.
3500 HasErrors |= ISC.CheckCond(For->getCond());
3501
3502 // Check incr-expr.
3503 HasErrors |= ISC.CheckInc(For->getInc());
3504
Alexander Musmana5f070a2014-10-01 06:03:56 +00003505 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003506 return HasErrors;
3507
Alexander Musmana5f070a2014-10-01 06:03:56 +00003508 // Build the loop's iteration space representation.
Alexey Bataev62dbb972015-04-22 11:59:37 +00003509 ResultIterSpace.PreCond = ISC.BuildPreCond(DSA.getCurScope(), For->getCond());
Alexander Musman174b3ca2014-10-06 11:16:29 +00003510 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003511 DSA.getCurScope(), (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003512 isOpenMPTaskLoopDirective(DKind) ||
3513 isOpenMPDistributeDirective(DKind)));
Alexander Musmana5f070a2014-10-01 06:03:56 +00003514 ResultIterSpace.CounterVar = ISC.BuildCounterVar();
Alexey Bataeva8899172015-08-06 12:30:57 +00003515 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003516 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
3517 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
3518 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
3519 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
3520 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
3521 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
3522
Alexey Bataev62dbb972015-04-22 11:59:37 +00003523 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
3524 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003525 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00003526 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003527 ResultIterSpace.CounterInit == nullptr ||
3528 ResultIterSpace.CounterStep == nullptr);
3529
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003530 return HasErrors;
3531}
3532
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003533/// \brief Build 'VarRef = Start.
3534static ExprResult BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc,
3535 ExprResult VarRef, ExprResult Start) {
3536 TransformToNewDefs Transform(SemaRef);
3537 // Build 'VarRef = Start.
3538 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
3539 if (NewStart.isInvalid())
3540 return ExprError();
3541 NewStart = SemaRef.PerformImplicitConversion(
3542 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
3543 Sema::AA_Converting,
3544 /*AllowExplicit=*/true);
3545 if (NewStart.isInvalid())
3546 return ExprError();
3547 NewStart = SemaRef.PerformImplicitConversion(
3548 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
3549 /*AllowExplicit=*/true);
3550 if (!NewStart.isUsable())
3551 return ExprError();
3552
3553 auto Init =
3554 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
3555 return Init;
3556}
3557
Alexander Musmana5f070a2014-10-01 06:03:56 +00003558/// \brief Build 'VarRef = Start + Iter * Step'.
3559static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S,
3560 SourceLocation Loc, ExprResult VarRef,
3561 ExprResult Start, ExprResult Iter,
3562 ExprResult Step, bool Subtract) {
3563 // Add parentheses (for debugging purposes only).
3564 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
3565 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
3566 !Step.isUsable())
3567 return ExprError();
3568
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003569 TransformToNewDefs Transform(SemaRef);
3570 auto NewStep = Transform.TransformExpr(Step.get()->IgnoreImplicit());
3571 if (NewStep.isInvalid())
3572 return ExprError();
3573 NewStep = SemaRef.PerformImplicitConversion(
3574 NewStep.get(), Step.get()->IgnoreImplicit()->getType(),
3575 Sema::AA_Converting,
3576 /*AllowExplicit=*/true);
3577 if (NewStep.isInvalid())
3578 return ExprError();
3579 ExprResult Update =
3580 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003581 if (!Update.isUsable())
3582 return ExprError();
3583
3584 // Build 'VarRef = Start + Iter * Step'.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003585 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
3586 if (NewStart.isInvalid())
3587 return ExprError();
3588 NewStart = SemaRef.PerformImplicitConversion(
3589 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
3590 Sema::AA_Converting,
3591 /*AllowExplicit=*/true);
3592 if (NewStart.isInvalid())
3593 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003594 Update = SemaRef.BuildBinOp(S, Loc, (Subtract ? BO_Sub : BO_Add),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003595 NewStart.get(), Update.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003596 if (!Update.isUsable())
3597 return ExprError();
3598
3599 Update = SemaRef.PerformImplicitConversion(
3600 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
3601 if (!Update.isUsable())
3602 return ExprError();
3603
3604 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
3605 return Update;
3606}
3607
3608/// \brief Convert integer expression \a E to make it have at least \a Bits
3609/// bits.
3610static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
3611 Sema &SemaRef) {
3612 if (E == nullptr)
3613 return ExprError();
3614 auto &C = SemaRef.Context;
3615 QualType OldType = E->getType();
3616 unsigned HasBits = C.getTypeSize(OldType);
3617 if (HasBits >= Bits)
3618 return ExprResult(E);
3619 // OK to convert to signed, because new type has more bits than old.
3620 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
3621 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
3622 true);
3623}
3624
3625/// \brief Check if the given expression \a E is a constant integer that fits
3626/// into \a Bits bits.
3627static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
3628 if (E == nullptr)
3629 return false;
3630 llvm::APSInt Result;
3631 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
3632 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
3633 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003634}
3635
3636/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00003637/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
3638/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003639static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00003640CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
3641 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
3642 DSAStackTy &DSA,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003643 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00003644 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003645 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003646 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003647 // Found 'collapse' clause - calculate collapse number.
3648 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003649 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00003650 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00003651 }
3652 if (OrderedLoopCountExpr) {
3653 // Found 'ordered' clause - calculate collapse number.
3654 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00003655 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
3656 if (Result.getLimitedValue() < NestedLoopCount) {
3657 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
3658 diag::err_omp_wrong_ordered_loop_count)
3659 << OrderedLoopCountExpr->getSourceRange();
3660 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
3661 diag::note_collapse_loop_count)
3662 << CollapseLoopCountExpr->getSourceRange();
3663 }
3664 NestedLoopCount = Result.getLimitedValue();
3665 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003666 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003667 // This is helper routine for loop directives (e.g., 'for', 'simd',
3668 // 'for simd', etc.).
Alexander Musmana5f070a2014-10-01 06:03:56 +00003669 SmallVector<LoopIterationSpace, 4> IterSpaces;
3670 IterSpaces.resize(NestedLoopCount);
3671 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003672 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003673 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003674 NestedLoopCount, CollapseLoopCountExpr,
3675 OrderedLoopCountExpr, VarsWithImplicitDSA,
3676 IterSpaces[Cnt]))
Alexey Bataevabfc0692014-06-25 06:52:00 +00003677 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003678 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003679 // OpenMP [2.8.1, simd construct, Restrictions]
3680 // All loops associated with the construct must be perfectly nested; that
3681 // is, there must be no intervening code nor any OpenMP directive between
3682 // any two loops.
3683 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003684 }
3685
Alexander Musmana5f070a2014-10-01 06:03:56 +00003686 Built.clear(/* size */ NestedLoopCount);
3687
3688 if (SemaRef.CurContext->isDependentContext())
3689 return NestedLoopCount;
3690
3691 // An example of what is generated for the following code:
3692 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00003693 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00003694 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003695 // for (k = 0; k < NK; ++k)
3696 // for (j = J0; j < NJ; j+=2) {
3697 // <loop body>
3698 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003699 //
3700 // We generate the code below.
3701 // Note: the loop body may be outlined in CodeGen.
3702 // Note: some counters may be C++ classes, operator- is used to find number of
3703 // iterations and operator+= to calculate counter value.
3704 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
3705 // or i64 is currently supported).
3706 //
3707 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
3708 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
3709 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
3710 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
3711 // // similar updates for vars in clauses (e.g. 'linear')
3712 // <loop body (using local i and j)>
3713 // }
3714 // i = NI; // assign final values of counters
3715 // j = NJ;
3716 //
3717
3718 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
3719 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00003720 // Precondition tests if there is at least one iteration (all conditions are
3721 // true).
3722 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003723 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003724 ExprResult LastIteration32 = WidenIterationCount(
3725 32 /* Bits */, SemaRef.PerformImplicitConversion(
3726 N0->IgnoreImpCasts(), N0->getType(),
3727 Sema::AA_Converting, /*AllowExplicit=*/true)
3728 .get(),
3729 SemaRef);
3730 ExprResult LastIteration64 = WidenIterationCount(
3731 64 /* Bits */, SemaRef.PerformImplicitConversion(
3732 N0->IgnoreImpCasts(), N0->getType(),
3733 Sema::AA_Converting, /*AllowExplicit=*/true)
3734 .get(),
3735 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003736
3737 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
3738 return NestedLoopCount;
3739
3740 auto &C = SemaRef.Context;
3741 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
3742
3743 Scope *CurScope = DSA.getCurScope();
3744 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003745 if (PreCond.isUsable()) {
3746 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
3747 PreCond.get(), IterSpaces[Cnt].PreCond);
3748 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003749 auto N = IterSpaces[Cnt].NumIterations;
3750 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
3751 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003752 LastIteration32 = SemaRef.BuildBinOp(
3753 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
3754 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
3755 Sema::AA_Converting,
3756 /*AllowExplicit=*/true)
3757 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003758 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003759 LastIteration64 = SemaRef.BuildBinOp(
3760 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
3761 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
3762 Sema::AA_Converting,
3763 /*AllowExplicit=*/true)
3764 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003765 }
3766
3767 // Choose either the 32-bit or 64-bit version.
3768 ExprResult LastIteration = LastIteration64;
3769 if (LastIteration32.isUsable() &&
3770 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
3771 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
3772 FitsInto(
3773 32 /* Bits */,
3774 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
3775 LastIteration64.get(), SemaRef)))
3776 LastIteration = LastIteration32;
3777
3778 if (!LastIteration.isUsable())
3779 return 0;
3780
3781 // Save the number of iterations.
3782 ExprResult NumIterations = LastIteration;
3783 {
3784 LastIteration = SemaRef.BuildBinOp(
3785 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
3786 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3787 if (!LastIteration.isUsable())
3788 return 0;
3789 }
3790
3791 // Calculate the last iteration number beforehand instead of doing this on
3792 // each iteration. Do not do this if the number of iterations may be kfold-ed.
3793 llvm::APSInt Result;
3794 bool IsConstant =
3795 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
3796 ExprResult CalcLastIteration;
3797 if (!IsConstant) {
3798 SourceLocation SaveLoc;
3799 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00003800 buildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00003801 ".omp.last.iteration");
Alexey Bataev39f915b82015-05-08 10:41:21 +00003802 ExprResult SaveRef = buildDeclRefExpr(
3803 SemaRef, SaveVar, LastIteration.get()->getType(), SaveLoc);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003804 CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign,
3805 SaveRef.get(), LastIteration.get());
3806 LastIteration = SaveRef;
3807
3808 // Prepare SaveRef + 1.
3809 NumIterations = SemaRef.BuildBinOp(
3810 CurScope, SaveLoc, BO_Add, SaveRef.get(),
3811 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3812 if (!NumIterations.isUsable())
3813 return 0;
3814 }
3815
3816 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
3817
Alexander Musmanc6388682014-12-15 07:07:06 +00003818 QualType VType = LastIteration.get()->getType();
3819 // Build variables passed into runtime, nesessary for worksharing directives.
3820 ExprResult LB, UB, IL, ST, EUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003821 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
3822 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00003823 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00003824 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
3825 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003826 SemaRef.AddInitializerToDecl(
3827 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
3828 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3829
3830 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00003831 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
3832 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003833 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
3834 /*DirectInit*/ false,
3835 /*TypeMayContainAuto*/ false);
3836
3837 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
3838 // This will be used to implement clause 'lastprivate'.
3839 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00003840 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
3841 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003842 SemaRef.AddInitializerToDecl(
3843 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
3844 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3845
3846 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev39f915b82015-05-08 10:41:21 +00003847 VarDecl *STDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.stride");
3848 ST = buildDeclRefExpr(SemaRef, STDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003849 SemaRef.AddInitializerToDecl(
3850 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
3851 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3852
3853 // Build expression: UB = min(UB, LastIteration)
3854 // It is nesessary for CodeGen of directives with static scheduling.
3855 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
3856 UB.get(), LastIteration.get());
3857 ExprResult CondOp = SemaRef.ActOnConditionalOp(
3858 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
3859 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
3860 CondOp.get());
3861 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
3862 }
3863
3864 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003865 ExprResult IV;
3866 ExprResult Init;
3867 {
Alexey Bataev39f915b82015-05-08 10:41:21 +00003868 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.iv");
3869 IV = buildDeclRefExpr(SemaRef, IVDecl, VType, InitLoc);
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003870 Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003871 isOpenMPTaskLoopDirective(DKind) ||
3872 isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00003873 ? LB.get()
3874 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
3875 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
3876 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003877 }
3878
Alexander Musmanc6388682014-12-15 07:07:06 +00003879 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003880 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00003881 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003882 (isOpenMPWorksharingDirective(DKind) ||
3883 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00003884 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
3885 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
3886 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003887
3888 // Loop increment (IV = IV + 1)
3889 SourceLocation IncLoc;
3890 ExprResult Inc =
3891 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
3892 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
3893 if (!Inc.isUsable())
3894 return 0;
3895 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00003896 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
3897 if (!Inc.isUsable())
3898 return 0;
3899
3900 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
3901 // Used for directives with static scheduling.
3902 ExprResult NextLB, NextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003903 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
3904 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00003905 // LB + ST
3906 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
3907 if (!NextLB.isUsable())
3908 return 0;
3909 // LB = LB + ST
3910 NextLB =
3911 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
3912 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
3913 if (!NextLB.isUsable())
3914 return 0;
3915 // UB + ST
3916 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
3917 if (!NextUB.isUsable())
3918 return 0;
3919 // UB = UB + ST
3920 NextUB =
3921 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
3922 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
3923 if (!NextUB.isUsable())
3924 return 0;
3925 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003926
3927 // Build updates and final values of the loop counters.
3928 bool HasErrors = false;
3929 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003930 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003931 Built.Updates.resize(NestedLoopCount);
3932 Built.Finals.resize(NestedLoopCount);
3933 {
3934 ExprResult Div;
3935 // Go from inner nested loop to outer.
3936 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
3937 LoopIterationSpace &IS = IterSpaces[Cnt];
3938 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
3939 // Build: Iter = (IV / Div) % IS.NumIters
3940 // where Div is product of previous iterations' IS.NumIters.
3941 ExprResult Iter;
3942 if (Div.isUsable()) {
3943 Iter =
3944 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
3945 } else {
3946 Iter = IV;
3947 assert((Cnt == (int)NestedLoopCount - 1) &&
3948 "unusable div expected on first iteration only");
3949 }
3950
3951 if (Cnt != 0 && Iter.isUsable())
3952 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
3953 IS.NumIterations);
3954 if (!Iter.isUsable()) {
3955 HasErrors = true;
3956 break;
3957 }
3958
Alexey Bataev39f915b82015-05-08 10:41:21 +00003959 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
3960 auto *CounterVar = buildDeclRefExpr(
3961 SemaRef, cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()),
3962 IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
3963 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003964 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
3965 IS.CounterInit);
3966 if (!Init.isUsable()) {
3967 HasErrors = true;
3968 break;
3969 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003970 ExprResult Update =
Alexey Bataev39f915b82015-05-08 10:41:21 +00003971 BuildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003972 IS.CounterInit, Iter, IS.CounterStep, IS.Subtract);
3973 if (!Update.isUsable()) {
3974 HasErrors = true;
3975 break;
3976 }
3977
3978 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
3979 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00003980 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003981 IS.NumIterations, IS.CounterStep, IS.Subtract);
3982 if (!Final.isUsable()) {
3983 HasErrors = true;
3984 break;
3985 }
3986
3987 // Build Div for the next iteration: Div <- Div * IS.NumIters
3988 if (Cnt != 0) {
3989 if (Div.isUnset())
3990 Div = IS.NumIterations;
3991 else
3992 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
3993 IS.NumIterations);
3994
3995 // Add parentheses (for debugging purposes only).
3996 if (Div.isUsable())
3997 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
3998 if (!Div.isUsable()) {
3999 HasErrors = true;
4000 break;
4001 }
4002 }
4003 if (!Update.isUsable() || !Final.isUsable()) {
4004 HasErrors = true;
4005 break;
4006 }
4007 // Save results
4008 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00004009 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004010 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004011 Built.Updates[Cnt] = Update.get();
4012 Built.Finals[Cnt] = Final.get();
4013 }
4014 }
4015
4016 if (HasErrors)
4017 return 0;
4018
4019 // Save results
4020 Built.IterationVarRef = IV.get();
4021 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00004022 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004023 Built.CalcLastIteration =
4024 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004025 Built.PreCond = PreCond.get();
4026 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004027 Built.Init = Init.get();
4028 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00004029 Built.LB = LB.get();
4030 Built.UB = UB.get();
4031 Built.IL = IL.get();
4032 Built.ST = ST.get();
4033 Built.EUB = EUB.get();
4034 Built.NLB = NextLB.get();
4035 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004036
Alexey Bataevabfc0692014-06-25 06:52:00 +00004037 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004038}
4039
Alexey Bataev10e775f2015-07-30 11:36:16 +00004040static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004041 auto CollapseClauses =
4042 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
4043 if (CollapseClauses.begin() != CollapseClauses.end())
4044 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004045 return nullptr;
4046}
4047
Alexey Bataev10e775f2015-07-30 11:36:16 +00004048static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004049 auto OrderedClauses =
4050 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
4051 if (OrderedClauses.begin() != OrderedClauses.end())
4052 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004053 return nullptr;
4054}
4055
Alexey Bataev66b15b52015-08-21 11:14:16 +00004056static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen,
4057 const Expr *Safelen) {
4058 llvm::APSInt SimdlenRes, SafelenRes;
4059 if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() ||
4060 Simdlen->isInstantiationDependent() ||
4061 Simdlen->containsUnexpandedParameterPack())
4062 return false;
4063 if (Safelen->isValueDependent() || Safelen->isTypeDependent() ||
4064 Safelen->isInstantiationDependent() ||
4065 Safelen->containsUnexpandedParameterPack())
4066 return false;
4067 Simdlen->EvaluateAsInt(SimdlenRes, S.Context);
4068 Safelen->EvaluateAsInt(SafelenRes, S.Context);
4069 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4070 // If both simdlen and safelen clauses are specified, the value of the simdlen
4071 // parameter must be less than or equal to the value of the safelen parameter.
4072 if (SimdlenRes > SafelenRes) {
4073 S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values)
4074 << Simdlen->getSourceRange() << Safelen->getSourceRange();
4075 return true;
4076 }
4077 return false;
4078}
4079
Alexey Bataev4acb8592014-07-07 13:01:15 +00004080StmtResult Sema::ActOnOpenMPSimdDirective(
4081 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4082 SourceLocation EndLoc,
4083 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004084 if (!AStmt)
4085 return StmtError();
4086
4087 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004088 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004089 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4090 // define the nested loops number.
4091 unsigned NestedLoopCount = CheckOpenMPLoop(
4092 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4093 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004094 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004095 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004096
Alexander Musmana5f070a2014-10-01 06:03:56 +00004097 assert((CurContext->isDependentContext() || B.builtAll()) &&
4098 "omp simd loop exprs were not built");
4099
Alexander Musman3276a272015-03-21 10:12:56 +00004100 if (!CurContext->isDependentContext()) {
4101 // Finalize the clauses that need pre-built expressions for CodeGen.
4102 for (auto C : Clauses) {
4103 if (auto LC = dyn_cast<OMPLinearClause>(C))
4104 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4105 B.NumIterations, *this, CurScope))
4106 return StmtError();
4107 }
4108 }
4109
Alexey Bataev66b15b52015-08-21 11:14:16 +00004110 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4111 // If both simdlen and safelen clauses are specified, the value of the simdlen
4112 // parameter must be less than or equal to the value of the safelen parameter.
4113 OMPSafelenClause *Safelen = nullptr;
4114 OMPSimdlenClause *Simdlen = nullptr;
4115 for (auto *Clause : Clauses) {
4116 if (Clause->getClauseKind() == OMPC_safelen)
4117 Safelen = cast<OMPSafelenClause>(Clause);
4118 else if (Clause->getClauseKind() == OMPC_simdlen)
4119 Simdlen = cast<OMPSimdlenClause>(Clause);
4120 if (Safelen && Simdlen)
4121 break;
4122 }
4123 if (Simdlen && Safelen &&
4124 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4125 Safelen->getSafelen()))
4126 return StmtError();
4127
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004128 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004129 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4130 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004131}
4132
Alexey Bataev4acb8592014-07-07 13:01:15 +00004133StmtResult Sema::ActOnOpenMPForDirective(
4134 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4135 SourceLocation EndLoc,
4136 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004137 if (!AStmt)
4138 return StmtError();
4139
4140 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004141 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004142 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4143 // define the nested loops number.
4144 unsigned NestedLoopCount = CheckOpenMPLoop(
4145 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4146 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004147 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00004148 return StmtError();
4149
Alexander Musmana5f070a2014-10-01 06:03:56 +00004150 assert((CurContext->isDependentContext() || B.builtAll()) &&
4151 "omp for loop exprs were not built");
4152
Alexey Bataev54acd402015-08-04 11:18:19 +00004153 if (!CurContext->isDependentContext()) {
4154 // Finalize the clauses that need pre-built expressions for CodeGen.
4155 for (auto C : Clauses) {
4156 if (auto LC = dyn_cast<OMPLinearClause>(C))
4157 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4158 B.NumIterations, *this, CurScope))
4159 return StmtError();
4160 }
4161 }
4162
Alexey Bataevf29276e2014-06-18 04:14:57 +00004163 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004164 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004165 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00004166}
4167
Alexander Musmanf82886e2014-09-18 05:12:34 +00004168StmtResult Sema::ActOnOpenMPForSimdDirective(
4169 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4170 SourceLocation EndLoc,
4171 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004172 if (!AStmt)
4173 return StmtError();
4174
4175 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004176 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004177 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4178 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00004179 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004180 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
4181 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4182 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004183 if (NestedLoopCount == 0)
4184 return StmtError();
4185
Alexander Musmanc6388682014-12-15 07:07:06 +00004186 assert((CurContext->isDependentContext() || B.builtAll()) &&
4187 "omp for simd loop exprs were not built");
4188
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00004189 if (!CurContext->isDependentContext()) {
4190 // Finalize the clauses that need pre-built expressions for CodeGen.
4191 for (auto C : Clauses) {
4192 if (auto LC = dyn_cast<OMPLinearClause>(C))
4193 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4194 B.NumIterations, *this, CurScope))
4195 return StmtError();
4196 }
4197 }
4198
Alexey Bataev66b15b52015-08-21 11:14:16 +00004199 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4200 // If both simdlen and safelen clauses are specified, the value of the simdlen
4201 // parameter must be less than or equal to the value of the safelen parameter.
4202 OMPSafelenClause *Safelen = nullptr;
4203 OMPSimdlenClause *Simdlen = nullptr;
4204 for (auto *Clause : Clauses) {
4205 if (Clause->getClauseKind() == OMPC_safelen)
4206 Safelen = cast<OMPSafelenClause>(Clause);
4207 else if (Clause->getClauseKind() == OMPC_simdlen)
4208 Simdlen = cast<OMPSimdlenClause>(Clause);
4209 if (Safelen && Simdlen)
4210 break;
4211 }
4212 if (Simdlen && Safelen &&
4213 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4214 Safelen->getSafelen()))
4215 return StmtError();
4216
Alexander Musmanf82886e2014-09-18 05:12:34 +00004217 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004218 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4219 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004220}
4221
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004222StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
4223 Stmt *AStmt,
4224 SourceLocation StartLoc,
4225 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004226 if (!AStmt)
4227 return StmtError();
4228
4229 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004230 auto BaseStmt = AStmt;
4231 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4232 BaseStmt = CS->getCapturedStmt();
4233 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4234 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004235 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004236 return StmtError();
4237 // All associated statements must be '#pragma omp section' except for
4238 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004239 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004240 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4241 if (SectionStmt)
4242 Diag(SectionStmt->getLocStart(),
4243 diag::err_omp_sections_substmt_not_section);
4244 return StmtError();
4245 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004246 cast<OMPSectionDirective>(SectionStmt)
4247 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004248 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004249 } else {
4250 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
4251 return StmtError();
4252 }
4253
4254 getCurFunction()->setHasBranchProtectedScope();
4255
Alexey Bataev25e5b442015-09-15 12:52:43 +00004256 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4257 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004258}
4259
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004260StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
4261 SourceLocation StartLoc,
4262 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004263 if (!AStmt)
4264 return StmtError();
4265
4266 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004267
4268 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00004269 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004270
Alexey Bataev25e5b442015-09-15 12:52:43 +00004271 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
4272 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004273}
4274
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004275StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
4276 Stmt *AStmt,
4277 SourceLocation StartLoc,
4278 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004279 if (!AStmt)
4280 return StmtError();
4281
4282 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00004283
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004284 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00004285
Alexey Bataev3255bf32015-01-19 05:20:46 +00004286 // OpenMP [2.7.3, single Construct, Restrictions]
4287 // The copyprivate clause must not be used with the nowait clause.
4288 OMPClause *Nowait = nullptr;
4289 OMPClause *Copyprivate = nullptr;
4290 for (auto *Clause : Clauses) {
4291 if (Clause->getClauseKind() == OMPC_nowait)
4292 Nowait = Clause;
4293 else if (Clause->getClauseKind() == OMPC_copyprivate)
4294 Copyprivate = Clause;
4295 if (Copyprivate && Nowait) {
4296 Diag(Copyprivate->getLocStart(),
4297 diag::err_omp_single_copyprivate_with_nowait);
4298 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
4299 return StmtError();
4300 }
4301 }
4302
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004303 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4304}
4305
Alexander Musman80c22892014-07-17 08:54:58 +00004306StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
4307 SourceLocation StartLoc,
4308 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004309 if (!AStmt)
4310 return StmtError();
4311
4312 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00004313
4314 getCurFunction()->setHasBranchProtectedScope();
4315
4316 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
4317}
4318
Alexey Bataev28c75412015-12-15 08:19:24 +00004319StmtResult Sema::ActOnOpenMPCriticalDirective(
4320 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
4321 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004322 if (!AStmt)
4323 return StmtError();
4324
4325 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004326
Alexey Bataev28c75412015-12-15 08:19:24 +00004327 bool ErrorFound = false;
4328 llvm::APSInt Hint;
4329 SourceLocation HintLoc;
4330 bool DependentHint = false;
4331 for (auto *C : Clauses) {
4332 if (C->getClauseKind() == OMPC_hint) {
4333 if (!DirName.getName()) {
4334 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
4335 ErrorFound = true;
4336 }
4337 Expr *E = cast<OMPHintClause>(C)->getHint();
4338 if (E->isTypeDependent() || E->isValueDependent() ||
4339 E->isInstantiationDependent())
4340 DependentHint = true;
4341 else {
4342 Hint = E->EvaluateKnownConstInt(Context);
4343 HintLoc = C->getLocStart();
4344 }
4345 }
4346 }
4347 if (ErrorFound)
4348 return StmtError();
4349 auto Pair = DSAStack->getCriticalWithHint(DirName);
4350 if (Pair.first && DirName.getName() && !DependentHint) {
4351 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
4352 Diag(StartLoc, diag::err_omp_critical_with_hint);
4353 if (HintLoc.isValid()) {
4354 Diag(HintLoc, diag::note_omp_critical_hint_here)
4355 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
4356 } else
4357 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
4358 if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
4359 Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
4360 << 1
4361 << C->getHint()->EvaluateKnownConstInt(Context).toString(
4362 /*Radix=*/10, /*Signed=*/false);
4363 } else
4364 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
4365 }
4366 }
4367
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004368 getCurFunction()->setHasBranchProtectedScope();
4369
Alexey Bataev28c75412015-12-15 08:19:24 +00004370 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
4371 Clauses, AStmt);
4372 if (!Pair.first && DirName.getName() && !DependentHint)
4373 DSAStack->addCriticalWithHint(Dir, Hint);
4374 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004375}
4376
Alexey Bataev4acb8592014-07-07 13:01:15 +00004377StmtResult Sema::ActOnOpenMPParallelForDirective(
4378 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4379 SourceLocation EndLoc,
4380 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004381 if (!AStmt)
4382 return StmtError();
4383
Alexey Bataev4acb8592014-07-07 13:01:15 +00004384 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4385 // 1.2.2 OpenMP Language Terminology
4386 // Structured block - An executable statement with a single entry at the
4387 // top and a single exit at the bottom.
4388 // The point of exit cannot be a branch out of the structured block.
4389 // longjmp() and throw() must not violate the entry/exit criteria.
4390 CS->getCapturedDecl()->setNothrow();
4391
Alexander Musmanc6388682014-12-15 07:07:06 +00004392 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004393 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4394 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004395 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004396 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
4397 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4398 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00004399 if (NestedLoopCount == 0)
4400 return StmtError();
4401
Alexander Musmana5f070a2014-10-01 06:03:56 +00004402 assert((CurContext->isDependentContext() || B.builtAll()) &&
4403 "omp parallel for loop exprs were not built");
4404
Alexey Bataev54acd402015-08-04 11:18:19 +00004405 if (!CurContext->isDependentContext()) {
4406 // Finalize the clauses that need pre-built expressions for CodeGen.
4407 for (auto C : Clauses) {
4408 if (auto LC = dyn_cast<OMPLinearClause>(C))
4409 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4410 B.NumIterations, *this, CurScope))
4411 return StmtError();
4412 }
4413 }
4414
Alexey Bataev4acb8592014-07-07 13:01:15 +00004415 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004416 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004417 NestedLoopCount, Clauses, AStmt, B,
4418 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00004419}
4420
Alexander Musmane4e893b2014-09-23 09:33:00 +00004421StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
4422 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4423 SourceLocation EndLoc,
4424 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004425 if (!AStmt)
4426 return StmtError();
4427
Alexander Musmane4e893b2014-09-23 09:33:00 +00004428 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4429 // 1.2.2 OpenMP Language Terminology
4430 // Structured block - An executable statement with a single entry at the
4431 // top and a single exit at the bottom.
4432 // The point of exit cannot be a branch out of the structured block.
4433 // longjmp() and throw() must not violate the entry/exit criteria.
4434 CS->getCapturedDecl()->setNothrow();
4435
Alexander Musmanc6388682014-12-15 07:07:06 +00004436 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004437 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4438 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00004439 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004440 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
4441 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4442 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004443 if (NestedLoopCount == 0)
4444 return StmtError();
4445
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00004446 if (!CurContext->isDependentContext()) {
4447 // Finalize the clauses that need pre-built expressions for CodeGen.
4448 for (auto C : Clauses) {
4449 if (auto LC = dyn_cast<OMPLinearClause>(C))
4450 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4451 B.NumIterations, *this, CurScope))
4452 return StmtError();
4453 }
4454 }
4455
Alexey Bataev66b15b52015-08-21 11:14:16 +00004456 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4457 // If both simdlen and safelen clauses are specified, the value of the simdlen
4458 // parameter must be less than or equal to the value of the safelen parameter.
4459 OMPSafelenClause *Safelen = nullptr;
4460 OMPSimdlenClause *Simdlen = nullptr;
4461 for (auto *Clause : Clauses) {
4462 if (Clause->getClauseKind() == OMPC_safelen)
4463 Safelen = cast<OMPSafelenClause>(Clause);
4464 else if (Clause->getClauseKind() == OMPC_simdlen)
4465 Simdlen = cast<OMPSimdlenClause>(Clause);
4466 if (Safelen && Simdlen)
4467 break;
4468 }
4469 if (Simdlen && Safelen &&
4470 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4471 Safelen->getSafelen()))
4472 return StmtError();
4473
Alexander Musmane4e893b2014-09-23 09:33:00 +00004474 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004475 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00004476 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004477}
4478
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004479StmtResult
4480Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
4481 Stmt *AStmt, SourceLocation StartLoc,
4482 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004483 if (!AStmt)
4484 return StmtError();
4485
4486 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004487 auto BaseStmt = AStmt;
4488 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4489 BaseStmt = CS->getCapturedStmt();
4490 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4491 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004492 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004493 return StmtError();
4494 // All associated statements must be '#pragma omp section' except for
4495 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004496 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004497 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4498 if (SectionStmt)
4499 Diag(SectionStmt->getLocStart(),
4500 diag::err_omp_parallel_sections_substmt_not_section);
4501 return StmtError();
4502 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004503 cast<OMPSectionDirective>(SectionStmt)
4504 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004505 }
4506 } else {
4507 Diag(AStmt->getLocStart(),
4508 diag::err_omp_parallel_sections_not_compound_stmt);
4509 return StmtError();
4510 }
4511
4512 getCurFunction()->setHasBranchProtectedScope();
4513
Alexey Bataev25e5b442015-09-15 12:52:43 +00004514 return OMPParallelSectionsDirective::Create(
4515 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004516}
4517
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004518StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
4519 Stmt *AStmt, SourceLocation StartLoc,
4520 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004521 if (!AStmt)
4522 return StmtError();
4523
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004524 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4525 // 1.2.2 OpenMP Language Terminology
4526 // Structured block - An executable statement with a single entry at the
4527 // top and a single exit at the bottom.
4528 // The point of exit cannot be a branch out of the structured block.
4529 // longjmp() and throw() must not violate the entry/exit criteria.
4530 CS->getCapturedDecl()->setNothrow();
4531
4532 getCurFunction()->setHasBranchProtectedScope();
4533
Alexey Bataev25e5b442015-09-15 12:52:43 +00004534 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4535 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004536}
4537
Alexey Bataev68446b72014-07-18 07:47:19 +00004538StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
4539 SourceLocation EndLoc) {
4540 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
4541}
4542
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00004543StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
4544 SourceLocation EndLoc) {
4545 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
4546}
4547
Alexey Bataev2df347a2014-07-18 10:17:07 +00004548StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
4549 SourceLocation EndLoc) {
4550 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
4551}
4552
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00004553StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
4554 SourceLocation StartLoc,
4555 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004556 if (!AStmt)
4557 return StmtError();
4558
4559 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00004560
4561 getCurFunction()->setHasBranchProtectedScope();
4562
4563 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
4564}
4565
Alexey Bataev6125da92014-07-21 11:26:11 +00004566StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
4567 SourceLocation StartLoc,
4568 SourceLocation EndLoc) {
4569 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
4570 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
4571}
4572
Alexey Bataev346265e2015-09-25 10:37:12 +00004573StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
4574 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004575 SourceLocation StartLoc,
4576 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004577 if (!AStmt)
4578 return StmtError();
4579
4580 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004581
4582 getCurFunction()->setHasBranchProtectedScope();
4583
Alexey Bataev346265e2015-09-25 10:37:12 +00004584 OMPThreadsClause *TC = nullptr;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004585 OMPSIMDClause *SC = nullptr;
Alexey Bataev346265e2015-09-25 10:37:12 +00004586 for (auto *C: Clauses) {
4587 if (C->getClauseKind() == OMPC_threads)
4588 TC = cast<OMPThreadsClause>(C);
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004589 else if (C->getClauseKind() == OMPC_simd)
4590 SC = cast<OMPSIMDClause>(C);
Alexey Bataev346265e2015-09-25 10:37:12 +00004591 }
4592
4593 // TODO: this must happen only if 'threads' clause specified or if no clauses
4594 // is specified.
4595 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
4596 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
4597 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param) << (TC != nullptr);
4598 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
4599 return StmtError();
4600 }
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004601 if (!SC && isOpenMPSimdDirective(DSAStack->getParentDirective())) {
4602 // OpenMP [2.8.1,simd Construct, Restrictions]
4603 // An ordered construct with the simd clause is the only OpenMP construct
4604 // that can appear in the simd region.
4605 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
4606 return StmtError();
4607 }
Alexey Bataev346265e2015-09-25 10:37:12 +00004608
4609 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004610}
4611
Alexey Bataev1d160b12015-03-13 12:27:31 +00004612namespace {
4613/// \brief Helper class for checking expression in 'omp atomic [update]'
4614/// construct.
4615class OpenMPAtomicUpdateChecker {
4616 /// \brief Error results for atomic update expressions.
4617 enum ExprAnalysisErrorCode {
4618 /// \brief A statement is not an expression statement.
4619 NotAnExpression,
4620 /// \brief Expression is not builtin binary or unary operation.
4621 NotABinaryOrUnaryExpression,
4622 /// \brief Unary operation is not post-/pre- increment/decrement operation.
4623 NotAnUnaryIncDecExpression,
4624 /// \brief An expression is not of scalar type.
4625 NotAScalarType,
4626 /// \brief A binary operation is not an assignment operation.
4627 NotAnAssignmentOp,
4628 /// \brief RHS part of the binary operation is not a binary expression.
4629 NotABinaryExpression,
4630 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
4631 /// expression.
4632 NotABinaryOperator,
4633 /// \brief RHS binary operation does not have reference to the updated LHS
4634 /// part.
4635 NotAnUpdateExpression,
4636 /// \brief No errors is found.
4637 NoError
4638 };
4639 /// \brief Reference to Sema.
4640 Sema &SemaRef;
4641 /// \brief A location for note diagnostics (when error is found).
4642 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004643 /// \brief 'x' lvalue part of the source atomic expression.
4644 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004645 /// \brief 'expr' rvalue part of the source atomic expression.
4646 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004647 /// \brief Helper expression of the form
4648 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
4649 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
4650 Expr *UpdateExpr;
4651 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
4652 /// important for non-associative operations.
4653 bool IsXLHSInRHSPart;
4654 BinaryOperatorKind Op;
4655 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004656 /// \brief true if the source expression is a postfix unary operation, false
4657 /// if it is a prefix unary operation.
4658 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004659
4660public:
4661 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00004662 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00004663 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00004664 /// \brief Check specified statement that it is suitable for 'atomic update'
4665 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00004666 /// expression. If DiagId and NoteId == 0, then only check is performed
4667 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00004668 /// \param DiagId Diagnostic which should be emitted if error is found.
4669 /// \param NoteId Diagnostic note for the main error message.
4670 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00004671 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00004672 /// \brief Return the 'x' lvalue part of the source atomic expression.
4673 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00004674 /// \brief Return the 'expr' rvalue part of the source atomic expression.
4675 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00004676 /// \brief Return the update expression used in calculation of the updated
4677 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
4678 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
4679 Expr *getUpdateExpr() const { return UpdateExpr; }
4680 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
4681 /// false otherwise.
4682 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
4683
Alexey Bataevb78ca832015-04-01 03:33:17 +00004684 /// \brief true if the source expression is a postfix unary operation, false
4685 /// if it is a prefix unary operation.
4686 bool isPostfixUpdate() const { return IsPostfixUpdate; }
4687
Alexey Bataev1d160b12015-03-13 12:27:31 +00004688private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00004689 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
4690 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00004691};
4692} // namespace
4693
4694bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
4695 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
4696 ExprAnalysisErrorCode ErrorFound = NoError;
4697 SourceLocation ErrorLoc, NoteLoc;
4698 SourceRange ErrorRange, NoteRange;
4699 // Allowed constructs are:
4700 // x = x binop expr;
4701 // x = expr binop x;
4702 if (AtomicBinOp->getOpcode() == BO_Assign) {
4703 X = AtomicBinOp->getLHS();
4704 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
4705 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
4706 if (AtomicInnerBinOp->isMultiplicativeOp() ||
4707 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
4708 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00004709 Op = AtomicInnerBinOp->getOpcode();
4710 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00004711 auto *LHS = AtomicInnerBinOp->getLHS();
4712 auto *RHS = AtomicInnerBinOp->getRHS();
4713 llvm::FoldingSetNodeID XId, LHSId, RHSId;
4714 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
4715 /*Canonical=*/true);
4716 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
4717 /*Canonical=*/true);
4718 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
4719 /*Canonical=*/true);
4720 if (XId == LHSId) {
4721 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004722 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004723 } else if (XId == RHSId) {
4724 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004725 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004726 } else {
4727 ErrorLoc = AtomicInnerBinOp->getExprLoc();
4728 ErrorRange = AtomicInnerBinOp->getSourceRange();
4729 NoteLoc = X->getExprLoc();
4730 NoteRange = X->getSourceRange();
4731 ErrorFound = NotAnUpdateExpression;
4732 }
4733 } else {
4734 ErrorLoc = AtomicInnerBinOp->getExprLoc();
4735 ErrorRange = AtomicInnerBinOp->getSourceRange();
4736 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
4737 NoteRange = SourceRange(NoteLoc, NoteLoc);
4738 ErrorFound = NotABinaryOperator;
4739 }
4740 } else {
4741 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
4742 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
4743 ErrorFound = NotABinaryExpression;
4744 }
4745 } else {
4746 ErrorLoc = AtomicBinOp->getExprLoc();
4747 ErrorRange = AtomicBinOp->getSourceRange();
4748 NoteLoc = AtomicBinOp->getOperatorLoc();
4749 NoteRange = SourceRange(NoteLoc, NoteLoc);
4750 ErrorFound = NotAnAssignmentOp;
4751 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00004752 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004753 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
4754 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
4755 return true;
4756 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00004757 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00004758 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004759}
4760
4761bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
4762 unsigned NoteId) {
4763 ExprAnalysisErrorCode ErrorFound = NoError;
4764 SourceLocation ErrorLoc, NoteLoc;
4765 SourceRange ErrorRange, NoteRange;
4766 // Allowed constructs are:
4767 // x++;
4768 // x--;
4769 // ++x;
4770 // --x;
4771 // x binop= expr;
4772 // x = x binop expr;
4773 // x = expr binop x;
4774 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
4775 AtomicBody = AtomicBody->IgnoreParenImpCasts();
4776 if (AtomicBody->getType()->isScalarType() ||
4777 AtomicBody->isInstantiationDependent()) {
4778 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
4779 AtomicBody->IgnoreParenImpCasts())) {
4780 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00004781 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00004782 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00004783 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00004784 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00004785 X = AtomicCompAssignOp->getLHS();
4786 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004787 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
4788 AtomicBody->IgnoreParenImpCasts())) {
4789 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00004790 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
4791 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004792 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00004793 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
4794 // Check for Unary Operation
4795 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004796 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00004797 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
4798 OpLoc = AtomicUnaryOp->getOperatorLoc();
4799 X = AtomicUnaryOp->getSubExpr();
4800 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
4801 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004802 } else {
4803 ErrorFound = NotAnUnaryIncDecExpression;
4804 ErrorLoc = AtomicUnaryOp->getExprLoc();
4805 ErrorRange = AtomicUnaryOp->getSourceRange();
4806 NoteLoc = AtomicUnaryOp->getOperatorLoc();
4807 NoteRange = SourceRange(NoteLoc, NoteLoc);
4808 }
Alexey Bataev5a195472015-09-04 12:55:50 +00004809 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004810 ErrorFound = NotABinaryOrUnaryExpression;
4811 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
4812 NoteRange = ErrorRange = AtomicBody->getSourceRange();
4813 }
4814 } else {
4815 ErrorFound = NotAScalarType;
4816 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
4817 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
4818 }
4819 } else {
4820 ErrorFound = NotAnExpression;
4821 NoteLoc = ErrorLoc = S->getLocStart();
4822 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
4823 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00004824 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004825 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
4826 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
4827 return true;
4828 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00004829 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00004830 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00004831 // Build an update expression of form 'OpaqueValueExpr(x) binop
4832 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
4833 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
4834 auto *OVEX = new (SemaRef.getASTContext())
4835 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
4836 auto *OVEExpr = new (SemaRef.getASTContext())
4837 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
4838 auto Update =
4839 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
4840 IsXLHSInRHSPart ? OVEExpr : OVEX);
4841 if (Update.isInvalid())
4842 return true;
4843 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
4844 Sema::AA_Casting);
4845 if (Update.isInvalid())
4846 return true;
4847 UpdateExpr = Update.get();
4848 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00004849 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004850}
4851
Alexey Bataev0162e452014-07-22 10:10:35 +00004852StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
4853 Stmt *AStmt,
4854 SourceLocation StartLoc,
4855 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004856 if (!AStmt)
4857 return StmtError();
4858
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004859 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00004860 // 1.2.2 OpenMP Language Terminology
4861 // Structured block - An executable statement with a single entry at the
4862 // top and a single exit at the bottom.
4863 // The point of exit cannot be a branch out of the structured block.
4864 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00004865 OpenMPClauseKind AtomicKind = OMPC_unknown;
4866 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004867 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00004868 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00004869 C->getClauseKind() == OMPC_update ||
4870 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00004871 if (AtomicKind != OMPC_unknown) {
4872 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
4873 << SourceRange(C->getLocStart(), C->getLocEnd());
4874 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
4875 << getOpenMPClauseName(AtomicKind);
4876 } else {
4877 AtomicKind = C->getClauseKind();
4878 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004879 }
4880 }
4881 }
Alexey Bataev62cec442014-11-18 10:14:22 +00004882
Alexey Bataev459dec02014-07-24 06:46:57 +00004883 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00004884 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
4885 Body = EWC->getSubExpr();
4886
Alexey Bataev62cec442014-11-18 10:14:22 +00004887 Expr *X = nullptr;
4888 Expr *V = nullptr;
4889 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004890 Expr *UE = nullptr;
4891 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004892 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00004893 // OpenMP [2.12.6, atomic Construct]
4894 // In the next expressions:
4895 // * x and v (as applicable) are both l-value expressions with scalar type.
4896 // * During the execution of an atomic region, multiple syntactic
4897 // occurrences of x must designate the same storage location.
4898 // * Neither of v and expr (as applicable) may access the storage location
4899 // designated by x.
4900 // * Neither of x and expr (as applicable) may access the storage location
4901 // designated by v.
4902 // * expr is an expression with scalar type.
4903 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
4904 // * binop, binop=, ++, and -- are not overloaded operators.
4905 // * The expression x binop expr must be numerically equivalent to x binop
4906 // (expr). This requirement is satisfied if the operators in expr have
4907 // precedence greater than binop, or by using parentheses around expr or
4908 // subexpressions of expr.
4909 // * The expression expr binop x must be numerically equivalent to (expr)
4910 // binop x. This requirement is satisfied if the operators in expr have
4911 // precedence equal to or greater than binop, or by using parentheses around
4912 // expr or subexpressions of expr.
4913 // * For forms that allow multiple occurrences of x, the number of times
4914 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00004915 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004916 enum {
4917 NotAnExpression,
4918 NotAnAssignmentOp,
4919 NotAScalarType,
4920 NotAnLValue,
4921 NoError
4922 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00004923 SourceLocation ErrorLoc, NoteLoc;
4924 SourceRange ErrorRange, NoteRange;
4925 // If clause is read:
4926 // v = x;
4927 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
4928 auto AtomicBinOp =
4929 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
4930 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
4931 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
4932 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
4933 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
4934 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
4935 if (!X->isLValue() || !V->isLValue()) {
4936 auto NotLValueExpr = X->isLValue() ? V : X;
4937 ErrorFound = NotAnLValue;
4938 ErrorLoc = AtomicBinOp->getExprLoc();
4939 ErrorRange = AtomicBinOp->getSourceRange();
4940 NoteLoc = NotLValueExpr->getExprLoc();
4941 NoteRange = NotLValueExpr->getSourceRange();
4942 }
4943 } else if (!X->isInstantiationDependent() ||
4944 !V->isInstantiationDependent()) {
4945 auto NotScalarExpr =
4946 (X->isInstantiationDependent() || X->getType()->isScalarType())
4947 ? V
4948 : X;
4949 ErrorFound = NotAScalarType;
4950 ErrorLoc = AtomicBinOp->getExprLoc();
4951 ErrorRange = AtomicBinOp->getSourceRange();
4952 NoteLoc = NotScalarExpr->getExprLoc();
4953 NoteRange = NotScalarExpr->getSourceRange();
4954 }
Alexey Bataev5a195472015-09-04 12:55:50 +00004955 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00004956 ErrorFound = NotAnAssignmentOp;
4957 ErrorLoc = AtomicBody->getExprLoc();
4958 ErrorRange = AtomicBody->getSourceRange();
4959 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
4960 : AtomicBody->getExprLoc();
4961 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
4962 : AtomicBody->getSourceRange();
4963 }
4964 } else {
4965 ErrorFound = NotAnExpression;
4966 NoteLoc = ErrorLoc = Body->getLocStart();
4967 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00004968 }
Alexey Bataev62cec442014-11-18 10:14:22 +00004969 if (ErrorFound != NoError) {
4970 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
4971 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00004972 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
4973 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00004974 return StmtError();
4975 } else if (CurContext->isDependentContext())
4976 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00004977 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004978 enum {
4979 NotAnExpression,
4980 NotAnAssignmentOp,
4981 NotAScalarType,
4982 NotAnLValue,
4983 NoError
4984 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00004985 SourceLocation ErrorLoc, NoteLoc;
4986 SourceRange ErrorRange, NoteRange;
4987 // If clause is write:
4988 // x = expr;
4989 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
4990 auto AtomicBinOp =
4991 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
4992 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00004993 X = AtomicBinOp->getLHS();
4994 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00004995 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
4996 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
4997 if (!X->isLValue()) {
4998 ErrorFound = NotAnLValue;
4999 ErrorLoc = AtomicBinOp->getExprLoc();
5000 ErrorRange = AtomicBinOp->getSourceRange();
5001 NoteLoc = X->getExprLoc();
5002 NoteRange = X->getSourceRange();
5003 }
5004 } else if (!X->isInstantiationDependent() ||
5005 !E->isInstantiationDependent()) {
5006 auto NotScalarExpr =
5007 (X->isInstantiationDependent() || X->getType()->isScalarType())
5008 ? E
5009 : X;
5010 ErrorFound = NotAScalarType;
5011 ErrorLoc = AtomicBinOp->getExprLoc();
5012 ErrorRange = AtomicBinOp->getSourceRange();
5013 NoteLoc = NotScalarExpr->getExprLoc();
5014 NoteRange = NotScalarExpr->getSourceRange();
5015 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005016 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00005017 ErrorFound = NotAnAssignmentOp;
5018 ErrorLoc = AtomicBody->getExprLoc();
5019 ErrorRange = AtomicBody->getSourceRange();
5020 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5021 : AtomicBody->getExprLoc();
5022 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5023 : AtomicBody->getSourceRange();
5024 }
5025 } else {
5026 ErrorFound = NotAnExpression;
5027 NoteLoc = ErrorLoc = Body->getLocStart();
5028 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005029 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00005030 if (ErrorFound != NoError) {
5031 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
5032 << ErrorRange;
5033 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5034 << NoteRange;
5035 return StmtError();
5036 } else if (CurContext->isDependentContext())
5037 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00005038 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005039 // If clause is update:
5040 // x++;
5041 // x--;
5042 // ++x;
5043 // --x;
5044 // x binop= expr;
5045 // x = x binop expr;
5046 // x = expr binop x;
5047 OpenMPAtomicUpdateChecker Checker(*this);
5048 if (Checker.checkStatement(
5049 Body, (AtomicKind == OMPC_update)
5050 ? diag::err_omp_atomic_update_not_expression_statement
5051 : diag::err_omp_atomic_not_expression_statement,
5052 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00005053 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005054 if (!CurContext->isDependentContext()) {
5055 E = Checker.getExpr();
5056 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005057 UE = Checker.getUpdateExpr();
5058 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00005059 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005060 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005061 enum {
5062 NotAnAssignmentOp,
5063 NotACompoundStatement,
5064 NotTwoSubstatements,
5065 NotASpecificExpression,
5066 NoError
5067 } ErrorFound = NoError;
5068 SourceLocation ErrorLoc, NoteLoc;
5069 SourceRange ErrorRange, NoteRange;
5070 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
5071 // If clause is a capture:
5072 // v = x++;
5073 // v = x--;
5074 // v = ++x;
5075 // v = --x;
5076 // v = x binop= expr;
5077 // v = x = x binop expr;
5078 // v = x = expr binop x;
5079 auto *AtomicBinOp =
5080 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5081 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5082 V = AtomicBinOp->getLHS();
5083 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5084 OpenMPAtomicUpdateChecker Checker(*this);
5085 if (Checker.checkStatement(
5086 Body, diag::err_omp_atomic_capture_not_expression_statement,
5087 diag::note_omp_atomic_update))
5088 return StmtError();
5089 E = Checker.getExpr();
5090 X = Checker.getX();
5091 UE = Checker.getUpdateExpr();
5092 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
5093 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00005094 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005095 ErrorLoc = AtomicBody->getExprLoc();
5096 ErrorRange = AtomicBody->getSourceRange();
5097 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5098 : AtomicBody->getExprLoc();
5099 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5100 : AtomicBody->getSourceRange();
5101 ErrorFound = NotAnAssignmentOp;
5102 }
5103 if (ErrorFound != NoError) {
5104 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
5105 << ErrorRange;
5106 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5107 return StmtError();
5108 } else if (CurContext->isDependentContext()) {
5109 UE = V = E = X = nullptr;
5110 }
5111 } else {
5112 // If clause is a capture:
5113 // { v = x; x = expr; }
5114 // { v = x; x++; }
5115 // { v = x; x--; }
5116 // { v = x; ++x; }
5117 // { v = x; --x; }
5118 // { v = x; x binop= expr; }
5119 // { v = x; x = x binop expr; }
5120 // { v = x; x = expr binop x; }
5121 // { x++; v = x; }
5122 // { x--; v = x; }
5123 // { ++x; v = x; }
5124 // { --x; v = x; }
5125 // { x binop= expr; v = x; }
5126 // { x = x binop expr; v = x; }
5127 // { x = expr binop x; v = x; }
5128 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
5129 // Check that this is { expr1; expr2; }
5130 if (CS->size() == 2) {
5131 auto *First = CS->body_front();
5132 auto *Second = CS->body_back();
5133 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
5134 First = EWC->getSubExpr()->IgnoreParenImpCasts();
5135 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
5136 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
5137 // Need to find what subexpression is 'v' and what is 'x'.
5138 OpenMPAtomicUpdateChecker Checker(*this);
5139 bool IsUpdateExprFound = !Checker.checkStatement(Second);
5140 BinaryOperator *BinOp = nullptr;
5141 if (IsUpdateExprFound) {
5142 BinOp = dyn_cast<BinaryOperator>(First);
5143 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5144 }
5145 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5146 // { v = x; x++; }
5147 // { v = x; x--; }
5148 // { v = x; ++x; }
5149 // { v = x; --x; }
5150 // { v = x; x binop= expr; }
5151 // { v = x; x = x binop expr; }
5152 // { v = x; x = expr binop x; }
5153 // Check that the first expression has form v = x.
5154 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5155 llvm::FoldingSetNodeID XId, PossibleXId;
5156 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5157 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5158 IsUpdateExprFound = XId == PossibleXId;
5159 if (IsUpdateExprFound) {
5160 V = BinOp->getLHS();
5161 X = Checker.getX();
5162 E = Checker.getExpr();
5163 UE = Checker.getUpdateExpr();
5164 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005165 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005166 }
5167 }
5168 if (!IsUpdateExprFound) {
5169 IsUpdateExprFound = !Checker.checkStatement(First);
5170 BinOp = nullptr;
5171 if (IsUpdateExprFound) {
5172 BinOp = dyn_cast<BinaryOperator>(Second);
5173 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5174 }
5175 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5176 // { x++; v = x; }
5177 // { x--; v = x; }
5178 // { ++x; v = x; }
5179 // { --x; v = x; }
5180 // { x binop= expr; v = x; }
5181 // { x = x binop expr; v = x; }
5182 // { x = expr binop x; v = x; }
5183 // Check that the second expression has form v = x.
5184 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5185 llvm::FoldingSetNodeID XId, PossibleXId;
5186 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5187 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5188 IsUpdateExprFound = XId == PossibleXId;
5189 if (IsUpdateExprFound) {
5190 V = BinOp->getLHS();
5191 X = Checker.getX();
5192 E = Checker.getExpr();
5193 UE = Checker.getUpdateExpr();
5194 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005195 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005196 }
5197 }
5198 }
5199 if (!IsUpdateExprFound) {
5200 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00005201 auto *FirstExpr = dyn_cast<Expr>(First);
5202 auto *SecondExpr = dyn_cast<Expr>(Second);
5203 if (!FirstExpr || !SecondExpr ||
5204 !(FirstExpr->isInstantiationDependent() ||
5205 SecondExpr->isInstantiationDependent())) {
5206 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
5207 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005208 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00005209 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
5210 : First->getLocStart();
5211 NoteRange = ErrorRange = FirstBinOp
5212 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00005213 : SourceRange(ErrorLoc, ErrorLoc);
5214 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005215 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
5216 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
5217 ErrorFound = NotAnAssignmentOp;
5218 NoteLoc = ErrorLoc = SecondBinOp
5219 ? SecondBinOp->getOperatorLoc()
5220 : Second->getLocStart();
5221 NoteRange = ErrorRange =
5222 SecondBinOp ? SecondBinOp->getSourceRange()
5223 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00005224 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005225 auto *PossibleXRHSInFirst =
5226 FirstBinOp->getRHS()->IgnoreParenImpCasts();
5227 auto *PossibleXLHSInSecond =
5228 SecondBinOp->getLHS()->IgnoreParenImpCasts();
5229 llvm::FoldingSetNodeID X1Id, X2Id;
5230 PossibleXRHSInFirst->Profile(X1Id, Context,
5231 /*Canonical=*/true);
5232 PossibleXLHSInSecond->Profile(X2Id, Context,
5233 /*Canonical=*/true);
5234 IsUpdateExprFound = X1Id == X2Id;
5235 if (IsUpdateExprFound) {
5236 V = FirstBinOp->getLHS();
5237 X = SecondBinOp->getLHS();
5238 E = SecondBinOp->getRHS();
5239 UE = nullptr;
5240 IsXLHSInRHSPart = false;
5241 IsPostfixUpdate = true;
5242 } else {
5243 ErrorFound = NotASpecificExpression;
5244 ErrorLoc = FirstBinOp->getExprLoc();
5245 ErrorRange = FirstBinOp->getSourceRange();
5246 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
5247 NoteRange = SecondBinOp->getRHS()->getSourceRange();
5248 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005249 }
5250 }
5251 }
5252 }
5253 } else {
5254 NoteLoc = ErrorLoc = Body->getLocStart();
5255 NoteRange = ErrorRange =
5256 SourceRange(Body->getLocStart(), Body->getLocStart());
5257 ErrorFound = NotTwoSubstatements;
5258 }
5259 } else {
5260 NoteLoc = ErrorLoc = Body->getLocStart();
5261 NoteRange = ErrorRange =
5262 SourceRange(Body->getLocStart(), Body->getLocStart());
5263 ErrorFound = NotACompoundStatement;
5264 }
5265 if (ErrorFound != NoError) {
5266 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
5267 << ErrorRange;
5268 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5269 return StmtError();
5270 } else if (CurContext->isDependentContext()) {
5271 UE = V = E = X = nullptr;
5272 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005273 }
Alexey Bataevdea47612014-07-23 07:46:59 +00005274 }
Alexey Bataev0162e452014-07-22 10:10:35 +00005275
5276 getCurFunction()->setHasBranchProtectedScope();
5277
Alexey Bataev62cec442014-11-18 10:14:22 +00005278 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00005279 X, V, E, UE, IsXLHSInRHSPart,
5280 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00005281}
5282
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005283StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
5284 Stmt *AStmt,
5285 SourceLocation StartLoc,
5286 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005287 if (!AStmt)
5288 return StmtError();
5289
Samuel Antao4af1b7b2015-12-02 17:44:43 +00005290 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5291 // 1.2.2 OpenMP Language Terminology
5292 // Structured block - An executable statement with a single entry at the
5293 // top and a single exit at the bottom.
5294 // The point of exit cannot be a branch out of the structured block.
5295 // longjmp() and throw() must not violate the entry/exit criteria.
5296 CS->getCapturedDecl()->setNothrow();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005297
Alexey Bataev13314bf2014-10-09 04:18:56 +00005298 // OpenMP [2.16, Nesting of Regions]
5299 // If specified, a teams construct must be contained within a target
5300 // construct. That target construct must contain no statements or directives
5301 // outside of the teams construct.
5302 if (DSAStack->hasInnerTeamsRegion()) {
5303 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
5304 bool OMPTeamsFound = true;
5305 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
5306 auto I = CS->body_begin();
5307 while (I != CS->body_end()) {
5308 auto OED = dyn_cast<OMPExecutableDirective>(*I);
5309 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
5310 OMPTeamsFound = false;
5311 break;
5312 }
5313 ++I;
5314 }
5315 assert(I != CS->body_end() && "Not found statement");
5316 S = *I;
5317 }
5318 if (!OMPTeamsFound) {
5319 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
5320 Diag(DSAStack->getInnerTeamsRegionLoc(),
5321 diag::note_omp_nested_teams_construct_here);
5322 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
5323 << isa<OMPExecutableDirective>(S);
5324 return StmtError();
5325 }
5326 }
5327
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005328 getCurFunction()->setHasBranchProtectedScope();
5329
5330 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5331}
5332
Michael Wong65f367f2015-07-21 13:44:28 +00005333StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
5334 Stmt *AStmt,
5335 SourceLocation StartLoc,
5336 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005337 if (!AStmt)
5338 return StmtError();
5339
5340 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5341
Michael Wong65f367f2015-07-21 13:44:28 +00005342 getCurFunction()->setHasBranchProtectedScope();
5343
5344 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
5345 AStmt);
5346}
5347
Alexey Bataev13314bf2014-10-09 04:18:56 +00005348StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
5349 Stmt *AStmt, SourceLocation StartLoc,
5350 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005351 if (!AStmt)
5352 return StmtError();
5353
Alexey Bataev13314bf2014-10-09 04:18:56 +00005354 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5355 // 1.2.2 OpenMP Language Terminology
5356 // Structured block - An executable statement with a single entry at the
5357 // top and a single exit at the bottom.
5358 // The point of exit cannot be a branch out of the structured block.
5359 // longjmp() and throw() must not violate the entry/exit criteria.
5360 CS->getCapturedDecl()->setNothrow();
5361
5362 getCurFunction()->setHasBranchProtectedScope();
5363
5364 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5365}
5366
Alexey Bataev6d4ed052015-07-01 06:57:41 +00005367StmtResult
5368Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
5369 SourceLocation EndLoc,
5370 OpenMPDirectiveKind CancelRegion) {
5371 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
5372 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
5373 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
5374 << getOpenMPDirectiveName(CancelRegion);
5375 return StmtError();
5376 }
5377 if (DSAStack->isParentNowaitRegion()) {
5378 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
5379 return StmtError();
5380 }
5381 if (DSAStack->isParentOrderedRegion()) {
5382 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
5383 return StmtError();
5384 }
5385 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
5386 CancelRegion);
5387}
5388
Alexey Bataev87933c72015-09-18 08:07:34 +00005389StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
5390 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00005391 SourceLocation EndLoc,
5392 OpenMPDirectiveKind CancelRegion) {
5393 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
5394 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
5395 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
5396 << getOpenMPDirectiveName(CancelRegion);
5397 return StmtError();
5398 }
5399 if (DSAStack->isParentNowaitRegion()) {
5400 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
5401 return StmtError();
5402 }
5403 if (DSAStack->isParentOrderedRegion()) {
5404 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
5405 return StmtError();
5406 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005407 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00005408 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
5409 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00005410}
5411
Alexey Bataev382967a2015-12-08 12:06:20 +00005412static bool checkGrainsizeNumTasksClauses(Sema &S,
5413 ArrayRef<OMPClause *> Clauses) {
5414 OMPClause *PrevClause = nullptr;
5415 bool ErrorFound = false;
5416 for (auto *C : Clauses) {
5417 if (C->getClauseKind() == OMPC_grainsize ||
5418 C->getClauseKind() == OMPC_num_tasks) {
5419 if (!PrevClause)
5420 PrevClause = C;
5421 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
5422 S.Diag(C->getLocStart(),
5423 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
5424 << getOpenMPClauseName(C->getClauseKind())
5425 << getOpenMPClauseName(PrevClause->getClauseKind());
5426 S.Diag(PrevClause->getLocStart(),
5427 diag::note_omp_previous_grainsize_num_tasks)
5428 << getOpenMPClauseName(PrevClause->getClauseKind());
5429 ErrorFound = true;
5430 }
5431 }
5432 }
5433 return ErrorFound;
5434}
5435
Alexey Bataev49f6e782015-12-01 04:18:41 +00005436StmtResult Sema::ActOnOpenMPTaskLoopDirective(
5437 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5438 SourceLocation EndLoc,
5439 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
5440 if (!AStmt)
5441 return StmtError();
5442
5443 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5444 OMPLoopDirective::HelperExprs B;
5445 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5446 // define the nested loops number.
5447 unsigned NestedLoopCount =
5448 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005449 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00005450 VarsWithImplicitDSA, B);
5451 if (NestedLoopCount == 0)
5452 return StmtError();
5453
5454 assert((CurContext->isDependentContext() || B.builtAll()) &&
5455 "omp for loop exprs were not built");
5456
Alexey Bataev382967a2015-12-08 12:06:20 +00005457 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
5458 // The grainsize clause and num_tasks clause are mutually exclusive and may
5459 // not appear on the same taskloop directive.
5460 if (checkGrainsizeNumTasksClauses(*this, Clauses))
5461 return StmtError();
5462
Alexey Bataev49f6e782015-12-01 04:18:41 +00005463 getCurFunction()->setHasBranchProtectedScope();
5464 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
5465 NestedLoopCount, Clauses, AStmt, B);
5466}
5467
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005468StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
5469 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5470 SourceLocation EndLoc,
5471 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
5472 if (!AStmt)
5473 return StmtError();
5474
5475 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5476 OMPLoopDirective::HelperExprs B;
5477 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5478 // define the nested loops number.
5479 unsigned NestedLoopCount =
5480 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
5481 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
5482 VarsWithImplicitDSA, B);
5483 if (NestedLoopCount == 0)
5484 return StmtError();
5485
5486 assert((CurContext->isDependentContext() || B.builtAll()) &&
5487 "omp for loop exprs were not built");
5488
Alexey Bataev382967a2015-12-08 12:06:20 +00005489 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
5490 // The grainsize clause and num_tasks clause are mutually exclusive and may
5491 // not appear on the same taskloop directive.
5492 if (checkGrainsizeNumTasksClauses(*this, Clauses))
5493 return StmtError();
5494
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005495 getCurFunction()->setHasBranchProtectedScope();
5496 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
5497 NestedLoopCount, Clauses, AStmt, B);
5498}
5499
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005500StmtResult Sema::ActOnOpenMPDistributeDirective(
5501 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5502 SourceLocation EndLoc,
5503 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
5504 if (!AStmt)
5505 return StmtError();
5506
5507 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5508 OMPLoopDirective::HelperExprs B;
5509 // In presence of clause 'collapse' with number of loops, it will
5510 // define the nested loops number.
5511 unsigned NestedLoopCount =
5512 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
5513 nullptr /*ordered not a clause on distribute*/, AStmt,
5514 *this, *DSAStack, VarsWithImplicitDSA, B);
5515 if (NestedLoopCount == 0)
5516 return StmtError();
5517
5518 assert((CurContext->isDependentContext() || B.builtAll()) &&
5519 "omp for loop exprs were not built");
5520
5521 getCurFunction()->setHasBranchProtectedScope();
5522 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
5523 NestedLoopCount, Clauses, AStmt, B);
5524}
5525
Alexey Bataeved09d242014-05-28 05:53:51 +00005526OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005527 SourceLocation StartLoc,
5528 SourceLocation LParenLoc,
5529 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005530 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005531 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00005532 case OMPC_final:
5533 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
5534 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00005535 case OMPC_num_threads:
5536 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
5537 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00005538 case OMPC_safelen:
5539 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
5540 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00005541 case OMPC_simdlen:
5542 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
5543 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00005544 case OMPC_collapse:
5545 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
5546 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005547 case OMPC_ordered:
5548 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
5549 break;
Michael Wonge710d542015-08-07 16:16:36 +00005550 case OMPC_device:
5551 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
5552 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00005553 case OMPC_num_teams:
5554 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
5555 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005556 case OMPC_thread_limit:
5557 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
5558 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00005559 case OMPC_priority:
5560 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
5561 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00005562 case OMPC_grainsize:
5563 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
5564 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00005565 case OMPC_num_tasks:
5566 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
5567 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00005568 case OMPC_hint:
5569 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
5570 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005571 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005572 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005573 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005574 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005575 case OMPC_private:
5576 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00005577 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005578 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00005579 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00005580 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00005581 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005582 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005583 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00005584 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005585 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005586 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005587 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00005588 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005589 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00005590 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00005591 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00005592 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005593 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005594 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00005595 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005596 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00005597 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00005598 case OMPC_nogroup:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005599 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005600 llvm_unreachable("Clause is not allowed.");
5601 }
5602 return Res;
5603}
5604
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005605OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
5606 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005607 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005608 SourceLocation NameModifierLoc,
5609 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005610 SourceLocation EndLoc) {
5611 Expr *ValExpr = Condition;
5612 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
5613 !Condition->isInstantiationDependent() &&
5614 !Condition->containsUnexpandedParameterPack()) {
5615 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00005616 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005617 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005618 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005619
Nikola Smiljanic01a75982014-05-29 10:55:11 +00005620 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005621 }
5622
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005623 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
5624 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005625}
5626
Alexey Bataev3778b602014-07-17 07:32:53 +00005627OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
5628 SourceLocation StartLoc,
5629 SourceLocation LParenLoc,
5630 SourceLocation EndLoc) {
5631 Expr *ValExpr = Condition;
5632 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
5633 !Condition->isInstantiationDependent() &&
5634 !Condition->containsUnexpandedParameterPack()) {
5635 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
5636 Condition->getExprLoc(), Condition);
5637 if (Val.isInvalid())
5638 return nullptr;
5639
5640 ValExpr = Val.get();
5641 }
5642
5643 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
5644}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005645ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
5646 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00005647 if (!Op)
5648 return ExprError();
5649
5650 class IntConvertDiagnoser : public ICEConvertDiagnoser {
5651 public:
5652 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00005653 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00005654 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
5655 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005656 return S.Diag(Loc, diag::err_omp_not_integral) << T;
5657 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005658 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
5659 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005660 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
5661 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005662 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
5663 QualType T,
5664 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005665 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
5666 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005667 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
5668 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005669 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00005670 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00005671 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005672 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
5673 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005674 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
5675 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005676 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
5677 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005678 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00005679 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00005680 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005681 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
5682 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005683 llvm_unreachable("conversion functions are permitted");
5684 }
5685 } ConvertDiagnoser;
5686 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
5687}
5688
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005689static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00005690 OpenMPClauseKind CKind,
5691 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005692 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
5693 !ValExpr->isInstantiationDependent()) {
5694 SourceLocation Loc = ValExpr->getExprLoc();
5695 ExprResult Value =
5696 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
5697 if (Value.isInvalid())
5698 return false;
5699
5700 ValExpr = Value.get();
5701 // The expression must evaluate to a non-negative integer value.
5702 llvm::APSInt Result;
5703 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00005704 Result.isSigned() &&
5705 !((!StrictlyPositive && Result.isNonNegative()) ||
5706 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005707 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00005708 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
5709 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005710 return false;
5711 }
5712 }
5713 return true;
5714}
5715
Alexey Bataev568a8332014-03-06 06:15:19 +00005716OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
5717 SourceLocation StartLoc,
5718 SourceLocation LParenLoc,
5719 SourceLocation EndLoc) {
5720 Expr *ValExpr = NumThreads;
Alexey Bataev568a8332014-03-06 06:15:19 +00005721
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005722 // OpenMP [2.5, Restrictions]
5723 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00005724 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
5725 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005726 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00005727
Alexey Bataeved09d242014-05-28 05:53:51 +00005728 return new (Context)
5729 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00005730}
5731
Alexey Bataev62c87d22014-03-21 04:51:18 +00005732ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
5733 OpenMPClauseKind CKind) {
5734 if (!E)
5735 return ExprError();
5736 if (E->isValueDependent() || E->isTypeDependent() ||
5737 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00005738 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00005739 llvm::APSInt Result;
5740 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
5741 if (ICE.isInvalid())
5742 return ExprError();
5743 if (!Result.isStrictlyPositive()) {
5744 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00005745 << getOpenMPClauseName(CKind) << 1 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00005746 return ExprError();
5747 }
Alexander Musman09184fe2014-09-30 05:29:28 +00005748 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
5749 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
5750 << E->getSourceRange();
5751 return ExprError();
5752 }
Alexey Bataev7b6bc882015-11-26 07:50:39 +00005753 if (CKind == OMPC_collapse)
5754 DSAStack->setCollapseNumber(Result.getExtValue());
5755 else if (CKind == OMPC_ordered)
5756 DSAStack->setCollapseNumber(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00005757 return ICE;
5758}
5759
5760OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
5761 SourceLocation LParenLoc,
5762 SourceLocation EndLoc) {
5763 // OpenMP [2.8.1, simd construct, Description]
5764 // The parameter of the safelen clause must be a constant
5765 // positive integer expression.
5766 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
5767 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005768 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00005769 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00005770 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00005771}
5772
Alexey Bataev66b15b52015-08-21 11:14:16 +00005773OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
5774 SourceLocation LParenLoc,
5775 SourceLocation EndLoc) {
5776 // OpenMP [2.8.1, simd construct, Description]
5777 // The parameter of the simdlen clause must be a constant
5778 // positive integer expression.
5779 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
5780 if (Simdlen.isInvalid())
5781 return nullptr;
5782 return new (Context)
5783 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
5784}
5785
Alexander Musman64d33f12014-06-04 07:53:32 +00005786OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
5787 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00005788 SourceLocation LParenLoc,
5789 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00005790 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00005791 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00005792 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00005793 // The parameter of the collapse clause must be a constant
5794 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00005795 ExprResult NumForLoopsResult =
5796 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
5797 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00005798 return nullptr;
5799 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00005800 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00005801}
5802
Alexey Bataev10e775f2015-07-30 11:36:16 +00005803OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
5804 SourceLocation EndLoc,
5805 SourceLocation LParenLoc,
5806 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00005807 // OpenMP [2.7.1, loop construct, Description]
5808 // OpenMP [2.8.1, simd construct, Description]
5809 // OpenMP [2.9.6, distribute construct, Description]
5810 // The parameter of the ordered clause must be a constant
5811 // positive integer expression if any.
5812 if (NumForLoops && LParenLoc.isValid()) {
5813 ExprResult NumForLoopsResult =
5814 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
5815 if (NumForLoopsResult.isInvalid())
5816 return nullptr;
5817 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00005818 } else
5819 NumForLoops = nullptr;
5820 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00005821 return new (Context)
5822 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
5823}
5824
Alexey Bataeved09d242014-05-28 05:53:51 +00005825OMPClause *Sema::ActOnOpenMPSimpleClause(
5826 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
5827 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005828 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005829 switch (Kind) {
5830 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00005831 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00005832 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
5833 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005834 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005835 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00005836 Res = ActOnOpenMPProcBindClause(
5837 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
5838 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005839 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005840 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00005841 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00005842 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00005843 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00005844 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00005845 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005846 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005847 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005848 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00005849 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00005850 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00005851 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00005852 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00005853 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005854 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005855 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005856 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00005857 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005858 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005859 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005860 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00005861 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005862 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00005863 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00005864 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00005865 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005866 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005867 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00005868 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00005869 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005870 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00005871 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00005872 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005873 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00005874 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00005875 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00005876 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00005877 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00005878 case OMPC_hint:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005879 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005880 llvm_unreachable("Clause is not allowed.");
5881 }
5882 return Res;
5883}
5884
5885OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
5886 SourceLocation KindKwLoc,
5887 SourceLocation StartLoc,
5888 SourceLocation LParenLoc,
5889 SourceLocation EndLoc) {
5890 if (Kind == OMPC_DEFAULT_unknown) {
5891 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005892 static_assert(OMPC_DEFAULT_unknown > 0,
5893 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00005894 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005895 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005896 Values += "'";
5897 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
5898 Values += "'";
5899 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005900 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005901 Values += " or ";
5902 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005903 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005904 break;
5905 default:
5906 Values += Sep;
5907 break;
5908 }
5909 }
5910 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00005911 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005912 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005913 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00005914 switch (Kind) {
5915 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005916 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005917 break;
5918 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005919 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005920 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005921 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005922 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00005923 break;
5924 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005925 return new (Context)
5926 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005927}
5928
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005929OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
5930 SourceLocation KindKwLoc,
5931 SourceLocation StartLoc,
5932 SourceLocation LParenLoc,
5933 SourceLocation EndLoc) {
5934 if (Kind == OMPC_PROC_BIND_unknown) {
5935 std::string Values;
5936 std::string Sep(", ");
5937 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
5938 Values += "'";
5939 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
5940 Values += "'";
5941 switch (i) {
5942 case OMPC_PROC_BIND_unknown - 2:
5943 Values += " or ";
5944 break;
5945 case OMPC_PROC_BIND_unknown - 1:
5946 break;
5947 default:
5948 Values += Sep;
5949 break;
5950 }
5951 }
5952 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00005953 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005954 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005955 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005956 return new (Context)
5957 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005958}
5959
Alexey Bataev56dafe82014-06-20 07:16:17 +00005960OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
5961 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
5962 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005963 SourceLocation ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00005964 SourceLocation EndLoc) {
5965 OMPClause *Res = nullptr;
5966 switch (Kind) {
5967 case OMPC_schedule:
5968 Res = ActOnOpenMPScheduleClause(
5969 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005970 LParenLoc, ArgumentLoc, DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00005971 break;
5972 case OMPC_if:
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005973 Res =
5974 ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument), Expr,
5975 StartLoc, LParenLoc, ArgumentLoc, DelimLoc, EndLoc);
5976 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00005977 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005978 case OMPC_num_threads:
5979 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00005980 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005981 case OMPC_collapse:
5982 case OMPC_default:
5983 case OMPC_proc_bind:
5984 case OMPC_private:
5985 case OMPC_firstprivate:
5986 case OMPC_lastprivate:
5987 case OMPC_shared:
5988 case OMPC_reduction:
5989 case OMPC_linear:
5990 case OMPC_aligned:
5991 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005992 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005993 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00005994 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005995 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005996 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005997 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00005998 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005999 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006000 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006001 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006002 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006003 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006004 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006005 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006006 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006007 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006008 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006009 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006010 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006011 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006012 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006013 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006014 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006015 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006016 case OMPC_unknown:
6017 llvm_unreachable("Clause is not allowed.");
6018 }
6019 return Res;
6020}
6021
6022OMPClause *Sema::ActOnOpenMPScheduleClause(
6023 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
6024 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
6025 SourceLocation EndLoc) {
6026 if (Kind == OMPC_SCHEDULE_unknown) {
6027 std::string Values;
6028 std::string Sep(", ");
6029 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) {
6030 Values += "'";
6031 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i);
6032 Values += "'";
6033 switch (i) {
6034 case OMPC_SCHEDULE_unknown - 2:
6035 Values += " or ";
6036 break;
6037 case OMPC_SCHEDULE_unknown - 1:
6038 break;
6039 default:
6040 Values += Sep;
6041 break;
6042 }
6043 }
6044 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
6045 << Values << getOpenMPClauseName(OMPC_schedule);
6046 return nullptr;
6047 }
6048 Expr *ValExpr = ChunkSize;
Alexey Bataev040d5402015-05-12 08:35:28 +00006049 Expr *HelperValExpr = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00006050 if (ChunkSize) {
6051 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
6052 !ChunkSize->isInstantiationDependent() &&
6053 !ChunkSize->containsUnexpandedParameterPack()) {
6054 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
6055 ExprResult Val =
6056 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
6057 if (Val.isInvalid())
6058 return nullptr;
6059
6060 ValExpr = Val.get();
6061
6062 // OpenMP [2.7.1, Restrictions]
6063 // chunk_size must be a loop invariant integer expression with a positive
6064 // value.
6065 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00006066 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
6067 if (Result.isSigned() && !Result.isStrictlyPositive()) {
6068 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00006069 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00006070 return nullptr;
6071 }
6072 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
6073 auto *ImpVar = buildVarDecl(*this, ChunkSize->getExprLoc(),
6074 ChunkSize->getType(), ".chunk.");
6075 auto *ImpVarRef = buildDeclRefExpr(*this, ImpVar, ChunkSize->getType(),
6076 ChunkSize->getExprLoc(),
6077 /*RefersToCapture=*/true);
6078 HelperValExpr = ImpVarRef;
Alexey Bataev56dafe82014-06-20 07:16:17 +00006079 }
6080 }
6081 }
6082
6083 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc,
Alexey Bataev040d5402015-05-12 08:35:28 +00006084 EndLoc, Kind, ValExpr, HelperValExpr);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006085}
6086
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006087OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
6088 SourceLocation StartLoc,
6089 SourceLocation EndLoc) {
6090 OMPClause *Res = nullptr;
6091 switch (Kind) {
6092 case OMPC_ordered:
6093 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
6094 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00006095 case OMPC_nowait:
6096 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
6097 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006098 case OMPC_untied:
6099 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
6100 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006101 case OMPC_mergeable:
6102 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
6103 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006104 case OMPC_read:
6105 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
6106 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00006107 case OMPC_write:
6108 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
6109 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006110 case OMPC_update:
6111 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
6112 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00006113 case OMPC_capture:
6114 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
6115 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006116 case OMPC_seq_cst:
6117 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
6118 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00006119 case OMPC_threads:
6120 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
6121 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006122 case OMPC_simd:
6123 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
6124 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00006125 case OMPC_nogroup:
6126 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
6127 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006128 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006129 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006130 case OMPC_num_threads:
6131 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006132 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006133 case OMPC_collapse:
6134 case OMPC_schedule:
6135 case OMPC_private:
6136 case OMPC_firstprivate:
6137 case OMPC_lastprivate:
6138 case OMPC_shared:
6139 case OMPC_reduction:
6140 case OMPC_linear:
6141 case OMPC_aligned:
6142 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006143 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006144 case OMPC_default:
6145 case OMPC_proc_bind:
6146 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006147 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006148 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006149 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006150 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006151 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006152 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006153 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006154 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00006155 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006156 case OMPC_hint:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006157 case OMPC_unknown:
6158 llvm_unreachable("Clause is not allowed.");
6159 }
6160 return Res;
6161}
6162
Alexey Bataev236070f2014-06-20 11:19:47 +00006163OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
6164 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006165 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00006166 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
6167}
6168
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006169OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
6170 SourceLocation EndLoc) {
6171 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
6172}
6173
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006174OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
6175 SourceLocation EndLoc) {
6176 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
6177}
6178
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006179OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
6180 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006181 return new (Context) OMPReadClause(StartLoc, EndLoc);
6182}
6183
Alexey Bataevdea47612014-07-23 07:46:59 +00006184OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
6185 SourceLocation EndLoc) {
6186 return new (Context) OMPWriteClause(StartLoc, EndLoc);
6187}
6188
Alexey Bataev67a4f222014-07-23 10:25:33 +00006189OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
6190 SourceLocation EndLoc) {
6191 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
6192}
6193
Alexey Bataev459dec02014-07-24 06:46:57 +00006194OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
6195 SourceLocation EndLoc) {
6196 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
6197}
6198
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006199OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
6200 SourceLocation EndLoc) {
6201 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
6202}
6203
Alexey Bataev346265e2015-09-25 10:37:12 +00006204OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
6205 SourceLocation EndLoc) {
6206 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
6207}
6208
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006209OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
6210 SourceLocation EndLoc) {
6211 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
6212}
6213
Alexey Bataevb825de12015-12-07 10:51:44 +00006214OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
6215 SourceLocation EndLoc) {
6216 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
6217}
6218
Alexey Bataevc5e02582014-06-16 07:08:35 +00006219OMPClause *Sema::ActOnOpenMPVarListClause(
6220 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
6221 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
6222 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006223 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Kelvin Li0bff7af2015-11-23 05:32:03 +00006224 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
6225 OpenMPMapClauseKind MapType, SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006226 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006227 switch (Kind) {
6228 case OMPC_private:
6229 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6230 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006231 case OMPC_firstprivate:
6232 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6233 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00006234 case OMPC_lastprivate:
6235 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6236 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00006237 case OMPC_shared:
6238 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
6239 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006240 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00006241 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
6242 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006243 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00006244 case OMPC_linear:
6245 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00006246 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00006247 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006248 case OMPC_aligned:
6249 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
6250 ColonLoc, EndLoc);
6251 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006252 case OMPC_copyin:
6253 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
6254 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00006255 case OMPC_copyprivate:
6256 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6257 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00006258 case OMPC_flush:
6259 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
6260 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006261 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006262 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
6263 StartLoc, LParenLoc, EndLoc);
6264 break;
6265 case OMPC_map:
6266 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, DepLinMapLoc, ColonLoc,
6267 VarList, StartLoc, LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006268 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006269 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006270 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00006271 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00006272 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006273 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00006274 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006275 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006276 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006277 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006278 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006279 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006280 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006281 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006282 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006283 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006284 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006285 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006286 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006287 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00006288 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006289 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006290 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006291 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006292 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006293 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006294 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006295 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006296 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006297 case OMPC_hint:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006298 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006299 llvm_unreachable("Clause is not allowed.");
6300 }
6301 return Res;
6302}
6303
6304OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
6305 SourceLocation StartLoc,
6306 SourceLocation LParenLoc,
6307 SourceLocation EndLoc) {
6308 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00006309 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00006310 for (auto &RefExpr : VarList) {
6311 assert(RefExpr && "NULL expr in OpenMP private clause.");
6312 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006313 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006314 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006315 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006316 continue;
6317 }
6318
Alexey Bataeved09d242014-05-28 05:53:51 +00006319 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006320 // OpenMP [2.1, C/C++]
6321 // A list item is a variable name.
6322 // OpenMP [2.9.3.3, Restrictions, p.1]
6323 // A variable that is part of another variable (as an array or
6324 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00006325 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006326 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006327 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006328 continue;
6329 }
6330 Decl *D = DE->getDecl();
6331 VarDecl *VD = cast<VarDecl>(D);
6332
6333 QualType Type = VD->getType();
6334 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6335 // It will be analyzed later.
6336 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006337 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006338 continue;
6339 }
6340
6341 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
6342 // A variable that appears in a private clause must not have an incomplete
6343 // type or a reference type.
6344 if (RequireCompleteType(ELoc, Type,
6345 diag::err_omp_private_incomplete_type)) {
6346 continue;
6347 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006348 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006349
Alexey Bataev758e55e2013-09-06 18:03:48 +00006350 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6351 // in a Construct]
6352 // Variables with the predetermined data-sharing attributes may not be
6353 // listed in data-sharing attributes clauses, except for the cases
6354 // listed below. For these exceptions only, listing a predetermined
6355 // variable in a data-sharing attribute clause is allowed and overrides
6356 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006357 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006358 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006359 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
6360 << getOpenMPClauseName(OMPC_private);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006361 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006362 continue;
6363 }
6364
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006365 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00006366 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006367 DSAStack->getCurrentDirective() == OMPD_task) {
6368 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
6369 << getOpenMPClauseName(OMPC_private) << Type
6370 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
6371 bool IsDecl =
6372 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6373 Diag(VD->getLocation(),
6374 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6375 << VD;
6376 continue;
6377 }
6378
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006379 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
6380 // A variable of class type (or array thereof) that appears in a private
6381 // clause requires an accessible, unambiguous default constructor for the
6382 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00006383 // Generate helper private variable and initialize it with the default
6384 // value. The address of the original variable is replaced by the address of
6385 // the new private variable in CodeGen. This new variable is not added to
6386 // IdResolver, so the code in the OpenMP region uses original variable for
6387 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006388 Type = Type.getUnqualifiedType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006389 auto VDPrivate = buildVarDecl(*this, DE->getExprLoc(), Type, VD->getName(),
6390 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00006391 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006392 if (VDPrivate->isInvalidDecl())
6393 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006394 auto VDPrivateRefExpr = buildDeclRefExpr(
6395 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev03b340a2014-10-21 03:16:40 +00006396
Alexey Bataev758e55e2013-09-06 18:03:48 +00006397 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006398 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006399 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006400 }
6401
Alexey Bataeved09d242014-05-28 05:53:51 +00006402 if (Vars.empty())
6403 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006404
Alexey Bataev03b340a2014-10-21 03:16:40 +00006405 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
6406 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006407}
6408
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006409namespace {
6410class DiagsUninitializedSeveretyRAII {
6411private:
6412 DiagnosticsEngine &Diags;
6413 SourceLocation SavedLoc;
6414 bool IsIgnored;
6415
6416public:
6417 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
6418 bool IsIgnored)
6419 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
6420 if (!IsIgnored) {
6421 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
6422 /*Map*/ diag::Severity::Ignored, Loc);
6423 }
6424 }
6425 ~DiagsUninitializedSeveretyRAII() {
6426 if (!IsIgnored)
6427 Diags.popMappings(SavedLoc);
6428 }
6429};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00006430}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006431
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006432OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
6433 SourceLocation StartLoc,
6434 SourceLocation LParenLoc,
6435 SourceLocation EndLoc) {
6436 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006437 SmallVector<Expr *, 8> PrivateCopies;
6438 SmallVector<Expr *, 8> Inits;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006439 bool IsImplicitClause =
6440 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
6441 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
6442
Alexey Bataeved09d242014-05-28 05:53:51 +00006443 for (auto &RefExpr : VarList) {
6444 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
6445 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006446 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006447 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006448 PrivateCopies.push_back(nullptr);
6449 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006450 continue;
6451 }
6452
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006453 SourceLocation ELoc =
6454 IsImplicitClause ? ImplicitClauseLoc : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006455 // OpenMP [2.1, C/C++]
6456 // A list item is a variable name.
6457 // OpenMP [2.9.3.3, Restrictions, p.1]
6458 // A variable that is part of another variable (as an array or
6459 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00006460 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006461 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006462 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006463 continue;
6464 }
6465 Decl *D = DE->getDecl();
6466 VarDecl *VD = cast<VarDecl>(D);
6467
6468 QualType Type = VD->getType();
6469 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6470 // It will be analyzed later.
6471 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006472 PrivateCopies.push_back(nullptr);
6473 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006474 continue;
6475 }
6476
6477 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
6478 // A variable that appears in a private clause must not have an incomplete
6479 // type or a reference type.
6480 if (RequireCompleteType(ELoc, Type,
6481 diag::err_omp_firstprivate_incomplete_type)) {
6482 continue;
6483 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006484 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006485
6486 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
6487 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00006488 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006489 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006490 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006491
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006492 // If an implicit firstprivate variable found it was checked already.
6493 if (!IsImplicitClause) {
6494 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006495 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006496 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
6497 // A list item that specifies a given variable may not appear in more
6498 // than one clause on the same directive, except that a variable may be
6499 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006500 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00006501 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006502 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00006503 << getOpenMPClauseName(DVar.CKind)
6504 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006505 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006506 continue;
6507 }
6508
6509 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6510 // in a Construct]
6511 // Variables with the predetermined data-sharing attributes may not be
6512 // listed in data-sharing attributes clauses, except for the cases
6513 // listed below. For these exceptions only, listing a predetermined
6514 // variable in a data-sharing attribute clause is allowed and overrides
6515 // the variable's predetermined data-sharing attributes.
6516 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6517 // in a Construct, C/C++, p.2]
6518 // Variables with const-qualified type having no mutable member may be
6519 // listed in a firstprivate clause, even if they are static data members.
6520 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
6521 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
6522 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00006523 << getOpenMPClauseName(DVar.CKind)
6524 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006525 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006526 continue;
6527 }
6528
Alexey Bataevf29276e2014-06-18 04:14:57 +00006529 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006530 // OpenMP [2.9.3.4, Restrictions, p.2]
6531 // A list item that is private within a parallel region must not appear
6532 // in a firstprivate clause on a worksharing construct if any of the
6533 // worksharing regions arising from the worksharing construct ever bind
6534 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00006535 if (isOpenMPWorksharingDirective(CurrDir) &&
6536 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006537 DVar = DSAStack->getImplicitDSA(VD, true);
6538 if (DVar.CKind != OMPC_shared &&
6539 (isOpenMPParallelDirective(DVar.DKind) ||
6540 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00006541 Diag(ELoc, diag::err_omp_required_access)
6542 << getOpenMPClauseName(OMPC_firstprivate)
6543 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006544 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006545 continue;
6546 }
6547 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006548 // OpenMP [2.9.3.4, Restrictions, p.3]
6549 // A list item that appears in a reduction clause of a parallel construct
6550 // must not appear in a firstprivate clause on a worksharing or task
6551 // construct if any of the worksharing or task regions arising from the
6552 // worksharing or task construct ever bind to any of the parallel regions
6553 // arising from the parallel construct.
6554 // OpenMP [2.9.3.4, Restrictions, p.4]
6555 // A list item that appears in a reduction clause in worksharing
6556 // construct must not appear in a firstprivate clause in a task construct
6557 // encountered during execution of any of the worksharing regions arising
6558 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006559 if (CurrDir == OMPD_task) {
6560 DVar =
6561 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
6562 [](OpenMPDirectiveKind K) -> bool {
6563 return isOpenMPParallelDirective(K) ||
6564 isOpenMPWorksharingDirective(K);
6565 },
6566 false);
6567 if (DVar.CKind == OMPC_reduction &&
6568 (isOpenMPParallelDirective(DVar.DKind) ||
6569 isOpenMPWorksharingDirective(DVar.DKind))) {
6570 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
6571 << getOpenMPDirectiveName(DVar.DKind);
6572 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6573 continue;
6574 }
6575 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006576
6577 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
6578 // A list item that is private within a teams region must not appear in a
6579 // firstprivate clause on a distribute construct if any of the distribute
6580 // regions arising from the distribute construct ever bind to any of the
6581 // teams regions arising from the teams construct.
6582 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
6583 // A list item that appears in a reduction clause of a teams construct
6584 // must not appear in a firstprivate clause on a distribute construct if
6585 // any of the distribute regions arising from the distribute construct
6586 // ever bind to any of the teams regions arising from the teams construct.
6587 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
6588 // A list item may appear in a firstprivate or lastprivate clause but not
6589 // both.
6590 if (CurrDir == OMPD_distribute) {
6591 DVar = DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_private),
6592 [](OpenMPDirectiveKind K) -> bool {
6593 return isOpenMPTeamsDirective(K);
6594 },
6595 false);
6596 if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
6597 Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
6598 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6599 continue;
6600 }
6601 DVar = DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
6602 [](OpenMPDirectiveKind K) -> bool {
6603 return isOpenMPTeamsDirective(K);
6604 },
6605 false);
6606 if (DVar.CKind == OMPC_reduction &&
6607 isOpenMPTeamsDirective(DVar.DKind)) {
6608 Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
6609 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6610 continue;
6611 }
6612 DVar = DSAStack->getTopDSA(VD, false);
6613 if (DVar.CKind == OMPC_lastprivate) {
6614 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
6615 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6616 continue;
6617 }
6618 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006619 }
6620
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006621 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00006622 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006623 DSAStack->getCurrentDirective() == OMPD_task) {
6624 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
6625 << getOpenMPClauseName(OMPC_firstprivate) << Type
6626 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
6627 bool IsDecl =
6628 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6629 Diag(VD->getLocation(),
6630 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6631 << VD;
6632 continue;
6633 }
6634
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006635 Type = Type.getUnqualifiedType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006636 auto VDPrivate = buildVarDecl(*this, ELoc, Type, VD->getName(),
6637 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006638 // Generate helper private variable and initialize it with the value of the
6639 // original variable. The address of the original variable is replaced by
6640 // the address of the new private variable in the CodeGen. This new variable
6641 // is not added to IdResolver, so the code in the OpenMP region uses
6642 // original variable for proper diagnostics and variable capturing.
6643 Expr *VDInitRefExpr = nullptr;
6644 // For arrays generate initializer for single element and replace it by the
6645 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006646 if (Type->isArrayType()) {
6647 auto VDInit =
6648 buildVarDecl(*this, DE->getExprLoc(), ElemType, VD->getName());
6649 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006650 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006651 ElemType = ElemType.getUnqualifiedType();
6652 auto *VDInitTemp = buildVarDecl(*this, DE->getLocStart(), ElemType,
6653 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00006654 InitializedEntity Entity =
6655 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006656 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
6657
6658 InitializationSequence InitSeq(*this, Entity, Kind, Init);
6659 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
6660 if (Result.isInvalid())
6661 VDPrivate->setInvalidDecl();
6662 else
6663 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006664 // Remove temp variable declaration.
6665 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006666 } else {
Alexey Bataev69c62a92015-04-15 04:52:20 +00006667 auto *VDInit =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006668 buildVarDecl(*this, DE->getLocStart(), Type, ".firstprivate.temp");
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006669 VDInitRefExpr =
6670 buildDeclRefExpr(*this, VDInit, DE->getType(), DE->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00006671 AddInitializerToDecl(VDPrivate,
6672 DefaultLvalueConversion(VDInitRefExpr).get(),
6673 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006674 }
6675 if (VDPrivate->isInvalidDecl()) {
6676 if (IsImplicitClause) {
6677 Diag(DE->getExprLoc(),
6678 diag::note_omp_task_predetermined_firstprivate_here);
6679 }
6680 continue;
6681 }
6682 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00006683 auto VDPrivateRefExpr = buildDeclRefExpr(
6684 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006685 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
6686 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006687 PrivateCopies.push_back(VDPrivateRefExpr);
6688 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006689 }
6690
Alexey Bataeved09d242014-05-28 05:53:51 +00006691 if (Vars.empty())
6692 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006693
6694 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006695 Vars, PrivateCopies, Inits);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006696}
6697
Alexander Musman1bb328c2014-06-04 13:06:39 +00006698OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
6699 SourceLocation StartLoc,
6700 SourceLocation LParenLoc,
6701 SourceLocation EndLoc) {
6702 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00006703 SmallVector<Expr *, 8> SrcExprs;
6704 SmallVector<Expr *, 8> DstExprs;
6705 SmallVector<Expr *, 8> AssignmentOps;
Alexander Musman1bb328c2014-06-04 13:06:39 +00006706 for (auto &RefExpr : VarList) {
6707 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
6708 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6709 // It will be analyzed later.
6710 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00006711 SrcExprs.push_back(nullptr);
6712 DstExprs.push_back(nullptr);
6713 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006714 continue;
6715 }
6716
6717 SourceLocation ELoc = RefExpr->getExprLoc();
6718 // OpenMP [2.1, C/C++]
6719 // A list item is a variable name.
6720 // OpenMP [2.14.3.5, Restrictions, p.1]
6721 // A variable that is part of another variable (as an array or structure
6722 // element) cannot appear in a lastprivate clause.
6723 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
6724 if (!DE || !isa<VarDecl>(DE->getDecl())) {
6725 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
6726 continue;
6727 }
6728 Decl *D = DE->getDecl();
6729 VarDecl *VD = cast<VarDecl>(D);
6730
6731 QualType Type = VD->getType();
6732 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6733 // It will be analyzed later.
6734 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00006735 SrcExprs.push_back(nullptr);
6736 DstExprs.push_back(nullptr);
6737 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006738 continue;
6739 }
6740
6741 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
6742 // A variable that appears in a lastprivate clause must not have an
6743 // incomplete type or a reference type.
6744 if (RequireCompleteType(ELoc, Type,
6745 diag::err_omp_lastprivate_incomplete_type)) {
6746 continue;
6747 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006748 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00006749
6750 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
6751 // in a Construct]
6752 // Variables with the predetermined data-sharing attributes may not be
6753 // listed in data-sharing attributes clauses, except for the cases
6754 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006755 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006756 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
6757 DVar.CKind != OMPC_firstprivate &&
6758 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
6759 Diag(ELoc, diag::err_omp_wrong_dsa)
6760 << getOpenMPClauseName(DVar.CKind)
6761 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006762 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006763 continue;
6764 }
6765
Alexey Bataevf29276e2014-06-18 04:14:57 +00006766 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
6767 // OpenMP [2.14.3.5, Restrictions, p.2]
6768 // A list item that is private within a parallel region, or that appears in
6769 // the reduction clause of a parallel construct, must not appear in a
6770 // lastprivate clause on a worksharing construct if any of the corresponding
6771 // worksharing regions ever binds to any of the corresponding parallel
6772 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00006773 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00006774 if (isOpenMPWorksharingDirective(CurrDir) &&
6775 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006776 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006777 if (DVar.CKind != OMPC_shared) {
6778 Diag(ELoc, diag::err_omp_required_access)
6779 << getOpenMPClauseName(OMPC_lastprivate)
6780 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006781 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006782 continue;
6783 }
6784 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00006785 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00006786 // A variable of class type (or array thereof) that appears in a
6787 // lastprivate clause requires an accessible, unambiguous default
6788 // constructor for the class type, unless the list item is also specified
6789 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00006790 // A variable of class type (or array thereof) that appears in a
6791 // lastprivate clause requires an accessible, unambiguous copy assignment
6792 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00006793 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00006794 auto *SrcVD = buildVarDecl(*this, DE->getLocStart(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006795 Type.getUnqualifiedType(), ".lastprivate.src",
6796 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00006797 auto *PseudoSrcExpr = buildDeclRefExpr(
6798 *this, SrcVD, Type.getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00006799 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006800 buildVarDecl(*this, DE->getLocStart(), Type, ".lastprivate.dst",
6801 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00006802 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006803 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00006804 // For arrays generate assignment operation for single element and replace
6805 // it by the original array element in CodeGen.
6806 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
6807 PseudoDstExpr, PseudoSrcExpr);
6808 if (AssignmentOp.isInvalid())
6809 continue;
6810 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
6811 /*DiscardedValue=*/true);
6812 if (AssignmentOp.isInvalid())
6813 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00006814
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006815 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
6816 // A list item may appear in a firstprivate or lastprivate clause but not
6817 // both.
6818 if (CurrDir == OMPD_distribute) {
6819 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
6820 if (DVar.CKind == OMPC_firstprivate) {
6821 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
6822 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6823 continue;
6824 }
6825 }
6826
Alexey Bataev39f915b82015-05-08 10:41:21 +00006827 if (TopDVar.CKind != OMPC_firstprivate)
Alexey Bataevf29276e2014-06-18 04:14:57 +00006828 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006829 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00006830 SrcExprs.push_back(PseudoSrcExpr);
6831 DstExprs.push_back(PseudoDstExpr);
6832 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00006833 }
6834
6835 if (Vars.empty())
6836 return nullptr;
6837
6838 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00006839 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006840}
6841
Alexey Bataev758e55e2013-09-06 18:03:48 +00006842OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
6843 SourceLocation StartLoc,
6844 SourceLocation LParenLoc,
6845 SourceLocation EndLoc) {
6846 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00006847 for (auto &RefExpr : VarList) {
6848 assert(RefExpr && "NULL expr in OpenMP shared clause.");
6849 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00006850 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006851 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006852 continue;
6853 }
6854
Alexey Bataeved09d242014-05-28 05:53:51 +00006855 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00006856 // OpenMP [2.1, C/C++]
6857 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00006858 // OpenMP [2.14.3.2, Restrictions, p.1]
6859 // A variable that is part of another variable (as an array or structure
6860 // element) cannot appear in a shared unless it is a static data member
6861 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00006862 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006863 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006864 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00006865 continue;
6866 }
6867 Decl *D = DE->getDecl();
6868 VarDecl *VD = cast<VarDecl>(D);
6869
6870 QualType Type = VD->getType();
6871 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6872 // It will be analyzed later.
6873 Vars.push_back(DE);
6874 continue;
6875 }
6876
6877 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6878 // in a Construct]
6879 // Variables with the predetermined data-sharing attributes may not be
6880 // listed in data-sharing attributes clauses, except for the cases
6881 // listed below. For these exceptions only, listing a predetermined
6882 // variable in a data-sharing attribute clause is allowed and overrides
6883 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006884 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00006885 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
6886 DVar.RefExpr) {
6887 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
6888 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006889 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006890 continue;
6891 }
6892
6893 DSAStack->addDSA(VD, DE, OMPC_shared);
6894 Vars.push_back(DE);
6895 }
6896
Alexey Bataeved09d242014-05-28 05:53:51 +00006897 if (Vars.empty())
6898 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00006899
6900 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
6901}
6902
Alexey Bataevc5e02582014-06-16 07:08:35 +00006903namespace {
6904class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
6905 DSAStackTy *Stack;
6906
6907public:
6908 bool VisitDeclRefExpr(DeclRefExpr *E) {
6909 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006910 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006911 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
6912 return false;
6913 if (DVar.CKind != OMPC_unknown)
6914 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00006915 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006916 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006917 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00006918 return true;
6919 return false;
6920 }
6921 return false;
6922 }
6923 bool VisitStmt(Stmt *S) {
6924 for (auto Child : S->children()) {
6925 if (Child && Visit(Child))
6926 return true;
6927 }
6928 return false;
6929 }
Alexey Bataev23b69422014-06-18 07:08:49 +00006930 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00006931};
Alexey Bataev23b69422014-06-18 07:08:49 +00006932} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00006933
6934OMPClause *Sema::ActOnOpenMPReductionClause(
6935 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
6936 SourceLocation ColonLoc, SourceLocation EndLoc,
6937 CXXScopeSpec &ReductionIdScopeSpec,
6938 const DeclarationNameInfo &ReductionId) {
6939 // TODO: Allow scope specification search when 'declare reduction' is
6940 // supported.
6941 assert(ReductionIdScopeSpec.isEmpty() &&
6942 "No support for scoped reduction identifiers yet.");
6943
6944 auto DN = ReductionId.getName();
6945 auto OOK = DN.getCXXOverloadedOperator();
6946 BinaryOperatorKind BOK = BO_Comma;
6947
6948 // OpenMP [2.14.3.6, reduction clause]
6949 // C
6950 // reduction-identifier is either an identifier or one of the following
6951 // operators: +, -, *, &, |, ^, && and ||
6952 // C++
6953 // reduction-identifier is either an id-expression or one of the following
6954 // operators: +, -, *, &, |, ^, && and ||
6955 // FIXME: Only 'min' and 'max' identifiers are supported for now.
6956 switch (OOK) {
6957 case OO_Plus:
6958 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006959 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006960 break;
6961 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006962 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006963 break;
6964 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006965 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006966 break;
6967 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006968 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006969 break;
6970 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006971 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006972 break;
6973 case OO_AmpAmp:
6974 BOK = BO_LAnd;
6975 break;
6976 case OO_PipePipe:
6977 BOK = BO_LOr;
6978 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006979 case OO_New:
6980 case OO_Delete:
6981 case OO_Array_New:
6982 case OO_Array_Delete:
6983 case OO_Slash:
6984 case OO_Percent:
6985 case OO_Tilde:
6986 case OO_Exclaim:
6987 case OO_Equal:
6988 case OO_Less:
6989 case OO_Greater:
6990 case OO_LessEqual:
6991 case OO_GreaterEqual:
6992 case OO_PlusEqual:
6993 case OO_MinusEqual:
6994 case OO_StarEqual:
6995 case OO_SlashEqual:
6996 case OO_PercentEqual:
6997 case OO_CaretEqual:
6998 case OO_AmpEqual:
6999 case OO_PipeEqual:
7000 case OO_LessLess:
7001 case OO_GreaterGreater:
7002 case OO_LessLessEqual:
7003 case OO_GreaterGreaterEqual:
7004 case OO_EqualEqual:
7005 case OO_ExclaimEqual:
7006 case OO_PlusPlus:
7007 case OO_MinusMinus:
7008 case OO_Comma:
7009 case OO_ArrowStar:
7010 case OO_Arrow:
7011 case OO_Call:
7012 case OO_Subscript:
7013 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +00007014 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007015 case NUM_OVERLOADED_OPERATORS:
7016 llvm_unreachable("Unexpected reduction identifier");
7017 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00007018 if (auto II = DN.getAsIdentifierInfo()) {
7019 if (II->isStr("max"))
7020 BOK = BO_GT;
7021 else if (II->isStr("min"))
7022 BOK = BO_LT;
7023 }
7024 break;
7025 }
7026 SourceRange ReductionIdRange;
7027 if (ReductionIdScopeSpec.isValid()) {
7028 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
7029 }
7030 ReductionIdRange.setEnd(ReductionId.getEndLoc());
7031 if (BOK == BO_Comma) {
7032 // Not allowed reduction identifier is found.
7033 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
7034 << ReductionIdRange;
7035 return nullptr;
7036 }
7037
7038 SmallVector<Expr *, 8> Vars;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007039 SmallVector<Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007040 SmallVector<Expr *, 8> LHSs;
7041 SmallVector<Expr *, 8> RHSs;
7042 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007043 for (auto RefExpr : VarList) {
7044 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
7045 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7046 // It will be analyzed later.
7047 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007048 Privates.push_back(nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007049 LHSs.push_back(nullptr);
7050 RHSs.push_back(nullptr);
7051 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007052 continue;
7053 }
7054
7055 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
7056 RefExpr->isInstantiationDependent() ||
7057 RefExpr->containsUnexpandedParameterPack()) {
7058 // It will be analyzed later.
7059 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007060 Privates.push_back(nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007061 LHSs.push_back(nullptr);
7062 RHSs.push_back(nullptr);
7063 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007064 continue;
7065 }
7066
7067 auto ELoc = RefExpr->getExprLoc();
7068 auto ERange = RefExpr->getSourceRange();
7069 // OpenMP [2.1, C/C++]
7070 // A list item is a variable or array section, subject to the restrictions
7071 // specified in Section 2.4 on page 42 and in each of the sections
7072 // describing clauses and directives for which a list appears.
7073 // OpenMP [2.14.3.3, Restrictions, p.1]
7074 // A variable that is part of another variable (as an array or
7075 // structure element) cannot appear in a private clause.
Alexey Bataeva1764212015-09-30 09:22:36 +00007076 auto *DE = dyn_cast<DeclRefExpr>(RefExpr);
7077 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr);
7078 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr);
7079 if (!ASE && !OASE && (!DE || !isa<VarDecl>(DE->getDecl()))) {
7080 Diag(ELoc, diag::err_omp_expected_var_name_or_array_item) << ERange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007081 continue;
7082 }
Alexey Bataeva1764212015-09-30 09:22:36 +00007083 QualType Type;
7084 VarDecl *VD = nullptr;
7085 if (DE) {
7086 auto D = DE->getDecl();
7087 VD = cast<VarDecl>(D);
7088 Type = VD->getType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007089 } else if (ASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007090 Type = ASE->getType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007091 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
7092 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7093 Base = TempASE->getBase()->IgnoreParenImpCasts();
7094 DE = dyn_cast<DeclRefExpr>(Base);
7095 if (DE)
7096 VD = dyn_cast<VarDecl>(DE->getDecl());
7097 if (!VD) {
7098 Diag(Base->getExprLoc(), diag::err_omp_expected_base_var_name)
7099 << 0 << Base->getSourceRange();
7100 continue;
7101 }
7102 } else if (OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007103 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
7104 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
7105 Type = ATy->getElementType();
7106 else
7107 Type = BaseType->getPointeeType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007108 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
7109 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
7110 Base = TempOASE->getBase()->IgnoreParenImpCasts();
7111 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7112 Base = TempASE->getBase()->IgnoreParenImpCasts();
7113 DE = dyn_cast<DeclRefExpr>(Base);
7114 if (DE)
7115 VD = dyn_cast<VarDecl>(DE->getDecl());
7116 if (!VD) {
7117 Diag(Base->getExprLoc(), diag::err_omp_expected_base_var_name)
7118 << 1 << Base->getSourceRange();
7119 continue;
7120 }
Alexey Bataeva1764212015-09-30 09:22:36 +00007121 }
7122
Alexey Bataevc5e02582014-06-16 07:08:35 +00007123 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7124 // A variable that appears in a private clause must not have an incomplete
7125 // type or a reference type.
7126 if (RequireCompleteType(ELoc, Type,
7127 diag::err_omp_reduction_incomplete_type))
7128 continue;
7129 // OpenMP [2.14.3.6, reduction clause, Restrictions]
7130 // Arrays may not appear in a reduction clause.
7131 if (Type.getNonReferenceType()->isArrayType()) {
7132 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007133 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007134 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7135 VarDecl::DeclarationOnly;
7136 Diag(VD->getLocation(),
7137 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7138 << VD;
7139 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007140 continue;
7141 }
7142 // OpenMP [2.14.3.6, reduction clause, Restrictions]
7143 // A list item that appears in a reduction clause must not be
7144 // const-qualified.
7145 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007146 Diag(ELoc, diag::err_omp_const_reduction_list_item)
Alexey Bataevc5e02582014-06-16 07:08:35 +00007147 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007148 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007149 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7150 VarDecl::DeclarationOnly;
7151 Diag(VD->getLocation(),
7152 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7153 << VD;
7154 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007155 continue;
7156 }
7157 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
7158 // If a list-item is a reference type then it must bind to the same object
7159 // for all threads of the team.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007160 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007161 VarDecl *VDDef = VD->getDefinition();
7162 if (Type->isReferenceType() && VDDef) {
7163 DSARefChecker Check(DSAStack);
7164 if (Check.Visit(VDDef->getInit())) {
7165 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
7166 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
7167 continue;
7168 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007169 }
7170 }
7171 // OpenMP [2.14.3.6, reduction clause, Restrictions]
7172 // The type of a list item that appears in a reduction clause must be valid
7173 // for the reduction-identifier. For a max or min reduction in C, the type
7174 // of the list item must be an allowed arithmetic data type: char, int,
7175 // float, double, or _Bool, possibly modified with long, short, signed, or
7176 // unsigned. For a max or min reduction in C++, the type of the list item
7177 // must be an allowed arithmetic data type: char, wchar_t, int, float,
7178 // double, or bool, possibly modified with long, short, signed, or unsigned.
7179 if ((BOK == BO_GT || BOK == BO_LT) &&
7180 !(Type->isScalarType() ||
7181 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
7182 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
7183 << getLangOpts().CPlusPlus;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007184 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007185 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7186 VarDecl::DeclarationOnly;
7187 Diag(VD->getLocation(),
7188 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7189 << VD;
7190 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007191 continue;
7192 }
7193 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
7194 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
7195 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007196 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007197 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7198 VarDecl::DeclarationOnly;
7199 Diag(VD->getLocation(),
7200 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7201 << VD;
7202 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007203 continue;
7204 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007205 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
7206 // in a Construct]
7207 // Variables with the predetermined data-sharing attributes may not be
7208 // listed in data-sharing attributes clauses, except for the cases
7209 // listed below. For these exceptions only, listing a predetermined
7210 // variable in a data-sharing attribute clause is allowed and overrides
7211 // the variable's predetermined data-sharing attributes.
7212 // OpenMP [2.14.3.6, Restrictions, p.3]
7213 // Any number of reduction clauses can be specified on the directive,
7214 // but a list item can appear only once in the reduction clauses for that
7215 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +00007216 DSAStackTy::DSAVarData DVar;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007217 DVar = DSAStack->getTopDSA(VD, false);
7218 if (DVar.CKind == OMPC_reduction) {
7219 Diag(ELoc, diag::err_omp_once_referenced)
7220 << getOpenMPClauseName(OMPC_reduction);
7221 if (DVar.RefExpr) {
7222 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007223 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007224 } else if (DVar.CKind != OMPC_unknown) {
7225 Diag(ELoc, diag::err_omp_wrong_dsa)
7226 << getOpenMPClauseName(DVar.CKind)
7227 << getOpenMPClauseName(OMPC_reduction);
7228 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7229 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007230 }
7231
7232 // OpenMP [2.14.3.6, Restrictions, p.1]
7233 // A list item that appears in a reduction clause of a worksharing
7234 // construct must be shared in the parallel regions to which any of the
7235 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007236 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
7237 if (isOpenMPWorksharingDirective(CurrDir) &&
7238 !isOpenMPParallelDirective(CurrDir)) {
7239 DVar = DSAStack->getImplicitDSA(VD, true);
7240 if (DVar.CKind != OMPC_shared) {
7241 Diag(ELoc, diag::err_omp_required_access)
7242 << getOpenMPClauseName(OMPC_reduction)
7243 << getOpenMPClauseName(OMPC_shared);
7244 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7245 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +00007246 }
7247 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007248
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007249 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007250 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
7251 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
7252 auto *RHSVD = buildVarDecl(*this, ELoc, Type, VD->getName(),
7253 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
7254 auto PrivateTy = Type;
7255 if (OASE) {
7256 // For array sections only:
7257 // Create pseudo array type for private copy. The size for this array will
7258 // be generated during codegen.
7259 // For array subscripts or single variables Private Ty is the same as Type
7260 // (type of the variable or single array element).
7261 PrivateTy = Context.getVariableArrayType(
7262 Type, new (Context) OpaqueValueExpr(SourceLocation(),
7263 Context.getSizeType(), VK_RValue),
7264 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
7265 }
7266 // Private copy.
7267 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, VD->getName(),
7268 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007269 // Add initializer for private variable.
7270 Expr *Init = nullptr;
7271 switch (BOK) {
7272 case BO_Add:
7273 case BO_Xor:
7274 case BO_Or:
7275 case BO_LOr:
7276 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
7277 if (Type->isScalarType() || Type->isAnyComplexType()) {
7278 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00007279 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007280 break;
7281 case BO_Mul:
7282 case BO_LAnd:
7283 if (Type->isScalarType() || Type->isAnyComplexType()) {
7284 // '*' and '&&' reduction ops - initializer is '1'.
7285 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
7286 }
7287 break;
7288 case BO_And: {
7289 // '&' reduction op - initializer is '~0'.
7290 QualType OrigType = Type;
7291 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) {
7292 Type = ComplexTy->getElementType();
7293 }
7294 if (Type->isRealFloatingType()) {
7295 llvm::APFloat InitValue =
7296 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
7297 /*isIEEE=*/true);
7298 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
7299 Type, ELoc);
7300 } else if (Type->isScalarType()) {
7301 auto Size = Context.getTypeSize(Type);
7302 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
7303 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
7304 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
7305 }
7306 if (Init && OrigType->isAnyComplexType()) {
7307 // Init = 0xFFFF + 0xFFFFi;
7308 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
7309 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
7310 }
7311 Type = OrigType;
7312 break;
7313 }
7314 case BO_LT:
7315 case BO_GT: {
7316 // 'min' reduction op - initializer is 'Largest representable number in
7317 // the reduction list item type'.
7318 // 'max' reduction op - initializer is 'Least representable number in
7319 // the reduction list item type'.
7320 if (Type->isIntegerType() || Type->isPointerType()) {
7321 bool IsSigned = Type->hasSignedIntegerRepresentation();
7322 auto Size = Context.getTypeSize(Type);
7323 QualType IntTy =
7324 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
7325 llvm::APInt InitValue =
7326 (BOK != BO_LT)
7327 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
7328 : llvm::APInt::getMinValue(Size)
7329 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
7330 : llvm::APInt::getMaxValue(Size);
7331 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
7332 if (Type->isPointerType()) {
7333 // Cast to pointer type.
7334 auto CastExpr = BuildCStyleCastExpr(
7335 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
7336 SourceLocation(), Init);
7337 if (CastExpr.isInvalid())
7338 continue;
7339 Init = CastExpr.get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00007340 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007341 } else if (Type->isRealFloatingType()) {
7342 llvm::APFloat InitValue = llvm::APFloat::getLargest(
7343 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
7344 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
7345 Type, ELoc);
7346 }
7347 break;
7348 }
7349 case BO_PtrMemD:
7350 case BO_PtrMemI:
7351 case BO_MulAssign:
7352 case BO_Div:
7353 case BO_Rem:
7354 case BO_Sub:
7355 case BO_Shl:
7356 case BO_Shr:
7357 case BO_LE:
7358 case BO_GE:
7359 case BO_EQ:
7360 case BO_NE:
7361 case BO_AndAssign:
7362 case BO_XorAssign:
7363 case BO_OrAssign:
7364 case BO_Assign:
7365 case BO_AddAssign:
7366 case BO_SubAssign:
7367 case BO_DivAssign:
7368 case BO_RemAssign:
7369 case BO_ShlAssign:
7370 case BO_ShrAssign:
7371 case BO_Comma:
7372 llvm_unreachable("Unexpected reduction operation");
7373 }
7374 if (Init) {
7375 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
7376 /*TypeMayContainAuto=*/false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007377 } else
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007378 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007379 if (!RHSVD->hasInit()) {
7380 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
7381 << ReductionIdRange;
Alexey Bataeva1764212015-09-30 09:22:36 +00007382 if (VD) {
7383 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7384 VarDecl::DeclarationOnly;
7385 Diag(VD->getLocation(),
7386 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7387 << VD;
7388 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007389 continue;
7390 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007391 // Store initializer for single element in private copy. Will be used during
7392 // codegen.
7393 PrivateVD->setInit(RHSVD->getInit());
7394 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataev39f915b82015-05-08 10:41:21 +00007395 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
7396 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007397 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007398 ExprResult ReductionOp =
7399 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
7400 LHSDRE, RHSDRE);
7401 if (ReductionOp.isUsable()) {
Alexey Bataev69a47792015-05-07 03:54:03 +00007402 if (BOK != BO_LT && BOK != BO_GT) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007403 ReductionOp =
7404 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
7405 BO_Assign, LHSDRE, ReductionOp.get());
7406 } else {
7407 auto *ConditionalOp = new (Context) ConditionalOperator(
7408 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
7409 RHSDRE, Type, VK_LValue, OK_Ordinary);
7410 ReductionOp =
7411 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
7412 BO_Assign, LHSDRE, ConditionalOp);
7413 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007414 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00007415 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007416 if (ReductionOp.isInvalid())
7417 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007418
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007419 DSAStack->addDSA(VD, DE, OMPC_reduction);
Alexey Bataeva1764212015-09-30 09:22:36 +00007420 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007421 Privates.push_back(PrivateDRE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007422 LHSs.push_back(LHSDRE);
7423 RHSs.push_back(RHSDRE);
7424 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00007425 }
7426
7427 if (Vars.empty())
7428 return nullptr;
7429
7430 return OMPReductionClause::Create(
7431 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007432 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
7433 LHSs, RHSs, ReductionOps);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007434}
7435
Alexey Bataev182227b2015-08-20 10:54:39 +00007436OMPClause *Sema::ActOnOpenMPLinearClause(
7437 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
7438 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
7439 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00007440 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007441 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00007442 SmallVector<Expr *, 8> Inits;
Alexey Bataev182227b2015-08-20 10:54:39 +00007443 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
7444 LinKind == OMPC_LINEAR_unknown) {
7445 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
7446 LinKind = OMPC_LINEAR_val;
7447 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007448 for (auto &RefExpr : VarList) {
7449 assert(RefExpr && "NULL expr in OpenMP linear clause.");
7450 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00007451 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007452 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007453 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00007454 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00007455 continue;
7456 }
7457
7458 // OpenMP [2.14.3.7, linear clause]
7459 // A list item that appears in a linear clause is subject to the private
7460 // clause semantics described in Section 2.14.3.3 on page 159 except as
7461 // noted. In addition, the value of the new list item on each iteration
7462 // of the associated loop(s) corresponds to the value of the original
7463 // list item before entering the construct plus the logical number of
7464 // the iteration times linear-step.
7465
Alexey Bataeved09d242014-05-28 05:53:51 +00007466 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00007467 // OpenMP [2.1, C/C++]
7468 // A list item is a variable name.
7469 // OpenMP [2.14.3.3, Restrictions, p.1]
7470 // A variable that is part of another variable (as an array or
7471 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00007472 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00007473 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00007474 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00007475 continue;
7476 }
7477
7478 VarDecl *VD = cast<VarDecl>(DE->getDecl());
7479
7480 // OpenMP [2.14.3.7, linear clause]
7481 // A list-item cannot appear in more than one linear clause.
7482 // A list-item that appears in a linear clause cannot appear in any
7483 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007484 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00007485 if (DVar.RefExpr) {
7486 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7487 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007488 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00007489 continue;
7490 }
7491
7492 QualType QType = VD->getType();
7493 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
7494 // It will be analyzed later.
7495 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007496 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00007497 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00007498 continue;
7499 }
7500
7501 // A variable must not have an incomplete type or a reference type.
7502 if (RequireCompleteType(ELoc, QType,
7503 diag::err_omp_linear_incomplete_type)) {
7504 continue;
7505 }
Alexey Bataev1185e192015-08-20 12:15:57 +00007506 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
7507 !QType->isReferenceType()) {
7508 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
7509 << QType << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
7510 continue;
7511 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007512 QType = QType.getNonReferenceType();
Alexander Musman8dba6642014-04-22 13:09:42 +00007513
7514 // A list item must not be const-qualified.
7515 if (QType.isConstant(Context)) {
7516 Diag(ELoc, diag::err_omp_const_variable)
7517 << getOpenMPClauseName(OMPC_linear);
7518 bool IsDecl =
7519 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7520 Diag(VD->getLocation(),
7521 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7522 << VD;
7523 continue;
7524 }
7525
7526 // A list item must be of integral or pointer type.
7527 QType = QType.getUnqualifiedType().getCanonicalType();
7528 const Type *Ty = QType.getTypePtrOrNull();
7529 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
7530 !Ty->isPointerType())) {
7531 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
7532 bool IsDecl =
7533 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7534 Diag(VD->getLocation(),
7535 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7536 << VD;
7537 continue;
7538 }
7539
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007540 // Build private copy of original var.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007541 auto *Private = buildVarDecl(*this, ELoc, QType, VD->getName(),
7542 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007543 auto *PrivateRef = buildDeclRefExpr(
7544 *this, Private, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman3276a272015-03-21 10:12:56 +00007545 // Build var to save initial value.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007546 VarDecl *Init = buildVarDecl(*this, ELoc, QType, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00007547 Expr *InitExpr;
7548 if (LinKind == OMPC_LINEAR_uval)
7549 InitExpr = VD->getInit();
7550 else
7551 InitExpr = DE;
7552 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexander Musman3276a272015-03-21 10:12:56 +00007553 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007554 auto InitRef = buildDeclRefExpr(
7555 *this, Init, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman8dba6642014-04-22 13:09:42 +00007556 DSAStack->addDSA(VD, DE, OMPC_linear);
7557 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007558 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00007559 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00007560 }
7561
7562 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007563 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00007564
7565 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00007566 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00007567 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
7568 !Step->isInstantiationDependent() &&
7569 !Step->containsUnexpandedParameterPack()) {
7570 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00007571 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00007572 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007573 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00007574 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00007575
Alexander Musman3276a272015-03-21 10:12:56 +00007576 // Build var to save the step value.
7577 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007578 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00007579 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007580 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00007581 ExprResult CalcStep =
7582 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007583 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00007584
Alexander Musman8dba6642014-04-22 13:09:42 +00007585 // Warn about zero linear step (it would be probably better specified as
7586 // making corresponding variables 'const').
7587 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00007588 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
7589 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00007590 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
7591 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00007592 if (!IsConstant && CalcStep.isUsable()) {
7593 // Calculate the step beforehand instead of doing this on each iteration.
7594 // (This is not used if the number of iterations may be kfold-ed).
7595 CalcStepExpr = CalcStep.get();
7596 }
Alexander Musman8dba6642014-04-22 13:09:42 +00007597 }
7598
Alexey Bataev182227b2015-08-20 10:54:39 +00007599 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
7600 ColonLoc, EndLoc, Vars, Privates, Inits,
7601 StepExpr, CalcStepExpr);
Alexander Musman3276a272015-03-21 10:12:56 +00007602}
7603
7604static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
7605 Expr *NumIterations, Sema &SemaRef,
7606 Scope *S) {
7607 // Walk the vars and build update/final expressions for the CodeGen.
7608 SmallVector<Expr *, 8> Updates;
7609 SmallVector<Expr *, 8> Finals;
7610 Expr *Step = Clause.getStep();
7611 Expr *CalcStep = Clause.getCalcStep();
7612 // OpenMP [2.14.3.7, linear clause]
7613 // If linear-step is not specified it is assumed to be 1.
7614 if (Step == nullptr)
7615 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
7616 else if (CalcStep)
7617 Step = cast<BinaryOperator>(CalcStep)->getLHS();
7618 bool HasErrors = false;
7619 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007620 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00007621 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00007622 for (auto &RefExpr : Clause.varlists()) {
7623 Expr *InitExpr = *CurInit;
7624
7625 // Build privatized reference to the current linear var.
7626 auto DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00007627 Expr *CapturedRef;
7628 if (LinKind == OMPC_LINEAR_uval)
7629 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
7630 else
7631 CapturedRef =
7632 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
7633 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
7634 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00007635
7636 // Build update: Var = InitExpr + IV * Step
7637 ExprResult Update =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007638 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
Alexander Musman3276a272015-03-21 10:12:56 +00007639 InitExpr, IV, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007640 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
7641 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00007642
7643 // Build final: Var = InitExpr + NumIterations * Step
7644 ExprResult Final =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007645 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
Alexey Bataev39f915b82015-05-08 10:41:21 +00007646 InitExpr, NumIterations, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007647 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
7648 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00007649 if (!Update.isUsable() || !Final.isUsable()) {
7650 Updates.push_back(nullptr);
7651 Finals.push_back(nullptr);
7652 HasErrors = true;
7653 } else {
7654 Updates.push_back(Update.get());
7655 Finals.push_back(Final.get());
7656 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007657 ++CurInit, ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00007658 }
7659 Clause.setUpdates(Updates);
7660 Clause.setFinals(Finals);
7661 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00007662}
7663
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007664OMPClause *Sema::ActOnOpenMPAlignedClause(
7665 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
7666 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
7667
7668 SmallVector<Expr *, 8> Vars;
7669 for (auto &RefExpr : VarList) {
7670 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
7671 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7672 // It will be analyzed later.
7673 Vars.push_back(RefExpr);
7674 continue;
7675 }
7676
7677 SourceLocation ELoc = RefExpr->getExprLoc();
7678 // OpenMP [2.1, C/C++]
7679 // A list item is a variable name.
7680 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
7681 if (!DE || !isa<VarDecl>(DE->getDecl())) {
7682 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
7683 continue;
7684 }
7685
7686 VarDecl *VD = cast<VarDecl>(DE->getDecl());
7687
7688 // OpenMP [2.8.1, simd construct, Restrictions]
7689 // The type of list items appearing in the aligned clause must be
7690 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007691 QualType QType = VD->getType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007692 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007693 const Type *Ty = QType.getTypePtrOrNull();
7694 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
7695 !Ty->isPointerType())) {
7696 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
7697 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
7698 bool IsDecl =
7699 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7700 Diag(VD->getLocation(),
7701 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7702 << VD;
7703 continue;
7704 }
7705
7706 // OpenMP [2.8.1, simd construct, Restrictions]
7707 // A list-item cannot appear in more than one aligned clause.
7708 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
7709 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
7710 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
7711 << getOpenMPClauseName(OMPC_aligned);
7712 continue;
7713 }
7714
7715 Vars.push_back(DE);
7716 }
7717
7718 // OpenMP [2.8.1, simd construct, Description]
7719 // The parameter of the aligned clause, alignment, must be a constant
7720 // positive integer expression.
7721 // If no optional parameter is specified, implementation-defined default
7722 // alignments for SIMD instructions on the target platforms are assumed.
7723 if (Alignment != nullptr) {
7724 ExprResult AlignResult =
7725 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
7726 if (AlignResult.isInvalid())
7727 return nullptr;
7728 Alignment = AlignResult.get();
7729 }
7730 if (Vars.empty())
7731 return nullptr;
7732
7733 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
7734 EndLoc, Vars, Alignment);
7735}
7736
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007737OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
7738 SourceLocation StartLoc,
7739 SourceLocation LParenLoc,
7740 SourceLocation EndLoc) {
7741 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007742 SmallVector<Expr *, 8> SrcExprs;
7743 SmallVector<Expr *, 8> DstExprs;
7744 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00007745 for (auto &RefExpr : VarList) {
7746 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
7747 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007748 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007749 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007750 SrcExprs.push_back(nullptr);
7751 DstExprs.push_back(nullptr);
7752 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007753 continue;
7754 }
7755
Alexey Bataeved09d242014-05-28 05:53:51 +00007756 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007757 // 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.
Alexey Bataeved09d242014-05-28 05:53:51 +00007761 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007762 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00007763 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007764 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 Bataevf56f98c2015-04-16 05:39:01 +00007774 SrcExprs.push_back(nullptr);
7775 DstExprs.push_back(nullptr);
7776 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007777 continue;
7778 }
7779
7780 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
7781 // A list item that appears in a copyin clause must be threadprivate.
7782 if (!DSAStack->isThreadPrivate(VD)) {
7783 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00007784 << getOpenMPClauseName(OMPC_copyin)
7785 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007786 continue;
7787 }
7788
7789 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
7790 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00007791 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007792 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007793 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007794 auto *SrcVD =
7795 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
7796 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007797 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007798 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
7799 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007800 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
7801 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007802 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007803 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007804 // For arrays generate assignment operation for single element and replace
7805 // it by the original array element in CodeGen.
7806 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
7807 PseudoDstExpr, PseudoSrcExpr);
7808 if (AssignmentOp.isInvalid())
7809 continue;
7810 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
7811 /*DiscardedValue=*/true);
7812 if (AssignmentOp.isInvalid())
7813 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007814
7815 DSAStack->addDSA(VD, DE, OMPC_copyin);
7816 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007817 SrcExprs.push_back(PseudoSrcExpr);
7818 DstExprs.push_back(PseudoDstExpr);
7819 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007820 }
7821
Alexey Bataeved09d242014-05-28 05:53:51 +00007822 if (Vars.empty())
7823 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007824
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007825 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
7826 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007827}
7828
Alexey Bataevbae9a792014-06-27 10:37:06 +00007829OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
7830 SourceLocation StartLoc,
7831 SourceLocation LParenLoc,
7832 SourceLocation EndLoc) {
7833 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00007834 SmallVector<Expr *, 8> SrcExprs;
7835 SmallVector<Expr *, 8> DstExprs;
7836 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00007837 for (auto &RefExpr : VarList) {
7838 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
7839 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7840 // It will be analyzed later.
7841 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00007842 SrcExprs.push_back(nullptr);
7843 DstExprs.push_back(nullptr);
7844 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00007845 continue;
7846 }
7847
7848 SourceLocation ELoc = RefExpr->getExprLoc();
7849 // OpenMP [2.1, C/C++]
7850 // A list item is a variable name.
7851 // OpenMP [2.14.4.1, Restrictions, p.1]
7852 // A list item that appears in a copyin clause must be threadprivate.
7853 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
7854 if (!DE || !isa<VarDecl>(DE->getDecl())) {
7855 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
7856 continue;
7857 }
7858
7859 Decl *D = DE->getDecl();
7860 VarDecl *VD = cast<VarDecl>(D);
7861
7862 QualType Type = VD->getType();
7863 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
7864 // It will be analyzed later.
7865 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00007866 SrcExprs.push_back(nullptr);
7867 DstExprs.push_back(nullptr);
7868 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00007869 continue;
7870 }
7871
7872 // OpenMP [2.14.4.2, Restrictions, p.2]
7873 // A list item that appears in a copyprivate clause may not appear in a
7874 // private or firstprivate clause on the single construct.
7875 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007876 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00007877 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
7878 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00007879 Diag(ELoc, diag::err_omp_wrong_dsa)
7880 << getOpenMPClauseName(DVar.CKind)
7881 << getOpenMPClauseName(OMPC_copyprivate);
7882 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7883 continue;
7884 }
7885
7886 // OpenMP [2.11.4.2, Restrictions, p.1]
7887 // All list items that appear in a copyprivate clause must be either
7888 // threadprivate or private in the enclosing context.
7889 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007890 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00007891 if (DVar.CKind == OMPC_shared) {
7892 Diag(ELoc, diag::err_omp_required_access)
7893 << getOpenMPClauseName(OMPC_copyprivate)
7894 << "threadprivate or private in the enclosing context";
7895 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7896 continue;
7897 }
7898 }
7899 }
7900
Alexey Bataev7a3e5852015-05-19 08:19:24 +00007901 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007902 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00007903 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007904 << getOpenMPClauseName(OMPC_copyprivate) << Type
7905 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00007906 bool IsDecl =
7907 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7908 Diag(VD->getLocation(),
7909 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7910 << VD;
7911 continue;
7912 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007913
Alexey Bataevbae9a792014-06-27 10:37:06 +00007914 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
7915 // A variable of class type (or array thereof) that appears in a
7916 // copyin clause requires an accessible, unambiguous copy assignment
7917 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007918 Type = Context.getBaseElementType(Type.getNonReferenceType())
7919 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +00007920 auto *SrcVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007921 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.src",
7922 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00007923 auto *PseudoSrcExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007924 buildDeclRefExpr(*this, SrcVD, Type, DE->getExprLoc());
Alexey Bataev420d45b2015-04-14 05:11:24 +00007925 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007926 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.dst",
7927 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00007928 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007929 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataeva63048e2015-03-23 06:18:07 +00007930 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
7931 PseudoDstExpr, PseudoSrcExpr);
7932 if (AssignmentOp.isInvalid())
7933 continue;
7934 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
7935 /*DiscardedValue=*/true);
7936 if (AssignmentOp.isInvalid())
7937 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00007938
7939 // No need to mark vars as copyprivate, they are already threadprivate or
7940 // implicitly private.
7941 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00007942 SrcExprs.push_back(PseudoSrcExpr);
7943 DstExprs.push_back(PseudoDstExpr);
7944 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +00007945 }
7946
7947 if (Vars.empty())
7948 return nullptr;
7949
Alexey Bataeva63048e2015-03-23 06:18:07 +00007950 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
7951 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00007952}
7953
Alexey Bataev6125da92014-07-21 11:26:11 +00007954OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
7955 SourceLocation StartLoc,
7956 SourceLocation LParenLoc,
7957 SourceLocation EndLoc) {
7958 if (VarList.empty())
7959 return nullptr;
7960
7961 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
7962}
Alexey Bataevdea47612014-07-23 07:46:59 +00007963
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007964OMPClause *
7965Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
7966 SourceLocation DepLoc, SourceLocation ColonLoc,
7967 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
7968 SourceLocation LParenLoc, SourceLocation EndLoc) {
7969 if (DepKind == OMPC_DEPEND_unknown) {
7970 std::string Values;
7971 std::string Sep(", ");
7972 for (unsigned i = 0; i < OMPC_DEPEND_unknown; ++i) {
7973 Values += "'";
7974 Values += getOpenMPSimpleClauseTypeName(OMPC_depend, i);
7975 Values += "'";
7976 switch (i) {
7977 case OMPC_DEPEND_unknown - 2:
7978 Values += " or ";
7979 break;
7980 case OMPC_DEPEND_unknown - 1:
7981 break;
7982 default:
7983 Values += Sep;
7984 break;
7985 }
7986 }
7987 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
7988 << Values << getOpenMPClauseName(OMPC_depend);
7989 return nullptr;
7990 }
7991 SmallVector<Expr *, 8> Vars;
7992 for (auto &RefExpr : VarList) {
7993 assert(RefExpr && "NULL expr in OpenMP shared clause.");
7994 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7995 // It will be analyzed later.
7996 Vars.push_back(RefExpr);
7997 continue;
7998 }
7999
8000 SourceLocation ELoc = RefExpr->getExprLoc();
8001 // OpenMP [2.11.1.1, Restrictions, p.3]
8002 // A variable that is part of another variable (such as a field of a
8003 // structure) but is not an array element or an array section cannot appear
8004 // in a depend clause.
8005 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
Alexey Bataev1a3320e2015-08-25 14:24:04 +00008006 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
8007 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
8008 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
8009 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
8010 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008011 (ASE && !ASE->getBase()->getType()->isAnyPointerType() &&
8012 !ASE->getBase()->getType()->isArrayType())) {
8013 Diag(ELoc, diag::err_omp_expected_var_name_or_array_item)
8014 << RefExpr->getSourceRange();
8015 continue;
8016 }
8017
8018 Vars.push_back(RefExpr->IgnoreParenImpCasts());
8019 }
8020
8021 if (Vars.empty())
8022 return nullptr;
8023
8024 return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
8025 DepLoc, ColonLoc, Vars);
8026}
Michael Wonge710d542015-08-07 16:16:36 +00008027
8028OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
8029 SourceLocation LParenLoc,
8030 SourceLocation EndLoc) {
8031 Expr *ValExpr = Device;
Michael Wonge710d542015-08-07 16:16:36 +00008032
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008033 // OpenMP [2.9.1, Restrictions]
8034 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00008035 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
8036 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008037 return nullptr;
8038
Michael Wonge710d542015-08-07 16:16:36 +00008039 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8040}
Kelvin Li0bff7af2015-11-23 05:32:03 +00008041
8042static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
8043 DSAStackTy *Stack, CXXRecordDecl *RD) {
8044 if (!RD || RD->isInvalidDecl())
8045 return true;
8046
Alexey Bataevc9bd03d2015-12-17 06:55:08 +00008047 if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
8048 if (auto *CTD = CTSD->getSpecializedTemplate())
8049 RD = CTD->getTemplatedDecl();
Kelvin Li0bff7af2015-11-23 05:32:03 +00008050 auto QTy = SemaRef.Context.getRecordType(RD);
8051 if (RD->isDynamicClass()) {
8052 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8053 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
8054 return false;
8055 }
8056 auto *DC = RD;
8057 bool IsCorrect = true;
8058 for (auto *I : DC->decls()) {
8059 if (I) {
8060 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
8061 if (MD->isStatic()) {
8062 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8063 SemaRef.Diag(MD->getLocation(),
8064 diag::note_omp_static_member_in_target);
8065 IsCorrect = false;
8066 }
8067 } else if (auto *VD = dyn_cast<VarDecl>(I)) {
8068 if (VD->isStaticDataMember()) {
8069 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8070 SemaRef.Diag(VD->getLocation(),
8071 diag::note_omp_static_member_in_target);
8072 IsCorrect = false;
8073 }
8074 }
8075 }
8076 }
8077
8078 for (auto &I : RD->bases()) {
8079 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
8080 I.getType()->getAsCXXRecordDecl()))
8081 IsCorrect = false;
8082 }
8083 return IsCorrect;
8084}
8085
8086static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
8087 DSAStackTy *Stack, QualType QTy) {
8088 NamedDecl *ND;
8089 if (QTy->isIncompleteType(&ND)) {
8090 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
8091 return false;
8092 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
8093 if (!RD->isInvalidDecl() &&
8094 !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
8095 return false;
8096 }
8097 return true;
8098}
8099
8100OMPClause *Sema::ActOnOpenMPMapClause(
8101 OpenMPMapClauseKind MapTypeModifier, OpenMPMapClauseKind MapType,
8102 SourceLocation MapLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VarList,
8103 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
8104 SmallVector<Expr *, 4> Vars;
8105
8106 for (auto &RE : VarList) {
8107 assert(RE && "Null expr in omp map");
8108 if (isa<DependentScopeDeclRefExpr>(RE)) {
8109 // It will be analyzed later.
8110 Vars.push_back(RE);
8111 continue;
8112 }
8113 SourceLocation ELoc = RE->getExprLoc();
8114
8115 // OpenMP [2.14.5, Restrictions]
8116 // A variable that is part of another variable (such as field of a
8117 // structure) but is not an array element or an array section cannot appear
8118 // in a map clause.
8119 auto *VE = RE->IgnoreParenLValueCasts();
8120
8121 if (VE->isValueDependent() || VE->isTypeDependent() ||
8122 VE->isInstantiationDependent() ||
8123 VE->containsUnexpandedParameterPack()) {
8124 // It will be analyzed later.
8125 Vars.push_back(RE);
8126 continue;
8127 }
8128
8129 auto *SimpleExpr = RE->IgnoreParenCasts();
8130 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
8131 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
8132 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
8133
8134 if (!RE->IgnoreParenImpCasts()->isLValue() ||
8135 (!OASE && !ASE && !DE) ||
8136 (DE && !isa<VarDecl>(DE->getDecl())) ||
8137 (ASE && !ASE->getBase()->getType()->isAnyPointerType() &&
8138 !ASE->getBase()->getType()->isArrayType())) {
8139 Diag(ELoc, diag::err_omp_expected_var_name_or_array_item)
8140 << RE->getSourceRange();
8141 continue;
8142 }
8143
8144 Decl *D = nullptr;
8145 if (DE) {
8146 D = DE->getDecl();
8147 } else if (ASE) {
8148 auto *B = ASE->getBase()->IgnoreParenCasts();
8149 D = dyn_cast<DeclRefExpr>(B)->getDecl();
8150 } else if (OASE) {
8151 auto *B = OASE->getBase();
8152 D = dyn_cast<DeclRefExpr>(B)->getDecl();
8153 }
8154 assert(D && "Null decl on map clause.");
8155 auto *VD = cast<VarDecl>(D);
8156
8157 // OpenMP [2.14.5, Restrictions, p.8]
8158 // threadprivate variables cannot appear in a map clause.
8159 if (DSAStack->isThreadPrivate(VD)) {
8160 auto DVar = DSAStack->getTopDSA(VD, false);
8161 Diag(ELoc, diag::err_omp_threadprivate_in_map);
8162 ReportOriginalDSA(*this, DSAStack, VD, DVar);
8163 continue;
8164 }
8165
8166 // OpenMP [2.14.5, Restrictions, p.2]
8167 // At most one list item can be an array item derived from a given variable
8168 // in map clauses of the same construct.
8169 // OpenMP [2.14.5, Restrictions, p.3]
8170 // List items of map clauses in the same construct must not share original
8171 // storage.
8172 // OpenMP [2.14.5, Restrictions, C/C++, p.2]
8173 // A variable for which the type is pointer, reference to array, or
8174 // reference to pointer and an array section derived from that variable
8175 // must not appear as list items of map clauses of the same construct.
8176 DSAStackTy::MapInfo MI = DSAStack->IsMappedInCurrentRegion(VD);
8177 if (MI.RefExpr) {
8178 Diag(ELoc, diag::err_omp_map_shared_storage) << ELoc;
8179 Diag(MI.RefExpr->getExprLoc(), diag::note_used_here)
8180 << MI.RefExpr->getSourceRange();
8181 continue;
8182 }
8183
8184 // OpenMP [2.14.5, Restrictions, C/C++, p.3,4]
8185 // A variable for which the type is pointer, reference to array, or
8186 // reference to pointer must not appear as a list item if the enclosing
8187 // device data environment already contains an array section derived from
8188 // that variable.
8189 // An array section derived from a variable for which the type is pointer,
8190 // reference to array, or reference to pointer must not appear as a list
8191 // item if the enclosing device data environment already contains that
8192 // variable.
8193 QualType Type = VD->getType();
8194 MI = DSAStack->getMapInfoForVar(VD);
8195 if (MI.RefExpr && (isa<DeclRefExpr>(MI.RefExpr->IgnoreParenLValueCasts()) !=
8196 isa<DeclRefExpr>(VE)) &&
8197 (Type->isPointerType() || Type->isReferenceType())) {
8198 Diag(ELoc, diag::err_omp_map_shared_storage) << ELoc;
8199 Diag(MI.RefExpr->getExprLoc(), diag::note_used_here)
8200 << MI.RefExpr->getSourceRange();
8201 continue;
8202 }
8203
8204 // OpenMP [2.14.5, Restrictions, C/C++, p.7]
8205 // A list item must have a mappable type.
8206 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), *this,
8207 DSAStack, Type))
8208 continue;
8209
8210 Vars.push_back(RE);
8211 MI.RefExpr = RE;
8212 DSAStack->addMapInfoForVar(VD, MI);
8213 }
8214 if (Vars.empty())
8215 return nullptr;
8216
8217 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
8218 MapTypeModifier, MapType, MapLoc);
8219}
Kelvin Li099bb8c2015-11-24 20:50:12 +00008220
8221OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
8222 SourceLocation StartLoc,
8223 SourceLocation LParenLoc,
8224 SourceLocation EndLoc) {
8225 Expr *ValExpr = NumTeams;
Kelvin Li099bb8c2015-11-24 20:50:12 +00008226
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008227 // OpenMP [teams Constrcut, Restrictions]
8228 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00008229 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
8230 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008231 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +00008232
8233 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8234}
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008235
8236OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
8237 SourceLocation StartLoc,
8238 SourceLocation LParenLoc,
8239 SourceLocation EndLoc) {
8240 Expr *ValExpr = ThreadLimit;
8241
8242 // OpenMP [teams Constrcut, Restrictions]
8243 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00008244 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
8245 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008246 return nullptr;
8247
8248 return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc,
8249 EndLoc);
8250}
Alexey Bataeva0569352015-12-01 10:17:31 +00008251
8252OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
8253 SourceLocation StartLoc,
8254 SourceLocation LParenLoc,
8255 SourceLocation EndLoc) {
8256 Expr *ValExpr = Priority;
8257
8258 // OpenMP [2.9.1, task Constrcut]
8259 // The priority-value is a non-negative numerical scalar expression.
8260 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
8261 /*StrictlyPositive=*/false))
8262 return nullptr;
8263
8264 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8265}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008266
8267OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
8268 SourceLocation StartLoc,
8269 SourceLocation LParenLoc,
8270 SourceLocation EndLoc) {
8271 Expr *ValExpr = Grainsize;
8272
8273 // OpenMP [2.9.2, taskloop Constrcut]
8274 // The parameter of the grainsize clause must be a positive integer
8275 // expression.
8276 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
8277 /*StrictlyPositive=*/true))
8278 return nullptr;
8279
8280 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8281}
Alexey Bataev382967a2015-12-08 12:06:20 +00008282
8283OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
8284 SourceLocation StartLoc,
8285 SourceLocation LParenLoc,
8286 SourceLocation EndLoc) {
8287 Expr *ValExpr = NumTasks;
8288
8289 // OpenMP [2.9.2, taskloop Constrcut]
8290 // The parameter of the num_tasks clause must be a positive integer
8291 // expression.
8292 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
8293 /*StrictlyPositive=*/true))
8294 return nullptr;
8295
8296 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8297}
8298
Alexey Bataev28c75412015-12-15 08:19:24 +00008299OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
8300 SourceLocation LParenLoc,
8301 SourceLocation EndLoc) {
8302 // OpenMP [2.13.2, critical construct, Description]
8303 // ... where hint-expression is an integer constant expression that evaluates
8304 // to a valid lock hint.
8305 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
8306 if (HintExpr.isInvalid())
8307 return nullptr;
8308 return new (Context)
8309 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
8310}
8311