blob: 01f5d1053d6c588851b7bfb2c8177aaf754c9370 [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 Bataev758e55e2013-09-06 18:03:48 +0000606 if (IsConstant &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000607 !(SemaRef.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000608 // Variables with const-qualified type having no mutable member may be
609 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000610 DSAVarData DVarTemp = hasDSA(D, MatchesAnyClause(OMPC_firstprivate),
611 MatchesAlways(), FromParent);
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000612 if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
613 return DVar;
614
Alexey Bataev758e55e2013-09-06 18:03:48 +0000615 DVar.CKind = OMPC_shared;
616 return DVar;
617 }
618
Alexey Bataev758e55e2013-09-06 18:03:48 +0000619 // Explicitly specified attributes and local variables with predetermined
620 // attributes.
Alexey Bataevdffa93a2015-12-10 08:20:58 +0000621 auto StartI = std::next(Stack.rbegin());
622 auto EndI = std::prev(Stack.rend());
623 if (FromParent && StartI != EndI) {
624 StartI = std::next(StartI);
625 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000626 auto I = std::prev(StartI);
627 if (I->SharingMap.count(D)) {
628 DVar.RefExpr = I->SharingMap[D].RefExpr;
629 DVar.CKind = I->SharingMap[D].Attributes;
630 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000631 }
632
633 return DVar;
634}
635
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000636DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D, bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000637 D = D->getCanonicalDecl();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000638 auto StartI = Stack.rbegin();
639 auto EndI = std::prev(Stack.rend());
640 if (FromParent && StartI != EndI) {
641 StartI = std::next(StartI);
642 }
643 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000644}
645
Alexey Bataevf29276e2014-06-18 04:14:57 +0000646template <class ClausesPredicate, class DirectivesPredicate>
647DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, ClausesPredicate CPred,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000648 DirectivesPredicate DPred,
649 bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000650 D = D->getCanonicalDecl();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000651 auto StartI = std::next(Stack.rbegin());
652 auto EndI = std::prev(Stack.rend());
653 if (FromParent && StartI != EndI) {
654 StartI = std::next(StartI);
655 }
656 for (auto I = StartI, EE = EndI; I != EE; ++I) {
657 if (!DPred(I->Directive) && !isParallelOrTaskRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +0000658 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000659 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000660 if (CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000661 return DVar;
662 }
663 return DSAVarData();
664}
665
Alexey Bataevf29276e2014-06-18 04:14:57 +0000666template <class ClausesPredicate, class DirectivesPredicate>
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000667DSAStackTy::DSAVarData
668DSAStackTy::hasInnermostDSA(VarDecl *D, ClausesPredicate CPred,
669 DirectivesPredicate DPred, bool FromParent) {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000670 D = D->getCanonicalDecl();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000671 auto StartI = std::next(Stack.rbegin());
672 auto EndI = std::prev(Stack.rend());
673 if (FromParent && StartI != EndI) {
674 StartI = std::next(StartI);
675 }
676 for (auto I = StartI, EE = EndI; I != EE; ++I) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000677 if (!DPred(I->Directive))
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000678 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +0000679 DSAVarData DVar = getDSA(I, D);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000680 if (CPred(DVar.CKind))
Alexey Bataevc5e02582014-06-16 07:08:35 +0000681 return DVar;
682 return DSAVarData();
683 }
684 return DSAVarData();
685}
686
Alexey Bataevaac108a2015-06-23 04:51:00 +0000687bool DSAStackTy::hasExplicitDSA(
688 VarDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> &CPred,
689 unsigned Level) {
690 if (CPred(ClauseKindMode))
691 return true;
692 if (isClauseParsingMode())
693 ++Level;
694 D = D->getCanonicalDecl();
695 auto StartI = Stack.rbegin();
696 auto EndI = std::prev(Stack.rend());
NAKAMURA Takumi0332eda2015-06-23 10:01:20 +0000697 if (std::distance(StartI, EndI) <= (int)Level)
Alexey Bataevaac108a2015-06-23 04:51:00 +0000698 return false;
699 std::advance(StartI, Level);
700 return (StartI->SharingMap.count(D) > 0) && StartI->SharingMap[D].RefExpr &&
701 CPred(StartI->SharingMap[D].Attributes);
702}
703
Samuel Antao4be30e92015-10-02 17:14:03 +0000704bool DSAStackTy::hasExplicitDirective(
705 const llvm::function_ref<bool(OpenMPDirectiveKind)> &DPred,
706 unsigned Level) {
707 if (isClauseParsingMode())
708 ++Level;
709 auto StartI = Stack.rbegin();
710 auto EndI = std::prev(Stack.rend());
711 if (std::distance(StartI, EndI) <= (int)Level)
712 return false;
713 std::advance(StartI, Level);
714 return DPred(StartI->Directive);
715}
716
Alexander Musmand9ed09f2014-07-21 09:42:05 +0000717template <class NamedDirectivesPredicate>
718bool DSAStackTy::hasDirective(NamedDirectivesPredicate DPred, bool FromParent) {
719 auto StartI = std::next(Stack.rbegin());
720 auto EndI = std::prev(Stack.rend());
721 if (FromParent && StartI != EndI) {
722 StartI = std::next(StartI);
723 }
724 for (auto I = StartI, EE = EndI; I != EE; ++I) {
725 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
726 return true;
727 }
728 return false;
729}
730
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000731OpenMPDirectiveKind DSAStackTy::getDirectiveForScope(const Scope *S) const {
732 for (auto I = Stack.rbegin(), EE = Stack.rend(); I != EE; ++I)
733 if (I->CurScope == S)
734 return I->Directive;
735 return OMPD_unknown;
736}
737
Alexey Bataev758e55e2013-09-06 18:03:48 +0000738void Sema::InitDataSharingAttributesStack() {
739 VarDataSharingAttributesStack = new DSAStackTy(*this);
740}
741
742#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
743
Samuel Antao4af1b7b2015-12-02 17:44:43 +0000744bool Sema::IsOpenMPCapturedByRef(VarDecl *VD,
745 const CapturedRegionScopeInfo *RSI) {
746 assert(LangOpts.OpenMP && "OpenMP is not allowed");
747
748 auto &Ctx = getASTContext();
749 bool IsByRef = true;
750
751 // Find the directive that is associated with the provided scope.
752 auto DKind = DSAStack->getDirectiveForScope(RSI->TheScope);
753 auto Ty = VD->getType();
754
755 if (isOpenMPTargetDirective(DKind)) {
756 // This table summarizes how a given variable should be passed to the device
757 // given its type and the clauses where it appears. This table is based on
758 // the description in OpenMP 4.5 [2.10.4, target Construct] and
759 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
760 //
761 // =========================================================================
762 // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
763 // | |(tofrom:scalar)| | pvt | | | |
764 // =========================================================================
765 // | scl | | | | - | | bycopy|
766 // | scl | | - | x | - | - | bycopy|
767 // | scl | | x | - | - | - | null |
768 // | scl | x | | | - | | byref |
769 // | scl | x | - | x | - | - | bycopy|
770 // | scl | x | x | - | - | - | null |
771 // | scl | | - | - | - | x | byref |
772 // | scl | x | - | - | - | x | byref |
773 //
774 // | agg | n.a. | | | - | | byref |
775 // | agg | n.a. | - | x | - | - | byref |
776 // | agg | n.a. | x | - | - | - | null |
777 // | agg | n.a. | - | - | - | x | byref |
778 // | agg | n.a. | - | - | - | x[] | byref |
779 //
780 // | ptr | n.a. | | | - | | bycopy|
781 // | ptr | n.a. | - | x | - | - | bycopy|
782 // | ptr | n.a. | x | - | - | - | null |
783 // | ptr | n.a. | - | - | - | x | byref |
784 // | ptr | n.a. | - | - | - | x[] | bycopy|
785 // | ptr | n.a. | - | - | x | | bycopy|
786 // | ptr | n.a. | - | - | x | x | bycopy|
787 // | ptr | n.a. | - | - | x | x[] | bycopy|
788 // =========================================================================
789 // Legend:
790 // scl - scalar
791 // ptr - pointer
792 // agg - aggregate
793 // x - applies
794 // - - invalid in this combination
795 // [] - mapped with an array section
796 // byref - should be mapped by reference
797 // byval - should be mapped by value
798 // null - initialize a local variable to null on the device
799 //
800 // Observations:
801 // - All scalar declarations that show up in a map clause have to be passed
802 // by reference, because they may have been mapped in the enclosing data
803 // environment.
804 // - If the scalar value does not fit the size of uintptr, it has to be
805 // passed by reference, regardless the result in the table above.
806 // - For pointers mapped by value that have either an implicit map or an
807 // array section, the runtime library may pass the NULL value to the
808 // device instead of the value passed to it by the compiler.
809
810 // FIXME: Right now, only implicit maps are implemented. Properly mapping
811 // values requires having the map, private, and firstprivate clauses SEMA
812 // and parsing in place, which we don't yet.
813
814 if (Ty->isReferenceType())
815 Ty = Ty->castAs<ReferenceType>()->getPointeeType();
816 IsByRef = !Ty->isScalarType();
817 }
818
819 // When passing data by value, we need to make sure it fits the uintptr size
820 // and alignment, because the runtime library only deals with uintptr types.
821 // If it does not fit the uintptr size, we need to pass the data by reference
822 // instead.
823 if (!IsByRef &&
824 (Ctx.getTypeSizeInChars(Ty) >
825 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
826 Ctx.getDeclAlign(VD) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType())))
827 IsByRef = true;
828
829 return IsByRef;
830}
831
Alexey Bataevf841bd92014-12-16 07:00:22 +0000832bool Sema::IsOpenMPCapturedVar(VarDecl *VD) {
833 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +0000834 VD = VD->getCanonicalDecl();
Samuel Antao4be30e92015-10-02 17:14:03 +0000835
836 // If we are attempting to capture a global variable in a directive with
837 // 'target' we return true so that this global is also mapped to the device.
838 //
839 // FIXME: If the declaration is enclosed in a 'declare target' directive,
840 // then it should not be captured. Therefore, an extra check has to be
841 // inserted here once support for 'declare target' is added.
842 //
843 if (!VD->hasLocalStorage()) {
844 if (DSAStack->getCurrentDirective() == OMPD_target &&
845 !DSAStack->isClauseParsingMode()) {
846 return true;
847 }
848 if (DSAStack->getCurScope() &&
849 DSAStack->hasDirective(
850 [](OpenMPDirectiveKind K, const DeclarationNameInfo &DNI,
851 SourceLocation Loc) -> bool {
852 return isOpenMPTargetDirective(K);
853 },
854 false)) {
855 return true;
856 }
857 }
858
Alexey Bataev48977c32015-08-04 08:10:48 +0000859 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
860 (!DSAStack->isClauseParsingMode() ||
861 DSAStack->getParentDirective() != OMPD_unknown)) {
Alexey Bataev39f915b82015-05-08 10:41:21 +0000862 if (DSAStack->isLoopControlVariable(VD) ||
863 (VD->hasLocalStorage() &&
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000864 isParallelOrTaskRegion(DSAStack->getCurrentDirective())) ||
865 DSAStack->isForceVarCapturing())
Alexey Bataev9c821032015-04-30 04:23:23 +0000866 return true;
Alexey Bataevaac108a2015-06-23 04:51:00 +0000867 auto DVarPrivate = DSAStack->getTopDSA(VD, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000868 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
869 return true;
870 DVarPrivate = DSAStack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(),
Alexey Bataevaac108a2015-06-23 04:51:00 +0000871 DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +0000872 return DVarPrivate.CKind != OMPC_unknown;
873 }
874 return false;
875}
876
Alexey Bataevaac108a2015-06-23 04:51:00 +0000877bool Sema::isOpenMPPrivateVar(VarDecl *VD, unsigned Level) {
878 assert(LangOpts.OpenMP && "OpenMP is not allowed");
879 return DSAStack->hasExplicitDSA(
880 VD, [](OpenMPClauseKind K) -> bool { return K == OMPC_private; }, Level);
881}
882
Samuel Antao4be30e92015-10-02 17:14:03 +0000883bool Sema::isOpenMPTargetCapturedVar(VarDecl *VD, unsigned Level) {
884 assert(LangOpts.OpenMP && "OpenMP is not allowed");
885 // Return true if the current level is no longer enclosed in a target region.
886
887 return !VD->hasLocalStorage() &&
888 DSAStack->hasExplicitDirective(isOpenMPTargetDirective, Level);
889}
890
Alexey Bataeved09d242014-05-28 05:53:51 +0000891void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000892
893void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
894 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000895 Scope *CurScope, SourceLocation Loc) {
896 DSAStack->push(DKind, DirName, CurScope, Loc);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000897 PushExpressionEvaluationContext(PotentiallyEvaluated);
898}
899
Alexey Bataevaac108a2015-06-23 04:51:00 +0000900void Sema::StartOpenMPClause(OpenMPClauseKind K) {
901 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000902}
903
Alexey Bataevaac108a2015-06-23 04:51:00 +0000904void Sema::EndOpenMPClause() {
905 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +0000906}
907
Alexey Bataev758e55e2013-09-06 18:03:48 +0000908void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000909 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
910 // A variable of class type (or array thereof) that appears in a lastprivate
911 // clause requires an accessible, unambiguous default constructor for the
912 // class type, unless the list item is also specified in a firstprivate
913 // clause.
914 if (auto D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000915 for (auto *C : D->clauses()) {
916 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
917 SmallVector<Expr *, 8> PrivateCopies;
918 for (auto *DE : Clause->varlists()) {
919 if (DE->isValueDependent() || DE->isTypeDependent()) {
920 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000921 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +0000922 }
923 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(DE)->getDecl());
Alexey Bataevbd9fec12015-08-18 06:47:21 +0000924 QualType Type = VD->getType().getNonReferenceType();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000925 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000926 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +0000927 // Generate helper private variable and initialize it with the
928 // default value. The address of the original variable is replaced
929 // by the address of the new private variable in CodeGen. This new
930 // variable is not added to IdResolver, so the code in the OpenMP
931 // region uses original variable for proper diagnostics.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +0000932 auto *VDPrivate = buildVarDecl(
933 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
934 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +0000935 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
936 if (VDPrivate->isInvalidDecl())
937 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +0000938 PrivateCopies.push_back(buildDeclRefExpr(
939 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +0000940 } else {
941 // The variable is also a firstprivate, so initialization sequence
942 // for private copy is generated already.
943 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000944 }
945 }
Alexey Bataev38e89532015-04-16 04:54:05 +0000946 // Set initializers to private copies if no errors were found.
947 if (PrivateCopies.size() == Clause->varlist_size()) {
948 Clause->setPrivateCopies(PrivateCopies);
949 }
Alexey Bataevf29276e2014-06-18 04:14:57 +0000950 }
951 }
952 }
953
Alexey Bataev758e55e2013-09-06 18:03:48 +0000954 DSAStack->pop();
955 DiscardCleanupsInEvaluationContext();
956 PopExpressionEvaluationContext();
957}
958
Alexander Musman3276a272015-03-21 10:12:56 +0000959static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
960 Expr *NumIterations, Sema &SemaRef,
961 Scope *S);
962
Alexey Bataeva769e072013-03-22 06:34:35 +0000963namespace {
964
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000965class VarDeclFilterCCC : public CorrectionCandidateCallback {
966private:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000967 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +0000968
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000969public:
Alexey Bataev7ff55242014-06-19 09:13:45 +0000970 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +0000971 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000972 NamedDecl *ND = Candidate.getCorrectionDecl();
973 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
974 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +0000975 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
976 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +0000977 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000978 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +0000979 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000980};
Alexey Bataeved09d242014-05-28 05:53:51 +0000981} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +0000982
983ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
984 CXXScopeSpec &ScopeSpec,
985 const DeclarationNameInfo &Id) {
986 LookupResult Lookup(*this, Id, LookupOrdinaryName);
987 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
988
989 if (Lookup.isAmbiguous())
990 return ExprError();
991
992 VarDecl *VD;
993 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +0000994 if (TypoCorrection Corrected = CorrectTypo(
995 Id, LookupOrdinaryName, CurScope, nullptr,
996 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +0000997 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000998 PDiag(Lookup.empty()
999 ? diag::err_undeclared_var_use_suggest
1000 : diag::err_omp_expected_var_arg_suggest)
1001 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +00001002 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001003 } else {
Richard Smithf9b15102013-08-17 00:46:16 +00001004 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
1005 : diag::err_omp_expected_var_arg)
1006 << Id.getName();
1007 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001008 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001009 } else {
1010 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00001011 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001012 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1013 return ExprError();
1014 }
1015 }
1016 Lookup.suppressDiagnostics();
1017
1018 // OpenMP [2.9.2, Syntax, C/C++]
1019 // Variables must be file-scope, namespace-scope, or static block-scope.
1020 if (!VD->hasGlobalStorage()) {
1021 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001022 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1023 bool IsDecl =
1024 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001025 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001026 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1027 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001028 return ExprError();
1029 }
1030
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001031 VarDecl *CanonicalVD = VD->getCanonicalDecl();
1032 NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001033 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1034 // A threadprivate directive for file-scope variables must appear outside
1035 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001036 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1037 !getCurLexicalContext()->isTranslationUnit()) {
1038 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001039 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1040 bool IsDecl =
1041 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1042 Diag(VD->getLocation(),
1043 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1044 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001045 return ExprError();
1046 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001047 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1048 // A threadprivate directive for static class member variables must appear
1049 // in the class definition, in the same scope in which the member
1050 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001051 if (CanonicalVD->isStaticDataMember() &&
1052 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1053 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001054 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1055 bool IsDecl =
1056 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1057 Diag(VD->getLocation(),
1058 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1059 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001060 return ExprError();
1061 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001062 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1063 // A threadprivate directive for namespace-scope variables must appear
1064 // outside any definition or declaration other than the namespace
1065 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001066 if (CanonicalVD->getDeclContext()->isNamespace() &&
1067 (!getCurLexicalContext()->isFileContext() ||
1068 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
1069 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001070 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1071 bool IsDecl =
1072 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1073 Diag(VD->getLocation(),
1074 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1075 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001076 return ExprError();
1077 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001078 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
1079 // A threadprivate directive for static block-scope variables must appear
1080 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001081 if (CanonicalVD->isStaticLocal() && CurScope &&
1082 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001083 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001084 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1085 bool IsDecl =
1086 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1087 Diag(VD->getLocation(),
1088 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1089 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001090 return ExprError();
1091 }
1092
1093 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1094 // A threadprivate directive must lexically precede all references to any
1095 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001096 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001097 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +00001098 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001099 return ExprError();
1100 }
1101
1102 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00001103 ExprResult DE = buildDeclRefExpr(*this, VD, ExprType, Id.getLoc());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001104 return DE;
1105}
1106
Alexey Bataeved09d242014-05-28 05:53:51 +00001107Sema::DeclGroupPtrTy
1108Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1109 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001110 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001111 CurContext->addDecl(D);
1112 return DeclGroupPtrTy::make(DeclGroupRef(D));
1113 }
1114 return DeclGroupPtrTy();
1115}
1116
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001117namespace {
1118class LocalVarRefChecker : public ConstStmtVisitor<LocalVarRefChecker, bool> {
1119 Sema &SemaRef;
1120
1121public:
1122 bool VisitDeclRefExpr(const DeclRefExpr *E) {
1123 if (auto VD = dyn_cast<VarDecl>(E->getDecl())) {
1124 if (VD->hasLocalStorage()) {
1125 SemaRef.Diag(E->getLocStart(),
1126 diag::err_omp_local_var_in_threadprivate_init)
1127 << E->getSourceRange();
1128 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
1129 << VD << VD->getSourceRange();
1130 return true;
1131 }
1132 }
1133 return false;
1134 }
1135 bool VisitStmt(const Stmt *S) {
1136 for (auto Child : S->children()) {
1137 if (Child && Visit(Child))
1138 return true;
1139 }
1140 return false;
1141 }
Alexey Bataev23b69422014-06-18 07:08:49 +00001142 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001143};
1144} // namespace
1145
Alexey Bataeved09d242014-05-28 05:53:51 +00001146OMPThreadPrivateDecl *
1147Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001148 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00001149 for (auto &RefExpr : VarList) {
1150 DeclRefExpr *DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001151 VarDecl *VD = cast<VarDecl>(DE->getDecl());
1152 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +00001153
Alexey Bataevf56f98c2015-04-16 05:39:01 +00001154 QualType QType = VD->getType();
1155 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
1156 // It will be analyzed later.
1157 Vars.push_back(DE);
1158 continue;
1159 }
1160
Alexey Bataeva769e072013-03-22 06:34:35 +00001161 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1162 // A threadprivate variable must not have an incomplete type.
1163 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001164 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001165 continue;
1166 }
1167
1168 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
1169 // A threadprivate variable must not have a reference type.
1170 if (VD->getType()->isReferenceType()) {
1171 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001172 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
1173 bool IsDecl =
1174 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1175 Diag(VD->getLocation(),
1176 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1177 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001178 continue;
1179 }
1180
Samuel Antaof8b50122015-07-13 22:54:53 +00001181 // Check if this is a TLS variable. If TLS is not being supported, produce
1182 // the corresponding diagnostic.
1183 if ((VD->getTLSKind() != VarDecl::TLS_None &&
1184 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1185 getLangOpts().OpenMPUseTLS &&
1186 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +00001187 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
1188 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +00001189 Diag(ILoc, diag::err_omp_var_thread_local)
1190 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +00001191 bool IsDecl =
1192 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1193 Diag(VD->getLocation(),
1194 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1195 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00001196 continue;
1197 }
1198
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001199 // Check if initial value of threadprivate variable reference variable with
1200 // local storage (it is not supported by runtime).
1201 if (auto Init = VD->getAnyInitializer()) {
1202 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001203 if (Checker.Visit(Init))
1204 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001205 }
1206
Alexey Bataeved09d242014-05-28 05:53:51 +00001207 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00001208 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00001209 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
1210 Context, SourceRange(Loc, Loc)));
1211 if (auto *ML = Context.getASTMutationListener())
1212 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00001213 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001214 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001215 if (!Vars.empty()) {
1216 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
1217 Vars);
1218 D->setAccess(AS_public);
1219 }
1220 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00001221}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001222
Alexey Bataev7ff55242014-06-19 09:13:45 +00001223static void ReportOriginalDSA(Sema &SemaRef, DSAStackTy *Stack,
1224 const VarDecl *VD, DSAStackTy::DSAVarData DVar,
1225 bool IsLoopIterVar = false) {
1226 if (DVar.RefExpr) {
1227 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1228 << getOpenMPClauseName(DVar.CKind);
1229 return;
1230 }
1231 enum {
1232 PDSA_StaticMemberShared,
1233 PDSA_StaticLocalVarShared,
1234 PDSA_LoopIterVarPrivate,
1235 PDSA_LoopIterVarLinear,
1236 PDSA_LoopIterVarLastprivate,
1237 PDSA_ConstVarShared,
1238 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001239 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001240 PDSA_LocalVarPrivate,
1241 PDSA_Implicit
1242 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001243 bool ReportHint = false;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001244 auto ReportLoc = VD->getLocation();
Alexey Bataev7ff55242014-06-19 09:13:45 +00001245 if (IsLoopIterVar) {
1246 if (DVar.CKind == OMPC_private)
1247 Reason = PDSA_LoopIterVarPrivate;
1248 else if (DVar.CKind == OMPC_lastprivate)
1249 Reason = PDSA_LoopIterVarLastprivate;
1250 else
1251 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001252 } else if (DVar.DKind == OMPD_task && DVar.CKind == OMPC_firstprivate) {
1253 Reason = PDSA_TaskVarFirstprivate;
1254 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001255 } else if (VD->isStaticLocal())
1256 Reason = PDSA_StaticLocalVarShared;
1257 else if (VD->isStaticDataMember())
1258 Reason = PDSA_StaticMemberShared;
1259 else if (VD->isFileVarDecl())
1260 Reason = PDSA_GlobalVarShared;
1261 else if (VD->getType().isConstant(SemaRef.getASTContext()))
1262 Reason = PDSA_ConstVarShared;
Alexey Bataevbae9a792014-06-27 10:37:06 +00001263 else if (VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00001264 ReportHint = true;
1265 Reason = PDSA_LocalVarPrivate;
1266 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00001267 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001268 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00001269 << Reason << ReportHint
1270 << getOpenMPDirectiveName(Stack->getCurrentDirective());
1271 } else if (DVar.ImplicitDSALoc.isValid()) {
1272 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
1273 << getOpenMPClauseName(DVar.CKind);
1274 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00001275}
1276
Alexey Bataev758e55e2013-09-06 18:03:48 +00001277namespace {
1278class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
1279 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001280 Sema &SemaRef;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001281 bool ErrorFound;
1282 CapturedStmt *CS;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001283 llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
Alexey Bataev4acb8592014-07-07 13:01:15 +00001284 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataeved09d242014-05-28 05:53:51 +00001285
Alexey Bataev758e55e2013-09-06 18:03:48 +00001286public:
1287 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001288 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00001289 // Skip internally declared variables.
Alexey Bataeved09d242014-05-28 05:53:51 +00001290 if (VD->isLocalVarDecl() && !CS->capturesVariable(VD))
1291 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001292
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001293 auto DVar = Stack->getTopDSA(VD, false);
1294 // Check if the variable has explicit DSA set and stop analysis if it so.
1295 if (DVar.RefExpr) return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001296
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001297 auto ELoc = E->getExprLoc();
1298 auto DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001299 // The default(none) clause requires that each variable that is referenced
1300 // in the construct, and does not have a predetermined data-sharing
1301 // attribute, must have its data-sharing attribute explicitly determined
1302 // by being listed in a data-sharing attribute clause.
1303 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001304 isParallelOrTaskRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00001305 VarsWithInheritedDSA.count(VD) == 0) {
1306 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001307 return;
1308 }
1309
1310 // OpenMP [2.9.3.6, Restrictions, p.2]
1311 // A list item that appears in a reduction clause of the innermost
1312 // enclosing worksharing or parallel construct may not be accessed in an
1313 // explicit task.
Alexey Bataevf29276e2014-06-18 04:14:57 +00001314 DVar = Stack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001315 [](OpenMPDirectiveKind K) -> bool {
1316 return isOpenMPParallelDirective(K) ||
Alexey Bataev13314bf2014-10-09 04:18:56 +00001317 isOpenMPWorksharingDirective(K) ||
1318 isOpenMPTeamsDirective(K);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001319 },
1320 false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001321 if (DKind == OMPD_task && DVar.CKind == OMPC_reduction) {
1322 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00001323 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
1324 ReportOriginalDSA(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00001325 return;
1326 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001327
1328 // Define implicit data-sharing attributes for task.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001329 DVar = Stack->getImplicitDSA(VD, false);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001330 if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001331 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001332 }
1333 }
1334 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001335 for (auto *C : S->clauses()) {
1336 // Skip analysis of arguments of implicitly defined firstprivate clause
1337 // for task directives.
1338 if (C && (!isa<OMPFirstprivateClause>(C) || C->getLocStart().isValid()))
1339 for (auto *CC : C->children()) {
1340 if (CC)
1341 Visit(CC);
1342 }
1343 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001344 }
1345 void VisitStmt(Stmt *S) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001346 for (auto *C : S->children()) {
1347 if (C && !isa<OMPExecutableDirective>(C))
1348 Visit(C);
1349 }
Alexey Bataeved09d242014-05-28 05:53:51 +00001350 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001351
1352 bool isErrorFound() { return ErrorFound; }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001353 ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001354 llvm::DenseMap<VarDecl *, Expr *> &getVarsWithInheritedDSA() {
1355 return VarsWithInheritedDSA;
1356 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001357
Alexey Bataev7ff55242014-06-19 09:13:45 +00001358 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
1359 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00001360};
Alexey Bataeved09d242014-05-28 05:53:51 +00001361} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00001362
Alexey Bataevbae9a792014-06-27 10:37:06 +00001363void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00001364 switch (DKind) {
1365 case OMPD_parallel: {
1366 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001367 QualType KmpInt32PtrTy =
1368 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001369 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001370 std::make_pair(".global_tid.", KmpInt32PtrTy),
1371 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1372 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001373 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001374 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1375 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001376 break;
1377 }
1378 case OMPD_simd: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001379 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001380 std::make_pair(StringRef(), QualType()) // __context with shared vars
1381 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001382 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1383 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001384 break;
1385 }
1386 case OMPD_for: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00001387 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001388 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00001389 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001390 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1391 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00001392 break;
1393 }
Alexander Musmanf82886e2014-09-18 05:12:34 +00001394 case OMPD_for_simd: {
1395 Sema::CapturedParamNameType Params[] = {
1396 std::make_pair(StringRef(), QualType()) // __context with shared vars
1397 };
1398 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1399 Params);
1400 break;
1401 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001402 case OMPD_sections: {
1403 Sema::CapturedParamNameType Params[] = {
1404 std::make_pair(StringRef(), QualType()) // __context with shared vars
1405 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001406 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1407 Params);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00001408 break;
1409 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001410 case OMPD_section: {
1411 Sema::CapturedParamNameType Params[] = {
1412 std::make_pair(StringRef(), QualType()) // __context with shared vars
1413 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001414 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1415 Params);
Alexey Bataev1e0498a2014-06-26 08:21:58 +00001416 break;
1417 }
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001418 case OMPD_single: {
1419 Sema::CapturedParamNameType Params[] = {
1420 std::make_pair(StringRef(), QualType()) // __context with shared vars
1421 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00001422 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1423 Params);
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00001424 break;
1425 }
Alexander Musman80c22892014-07-17 08:54:58 +00001426 case OMPD_master: {
1427 Sema::CapturedParamNameType Params[] = {
1428 std::make_pair(StringRef(), QualType()) // __context with shared vars
1429 };
1430 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1431 Params);
1432 break;
1433 }
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001434 case OMPD_critical: {
1435 Sema::CapturedParamNameType Params[] = {
1436 std::make_pair(StringRef(), QualType()) // __context with shared vars
1437 };
1438 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1439 Params);
1440 break;
1441 }
Alexey Bataev4acb8592014-07-07 13:01:15 +00001442 case OMPD_parallel_for: {
1443 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001444 QualType KmpInt32PtrTy =
1445 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev4acb8592014-07-07 13:01:15 +00001446 Sema::CapturedParamNameType Params[] = {
1447 std::make_pair(".global_tid.", KmpInt32PtrTy),
1448 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1449 std::make_pair(StringRef(), QualType()) // __context with shared vars
1450 };
1451 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1452 Params);
1453 break;
1454 }
Alexander Musmane4e893b2014-09-23 09:33:00 +00001455 case OMPD_parallel_for_simd: {
1456 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001457 QualType KmpInt32PtrTy =
1458 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexander Musmane4e893b2014-09-23 09:33:00 +00001459 Sema::CapturedParamNameType Params[] = {
1460 std::make_pair(".global_tid.", KmpInt32PtrTy),
1461 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1462 std::make_pair(StringRef(), QualType()) // __context with shared vars
1463 };
1464 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1465 Params);
1466 break;
1467 }
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001468 case OMPD_parallel_sections: {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001469 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001470 QualType KmpInt32PtrTy =
1471 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001472 Sema::CapturedParamNameType Params[] = {
Alexey Bataev68adb7d2015-04-14 03:29:22 +00001473 std::make_pair(".global_tid.", KmpInt32PtrTy),
1474 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001475 std::make_pair(StringRef(), QualType()) // __context with shared vars
1476 };
1477 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1478 Params);
1479 break;
1480 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001481 case OMPD_task: {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001482 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001483 QualType Args[] = {Context.VoidPtrTy.withConst().withRestrict()};
1484 FunctionProtoType::ExtProtoInfo EPI;
1485 EPI.Variadic = true;
1486 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001487 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00001488 std::make_pair(".global_tid.", KmpInt32Ty),
1489 std::make_pair(".part_id.", KmpInt32Ty),
Alexey Bataev3ae88e22015-05-22 08:56:35 +00001490 std::make_pair(".privates.",
1491 Context.VoidPtrTy.withConst().withRestrict()),
1492 std::make_pair(
1493 ".copy_fn.",
1494 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001495 std::make_pair(StringRef(), QualType()) // __context with shared vars
1496 };
1497 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1498 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00001499 // Mark this captured region as inlined, because we don't use outlined
1500 // function directly.
1501 getCurCapturedRegion()->TheCapturedDecl->addAttr(
1502 AlwaysInlineAttr::CreateImplicit(
1503 Context, AlwaysInlineAttr::Keyword_forceinline, SourceRange()));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001504 break;
1505 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001506 case OMPD_ordered: {
1507 Sema::CapturedParamNameType Params[] = {
1508 std::make_pair(StringRef(), QualType()) // __context with shared vars
1509 };
1510 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1511 Params);
1512 break;
1513 }
Alexey Bataev0162e452014-07-22 10:10:35 +00001514 case OMPD_atomic: {
1515 Sema::CapturedParamNameType Params[] = {
1516 std::make_pair(StringRef(), QualType()) // __context with shared vars
1517 };
1518 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1519 Params);
1520 break;
1521 }
Michael Wong65f367f2015-07-21 13:44:28 +00001522 case OMPD_target_data:
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001523 case OMPD_target: {
1524 Sema::CapturedParamNameType Params[] = {
1525 std::make_pair(StringRef(), QualType()) // __context with shared vars
1526 };
1527 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1528 Params);
1529 break;
1530 }
Alexey Bataev13314bf2014-10-09 04:18:56 +00001531 case OMPD_teams: {
1532 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1);
Alexey Bataev2377fe92015-09-10 08:12:02 +00001533 QualType KmpInt32PtrTy =
1534 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataev13314bf2014-10-09 04:18:56 +00001535 Sema::CapturedParamNameType Params[] = {
1536 std::make_pair(".global_tid.", KmpInt32PtrTy),
1537 std::make_pair(".bound_tid.", KmpInt32PtrTy),
1538 std::make_pair(StringRef(), QualType()) // __context with shared vars
1539 };
1540 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1541 Params);
1542 break;
1543 }
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001544 case OMPD_taskgroup: {
1545 Sema::CapturedParamNameType Params[] = {
1546 std::make_pair(StringRef(), QualType()) // __context with shared vars
1547 };
1548 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1549 Params);
1550 break;
1551 }
Alexey Bataev49f6e782015-12-01 04:18:41 +00001552 case OMPD_taskloop: {
1553 Sema::CapturedParamNameType Params[] = {
1554 std::make_pair(StringRef(), QualType()) // __context with shared vars
1555 };
1556 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1557 Params);
1558 break;
1559 }
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001560 case OMPD_taskloop_simd: {
1561 Sema::CapturedParamNameType Params[] = {
1562 std::make_pair(StringRef(), QualType()) // __context with shared vars
1563 };
1564 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1565 Params);
1566 break;
1567 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001568 case OMPD_distribute: {
1569 Sema::CapturedParamNameType Params[] = {
1570 std::make_pair(StringRef(), QualType()) // __context with shared vars
1571 };
1572 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
1573 Params);
1574 break;
1575 }
Alexey Bataev9959db52014-05-06 10:08:46 +00001576 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00001577 case OMPD_taskyield:
1578 case OMPD_barrier:
1579 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001580 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00001581 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00001582 case OMPD_flush:
Alexey Bataev9959db52014-05-06 10:08:46 +00001583 llvm_unreachable("OpenMP Directive is not allowed");
1584 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00001585 llvm_unreachable("Unknown OpenMP directive");
1586 }
1587}
1588
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001589StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
1590 ArrayRef<OMPClause *> Clauses) {
1591 if (!S.isUsable()) {
1592 ActOnCapturedRegionError();
1593 return StmtError();
1594 }
Alexey Bataev040d5402015-05-12 08:35:28 +00001595 // This is required for proper codegen.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001596 for (auto *Clause : Clauses) {
Alexey Bataev16dc7b62015-05-20 03:46:04 +00001597 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001598 Clause->getClauseKind() == OMPC_copyprivate ||
1599 (getLangOpts().OpenMPUseTLS &&
1600 getASTContext().getTargetInfo().isTLSSupported() &&
1601 Clause->getClauseKind() == OMPC_copyin)) {
1602 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00001603 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001604 for (auto *VarRef : Clause->children()) {
1605 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00001606 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001607 }
1608 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00001609 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev040d5402015-05-12 08:35:28 +00001610 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective()) &&
1611 Clause->getClauseKind() == OMPC_schedule) {
1612 // Mark all variables in private list clauses as used in inner region.
1613 // Required for proper codegen of combined directives.
1614 // TODO: add processing for other clauses.
1615 if (auto *E = cast_or_null<Expr>(
1616 cast<OMPScheduleClause>(Clause)->getHelperChunkSize())) {
1617 MarkDeclarationsReferencedInExpr(E);
1618 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00001619 }
1620 }
1621 return ActOnCapturedRegionEnd(S.get());
1622}
1623
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001624static bool CheckNestingOfRegions(Sema &SemaRef, DSAStackTy *Stack,
1625 OpenMPDirectiveKind CurrentRegion,
1626 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001627 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001628 SourceLocation StartLoc) {
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001629 // Allowed nesting of constructs
1630 // +------------------+-----------------+------------------------------------+
1631 // | Parent directive | Child directive | Closely (!), No-Closely(+), Both(*)|
1632 // +------------------+-----------------+------------------------------------+
1633 // | parallel | parallel | * |
1634 // | parallel | for | * |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001635 // | parallel | for simd | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001636 // | parallel | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001637 // | parallel | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001638 // | parallel | simd | * |
1639 // | parallel | sections | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001640 // | parallel | section | + |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001641 // | parallel | single | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001642 // | parallel | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001643 // | parallel |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001644 // | parallel |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001645 // | parallel | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001646 // | parallel | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001647 // | parallel | barrier | * |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001648 // | parallel | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001649 // | parallel | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001650 // | parallel | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001651 // | parallel | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001652 // | parallel | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001653 // | parallel | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001654 // | parallel | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001655 // | parallel | cancellation | |
1656 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001657 // | parallel | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001658 // | parallel | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001659 // | parallel | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001660 // | parallel | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001661 // +------------------+-----------------+------------------------------------+
1662 // | for | parallel | * |
1663 // | for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001664 // | for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001665 // | for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001666 // | for | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001667 // | for | simd | * |
1668 // | for | sections | + |
1669 // | for | section | + |
1670 // | for | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001671 // | for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001672 // | for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001673 // | for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001674 // | for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001675 // | for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001676 // | for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001677 // | for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001678 // | for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001679 // | for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001680 // | for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001681 // | for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001682 // | for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001683 // | for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001684 // | for | cancellation | |
1685 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001686 // | for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001687 // | for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001688 // | for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001689 // | for | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001690 // +------------------+-----------------+------------------------------------+
Alexander Musman80c22892014-07-17 08:54:58 +00001691 // | master | parallel | * |
1692 // | master | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001693 // | master | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001694 // | master | master | * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001695 // | master | critical | * |
Alexander Musman80c22892014-07-17 08:54:58 +00001696 // | master | simd | * |
1697 // | master | sections | + |
1698 // | master | section | + |
1699 // | master | single | + |
1700 // | master | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001701 // | master |parallel for simd| * |
Alexander Musman80c22892014-07-17 08:54:58 +00001702 // | master |parallel sections| * |
1703 // | master | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001704 // | master | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001705 // | master | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001706 // | master | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001707 // | master | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001708 // | master | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001709 // | master | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001710 // | master | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001711 // | master | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001712 // | master | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001713 // | master | cancellation | |
1714 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001715 // | master | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001716 // | master | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001717 // | master | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001718 // | master | distribute | |
Alexander Musman80c22892014-07-17 08:54:58 +00001719 // +------------------+-----------------+------------------------------------+
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001720 // | critical | parallel | * |
1721 // | critical | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001722 // | critical | for simd | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001723 // | critical | master | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001724 // | critical | critical | * (should have different names) |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001725 // | critical | simd | * |
1726 // | critical | sections | + |
1727 // | critical | section | + |
1728 // | critical | single | + |
1729 // | critical | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001730 // | critical |parallel for simd| * |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001731 // | critical |parallel sections| * |
1732 // | critical | task | * |
1733 // | critical | taskyield | * |
1734 // | critical | barrier | + |
1735 // | critical | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001736 // | critical | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001737 // | critical | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001738 // | critical | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001739 // | critical | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001740 // | critical | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001741 // | critical | cancellation | |
1742 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001743 // | critical | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001744 // | critical | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001745 // | critical | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001746 // | critical | distribute | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001747 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001748 // | simd | parallel | |
1749 // | simd | for | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001750 // | simd | for simd | |
Alexander Musman80c22892014-07-17 08:54:58 +00001751 // | simd | master | |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001752 // | simd | critical | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001753 // | simd | simd | |
1754 // | simd | sections | |
1755 // | simd | section | |
1756 // | simd | single | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001757 // | simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001758 // | simd |parallel for simd| |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001759 // | simd |parallel sections| |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001760 // | simd | task | |
Alexey Bataev68446b72014-07-18 07:47:19 +00001761 // | simd | taskyield | |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001762 // | simd | barrier | |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001763 // | simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001764 // | simd | taskgroup | |
Alexey Bataev6125da92014-07-21 11:26:11 +00001765 // | simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001766 // | simd | ordered | + (with simd clause) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001767 // | simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001768 // | simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001769 // | simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001770 // | simd | cancellation | |
1771 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001772 // | simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001773 // | simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001774 // | simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001775 // | simd | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001776 // +------------------+-----------------+------------------------------------+
Alexander Musmanf82886e2014-09-18 05:12:34 +00001777 // | for simd | parallel | |
1778 // | for simd | for | |
1779 // | for simd | for simd | |
1780 // | for simd | master | |
1781 // | for simd | critical | |
1782 // | for simd | simd | |
1783 // | for simd | sections | |
1784 // | for simd | section | |
1785 // | for simd | single | |
1786 // | for simd | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001787 // | for simd |parallel for simd| |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001788 // | for simd |parallel sections| |
1789 // | for simd | task | |
1790 // | for simd | taskyield | |
1791 // | for simd | barrier | |
1792 // | for simd | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001793 // | for simd | taskgroup | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001794 // | for simd | flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001795 // | for simd | ordered | + (with simd clause) |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001796 // | for simd | atomic | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001797 // | for simd | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001798 // | for simd | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001799 // | for simd | cancellation | |
1800 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001801 // | for simd | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001802 // | for simd | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001803 // | for simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001804 // | for simd | distribute | |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001805 // +------------------+-----------------+------------------------------------+
Alexander Musmane4e893b2014-09-23 09:33:00 +00001806 // | parallel for simd| parallel | |
1807 // | parallel for simd| for | |
1808 // | parallel for simd| for simd | |
1809 // | parallel for simd| master | |
1810 // | parallel for simd| critical | |
1811 // | parallel for simd| simd | |
1812 // | parallel for simd| sections | |
1813 // | parallel for simd| section | |
1814 // | parallel for simd| single | |
1815 // | parallel for simd| parallel for | |
1816 // | parallel for simd|parallel for simd| |
1817 // | parallel for simd|parallel sections| |
1818 // | parallel for simd| task | |
1819 // | parallel for simd| taskyield | |
1820 // | parallel for simd| barrier | |
1821 // | parallel for simd| taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001822 // | parallel for simd| taskgroup | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001823 // | parallel for simd| flush | |
Alexey Bataevd14d1e62015-09-28 06:39:35 +00001824 // | parallel for simd| ordered | + (with simd clause) |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001825 // | parallel for simd| atomic | |
1826 // | parallel for simd| target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001827 // | parallel for simd| teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001828 // | parallel for simd| cancellation | |
1829 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001830 // | parallel for simd| cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001831 // | parallel for simd| taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001832 // | parallel for simd| taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001833 // | parallel for simd| distribute | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001834 // +------------------+-----------------+------------------------------------+
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001835 // | sections | parallel | * |
1836 // | sections | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001837 // | sections | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001838 // | sections | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001839 // | sections | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001840 // | sections | simd | * |
1841 // | sections | sections | + |
1842 // | sections | section | * |
1843 // | sections | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001844 // | sections | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001845 // | sections |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001846 // | sections |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001847 // | sections | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001848 // | sections | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001849 // | sections | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001850 // | sections | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001851 // | sections | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001852 // | sections | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001853 // | sections | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001854 // | sections | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001855 // | sections | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001856 // | sections | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001857 // | sections | cancellation | |
1858 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001859 // | sections | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001860 // | sections | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001861 // | sections | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001862 // | sections | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001863 // +------------------+-----------------+------------------------------------+
1864 // | section | parallel | * |
1865 // | section | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001866 // | section | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001867 // | section | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001868 // | section | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001869 // | section | simd | * |
1870 // | section | sections | + |
1871 // | section | section | + |
1872 // | section | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001873 // | section | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001874 // | section |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001875 // | section |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001876 // | section | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001877 // | section | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001878 // | section | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001879 // | section | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001880 // | section | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001881 // | section | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001882 // | section | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001883 // | section | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001884 // | section | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001885 // | section | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001886 // | section | cancellation | |
1887 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001888 // | section | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001889 // | section | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001890 // | section | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001891 // | section | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001892 // +------------------+-----------------+------------------------------------+
1893 // | single | parallel | * |
1894 // | single | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001895 // | single | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001896 // | single | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001897 // | single | critical | * |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00001898 // | single | simd | * |
1899 // | single | sections | + |
1900 // | single | section | + |
1901 // | single | single | + |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001902 // | single | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001903 // | single |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001904 // | single |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001905 // | single | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001906 // | single | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001907 // | single | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001908 // | single | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001909 // | single | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001910 // | single | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001911 // | single | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001912 // | single | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001913 // | single | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001914 // | single | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001915 // | single | cancellation | |
1916 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00001917 // | single | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001918 // | single | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001919 // | single | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001920 // | single | distribute | |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001921 // +------------------+-----------------+------------------------------------+
1922 // | parallel for | parallel | * |
1923 // | parallel for | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001924 // | parallel for | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001925 // | parallel for | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001926 // | parallel for | critical | * |
Alexey Bataev4acb8592014-07-07 13:01:15 +00001927 // | parallel for | simd | * |
1928 // | parallel for | sections | + |
1929 // | parallel for | section | + |
1930 // | parallel for | single | + |
1931 // | parallel for | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001932 // | parallel for |parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001933 // | parallel for |parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001934 // | parallel for | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001935 // | parallel for | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001936 // | parallel for | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001937 // | parallel for | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001938 // | parallel for | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001939 // | parallel for | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001940 // | parallel for | ordered | * (if construct is ordered) |
Alexey Bataev0162e452014-07-22 10:10:35 +00001941 // | parallel for | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001942 // | parallel for | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001943 // | parallel for | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001944 // | parallel for | cancellation | |
1945 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001946 // | parallel for | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001947 // | parallel for | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001948 // | parallel for | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001949 // | parallel for | distribute | |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001950 // +------------------+-----------------+------------------------------------+
1951 // | parallel sections| parallel | * |
1952 // | parallel sections| for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001953 // | parallel sections| for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001954 // | parallel sections| master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001955 // | parallel sections| critical | + |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001956 // | parallel sections| simd | * |
1957 // | parallel sections| sections | + |
1958 // | parallel sections| section | * |
1959 // | parallel sections| single | + |
1960 // | parallel sections| parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001961 // | parallel sections|parallel for simd| * |
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00001962 // | parallel sections|parallel sections| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001963 // | parallel sections| task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001964 // | parallel sections| taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001965 // | parallel sections| barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001966 // | parallel sections| taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001967 // | parallel sections| taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001968 // | parallel sections| flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001969 // | parallel sections| ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001970 // | parallel sections| atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00001971 // | parallel sections| target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00001972 // | parallel sections| teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00001973 // | parallel sections| cancellation | |
1974 // | | point | ! |
Alexey Bataev80909872015-07-02 11:25:17 +00001975 // | parallel sections| cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00001976 // | parallel sections| taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00001977 // | parallel sections| taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00001978 // | parallel sections| distribute | |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001979 // +------------------+-----------------+------------------------------------+
1980 // | task | parallel | * |
1981 // | task | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00001982 // | task | for simd | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001983 // | task | master | + |
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001984 // | task | critical | * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001985 // | task | simd | * |
1986 // | task | sections | + |
Alexander Musman80c22892014-07-17 08:54:58 +00001987 // | task | section | + |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001988 // | task | single | + |
1989 // | task | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00001990 // | task |parallel for simd| * |
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001991 // | task |parallel sections| * |
1992 // | task | task | * |
Alexey Bataev68446b72014-07-18 07:47:19 +00001993 // | task | taskyield | * |
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00001994 // | task | barrier | + |
Alexey Bataev2df347a2014-07-18 10:17:07 +00001995 // | task | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00001996 // | task | taskgroup | * |
Alexey Bataev6125da92014-07-21 11:26:11 +00001997 // | task | flush | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00001998 // | task | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00001999 // | task | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002000 // | task | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002001 // | task | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002002 // | task | cancellation | |
Alexey Bataev80909872015-07-02 11:25:17 +00002003 // | | point | ! |
2004 // | task | cancel | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002005 // | task | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002006 // | task | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002007 // | task | distribute | |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002008 // +------------------+-----------------+------------------------------------+
2009 // | ordered | parallel | * |
2010 // | ordered | for | + |
Alexander Musmanf82886e2014-09-18 05:12:34 +00002011 // | ordered | for simd | + |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002012 // | ordered | master | * |
2013 // | ordered | critical | * |
2014 // | ordered | simd | * |
2015 // | ordered | sections | + |
2016 // | ordered | section | + |
2017 // | ordered | single | + |
2018 // | ordered | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002019 // | ordered |parallel for simd| * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002020 // | ordered |parallel sections| * |
2021 // | ordered | task | * |
2022 // | ordered | taskyield | * |
2023 // | ordered | barrier | + |
2024 // | ordered | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002025 // | ordered | taskgroup | * |
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002026 // | ordered | flush | * |
2027 // | ordered | ordered | + |
Alexey Bataev0162e452014-07-22 10:10:35 +00002028 // | ordered | atomic | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002029 // | ordered | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002030 // | ordered | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002031 // | ordered | cancellation | |
2032 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002033 // | ordered | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002034 // | ordered | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002035 // | ordered | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002036 // | ordered | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002037 // +------------------+-----------------+------------------------------------+
2038 // | atomic | parallel | |
2039 // | atomic | for | |
2040 // | atomic | for simd | |
2041 // | atomic | master | |
2042 // | atomic | critical | |
2043 // | atomic | simd | |
2044 // | atomic | sections | |
2045 // | atomic | section | |
2046 // | atomic | single | |
2047 // | atomic | parallel for | |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002048 // | atomic |parallel for simd| |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002049 // | atomic |parallel sections| |
2050 // | atomic | task | |
2051 // | atomic | taskyield | |
2052 // | atomic | barrier | |
2053 // | atomic | taskwait | |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002054 // | atomic | taskgroup | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002055 // | atomic | flush | |
2056 // | atomic | ordered | |
2057 // | atomic | atomic | |
2058 // | atomic | target | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002059 // | atomic | teams | |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002060 // | atomic | cancellation | |
2061 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002062 // | atomic | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002063 // | atomic | taskloop | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002064 // | atomic | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002065 // | atomic | distribute | |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002066 // +------------------+-----------------+------------------------------------+
2067 // | target | parallel | * |
2068 // | target | for | * |
2069 // | target | for simd | * |
2070 // | target | master | * |
2071 // | target | critical | * |
2072 // | target | simd | * |
2073 // | target | sections | * |
2074 // | target | section | * |
2075 // | target | single | * |
2076 // | target | parallel for | * |
Alexander Musmane4e893b2014-09-23 09:33:00 +00002077 // | target |parallel for simd| * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002078 // | target |parallel sections| * |
2079 // | target | task | * |
2080 // | target | taskyield | * |
2081 // | target | barrier | * |
2082 // | target | taskwait | * |
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002083 // | target | taskgroup | * |
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002084 // | target | flush | * |
2085 // | target | ordered | * |
2086 // | target | atomic | * |
2087 // | target | target | * |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002088 // | target | teams | * |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002089 // | target | cancellation | |
2090 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002091 // | target | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002092 // | target | taskloop | * |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002093 // | target | taskloop simd | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002094 // | target | distribute | |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002095 // +------------------+-----------------+------------------------------------+
2096 // | teams | parallel | * |
2097 // | teams | for | + |
2098 // | teams | for simd | + |
2099 // | teams | master | + |
2100 // | teams | critical | + |
2101 // | teams | simd | + |
2102 // | teams | sections | + |
2103 // | teams | section | + |
2104 // | teams | single | + |
2105 // | teams | parallel for | * |
2106 // | teams |parallel for simd| * |
2107 // | teams |parallel sections| * |
2108 // | teams | task | + |
2109 // | teams | taskyield | + |
2110 // | teams | barrier | + |
2111 // | teams | taskwait | + |
Alexey Bataev80909872015-07-02 11:25:17 +00002112 // | teams | taskgroup | + |
Alexey Bataev13314bf2014-10-09 04:18:56 +00002113 // | teams | flush | + |
2114 // | teams | ordered | + |
2115 // | teams | atomic | + |
2116 // | teams | target | + |
2117 // | teams | teams | + |
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002118 // | teams | cancellation | |
2119 // | | point | |
Alexey Bataev80909872015-07-02 11:25:17 +00002120 // | teams | cancel | |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002121 // | teams | taskloop | + |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002122 // | teams | taskloop simd | + |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002123 // | teams | distribute | ! |
Alexey Bataev49f6e782015-12-01 04:18:41 +00002124 // +------------------+-----------------+------------------------------------+
2125 // | taskloop | parallel | * |
2126 // | taskloop | for | + |
2127 // | taskloop | for simd | + |
2128 // | taskloop | master | + |
2129 // | taskloop | critical | * |
2130 // | taskloop | simd | * |
2131 // | taskloop | sections | + |
2132 // | taskloop | section | + |
2133 // | taskloop | single | + |
2134 // | taskloop | parallel for | * |
2135 // | taskloop |parallel for simd| * |
2136 // | taskloop |parallel sections| * |
2137 // | taskloop | task | * |
2138 // | taskloop | taskyield | * |
2139 // | taskloop | barrier | + |
2140 // | taskloop | taskwait | * |
2141 // | taskloop | taskgroup | * |
2142 // | taskloop | flush | * |
2143 // | taskloop | ordered | + |
2144 // | taskloop | atomic | * |
2145 // | taskloop | target | * |
2146 // | taskloop | teams | + |
2147 // | taskloop | cancellation | |
2148 // | | point | |
2149 // | taskloop | cancel | |
2150 // | taskloop | taskloop | * |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002151 // | taskloop | distribute | |
Alexey Bataev18eb25e2014-06-30 10:22:46 +00002152 // +------------------+-----------------+------------------------------------+
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002153 // | taskloop simd | parallel | |
2154 // | taskloop simd | for | |
2155 // | taskloop simd | for simd | |
2156 // | taskloop simd | master | |
2157 // | taskloop simd | critical | |
2158 // | taskloop simd | simd | |
2159 // | taskloop simd | sections | |
2160 // | taskloop simd | section | |
2161 // | taskloop simd | single | |
2162 // | taskloop simd | parallel for | |
2163 // | taskloop simd |parallel for simd| |
2164 // | taskloop simd |parallel sections| |
2165 // | taskloop simd | task | |
2166 // | taskloop simd | taskyield | |
2167 // | taskloop simd | barrier | |
2168 // | taskloop simd | taskwait | |
2169 // | taskloop simd | taskgroup | |
2170 // | taskloop simd | flush | |
2171 // | taskloop simd | ordered | + (with simd clause) |
2172 // | taskloop simd | atomic | |
2173 // | taskloop simd | target | |
2174 // | taskloop simd | teams | |
2175 // | taskloop simd | cancellation | |
2176 // | | point | |
2177 // | taskloop simd | cancel | |
2178 // | taskloop simd | taskloop | |
2179 // | taskloop simd | taskloop simd | |
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002180 // | taskloop simd | distribute | |
2181 // +------------------+-----------------+------------------------------------+
2182 // | distribute | parallel | * |
2183 // | distribute | for | * |
2184 // | distribute | for simd | * |
2185 // | distribute | master | * |
2186 // | distribute | critical | * |
2187 // | distribute | simd | * |
2188 // | distribute | sections | * |
2189 // | distribute | section | * |
2190 // | distribute | single | * |
2191 // | distribute | parallel for | * |
2192 // | distribute |parallel for simd| * |
2193 // | distribute |parallel sections| * |
2194 // | distribute | task | * |
2195 // | distribute | taskyield | * |
2196 // | distribute | barrier | * |
2197 // | distribute | taskwait | * |
2198 // | distribute | taskgroup | * |
2199 // | distribute | flush | * |
2200 // | distribute | ordered | + |
2201 // | distribute | atomic | * |
2202 // | distribute | target | |
2203 // | distribute | teams | |
2204 // | distribute | cancellation | + |
2205 // | | point | |
2206 // | distribute | cancel | + |
2207 // | distribute | taskloop | * |
2208 // | distribute | taskloop simd | * |
2209 // | distribute | distribute | |
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002210 // +------------------+-----------------+------------------------------------+
Alexey Bataev549210e2014-06-24 04:39:47 +00002211 if (Stack->getCurScope()) {
2212 auto ParentRegion = Stack->getParentDirective();
2213 bool NestingProhibited = false;
2214 bool CloseNesting = true;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002215 enum {
2216 NoRecommend,
2217 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00002218 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002219 ShouldBeInTargetRegion,
2220 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002221 } Recommend = NoRecommend;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002222 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002223 // OpenMP [2.16, Nesting of Regions]
2224 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002225 // OpenMP [2.8.1,simd Construct, Restrictions]
2226 // An ordered construct with the simd clause is the only OpenMP construct
2227 // that can appear in the simd region.
Alexey Bataev549210e2014-06-24 04:39:47 +00002228 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_simd);
2229 return true;
2230 }
Alexey Bataev0162e452014-07-22 10:10:35 +00002231 if (ParentRegion == OMPD_atomic) {
2232 // OpenMP [2.16, Nesting of Regions]
2233 // OpenMP constructs may not be nested inside an atomic region.
2234 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
2235 return true;
2236 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002237 if (CurrentRegion == OMPD_section) {
2238 // OpenMP [2.7.2, sections Construct, Restrictions]
2239 // Orphaned section directives are prohibited. That is, the section
2240 // directives must appear within the sections construct and must not be
2241 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002242 if (ParentRegion != OMPD_sections &&
2243 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002244 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
2245 << (ParentRegion != OMPD_unknown)
2246 << getOpenMPDirectiveName(ParentRegion);
2247 return true;
2248 }
2249 return false;
2250 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002251 // Allow some constructs to be orphaned (they could be used in functions,
2252 // called from OpenMP regions with the required preconditions).
2253 if (ParentRegion == OMPD_unknown)
2254 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00002255 if (CurrentRegion == OMPD_cancellation_point ||
2256 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002257 // OpenMP [2.16, Nesting of Regions]
2258 // A cancellation point construct for which construct-type-clause is
2259 // taskgroup must be nested inside a task construct. A cancellation
2260 // point construct for which construct-type-clause is not taskgroup must
2261 // be closely nested inside an OpenMP construct that matches the type
2262 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00002263 // A cancel construct for which construct-type-clause is taskgroup must be
2264 // nested inside a task construct. A cancel construct for which
2265 // construct-type-clause is not taskgroup must be closely nested inside an
2266 // OpenMP construct that matches the type specified in
2267 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002268 NestingProhibited =
2269 !((CancelRegion == OMPD_parallel && ParentRegion == OMPD_parallel) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00002270 (CancelRegion == OMPD_for &&
2271 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002272 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
2273 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00002274 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
2275 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002276 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00002277 // OpenMP [2.16, Nesting of Regions]
2278 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002279 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00002280 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002281 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002282 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002283 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
2284 // OpenMP [2.16, Nesting of Regions]
2285 // A critical region may not be nested (closely or otherwise) inside a
2286 // critical region with the same name. Note that this restriction is not
2287 // sufficient to prevent deadlock.
2288 SourceLocation PreviousCriticalLoc;
2289 bool DeadLock =
2290 Stack->hasDirective([CurrentName, &PreviousCriticalLoc](
2291 OpenMPDirectiveKind K,
2292 const DeclarationNameInfo &DNI,
2293 SourceLocation Loc)
2294 ->bool {
2295 if (K == OMPD_critical &&
2296 DNI.getName() == CurrentName.getName()) {
2297 PreviousCriticalLoc = Loc;
2298 return true;
2299 } else
2300 return false;
2301 },
2302 false /* skip top directive */);
2303 if (DeadLock) {
2304 SemaRef.Diag(StartLoc,
2305 diag::err_omp_prohibited_region_critical_same_name)
2306 << CurrentName.getName();
2307 if (PreviousCriticalLoc.isValid())
2308 SemaRef.Diag(PreviousCriticalLoc,
2309 diag::note_omp_previous_critical_region);
2310 return true;
2311 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002312 } else if (CurrentRegion == OMPD_barrier) {
2313 // OpenMP [2.16, Nesting of Regions]
2314 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00002315 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002316 NestingProhibited =
2317 isOpenMPWorksharingDirective(ParentRegion) ||
2318 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002319 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002320 isOpenMPTaskLoopDirective(ParentRegion);
Alexander Musman80c22892014-07-17 08:54:58 +00002321 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Alexander Musmanf82886e2014-09-18 05:12:34 +00002322 !isOpenMPParallelDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00002323 // OpenMP [2.16, Nesting of Regions]
2324 // A worksharing region may not be closely nested inside a worksharing,
2325 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002326 NestingProhibited =
Alexander Musmanf82886e2014-09-18 05:12:34 +00002327 isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002328 ParentRegion == OMPD_task || ParentRegion == OMPD_master ||
Alexey Bataev49f6e782015-12-01 04:18:41 +00002329 ParentRegion == OMPD_critical || ParentRegion == OMPD_ordered ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002330 isOpenMPTaskLoopDirective(ParentRegion);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002331 Recommend = ShouldBeInParallelRegion;
2332 } else if (CurrentRegion == OMPD_ordered) {
2333 // OpenMP [2.16, Nesting of Regions]
2334 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00002335 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002336 // An ordered region must be closely nested inside a loop region (or
2337 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002338 // OpenMP [2.8.1,simd Construct, Restrictions]
2339 // An ordered construct with the simd clause is the only OpenMP construct
2340 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002341 NestingProhibited = ParentRegion == OMPD_critical ||
Alexander Musman80c22892014-07-17 08:54:58 +00002342 ParentRegion == OMPD_task ||
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002343 isOpenMPTaskLoopDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00002344 !(isOpenMPSimdDirective(ParentRegion) ||
2345 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002346 Recommend = ShouldBeInOrderedRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002347 } else if (isOpenMPTeamsDirective(CurrentRegion)) {
2348 // OpenMP [2.16, Nesting of Regions]
2349 // If specified, a teams construct must be contained within a target
2350 // construct.
2351 NestingProhibited = ParentRegion != OMPD_target;
2352 Recommend = ShouldBeInTargetRegion;
2353 Stack->setParentTeamsRegionLoc(Stack->getConstructLoc());
2354 }
2355 if (!NestingProhibited && isOpenMPTeamsDirective(ParentRegion)) {
2356 // OpenMP [2.16, Nesting of Regions]
2357 // distribute, parallel, parallel sections, parallel workshare, and the
2358 // parallel loop and parallel loop SIMD constructs are the only OpenMP
2359 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002360 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
2361 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00002362 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00002363 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002364 if (!NestingProhibited && isOpenMPDistributeDirective(CurrentRegion)) {
2365 // OpenMP 4.5 [2.17 Nesting of Regions]
2366 // The region associated with the distribute construct must be strictly
2367 // nested inside a teams region
2368 NestingProhibited = !isOpenMPTeamsDirective(ParentRegion);
2369 Recommend = ShouldBeInTeamsRegion;
2370 }
Alexey Bataev549210e2014-06-24 04:39:47 +00002371 if (NestingProhibited) {
2372 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002373 << CloseNesting << getOpenMPDirectiveName(ParentRegion) << Recommend
2374 << getOpenMPDirectiveName(CurrentRegion);
Alexey Bataev549210e2014-06-24 04:39:47 +00002375 return true;
2376 }
2377 }
2378 return false;
2379}
2380
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002381static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
2382 ArrayRef<OMPClause *> Clauses,
2383 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
2384 bool ErrorFound = false;
2385 unsigned NamedModifiersNumber = 0;
2386 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
2387 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00002388 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002389 for (const auto *C : Clauses) {
2390 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
2391 // At most one if clause without a directive-name-modifier can appear on
2392 // the directive.
2393 OpenMPDirectiveKind CurNM = IC->getNameModifier();
2394 if (FoundNameModifiers[CurNM]) {
2395 S.Diag(C->getLocStart(), diag::err_omp_more_one_clause)
2396 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
2397 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
2398 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002399 } else if (CurNM != OMPD_unknown) {
2400 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002401 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00002402 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002403 FoundNameModifiers[CurNM] = IC;
2404 if (CurNM == OMPD_unknown)
2405 continue;
2406 // Check if the specified name modifier is allowed for the current
2407 // directive.
2408 // At most one if clause with the particular directive-name-modifier can
2409 // appear on the directive.
2410 bool MatchFound = false;
2411 for (auto NM : AllowedNameModifiers) {
2412 if (CurNM == NM) {
2413 MatchFound = true;
2414 break;
2415 }
2416 }
2417 if (!MatchFound) {
2418 S.Diag(IC->getNameModifierLoc(),
2419 diag::err_omp_wrong_if_directive_name_modifier)
2420 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
2421 ErrorFound = true;
2422 }
2423 }
2424 }
2425 // If any if clause on the directive includes a directive-name-modifier then
2426 // all if clauses on the directive must include a directive-name-modifier.
2427 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
2428 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
2429 S.Diag(FoundNameModifiers[OMPD_unknown]->getLocStart(),
2430 diag::err_omp_no_more_if_clause);
2431 } else {
2432 std::string Values;
2433 std::string Sep(", ");
2434 unsigned AllowedCnt = 0;
2435 unsigned TotalAllowedNum =
2436 AllowedNameModifiers.size() - NamedModifiersNumber;
2437 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
2438 ++Cnt) {
2439 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
2440 if (!FoundNameModifiers[NM]) {
2441 Values += "'";
2442 Values += getOpenMPDirectiveName(NM);
2443 Values += "'";
2444 if (AllowedCnt + 2 == TotalAllowedNum)
2445 Values += " or ";
2446 else if (AllowedCnt + 1 != TotalAllowedNum)
2447 Values += Sep;
2448 ++AllowedCnt;
2449 }
2450 }
2451 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getLocStart(),
2452 diag::err_omp_unnamed_if_clause)
2453 << (TotalAllowedNum > 1) << Values;
2454 }
Alexey Bataevecb156a2015-09-15 17:23:56 +00002455 for (auto Loc : NameModifierLoc) {
2456 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
2457 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002458 ErrorFound = true;
2459 }
2460 return ErrorFound;
2461}
2462
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002463StmtResult Sema::ActOnOpenMPExecutableDirective(
2464 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
2465 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
2466 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002467 StmtResult Res = StmtError();
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002468 if (CheckNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
2469 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00002470 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002471
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002472 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataev68446b72014-07-18 07:47:19 +00002473 llvm::DenseMap<VarDecl *, Expr *> VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002474 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00002475 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev68446b72014-07-18 07:47:19 +00002476 if (AStmt) {
2477 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
2478
2479 // Check default data sharing attributes for referenced variables.
2480 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
2481 DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
2482 if (DSAChecker.isErrorFound())
2483 return StmtError();
2484 // Generate list of implicitly defined firstprivate variables.
2485 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00002486
2487 if (!DSAChecker.getImplicitFirstprivate().empty()) {
2488 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
2489 DSAChecker.getImplicitFirstprivate(), SourceLocation(),
2490 SourceLocation(), SourceLocation())) {
2491 ClausesWithImplicit.push_back(Implicit);
2492 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
2493 DSAChecker.getImplicitFirstprivate().size();
2494 } else
2495 ErrorFound = true;
2496 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002497 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002498
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002499 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002500 switch (Kind) {
2501 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00002502 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
2503 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002504 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002505 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002506 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002507 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2508 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00002509 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00002510 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00002511 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
2512 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002513 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00002514 case OMPD_for_simd:
2515 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2516 EndLoc, VarsWithInheritedDSA);
2517 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00002518 case OMPD_sections:
2519 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
2520 EndLoc);
2521 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002522 case OMPD_section:
2523 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00002524 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00002525 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
2526 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00002527 case OMPD_single:
2528 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
2529 EndLoc);
2530 break;
Alexander Musman80c22892014-07-17 08:54:58 +00002531 case OMPD_master:
2532 assert(ClausesWithImplicit.empty() &&
2533 "No clauses are allowed for 'omp master' directive");
2534 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
2535 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002536 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00002537 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
2538 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00002539 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002540 case OMPD_parallel_for:
2541 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
2542 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002543 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00002544 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00002545 case OMPD_parallel_for_simd:
2546 Res = ActOnOpenMPParallelForSimdDirective(
2547 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002548 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00002549 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002550 case OMPD_parallel_sections:
2551 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
2552 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002553 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00002554 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002555 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002556 Res =
2557 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002558 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002559 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00002560 case OMPD_taskyield:
2561 assert(ClausesWithImplicit.empty() &&
2562 "No clauses are allowed for 'omp taskyield' directive");
2563 assert(AStmt == nullptr &&
2564 "No associated statement allowed for 'omp taskyield' directive");
2565 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
2566 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00002567 case OMPD_barrier:
2568 assert(ClausesWithImplicit.empty() &&
2569 "No clauses are allowed for 'omp barrier' directive");
2570 assert(AStmt == nullptr &&
2571 "No associated statement allowed for 'omp barrier' directive");
2572 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
2573 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00002574 case OMPD_taskwait:
2575 assert(ClausesWithImplicit.empty() &&
2576 "No clauses are allowed for 'omp taskwait' directive");
2577 assert(AStmt == nullptr &&
2578 "No associated statement allowed for 'omp taskwait' directive");
2579 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
2580 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00002581 case OMPD_taskgroup:
2582 assert(ClausesWithImplicit.empty() &&
2583 "No clauses are allowed for 'omp taskgroup' directive");
2584 Res = ActOnOpenMPTaskgroupDirective(AStmt, StartLoc, EndLoc);
2585 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00002586 case OMPD_flush:
2587 assert(AStmt == nullptr &&
2588 "No associated statement allowed for 'omp flush' directive");
2589 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
2590 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002591 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00002592 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
2593 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00002594 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00002595 case OMPD_atomic:
2596 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
2597 EndLoc);
2598 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00002599 case OMPD_teams:
2600 Res =
2601 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
2602 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002603 case OMPD_target:
2604 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
2605 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002606 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00002607 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002608 case OMPD_cancellation_point:
2609 assert(ClausesWithImplicit.empty() &&
2610 "No clauses are allowed for 'omp cancellation point' directive");
2611 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
2612 "cancellation point' directive");
2613 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
2614 break;
Alexey Bataev80909872015-07-02 11:25:17 +00002615 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00002616 assert(AStmt == nullptr &&
2617 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00002618 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
2619 CancelRegion);
2620 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00002621 break;
Michael Wong65f367f2015-07-21 13:44:28 +00002622 case OMPD_target_data:
2623 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
2624 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002625 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00002626 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00002627 case OMPD_taskloop:
2628 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
2629 EndLoc, VarsWithInheritedDSA);
2630 AllowedNameModifiers.push_back(OMPD_taskloop);
2631 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00002632 case OMPD_taskloop_simd:
2633 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
2634 EndLoc, VarsWithInheritedDSA);
2635 AllowedNameModifiers.push_back(OMPD_taskloop);
2636 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00002637 case OMPD_distribute:
2638 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
2639 EndLoc, VarsWithInheritedDSA);
2640 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002641 case OMPD_threadprivate:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002642 llvm_unreachable("OpenMP Directive is not allowed");
2643 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002644 llvm_unreachable("Unknown OpenMP directive");
2645 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00002646
Alexey Bataev4acb8592014-07-07 13:01:15 +00002647 for (auto P : VarsWithInheritedDSA) {
2648 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
2649 << P.first << P.second->getSourceRange();
2650 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002651 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
2652
2653 if (!AllowedNameModifiers.empty())
2654 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
2655 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00002656
Alexey Bataeved09d242014-05-28 05:53:51 +00002657 if (ErrorFound)
2658 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002659 return Res;
2660}
2661
2662StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
2663 Stmt *AStmt,
2664 SourceLocation StartLoc,
2665 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00002666 if (!AStmt)
2667 return StmtError();
2668
Alexey Bataev9959db52014-05-06 10:08:46 +00002669 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
2670 // 1.2.2 OpenMP Language Terminology
2671 // Structured block - An executable statement with a single entry at the
2672 // top and a single exit at the bottom.
2673 // The point of exit cannot be a branch out of the structured block.
2674 // longjmp() and throw() must not violate the entry/exit criteria.
2675 CS->getCapturedDecl()->setNothrow();
2676
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002677 getCurFunction()->setHasBranchProtectedScope();
2678
Alexey Bataev25e5b442015-09-15 12:52:43 +00002679 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
2680 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00002681}
2682
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002683namespace {
2684/// \brief Helper class for checking canonical form of the OpenMP loops and
2685/// extracting iteration space of each loop in the loop nest, that will be used
2686/// for IR generation.
2687class OpenMPIterationSpaceChecker {
2688 /// \brief Reference to Sema.
2689 Sema &SemaRef;
2690 /// \brief A location for diagnostics (when there is no some better location).
2691 SourceLocation DefaultLoc;
2692 /// \brief A location for diagnostics (when increment is not compatible).
2693 SourceLocation ConditionLoc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002694 /// \brief A source location for referring to loop init later.
2695 SourceRange InitSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002696 /// \brief A source location for referring to condition later.
2697 SourceRange ConditionSrcRange;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002698 /// \brief A source location for referring to increment later.
2699 SourceRange IncrementSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002700 /// \brief Loop variable.
2701 VarDecl *Var;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002702 /// \brief Reference to loop variable.
2703 DeclRefExpr *VarRef;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002704 /// \brief Lower bound (initializer for the var).
2705 Expr *LB;
2706 /// \brief Upper bound.
2707 Expr *UB;
2708 /// \brief Loop step (increment).
2709 Expr *Step;
2710 /// \brief This flag is true when condition is one of:
2711 /// Var < UB
2712 /// Var <= UB
2713 /// UB > Var
2714 /// UB >= Var
2715 bool TestIsLessOp;
2716 /// \brief This flag is true when condition is strict ( < or > ).
2717 bool TestIsStrictOp;
2718 /// \brief This flag is true when step is subtracted on each iteration.
2719 bool SubtractStep;
2720
2721public:
2722 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
2723 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc),
Alexander Musmana5f070a2014-10-01 06:03:56 +00002724 InitSrcRange(SourceRange()), ConditionSrcRange(SourceRange()),
2725 IncrementSrcRange(SourceRange()), Var(nullptr), VarRef(nullptr),
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002726 LB(nullptr), UB(nullptr), Step(nullptr), TestIsLessOp(false),
2727 TestIsStrictOp(false), SubtractStep(false) {}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002728 /// \brief Check init-expr for canonical loop form and save loop counter
2729 /// variable - #Var and its initialization value - #LB.
Alexey Bataev9c821032015-04-30 04:23:23 +00002730 bool CheckInit(Stmt *S, bool EmitDiags = true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002731 /// \brief Check test-expr for canonical form, save upper-bound (#UB), flags
2732 /// for less/greater and for strict/non-strict comparison.
2733 bool CheckCond(Expr *S);
2734 /// \brief Check incr-expr for canonical loop form and return true if it
2735 /// does not conform, otherwise save loop step (#Step).
2736 bool CheckInc(Expr *S);
2737 /// \brief Return the loop counter variable.
2738 VarDecl *GetLoopVar() const { return Var; }
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002739 /// \brief Return the reference expression to loop counter variable.
2740 DeclRefExpr *GetLoopVarRefExpr() const { return VarRef; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002741 /// \brief Source range of the loop init.
2742 SourceRange GetInitSrcRange() const { return InitSrcRange; }
2743 /// \brief Source range of the loop condition.
2744 SourceRange GetConditionSrcRange() const { return ConditionSrcRange; }
2745 /// \brief Source range of the loop increment.
2746 SourceRange GetIncrementSrcRange() const { return IncrementSrcRange; }
2747 /// \brief True if the step should be subtracted.
2748 bool ShouldSubtractStep() const { return SubtractStep; }
2749 /// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00002750 Expr *BuildNumIterations(Scope *S, const bool LimitedType) const;
Alexey Bataev62dbb972015-04-22 11:59:37 +00002751 /// \brief Build the precondition expression for the loops.
2752 Expr *BuildPreCond(Scope *S, Expr *Cond) const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002753 /// \brief Build reference expression to the counter be used for codegen.
2754 Expr *BuildCounterVar() const;
Alexey Bataeva8899172015-08-06 12:30:57 +00002755 /// \brief Build reference expression to the private counter be used for
2756 /// codegen.
2757 Expr *BuildPrivateCounterVar() const;
Alexander Musmana5f070a2014-10-01 06:03:56 +00002758 /// \brief Build initization of the counter be used for codegen.
2759 Expr *BuildCounterInit() const;
2760 /// \brief Build step of the counter be used for codegen.
2761 Expr *BuildCounterStep() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002762 /// \brief Return true if any expression is dependent.
2763 bool Dependent() const;
2764
2765private:
2766 /// \brief Check the right-hand side of an assignment in the increment
2767 /// expression.
2768 bool CheckIncRHS(Expr *RHS);
2769 /// \brief Helper to set loop counter variable and its initializer.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002770 bool SetVarAndLB(VarDecl *NewVar, DeclRefExpr *NewVarRefExpr, Expr *NewLB);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002771 /// \brief Helper to set upper bound.
Craig Toppere335f252015-10-04 04:53:55 +00002772 bool SetUB(Expr *NewUB, bool LessOp, bool StrictOp, SourceRange SR,
Craig Topper9cd5e4f2015-09-21 01:23:32 +00002773 SourceLocation SL);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002774 /// \brief Helper to set loop increment.
2775 bool SetStep(Expr *NewStep, bool Subtract);
2776};
2777
2778bool OpenMPIterationSpaceChecker::Dependent() const {
2779 if (!Var) {
2780 assert(!LB && !UB && !Step);
2781 return false;
2782 }
2783 return Var->getType()->isDependentType() || (LB && LB->isValueDependent()) ||
2784 (UB && UB->isValueDependent()) || (Step && Step->isValueDependent());
2785}
2786
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002787template <typename T>
2788static T *getExprAsWritten(T *E) {
2789 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(E))
2790 E = ExprTemp->getSubExpr();
2791
2792 if (auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
2793 E = MTE->GetTemporaryExpr();
2794
2795 while (auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
2796 E = Binder->getSubExpr();
2797
2798 if (auto *ICE = dyn_cast<ImplicitCastExpr>(E))
2799 E = ICE->getSubExprAsWritten();
2800 return E->IgnoreParens();
2801}
2802
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002803bool OpenMPIterationSpaceChecker::SetVarAndLB(VarDecl *NewVar,
2804 DeclRefExpr *NewVarRefExpr,
2805 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002806 // State consistency checking to ensure correct usage.
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002807 assert(Var == nullptr && LB == nullptr && VarRef == nullptr &&
2808 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002809 if (!NewVar || !NewLB)
2810 return true;
2811 Var = NewVar;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002812 VarRef = NewVarRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002813 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
2814 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00002815 if ((Ctor->isCopyOrMoveConstructor() ||
2816 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
2817 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002818 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002819 LB = NewLB;
2820 return false;
2821}
2822
2823bool OpenMPIterationSpaceChecker::SetUB(Expr *NewUB, bool LessOp, bool StrictOp,
Craig Toppere335f252015-10-04 04:53:55 +00002824 SourceRange SR, SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002825 // State consistency checking to ensure correct usage.
2826 assert(Var != nullptr && LB != nullptr && UB == nullptr && Step == nullptr &&
2827 !TestIsLessOp && !TestIsStrictOp);
2828 if (!NewUB)
2829 return true;
2830 UB = NewUB;
2831 TestIsLessOp = LessOp;
2832 TestIsStrictOp = StrictOp;
2833 ConditionSrcRange = SR;
2834 ConditionLoc = SL;
2835 return false;
2836}
2837
2838bool OpenMPIterationSpaceChecker::SetStep(Expr *NewStep, bool Subtract) {
2839 // State consistency checking to ensure correct usage.
2840 assert(Var != nullptr && LB != nullptr && Step == nullptr);
2841 if (!NewStep)
2842 return true;
2843 if (!NewStep->isValueDependent()) {
2844 // Check that the step is integer expression.
2845 SourceLocation StepLoc = NewStep->getLocStart();
2846 ExprResult Val =
2847 SemaRef.PerformOpenMPImplicitIntegerConversion(StepLoc, NewStep);
2848 if (Val.isInvalid())
2849 return true;
2850 NewStep = Val.get();
2851
2852 // OpenMP [2.6, Canonical Loop Form, Restrictions]
2853 // If test-expr is of form var relational-op b and relational-op is < or
2854 // <= then incr-expr must cause var to increase on each iteration of the
2855 // loop. If test-expr is of form var relational-op b and relational-op is
2856 // > or >= then incr-expr must cause var to decrease on each iteration of
2857 // the loop.
2858 // If test-expr is of form b relational-op var and relational-op is < or
2859 // <= then incr-expr must cause var to decrease on each iteration of the
2860 // loop. If test-expr is of form b relational-op var and relational-op is
2861 // > or >= then incr-expr must cause var to increase on each iteration of
2862 // the loop.
2863 llvm::APSInt Result;
2864 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
2865 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
2866 bool IsConstNeg =
2867 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00002868 bool IsConstPos =
2869 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002870 bool IsConstZero = IsConstant && !Result.getBoolValue();
2871 if (UB && (IsConstZero ||
2872 (TestIsLessOp ? (IsConstNeg || (IsUnsigned && Subtract))
Alexander Musmana5f070a2014-10-01 06:03:56 +00002873 : (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002874 SemaRef.Diag(NewStep->getExprLoc(),
2875 diag::err_omp_loop_incr_not_compatible)
2876 << Var << TestIsLessOp << NewStep->getSourceRange();
2877 SemaRef.Diag(ConditionLoc,
2878 diag::note_omp_loop_cond_requres_compatible_incr)
2879 << TestIsLessOp << ConditionSrcRange;
2880 return true;
2881 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002882 if (TestIsLessOp == Subtract) {
2883 NewStep = SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus,
2884 NewStep).get();
2885 Subtract = !Subtract;
2886 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002887 }
2888
2889 Step = NewStep;
2890 SubtractStep = Subtract;
2891 return false;
2892}
2893
Alexey Bataev9c821032015-04-30 04:23:23 +00002894bool OpenMPIterationSpaceChecker::CheckInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002895 // Check init-expr for canonical loop form and save loop counter
2896 // variable - #Var and its initialization value - #LB.
2897 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
2898 // var = lb
2899 // integer-type var = lb
2900 // random-access-iterator-type var = lb
2901 // pointer-type var = lb
2902 //
2903 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00002904 if (EmitDiags) {
2905 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
2906 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002907 return true;
2908 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00002909 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002910 if (Expr *E = dyn_cast<Expr>(S))
2911 S = E->IgnoreParens();
2912 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2913 if (BO->getOpcode() == BO_Assign)
2914 if (auto DRE = dyn_cast<DeclRefExpr>(BO->getLHS()->IgnoreParens()))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002915 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
Alexander Musmana5f070a2014-10-01 06:03:56 +00002916 BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002917 } else if (auto DS = dyn_cast<DeclStmt>(S)) {
2918 if (DS->isSingleDecl()) {
2919 if (auto Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00002920 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002921 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00002922 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002923 SemaRef.Diag(S->getLocStart(),
2924 diag::ext_omp_loop_not_canonical_init)
2925 << S->getSourceRange();
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002926 return SetVarAndLB(Var, nullptr, Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002927 }
2928 }
2929 }
2930 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S))
2931 if (CE->getOperator() == OO_Equal)
2932 if (auto DRE = dyn_cast<DeclRefExpr>(CE->getArg(0)))
Alexey Bataevcaf09b02014-07-25 06:27:47 +00002933 return SetVarAndLB(dyn_cast<VarDecl>(DRE->getDecl()), DRE,
2934 CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002935
Alexey Bataev9c821032015-04-30 04:23:23 +00002936 if (EmitDiags) {
2937 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_init)
2938 << S->getSourceRange();
2939 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002940 return true;
2941}
2942
Alexey Bataev23b69422014-06-18 07:08:49 +00002943/// \brief Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002944/// variable (which may be the loop variable) if possible.
2945static const VarDecl *GetInitVarDecl(const Expr *E) {
2946 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00002947 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002948 E = getExprAsWritten(E);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002949 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
2950 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00002951 if ((Ctor->isCopyOrMoveConstructor() ||
2952 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
2953 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002954 E = CE->getArg(0)->IgnoreParenImpCasts();
2955 auto DRE = dyn_cast_or_null<DeclRefExpr>(E);
2956 if (!DRE)
2957 return nullptr;
2958 return dyn_cast<VarDecl>(DRE->getDecl());
2959}
2960
2961bool OpenMPIterationSpaceChecker::CheckCond(Expr *S) {
2962 // Check test-expr for canonical form, save upper-bound UB, flags for
2963 // less/greater and for strict/non-strict comparison.
2964 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
2965 // var relational-op b
2966 // b relational-op var
2967 //
2968 if (!S) {
2969 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << Var;
2970 return true;
2971 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00002972 S = getExprAsWritten(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00002973 SourceLocation CondLoc = S->getLocStart();
2974 if (auto BO = dyn_cast<BinaryOperator>(S)) {
2975 if (BO->isRelationalOp()) {
2976 if (GetInitVarDecl(BO->getLHS()) == Var)
2977 return SetUB(BO->getRHS(),
2978 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
2979 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2980 BO->getSourceRange(), BO->getOperatorLoc());
2981 if (GetInitVarDecl(BO->getRHS()) == Var)
2982 return SetUB(BO->getLHS(),
2983 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
2984 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
2985 BO->getSourceRange(), BO->getOperatorLoc());
2986 }
2987 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
2988 if (CE->getNumArgs() == 2) {
2989 auto Op = CE->getOperator();
2990 switch (Op) {
2991 case OO_Greater:
2992 case OO_GreaterEqual:
2993 case OO_Less:
2994 case OO_LessEqual:
2995 if (GetInitVarDecl(CE->getArg(0)) == Var)
2996 return SetUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
2997 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
2998 CE->getOperatorLoc());
2999 if (GetInitVarDecl(CE->getArg(1)) == Var)
3000 return SetUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
3001 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
3002 CE->getOperatorLoc());
3003 break;
3004 default:
3005 break;
3006 }
3007 }
3008 }
3009 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
3010 << S->getSourceRange() << Var;
3011 return true;
3012}
3013
3014bool OpenMPIterationSpaceChecker::CheckIncRHS(Expr *RHS) {
3015 // RHS of canonical loop form increment can be:
3016 // var + incr
3017 // incr + var
3018 // var - incr
3019 //
3020 RHS = RHS->IgnoreParenImpCasts();
3021 if (auto BO = dyn_cast<BinaryOperator>(RHS)) {
3022 if (BO->isAdditiveOp()) {
3023 bool IsAdd = BO->getOpcode() == BO_Add;
3024 if (GetInitVarDecl(BO->getLHS()) == Var)
3025 return SetStep(BO->getRHS(), !IsAdd);
3026 if (IsAdd && GetInitVarDecl(BO->getRHS()) == Var)
3027 return SetStep(BO->getLHS(), false);
3028 }
3029 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
3030 bool IsAdd = CE->getOperator() == OO_Plus;
3031 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
3032 if (GetInitVarDecl(CE->getArg(0)) == Var)
3033 return SetStep(CE->getArg(1), !IsAdd);
3034 if (IsAdd && GetInitVarDecl(CE->getArg(1)) == Var)
3035 return SetStep(CE->getArg(0), false);
3036 }
3037 }
3038 SemaRef.Diag(RHS->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3039 << RHS->getSourceRange() << Var;
3040 return true;
3041}
3042
3043bool OpenMPIterationSpaceChecker::CheckInc(Expr *S) {
3044 // Check incr-expr for canonical loop form and return true if it
3045 // does not conform.
3046 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
3047 // ++var
3048 // var++
3049 // --var
3050 // var--
3051 // var += incr
3052 // var -= incr
3053 // var = var + incr
3054 // var = incr + var
3055 // var = var - incr
3056 //
3057 if (!S) {
3058 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << Var;
3059 return true;
3060 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003061 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003062 S = S->IgnoreParens();
3063 if (auto UO = dyn_cast<UnaryOperator>(S)) {
3064 if (UO->isIncrementDecrementOp() && GetInitVarDecl(UO->getSubExpr()) == Var)
3065 return SetStep(
3066 SemaRef.ActOnIntegerConstant(UO->getLocStart(),
3067 (UO->isDecrementOp() ? -1 : 1)).get(),
3068 false);
3069 } else if (auto BO = dyn_cast<BinaryOperator>(S)) {
3070 switch (BO->getOpcode()) {
3071 case BO_AddAssign:
3072 case BO_SubAssign:
3073 if (GetInitVarDecl(BO->getLHS()) == Var)
3074 return SetStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
3075 break;
3076 case BO_Assign:
3077 if (GetInitVarDecl(BO->getLHS()) == Var)
3078 return CheckIncRHS(BO->getRHS());
3079 break;
3080 default:
3081 break;
3082 }
3083 } else if (auto CE = dyn_cast<CXXOperatorCallExpr>(S)) {
3084 switch (CE->getOperator()) {
3085 case OO_PlusPlus:
3086 case OO_MinusMinus:
3087 if (GetInitVarDecl(CE->getArg(0)) == Var)
3088 return SetStep(
3089 SemaRef.ActOnIntegerConstant(
3090 CE->getLocStart(),
3091 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1)).get(),
3092 false);
3093 break;
3094 case OO_PlusEqual:
3095 case OO_MinusEqual:
3096 if (GetInitVarDecl(CE->getArg(0)) == Var)
3097 return SetStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
3098 break;
3099 case OO_Equal:
3100 if (GetInitVarDecl(CE->getArg(0)) == Var)
3101 return CheckIncRHS(CE->getArg(1));
3102 break;
3103 default:
3104 break;
3105 }
3106 }
3107 SemaRef.Diag(S->getLocStart(), diag::err_omp_loop_not_canonical_incr)
3108 << S->getSourceRange() << Var;
3109 return true;
3110}
Alexander Musmana5f070a2014-10-01 06:03:56 +00003111
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003112namespace {
3113// Transform variables declared in GNU statement expressions to new ones to
3114// avoid crash on codegen.
3115class TransformToNewDefs : public TreeTransform<TransformToNewDefs> {
3116 typedef TreeTransform<TransformToNewDefs> BaseTransform;
3117
3118public:
3119 TransformToNewDefs(Sema &SemaRef) : BaseTransform(SemaRef) {}
3120
3121 Decl *TransformDefinition(SourceLocation Loc, Decl *D) {
3122 if (auto *VD = cast<VarDecl>(D))
3123 if (!isa<ParmVarDecl>(D) && !isa<VarTemplateSpecializationDecl>(D) &&
3124 !isa<ImplicitParamDecl>(D)) {
3125 auto *NewVD = VarDecl::Create(
3126 SemaRef.Context, VD->getDeclContext(), VD->getLocStart(),
3127 VD->getLocation(), VD->getIdentifier(), VD->getType(),
3128 VD->getTypeSourceInfo(), VD->getStorageClass());
3129 NewVD->setTSCSpec(VD->getTSCSpec());
3130 NewVD->setInit(VD->getInit());
3131 NewVD->setInitStyle(VD->getInitStyle());
3132 NewVD->setExceptionVariable(VD->isExceptionVariable());
3133 NewVD->setNRVOVariable(VD->isNRVOVariable());
3134 NewVD->setCXXForRangeDecl(VD->isInExternCXXContext());
3135 NewVD->setConstexpr(VD->isConstexpr());
3136 NewVD->setInitCapture(VD->isInitCapture());
3137 NewVD->setPreviousDeclInSameBlockScope(
3138 VD->isPreviousDeclInSameBlockScope());
3139 VD->getDeclContext()->addHiddenDecl(NewVD);
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003140 if (VD->hasAttrs())
3141 NewVD->setAttrs(VD->getAttrs());
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003142 transformedLocalDecl(VD, NewVD);
3143 return NewVD;
3144 }
3145 return BaseTransform::TransformDefinition(Loc, D);
3146 }
3147
3148 ExprResult TransformDeclRefExpr(DeclRefExpr *E) {
3149 if (auto *NewD = TransformDecl(E->getExprLoc(), E->getDecl()))
3150 if (E->getDecl() != NewD) {
3151 NewD->setReferenced();
3152 NewD->markUsed(SemaRef.Context);
3153 return DeclRefExpr::Create(
3154 SemaRef.Context, E->getQualifierLoc(), E->getTemplateKeywordLoc(),
3155 cast<ValueDecl>(NewD), E->refersToEnclosingVariableOrCapture(),
3156 E->getNameInfo(), E->getType(), E->getValueKind());
3157 }
3158 return BaseTransform::TransformDeclRefExpr(E);
3159 }
3160};
3161}
3162
Alexander Musmana5f070a2014-10-01 06:03:56 +00003163/// \brief Build the expression to calculate the number of iterations.
Alexander Musman174b3ca2014-10-06 11:16:29 +00003164Expr *
3165OpenMPIterationSpaceChecker::BuildNumIterations(Scope *S,
3166 const bool LimitedType) const {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003167 TransformToNewDefs Transform(SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003168 ExprResult Diff;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003169 auto VarType = Var->getType().getNonReferenceType();
3170 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003171 SemaRef.getLangOpts().CPlusPlus) {
3172 // Upper - Lower
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003173 auto *UBExpr = TestIsLessOp ? UB : LB;
3174 auto *LBExpr = TestIsLessOp ? LB : UB;
3175 Expr *Upper = Transform.TransformExpr(UBExpr).get();
3176 Expr *Lower = Transform.TransformExpr(LBExpr).get();
3177 if (!Upper || !Lower)
3178 return nullptr;
3179 Upper = SemaRef.PerformImplicitConversion(Upper, UBExpr->getType(),
3180 Sema::AA_Converting,
3181 /*AllowExplicit=*/true)
3182 .get();
3183 Lower = SemaRef.PerformImplicitConversion(Lower, LBExpr->getType(),
3184 Sema::AA_Converting,
3185 /*AllowExplicit=*/true)
3186 .get();
3187 if (!Upper || !Lower)
3188 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003189
3190 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
3191
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003192 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00003193 // BuildBinOp already emitted error, this one is to point user to upper
3194 // and lower bound, and to tell what is passed to 'operator-'.
3195 SemaRef.Diag(Upper->getLocStart(), diag::err_omp_loop_diff_cxx)
3196 << Upper->getSourceRange() << Lower->getSourceRange();
3197 return nullptr;
3198 }
3199 }
3200
3201 if (!Diff.isUsable())
3202 return nullptr;
3203
3204 // Upper - Lower [- 1]
3205 if (TestIsStrictOp)
3206 Diff = SemaRef.BuildBinOp(
3207 S, DefaultLoc, BO_Sub, Diff.get(),
3208 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3209 if (!Diff.isUsable())
3210 return nullptr;
3211
3212 // Upper - Lower [- 1] + Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003213 auto NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
3214 if (NewStep.isInvalid())
3215 return nullptr;
3216 NewStep = SemaRef.PerformImplicitConversion(
3217 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
3218 /*AllowExplicit=*/true);
3219 if (NewStep.isInvalid())
3220 return nullptr;
3221 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003222 if (!Diff.isUsable())
3223 return nullptr;
3224
3225 // Parentheses (for dumping/debugging purposes only).
3226 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
3227 if (!Diff.isUsable())
3228 return nullptr;
3229
3230 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003231 NewStep = Transform.TransformExpr(Step->IgnoreImplicit());
3232 if (NewStep.isInvalid())
3233 return nullptr;
3234 NewStep = SemaRef.PerformImplicitConversion(
3235 NewStep.get(), Step->IgnoreImplicit()->getType(), Sema::AA_Converting,
3236 /*AllowExplicit=*/true);
3237 if (NewStep.isInvalid())
3238 return nullptr;
3239 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003240 if (!Diff.isUsable())
3241 return nullptr;
3242
Alexander Musman174b3ca2014-10-06 11:16:29 +00003243 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003244 QualType Type = Diff.get()->getType();
3245 auto &C = SemaRef.Context;
3246 bool UseVarType = VarType->hasIntegerRepresentation() &&
3247 C.getTypeSize(Type) > C.getTypeSize(VarType);
3248 if (!Type->isIntegerType() || UseVarType) {
3249 unsigned NewSize =
3250 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
3251 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
3252 : Type->hasSignedIntegerRepresentation();
3253 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
3254 Diff = SemaRef.PerformImplicitConversion(
3255 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
3256 if (!Diff.isUsable())
3257 return nullptr;
3258 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00003259 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00003260 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
3261 if (NewSize != C.getTypeSize(Type)) {
3262 if (NewSize < C.getTypeSize(Type)) {
3263 assert(NewSize == 64 && "incorrect loop var size");
3264 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
3265 << InitSrcRange << ConditionSrcRange;
3266 }
3267 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003268 NewSize, Type->hasSignedIntegerRepresentation() ||
3269 C.getTypeSize(Type) < NewSize);
Alexander Musman174b3ca2014-10-06 11:16:29 +00003270 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
3271 Sema::AA_Converting, true);
3272 if (!Diff.isUsable())
3273 return nullptr;
3274 }
3275 }
3276
Alexander Musmana5f070a2014-10-01 06:03:56 +00003277 return Diff.get();
3278}
3279
Alexey Bataev62dbb972015-04-22 11:59:37 +00003280Expr *OpenMPIterationSpaceChecker::BuildPreCond(Scope *S, Expr *Cond) const {
3281 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
3282 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
3283 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003284 TransformToNewDefs Transform(SemaRef);
3285
3286 auto NewLB = Transform.TransformExpr(LB);
3287 auto NewUB = Transform.TransformExpr(UB);
3288 if (NewLB.isInvalid() || NewUB.isInvalid())
3289 return Cond;
3290 NewLB = SemaRef.PerformImplicitConversion(NewLB.get(), LB->getType(),
3291 Sema::AA_Converting,
3292 /*AllowExplicit=*/true);
3293 NewUB = SemaRef.PerformImplicitConversion(NewUB.get(), UB->getType(),
3294 Sema::AA_Converting,
3295 /*AllowExplicit=*/true);
3296 if (NewLB.isInvalid() || NewUB.isInvalid())
3297 return Cond;
Alexey Bataev62dbb972015-04-22 11:59:37 +00003298 auto CondExpr = SemaRef.BuildBinOp(
3299 S, DefaultLoc, TestIsLessOp ? (TestIsStrictOp ? BO_LT : BO_LE)
3300 : (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003301 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00003302 if (CondExpr.isUsable()) {
3303 CondExpr = SemaRef.PerformImplicitConversion(
3304 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
3305 /*AllowExplicit=*/true);
3306 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00003307 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
3308 // Otherwise use original loop conditon and evaluate it in runtime.
3309 return CondExpr.isUsable() ? CondExpr.get() : Cond;
3310}
3311
Alexander Musmana5f070a2014-10-01 06:03:56 +00003312/// \brief Build reference expression to the counter be used for codegen.
3313Expr *OpenMPIterationSpaceChecker::BuildCounterVar() const {
Alexey Bataeva8899172015-08-06 12:30:57 +00003314 return buildDeclRefExpr(SemaRef, Var, Var->getType().getNonReferenceType(),
3315 DefaultLoc);
3316}
3317
3318Expr *OpenMPIterationSpaceChecker::BuildPrivateCounterVar() const {
3319 if (Var && !Var->isInvalidDecl()) {
3320 auto Type = Var->getType().getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00003321 auto *PrivateVar =
3322 buildVarDecl(SemaRef, DefaultLoc, Type, Var->getName(),
3323 Var->hasAttrs() ? &Var->getAttrs() : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00003324 if (PrivateVar->isInvalidDecl())
3325 return nullptr;
3326 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
3327 }
3328 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003329}
3330
3331/// \brief Build initization of the counter be used for codegen.
3332Expr *OpenMPIterationSpaceChecker::BuildCounterInit() const { return LB; }
3333
3334/// \brief Build step of the counter be used for codegen.
3335Expr *OpenMPIterationSpaceChecker::BuildCounterStep() const { return Step; }
3336
3337/// \brief Iteration space of a single for loop.
3338struct LoopIterationSpace {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003339 /// \brief Condition of the loop.
3340 Expr *PreCond;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003341 /// \brief This expression calculates the number of iterations in the loop.
3342 /// It is always possible to calculate it before starting the loop.
3343 Expr *NumIterations;
3344 /// \brief The loop counter variable.
3345 Expr *CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00003346 /// \brief Private loop counter variable.
3347 Expr *PrivateCounterVar;
Alexander Musmana5f070a2014-10-01 06:03:56 +00003348 /// \brief This is initializer for the initial value of #CounterVar.
3349 Expr *CounterInit;
3350 /// \brief This is step for the #CounterVar used to generate its update:
3351 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
3352 Expr *CounterStep;
3353 /// \brief Should step be subtracted?
3354 bool Subtract;
3355 /// \brief Source range of the loop init.
3356 SourceRange InitSrcRange;
3357 /// \brief Source range of the loop condition.
3358 SourceRange CondSrcRange;
3359 /// \brief Source range of the loop increment.
3360 SourceRange IncSrcRange;
3361};
3362
Alexey Bataev23b69422014-06-18 07:08:49 +00003363} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003364
Alexey Bataev9c821032015-04-30 04:23:23 +00003365void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
3366 assert(getLangOpts().OpenMP && "OpenMP is not active.");
3367 assert(Init && "Expected loop in canonical form.");
3368 unsigned CollapseIteration = DSAStack->getCollapseNumber();
3369 if (CollapseIteration > 0 &&
3370 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
3371 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
3372 if (!ISC.CheckInit(Init, /*EmitDiags=*/false)) {
3373 DSAStack->addLoopControlVariable(ISC.GetLoopVar());
3374 }
3375 DSAStack->setCollapseNumber(CollapseIteration - 1);
3376 }
3377}
3378
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003379/// \brief Called on a for stmt to check and extract its iteration space
3380/// for further processing (such as collapsing).
Alexey Bataev4acb8592014-07-07 13:01:15 +00003381static bool CheckOpenMPIterationSpace(
3382 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
3383 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003384 Expr *CollapseLoopCountExpr, Expr *OrderedLoopCountExpr,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003385 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
3386 LoopIterationSpace &ResultIterSpace) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003387 // OpenMP [2.6, Canonical Loop Form]
3388 // for (init-expr; test-expr; incr-expr) structured-block
3389 auto For = dyn_cast_or_null<ForStmt>(S);
3390 if (!For) {
3391 SemaRef.Diag(S->getLocStart(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003392 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
3393 << getOpenMPDirectiveName(DKind) << NestedLoopCount
3394 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
3395 if (NestedLoopCount > 1) {
3396 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
3397 SemaRef.Diag(DSA.getConstructLoc(),
3398 diag::note_omp_collapse_ordered_expr)
3399 << 2 << CollapseLoopCountExpr->getSourceRange()
3400 << OrderedLoopCountExpr->getSourceRange();
3401 else if (CollapseLoopCountExpr)
3402 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
3403 diag::note_omp_collapse_ordered_expr)
3404 << 0 << CollapseLoopCountExpr->getSourceRange();
3405 else
3406 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
3407 diag::note_omp_collapse_ordered_expr)
3408 << 1 << OrderedLoopCountExpr->getSourceRange();
3409 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003410 return true;
3411 }
3412 assert(For->getBody());
3413
3414 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
3415
3416 // Check init.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003417 auto Init = For->getInit();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003418 if (ISC.CheckInit(Init)) {
3419 return true;
3420 }
3421
3422 bool HasErrors = false;
3423
3424 // Check loop variable's type.
Alexey Bataevdf9b1592014-06-25 04:09:13 +00003425 auto Var = ISC.GetLoopVar();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003426
3427 // OpenMP [2.6, Canonical Loop Form]
3428 // Var is one of the following:
3429 // A variable of signed or unsigned integer type.
3430 // For C++, a variable of a random access iterator type.
3431 // For C, a variable of a pointer type.
Alexey Bataeva8899172015-08-06 12:30:57 +00003432 auto VarType = Var->getType().getNonReferenceType();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003433 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
3434 !VarType->isPointerType() &&
3435 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
3436 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_variable_type)
3437 << SemaRef.getLangOpts().CPlusPlus;
3438 HasErrors = true;
3439 }
3440
Alexey Bataev4acb8592014-07-07 13:01:15 +00003441 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in a
3442 // Construct
3443 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3444 // parallel for construct is (are) private.
3445 // The loop iteration variable in the associated for-loop of a simd construct
3446 // with just one associated for-loop is linear with a constant-linear-step
3447 // that is the increment of the associated for-loop.
3448 // Exclude loop var from the list of variables with implicitly defined data
3449 // sharing attributes.
Benjamin Kramerad8e0792014-10-10 15:32:48 +00003450 VarsWithImplicitDSA.erase(Var);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003451
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003452 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced in
3453 // a Construct, C/C++].
Alexey Bataevcefffae2014-06-23 08:21:53 +00003454 // The loop iteration variable in the associated for-loop of a simd construct
3455 // with just one associated for-loop may be listed in a linear clause with a
3456 // constant-linear-step that is the increment of the associated for-loop.
Alexey Bataevf29276e2014-06-18 04:14:57 +00003457 // The loop iteration variable(s) in the associated for-loop(s) of a for or
3458 // parallel for construct may be listed in a private or lastprivate clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003459 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(Var, false);
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003460 auto LoopVarRefExpr = ISC.GetLoopVarRefExpr();
3461 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
3462 // declared in the loop and it is predetermined as a private.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003463 auto PredeterminedCKind =
3464 isOpenMPSimdDirective(DKind)
3465 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
3466 : OMPC_private;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003467 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
Alexey Bataev0c024df2015-05-12 09:02:07 +00003468 DVar.CKind != OMPC_threadprivate && DVar.CKind != PredeterminedCKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003469 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
3470 isOpenMPDistributeDirective(DKind)) &&
Alexey Bataev49f6e782015-12-01 04:18:41 +00003471 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
3472 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate &&
3473 DVar.CKind != OMPC_threadprivate)) &&
Alexey Bataev0c024df2015-05-12 09:02:07 +00003474 ((DVar.CKind != OMPC_private && DVar.CKind != OMPC_threadprivate) ||
3475 DVar.RefExpr != nullptr)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003476 SemaRef.Diag(Init->getLocStart(), diag::err_omp_loop_var_dsa)
Alexey Bataev4acb8592014-07-07 13:01:15 +00003477 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
3478 << getOpenMPClauseName(PredeterminedCKind);
Alexey Bataevf2453a02015-05-06 07:25:08 +00003479 if (DVar.RefExpr == nullptr)
3480 DVar.CKind = PredeterminedCKind;
3481 ReportOriginalDSA(SemaRef, &DSA, Var, DVar, /*IsLoopIterVar=*/true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003482 HasErrors = true;
Alexey Bataevcaf09b02014-07-25 06:27:47 +00003483 } else if (LoopVarRefExpr != nullptr) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00003484 // Make the loop iteration variable private (for worksharing constructs),
3485 // linear (for simd directives with the only one associated loop) or
Alexey Bataev10e775f2015-07-30 11:36:16 +00003486 // lastprivate (for simd directives with several collapsed or ordered
3487 // loops).
Alexey Bataev9aba41c2014-11-14 04:08:45 +00003488 if (DVar.CKind == OMPC_unknown)
3489 DVar = DSA.hasDSA(Var, isOpenMPPrivate, MatchesAlways(),
3490 /*FromParent=*/false);
Alexey Bataev9c821032015-04-30 04:23:23 +00003491 DSA.addDSA(Var, LoopVarRefExpr, PredeterminedCKind);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003492 }
3493
Alexey Bataev7ff55242014-06-19 09:13:45 +00003494 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
Alexander Musman1bb328c2014-06-04 13:06:39 +00003495
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003496 // Check test-expr.
3497 HasErrors |= ISC.CheckCond(For->getCond());
3498
3499 // Check incr-expr.
3500 HasErrors |= ISC.CheckInc(For->getInc());
3501
Alexander Musmana5f070a2014-10-01 06:03:56 +00003502 if (ISC.Dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003503 return HasErrors;
3504
Alexander Musmana5f070a2014-10-01 06:03:56 +00003505 // Build the loop's iteration space representation.
Alexey Bataev62dbb972015-04-22 11:59:37 +00003506 ResultIterSpace.PreCond = ISC.BuildPreCond(DSA.getCurScope(), For->getCond());
Alexander Musman174b3ca2014-10-06 11:16:29 +00003507 ResultIterSpace.NumIterations = ISC.BuildNumIterations(
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003508 DSA.getCurScope(), (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003509 isOpenMPTaskLoopDirective(DKind) ||
3510 isOpenMPDistributeDirective(DKind)));
Alexander Musmana5f070a2014-10-01 06:03:56 +00003511 ResultIterSpace.CounterVar = ISC.BuildCounterVar();
Alexey Bataeva8899172015-08-06 12:30:57 +00003512 ResultIterSpace.PrivateCounterVar = ISC.BuildPrivateCounterVar();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003513 ResultIterSpace.CounterInit = ISC.BuildCounterInit();
3514 ResultIterSpace.CounterStep = ISC.BuildCounterStep();
3515 ResultIterSpace.InitSrcRange = ISC.GetInitSrcRange();
3516 ResultIterSpace.CondSrcRange = ISC.GetConditionSrcRange();
3517 ResultIterSpace.IncSrcRange = ISC.GetIncrementSrcRange();
3518 ResultIterSpace.Subtract = ISC.ShouldSubtractStep();
3519
Alexey Bataev62dbb972015-04-22 11:59:37 +00003520 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
3521 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003522 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00003523 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00003524 ResultIterSpace.CounterInit == nullptr ||
3525 ResultIterSpace.CounterStep == nullptr);
3526
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003527 return HasErrors;
3528}
3529
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003530/// \brief Build 'VarRef = Start.
3531static ExprResult BuildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc,
3532 ExprResult VarRef, ExprResult Start) {
3533 TransformToNewDefs Transform(SemaRef);
3534 // Build 'VarRef = Start.
3535 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
3536 if (NewStart.isInvalid())
3537 return ExprError();
3538 NewStart = SemaRef.PerformImplicitConversion(
3539 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
3540 Sema::AA_Converting,
3541 /*AllowExplicit=*/true);
3542 if (NewStart.isInvalid())
3543 return ExprError();
3544 NewStart = SemaRef.PerformImplicitConversion(
3545 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
3546 /*AllowExplicit=*/true);
3547 if (!NewStart.isUsable())
3548 return ExprError();
3549
3550 auto Init =
3551 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
3552 return Init;
3553}
3554
Alexander Musmana5f070a2014-10-01 06:03:56 +00003555/// \brief Build 'VarRef = Start + Iter * Step'.
3556static ExprResult BuildCounterUpdate(Sema &SemaRef, Scope *S,
3557 SourceLocation Loc, ExprResult VarRef,
3558 ExprResult Start, ExprResult Iter,
3559 ExprResult Step, bool Subtract) {
3560 // Add parentheses (for debugging purposes only).
3561 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
3562 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
3563 !Step.isUsable())
3564 return ExprError();
3565
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003566 TransformToNewDefs Transform(SemaRef);
3567 auto NewStep = Transform.TransformExpr(Step.get()->IgnoreImplicit());
3568 if (NewStep.isInvalid())
3569 return ExprError();
3570 NewStep = SemaRef.PerformImplicitConversion(
3571 NewStep.get(), Step.get()->IgnoreImplicit()->getType(),
3572 Sema::AA_Converting,
3573 /*AllowExplicit=*/true);
3574 if (NewStep.isInvalid())
3575 return ExprError();
3576 ExprResult Update =
3577 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003578 if (!Update.isUsable())
3579 return ExprError();
3580
3581 // Build 'VarRef = Start + Iter * Step'.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003582 auto NewStart = Transform.TransformExpr(Start.get()->IgnoreImplicit());
3583 if (NewStart.isInvalid())
3584 return ExprError();
3585 NewStart = SemaRef.PerformImplicitConversion(
3586 NewStart.get(), Start.get()->IgnoreImplicit()->getType(),
3587 Sema::AA_Converting,
3588 /*AllowExplicit=*/true);
3589 if (NewStart.isInvalid())
3590 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00003591 Update = SemaRef.BuildBinOp(S, Loc, (Subtract ? BO_Sub : BO_Add),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003592 NewStart.get(), Update.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003593 if (!Update.isUsable())
3594 return ExprError();
3595
3596 Update = SemaRef.PerformImplicitConversion(
3597 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
3598 if (!Update.isUsable())
3599 return ExprError();
3600
3601 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
3602 return Update;
3603}
3604
3605/// \brief Convert integer expression \a E to make it have at least \a Bits
3606/// bits.
3607static ExprResult WidenIterationCount(unsigned Bits, Expr *E,
3608 Sema &SemaRef) {
3609 if (E == nullptr)
3610 return ExprError();
3611 auto &C = SemaRef.Context;
3612 QualType OldType = E->getType();
3613 unsigned HasBits = C.getTypeSize(OldType);
3614 if (HasBits >= Bits)
3615 return ExprResult(E);
3616 // OK to convert to signed, because new type has more bits than old.
3617 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
3618 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
3619 true);
3620}
3621
3622/// \brief Check if the given expression \a E is a constant integer that fits
3623/// into \a Bits bits.
3624static bool FitsInto(unsigned Bits, bool Signed, Expr *E, Sema &SemaRef) {
3625 if (E == nullptr)
3626 return false;
3627 llvm::APSInt Result;
3628 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
3629 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
3630 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003631}
3632
3633/// \brief Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00003634/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
3635/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00003636static unsigned
Alexey Bataev10e775f2015-07-30 11:36:16 +00003637CheckOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
3638 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
3639 DSAStackTy &DSA,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003640 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00003641 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003642 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003643 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003644 // Found 'collapse' clause - calculate collapse number.
3645 llvm::APSInt Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00003646 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Alexey Bataev7b6bc882015-11-26 07:50:39 +00003647 NestedLoopCount = Result.getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00003648 }
3649 if (OrderedLoopCountExpr) {
3650 // Found 'ordered' clause - calculate collapse number.
3651 llvm::APSInt Result;
Alexey Bataev7b6bc882015-11-26 07:50:39 +00003652 if (OrderedLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext())) {
3653 if (Result.getLimitedValue() < NestedLoopCount) {
3654 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
3655 diag::err_omp_wrong_ordered_loop_count)
3656 << OrderedLoopCountExpr->getSourceRange();
3657 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
3658 diag::note_collapse_loop_count)
3659 << CollapseLoopCountExpr->getSourceRange();
3660 }
3661 NestedLoopCount = Result.getLimitedValue();
3662 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003663 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003664 // This is helper routine for loop directives (e.g., 'for', 'simd',
3665 // 'for simd', etc.).
Alexander Musmana5f070a2014-10-01 06:03:56 +00003666 SmallVector<LoopIterationSpace, 4> IterSpaces;
3667 IterSpaces.resize(NestedLoopCount);
3668 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003669 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00003670 if (CheckOpenMPIterationSpace(DKind, CurStmt, SemaRef, DSA, Cnt,
Alexey Bataev10e775f2015-07-30 11:36:16 +00003671 NestedLoopCount, CollapseLoopCountExpr,
3672 OrderedLoopCountExpr, VarsWithImplicitDSA,
3673 IterSpaces[Cnt]))
Alexey Bataevabfc0692014-06-25 06:52:00 +00003674 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003675 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003676 // OpenMP [2.8.1, simd construct, Restrictions]
3677 // All loops associated with the construct must be perfectly nested; that
3678 // is, there must be no intervening code nor any OpenMP directive between
3679 // any two loops.
3680 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003681 }
3682
Alexander Musmana5f070a2014-10-01 06:03:56 +00003683 Built.clear(/* size */ NestedLoopCount);
3684
3685 if (SemaRef.CurContext->isDependentContext())
3686 return NestedLoopCount;
3687
3688 // An example of what is generated for the following code:
3689 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00003690 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00003691 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00003692 // for (k = 0; k < NK; ++k)
3693 // for (j = J0; j < NJ; j+=2) {
3694 // <loop body>
3695 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003696 //
3697 // We generate the code below.
3698 // Note: the loop body may be outlined in CodeGen.
3699 // Note: some counters may be C++ classes, operator- is used to find number of
3700 // iterations and operator+= to calculate counter value.
3701 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
3702 // or i64 is currently supported).
3703 //
3704 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
3705 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
3706 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
3707 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
3708 // // similar updates for vars in clauses (e.g. 'linear')
3709 // <loop body (using local i and j)>
3710 // }
3711 // i = NI; // assign final values of counters
3712 // j = NJ;
3713 //
3714
3715 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
3716 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00003717 // Precondition tests if there is at least one iteration (all conditions are
3718 // true).
3719 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003720 auto N0 = IterSpaces[0].NumIterations;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003721 ExprResult LastIteration32 = WidenIterationCount(
3722 32 /* Bits */, SemaRef.PerformImplicitConversion(
3723 N0->IgnoreImpCasts(), N0->getType(),
3724 Sema::AA_Converting, /*AllowExplicit=*/true)
3725 .get(),
3726 SemaRef);
3727 ExprResult LastIteration64 = WidenIterationCount(
3728 64 /* Bits */, SemaRef.PerformImplicitConversion(
3729 N0->IgnoreImpCasts(), N0->getType(),
3730 Sema::AA_Converting, /*AllowExplicit=*/true)
3731 .get(),
3732 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003733
3734 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
3735 return NestedLoopCount;
3736
3737 auto &C = SemaRef.Context;
3738 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
3739
3740 Scope *CurScope = DSA.getCurScope();
3741 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00003742 if (PreCond.isUsable()) {
3743 PreCond = SemaRef.BuildBinOp(CurScope, SourceLocation(), BO_LAnd,
3744 PreCond.get(), IterSpaces[Cnt].PreCond);
3745 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003746 auto N = IterSpaces[Cnt].NumIterations;
3747 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
3748 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003749 LastIteration32 = SemaRef.BuildBinOp(
3750 CurScope, SourceLocation(), BO_Mul, LastIteration32.get(),
3751 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
3752 Sema::AA_Converting,
3753 /*AllowExplicit=*/true)
3754 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003755 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003756 LastIteration64 = SemaRef.BuildBinOp(
3757 CurScope, SourceLocation(), BO_Mul, LastIteration64.get(),
3758 SemaRef.PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
3759 Sema::AA_Converting,
3760 /*AllowExplicit=*/true)
3761 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003762 }
3763
3764 // Choose either the 32-bit or 64-bit version.
3765 ExprResult LastIteration = LastIteration64;
3766 if (LastIteration32.isUsable() &&
3767 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
3768 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
3769 FitsInto(
3770 32 /* Bits */,
3771 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
3772 LastIteration64.get(), SemaRef)))
3773 LastIteration = LastIteration32;
3774
3775 if (!LastIteration.isUsable())
3776 return 0;
3777
3778 // Save the number of iterations.
3779 ExprResult NumIterations = LastIteration;
3780 {
3781 LastIteration = SemaRef.BuildBinOp(
3782 CurScope, SourceLocation(), BO_Sub, LastIteration.get(),
3783 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3784 if (!LastIteration.isUsable())
3785 return 0;
3786 }
3787
3788 // Calculate the last iteration number beforehand instead of doing this on
3789 // each iteration. Do not do this if the number of iterations may be kfold-ed.
3790 llvm::APSInt Result;
3791 bool IsConstant =
3792 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
3793 ExprResult CalcLastIteration;
3794 if (!IsConstant) {
3795 SourceLocation SaveLoc;
3796 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00003797 buildVarDecl(SemaRef, SaveLoc, LastIteration.get()->getType(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00003798 ".omp.last.iteration");
Alexey Bataev39f915b82015-05-08 10:41:21 +00003799 ExprResult SaveRef = buildDeclRefExpr(
3800 SemaRef, SaveVar, LastIteration.get()->getType(), SaveLoc);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003801 CalcLastIteration = SemaRef.BuildBinOp(CurScope, SaveLoc, BO_Assign,
3802 SaveRef.get(), LastIteration.get());
3803 LastIteration = SaveRef;
3804
3805 // Prepare SaveRef + 1.
3806 NumIterations = SemaRef.BuildBinOp(
3807 CurScope, SaveLoc, BO_Add, SaveRef.get(),
3808 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
3809 if (!NumIterations.isUsable())
3810 return 0;
3811 }
3812
3813 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
3814
Alexander Musmanc6388682014-12-15 07:07:06 +00003815 QualType VType = LastIteration.get()->getType();
3816 // Build variables passed into runtime, nesessary for worksharing directives.
3817 ExprResult LB, UB, IL, ST, EUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003818 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
3819 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00003820 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00003821 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
3822 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003823 SemaRef.AddInitializerToDecl(
3824 LBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
3825 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3826
3827 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00003828 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
3829 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003830 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
3831 /*DirectInit*/ false,
3832 /*TypeMayContainAuto*/ false);
3833
3834 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
3835 // This will be used to implement clause 'lastprivate'.
3836 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00003837 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
3838 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003839 SemaRef.AddInitializerToDecl(
3840 ILDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
3841 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3842
3843 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev39f915b82015-05-08 10:41:21 +00003844 VarDecl *STDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.stride");
3845 ST = buildDeclRefExpr(SemaRef, STDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00003846 SemaRef.AddInitializerToDecl(
3847 STDecl, SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
3848 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
3849
3850 // Build expression: UB = min(UB, LastIteration)
3851 // It is nesessary for CodeGen of directives with static scheduling.
3852 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
3853 UB.get(), LastIteration.get());
3854 ExprResult CondOp = SemaRef.ActOnConditionalOp(
3855 InitLoc, InitLoc, IsUBGreater.get(), LastIteration.get(), UB.get());
3856 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
3857 CondOp.get());
3858 EUB = SemaRef.ActOnFinishFullExpr(EUB.get());
3859 }
3860
3861 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003862 ExprResult IV;
3863 ExprResult Init;
3864 {
Alexey Bataev39f915b82015-05-08 10:41:21 +00003865 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.iv");
3866 IV = buildDeclRefExpr(SemaRef, IVDecl, VType, InitLoc);
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003867 Expr *RHS = (isOpenMPWorksharingDirective(DKind) ||
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003868 isOpenMPTaskLoopDirective(DKind) ||
3869 isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00003870 ? LB.get()
3871 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
3872 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
3873 Init = SemaRef.ActOnFinishFullExpr(Init.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003874 }
3875
Alexander Musmanc6388682014-12-15 07:07:06 +00003876 // Loop condition (IV < NumIterations) or (IV <= UB) for worksharing loops.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003877 SourceLocation CondLoc;
Alexander Musmanc6388682014-12-15 07:07:06 +00003878 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003879 (isOpenMPWorksharingDirective(DKind) ||
3880 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexander Musmanc6388682014-12-15 07:07:06 +00003881 ? SemaRef.BuildBinOp(CurScope, CondLoc, BO_LE, IV.get(), UB.get())
3882 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
3883 NumIterations.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00003884
3885 // Loop increment (IV = IV + 1)
3886 SourceLocation IncLoc;
3887 ExprResult Inc =
3888 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
3889 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
3890 if (!Inc.isUsable())
3891 return 0;
3892 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00003893 Inc = SemaRef.ActOnFinishFullExpr(Inc.get());
3894 if (!Inc.isUsable())
3895 return 0;
3896
3897 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
3898 // Used for directives with static scheduling.
3899 ExprResult NextLB, NextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003900 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
3901 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00003902 // LB + ST
3903 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
3904 if (!NextLB.isUsable())
3905 return 0;
3906 // LB = LB + ST
3907 NextLB =
3908 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
3909 NextLB = SemaRef.ActOnFinishFullExpr(NextLB.get());
3910 if (!NextLB.isUsable())
3911 return 0;
3912 // UB + ST
3913 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
3914 if (!NextUB.isUsable())
3915 return 0;
3916 // UB = UB + ST
3917 NextUB =
3918 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
3919 NextUB = SemaRef.ActOnFinishFullExpr(NextUB.get());
3920 if (!NextUB.isUsable())
3921 return 0;
3922 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003923
3924 // Build updates and final values of the loop counters.
3925 bool HasErrors = false;
3926 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003927 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00003928 Built.Updates.resize(NestedLoopCount);
3929 Built.Finals.resize(NestedLoopCount);
3930 {
3931 ExprResult Div;
3932 // Go from inner nested loop to outer.
3933 for (int Cnt = NestedLoopCount - 1; Cnt >= 0; --Cnt) {
3934 LoopIterationSpace &IS = IterSpaces[Cnt];
3935 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
3936 // Build: Iter = (IV / Div) % IS.NumIters
3937 // where Div is product of previous iterations' IS.NumIters.
3938 ExprResult Iter;
3939 if (Div.isUsable()) {
3940 Iter =
3941 SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div, IV.get(), Div.get());
3942 } else {
3943 Iter = IV;
3944 assert((Cnt == (int)NestedLoopCount - 1) &&
3945 "unusable div expected on first iteration only");
3946 }
3947
3948 if (Cnt != 0 && Iter.isUsable())
3949 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Rem, Iter.get(),
3950 IS.NumIterations);
3951 if (!Iter.isUsable()) {
3952 HasErrors = true;
3953 break;
3954 }
3955
Alexey Bataev39f915b82015-05-08 10:41:21 +00003956 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
3957 auto *CounterVar = buildDeclRefExpr(
3958 SemaRef, cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl()),
3959 IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
3960 /*RefersToCapture=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00003961 ExprResult Init = BuildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
3962 IS.CounterInit);
3963 if (!Init.isUsable()) {
3964 HasErrors = true;
3965 break;
3966 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00003967 ExprResult Update =
Alexey Bataev39f915b82015-05-08 10:41:21 +00003968 BuildCounterUpdate(SemaRef, CurScope, UpdLoc, CounterVar,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003969 IS.CounterInit, Iter, IS.CounterStep, IS.Subtract);
3970 if (!Update.isUsable()) {
3971 HasErrors = true;
3972 break;
3973 }
3974
3975 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
3976 ExprResult Final = BuildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00003977 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexander Musmana5f070a2014-10-01 06:03:56 +00003978 IS.NumIterations, IS.CounterStep, IS.Subtract);
3979 if (!Final.isUsable()) {
3980 HasErrors = true;
3981 break;
3982 }
3983
3984 // Build Div for the next iteration: Div <- Div * IS.NumIters
3985 if (Cnt != 0) {
3986 if (Div.isUnset())
3987 Div = IS.NumIterations;
3988 else
3989 Div = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Div.get(),
3990 IS.NumIterations);
3991
3992 // Add parentheses (for debugging purposes only).
3993 if (Div.isUsable())
3994 Div = SemaRef.ActOnParenExpr(UpdLoc, UpdLoc, Div.get());
3995 if (!Div.isUsable()) {
3996 HasErrors = true;
3997 break;
3998 }
3999 }
4000 if (!Update.isUsable() || !Final.isUsable()) {
4001 HasErrors = true;
4002 break;
4003 }
4004 // Save results
4005 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00004006 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004007 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004008 Built.Updates[Cnt] = Update.get();
4009 Built.Finals[Cnt] = Final.get();
4010 }
4011 }
4012
4013 if (HasErrors)
4014 return 0;
4015
4016 // Save results
4017 Built.IterationVarRef = IV.get();
4018 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00004019 Built.NumIterations = NumIterations.get();
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004020 Built.CalcLastIteration =
4021 SemaRef.ActOnFinishFullExpr(CalcLastIteration.get()).get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004022 Built.PreCond = PreCond.get();
4023 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004024 Built.Init = Init.get();
4025 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00004026 Built.LB = LB.get();
4027 Built.UB = UB.get();
4028 Built.IL = IL.get();
4029 Built.ST = ST.get();
4030 Built.EUB = EUB.get();
4031 Built.NLB = NextLB.get();
4032 Built.NUB = NextUB.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004033
Alexey Bataevabfc0692014-06-25 06:52:00 +00004034 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004035}
4036
Alexey Bataev10e775f2015-07-30 11:36:16 +00004037static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004038 auto CollapseClauses =
4039 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
4040 if (CollapseClauses.begin() != CollapseClauses.end())
4041 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00004042 return nullptr;
4043}
4044
Alexey Bataev10e775f2015-07-30 11:36:16 +00004045static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00004046 auto OrderedClauses =
4047 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
4048 if (OrderedClauses.begin() != OrderedClauses.end())
4049 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00004050 return nullptr;
4051}
4052
Alexey Bataev66b15b52015-08-21 11:14:16 +00004053static bool checkSimdlenSafelenValues(Sema &S, const Expr *Simdlen,
4054 const Expr *Safelen) {
4055 llvm::APSInt SimdlenRes, SafelenRes;
4056 if (Simdlen->isValueDependent() || Simdlen->isTypeDependent() ||
4057 Simdlen->isInstantiationDependent() ||
4058 Simdlen->containsUnexpandedParameterPack())
4059 return false;
4060 if (Safelen->isValueDependent() || Safelen->isTypeDependent() ||
4061 Safelen->isInstantiationDependent() ||
4062 Safelen->containsUnexpandedParameterPack())
4063 return false;
4064 Simdlen->EvaluateAsInt(SimdlenRes, S.Context);
4065 Safelen->EvaluateAsInt(SafelenRes, S.Context);
4066 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4067 // If both simdlen and safelen clauses are specified, the value of the simdlen
4068 // parameter must be less than or equal to the value of the safelen parameter.
4069 if (SimdlenRes > SafelenRes) {
4070 S.Diag(Simdlen->getExprLoc(), diag::err_omp_wrong_simdlen_safelen_values)
4071 << Simdlen->getSourceRange() << Safelen->getSourceRange();
4072 return true;
4073 }
4074 return false;
4075}
4076
Alexey Bataev4acb8592014-07-07 13:01:15 +00004077StmtResult Sema::ActOnOpenMPSimdDirective(
4078 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4079 SourceLocation EndLoc,
4080 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004081 if (!AStmt)
4082 return StmtError();
4083
4084 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004085 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004086 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4087 // define the nested loops number.
4088 unsigned NestedLoopCount = CheckOpenMPLoop(
4089 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4090 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004091 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004092 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004093
Alexander Musmana5f070a2014-10-01 06:03:56 +00004094 assert((CurContext->isDependentContext() || B.builtAll()) &&
4095 "omp simd loop exprs were not built");
4096
Alexander Musman3276a272015-03-21 10:12:56 +00004097 if (!CurContext->isDependentContext()) {
4098 // Finalize the clauses that need pre-built expressions for CodeGen.
4099 for (auto C : Clauses) {
4100 if (auto LC = dyn_cast<OMPLinearClause>(C))
4101 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4102 B.NumIterations, *this, CurScope))
4103 return StmtError();
4104 }
4105 }
4106
Alexey Bataev66b15b52015-08-21 11:14:16 +00004107 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4108 // If both simdlen and safelen clauses are specified, the value of the simdlen
4109 // parameter must be less than or equal to the value of the safelen parameter.
4110 OMPSafelenClause *Safelen = nullptr;
4111 OMPSimdlenClause *Simdlen = nullptr;
4112 for (auto *Clause : Clauses) {
4113 if (Clause->getClauseKind() == OMPC_safelen)
4114 Safelen = cast<OMPSafelenClause>(Clause);
4115 else if (Clause->getClauseKind() == OMPC_simdlen)
4116 Simdlen = cast<OMPSimdlenClause>(Clause);
4117 if (Safelen && Simdlen)
4118 break;
4119 }
4120 if (Simdlen && Safelen &&
4121 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4122 Safelen->getSafelen()))
4123 return StmtError();
4124
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004125 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004126 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4127 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00004128}
4129
Alexey Bataev4acb8592014-07-07 13:01:15 +00004130StmtResult Sema::ActOnOpenMPForDirective(
4131 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4132 SourceLocation EndLoc,
4133 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004134 if (!AStmt)
4135 return StmtError();
4136
4137 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004138 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004139 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4140 // define the nested loops number.
4141 unsigned NestedLoopCount = CheckOpenMPLoop(
4142 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
4143 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00004144 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00004145 return StmtError();
4146
Alexander Musmana5f070a2014-10-01 06:03:56 +00004147 assert((CurContext->isDependentContext() || B.builtAll()) &&
4148 "omp for loop exprs were not built");
4149
Alexey Bataev54acd402015-08-04 11:18:19 +00004150 if (!CurContext->isDependentContext()) {
4151 // Finalize the clauses that need pre-built expressions for CodeGen.
4152 for (auto C : Clauses) {
4153 if (auto LC = dyn_cast<OMPLinearClause>(C))
4154 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4155 B.NumIterations, *this, CurScope))
4156 return StmtError();
4157 }
4158 }
4159
Alexey Bataevf29276e2014-06-18 04:14:57 +00004160 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004161 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004162 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00004163}
4164
Alexander Musmanf82886e2014-09-18 05:12:34 +00004165StmtResult Sema::ActOnOpenMPForSimdDirective(
4166 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4167 SourceLocation EndLoc,
4168 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004169 if (!AStmt)
4170 return StmtError();
4171
4172 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00004173 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004174 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4175 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00004176 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004177 CheckOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
4178 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4179 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004180 if (NestedLoopCount == 0)
4181 return StmtError();
4182
Alexander Musmanc6388682014-12-15 07:07:06 +00004183 assert((CurContext->isDependentContext() || B.builtAll()) &&
4184 "omp for simd loop exprs were not built");
4185
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00004186 if (!CurContext->isDependentContext()) {
4187 // Finalize the clauses that need pre-built expressions for CodeGen.
4188 for (auto C : Clauses) {
4189 if (auto LC = dyn_cast<OMPLinearClause>(C))
4190 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4191 B.NumIterations, *this, CurScope))
4192 return StmtError();
4193 }
4194 }
4195
Alexey Bataev66b15b52015-08-21 11:14:16 +00004196 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4197 // If both simdlen and safelen clauses are specified, the value of the simdlen
4198 // parameter must be less than or equal to the value of the safelen parameter.
4199 OMPSafelenClause *Safelen = nullptr;
4200 OMPSimdlenClause *Simdlen = nullptr;
4201 for (auto *Clause : Clauses) {
4202 if (Clause->getClauseKind() == OMPC_safelen)
4203 Safelen = cast<OMPSafelenClause>(Clause);
4204 else if (Clause->getClauseKind() == OMPC_simdlen)
4205 Simdlen = cast<OMPSimdlenClause>(Clause);
4206 if (Safelen && Simdlen)
4207 break;
4208 }
4209 if (Simdlen && Safelen &&
4210 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4211 Safelen->getSafelen()))
4212 return StmtError();
4213
Alexander Musmanf82886e2014-09-18 05:12:34 +00004214 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004215 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
4216 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00004217}
4218
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004219StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
4220 Stmt *AStmt,
4221 SourceLocation StartLoc,
4222 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004223 if (!AStmt)
4224 return StmtError();
4225
4226 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004227 auto BaseStmt = AStmt;
4228 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4229 BaseStmt = CS->getCapturedStmt();
4230 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4231 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004232 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004233 return StmtError();
4234 // All associated statements must be '#pragma omp section' except for
4235 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004236 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004237 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4238 if (SectionStmt)
4239 Diag(SectionStmt->getLocStart(),
4240 diag::err_omp_sections_substmt_not_section);
4241 return StmtError();
4242 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004243 cast<OMPSectionDirective>(SectionStmt)
4244 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004245 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004246 } else {
4247 Diag(AStmt->getLocStart(), diag::err_omp_sections_not_compound_stmt);
4248 return StmtError();
4249 }
4250
4251 getCurFunction()->setHasBranchProtectedScope();
4252
Alexey Bataev25e5b442015-09-15 12:52:43 +00004253 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4254 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00004255}
4256
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004257StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
4258 SourceLocation StartLoc,
4259 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004260 if (!AStmt)
4261 return StmtError();
4262
4263 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004264
4265 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00004266 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004267
Alexey Bataev25e5b442015-09-15 12:52:43 +00004268 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
4269 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00004270}
4271
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004272StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
4273 Stmt *AStmt,
4274 SourceLocation StartLoc,
4275 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004276 if (!AStmt)
4277 return StmtError();
4278
4279 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00004280
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004281 getCurFunction()->setHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00004282
Alexey Bataev3255bf32015-01-19 05:20:46 +00004283 // OpenMP [2.7.3, single Construct, Restrictions]
4284 // The copyprivate clause must not be used with the nowait clause.
4285 OMPClause *Nowait = nullptr;
4286 OMPClause *Copyprivate = nullptr;
4287 for (auto *Clause : Clauses) {
4288 if (Clause->getClauseKind() == OMPC_nowait)
4289 Nowait = Clause;
4290 else if (Clause->getClauseKind() == OMPC_copyprivate)
4291 Copyprivate = Clause;
4292 if (Copyprivate && Nowait) {
4293 Diag(Copyprivate->getLocStart(),
4294 diag::err_omp_single_copyprivate_with_nowait);
4295 Diag(Nowait->getLocStart(), diag::note_omp_nowait_clause_here);
4296 return StmtError();
4297 }
4298 }
4299
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00004300 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
4301}
4302
Alexander Musman80c22892014-07-17 08:54:58 +00004303StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
4304 SourceLocation StartLoc,
4305 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004306 if (!AStmt)
4307 return StmtError();
4308
4309 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00004310
4311 getCurFunction()->setHasBranchProtectedScope();
4312
4313 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
4314}
4315
Alexey Bataev28c75412015-12-15 08:19:24 +00004316StmtResult Sema::ActOnOpenMPCriticalDirective(
4317 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
4318 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004319 if (!AStmt)
4320 return StmtError();
4321
4322 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004323
Alexey Bataev28c75412015-12-15 08:19:24 +00004324 bool ErrorFound = false;
4325 llvm::APSInt Hint;
4326 SourceLocation HintLoc;
4327 bool DependentHint = false;
4328 for (auto *C : Clauses) {
4329 if (C->getClauseKind() == OMPC_hint) {
4330 if (!DirName.getName()) {
4331 Diag(C->getLocStart(), diag::err_omp_hint_clause_no_name);
4332 ErrorFound = true;
4333 }
4334 Expr *E = cast<OMPHintClause>(C)->getHint();
4335 if (E->isTypeDependent() || E->isValueDependent() ||
4336 E->isInstantiationDependent())
4337 DependentHint = true;
4338 else {
4339 Hint = E->EvaluateKnownConstInt(Context);
4340 HintLoc = C->getLocStart();
4341 }
4342 }
4343 }
4344 if (ErrorFound)
4345 return StmtError();
4346 auto Pair = DSAStack->getCriticalWithHint(DirName);
4347 if (Pair.first && DirName.getName() && !DependentHint) {
4348 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
4349 Diag(StartLoc, diag::err_omp_critical_with_hint);
4350 if (HintLoc.isValid()) {
4351 Diag(HintLoc, diag::note_omp_critical_hint_here)
4352 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
4353 } else
4354 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
4355 if (auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
4356 Diag(C->getLocStart(), diag::note_omp_critical_hint_here)
4357 << 1
4358 << C->getHint()->EvaluateKnownConstInt(Context).toString(
4359 /*Radix=*/10, /*Signed=*/false);
4360 } else
4361 Diag(Pair.first->getLocStart(), diag::note_omp_critical_no_hint) << 1;
4362 }
4363 }
4364
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004365 getCurFunction()->setHasBranchProtectedScope();
4366
Alexey Bataev28c75412015-12-15 08:19:24 +00004367 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
4368 Clauses, AStmt);
4369 if (!Pair.first && DirName.getName() && !DependentHint)
4370 DSAStack->addCriticalWithHint(Dir, Hint);
4371 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00004372}
4373
Alexey Bataev4acb8592014-07-07 13:01:15 +00004374StmtResult Sema::ActOnOpenMPParallelForDirective(
4375 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4376 SourceLocation EndLoc,
4377 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004378 if (!AStmt)
4379 return StmtError();
4380
Alexey Bataev4acb8592014-07-07 13:01:15 +00004381 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4382 // 1.2.2 OpenMP Language Terminology
4383 // Structured block - An executable statement with a single entry at the
4384 // top and a single exit at the bottom.
4385 // The point of exit cannot be a branch out of the structured block.
4386 // longjmp() and throw() must not violate the entry/exit criteria.
4387 CS->getCapturedDecl()->setNothrow();
4388
Alexander Musmanc6388682014-12-15 07:07:06 +00004389 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004390 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4391 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00004392 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004393 CheckOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
4394 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4395 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00004396 if (NestedLoopCount == 0)
4397 return StmtError();
4398
Alexander Musmana5f070a2014-10-01 06:03:56 +00004399 assert((CurContext->isDependentContext() || B.builtAll()) &&
4400 "omp parallel for loop exprs were not built");
4401
Alexey Bataev54acd402015-08-04 11:18:19 +00004402 if (!CurContext->isDependentContext()) {
4403 // Finalize the clauses that need pre-built expressions for CodeGen.
4404 for (auto C : Clauses) {
4405 if (auto LC = dyn_cast<OMPLinearClause>(C))
4406 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4407 B.NumIterations, *this, CurScope))
4408 return StmtError();
4409 }
4410 }
4411
Alexey Bataev4acb8592014-07-07 13:01:15 +00004412 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00004413 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00004414 NestedLoopCount, Clauses, AStmt, B,
4415 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00004416}
4417
Alexander Musmane4e893b2014-09-23 09:33:00 +00004418StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
4419 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
4420 SourceLocation EndLoc,
4421 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004422 if (!AStmt)
4423 return StmtError();
4424
Alexander Musmane4e893b2014-09-23 09:33:00 +00004425 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4426 // 1.2.2 OpenMP Language Terminology
4427 // Structured block - An executable statement with a single entry at the
4428 // top and a single exit at the bottom.
4429 // The point of exit cannot be a branch out of the structured block.
4430 // longjmp() and throw() must not violate the entry/exit criteria.
4431 CS->getCapturedDecl()->setNothrow();
4432
Alexander Musmanc6388682014-12-15 07:07:06 +00004433 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00004434 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
4435 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00004436 unsigned NestedLoopCount =
Alexey Bataev10e775f2015-07-30 11:36:16 +00004437 CheckOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
4438 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
4439 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004440 if (NestedLoopCount == 0)
4441 return StmtError();
4442
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00004443 if (!CurContext->isDependentContext()) {
4444 // Finalize the clauses that need pre-built expressions for CodeGen.
4445 for (auto C : Clauses) {
4446 if (auto LC = dyn_cast<OMPLinearClause>(C))
4447 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
4448 B.NumIterations, *this, CurScope))
4449 return StmtError();
4450 }
4451 }
4452
Alexey Bataev66b15b52015-08-21 11:14:16 +00004453 // OpenMP 4.1 [2.8.1, simd Construct, Restrictions]
4454 // If both simdlen and safelen clauses are specified, the value of the simdlen
4455 // parameter must be less than or equal to the value of the safelen parameter.
4456 OMPSafelenClause *Safelen = nullptr;
4457 OMPSimdlenClause *Simdlen = nullptr;
4458 for (auto *Clause : Clauses) {
4459 if (Clause->getClauseKind() == OMPC_safelen)
4460 Safelen = cast<OMPSafelenClause>(Clause);
4461 else if (Clause->getClauseKind() == OMPC_simdlen)
4462 Simdlen = cast<OMPSimdlenClause>(Clause);
4463 if (Safelen && Simdlen)
4464 break;
4465 }
4466 if (Simdlen && Safelen &&
4467 checkSimdlenSafelenValues(*this, Simdlen->getSimdlen(),
4468 Safelen->getSafelen()))
4469 return StmtError();
4470
Alexander Musmane4e893b2014-09-23 09:33:00 +00004471 getCurFunction()->setHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004472 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00004473 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00004474}
4475
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004476StmtResult
4477Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
4478 Stmt *AStmt, SourceLocation StartLoc,
4479 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004480 if (!AStmt)
4481 return StmtError();
4482
4483 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004484 auto BaseStmt = AStmt;
4485 while (CapturedStmt *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
4486 BaseStmt = CS->getCapturedStmt();
4487 if (auto C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
4488 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00004489 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004490 return StmtError();
4491 // All associated statements must be '#pragma omp section' except for
4492 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00004493 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004494 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
4495 if (SectionStmt)
4496 Diag(SectionStmt->getLocStart(),
4497 diag::err_omp_parallel_sections_substmt_not_section);
4498 return StmtError();
4499 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00004500 cast<OMPSectionDirective>(SectionStmt)
4501 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004502 }
4503 } else {
4504 Diag(AStmt->getLocStart(),
4505 diag::err_omp_parallel_sections_not_compound_stmt);
4506 return StmtError();
4507 }
4508
4509 getCurFunction()->setHasBranchProtectedScope();
4510
Alexey Bataev25e5b442015-09-15 12:52:43 +00004511 return OMPParallelSectionsDirective::Create(
4512 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00004513}
4514
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004515StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
4516 Stmt *AStmt, SourceLocation StartLoc,
4517 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004518 if (!AStmt)
4519 return StmtError();
4520
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004521 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
4522 // 1.2.2 OpenMP Language Terminology
4523 // Structured block - An executable statement with a single entry at the
4524 // top and a single exit at the bottom.
4525 // The point of exit cannot be a branch out of the structured block.
4526 // longjmp() and throw() must not violate the entry/exit criteria.
4527 CS->getCapturedDecl()->setNothrow();
4528
4529 getCurFunction()->setHasBranchProtectedScope();
4530
Alexey Bataev25e5b442015-09-15 12:52:43 +00004531 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
4532 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00004533}
4534
Alexey Bataev68446b72014-07-18 07:47:19 +00004535StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
4536 SourceLocation EndLoc) {
4537 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
4538}
4539
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00004540StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
4541 SourceLocation EndLoc) {
4542 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
4543}
4544
Alexey Bataev2df347a2014-07-18 10:17:07 +00004545StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
4546 SourceLocation EndLoc) {
4547 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
4548}
4549
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00004550StmtResult Sema::ActOnOpenMPTaskgroupDirective(Stmt *AStmt,
4551 SourceLocation StartLoc,
4552 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004553 if (!AStmt)
4554 return StmtError();
4555
4556 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00004557
4558 getCurFunction()->setHasBranchProtectedScope();
4559
4560 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, AStmt);
4561}
4562
Alexey Bataev6125da92014-07-21 11:26:11 +00004563StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
4564 SourceLocation StartLoc,
4565 SourceLocation EndLoc) {
4566 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
4567 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
4568}
4569
Alexey Bataev346265e2015-09-25 10:37:12 +00004570StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
4571 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004572 SourceLocation StartLoc,
4573 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004574 if (!AStmt)
4575 return StmtError();
4576
4577 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004578
4579 getCurFunction()->setHasBranchProtectedScope();
4580
Alexey Bataev346265e2015-09-25 10:37:12 +00004581 OMPThreadsClause *TC = nullptr;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004582 OMPSIMDClause *SC = nullptr;
Alexey Bataev346265e2015-09-25 10:37:12 +00004583 for (auto *C: Clauses) {
4584 if (C->getClauseKind() == OMPC_threads)
4585 TC = cast<OMPThreadsClause>(C);
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004586 else if (C->getClauseKind() == OMPC_simd)
4587 SC = cast<OMPSIMDClause>(C);
Alexey Bataev346265e2015-09-25 10:37:12 +00004588 }
4589
4590 // TODO: this must happen only if 'threads' clause specified or if no clauses
4591 // is specified.
4592 if (auto *Param = DSAStack->getParentOrderedRegionParam()) {
4593 SourceLocation ErrLoc = TC ? TC->getLocStart() : StartLoc;
4594 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param) << (TC != nullptr);
4595 Diag(Param->getLocStart(), diag::note_omp_ordered_param);
4596 return StmtError();
4597 }
Alexey Bataevd14d1e62015-09-28 06:39:35 +00004598 if (!SC && isOpenMPSimdDirective(DSAStack->getParentDirective())) {
4599 // OpenMP [2.8.1,simd Construct, Restrictions]
4600 // An ordered construct with the simd clause is the only OpenMP construct
4601 // that can appear in the simd region.
4602 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
4603 return StmtError();
4604 }
Alexey Bataev346265e2015-09-25 10:37:12 +00004605
4606 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00004607}
4608
Alexey Bataev1d160b12015-03-13 12:27:31 +00004609namespace {
4610/// \brief Helper class for checking expression in 'omp atomic [update]'
4611/// construct.
4612class OpenMPAtomicUpdateChecker {
4613 /// \brief Error results for atomic update expressions.
4614 enum ExprAnalysisErrorCode {
4615 /// \brief A statement is not an expression statement.
4616 NotAnExpression,
4617 /// \brief Expression is not builtin binary or unary operation.
4618 NotABinaryOrUnaryExpression,
4619 /// \brief Unary operation is not post-/pre- increment/decrement operation.
4620 NotAnUnaryIncDecExpression,
4621 /// \brief An expression is not of scalar type.
4622 NotAScalarType,
4623 /// \brief A binary operation is not an assignment operation.
4624 NotAnAssignmentOp,
4625 /// \brief RHS part of the binary operation is not a binary expression.
4626 NotABinaryExpression,
4627 /// \brief RHS part is not additive/multiplicative/shift/biwise binary
4628 /// expression.
4629 NotABinaryOperator,
4630 /// \brief RHS binary operation does not have reference to the updated LHS
4631 /// part.
4632 NotAnUpdateExpression,
4633 /// \brief No errors is found.
4634 NoError
4635 };
4636 /// \brief Reference to Sema.
4637 Sema &SemaRef;
4638 /// \brief A location for note diagnostics (when error is found).
4639 SourceLocation NoteLoc;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004640 /// \brief 'x' lvalue part of the source atomic expression.
4641 Expr *X;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004642 /// \brief 'expr' rvalue part of the source atomic expression.
4643 Expr *E;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004644 /// \brief Helper expression of the form
4645 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
4646 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
4647 Expr *UpdateExpr;
4648 /// \brief Is 'x' a LHS in a RHS part of full update expression. It is
4649 /// important for non-associative operations.
4650 bool IsXLHSInRHSPart;
4651 BinaryOperatorKind Op;
4652 SourceLocation OpLoc;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004653 /// \brief true if the source expression is a postfix unary operation, false
4654 /// if it is a prefix unary operation.
4655 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004656
4657public:
4658 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00004659 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00004660 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Alexey Bataev1d160b12015-03-13 12:27:31 +00004661 /// \brief Check specified statement that it is suitable for 'atomic update'
4662 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00004663 /// expression. If DiagId and NoteId == 0, then only check is performed
4664 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00004665 /// \param DiagId Diagnostic which should be emitted if error is found.
4666 /// \param NoteId Diagnostic note for the main error message.
4667 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00004668 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00004669 /// \brief Return the 'x' lvalue part of the source atomic expression.
4670 Expr *getX() const { return X; }
Alexey Bataev1d160b12015-03-13 12:27:31 +00004671 /// \brief Return the 'expr' rvalue part of the source atomic expression.
4672 Expr *getExpr() const { return E; }
Alexey Bataevb4505a72015-03-30 05:20:59 +00004673 /// \brief Return the update expression used in calculation of the updated
4674 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
4675 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
4676 Expr *getUpdateExpr() const { return UpdateExpr; }
4677 /// \brief Return true if 'x' is LHS in RHS part of full update expression,
4678 /// false otherwise.
4679 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
4680
Alexey Bataevb78ca832015-04-01 03:33:17 +00004681 /// \brief true if the source expression is a postfix unary operation, false
4682 /// if it is a prefix unary operation.
4683 bool isPostfixUpdate() const { return IsPostfixUpdate; }
4684
Alexey Bataev1d160b12015-03-13 12:27:31 +00004685private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00004686 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
4687 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00004688};
4689} // namespace
4690
4691bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
4692 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
4693 ExprAnalysisErrorCode ErrorFound = NoError;
4694 SourceLocation ErrorLoc, NoteLoc;
4695 SourceRange ErrorRange, NoteRange;
4696 // Allowed constructs are:
4697 // x = x binop expr;
4698 // x = expr binop x;
4699 if (AtomicBinOp->getOpcode() == BO_Assign) {
4700 X = AtomicBinOp->getLHS();
4701 if (auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
4702 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
4703 if (AtomicInnerBinOp->isMultiplicativeOp() ||
4704 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
4705 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00004706 Op = AtomicInnerBinOp->getOpcode();
4707 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00004708 auto *LHS = AtomicInnerBinOp->getLHS();
4709 auto *RHS = AtomicInnerBinOp->getRHS();
4710 llvm::FoldingSetNodeID XId, LHSId, RHSId;
4711 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
4712 /*Canonical=*/true);
4713 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
4714 /*Canonical=*/true);
4715 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
4716 /*Canonical=*/true);
4717 if (XId == LHSId) {
4718 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004719 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004720 } else if (XId == RHSId) {
4721 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004722 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004723 } else {
4724 ErrorLoc = AtomicInnerBinOp->getExprLoc();
4725 ErrorRange = AtomicInnerBinOp->getSourceRange();
4726 NoteLoc = X->getExprLoc();
4727 NoteRange = X->getSourceRange();
4728 ErrorFound = NotAnUpdateExpression;
4729 }
4730 } else {
4731 ErrorLoc = AtomicInnerBinOp->getExprLoc();
4732 ErrorRange = AtomicInnerBinOp->getSourceRange();
4733 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
4734 NoteRange = SourceRange(NoteLoc, NoteLoc);
4735 ErrorFound = NotABinaryOperator;
4736 }
4737 } else {
4738 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
4739 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
4740 ErrorFound = NotABinaryExpression;
4741 }
4742 } else {
4743 ErrorLoc = AtomicBinOp->getExprLoc();
4744 ErrorRange = AtomicBinOp->getSourceRange();
4745 NoteLoc = AtomicBinOp->getOperatorLoc();
4746 NoteRange = SourceRange(NoteLoc, NoteLoc);
4747 ErrorFound = NotAnAssignmentOp;
4748 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00004749 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004750 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
4751 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
4752 return true;
4753 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00004754 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00004755 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004756}
4757
4758bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
4759 unsigned NoteId) {
4760 ExprAnalysisErrorCode ErrorFound = NoError;
4761 SourceLocation ErrorLoc, NoteLoc;
4762 SourceRange ErrorRange, NoteRange;
4763 // Allowed constructs are:
4764 // x++;
4765 // x--;
4766 // ++x;
4767 // --x;
4768 // x binop= expr;
4769 // x = x binop expr;
4770 // x = expr binop x;
4771 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
4772 AtomicBody = AtomicBody->IgnoreParenImpCasts();
4773 if (AtomicBody->getType()->isScalarType() ||
4774 AtomicBody->isInstantiationDependent()) {
4775 if (auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
4776 AtomicBody->IgnoreParenImpCasts())) {
4777 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00004778 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00004779 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00004780 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00004781 E = AtomicCompAssignOp->getRHS();
Alexey Bataevb4505a72015-03-30 05:20:59 +00004782 X = AtomicCompAssignOp->getLHS();
4783 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004784 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
4785 AtomicBody->IgnoreParenImpCasts())) {
4786 // Check for Binary Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00004787 if(checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
4788 return true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004789 } else if (auto *AtomicUnaryOp =
Alexey Bataev1d160b12015-03-13 12:27:31 +00004790 dyn_cast<UnaryOperator>(AtomicBody->IgnoreParenImpCasts())) {
4791 // Check for Unary Operation
4792 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004793 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00004794 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
4795 OpLoc = AtomicUnaryOp->getOperatorLoc();
4796 X = AtomicUnaryOp->getSubExpr();
4797 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
4798 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004799 } else {
4800 ErrorFound = NotAnUnaryIncDecExpression;
4801 ErrorLoc = AtomicUnaryOp->getExprLoc();
4802 ErrorRange = AtomicUnaryOp->getSourceRange();
4803 NoteLoc = AtomicUnaryOp->getOperatorLoc();
4804 NoteRange = SourceRange(NoteLoc, NoteLoc);
4805 }
Alexey Bataev5a195472015-09-04 12:55:50 +00004806 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004807 ErrorFound = NotABinaryOrUnaryExpression;
4808 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
4809 NoteRange = ErrorRange = AtomicBody->getSourceRange();
4810 }
4811 } else {
4812 ErrorFound = NotAScalarType;
4813 NoteLoc = ErrorLoc = AtomicBody->getLocStart();
4814 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
4815 }
4816 } else {
4817 ErrorFound = NotAnExpression;
4818 NoteLoc = ErrorLoc = S->getLocStart();
4819 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
4820 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00004821 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00004822 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
4823 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
4824 return true;
4825 } else if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00004826 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00004827 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00004828 // Build an update expression of form 'OpaqueValueExpr(x) binop
4829 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
4830 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
4831 auto *OVEX = new (SemaRef.getASTContext())
4832 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
4833 auto *OVEExpr = new (SemaRef.getASTContext())
4834 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
4835 auto Update =
4836 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
4837 IsXLHSInRHSPart ? OVEExpr : OVEX);
4838 if (Update.isInvalid())
4839 return true;
4840 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
4841 Sema::AA_Casting);
4842 if (Update.isInvalid())
4843 return true;
4844 UpdateExpr = Update.get();
4845 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00004846 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00004847}
4848
Alexey Bataev0162e452014-07-22 10:10:35 +00004849StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
4850 Stmt *AStmt,
4851 SourceLocation StartLoc,
4852 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00004853 if (!AStmt)
4854 return StmtError();
4855
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004856 auto CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00004857 // 1.2.2 OpenMP Language Terminology
4858 // Structured block - An executable statement with a single entry at the
4859 // top and a single exit at the bottom.
4860 // The point of exit cannot be a branch out of the structured block.
4861 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00004862 OpenMPClauseKind AtomicKind = OMPC_unknown;
4863 SourceLocation AtomicKindLoc;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004864 for (auto *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00004865 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00004866 C->getClauseKind() == OMPC_update ||
4867 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00004868 if (AtomicKind != OMPC_unknown) {
4869 Diag(C->getLocStart(), diag::err_omp_atomic_several_clauses)
4870 << SourceRange(C->getLocStart(), C->getLocEnd());
4871 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
4872 << getOpenMPClauseName(AtomicKind);
4873 } else {
4874 AtomicKind = C->getClauseKind();
4875 AtomicKindLoc = C->getLocStart();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00004876 }
4877 }
4878 }
Alexey Bataev62cec442014-11-18 10:14:22 +00004879
Alexey Bataev459dec02014-07-24 06:46:57 +00004880 auto Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00004881 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
4882 Body = EWC->getSubExpr();
4883
Alexey Bataev62cec442014-11-18 10:14:22 +00004884 Expr *X = nullptr;
4885 Expr *V = nullptr;
4886 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00004887 Expr *UE = nullptr;
4888 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00004889 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00004890 // OpenMP [2.12.6, atomic Construct]
4891 // In the next expressions:
4892 // * x and v (as applicable) are both l-value expressions with scalar type.
4893 // * During the execution of an atomic region, multiple syntactic
4894 // occurrences of x must designate the same storage location.
4895 // * Neither of v and expr (as applicable) may access the storage location
4896 // designated by x.
4897 // * Neither of x and expr (as applicable) may access the storage location
4898 // designated by v.
4899 // * expr is an expression with scalar type.
4900 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
4901 // * binop, binop=, ++, and -- are not overloaded operators.
4902 // * The expression x binop expr must be numerically equivalent to x binop
4903 // (expr). This requirement is satisfied if the operators in expr have
4904 // precedence greater than binop, or by using parentheses around expr or
4905 // subexpressions of expr.
4906 // * The expression expr binop x must be numerically equivalent to (expr)
4907 // binop x. This requirement is satisfied if the operators in expr have
4908 // precedence equal to or greater than binop, or by using parentheses around
4909 // expr or subexpressions of expr.
4910 // * For forms that allow multiple occurrences of x, the number of times
4911 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00004912 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004913 enum {
4914 NotAnExpression,
4915 NotAnAssignmentOp,
4916 NotAScalarType,
4917 NotAnLValue,
4918 NoError
4919 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00004920 SourceLocation ErrorLoc, NoteLoc;
4921 SourceRange ErrorRange, NoteRange;
4922 // If clause is read:
4923 // v = x;
4924 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
4925 auto AtomicBinOp =
4926 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
4927 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
4928 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
4929 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
4930 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
4931 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
4932 if (!X->isLValue() || !V->isLValue()) {
4933 auto NotLValueExpr = X->isLValue() ? V : X;
4934 ErrorFound = NotAnLValue;
4935 ErrorLoc = AtomicBinOp->getExprLoc();
4936 ErrorRange = AtomicBinOp->getSourceRange();
4937 NoteLoc = NotLValueExpr->getExprLoc();
4938 NoteRange = NotLValueExpr->getSourceRange();
4939 }
4940 } else if (!X->isInstantiationDependent() ||
4941 !V->isInstantiationDependent()) {
4942 auto NotScalarExpr =
4943 (X->isInstantiationDependent() || X->getType()->isScalarType())
4944 ? V
4945 : X;
4946 ErrorFound = NotAScalarType;
4947 ErrorLoc = AtomicBinOp->getExprLoc();
4948 ErrorRange = AtomicBinOp->getSourceRange();
4949 NoteLoc = NotScalarExpr->getExprLoc();
4950 NoteRange = NotScalarExpr->getSourceRange();
4951 }
Alexey Bataev5a195472015-09-04 12:55:50 +00004952 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00004953 ErrorFound = NotAnAssignmentOp;
4954 ErrorLoc = AtomicBody->getExprLoc();
4955 ErrorRange = AtomicBody->getSourceRange();
4956 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
4957 : AtomicBody->getExprLoc();
4958 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
4959 : AtomicBody->getSourceRange();
4960 }
4961 } else {
4962 ErrorFound = NotAnExpression;
4963 NoteLoc = ErrorLoc = Body->getLocStart();
4964 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00004965 }
Alexey Bataev62cec442014-11-18 10:14:22 +00004966 if (ErrorFound != NoError) {
4967 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
4968 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00004969 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
4970 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00004971 return StmtError();
4972 } else if (CurContext->isDependentContext())
4973 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00004974 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00004975 enum {
4976 NotAnExpression,
4977 NotAnAssignmentOp,
4978 NotAScalarType,
4979 NotAnLValue,
4980 NoError
4981 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00004982 SourceLocation ErrorLoc, NoteLoc;
4983 SourceRange ErrorRange, NoteRange;
4984 // If clause is write:
4985 // x = expr;
4986 if (auto AtomicBody = dyn_cast<Expr>(Body)) {
4987 auto AtomicBinOp =
4988 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
4989 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00004990 X = AtomicBinOp->getLHS();
4991 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00004992 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
4993 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
4994 if (!X->isLValue()) {
4995 ErrorFound = NotAnLValue;
4996 ErrorLoc = AtomicBinOp->getExprLoc();
4997 ErrorRange = AtomicBinOp->getSourceRange();
4998 NoteLoc = X->getExprLoc();
4999 NoteRange = X->getSourceRange();
5000 }
5001 } else if (!X->isInstantiationDependent() ||
5002 !E->isInstantiationDependent()) {
5003 auto NotScalarExpr =
5004 (X->isInstantiationDependent() || X->getType()->isScalarType())
5005 ? E
5006 : X;
5007 ErrorFound = NotAScalarType;
5008 ErrorLoc = AtomicBinOp->getExprLoc();
5009 ErrorRange = AtomicBinOp->getSourceRange();
5010 NoteLoc = NotScalarExpr->getExprLoc();
5011 NoteRange = NotScalarExpr->getSourceRange();
5012 }
Alexey Bataev5a195472015-09-04 12:55:50 +00005013 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00005014 ErrorFound = NotAnAssignmentOp;
5015 ErrorLoc = AtomicBody->getExprLoc();
5016 ErrorRange = AtomicBody->getSourceRange();
5017 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5018 : AtomicBody->getExprLoc();
5019 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5020 : AtomicBody->getSourceRange();
5021 }
5022 } else {
5023 ErrorFound = NotAnExpression;
5024 NoteLoc = ErrorLoc = Body->getLocStart();
5025 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00005026 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00005027 if (ErrorFound != NoError) {
5028 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
5029 << ErrorRange;
5030 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
5031 << NoteRange;
5032 return StmtError();
5033 } else if (CurContext->isDependentContext())
5034 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00005035 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00005036 // If clause is update:
5037 // x++;
5038 // x--;
5039 // ++x;
5040 // --x;
5041 // x binop= expr;
5042 // x = x binop expr;
5043 // x = expr binop x;
5044 OpenMPAtomicUpdateChecker Checker(*this);
5045 if (Checker.checkStatement(
5046 Body, (AtomicKind == OMPC_update)
5047 ? diag::err_omp_atomic_update_not_expression_statement
5048 : diag::err_omp_atomic_not_expression_statement,
5049 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00005050 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00005051 if (!CurContext->isDependentContext()) {
5052 E = Checker.getExpr();
5053 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00005054 UE = Checker.getUpdateExpr();
5055 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00005056 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005057 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005058 enum {
5059 NotAnAssignmentOp,
5060 NotACompoundStatement,
5061 NotTwoSubstatements,
5062 NotASpecificExpression,
5063 NoError
5064 } ErrorFound = NoError;
5065 SourceLocation ErrorLoc, NoteLoc;
5066 SourceRange ErrorRange, NoteRange;
5067 if (auto *AtomicBody = dyn_cast<Expr>(Body)) {
5068 // If clause is a capture:
5069 // v = x++;
5070 // v = x--;
5071 // v = ++x;
5072 // v = --x;
5073 // v = x binop= expr;
5074 // v = x = x binop expr;
5075 // v = x = expr binop x;
5076 auto *AtomicBinOp =
5077 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
5078 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
5079 V = AtomicBinOp->getLHS();
5080 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
5081 OpenMPAtomicUpdateChecker Checker(*this);
5082 if (Checker.checkStatement(
5083 Body, diag::err_omp_atomic_capture_not_expression_statement,
5084 diag::note_omp_atomic_update))
5085 return StmtError();
5086 E = Checker.getExpr();
5087 X = Checker.getX();
5088 UE = Checker.getUpdateExpr();
5089 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
5090 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00005091 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005092 ErrorLoc = AtomicBody->getExprLoc();
5093 ErrorRange = AtomicBody->getSourceRange();
5094 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
5095 : AtomicBody->getExprLoc();
5096 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
5097 : AtomicBody->getSourceRange();
5098 ErrorFound = NotAnAssignmentOp;
5099 }
5100 if (ErrorFound != NoError) {
5101 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
5102 << ErrorRange;
5103 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5104 return StmtError();
5105 } else if (CurContext->isDependentContext()) {
5106 UE = V = E = X = nullptr;
5107 }
5108 } else {
5109 // If clause is a capture:
5110 // { v = x; x = expr; }
5111 // { v = x; x++; }
5112 // { v = x; x--; }
5113 // { v = x; ++x; }
5114 // { v = x; --x; }
5115 // { v = x; x binop= expr; }
5116 // { v = x; x = x binop expr; }
5117 // { v = x; x = expr binop x; }
5118 // { x++; v = x; }
5119 // { x--; v = x; }
5120 // { ++x; v = x; }
5121 // { --x; v = x; }
5122 // { x binop= expr; v = x; }
5123 // { x = x binop expr; v = x; }
5124 // { x = expr binop x; v = x; }
5125 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
5126 // Check that this is { expr1; expr2; }
5127 if (CS->size() == 2) {
5128 auto *First = CS->body_front();
5129 auto *Second = CS->body_back();
5130 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
5131 First = EWC->getSubExpr()->IgnoreParenImpCasts();
5132 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
5133 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
5134 // Need to find what subexpression is 'v' and what is 'x'.
5135 OpenMPAtomicUpdateChecker Checker(*this);
5136 bool IsUpdateExprFound = !Checker.checkStatement(Second);
5137 BinaryOperator *BinOp = nullptr;
5138 if (IsUpdateExprFound) {
5139 BinOp = dyn_cast<BinaryOperator>(First);
5140 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5141 }
5142 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5143 // { v = x; x++; }
5144 // { v = x; x--; }
5145 // { v = x; ++x; }
5146 // { v = x; --x; }
5147 // { v = x; x binop= expr; }
5148 // { v = x; x = x binop expr; }
5149 // { v = x; x = expr binop x; }
5150 // Check that the first expression has form v = x.
5151 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5152 llvm::FoldingSetNodeID XId, PossibleXId;
5153 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5154 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5155 IsUpdateExprFound = XId == PossibleXId;
5156 if (IsUpdateExprFound) {
5157 V = BinOp->getLHS();
5158 X = Checker.getX();
5159 E = Checker.getExpr();
5160 UE = Checker.getUpdateExpr();
5161 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005162 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005163 }
5164 }
5165 if (!IsUpdateExprFound) {
5166 IsUpdateExprFound = !Checker.checkStatement(First);
5167 BinOp = nullptr;
5168 if (IsUpdateExprFound) {
5169 BinOp = dyn_cast<BinaryOperator>(Second);
5170 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
5171 }
5172 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
5173 // { x++; v = x; }
5174 // { x--; v = x; }
5175 // { ++x; v = x; }
5176 // { --x; v = x; }
5177 // { x binop= expr; v = x; }
5178 // { x = x binop expr; v = x; }
5179 // { x = expr binop x; v = x; }
5180 // Check that the second expression has form v = x.
5181 auto *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
5182 llvm::FoldingSetNodeID XId, PossibleXId;
5183 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
5184 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
5185 IsUpdateExprFound = XId == PossibleXId;
5186 if (IsUpdateExprFound) {
5187 V = BinOp->getLHS();
5188 X = Checker.getX();
5189 E = Checker.getExpr();
5190 UE = Checker.getUpdateExpr();
5191 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00005192 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00005193 }
5194 }
5195 }
5196 if (!IsUpdateExprFound) {
5197 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00005198 auto *FirstExpr = dyn_cast<Expr>(First);
5199 auto *SecondExpr = dyn_cast<Expr>(Second);
5200 if (!FirstExpr || !SecondExpr ||
5201 !(FirstExpr->isInstantiationDependent() ||
5202 SecondExpr->isInstantiationDependent())) {
5203 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
5204 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00005205 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00005206 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
5207 : First->getLocStart();
5208 NoteRange = ErrorRange = FirstBinOp
5209 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00005210 : SourceRange(ErrorLoc, ErrorLoc);
5211 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005212 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
5213 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
5214 ErrorFound = NotAnAssignmentOp;
5215 NoteLoc = ErrorLoc = SecondBinOp
5216 ? SecondBinOp->getOperatorLoc()
5217 : Second->getLocStart();
5218 NoteRange = ErrorRange =
5219 SecondBinOp ? SecondBinOp->getSourceRange()
5220 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00005221 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00005222 auto *PossibleXRHSInFirst =
5223 FirstBinOp->getRHS()->IgnoreParenImpCasts();
5224 auto *PossibleXLHSInSecond =
5225 SecondBinOp->getLHS()->IgnoreParenImpCasts();
5226 llvm::FoldingSetNodeID X1Id, X2Id;
5227 PossibleXRHSInFirst->Profile(X1Id, Context,
5228 /*Canonical=*/true);
5229 PossibleXLHSInSecond->Profile(X2Id, Context,
5230 /*Canonical=*/true);
5231 IsUpdateExprFound = X1Id == X2Id;
5232 if (IsUpdateExprFound) {
5233 V = FirstBinOp->getLHS();
5234 X = SecondBinOp->getLHS();
5235 E = SecondBinOp->getRHS();
5236 UE = nullptr;
5237 IsXLHSInRHSPart = false;
5238 IsPostfixUpdate = true;
5239 } else {
5240 ErrorFound = NotASpecificExpression;
5241 ErrorLoc = FirstBinOp->getExprLoc();
5242 ErrorRange = FirstBinOp->getSourceRange();
5243 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
5244 NoteRange = SecondBinOp->getRHS()->getSourceRange();
5245 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00005246 }
5247 }
5248 }
5249 }
5250 } else {
5251 NoteLoc = ErrorLoc = Body->getLocStart();
5252 NoteRange = ErrorRange =
5253 SourceRange(Body->getLocStart(), Body->getLocStart());
5254 ErrorFound = NotTwoSubstatements;
5255 }
5256 } else {
5257 NoteLoc = ErrorLoc = Body->getLocStart();
5258 NoteRange = ErrorRange =
5259 SourceRange(Body->getLocStart(), Body->getLocStart());
5260 ErrorFound = NotACompoundStatement;
5261 }
5262 if (ErrorFound != NoError) {
5263 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
5264 << ErrorRange;
5265 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
5266 return StmtError();
5267 } else if (CurContext->isDependentContext()) {
5268 UE = V = E = X = nullptr;
5269 }
Alexey Bataev459dec02014-07-24 06:46:57 +00005270 }
Alexey Bataevdea47612014-07-23 07:46:59 +00005271 }
Alexey Bataev0162e452014-07-22 10:10:35 +00005272
5273 getCurFunction()->setHasBranchProtectedScope();
5274
Alexey Bataev62cec442014-11-18 10:14:22 +00005275 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00005276 X, V, E, UE, IsXLHSInRHSPart,
5277 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00005278}
5279
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005280StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
5281 Stmt *AStmt,
5282 SourceLocation StartLoc,
5283 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005284 if (!AStmt)
5285 return StmtError();
5286
Samuel Antao4af1b7b2015-12-02 17:44:43 +00005287 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5288 // 1.2.2 OpenMP Language Terminology
5289 // Structured block - An executable statement with a single entry at the
5290 // top and a single exit at the bottom.
5291 // The point of exit cannot be a branch out of the structured block.
5292 // longjmp() and throw() must not violate the entry/exit criteria.
5293 CS->getCapturedDecl()->setNothrow();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005294
Alexey Bataev13314bf2014-10-09 04:18:56 +00005295 // OpenMP [2.16, Nesting of Regions]
5296 // If specified, a teams construct must be contained within a target
5297 // construct. That target construct must contain no statements or directives
5298 // outside of the teams construct.
5299 if (DSAStack->hasInnerTeamsRegion()) {
5300 auto S = AStmt->IgnoreContainers(/*IgnoreCaptured*/ true);
5301 bool OMPTeamsFound = true;
5302 if (auto *CS = dyn_cast<CompoundStmt>(S)) {
5303 auto I = CS->body_begin();
5304 while (I != CS->body_end()) {
5305 auto OED = dyn_cast<OMPExecutableDirective>(*I);
5306 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
5307 OMPTeamsFound = false;
5308 break;
5309 }
5310 ++I;
5311 }
5312 assert(I != CS->body_end() && "Not found statement");
5313 S = *I;
5314 }
5315 if (!OMPTeamsFound) {
5316 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
5317 Diag(DSAStack->getInnerTeamsRegionLoc(),
5318 diag::note_omp_nested_teams_construct_here);
5319 Diag(S->getLocStart(), diag::note_omp_nested_statement_here)
5320 << isa<OMPExecutableDirective>(S);
5321 return StmtError();
5322 }
5323 }
5324
Alexey Bataev0bd520b2014-09-19 08:19:49 +00005325 getCurFunction()->setHasBranchProtectedScope();
5326
5327 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5328}
5329
Michael Wong65f367f2015-07-21 13:44:28 +00005330StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
5331 Stmt *AStmt,
5332 SourceLocation StartLoc,
5333 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005334 if (!AStmt)
5335 return StmtError();
5336
5337 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5338
Michael Wong65f367f2015-07-21 13:44:28 +00005339 getCurFunction()->setHasBranchProtectedScope();
5340
5341 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
5342 AStmt);
5343}
5344
Alexey Bataev13314bf2014-10-09 04:18:56 +00005345StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
5346 Stmt *AStmt, SourceLocation StartLoc,
5347 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005348 if (!AStmt)
5349 return StmtError();
5350
Alexey Bataev13314bf2014-10-09 04:18:56 +00005351 CapturedStmt *CS = cast<CapturedStmt>(AStmt);
5352 // 1.2.2 OpenMP Language Terminology
5353 // Structured block - An executable statement with a single entry at the
5354 // top and a single exit at the bottom.
5355 // The point of exit cannot be a branch out of the structured block.
5356 // longjmp() and throw() must not violate the entry/exit criteria.
5357 CS->getCapturedDecl()->setNothrow();
5358
5359 getCurFunction()->setHasBranchProtectedScope();
5360
5361 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
5362}
5363
Alexey Bataev6d4ed052015-07-01 06:57:41 +00005364StmtResult
5365Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
5366 SourceLocation EndLoc,
5367 OpenMPDirectiveKind CancelRegion) {
5368 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
5369 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
5370 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
5371 << getOpenMPDirectiveName(CancelRegion);
5372 return StmtError();
5373 }
5374 if (DSAStack->isParentNowaitRegion()) {
5375 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
5376 return StmtError();
5377 }
5378 if (DSAStack->isParentOrderedRegion()) {
5379 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
5380 return StmtError();
5381 }
5382 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
5383 CancelRegion);
5384}
5385
Alexey Bataev87933c72015-09-18 08:07:34 +00005386StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
5387 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00005388 SourceLocation EndLoc,
5389 OpenMPDirectiveKind CancelRegion) {
5390 if (CancelRegion != OMPD_parallel && CancelRegion != OMPD_for &&
5391 CancelRegion != OMPD_sections && CancelRegion != OMPD_taskgroup) {
5392 Diag(StartLoc, diag::err_omp_wrong_cancel_region)
5393 << getOpenMPDirectiveName(CancelRegion);
5394 return StmtError();
5395 }
5396 if (DSAStack->isParentNowaitRegion()) {
5397 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
5398 return StmtError();
5399 }
5400 if (DSAStack->isParentOrderedRegion()) {
5401 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
5402 return StmtError();
5403 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005404 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00005405 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
5406 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00005407}
5408
Alexey Bataev382967a2015-12-08 12:06:20 +00005409static bool checkGrainsizeNumTasksClauses(Sema &S,
5410 ArrayRef<OMPClause *> Clauses) {
5411 OMPClause *PrevClause = nullptr;
5412 bool ErrorFound = false;
5413 for (auto *C : Clauses) {
5414 if (C->getClauseKind() == OMPC_grainsize ||
5415 C->getClauseKind() == OMPC_num_tasks) {
5416 if (!PrevClause)
5417 PrevClause = C;
5418 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
5419 S.Diag(C->getLocStart(),
5420 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
5421 << getOpenMPClauseName(C->getClauseKind())
5422 << getOpenMPClauseName(PrevClause->getClauseKind());
5423 S.Diag(PrevClause->getLocStart(),
5424 diag::note_omp_previous_grainsize_num_tasks)
5425 << getOpenMPClauseName(PrevClause->getClauseKind());
5426 ErrorFound = true;
5427 }
5428 }
5429 }
5430 return ErrorFound;
5431}
5432
Alexey Bataev49f6e782015-12-01 04:18:41 +00005433StmtResult Sema::ActOnOpenMPTaskLoopDirective(
5434 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5435 SourceLocation EndLoc,
5436 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
5437 if (!AStmt)
5438 return StmtError();
5439
5440 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5441 OMPLoopDirective::HelperExprs B;
5442 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5443 // define the nested loops number.
5444 unsigned NestedLoopCount =
5445 CheckOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005446 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00005447 VarsWithImplicitDSA, B);
5448 if (NestedLoopCount == 0)
5449 return StmtError();
5450
5451 assert((CurContext->isDependentContext() || B.builtAll()) &&
5452 "omp for loop exprs were not built");
5453
Alexey Bataev382967a2015-12-08 12:06:20 +00005454 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
5455 // The grainsize clause and num_tasks clause are mutually exclusive and may
5456 // not appear on the same taskloop directive.
5457 if (checkGrainsizeNumTasksClauses(*this, Clauses))
5458 return StmtError();
5459
Alexey Bataev49f6e782015-12-01 04:18:41 +00005460 getCurFunction()->setHasBranchProtectedScope();
5461 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
5462 NestedLoopCount, Clauses, AStmt, B);
5463}
5464
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005465StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
5466 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5467 SourceLocation EndLoc,
5468 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
5469 if (!AStmt)
5470 return StmtError();
5471
5472 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5473 OMPLoopDirective::HelperExprs B;
5474 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5475 // define the nested loops number.
5476 unsigned NestedLoopCount =
5477 CheckOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
5478 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
5479 VarsWithImplicitDSA, B);
5480 if (NestedLoopCount == 0)
5481 return StmtError();
5482
5483 assert((CurContext->isDependentContext() || B.builtAll()) &&
5484 "omp for loop exprs were not built");
5485
Alexey Bataev382967a2015-12-08 12:06:20 +00005486 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
5487 // The grainsize clause and num_tasks clause are mutually exclusive and may
5488 // not appear on the same taskloop directive.
5489 if (checkGrainsizeNumTasksClauses(*this, Clauses))
5490 return StmtError();
5491
Alexey Bataev0a6ed842015-12-03 09:40:15 +00005492 getCurFunction()->setHasBranchProtectedScope();
5493 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
5494 NestedLoopCount, Clauses, AStmt, B);
5495}
5496
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005497StmtResult Sema::ActOnOpenMPDistributeDirective(
5498 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
5499 SourceLocation EndLoc,
5500 llvm::DenseMap<VarDecl *, Expr *> &VarsWithImplicitDSA) {
5501 if (!AStmt)
5502 return StmtError();
5503
5504 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
5505 OMPLoopDirective::HelperExprs B;
5506 // In presence of clause 'collapse' with number of loops, it will
5507 // define the nested loops number.
5508 unsigned NestedLoopCount =
5509 CheckOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
5510 nullptr /*ordered not a clause on distribute*/, AStmt,
5511 *this, *DSAStack, VarsWithImplicitDSA, B);
5512 if (NestedLoopCount == 0)
5513 return StmtError();
5514
5515 assert((CurContext->isDependentContext() || B.builtAll()) &&
5516 "omp for loop exprs were not built");
5517
5518 getCurFunction()->setHasBranchProtectedScope();
5519 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
5520 NestedLoopCount, Clauses, AStmt, B);
5521}
5522
Alexey Bataeved09d242014-05-28 05:53:51 +00005523OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005524 SourceLocation StartLoc,
5525 SourceLocation LParenLoc,
5526 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005527 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005528 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00005529 case OMPC_final:
5530 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
5531 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00005532 case OMPC_num_threads:
5533 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
5534 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00005535 case OMPC_safelen:
5536 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
5537 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00005538 case OMPC_simdlen:
5539 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
5540 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00005541 case OMPC_collapse:
5542 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
5543 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005544 case OMPC_ordered:
5545 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
5546 break;
Michael Wonge710d542015-08-07 16:16:36 +00005547 case OMPC_device:
5548 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
5549 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00005550 case OMPC_num_teams:
5551 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
5552 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005553 case OMPC_thread_limit:
5554 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
5555 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00005556 case OMPC_priority:
5557 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
5558 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00005559 case OMPC_grainsize:
5560 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
5561 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00005562 case OMPC_num_tasks:
5563 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
5564 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00005565 case OMPC_hint:
5566 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
5567 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005568 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005569 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005570 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005571 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005572 case OMPC_private:
5573 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00005574 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005575 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00005576 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00005577 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00005578 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005579 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005580 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00005581 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005582 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005583 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005584 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00005585 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005586 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00005587 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00005588 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00005589 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005590 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005591 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00005592 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005593 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00005594 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00005595 case OMPC_nogroup:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005596 case OMPC_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005597 llvm_unreachable("Clause is not allowed.");
5598 }
5599 return Res;
5600}
5601
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005602OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
5603 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005604 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005605 SourceLocation NameModifierLoc,
5606 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005607 SourceLocation EndLoc) {
5608 Expr *ValExpr = Condition;
5609 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
5610 !Condition->isInstantiationDependent() &&
5611 !Condition->containsUnexpandedParameterPack()) {
5612 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
Alexey Bataeved09d242014-05-28 05:53:51 +00005613 Condition->getExprLoc(), Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005614 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005615 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005616
Nikola Smiljanic01a75982014-05-29 10:55:11 +00005617 ValExpr = Val.get();
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005618 }
5619
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005620 return new (Context) OMPIfClause(NameModifier, ValExpr, StartLoc, LParenLoc,
5621 NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005622}
5623
Alexey Bataev3778b602014-07-17 07:32:53 +00005624OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
5625 SourceLocation StartLoc,
5626 SourceLocation LParenLoc,
5627 SourceLocation EndLoc) {
5628 Expr *ValExpr = Condition;
5629 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
5630 !Condition->isInstantiationDependent() &&
5631 !Condition->containsUnexpandedParameterPack()) {
5632 ExprResult Val = ActOnBooleanCondition(DSAStack->getCurScope(),
5633 Condition->getExprLoc(), Condition);
5634 if (Val.isInvalid())
5635 return nullptr;
5636
5637 ValExpr = Val.get();
5638 }
5639
5640 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
5641}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005642ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
5643 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00005644 if (!Op)
5645 return ExprError();
5646
5647 class IntConvertDiagnoser : public ICEConvertDiagnoser {
5648 public:
5649 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00005650 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00005651 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
5652 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005653 return S.Diag(Loc, diag::err_omp_not_integral) << T;
5654 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005655 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
5656 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005657 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
5658 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005659 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
5660 QualType T,
5661 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005662 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
5663 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005664 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
5665 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005666 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00005667 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00005668 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005669 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
5670 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005671 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
5672 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005673 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
5674 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005675 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00005676 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00005677 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005678 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
5679 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00005680 llvm_unreachable("conversion functions are permitted");
5681 }
5682 } ConvertDiagnoser;
5683 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
5684}
5685
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005686static bool IsNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00005687 OpenMPClauseKind CKind,
5688 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005689 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
5690 !ValExpr->isInstantiationDependent()) {
5691 SourceLocation Loc = ValExpr->getExprLoc();
5692 ExprResult Value =
5693 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
5694 if (Value.isInvalid())
5695 return false;
5696
5697 ValExpr = Value.get();
5698 // The expression must evaluate to a non-negative integer value.
5699 llvm::APSInt Result;
5700 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00005701 Result.isSigned() &&
5702 !((!StrictlyPositive && Result.isNonNegative()) ||
5703 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005704 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00005705 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
5706 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005707 return false;
5708 }
5709 }
5710 return true;
5711}
5712
Alexey Bataev568a8332014-03-06 06:15:19 +00005713OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
5714 SourceLocation StartLoc,
5715 SourceLocation LParenLoc,
5716 SourceLocation EndLoc) {
5717 Expr *ValExpr = NumThreads;
Alexey Bataev568a8332014-03-06 06:15:19 +00005718
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005719 // OpenMP [2.5, Restrictions]
5720 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00005721 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
5722 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005723 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00005724
Alexey Bataeved09d242014-05-28 05:53:51 +00005725 return new (Context)
5726 OMPNumThreadsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00005727}
5728
Alexey Bataev62c87d22014-03-21 04:51:18 +00005729ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
5730 OpenMPClauseKind CKind) {
5731 if (!E)
5732 return ExprError();
5733 if (E->isValueDependent() || E->isTypeDependent() ||
5734 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00005735 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00005736 llvm::APSInt Result;
5737 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
5738 if (ICE.isInvalid())
5739 return ExprError();
5740 if (!Result.isStrictlyPositive()) {
5741 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00005742 << getOpenMPClauseName(CKind) << 1 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00005743 return ExprError();
5744 }
Alexander Musman09184fe2014-09-30 05:29:28 +00005745 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
5746 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
5747 << E->getSourceRange();
5748 return ExprError();
5749 }
Alexey Bataev7b6bc882015-11-26 07:50:39 +00005750 if (CKind == OMPC_collapse)
5751 DSAStack->setCollapseNumber(Result.getExtValue());
5752 else if (CKind == OMPC_ordered)
5753 DSAStack->setCollapseNumber(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00005754 return ICE;
5755}
5756
5757OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
5758 SourceLocation LParenLoc,
5759 SourceLocation EndLoc) {
5760 // OpenMP [2.8.1, simd construct, Description]
5761 // The parameter of the safelen clause must be a constant
5762 // positive integer expression.
5763 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
5764 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005765 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00005766 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00005767 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00005768}
5769
Alexey Bataev66b15b52015-08-21 11:14:16 +00005770OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
5771 SourceLocation LParenLoc,
5772 SourceLocation EndLoc) {
5773 // OpenMP [2.8.1, simd construct, Description]
5774 // The parameter of the simdlen clause must be a constant
5775 // positive integer expression.
5776 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
5777 if (Simdlen.isInvalid())
5778 return nullptr;
5779 return new (Context)
5780 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
5781}
5782
Alexander Musman64d33f12014-06-04 07:53:32 +00005783OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
5784 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00005785 SourceLocation LParenLoc,
5786 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00005787 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00005788 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00005789 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00005790 // The parameter of the collapse clause must be a constant
5791 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00005792 ExprResult NumForLoopsResult =
5793 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
5794 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00005795 return nullptr;
5796 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00005797 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00005798}
5799
Alexey Bataev10e775f2015-07-30 11:36:16 +00005800OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
5801 SourceLocation EndLoc,
5802 SourceLocation LParenLoc,
5803 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00005804 // OpenMP [2.7.1, loop construct, Description]
5805 // OpenMP [2.8.1, simd construct, Description]
5806 // OpenMP [2.9.6, distribute construct, Description]
5807 // The parameter of the ordered clause must be a constant
5808 // positive integer expression if any.
5809 if (NumForLoops && LParenLoc.isValid()) {
5810 ExprResult NumForLoopsResult =
5811 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
5812 if (NumForLoopsResult.isInvalid())
5813 return nullptr;
5814 NumForLoops = NumForLoopsResult.get();
Alexey Bataev346265e2015-09-25 10:37:12 +00005815 } else
5816 NumForLoops = nullptr;
5817 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops);
Alexey Bataev10e775f2015-07-30 11:36:16 +00005818 return new (Context)
5819 OMPOrderedClause(NumForLoops, StartLoc, LParenLoc, EndLoc);
5820}
5821
Alexey Bataeved09d242014-05-28 05:53:51 +00005822OMPClause *Sema::ActOnOpenMPSimpleClause(
5823 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
5824 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005825 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005826 switch (Kind) {
5827 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00005828 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00005829 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
5830 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005831 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005832 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00005833 Res = ActOnOpenMPProcBindClause(
5834 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
5835 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005836 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005837 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00005838 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00005839 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00005840 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00005841 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00005842 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005843 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005844 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00005845 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00005846 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00005847 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00005848 case OMPC_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00005849 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00005850 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00005851 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005852 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005853 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00005854 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005855 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005856 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005857 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00005858 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005859 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00005860 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00005861 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00005862 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00005863 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00005864 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00005865 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00005866 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00005867 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00005868 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00005869 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00005870 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00005871 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00005872 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00005873 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00005874 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00005875 case OMPC_hint:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005876 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005877 llvm_unreachable("Clause is not allowed.");
5878 }
5879 return Res;
5880}
5881
5882OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
5883 SourceLocation KindKwLoc,
5884 SourceLocation StartLoc,
5885 SourceLocation LParenLoc,
5886 SourceLocation EndLoc) {
5887 if (Kind == OMPC_DEFAULT_unknown) {
5888 std::string Values;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005889 static_assert(OMPC_DEFAULT_unknown > 0,
5890 "OMPC_DEFAULT_unknown not greater than 0");
Ted Kremenek725a0972014-03-21 17:34:28 +00005891 std::string Sep(", ");
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005892 for (unsigned i = 0; i < OMPC_DEFAULT_unknown; ++i) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005893 Values += "'";
5894 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
5895 Values += "'";
5896 switch (i) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005897 case OMPC_DEFAULT_unknown - 2:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005898 Values += " or ";
5899 break;
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00005900 case OMPC_DEFAULT_unknown - 1:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005901 break;
5902 default:
5903 Values += Sep;
5904 break;
5905 }
5906 }
5907 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00005908 << Values << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005909 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005910 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00005911 switch (Kind) {
5912 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005913 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005914 break;
5915 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005916 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00005917 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005918 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00005919 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00005920 break;
5921 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005922 return new (Context)
5923 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00005924}
5925
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005926OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
5927 SourceLocation KindKwLoc,
5928 SourceLocation StartLoc,
5929 SourceLocation LParenLoc,
5930 SourceLocation EndLoc) {
5931 if (Kind == OMPC_PROC_BIND_unknown) {
5932 std::string Values;
5933 std::string Sep(", ");
5934 for (unsigned i = 0; i < OMPC_PROC_BIND_unknown; ++i) {
5935 Values += "'";
5936 Values += getOpenMPSimpleClauseTypeName(OMPC_proc_bind, i);
5937 Values += "'";
5938 switch (i) {
5939 case OMPC_PROC_BIND_unknown - 2:
5940 Values += " or ";
5941 break;
5942 case OMPC_PROC_BIND_unknown - 1:
5943 break;
5944 default:
5945 Values += Sep;
5946 break;
5947 }
5948 }
5949 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataeved09d242014-05-28 05:53:51 +00005950 << Values << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00005951 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005952 }
Alexey Bataeved09d242014-05-28 05:53:51 +00005953 return new (Context)
5954 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00005955}
5956
Alexey Bataev56dafe82014-06-20 07:16:17 +00005957OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
5958 OpenMPClauseKind Kind, unsigned Argument, Expr *Expr,
5959 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005960 SourceLocation ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00005961 SourceLocation EndLoc) {
5962 OMPClause *Res = nullptr;
5963 switch (Kind) {
5964 case OMPC_schedule:
5965 Res = ActOnOpenMPScheduleClause(
5966 static_cast<OpenMPScheduleClauseKind>(Argument), Expr, StartLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005967 LParenLoc, ArgumentLoc, DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00005968 break;
5969 case OMPC_if:
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005970 Res =
5971 ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument), Expr,
5972 StartLoc, LParenLoc, ArgumentLoc, DelimLoc, EndLoc);
5973 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00005974 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005975 case OMPC_num_threads:
5976 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00005977 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005978 case OMPC_collapse:
5979 case OMPC_default:
5980 case OMPC_proc_bind:
5981 case OMPC_private:
5982 case OMPC_firstprivate:
5983 case OMPC_lastprivate:
5984 case OMPC_shared:
5985 case OMPC_reduction:
5986 case OMPC_linear:
5987 case OMPC_aligned:
5988 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00005989 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00005990 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00005991 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00005992 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00005993 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00005994 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00005995 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00005996 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00005997 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00005998 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00005999 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006000 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006001 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006002 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006003 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006004 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006005 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006006 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006007 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006008 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006009 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006010 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006011 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006012 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006013 case OMPC_unknown:
6014 llvm_unreachable("Clause is not allowed.");
6015 }
6016 return Res;
6017}
6018
6019OMPClause *Sema::ActOnOpenMPScheduleClause(
6020 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
6021 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
6022 SourceLocation EndLoc) {
6023 if (Kind == OMPC_SCHEDULE_unknown) {
6024 std::string Values;
6025 std::string Sep(", ");
6026 for (unsigned i = 0; i < OMPC_SCHEDULE_unknown; ++i) {
6027 Values += "'";
6028 Values += getOpenMPSimpleClauseTypeName(OMPC_schedule, i);
6029 Values += "'";
6030 switch (i) {
6031 case OMPC_SCHEDULE_unknown - 2:
6032 Values += " or ";
6033 break;
6034 case OMPC_SCHEDULE_unknown - 1:
6035 break;
6036 default:
6037 Values += Sep;
6038 break;
6039 }
6040 }
6041 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
6042 << Values << getOpenMPClauseName(OMPC_schedule);
6043 return nullptr;
6044 }
6045 Expr *ValExpr = ChunkSize;
Alexey Bataev040d5402015-05-12 08:35:28 +00006046 Expr *HelperValExpr = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00006047 if (ChunkSize) {
6048 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
6049 !ChunkSize->isInstantiationDependent() &&
6050 !ChunkSize->containsUnexpandedParameterPack()) {
6051 SourceLocation ChunkSizeLoc = ChunkSize->getLocStart();
6052 ExprResult Val =
6053 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
6054 if (Val.isInvalid())
6055 return nullptr;
6056
6057 ValExpr = Val.get();
6058
6059 // OpenMP [2.7.1, Restrictions]
6060 // chunk_size must be a loop invariant integer expression with a positive
6061 // value.
6062 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00006063 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
6064 if (Result.isSigned() && !Result.isStrictlyPositive()) {
6065 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00006066 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00006067 return nullptr;
6068 }
6069 } else if (isParallelOrTaskRegion(DSAStack->getCurrentDirective())) {
6070 auto *ImpVar = buildVarDecl(*this, ChunkSize->getExprLoc(),
6071 ChunkSize->getType(), ".chunk.");
6072 auto *ImpVarRef = buildDeclRefExpr(*this, ImpVar, ChunkSize->getType(),
6073 ChunkSize->getExprLoc(),
6074 /*RefersToCapture=*/true);
6075 HelperValExpr = ImpVarRef;
Alexey Bataev56dafe82014-06-20 07:16:17 +00006076 }
6077 }
6078 }
6079
6080 return new (Context) OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc,
Alexey Bataev040d5402015-05-12 08:35:28 +00006081 EndLoc, Kind, ValExpr, HelperValExpr);
Alexey Bataev56dafe82014-06-20 07:16:17 +00006082}
6083
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006084OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
6085 SourceLocation StartLoc,
6086 SourceLocation EndLoc) {
6087 OMPClause *Res = nullptr;
6088 switch (Kind) {
6089 case OMPC_ordered:
6090 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
6091 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00006092 case OMPC_nowait:
6093 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
6094 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006095 case OMPC_untied:
6096 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
6097 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006098 case OMPC_mergeable:
6099 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
6100 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006101 case OMPC_read:
6102 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
6103 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00006104 case OMPC_write:
6105 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
6106 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006107 case OMPC_update:
6108 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
6109 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00006110 case OMPC_capture:
6111 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
6112 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006113 case OMPC_seq_cst:
6114 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
6115 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00006116 case OMPC_threads:
6117 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
6118 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006119 case OMPC_simd:
6120 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
6121 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00006122 case OMPC_nogroup:
6123 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
6124 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006125 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006126 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006127 case OMPC_num_threads:
6128 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006129 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006130 case OMPC_collapse:
6131 case OMPC_schedule:
6132 case OMPC_private:
6133 case OMPC_firstprivate:
6134 case OMPC_lastprivate:
6135 case OMPC_shared:
6136 case OMPC_reduction:
6137 case OMPC_linear:
6138 case OMPC_aligned:
6139 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00006140 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006141 case OMPC_default:
6142 case OMPC_proc_bind:
6143 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00006144 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006145 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00006146 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006147 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006148 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006149 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006150 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006151 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00006152 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006153 case OMPC_hint:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006154 case OMPC_unknown:
6155 llvm_unreachable("Clause is not allowed.");
6156 }
6157 return Res;
6158}
6159
Alexey Bataev236070f2014-06-20 11:19:47 +00006160OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
6161 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00006162 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00006163 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
6164}
6165
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006166OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
6167 SourceLocation EndLoc) {
6168 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
6169}
6170
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006171OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
6172 SourceLocation EndLoc) {
6173 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
6174}
6175
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006176OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
6177 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006178 return new (Context) OMPReadClause(StartLoc, EndLoc);
6179}
6180
Alexey Bataevdea47612014-07-23 07:46:59 +00006181OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
6182 SourceLocation EndLoc) {
6183 return new (Context) OMPWriteClause(StartLoc, EndLoc);
6184}
6185
Alexey Bataev67a4f222014-07-23 10:25:33 +00006186OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
6187 SourceLocation EndLoc) {
6188 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
6189}
6190
Alexey Bataev459dec02014-07-24 06:46:57 +00006191OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
6192 SourceLocation EndLoc) {
6193 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
6194}
6195
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006196OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
6197 SourceLocation EndLoc) {
6198 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
6199}
6200
Alexey Bataev346265e2015-09-25 10:37:12 +00006201OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
6202 SourceLocation EndLoc) {
6203 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
6204}
6205
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006206OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
6207 SourceLocation EndLoc) {
6208 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
6209}
6210
Alexey Bataevb825de12015-12-07 10:51:44 +00006211OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
6212 SourceLocation EndLoc) {
6213 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
6214}
6215
Alexey Bataevc5e02582014-06-16 07:08:35 +00006216OMPClause *Sema::ActOnOpenMPVarListClause(
6217 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
6218 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
6219 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006220 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Kelvin Li0bff7af2015-11-23 05:32:03 +00006221 OpenMPLinearClauseKind LinKind, OpenMPMapClauseKind MapTypeModifier,
6222 OpenMPMapClauseKind MapType, SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00006223 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006224 switch (Kind) {
6225 case OMPC_private:
6226 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6227 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006228 case OMPC_firstprivate:
6229 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6230 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00006231 case OMPC_lastprivate:
6232 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6233 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00006234 case OMPC_shared:
6235 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
6236 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006237 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00006238 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
6239 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006240 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00006241 case OMPC_linear:
6242 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00006243 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00006244 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00006245 case OMPC_aligned:
6246 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
6247 ColonLoc, EndLoc);
6248 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00006249 case OMPC_copyin:
6250 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
6251 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00006252 case OMPC_copyprivate:
6253 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
6254 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00006255 case OMPC_flush:
6256 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
6257 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006258 case OMPC_depend:
Kelvin Li0bff7af2015-11-23 05:32:03 +00006259 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
6260 StartLoc, LParenLoc, EndLoc);
6261 break;
6262 case OMPC_map:
6263 Res = ActOnOpenMPMapClause(MapTypeModifier, MapType, DepLinMapLoc, ColonLoc,
6264 VarList, StartLoc, LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00006265 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00006266 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00006267 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00006268 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00006269 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00006270 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00006271 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006272 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00006273 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00006274 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00006275 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00006276 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00006277 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00006278 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006279 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006280 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00006281 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00006282 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00006283 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00006284 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00006285 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00006286 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006287 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00006288 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00006289 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00006290 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00006291 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00006292 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00006293 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00006294 case OMPC_hint:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006295 case OMPC_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006296 llvm_unreachable("Clause is not allowed.");
6297 }
6298 return Res;
6299}
6300
6301OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
6302 SourceLocation StartLoc,
6303 SourceLocation LParenLoc,
6304 SourceLocation EndLoc) {
6305 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00006306 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeved09d242014-05-28 05:53:51 +00006307 for (auto &RefExpr : VarList) {
6308 assert(RefExpr && "NULL expr in OpenMP private clause.");
6309 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006310 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006311 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006312 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006313 continue;
6314 }
6315
Alexey Bataeved09d242014-05-28 05:53:51 +00006316 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006317 // OpenMP [2.1, C/C++]
6318 // A list item is a variable name.
6319 // OpenMP [2.9.3.3, Restrictions, p.1]
6320 // A variable that is part of another variable (as an array or
6321 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00006322 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006323 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006324 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006325 continue;
6326 }
6327 Decl *D = DE->getDecl();
6328 VarDecl *VD = cast<VarDecl>(D);
6329
6330 QualType Type = VD->getType();
6331 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6332 // It will be analyzed later.
6333 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006334 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006335 continue;
6336 }
6337
6338 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
6339 // A variable that appears in a private clause must not have an incomplete
6340 // type or a reference type.
6341 if (RequireCompleteType(ELoc, Type,
6342 diag::err_omp_private_incomplete_type)) {
6343 continue;
6344 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006345 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006346
Alexey Bataev758e55e2013-09-06 18:03:48 +00006347 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6348 // in a Construct]
6349 // Variables with the predetermined data-sharing attributes may not be
6350 // listed in data-sharing attributes clauses, except for the cases
6351 // listed below. For these exceptions only, listing a predetermined
6352 // variable in a data-sharing attribute clause is allowed and overrides
6353 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006354 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006355 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006356 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
6357 << getOpenMPClauseName(OMPC_private);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006358 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006359 continue;
6360 }
6361
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006362 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00006363 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006364 DSAStack->getCurrentDirective() == OMPD_task) {
6365 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
6366 << getOpenMPClauseName(OMPC_private) << Type
6367 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
6368 bool IsDecl =
6369 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6370 Diag(VD->getLocation(),
6371 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6372 << VD;
6373 continue;
6374 }
6375
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006376 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
6377 // A variable of class type (or array thereof) that appears in a private
6378 // clause requires an accessible, unambiguous default constructor for the
6379 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00006380 // Generate helper private variable and initialize it with the default
6381 // value. The address of the original variable is replaced by the address of
6382 // the new private variable in CodeGen. This new variable is not added to
6383 // IdResolver, so the code in the OpenMP region uses original variable for
6384 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006385 Type = Type.getUnqualifiedType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006386 auto VDPrivate = buildVarDecl(*this, DE->getExprLoc(), Type, VD->getName(),
6387 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00006388 ActOnUninitializedDecl(VDPrivate, /*TypeMayContainAuto=*/false);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006389 if (VDPrivate->isInvalidDecl())
6390 continue;
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006391 auto VDPrivateRefExpr = buildDeclRefExpr(
6392 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev03b340a2014-10-21 03:16:40 +00006393
Alexey Bataev758e55e2013-09-06 18:03:48 +00006394 DSAStack->addDSA(VD, DE, OMPC_private);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006395 Vars.push_back(DE);
Alexey Bataev03b340a2014-10-21 03:16:40 +00006396 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006397 }
6398
Alexey Bataeved09d242014-05-28 05:53:51 +00006399 if (Vars.empty())
6400 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006401
Alexey Bataev03b340a2014-10-21 03:16:40 +00006402 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
6403 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00006404}
6405
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006406namespace {
6407class DiagsUninitializedSeveretyRAII {
6408private:
6409 DiagnosticsEngine &Diags;
6410 SourceLocation SavedLoc;
6411 bool IsIgnored;
6412
6413public:
6414 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
6415 bool IsIgnored)
6416 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
6417 if (!IsIgnored) {
6418 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
6419 /*Map*/ diag::Severity::Ignored, Loc);
6420 }
6421 }
6422 ~DiagsUninitializedSeveretyRAII() {
6423 if (!IsIgnored)
6424 Diags.popMappings(SavedLoc);
6425 }
6426};
Alexander Kornienkoab9db512015-06-22 23:07:51 +00006427}
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006428
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006429OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
6430 SourceLocation StartLoc,
6431 SourceLocation LParenLoc,
6432 SourceLocation EndLoc) {
6433 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006434 SmallVector<Expr *, 8> PrivateCopies;
6435 SmallVector<Expr *, 8> Inits;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006436 bool IsImplicitClause =
6437 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
6438 auto ImplicitClauseLoc = DSAStack->getConstructLoc();
6439
Alexey Bataeved09d242014-05-28 05:53:51 +00006440 for (auto &RefExpr : VarList) {
6441 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
6442 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006443 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006444 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006445 PrivateCopies.push_back(nullptr);
6446 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006447 continue;
6448 }
6449
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006450 SourceLocation ELoc =
6451 IsImplicitClause ? ImplicitClauseLoc : RefExpr->getExprLoc();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006452 // OpenMP [2.1, C/C++]
6453 // A list item is a variable name.
6454 // OpenMP [2.9.3.3, Restrictions, p.1]
6455 // A variable that is part of another variable (as an array or
6456 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00006457 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006458 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006459 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006460 continue;
6461 }
6462 Decl *D = DE->getDecl();
6463 VarDecl *VD = cast<VarDecl>(D);
6464
6465 QualType Type = VD->getType();
6466 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6467 // It will be analyzed later.
6468 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006469 PrivateCopies.push_back(nullptr);
6470 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006471 continue;
6472 }
6473
6474 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
6475 // A variable that appears in a private clause must not have an incomplete
6476 // type or a reference type.
6477 if (RequireCompleteType(ELoc, Type,
6478 diag::err_omp_firstprivate_incomplete_type)) {
6479 continue;
6480 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006481 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006482
6483 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
6484 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +00006485 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006486 // class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006487 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006488
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006489 // If an implicit firstprivate variable found it was checked already.
6490 if (!IsImplicitClause) {
6491 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006492 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006493 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
6494 // A list item that specifies a given variable may not appear in more
6495 // than one clause on the same directive, except that a variable may be
6496 // specified in both firstprivate and lastprivate clauses.
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006497 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevf29276e2014-06-18 04:14:57 +00006498 DVar.CKind != OMPC_lastprivate && DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006499 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00006500 << getOpenMPClauseName(DVar.CKind)
6501 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006502 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006503 continue;
6504 }
6505
6506 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6507 // in a Construct]
6508 // Variables with the predetermined data-sharing attributes may not be
6509 // listed in data-sharing attributes clauses, except for the cases
6510 // listed below. For these exceptions only, listing a predetermined
6511 // variable in a data-sharing attribute clause is allowed and overrides
6512 // the variable's predetermined data-sharing attributes.
6513 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6514 // in a Construct, C/C++, p.2]
6515 // Variables with const-qualified type having no mutable member may be
6516 // listed in a firstprivate clause, even if they are static data members.
6517 if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
6518 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
6519 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +00006520 << getOpenMPClauseName(DVar.CKind)
6521 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006522 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006523 continue;
6524 }
6525
Alexey Bataevf29276e2014-06-18 04:14:57 +00006526 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006527 // OpenMP [2.9.3.4, Restrictions, p.2]
6528 // A list item that is private within a parallel region must not appear
6529 // in a firstprivate clause on a worksharing construct if any of the
6530 // worksharing regions arising from the worksharing construct ever bind
6531 // to any of the parallel regions arising from the parallel construct.
Alexey Bataev549210e2014-06-24 04:39:47 +00006532 if (isOpenMPWorksharingDirective(CurrDir) &&
6533 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006534 DVar = DSAStack->getImplicitDSA(VD, true);
6535 if (DVar.CKind != OMPC_shared &&
6536 (isOpenMPParallelDirective(DVar.DKind) ||
6537 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00006538 Diag(ELoc, diag::err_omp_required_access)
6539 << getOpenMPClauseName(OMPC_firstprivate)
6540 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006541 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006542 continue;
6543 }
6544 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006545 // OpenMP [2.9.3.4, Restrictions, p.3]
6546 // A list item that appears in a reduction clause of a parallel construct
6547 // must not appear in a firstprivate clause on a worksharing or task
6548 // construct if any of the worksharing or task regions arising from the
6549 // worksharing or task construct ever bind to any of the parallel regions
6550 // arising from the parallel construct.
6551 // OpenMP [2.9.3.4, Restrictions, p.4]
6552 // A list item that appears in a reduction clause in worksharing
6553 // construct must not appear in a firstprivate clause in a task construct
6554 // encountered during execution of any of the worksharing regions arising
6555 // from the worksharing construct.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006556 if (CurrDir == OMPD_task) {
6557 DVar =
6558 DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
6559 [](OpenMPDirectiveKind K) -> bool {
6560 return isOpenMPParallelDirective(K) ||
6561 isOpenMPWorksharingDirective(K);
6562 },
6563 false);
6564 if (DVar.CKind == OMPC_reduction &&
6565 (isOpenMPParallelDirective(DVar.DKind) ||
6566 isOpenMPWorksharingDirective(DVar.DKind))) {
6567 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
6568 << getOpenMPDirectiveName(DVar.DKind);
6569 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6570 continue;
6571 }
6572 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006573
6574 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
6575 // A list item that is private within a teams region must not appear in a
6576 // firstprivate clause on a distribute construct if any of the distribute
6577 // regions arising from the distribute construct ever bind to any of the
6578 // teams regions arising from the teams construct.
6579 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
6580 // A list item that appears in a reduction clause of a teams construct
6581 // must not appear in a firstprivate clause on a distribute construct if
6582 // any of the distribute regions arising from the distribute construct
6583 // ever bind to any of the teams regions arising from the teams construct.
6584 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
6585 // A list item may appear in a firstprivate or lastprivate clause but not
6586 // both.
6587 if (CurrDir == OMPD_distribute) {
6588 DVar = DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_private),
6589 [](OpenMPDirectiveKind K) -> bool {
6590 return isOpenMPTeamsDirective(K);
6591 },
6592 false);
6593 if (DVar.CKind == OMPC_private && isOpenMPTeamsDirective(DVar.DKind)) {
6594 Diag(ELoc, diag::err_omp_firstprivate_distribute_private_teams);
6595 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6596 continue;
6597 }
6598 DVar = DSAStack->hasInnermostDSA(VD, MatchesAnyClause(OMPC_reduction),
6599 [](OpenMPDirectiveKind K) -> bool {
6600 return isOpenMPTeamsDirective(K);
6601 },
6602 false);
6603 if (DVar.CKind == OMPC_reduction &&
6604 isOpenMPTeamsDirective(DVar.DKind)) {
6605 Diag(ELoc, diag::err_omp_firstprivate_distribute_in_teams_reduction);
6606 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6607 continue;
6608 }
6609 DVar = DSAStack->getTopDSA(VD, false);
6610 if (DVar.CKind == OMPC_lastprivate) {
6611 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
6612 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6613 continue;
6614 }
6615 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006616 }
6617
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006618 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00006619 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataevccb59ec2015-05-19 08:44:56 +00006620 DSAStack->getCurrentDirective() == OMPD_task) {
6621 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
6622 << getOpenMPClauseName(OMPC_firstprivate) << Type
6623 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
6624 bool IsDecl =
6625 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
6626 Diag(VD->getLocation(),
6627 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
6628 << VD;
6629 continue;
6630 }
6631
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006632 Type = Type.getUnqualifiedType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006633 auto VDPrivate = buildVarDecl(*this, ELoc, Type, VD->getName(),
6634 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006635 // Generate helper private variable and initialize it with the value of the
6636 // original variable. The address of the original variable is replaced by
6637 // the address of the new private variable in the CodeGen. This new variable
6638 // is not added to IdResolver, so the code in the OpenMP region uses
6639 // original variable for proper diagnostics and variable capturing.
6640 Expr *VDInitRefExpr = nullptr;
6641 // For arrays generate initializer for single element and replace it by the
6642 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006643 if (Type->isArrayType()) {
6644 auto VDInit =
6645 buildVarDecl(*this, DE->getExprLoc(), ElemType, VD->getName());
6646 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006647 auto Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006648 ElemType = ElemType.getUnqualifiedType();
6649 auto *VDInitTemp = buildVarDecl(*this, DE->getLocStart(), ElemType,
6650 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +00006651 InitializedEntity Entity =
6652 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006653 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
6654
6655 InitializationSequence InitSeq(*this, Entity, Kind, Init);
6656 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
6657 if (Result.isInvalid())
6658 VDPrivate->setInvalidDecl();
6659 else
6660 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +00006661 // Remove temp variable declaration.
6662 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006663 } else {
Alexey Bataev69c62a92015-04-15 04:52:20 +00006664 auto *VDInit =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006665 buildVarDecl(*this, DE->getLocStart(), Type, ".firstprivate.temp");
Alexey Bataevf120c0d2015-05-19 07:46:42 +00006666 VDInitRefExpr =
6667 buildDeclRefExpr(*this, VDInit, DE->getType(), DE->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +00006668 AddInitializerToDecl(VDPrivate,
6669 DefaultLvalueConversion(VDInitRefExpr).get(),
6670 /*DirectInit=*/false, /*TypeMayContainAuto=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006671 }
6672 if (VDPrivate->isInvalidDecl()) {
6673 if (IsImplicitClause) {
6674 Diag(DE->getExprLoc(),
6675 diag::note_omp_task_predetermined_firstprivate_here);
6676 }
6677 continue;
6678 }
6679 CurContext->addDecl(VDPrivate);
Alexey Bataev39f915b82015-05-08 10:41:21 +00006680 auto VDPrivateRefExpr = buildDeclRefExpr(
6681 *this, VDPrivate, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006682 DSAStack->addDSA(VD, DE, OMPC_firstprivate);
6683 Vars.push_back(DE);
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006684 PrivateCopies.push_back(VDPrivateRefExpr);
6685 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006686 }
6687
Alexey Bataeved09d242014-05-28 05:53:51 +00006688 if (Vars.empty())
6689 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006690
6691 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev4a5bb772014-10-08 14:01:46 +00006692 Vars, PrivateCopies, Inits);
Alexey Bataevd5af8e42013-10-01 05:32:34 +00006693}
6694
Alexander Musman1bb328c2014-06-04 13:06:39 +00006695OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
6696 SourceLocation StartLoc,
6697 SourceLocation LParenLoc,
6698 SourceLocation EndLoc) {
6699 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +00006700 SmallVector<Expr *, 8> SrcExprs;
6701 SmallVector<Expr *, 8> DstExprs;
6702 SmallVector<Expr *, 8> AssignmentOps;
Alexander Musman1bb328c2014-06-04 13:06:39 +00006703 for (auto &RefExpr : VarList) {
6704 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
6705 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
6706 // It will be analyzed later.
6707 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +00006708 SrcExprs.push_back(nullptr);
6709 DstExprs.push_back(nullptr);
6710 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006711 continue;
6712 }
6713
6714 SourceLocation ELoc = RefExpr->getExprLoc();
6715 // OpenMP [2.1, C/C++]
6716 // A list item is a variable name.
6717 // OpenMP [2.14.3.5, Restrictions, p.1]
6718 // A variable that is part of another variable (as an array or structure
6719 // element) cannot appear in a lastprivate clause.
6720 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
6721 if (!DE || !isa<VarDecl>(DE->getDecl())) {
6722 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
6723 continue;
6724 }
6725 Decl *D = DE->getDecl();
6726 VarDecl *VD = cast<VarDecl>(D);
6727
6728 QualType Type = VD->getType();
6729 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6730 // It will be analyzed later.
6731 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00006732 SrcExprs.push_back(nullptr);
6733 DstExprs.push_back(nullptr);
6734 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006735 continue;
6736 }
6737
6738 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
6739 // A variable that appears in a lastprivate clause must not have an
6740 // incomplete type or a reference type.
6741 if (RequireCompleteType(ELoc, Type,
6742 diag::err_omp_lastprivate_incomplete_type)) {
6743 continue;
6744 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00006745 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +00006746
6747 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
6748 // in a Construct]
6749 // Variables with the predetermined data-sharing attributes may not be
6750 // listed in data-sharing attributes clauses, except for the cases
6751 // listed below.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006752 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006753 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
6754 DVar.CKind != OMPC_firstprivate &&
6755 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
6756 Diag(ELoc, diag::err_omp_wrong_dsa)
6757 << getOpenMPClauseName(DVar.CKind)
6758 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006759 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006760 continue;
6761 }
6762
Alexey Bataevf29276e2014-06-18 04:14:57 +00006763 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
6764 // OpenMP [2.14.3.5, Restrictions, p.2]
6765 // A list item that is private within a parallel region, or that appears in
6766 // the reduction clause of a parallel construct, must not appear in a
6767 // lastprivate clause on a worksharing construct if any of the corresponding
6768 // worksharing regions ever binds to any of the corresponding parallel
6769 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +00006770 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +00006771 if (isOpenMPWorksharingDirective(CurrDir) &&
6772 !isOpenMPParallelDirective(CurrDir)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006773 DVar = DSAStack->getImplicitDSA(VD, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006774 if (DVar.CKind != OMPC_shared) {
6775 Diag(ELoc, diag::err_omp_required_access)
6776 << getOpenMPClauseName(OMPC_lastprivate)
6777 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006778 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006779 continue;
6780 }
6781 }
Alexander Musman1bb328c2014-06-04 13:06:39 +00006782 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +00006783 // A variable of class type (or array thereof) that appears in a
6784 // lastprivate clause requires an accessible, unambiguous default
6785 // constructor for the class type, unless the list item is also specified
6786 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +00006787 // A variable of class type (or array thereof) that appears in a
6788 // lastprivate clause requires an accessible, unambiguous copy assignment
6789 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +00006790 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev39f915b82015-05-08 10:41:21 +00006791 auto *SrcVD = buildVarDecl(*this, DE->getLocStart(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006792 Type.getUnqualifiedType(), ".lastprivate.src",
6793 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00006794 auto *PseudoSrcExpr = buildDeclRefExpr(
6795 *this, SrcVD, Type.getUnqualifiedType(), DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00006796 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00006797 buildVarDecl(*this, DE->getLocStart(), Type, ".lastprivate.dst",
6798 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev38e89532015-04-16 04:54:05 +00006799 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00006800 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataev38e89532015-04-16 04:54:05 +00006801 // For arrays generate assignment operation for single element and replace
6802 // it by the original array element in CodeGen.
6803 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
6804 PseudoDstExpr, PseudoSrcExpr);
6805 if (AssignmentOp.isInvalid())
6806 continue;
6807 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
6808 /*DiscardedValue=*/true);
6809 if (AssignmentOp.isInvalid())
6810 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +00006811
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00006812 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
6813 // A list item may appear in a firstprivate or lastprivate clause but not
6814 // both.
6815 if (CurrDir == OMPD_distribute) {
6816 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
6817 if (DVar.CKind == OMPC_firstprivate) {
6818 Diag(ELoc, diag::err_omp_firstprivate_and_lastprivate_in_distribute);
6819 ReportOriginalDSA(*this, DSAStack, VD, DVar);
6820 continue;
6821 }
6822 }
6823
Alexey Bataev39f915b82015-05-08 10:41:21 +00006824 if (TopDVar.CKind != OMPC_firstprivate)
Alexey Bataevf29276e2014-06-18 04:14:57 +00006825 DSAStack->addDSA(VD, DE, OMPC_lastprivate);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006826 Vars.push_back(DE);
Alexey Bataev38e89532015-04-16 04:54:05 +00006827 SrcExprs.push_back(PseudoSrcExpr);
6828 DstExprs.push_back(PseudoDstExpr);
6829 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +00006830 }
6831
6832 if (Vars.empty())
6833 return nullptr;
6834
6835 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev38e89532015-04-16 04:54:05 +00006836 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexander Musman1bb328c2014-06-04 13:06:39 +00006837}
6838
Alexey Bataev758e55e2013-09-06 18:03:48 +00006839OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
6840 SourceLocation StartLoc,
6841 SourceLocation LParenLoc,
6842 SourceLocation EndLoc) {
6843 SmallVector<Expr *, 8> Vars;
Alexey Bataeved09d242014-05-28 05:53:51 +00006844 for (auto &RefExpr : VarList) {
6845 assert(RefExpr && "NULL expr in OpenMP shared clause.");
6846 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +00006847 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00006848 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006849 continue;
6850 }
6851
Alexey Bataeved09d242014-05-28 05:53:51 +00006852 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataev758e55e2013-09-06 18:03:48 +00006853 // OpenMP [2.1, C/C++]
6854 // A list item is a variable name.
Alexey Bataevd4dbdf52014-03-06 12:27:56 +00006855 // OpenMP [2.14.3.2, Restrictions, p.1]
6856 // A variable that is part of another variable (as an array or structure
6857 // element) cannot appear in a shared unless it is a static data member
6858 // of a C++ class.
Alexey Bataeved09d242014-05-28 05:53:51 +00006859 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006860 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00006861 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataev758e55e2013-09-06 18:03:48 +00006862 continue;
6863 }
6864 Decl *D = DE->getDecl();
6865 VarDecl *VD = cast<VarDecl>(D);
6866
6867 QualType Type = VD->getType();
6868 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
6869 // It will be analyzed later.
6870 Vars.push_back(DE);
6871 continue;
6872 }
6873
6874 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
6875 // in a Construct]
6876 // Variables with the predetermined data-sharing attributes may not be
6877 // listed in data-sharing attributes clauses, except for the cases
6878 // listed below. For these exceptions only, listing a predetermined
6879 // variable in a data-sharing attribute clause is allowed and overrides
6880 // the variable's predetermined data-sharing attributes.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006881 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeved09d242014-05-28 05:53:51 +00006882 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
6883 DVar.RefExpr) {
6884 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
6885 << getOpenMPClauseName(OMPC_shared);
Alexey Bataev7ff55242014-06-19 09:13:45 +00006886 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00006887 continue;
6888 }
6889
6890 DSAStack->addDSA(VD, DE, OMPC_shared);
6891 Vars.push_back(DE);
6892 }
6893
Alexey Bataeved09d242014-05-28 05:53:51 +00006894 if (Vars.empty())
6895 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +00006896
6897 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
6898}
6899
Alexey Bataevc5e02582014-06-16 07:08:35 +00006900namespace {
6901class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
6902 DSAStackTy *Stack;
6903
6904public:
6905 bool VisitDeclRefExpr(DeclRefExpr *E) {
6906 if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006907 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, false);
Alexey Bataevc5e02582014-06-16 07:08:35 +00006908 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
6909 return false;
6910 if (DVar.CKind != OMPC_unknown)
6911 return true;
Alexey Bataevf29276e2014-06-18 04:14:57 +00006912 DSAStackTy::DSAVarData DVarPrivate =
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006913 Stack->hasDSA(VD, isOpenMPPrivate, MatchesAlways(), false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00006914 if (DVarPrivate.CKind != OMPC_unknown)
Alexey Bataevc5e02582014-06-16 07:08:35 +00006915 return true;
6916 return false;
6917 }
6918 return false;
6919 }
6920 bool VisitStmt(Stmt *S) {
6921 for (auto Child : S->children()) {
6922 if (Child && Visit(Child))
6923 return true;
6924 }
6925 return false;
6926 }
Alexey Bataev23b69422014-06-18 07:08:49 +00006927 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +00006928};
Alexey Bataev23b69422014-06-18 07:08:49 +00006929} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +00006930
6931OMPClause *Sema::ActOnOpenMPReductionClause(
6932 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
6933 SourceLocation ColonLoc, SourceLocation EndLoc,
6934 CXXScopeSpec &ReductionIdScopeSpec,
6935 const DeclarationNameInfo &ReductionId) {
6936 // TODO: Allow scope specification search when 'declare reduction' is
6937 // supported.
6938 assert(ReductionIdScopeSpec.isEmpty() &&
6939 "No support for scoped reduction identifiers yet.");
6940
6941 auto DN = ReductionId.getName();
6942 auto OOK = DN.getCXXOverloadedOperator();
6943 BinaryOperatorKind BOK = BO_Comma;
6944
6945 // OpenMP [2.14.3.6, reduction clause]
6946 // C
6947 // reduction-identifier is either an identifier or one of the following
6948 // operators: +, -, *, &, |, ^, && and ||
6949 // C++
6950 // reduction-identifier is either an id-expression or one of the following
6951 // operators: +, -, *, &, |, ^, && and ||
6952 // FIXME: Only 'min' and 'max' identifiers are supported for now.
6953 switch (OOK) {
6954 case OO_Plus:
6955 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006956 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006957 break;
6958 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006959 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006960 break;
6961 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006962 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006963 break;
6964 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006965 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006966 break;
6967 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006968 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +00006969 break;
6970 case OO_AmpAmp:
6971 BOK = BO_LAnd;
6972 break;
6973 case OO_PipePipe:
6974 BOK = BO_LOr;
6975 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00006976 case OO_New:
6977 case OO_Delete:
6978 case OO_Array_New:
6979 case OO_Array_Delete:
6980 case OO_Slash:
6981 case OO_Percent:
6982 case OO_Tilde:
6983 case OO_Exclaim:
6984 case OO_Equal:
6985 case OO_Less:
6986 case OO_Greater:
6987 case OO_LessEqual:
6988 case OO_GreaterEqual:
6989 case OO_PlusEqual:
6990 case OO_MinusEqual:
6991 case OO_StarEqual:
6992 case OO_SlashEqual:
6993 case OO_PercentEqual:
6994 case OO_CaretEqual:
6995 case OO_AmpEqual:
6996 case OO_PipeEqual:
6997 case OO_LessLess:
6998 case OO_GreaterGreater:
6999 case OO_LessLessEqual:
7000 case OO_GreaterGreaterEqual:
7001 case OO_EqualEqual:
7002 case OO_ExclaimEqual:
7003 case OO_PlusPlus:
7004 case OO_MinusMinus:
7005 case OO_Comma:
7006 case OO_ArrowStar:
7007 case OO_Arrow:
7008 case OO_Call:
7009 case OO_Subscript:
7010 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +00007011 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007012 case NUM_OVERLOADED_OPERATORS:
7013 llvm_unreachable("Unexpected reduction identifier");
7014 case OO_None:
Alexey Bataevc5e02582014-06-16 07:08:35 +00007015 if (auto II = DN.getAsIdentifierInfo()) {
7016 if (II->isStr("max"))
7017 BOK = BO_GT;
7018 else if (II->isStr("min"))
7019 BOK = BO_LT;
7020 }
7021 break;
7022 }
7023 SourceRange ReductionIdRange;
7024 if (ReductionIdScopeSpec.isValid()) {
7025 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
7026 }
7027 ReductionIdRange.setEnd(ReductionId.getEndLoc());
7028 if (BOK == BO_Comma) {
7029 // Not allowed reduction identifier is found.
7030 Diag(ReductionId.getLocStart(), diag::err_omp_unknown_reduction_identifier)
7031 << ReductionIdRange;
7032 return nullptr;
7033 }
7034
7035 SmallVector<Expr *, 8> Vars;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007036 SmallVector<Expr *, 8> Privates;
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007037 SmallVector<Expr *, 8> LHSs;
7038 SmallVector<Expr *, 8> RHSs;
7039 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007040 for (auto RefExpr : VarList) {
7041 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
7042 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7043 // It will be analyzed later.
7044 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007045 Privates.push_back(nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007046 LHSs.push_back(nullptr);
7047 RHSs.push_back(nullptr);
7048 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007049 continue;
7050 }
7051
7052 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
7053 RefExpr->isInstantiationDependent() ||
7054 RefExpr->containsUnexpandedParameterPack()) {
7055 // It will be analyzed later.
7056 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007057 Privates.push_back(nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007058 LHSs.push_back(nullptr);
7059 RHSs.push_back(nullptr);
7060 ReductionOps.push_back(nullptr);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007061 continue;
7062 }
7063
7064 auto ELoc = RefExpr->getExprLoc();
7065 auto ERange = RefExpr->getSourceRange();
7066 // OpenMP [2.1, C/C++]
7067 // A list item is a variable or array section, subject to the restrictions
7068 // specified in Section 2.4 on page 42 and in each of the sections
7069 // describing clauses and directives for which a list appears.
7070 // OpenMP [2.14.3.3, Restrictions, p.1]
7071 // A variable that is part of another variable (as an array or
7072 // structure element) cannot appear in a private clause.
Alexey Bataeva1764212015-09-30 09:22:36 +00007073 auto *DE = dyn_cast<DeclRefExpr>(RefExpr);
7074 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr);
7075 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr);
7076 if (!ASE && !OASE && (!DE || !isa<VarDecl>(DE->getDecl()))) {
7077 Diag(ELoc, diag::err_omp_expected_var_name_or_array_item) << ERange;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007078 continue;
7079 }
Alexey Bataeva1764212015-09-30 09:22:36 +00007080 QualType Type;
7081 VarDecl *VD = nullptr;
7082 if (DE) {
7083 auto D = DE->getDecl();
7084 VD = cast<VarDecl>(D);
7085 Type = VD->getType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007086 } else if (ASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007087 Type = ASE->getType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007088 auto *Base = ASE->getBase()->IgnoreParenImpCasts();
7089 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7090 Base = TempASE->getBase()->IgnoreParenImpCasts();
7091 DE = dyn_cast<DeclRefExpr>(Base);
7092 if (DE)
7093 VD = dyn_cast<VarDecl>(DE->getDecl());
7094 if (!VD) {
7095 Diag(Base->getExprLoc(), diag::err_omp_expected_base_var_name)
7096 << 0 << Base->getSourceRange();
7097 continue;
7098 }
7099 } else if (OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007100 auto BaseType = OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
7101 if (auto *ATy = BaseType->getAsArrayTypeUnsafe())
7102 Type = ATy->getElementType();
7103 else
7104 Type = BaseType->getPointeeType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007105 auto *Base = OASE->getBase()->IgnoreParenImpCasts();
7106 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
7107 Base = TempOASE->getBase()->IgnoreParenImpCasts();
7108 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
7109 Base = TempASE->getBase()->IgnoreParenImpCasts();
7110 DE = dyn_cast<DeclRefExpr>(Base);
7111 if (DE)
7112 VD = dyn_cast<VarDecl>(DE->getDecl());
7113 if (!VD) {
7114 Diag(Base->getExprLoc(), diag::err_omp_expected_base_var_name)
7115 << 1 << Base->getSourceRange();
7116 continue;
7117 }
Alexey Bataeva1764212015-09-30 09:22:36 +00007118 }
7119
Alexey Bataevc5e02582014-06-16 07:08:35 +00007120 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
7121 // A variable that appears in a private clause must not have an incomplete
7122 // type or a reference type.
7123 if (RequireCompleteType(ELoc, Type,
7124 diag::err_omp_reduction_incomplete_type))
7125 continue;
7126 // OpenMP [2.14.3.6, reduction clause, Restrictions]
7127 // Arrays may not appear in a reduction clause.
7128 if (Type.getNonReferenceType()->isArrayType()) {
7129 Diag(ELoc, diag::err_omp_reduction_type_array) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007130 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007131 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7132 VarDecl::DeclarationOnly;
7133 Diag(VD->getLocation(),
7134 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7135 << VD;
7136 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007137 continue;
7138 }
7139 // OpenMP [2.14.3.6, reduction clause, Restrictions]
7140 // A list item that appears in a reduction clause must not be
7141 // const-qualified.
7142 if (Type.getNonReferenceType().isConstant(Context)) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007143 Diag(ELoc, diag::err_omp_const_reduction_list_item)
Alexey Bataevc5e02582014-06-16 07:08:35 +00007144 << getOpenMPClauseName(OMPC_reduction) << Type << ERange;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007145 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007146 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7147 VarDecl::DeclarationOnly;
7148 Diag(VD->getLocation(),
7149 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7150 << VD;
7151 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007152 continue;
7153 }
7154 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
7155 // If a list-item is a reference type then it must bind to the same object
7156 // for all threads of the team.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007157 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007158 VarDecl *VDDef = VD->getDefinition();
7159 if (Type->isReferenceType() && VDDef) {
7160 DSARefChecker Check(DSAStack);
7161 if (Check.Visit(VDDef->getInit())) {
7162 Diag(ELoc, diag::err_omp_reduction_ref_type_arg) << ERange;
7163 Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
7164 continue;
7165 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007166 }
7167 }
7168 // OpenMP [2.14.3.6, reduction clause, Restrictions]
7169 // The type of a list item that appears in a reduction clause must be valid
7170 // for the reduction-identifier. For a max or min reduction in C, the type
7171 // of the list item must be an allowed arithmetic data type: char, int,
7172 // float, double, or _Bool, possibly modified with long, short, signed, or
7173 // unsigned. For a max or min reduction in C++, the type of the list item
7174 // must be an allowed arithmetic data type: char, wchar_t, int, float,
7175 // double, or bool, possibly modified with long, short, signed, or unsigned.
7176 if ((BOK == BO_GT || BOK == BO_LT) &&
7177 !(Type->isScalarType() ||
7178 (getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
7179 Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
7180 << getLangOpts().CPlusPlus;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007181 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007182 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7183 VarDecl::DeclarationOnly;
7184 Diag(VD->getLocation(),
7185 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7186 << VD;
7187 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007188 continue;
7189 }
7190 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
7191 !getLangOpts().CPlusPlus && Type->isFloatingType()) {
7192 Diag(ELoc, diag::err_omp_clause_floating_type_arg);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007193 if (!ASE && !OASE) {
Alexey Bataeva1764212015-09-30 09:22:36 +00007194 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7195 VarDecl::DeclarationOnly;
7196 Diag(VD->getLocation(),
7197 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7198 << VD;
7199 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007200 continue;
7201 }
Alexey Bataevc5e02582014-06-16 07:08:35 +00007202 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
7203 // in a Construct]
7204 // Variables with the predetermined data-sharing attributes may not be
7205 // listed in data-sharing attributes clauses, except for the cases
7206 // listed below. For these exceptions only, listing a predetermined
7207 // variable in a data-sharing attribute clause is allowed and overrides
7208 // the variable's predetermined data-sharing attributes.
7209 // OpenMP [2.14.3.6, Restrictions, p.3]
7210 // Any number of reduction clauses can be specified on the directive,
7211 // but a list item can appear only once in the reduction clauses for that
7212 // directive.
Alexey Bataeva1764212015-09-30 09:22:36 +00007213 DSAStackTy::DSAVarData DVar;
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007214 DVar = DSAStack->getTopDSA(VD, false);
7215 if (DVar.CKind == OMPC_reduction) {
7216 Diag(ELoc, diag::err_omp_once_referenced)
7217 << getOpenMPClauseName(OMPC_reduction);
7218 if (DVar.RefExpr) {
7219 Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007220 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007221 } else if (DVar.CKind != OMPC_unknown) {
7222 Diag(ELoc, diag::err_omp_wrong_dsa)
7223 << getOpenMPClauseName(DVar.CKind)
7224 << getOpenMPClauseName(OMPC_reduction);
7225 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7226 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007227 }
7228
7229 // OpenMP [2.14.3.6, Restrictions, p.1]
7230 // A list item that appears in a reduction clause of a worksharing
7231 // construct must be shared in the parallel regions to which any of the
7232 // worksharing regions arising from the worksharing construct bind.
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007233 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
7234 if (isOpenMPWorksharingDirective(CurrDir) &&
7235 !isOpenMPParallelDirective(CurrDir)) {
7236 DVar = DSAStack->getImplicitDSA(VD, true);
7237 if (DVar.CKind != OMPC_shared) {
7238 Diag(ELoc, diag::err_omp_required_access)
7239 << getOpenMPClauseName(OMPC_reduction)
7240 << getOpenMPClauseName(OMPC_shared);
7241 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7242 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +00007243 }
7244 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007245
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007246 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007247 auto *LHSVD = buildVarDecl(*this, ELoc, Type, ".reduction.lhs",
7248 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
7249 auto *RHSVD = buildVarDecl(*this, ELoc, Type, VD->getName(),
7250 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
7251 auto PrivateTy = Type;
7252 if (OASE) {
7253 // For array sections only:
7254 // Create pseudo array type for private copy. The size for this array will
7255 // be generated during codegen.
7256 // For array subscripts or single variables Private Ty is the same as Type
7257 // (type of the variable or single array element).
7258 PrivateTy = Context.getVariableArrayType(
7259 Type, new (Context) OpaqueValueExpr(SourceLocation(),
7260 Context.getSizeType(), VK_RValue),
7261 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
7262 }
7263 // Private copy.
7264 auto *PrivateVD = buildVarDecl(*this, ELoc, PrivateTy, VD->getName(),
7265 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007266 // Add initializer for private variable.
7267 Expr *Init = nullptr;
7268 switch (BOK) {
7269 case BO_Add:
7270 case BO_Xor:
7271 case BO_Or:
7272 case BO_LOr:
7273 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
7274 if (Type->isScalarType() || Type->isAnyComplexType()) {
7275 Init = ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00007276 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007277 break;
7278 case BO_Mul:
7279 case BO_LAnd:
7280 if (Type->isScalarType() || Type->isAnyComplexType()) {
7281 // '*' and '&&' reduction ops - initializer is '1'.
7282 Init = ActOnIntegerConstant(ELoc, /*Val=*/1).get();
7283 }
7284 break;
7285 case BO_And: {
7286 // '&' reduction op - initializer is '~0'.
7287 QualType OrigType = Type;
7288 if (auto *ComplexTy = OrigType->getAs<ComplexType>()) {
7289 Type = ComplexTy->getElementType();
7290 }
7291 if (Type->isRealFloatingType()) {
7292 llvm::APFloat InitValue =
7293 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
7294 /*isIEEE=*/true);
7295 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
7296 Type, ELoc);
7297 } else if (Type->isScalarType()) {
7298 auto Size = Context.getTypeSize(Type);
7299 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
7300 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
7301 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
7302 }
7303 if (Init && OrigType->isAnyComplexType()) {
7304 // Init = 0xFFFF + 0xFFFFi;
7305 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
7306 Init = CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
7307 }
7308 Type = OrigType;
7309 break;
7310 }
7311 case BO_LT:
7312 case BO_GT: {
7313 // 'min' reduction op - initializer is 'Largest representable number in
7314 // the reduction list item type'.
7315 // 'max' reduction op - initializer is 'Least representable number in
7316 // the reduction list item type'.
7317 if (Type->isIntegerType() || Type->isPointerType()) {
7318 bool IsSigned = Type->hasSignedIntegerRepresentation();
7319 auto Size = Context.getTypeSize(Type);
7320 QualType IntTy =
7321 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
7322 llvm::APInt InitValue =
7323 (BOK != BO_LT)
7324 ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
7325 : llvm::APInt::getMinValue(Size)
7326 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
7327 : llvm::APInt::getMaxValue(Size);
7328 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
7329 if (Type->isPointerType()) {
7330 // Cast to pointer type.
7331 auto CastExpr = BuildCStyleCastExpr(
7332 SourceLocation(), Context.getTrivialTypeSourceInfo(Type, ELoc),
7333 SourceLocation(), Init);
7334 if (CastExpr.isInvalid())
7335 continue;
7336 Init = CastExpr.get();
Alexey Bataevc5e02582014-06-16 07:08:35 +00007337 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007338 } else if (Type->isRealFloatingType()) {
7339 llvm::APFloat InitValue = llvm::APFloat::getLargest(
7340 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
7341 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
7342 Type, ELoc);
7343 }
7344 break;
7345 }
7346 case BO_PtrMemD:
7347 case BO_PtrMemI:
7348 case BO_MulAssign:
7349 case BO_Div:
7350 case BO_Rem:
7351 case BO_Sub:
7352 case BO_Shl:
7353 case BO_Shr:
7354 case BO_LE:
7355 case BO_GE:
7356 case BO_EQ:
7357 case BO_NE:
7358 case BO_AndAssign:
7359 case BO_XorAssign:
7360 case BO_OrAssign:
7361 case BO_Assign:
7362 case BO_AddAssign:
7363 case BO_SubAssign:
7364 case BO_DivAssign:
7365 case BO_RemAssign:
7366 case BO_ShlAssign:
7367 case BO_ShrAssign:
7368 case BO_Comma:
7369 llvm_unreachable("Unexpected reduction operation");
7370 }
7371 if (Init) {
7372 AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false,
7373 /*TypeMayContainAuto=*/false);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007374 } else
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007375 ActOnUninitializedDecl(RHSVD, /*TypeMayContainAuto=*/false);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007376 if (!RHSVD->hasInit()) {
7377 Diag(ELoc, diag::err_omp_reduction_id_not_compatible) << Type
7378 << ReductionIdRange;
Alexey Bataeva1764212015-09-30 09:22:36 +00007379 if (VD) {
7380 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
7381 VarDecl::DeclarationOnly;
7382 Diag(VD->getLocation(),
7383 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7384 << VD;
7385 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007386 continue;
7387 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007388 // Store initializer for single element in private copy. Will be used during
7389 // codegen.
7390 PrivateVD->setInit(RHSVD->getInit());
7391 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataev39f915b82015-05-08 10:41:21 +00007392 auto *LHSDRE = buildDeclRefExpr(*this, LHSVD, Type, ELoc);
7393 auto *RHSDRE = buildDeclRefExpr(*this, RHSVD, Type, ELoc);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007394 auto *PrivateDRE = buildDeclRefExpr(*this, PrivateVD, PrivateTy, ELoc);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007395 ExprResult ReductionOp =
7396 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(), BOK,
7397 LHSDRE, RHSDRE);
7398 if (ReductionOp.isUsable()) {
Alexey Bataev69a47792015-05-07 03:54:03 +00007399 if (BOK != BO_LT && BOK != BO_GT) {
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007400 ReductionOp =
7401 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
7402 BO_Assign, LHSDRE, ReductionOp.get());
7403 } else {
7404 auto *ConditionalOp = new (Context) ConditionalOperator(
7405 ReductionOp.get(), SourceLocation(), LHSDRE, SourceLocation(),
7406 RHSDRE, Type, VK_LValue, OK_Ordinary);
7407 ReductionOp =
7408 BuildBinOp(DSAStack->getCurScope(), ReductionId.getLocStart(),
7409 BO_Assign, LHSDRE, ConditionalOp);
7410 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007411 ReductionOp = ActOnFinishFullExpr(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00007412 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007413 if (ReductionOp.isInvalid())
7414 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +00007415
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007416 DSAStack->addDSA(VD, DE, OMPC_reduction);
Alexey Bataeva1764212015-09-30 09:22:36 +00007417 Vars.push_back(RefExpr);
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007418 Privates.push_back(PrivateDRE);
Alexey Bataev794ba0d2015-04-10 10:43:45 +00007419 LHSs.push_back(LHSDRE);
7420 RHSs.push_back(RHSDRE);
7421 ReductionOps.push_back(ReductionOp.get());
Alexey Bataevc5e02582014-06-16 07:08:35 +00007422 }
7423
7424 if (Vars.empty())
7425 return nullptr;
7426
7427 return OMPReductionClause::Create(
7428 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, Vars,
Alexey Bataevf24e7b12015-10-08 09:10:53 +00007429 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId, Privates,
7430 LHSs, RHSs, ReductionOps);
Alexey Bataevc5e02582014-06-16 07:08:35 +00007431}
7432
Alexey Bataev182227b2015-08-20 10:54:39 +00007433OMPClause *Sema::ActOnOpenMPLinearClause(
7434 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
7435 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
7436 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +00007437 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007438 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +00007439 SmallVector<Expr *, 8> Inits;
Alexey Bataev182227b2015-08-20 10:54:39 +00007440 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
7441 LinKind == OMPC_LINEAR_unknown) {
7442 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
7443 LinKind = OMPC_LINEAR_val;
7444 }
Alexey Bataeved09d242014-05-28 05:53:51 +00007445 for (auto &RefExpr : VarList) {
7446 assert(RefExpr && "NULL expr in OpenMP linear clause.");
7447 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexander Musman8dba6642014-04-22 13:09:42 +00007448 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007449 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007450 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00007451 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00007452 continue;
7453 }
7454
7455 // OpenMP [2.14.3.7, linear clause]
7456 // A list item that appears in a linear clause is subject to the private
7457 // clause semantics described in Section 2.14.3.3 on page 159 except as
7458 // noted. In addition, the value of the new list item on each iteration
7459 // of the associated loop(s) corresponds to the value of the original
7460 // list item before entering the construct plus the logical number of
7461 // the iteration times linear-step.
7462
Alexey Bataeved09d242014-05-28 05:53:51 +00007463 SourceLocation ELoc = RefExpr->getExprLoc();
Alexander Musman8dba6642014-04-22 13:09:42 +00007464 // OpenMP [2.1, C/C++]
7465 // A list item is a variable name.
7466 // OpenMP [2.14.3.3, Restrictions, p.1]
7467 // A variable that is part of another variable (as an array or
7468 // structure element) cannot appear in a private clause.
Alexey Bataeved09d242014-05-28 05:53:51 +00007469 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexander Musman8dba6642014-04-22 13:09:42 +00007470 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00007471 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexander Musman8dba6642014-04-22 13:09:42 +00007472 continue;
7473 }
7474
7475 VarDecl *VD = cast<VarDecl>(DE->getDecl());
7476
7477 // OpenMP [2.14.3.7, linear clause]
7478 // A list-item cannot appear in more than one linear clause.
7479 // A list-item that appears in a linear clause cannot appear in any
7480 // other data-sharing attribute clause.
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007481 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD, false);
Alexander Musman8dba6642014-04-22 13:09:42 +00007482 if (DVar.RefExpr) {
7483 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
7484 << getOpenMPClauseName(OMPC_linear);
Alexey Bataev7ff55242014-06-19 09:13:45 +00007485 ReportOriginalDSA(*this, DSAStack, VD, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +00007486 continue;
7487 }
7488
7489 QualType QType = VD->getType();
7490 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
7491 // It will be analyzed later.
7492 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007493 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +00007494 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +00007495 continue;
7496 }
7497
7498 // A variable must not have an incomplete type or a reference type.
7499 if (RequireCompleteType(ELoc, QType,
7500 diag::err_omp_linear_incomplete_type)) {
7501 continue;
7502 }
Alexey Bataev1185e192015-08-20 12:15:57 +00007503 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
7504 !QType->isReferenceType()) {
7505 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
7506 << QType << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
7507 continue;
7508 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007509 QType = QType.getNonReferenceType();
Alexander Musman8dba6642014-04-22 13:09:42 +00007510
7511 // A list item must not be const-qualified.
7512 if (QType.isConstant(Context)) {
7513 Diag(ELoc, diag::err_omp_const_variable)
7514 << getOpenMPClauseName(OMPC_linear);
7515 bool IsDecl =
7516 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7517 Diag(VD->getLocation(),
7518 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7519 << VD;
7520 continue;
7521 }
7522
7523 // A list item must be of integral or pointer type.
7524 QType = QType.getUnqualifiedType().getCanonicalType();
7525 const Type *Ty = QType.getTypePtrOrNull();
7526 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
7527 !Ty->isPointerType())) {
7528 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << QType;
7529 bool IsDecl =
7530 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7531 Diag(VD->getLocation(),
7532 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7533 << VD;
7534 continue;
7535 }
7536
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007537 // Build private copy of original var.
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007538 auto *Private = buildVarDecl(*this, ELoc, QType, VD->getName(),
7539 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007540 auto *PrivateRef = buildDeclRefExpr(
7541 *this, Private, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman3276a272015-03-21 10:12:56 +00007542 // Build var to save initial value.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007543 VarDecl *Init = buildVarDecl(*this, ELoc, QType, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00007544 Expr *InitExpr;
7545 if (LinKind == OMPC_LINEAR_uval)
7546 InitExpr = VD->getInit();
7547 else
7548 InitExpr = DE;
7549 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Alexander Musman3276a272015-03-21 10:12:56 +00007550 /*DirectInit*/ false, /*TypeMayContainAuto*/ false);
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007551 auto InitRef = buildDeclRefExpr(
7552 *this, Init, DE->getType().getUnqualifiedType(), DE->getExprLoc());
Alexander Musman8dba6642014-04-22 13:09:42 +00007553 DSAStack->addDSA(VD, DE, OMPC_linear);
7554 Vars.push_back(DE);
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007555 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +00007556 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +00007557 }
7558
7559 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007560 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00007561
7562 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +00007563 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +00007564 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
7565 !Step->isInstantiationDependent() &&
7566 !Step->containsUnexpandedParameterPack()) {
7567 SourceLocation StepLoc = Step->getLocStart();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00007568 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +00007569 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00007570 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00007571 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +00007572
Alexander Musman3276a272015-03-21 10:12:56 +00007573 // Build var to save the step value.
7574 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007575 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +00007576 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007577 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +00007578 ExprResult CalcStep =
7579 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007580 CalcStep = ActOnFinishFullExpr(CalcStep.get());
Alexander Musman3276a272015-03-21 10:12:56 +00007581
Alexander Musman8dba6642014-04-22 13:09:42 +00007582 // Warn about zero linear step (it would be probably better specified as
7583 // making corresponding variables 'const').
7584 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +00007585 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
7586 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +00007587 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
7588 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +00007589 if (!IsConstant && CalcStep.isUsable()) {
7590 // Calculate the step beforehand instead of doing this on each iteration.
7591 // (This is not used if the number of iterations may be kfold-ed).
7592 CalcStepExpr = CalcStep.get();
7593 }
Alexander Musman8dba6642014-04-22 13:09:42 +00007594 }
7595
Alexey Bataev182227b2015-08-20 10:54:39 +00007596 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
7597 ColonLoc, EndLoc, Vars, Privates, Inits,
7598 StepExpr, CalcStepExpr);
Alexander Musman3276a272015-03-21 10:12:56 +00007599}
7600
7601static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
7602 Expr *NumIterations, Sema &SemaRef,
7603 Scope *S) {
7604 // Walk the vars and build update/final expressions for the CodeGen.
7605 SmallVector<Expr *, 8> Updates;
7606 SmallVector<Expr *, 8> Finals;
7607 Expr *Step = Clause.getStep();
7608 Expr *CalcStep = Clause.getCalcStep();
7609 // OpenMP [2.14.3.7, linear clause]
7610 // If linear-step is not specified it is assumed to be 1.
7611 if (Step == nullptr)
7612 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
7613 else if (CalcStep)
7614 Step = cast<BinaryOperator>(CalcStep)->getLHS();
7615 bool HasErrors = false;
7616 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007617 auto CurPrivate = Clause.privates().begin();
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00007618 auto LinKind = Clause.getModifier();
Alexander Musman3276a272015-03-21 10:12:56 +00007619 for (auto &RefExpr : Clause.varlists()) {
7620 Expr *InitExpr = *CurInit;
7621
7622 // Build privatized reference to the current linear var.
7623 auto DE = cast<DeclRefExpr>(RefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +00007624 Expr *CapturedRef;
7625 if (LinKind == OMPC_LINEAR_uval)
7626 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
7627 else
7628 CapturedRef =
7629 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
7630 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
7631 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00007632
7633 // Build update: Var = InitExpr + IV * Step
7634 ExprResult Update =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007635 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
Alexander Musman3276a272015-03-21 10:12:56 +00007636 InitExpr, IV, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007637 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getLocStart(),
7638 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00007639
7640 // Build final: Var = InitExpr + NumIterations * Step
7641 ExprResult Final =
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007642 BuildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
Alexey Bataev39f915b82015-05-08 10:41:21 +00007643 InitExpr, NumIterations, Step, /* Subtract */ false);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007644 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getLocStart(),
7645 /*DiscardedValue=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +00007646 if (!Update.isUsable() || !Final.isUsable()) {
7647 Updates.push_back(nullptr);
7648 Finals.push_back(nullptr);
7649 HasErrors = true;
7650 } else {
7651 Updates.push_back(Update.get());
7652 Finals.push_back(Final.get());
7653 }
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007654 ++CurInit, ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +00007655 }
7656 Clause.setUpdates(Updates);
7657 Clause.setFinals(Finals);
7658 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +00007659}
7660
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007661OMPClause *Sema::ActOnOpenMPAlignedClause(
7662 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
7663 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
7664
7665 SmallVector<Expr *, 8> Vars;
7666 for (auto &RefExpr : VarList) {
7667 assert(RefExpr && "NULL expr in OpenMP aligned clause.");
7668 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7669 // It will be analyzed later.
7670 Vars.push_back(RefExpr);
7671 continue;
7672 }
7673
7674 SourceLocation ELoc = RefExpr->getExprLoc();
7675 // OpenMP [2.1, C/C++]
7676 // A list item is a variable name.
7677 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
7678 if (!DE || !isa<VarDecl>(DE->getDecl())) {
7679 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
7680 continue;
7681 }
7682
7683 VarDecl *VD = cast<VarDecl>(DE->getDecl());
7684
7685 // OpenMP [2.8.1, simd construct, Restrictions]
7686 // The type of list items appearing in the aligned clause must be
7687 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007688 QualType QType = VD->getType();
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007689 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +00007690 const Type *Ty = QType.getTypePtrOrNull();
7691 if (!Ty || (!Ty->isDependentType() && !Ty->isArrayType() &&
7692 !Ty->isPointerType())) {
7693 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
7694 << QType << getLangOpts().CPlusPlus << RefExpr->getSourceRange();
7695 bool IsDecl =
7696 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7697 Diag(VD->getLocation(),
7698 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7699 << VD;
7700 continue;
7701 }
7702
7703 // OpenMP [2.8.1, simd construct, Restrictions]
7704 // A list-item cannot appear in more than one aligned clause.
7705 if (DeclRefExpr *PrevRef = DSAStack->addUniqueAligned(VD, DE)) {
7706 Diag(ELoc, diag::err_omp_aligned_twice) << RefExpr->getSourceRange();
7707 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
7708 << getOpenMPClauseName(OMPC_aligned);
7709 continue;
7710 }
7711
7712 Vars.push_back(DE);
7713 }
7714
7715 // OpenMP [2.8.1, simd construct, Description]
7716 // The parameter of the aligned clause, alignment, must be a constant
7717 // positive integer expression.
7718 // If no optional parameter is specified, implementation-defined default
7719 // alignments for SIMD instructions on the target platforms are assumed.
7720 if (Alignment != nullptr) {
7721 ExprResult AlignResult =
7722 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
7723 if (AlignResult.isInvalid())
7724 return nullptr;
7725 Alignment = AlignResult.get();
7726 }
7727 if (Vars.empty())
7728 return nullptr;
7729
7730 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
7731 EndLoc, Vars, Alignment);
7732}
7733
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007734OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
7735 SourceLocation StartLoc,
7736 SourceLocation LParenLoc,
7737 SourceLocation EndLoc) {
7738 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007739 SmallVector<Expr *, 8> SrcExprs;
7740 SmallVector<Expr *, 8> DstExprs;
7741 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeved09d242014-05-28 05:53:51 +00007742 for (auto &RefExpr : VarList) {
7743 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
7744 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007745 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00007746 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007747 SrcExprs.push_back(nullptr);
7748 DstExprs.push_back(nullptr);
7749 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007750 continue;
7751 }
7752
Alexey Bataeved09d242014-05-28 05:53:51 +00007753 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007754 // OpenMP [2.1, C/C++]
7755 // A list item is a variable name.
7756 // OpenMP [2.14.4.1, Restrictions, p.1]
7757 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeved09d242014-05-28 05:53:51 +00007758 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007759 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataeved09d242014-05-28 05:53:51 +00007760 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007761 continue;
7762 }
7763
7764 Decl *D = DE->getDecl();
7765 VarDecl *VD = cast<VarDecl>(D);
7766
7767 QualType Type = VD->getType();
7768 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
7769 // It will be analyzed later.
7770 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007771 SrcExprs.push_back(nullptr);
7772 DstExprs.push_back(nullptr);
7773 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007774 continue;
7775 }
7776
7777 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
7778 // A list item that appears in a copyin clause must be threadprivate.
7779 if (!DSAStack->isThreadPrivate(VD)) {
7780 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +00007781 << getOpenMPClauseName(OMPC_copyin)
7782 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007783 continue;
7784 }
7785
7786 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
7787 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +00007788 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007789 // operator for the class type.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007790 auto ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007791 auto *SrcVD =
7792 buildVarDecl(*this, DE->getLocStart(), ElemType.getUnqualifiedType(),
7793 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev39f915b82015-05-08 10:41:21 +00007794 auto *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007795 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
7796 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007797 buildVarDecl(*this, DE->getLocStart(), ElemType, ".copyin.dst",
7798 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007799 auto *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +00007800 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007801 // For arrays generate assignment operation for single element and replace
7802 // it by the original array element in CodeGen.
7803 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
7804 PseudoDstExpr, PseudoSrcExpr);
7805 if (AssignmentOp.isInvalid())
7806 continue;
7807 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
7808 /*DiscardedValue=*/true);
7809 if (AssignmentOp.isInvalid())
7810 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007811
7812 DSAStack->addDSA(VD, DE, OMPC_copyin);
7813 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007814 SrcExprs.push_back(PseudoSrcExpr);
7815 DstExprs.push_back(PseudoDstExpr);
7816 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007817 }
7818
Alexey Bataeved09d242014-05-28 05:53:51 +00007819 if (Vars.empty())
7820 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007821
Alexey Bataevf56f98c2015-04-16 05:39:01 +00007822 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
7823 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +00007824}
7825
Alexey Bataevbae9a792014-06-27 10:37:06 +00007826OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
7827 SourceLocation StartLoc,
7828 SourceLocation LParenLoc,
7829 SourceLocation EndLoc) {
7830 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +00007831 SmallVector<Expr *, 8> SrcExprs;
7832 SmallVector<Expr *, 8> DstExprs;
7833 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataevbae9a792014-06-27 10:37:06 +00007834 for (auto &RefExpr : VarList) {
7835 assert(RefExpr && "NULL expr in OpenMP copyprivate clause.");
7836 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7837 // It will be analyzed later.
7838 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +00007839 SrcExprs.push_back(nullptr);
7840 DstExprs.push_back(nullptr);
7841 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00007842 continue;
7843 }
7844
7845 SourceLocation ELoc = RefExpr->getExprLoc();
7846 // OpenMP [2.1, C/C++]
7847 // A list item is a variable name.
7848 // OpenMP [2.14.4.1, Restrictions, p.1]
7849 // A list item that appears in a copyin clause must be threadprivate.
7850 DeclRefExpr *DE = dyn_cast<DeclRefExpr>(RefExpr);
7851 if (!DE || !isa<VarDecl>(DE->getDecl())) {
7852 Diag(ELoc, diag::err_omp_expected_var_name) << RefExpr->getSourceRange();
7853 continue;
7854 }
7855
7856 Decl *D = DE->getDecl();
7857 VarDecl *VD = cast<VarDecl>(D);
7858
7859 QualType Type = VD->getType();
7860 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
7861 // It will be analyzed later.
7862 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00007863 SrcExprs.push_back(nullptr);
7864 DstExprs.push_back(nullptr);
7865 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +00007866 continue;
7867 }
7868
7869 // OpenMP [2.14.4.2, Restrictions, p.2]
7870 // A list item that appears in a copyprivate clause may not appear in a
7871 // private or firstprivate clause on the single construct.
7872 if (!DSAStack->isThreadPrivate(VD)) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007873 auto DVar = DSAStack->getTopDSA(VD, false);
Alexey Bataeva63048e2015-03-23 06:18:07 +00007874 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
7875 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +00007876 Diag(ELoc, diag::err_omp_wrong_dsa)
7877 << getOpenMPClauseName(DVar.CKind)
7878 << getOpenMPClauseName(OMPC_copyprivate);
7879 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7880 continue;
7881 }
7882
7883 // OpenMP [2.11.4.2, Restrictions, p.1]
7884 // All list items that appear in a copyprivate clause must be either
7885 // threadprivate or private in the enclosing context.
7886 if (DVar.CKind == OMPC_unknown) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00007887 DVar = DSAStack->getImplicitDSA(VD, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +00007888 if (DVar.CKind == OMPC_shared) {
7889 Diag(ELoc, diag::err_omp_required_access)
7890 << getOpenMPClauseName(OMPC_copyprivate)
7891 << "threadprivate or private in the enclosing context";
7892 ReportOriginalDSA(*this, DSAStack, VD, DVar);
7893 continue;
7894 }
7895 }
7896 }
7897
Alexey Bataev7a3e5852015-05-19 08:19:24 +00007898 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00007899 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +00007900 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007901 << getOpenMPClauseName(OMPC_copyprivate) << Type
7902 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +00007903 bool IsDecl =
7904 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
7905 Diag(VD->getLocation(),
7906 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
7907 << VD;
7908 continue;
7909 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +00007910
Alexey Bataevbae9a792014-06-27 10:37:06 +00007911 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
7912 // A variable of class type (or array thereof) that appears in a
7913 // copyin clause requires an accessible, unambiguous copy assignment
7914 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +00007915 Type = Context.getBaseElementType(Type.getNonReferenceType())
7916 .getUnqualifiedType();
Alexey Bataev420d45b2015-04-14 05:11:24 +00007917 auto *SrcVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007918 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.src",
7919 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00007920 auto *PseudoSrcExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007921 buildDeclRefExpr(*this, SrcVD, Type, DE->getExprLoc());
Alexey Bataev420d45b2015-04-14 05:11:24 +00007922 auto *DstVD =
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00007923 buildVarDecl(*this, DE->getLocStart(), Type, ".copyprivate.dst",
7924 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataev420d45b2015-04-14 05:11:24 +00007925 auto *PseudoDstExpr =
Alexey Bataev39f915b82015-05-08 10:41:21 +00007926 buildDeclRefExpr(*this, DstVD, Type, DE->getExprLoc());
Alexey Bataeva63048e2015-03-23 06:18:07 +00007927 auto AssignmentOp = BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign,
7928 PseudoDstExpr, PseudoSrcExpr);
7929 if (AssignmentOp.isInvalid())
7930 continue;
7931 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
7932 /*DiscardedValue=*/true);
7933 if (AssignmentOp.isInvalid())
7934 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +00007935
7936 // No need to mark vars as copyprivate, they are already threadprivate or
7937 // implicitly private.
7938 Vars.push_back(DE);
Alexey Bataeva63048e2015-03-23 06:18:07 +00007939 SrcExprs.push_back(PseudoSrcExpr);
7940 DstExprs.push_back(PseudoDstExpr);
7941 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +00007942 }
7943
7944 if (Vars.empty())
7945 return nullptr;
7946
Alexey Bataeva63048e2015-03-23 06:18:07 +00007947 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
7948 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +00007949}
7950
Alexey Bataev6125da92014-07-21 11:26:11 +00007951OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
7952 SourceLocation StartLoc,
7953 SourceLocation LParenLoc,
7954 SourceLocation EndLoc) {
7955 if (VarList.empty())
7956 return nullptr;
7957
7958 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
7959}
Alexey Bataevdea47612014-07-23 07:46:59 +00007960
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00007961OMPClause *
7962Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
7963 SourceLocation DepLoc, SourceLocation ColonLoc,
7964 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
7965 SourceLocation LParenLoc, SourceLocation EndLoc) {
7966 if (DepKind == OMPC_DEPEND_unknown) {
7967 std::string Values;
7968 std::string Sep(", ");
7969 for (unsigned i = 0; i < OMPC_DEPEND_unknown; ++i) {
7970 Values += "'";
7971 Values += getOpenMPSimpleClauseTypeName(OMPC_depend, i);
7972 Values += "'";
7973 switch (i) {
7974 case OMPC_DEPEND_unknown - 2:
7975 Values += " or ";
7976 break;
7977 case OMPC_DEPEND_unknown - 1:
7978 break;
7979 default:
7980 Values += Sep;
7981 break;
7982 }
7983 }
7984 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
7985 << Values << getOpenMPClauseName(OMPC_depend);
7986 return nullptr;
7987 }
7988 SmallVector<Expr *, 8> Vars;
7989 for (auto &RefExpr : VarList) {
7990 assert(RefExpr && "NULL expr in OpenMP shared clause.");
7991 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
7992 // It will be analyzed later.
7993 Vars.push_back(RefExpr);
7994 continue;
7995 }
7996
7997 SourceLocation ELoc = RefExpr->getExprLoc();
7998 // OpenMP [2.11.1.1, Restrictions, p.3]
7999 // A variable that is part of another variable (such as a field of a
8000 // structure) but is not an array element or an array section cannot appear
8001 // in a depend clause.
8002 auto *SimpleExpr = RefExpr->IgnoreParenCasts();
Alexey Bataev1a3320e2015-08-25 14:24:04 +00008003 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
8004 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
8005 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
8006 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
8007 (!ASE && !DE && !OASE) || (DE && !isa<VarDecl>(DE->getDecl())) ||
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008008 (ASE && !ASE->getBase()->getType()->isAnyPointerType() &&
8009 !ASE->getBase()->getType()->isArrayType())) {
8010 Diag(ELoc, diag::err_omp_expected_var_name_or_array_item)
8011 << RefExpr->getSourceRange();
8012 continue;
8013 }
8014
8015 Vars.push_back(RefExpr->IgnoreParenImpCasts());
8016 }
8017
8018 if (Vars.empty())
8019 return nullptr;
8020
8021 return OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc, DepKind,
8022 DepLoc, ColonLoc, Vars);
8023}
Michael Wonge710d542015-08-07 16:16:36 +00008024
8025OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
8026 SourceLocation LParenLoc,
8027 SourceLocation EndLoc) {
8028 Expr *ValExpr = Device;
Michael Wonge710d542015-08-07 16:16:36 +00008029
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008030 // OpenMP [2.9.1, Restrictions]
8031 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00008032 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
8033 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008034 return nullptr;
8035
Michael Wonge710d542015-08-07 16:16:36 +00008036 return new (Context) OMPDeviceClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8037}
Kelvin Li0bff7af2015-11-23 05:32:03 +00008038
8039static bool IsCXXRecordForMappable(Sema &SemaRef, SourceLocation Loc,
8040 DSAStackTy *Stack, CXXRecordDecl *RD) {
8041 if (!RD || RD->isInvalidDecl())
8042 return true;
8043
8044 auto QTy = SemaRef.Context.getRecordType(RD);
8045 if (RD->isDynamicClass()) {
8046 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8047 SemaRef.Diag(RD->getLocation(), diag::note_omp_polymorphic_in_target);
8048 return false;
8049 }
8050 auto *DC = RD;
8051 bool IsCorrect = true;
8052 for (auto *I : DC->decls()) {
8053 if (I) {
8054 if (auto *MD = dyn_cast<CXXMethodDecl>(I)) {
8055 if (MD->isStatic()) {
8056 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8057 SemaRef.Diag(MD->getLocation(),
8058 diag::note_omp_static_member_in_target);
8059 IsCorrect = false;
8060 }
8061 } else if (auto *VD = dyn_cast<VarDecl>(I)) {
8062 if (VD->isStaticDataMember()) {
8063 SemaRef.Diag(Loc, diag::err_omp_not_mappable_type) << QTy;
8064 SemaRef.Diag(VD->getLocation(),
8065 diag::note_omp_static_member_in_target);
8066 IsCorrect = false;
8067 }
8068 }
8069 }
8070 }
8071
8072 for (auto &I : RD->bases()) {
8073 if (!IsCXXRecordForMappable(SemaRef, I.getLocStart(), Stack,
8074 I.getType()->getAsCXXRecordDecl()))
8075 IsCorrect = false;
8076 }
8077 return IsCorrect;
8078}
8079
8080static bool CheckTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
8081 DSAStackTy *Stack, QualType QTy) {
8082 NamedDecl *ND;
8083 if (QTy->isIncompleteType(&ND)) {
8084 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
8085 return false;
8086 } else if (CXXRecordDecl *RD = dyn_cast_or_null<CXXRecordDecl>(ND)) {
8087 if (!RD->isInvalidDecl() &&
8088 !IsCXXRecordForMappable(SemaRef, SL, Stack, RD))
8089 return false;
8090 }
8091 return true;
8092}
8093
8094OMPClause *Sema::ActOnOpenMPMapClause(
8095 OpenMPMapClauseKind MapTypeModifier, OpenMPMapClauseKind MapType,
8096 SourceLocation MapLoc, SourceLocation ColonLoc, ArrayRef<Expr *> VarList,
8097 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
8098 SmallVector<Expr *, 4> Vars;
8099
8100 for (auto &RE : VarList) {
8101 assert(RE && "Null expr in omp map");
8102 if (isa<DependentScopeDeclRefExpr>(RE)) {
8103 // It will be analyzed later.
8104 Vars.push_back(RE);
8105 continue;
8106 }
8107 SourceLocation ELoc = RE->getExprLoc();
8108
8109 // OpenMP [2.14.5, Restrictions]
8110 // A variable that is part of another variable (such as field of a
8111 // structure) but is not an array element or an array section cannot appear
8112 // in a map clause.
8113 auto *VE = RE->IgnoreParenLValueCasts();
8114
8115 if (VE->isValueDependent() || VE->isTypeDependent() ||
8116 VE->isInstantiationDependent() ||
8117 VE->containsUnexpandedParameterPack()) {
8118 // It will be analyzed later.
8119 Vars.push_back(RE);
8120 continue;
8121 }
8122
8123 auto *SimpleExpr = RE->IgnoreParenCasts();
8124 auto *DE = dyn_cast<DeclRefExpr>(SimpleExpr);
8125 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
8126 auto *OASE = dyn_cast<OMPArraySectionExpr>(SimpleExpr);
8127
8128 if (!RE->IgnoreParenImpCasts()->isLValue() ||
8129 (!OASE && !ASE && !DE) ||
8130 (DE && !isa<VarDecl>(DE->getDecl())) ||
8131 (ASE && !ASE->getBase()->getType()->isAnyPointerType() &&
8132 !ASE->getBase()->getType()->isArrayType())) {
8133 Diag(ELoc, diag::err_omp_expected_var_name_or_array_item)
8134 << RE->getSourceRange();
8135 continue;
8136 }
8137
8138 Decl *D = nullptr;
8139 if (DE) {
8140 D = DE->getDecl();
8141 } else if (ASE) {
8142 auto *B = ASE->getBase()->IgnoreParenCasts();
8143 D = dyn_cast<DeclRefExpr>(B)->getDecl();
8144 } else if (OASE) {
8145 auto *B = OASE->getBase();
8146 D = dyn_cast<DeclRefExpr>(B)->getDecl();
8147 }
8148 assert(D && "Null decl on map clause.");
8149 auto *VD = cast<VarDecl>(D);
8150
8151 // OpenMP [2.14.5, Restrictions, p.8]
8152 // threadprivate variables cannot appear in a map clause.
8153 if (DSAStack->isThreadPrivate(VD)) {
8154 auto DVar = DSAStack->getTopDSA(VD, false);
8155 Diag(ELoc, diag::err_omp_threadprivate_in_map);
8156 ReportOriginalDSA(*this, DSAStack, VD, DVar);
8157 continue;
8158 }
8159
8160 // OpenMP [2.14.5, Restrictions, p.2]
8161 // At most one list item can be an array item derived from a given variable
8162 // in map clauses of the same construct.
8163 // OpenMP [2.14.5, Restrictions, p.3]
8164 // List items of map clauses in the same construct must not share original
8165 // storage.
8166 // OpenMP [2.14.5, Restrictions, C/C++, p.2]
8167 // A variable for which the type is pointer, reference to array, or
8168 // reference to pointer and an array section derived from that variable
8169 // must not appear as list items of map clauses of the same construct.
8170 DSAStackTy::MapInfo MI = DSAStack->IsMappedInCurrentRegion(VD);
8171 if (MI.RefExpr) {
8172 Diag(ELoc, diag::err_omp_map_shared_storage) << ELoc;
8173 Diag(MI.RefExpr->getExprLoc(), diag::note_used_here)
8174 << MI.RefExpr->getSourceRange();
8175 continue;
8176 }
8177
8178 // OpenMP [2.14.5, Restrictions, C/C++, p.3,4]
8179 // A variable for which the type is pointer, reference to array, or
8180 // reference to pointer must not appear as a list item if the enclosing
8181 // device data environment already contains an array section derived from
8182 // that variable.
8183 // An array section derived from a variable for which the type is pointer,
8184 // reference to array, or reference to pointer must not appear as a list
8185 // item if the enclosing device data environment already contains that
8186 // variable.
8187 QualType Type = VD->getType();
8188 MI = DSAStack->getMapInfoForVar(VD);
8189 if (MI.RefExpr && (isa<DeclRefExpr>(MI.RefExpr->IgnoreParenLValueCasts()) !=
8190 isa<DeclRefExpr>(VE)) &&
8191 (Type->isPointerType() || Type->isReferenceType())) {
8192 Diag(ELoc, diag::err_omp_map_shared_storage) << ELoc;
8193 Diag(MI.RefExpr->getExprLoc(), diag::note_used_here)
8194 << MI.RefExpr->getSourceRange();
8195 continue;
8196 }
8197
8198 // OpenMP [2.14.5, Restrictions, C/C++, p.7]
8199 // A list item must have a mappable type.
8200 if (!CheckTypeMappable(VE->getExprLoc(), VE->getSourceRange(), *this,
8201 DSAStack, Type))
8202 continue;
8203
8204 Vars.push_back(RE);
8205 MI.RefExpr = RE;
8206 DSAStack->addMapInfoForVar(VD, MI);
8207 }
8208 if (Vars.empty())
8209 return nullptr;
8210
8211 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
8212 MapTypeModifier, MapType, MapLoc);
8213}
Kelvin Li099bb8c2015-11-24 20:50:12 +00008214
8215OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
8216 SourceLocation StartLoc,
8217 SourceLocation LParenLoc,
8218 SourceLocation EndLoc) {
8219 Expr *ValExpr = NumTeams;
Kelvin Li099bb8c2015-11-24 20:50:12 +00008220
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008221 // OpenMP [teams Constrcut, Restrictions]
8222 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00008223 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
8224 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008225 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +00008226
8227 return new (Context) OMPNumTeamsClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8228}
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008229
8230OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
8231 SourceLocation StartLoc,
8232 SourceLocation LParenLoc,
8233 SourceLocation EndLoc) {
8234 Expr *ValExpr = ThreadLimit;
8235
8236 // OpenMP [teams Constrcut, Restrictions]
8237 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeva0569352015-12-01 10:17:31 +00008238 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
8239 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008240 return nullptr;
8241
8242 return new (Context) OMPThreadLimitClause(ValExpr, StartLoc, LParenLoc,
8243 EndLoc);
8244}
Alexey Bataeva0569352015-12-01 10:17:31 +00008245
8246OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
8247 SourceLocation StartLoc,
8248 SourceLocation LParenLoc,
8249 SourceLocation EndLoc) {
8250 Expr *ValExpr = Priority;
8251
8252 // OpenMP [2.9.1, task Constrcut]
8253 // The priority-value is a non-negative numerical scalar expression.
8254 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
8255 /*StrictlyPositive=*/false))
8256 return nullptr;
8257
8258 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8259}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008260
8261OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
8262 SourceLocation StartLoc,
8263 SourceLocation LParenLoc,
8264 SourceLocation EndLoc) {
8265 Expr *ValExpr = Grainsize;
8266
8267 // OpenMP [2.9.2, taskloop Constrcut]
8268 // The parameter of the grainsize clause must be a positive integer
8269 // expression.
8270 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
8271 /*StrictlyPositive=*/true))
8272 return nullptr;
8273
8274 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8275}
Alexey Bataev382967a2015-12-08 12:06:20 +00008276
8277OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
8278 SourceLocation StartLoc,
8279 SourceLocation LParenLoc,
8280 SourceLocation EndLoc) {
8281 Expr *ValExpr = NumTasks;
8282
8283 // OpenMP [2.9.2, taskloop Constrcut]
8284 // The parameter of the num_tasks clause must be a positive integer
8285 // expression.
8286 if (!IsNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
8287 /*StrictlyPositive=*/true))
8288 return nullptr;
8289
8290 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8291}
8292
Alexey Bataev28c75412015-12-15 08:19:24 +00008293OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
8294 SourceLocation LParenLoc,
8295 SourceLocation EndLoc) {
8296 // OpenMP [2.13.2, critical construct, Description]
8297 // ... where hint-expression is an integer constant expression that evaluates
8298 // to a valid lock hint.
8299 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
8300 if (HintExpr.isInvalid())
8301 return nullptr;
8302 return new (Context)
8303 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
8304}
8305